| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var useState = wp.element.useState; |
| 4 |
var Fragment = wp.element.Fragment; |
| 5 |
var registerBlockType = wp.blocks.registerBlockType; |
| 6 |
var __ = wp.i18n.__; |
| 7 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 8 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 9 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 10 |
var PanelBody = wp.components.PanelBody; |
| 11 |
var RangeControl = wp.components.RangeControl; |
| 12 |
var SelectControl = wp.components.SelectControl; |
| 13 |
var TextControl = wp.components.TextControl; |
| 14 |
var ToggleControl = wp.components.ToggleControl; |
| 15 |
var Button = wp.components.Button; |
| 16 |
|
| 17 |
var _tc, _tv; |
| 18 |
function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 19 |
function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 20 |
|
| 21 |
/* ── Calculation helpers ─────────────────────────────────────────────── */ |
| 22 |
function pad2(n) { return String(Math.floor(n)).padStart(2, '0'); } |
| 23 |
function secsToHMS(s) { |
| 24 |
s = Math.round(Math.max(0, s)); |
| 25 |
var h = Math.floor(s / 3600); var m = Math.floor((s % 3600) / 60); var sec = s % 60; |
| 26 |
return (h > 0 ? h + ':' : '') + (h > 0 ? pad2(m) : m) + ':' + pad2(sec); |
| 27 |
} |
| 28 |
function secsToPace(s) { |
| 29 |
var m = Math.floor(s / 60); var sec = Math.round(s % 60); |
| 30 |
return m + ':' + pad2(sec); |
| 31 |
} |
| 32 |
// pace string "M:SS" -> seconds |
| 33 |
function paceToSecs(str) { |
| 34 |
var parts = String(str).split(':'); |
| 35 |
if (parts.length < 2) return parseFloat(parts[0]) * 60 || 0; |
| 36 |
return parseInt(parts[0],10) * 60 + parseInt(parts[1],10); |
| 37 |
} |
| 38 |
// time "H:MM:SS" or "M:SS" -> seconds |
| 39 |
function timeToSecs(str) { |
| 40 |
var parts = String(str).split(':'); |
| 41 |
if (parts.length === 3) return parseInt(parts[0])*3600 + parseInt(parts[1])*60 + parseInt(parts[2]); |
| 42 |
if (parts.length === 2) return parseInt(parts[0])*60 + parseInt(parts[1]); |
| 43 |
return parseFloat(str) * 60 || 0; |
| 44 |
} |
| 45 |
|
| 46 |
var KM_PER_MILE = 1.60934; |
| 47 |
|
| 48 |
/* find pace (min/unit) given distance (units) and time (secs) */ |
| 49 |
function findPace(dist, secs) { |
| 50 |
if (dist <= 0) return null; |
| 51 |
return secs / dist; // secs per unit |
| 52 |
} |
| 53 |
/* find total time given dist and pace (secs/unit) */ |
| 54 |
function findTime(dist, paceSecs) { |
| 55 |
return dist * paceSecs; |
| 56 |
} |
| 57 |
/* find distance given time and pace */ |
| 58 |
function findDist(timeSecs, paceSecs) { |
| 59 |
if (paceSecs <= 0) return null; |
| 60 |
return timeSecs / paceSecs; |
| 61 |
} |
| 62 |
|
| 63 |
function PacePreview(props) { |
| 64 |
var a = props.attributes; |
| 65 |
var sa = props.setAttributes; |
| 66 |
|
| 67 |
var _mode = useState(a.defaultMode || 'find_pace'); var mode = _mode[0]; var setMode = _mode[1]; |
| 68 |
var _unit = useState(a.defaultUnit || 'km'); var unit = _unit[0]; var setUnit = _unit[1]; |
| 69 |
// inputs |
| 70 |
var _dist = useState('10'); var dist = _dist[0]; var setDist = _dist[1]; |
| 71 |
var _time = useState('1:00:00');var time = _time[0]; var setTime = _time[1]; |
| 72 |
var _pace = useState('6:00'); var pace = _pace[0]; var setPace = _pace[1]; |
| 73 |
|
| 74 |
var pu = unit === 'km' ? 'km' : 'mi'; |
| 75 |
var accent = a.accentColor || '#6c3fb5'; |
| 76 |
|
| 77 |
function computeResult() { |
| 78 |
var distN = parseFloat(dist) || 0; |
| 79 |
var timeSec= timeToSecs(time); |
| 80 |
var paceSec= paceToSecs(pace); |
| 81 |
if (mode === 'find_pace') { |
| 82 |
var ps = findPace(distN, timeSec); |
| 83 |
if (!ps) return null; |
| 84 |
var speedKmh = unit === 'km' ? 3600 / ps : 3600 / ps / KM_PER_MILE; |
| 85 |
return { label: 'Your Pace', main: secsToPace(ps), sub: '/' + pu, extra: 'Speed: ' + speedKmh.toFixed(2) + ' km/h' }; |
| 86 |
} |
| 87 |
if (mode === 'find_time') { |
| 88 |
var ts = findTime(distN, paceSec); |
| 89 |
var spd2 = unit === 'km' ? 3600 / paceSec : 3600 / paceSec / KM_PER_MILE; |
| 90 |
return { label: 'Finish Time', main: secsToHMS(ts), sub: '', extra: 'Speed: ' + spd2.toFixed(2) + ' km/h' }; |
| 91 |
} |
| 92 |
// find_dist |
| 93 |
var d = findDist(timeSec, paceSec); |
| 94 |
if (!d) return null; |
| 95 |
return { label: 'Distance', main: d.toFixed(2), sub: pu, extra: '' }; |
| 96 |
} |
| 97 |
var result = computeResult(); |
| 98 |
|
| 99 |
var modes = [ |
| 100 |
{ key: 'find_pace', label: 'Find Pace' }, |
| 101 |
{ key: 'find_time', label: 'Find Time' }, |
| 102 |
{ key: 'find_dist', label: 'Find Distance' } |
| 103 |
]; |
| 104 |
var units = [{ key: 'km', label: 'Kilometers' }, { key: 'mi', label: 'Miles' }]; |
| 105 |
|
| 106 |
function tabBtn(label, active, onClick) { |
| 107 |
return el('button', { |
| 108 |
style: { |
| 109 |
flex: 1, padding: '9px 12px', borderRadius: '8px', cursor: 'pointer', |
| 110 |
border: '2px solid ' + (active ? accent : '#e5e7eb'), |
| 111 |
background: active ? accent : 'transparent', |
| 112 |
color: active ? '#fff' : accent, |
| 113 |
fontWeight: 700, fontSize: '13px', transition: 'all .15s', fontFamily: 'inherit' |
| 114 |
}, |
| 115 |
onClick: onClick |
| 116 |
}, label); |
| 117 |
} |
| 118 |
|
| 119 |
var presets = a.presets || [{ label: '5K', dist: 5 }, { label: '10K', dist: 10 }, { label: 'Half', dist: 21.0975 }, { label: 'Marathon', dist: 42.195 }]; |
| 120 |
|
| 121 |
function inputRow(lbl, val, setVal, hint) { |
| 122 |
return el('div', { style: { marginBottom: '14px' } }, |
| 123 |
el('label', { style: { display: 'flex', justifyContent: 'space-between', fontSize: '13px', fontWeight: 600, color: a.labelColor, marginBottom: '5px' } }, |
| 124 |
el('span', null, lbl), |
| 125 |
hint && el('span', { style: { fontWeight: 400, color: '#9ca3af', fontSize: '12px' } }, hint) |
| 126 |
), |
| 127 |
el('input', { |
| 128 |
type: 'text', value: val, |
| 129 |
style: { width: '100%', padding: '10px 14px', borderRadius: a.inputRadius + 'px', border: '1.5px solid #e5e7eb', fontSize: '16px', outline: 'none', boxSizing: 'border-box', fontFamily: 'inherit' }, |
| 130 |
onChange: function(e) { setVal(e.target.value); } |
| 131 |
}) |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
return el('div', { className: 'bkbg-pace-editor', style: { background: a.sectionBg || undefined, paddingTop: a.paddingTop + 'px', paddingBottom: a.paddingBottom + 'px', fontFamily: 'inherit' } }, |
| 136 |
el('div', { style: { background: a.cardBg, borderRadius: a.cardRadius + 'px', padding: '36px 32px', maxWidth: a.maxWidth + 'px', margin: '0 auto', boxShadow: '0 4px 24px rgba(0,0,0,0.09)' } }, |
| 137 |
|
| 138 |
// Title/subtitle |
| 139 |
(a.showTitle || a.showSubtitle) && el('div', { style: { marginBottom: '20px' } }, |
| 140 |
a.showTitle && el('div', { className: 'bkbg-pace-title', style: { color: a.titleColor, marginBottom: '6px', contentEditable: true, suppressContentEditableWarning: true, outline: 'none' }, onBlur: function(e) { sa({ title: e.target.innerText }); }, dangerouslySetInnerHTML: { __html: a.title } }), |
| 141 |
a.showSubtitle && el('div', { style: { fontSize: '15px', color: a.subtitleColor, contentEditable: true, suppressContentEditableWarning: true, outline: 'none' }, onBlur: function(e) { sa({ subtitle: e.target.innerText }); }, dangerouslySetInnerHTML: { __html: a.subtitle } }) |
| 142 |
), |
| 143 |
|
| 144 |
// Mode tabs |
| 145 |
el('div', { style: { display: 'flex', gap: '8px', marginBottom: '14px', flexWrap: 'wrap' } }, |
| 146 |
modes.map(function(m) { return tabBtn(m.label, mode === m.key, function() { setMode(m.key); }); }) |
| 147 |
), |
| 148 |
|
| 149 |
// Unit tabs |
| 150 |
el('div', { style: { display: 'flex', gap: '8px', marginBottom: '20px' } }, |
| 151 |
units.map(function(u) { return tabBtn(u.label, unit === u.key, function() { setUnit(u.key); }); }) |
| 152 |
), |
| 153 |
|
| 154 |
// Preset distance chips |
| 155 |
a.showPresets && (mode === 'find_pace' || mode === 'find_time') && el('div', { style: { display: 'flex', gap: '8px', flexWrap: 'wrap', marginBottom: '20px' } }, |
| 156 |
presets.map(function(p) { |
| 157 |
var d = unit === 'km' ? p.dist : p.dist / KM_PER_MILE; |
| 158 |
return el('button', { |
| 159 |
key: p.label, |
| 160 |
style: { padding: '6px 14px', borderRadius: '100px', border: '1.5px solid #e5e7eb', background: a.presetBg, color: a.presetColor, fontWeight: 600, fontSize: '13px', cursor: 'pointer' }, |
| 161 |
onClick: function() { setDist(String(parseFloat(d.toFixed(3)))); } |
| 162 |
}, p.label); |
| 163 |
}) |
| 164 |
), |
| 165 |
|
| 166 |
// Inputs |
| 167 |
mode !== 'find_dist' && el('div', { style: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '0 20px' } }, |
| 168 |
inputRow('Distance', dist, setDist, pu), |
| 169 |
mode === 'find_pace' ? inputRow('Time', time, setTime, 'H:MM:SS') : inputRow('Pace', pace, setPace, 'M:SS/' + pu) |
| 170 |
), |
| 171 |
mode === 'find_dist' && el('div', { style: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '0 20px' } }, |
| 172 |
inputRow('Time', time, setTime, 'H:MM:SS'), |
| 173 |
inputRow('Pace', pace, setPace, 'M:SS/' + pu) |
| 174 |
), |
| 175 |
|
| 176 |
// Result card |
| 177 |
el('div', { style: { background: a.resultBg, border: '2px solid ' + a.resultBorder, borderRadius: a.cardRadius + 'px', padding: '24px 28px', textAlign: 'center', marginTop: '20px' } }, |
| 178 |
result ? el(Fragment, null, |
| 179 |
el('div', { style: { fontSize: '13px', fontWeight: 600, color: a.labelColor, marginBottom: '6px' } }, result.label), |
| 180 |
el('div', { style: { display: 'flex', alignItems: 'baseline', justifyContent: 'center', gap: '4px' } }, |
| 181 |
el('span', { className: 'bkbg-pace-result-value', style: { color: a.resultColor } }, result.main), |
| 182 |
result.sub && el('span', { style: { fontSize: '22px', fontWeight: 600, color: a.labelColor } }, ' ' + result.sub) |
| 183 |
), |
| 184 |
result.extra && a.showSpeedConv && el('div', { style: { marginTop: '8px', fontSize: '14px', color: a.labelColor } }, result.extra) |
| 185 |
) : el('div', { style: { color: a.labelColor } }, 'Enter values above to calculate') |
| 186 |
) |
| 187 |
) |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
registerBlockType('blockenberg/pace-calculator', { |
| 192 |
edit: function(props) { |
| 193 |
var a = props.attributes; var set = props.setAttributes; |
| 194 |
var blockProps = useBlockProps((function () { |
| 195 |
var tv = getTypoCssVars(); |
| 196 |
var s = {}; |
| 197 |
Object.assign(s, tv(a.titleTypo, '--bkbg-pace-tt-')); |
| 198 |
Object.assign(s, tv(a.resultTypo, '--bkbg-pace-rs-')); |
| 199 |
return { style: s }; |
| 200 |
})()); |
| 201 |
var colorSettings = [ |
| 202 |
{ value: a.accentColor, onChange: function(v) { set({ accentColor: v }); }, label: 'Accent / Tabs' }, |
| 203 |
{ value: a.cardBg, onChange: function(v) { set({ cardBg: v }); }, label: 'Card Background' }, |
| 204 |
{ value: a.resultBg, onChange: function(v) { set({ resultBg: v }); }, label: 'Result Background' }, |
| 205 |
{ value: a.resultBorder, onChange: function(v) { set({ resultBorder: v }); }, label: 'Result Border' }, |
| 206 |
{ value: a.resultColor, onChange: function(v) { set({ resultColor: v }); }, label: 'Result Value Color' }, |
| 207 |
{ value: a.presetBg, onChange: function(v) { set({ presetBg: v }); }, label: 'Preset Chip Background' }, |
| 208 |
{ value: a.presetColor, onChange: function(v) { set({ presetColor: v }); }, label: 'Preset Chip Text' }, |
| 209 |
{ value: a.convBg, onChange: function(v) { set({ convBg: v }); }, label: 'Conversion Card Background' }, |
| 210 |
{ value: a.titleColor, onChange: function(v) { set({ titleColor: v }); }, label: 'Title Color' }, |
| 211 |
{ value: a.subtitleColor, onChange: function(v) { set({ subtitleColor: v }); }, label: 'Subtitle Color' }, |
| 212 |
{ value: a.labelColor, onChange: function(v) { set({ labelColor: v }); }, label: 'Label Color' }, |
| 213 |
{ value: a.sectionBg, onChange: function(v) { set({ sectionBg: v }); }, label: 'Section Background' } |
| 214 |
]; |
| 215 |
return el(Fragment, null, |
| 216 |
el(InspectorControls, null, |
| 217 |
el(PanelBody, { title: 'Header', initialOpen: false }, |
| 218 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Title', checked: a.showTitle, onChange: function(v) { set({ showTitle: v }); } }), |
| 219 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Subtitle', checked: a.showSubtitle, onChange: function(v) { set({ showSubtitle: v }); } }), |
| 220 |
el(TextControl, { label: 'Title', value: a.title, onChange: function(v) { set({ title: v }); } }), |
| 221 |
el(TextControl, { label: 'Subtitle', value: a.subtitle, onChange: function(v) { set({ subtitle: v }); } }) |
| 222 |
), |
| 223 |
el(PanelBody, { title: 'Calculator Settings', initialOpen: true }, |
| 224 |
el(SelectControl, { label: 'Default Mode', value: a.defaultMode, options: [{ label: 'Find Pace', value: 'find_pace' }, { label: 'Find Finish Time', value: 'find_time' }, { label: 'Find Distance', value: 'find_dist' }], onChange: function(v) { set({ defaultMode: v }); } }), |
| 225 |
el(SelectControl, { label: 'Default Unit', value: a.defaultUnit, options: [{ label: 'Kilometers (km)', value: 'km' }, { label: 'Miles (mi)', value: 'mi' }], onChange: function(v) { set({ defaultUnit: v }); } }), |
| 226 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Race Distance Presets', checked: a.showPresets, onChange: function(v) { set({ showPresets: v }); } }), |
| 227 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Speed Conversion', checked: a.showSpeedConv, onChange: function(v) { set({ showSpeedConv: v }); } }) |
| 228 |
), |
| 229 |
|
| 230 |
el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false }, |
| 231 |
el(getTypoControl(), { label: __('Title', 'blockenberg'), value: a.titleTypo, onChange: function (v) { set({ titleTypo: v }); } }), |
| 232 |
el(getTypoControl(), { label: __('Result', 'blockenberg'), value: a.resultTypo, onChange: function (v) { set({ resultTypo: v }); } }) |
| 233 |
), |
| 234 |
el(PanelColorSettings, { title: 'Colors', initialOpen: false, colorSettings: colorSettings }), |
| 235 |
el(PanelBody, { title: 'Sizing & Layout', initialOpen: false }, |
| 236 |
el(RangeControl, { label: 'Card Border Radius', value: a.cardRadius, min: 0, max: 40, step: 1, onChange: function(v) { set({ cardRadius: v }); } }), |
| 237 |
el(RangeControl, { label: 'Input Border Radius', value: a.inputRadius, min: 0, max: 24, step: 1, onChange: function(v) { set({ inputRadius: v }); } }), |
| 238 |
el(RangeControl, { label: 'Max Width (px)', value: a.maxWidth, min: 300, max: 900, step: 10, onChange: function(v) { set({ maxWidth: v }); } }), |
| 239 |
el(RangeControl, { label: 'Padding Top (px)', value: a.paddingTop, min: 0, max: 160, step: 4, onChange: function(v) { set({ paddingTop: v }); } }), |
| 240 |
el(RangeControl, { label: 'Padding Bottom (px)', value: a.paddingBottom, min: 0, max: 160, step: 4, onChange: function(v) { set({ paddingBottom: v }); } }) |
| 241 |
) |
| 242 |
), |
| 243 |
el('div', blockProps, el(PacePreview, { attributes: a, setAttributes: set })) |
| 244 |
); |
| 245 |
}, |
| 246 |
save: function(props) { |
| 247 |
var a = props.attributes; |
| 248 |
var blockProps = wp.blockEditor.useBlockProps.save(); |
| 249 |
return el('div', blockProps, el('div', { className: 'bkbg-pace-app', 'data-opts': JSON.stringify(a) })); |
| 250 |
} |
| 251 |
}); |
| 252 |
}() ); |
| 253 |
|