| 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; function getTC() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 18 |
var _tv; function getTV() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 19 |
|
| 20 |
var CYCLE_MIN = 90; // minutes |
| 21 |
|
| 22 |
function pad2(n) { return String(n).padStart(2, '0'); } |
| 23 |
|
| 24 |
function addMinutes(h, m, mins) { |
| 25 |
var total = h * 60 + m + mins; |
| 26 |
total = ((total % 1440) + 1440) % 1440; |
| 27 |
return { h: Math.floor(total / 60), m: total % 60 }; |
| 28 |
} |
| 29 |
function subMinutes(h, m, mins) { |
| 30 |
return addMinutes(h, m, -mins); |
| 31 |
} |
| 32 |
|
| 33 |
function fmt12(h, m) { |
| 34 |
var ampm = h < 12 ? 'AM' : 'PM'; |
| 35 |
var hh = h % 12 || 12; |
| 36 |
return hh + ':' + pad2(m) + ' ' + ampm; |
| 37 |
} |
| 38 |
|
| 39 |
// mode=wake: user sets bedtime → output wake times |
| 40 |
// mode=sleep: user sets wake time → output bedtimes |
| 41 |
function calcTimes(mode, h, m, onsetDelay, cMin, cMax) { |
| 42 |
var results = []; |
| 43 |
for (var c = cMax; c >= cMin; c--) { |
| 44 |
var totalMins = onsetDelay + c * CYCLE_MIN; |
| 45 |
var t; |
| 46 |
if (mode === 'wake') { |
| 47 |
t = addMinutes(h, m, totalMins); |
| 48 |
} else { |
| 49 |
t = subMinutes(h, m, totalMins); |
| 50 |
} |
| 51 |
results.push({ cycles: c, h: t.h, m: t.m, totalSleep: c * CYCLE_MIN }); |
| 52 |
} |
| 53 |
return results; |
| 54 |
} |
| 55 |
|
| 56 |
function cycleQuality(c) { |
| 57 |
if (c >= 6) return 'best'; |
| 58 |
if (c >= 5) return 'good'; |
| 59 |
return 'ok'; |
| 60 |
} |
| 61 |
|
| 62 |
function SleepPreview(props) { |
| 63 |
var a = props.attributes; |
| 64 |
var sa = props.setAttributes; |
| 65 |
|
| 66 |
var nowH = new Date().getHours(); |
| 67 |
var nowM = new Date().getMinutes(); |
| 68 |
|
| 69 |
var _mode = useState(a.mode); var mode = _mode[0]; var setMode = _mode[1]; |
| 70 |
var _h = useState(nowH); var selH = _h[0]; var setH = _h[1]; |
| 71 |
var _m = useState(Math.floor(nowM / 5) * 5); var selM = _m[0]; var setM = _m[1]; |
| 72 |
|
| 73 |
var results = calcTimes(mode, selH, selM, a.onsetDelay, a.cyclesMin, a.cyclesMax); |
| 74 |
|
| 75 |
var containerStyle = { |
| 76 |
background: a.sectionBg || undefined, |
| 77 |
paddingTop: a.paddingTop + 'px', |
| 78 |
paddingBottom: a.paddingBottom + 'px', |
| 79 |
fontFamily: 'inherit' |
| 80 |
}; |
| 81 |
var cardStyle = { |
| 82 |
background: a.cardBg, |
| 83 |
borderRadius: a.cardRadius + 'px', |
| 84 |
padding: '36px 32px', |
| 85 |
maxWidth: a.maxWidth + 'px', |
| 86 |
margin: '0 auto', |
| 87 |
boxShadow: '0 4px 24px rgba(0,0,0,0.09)' |
| 88 |
}; |
| 89 |
|
| 90 |
function qualColor(q) { |
| 91 |
if (q === 'best') return a.bestColor; |
| 92 |
if (q === 'good') return a.goodColor; |
| 93 |
return a.okColor; |
| 94 |
} |
| 95 |
|
| 96 |
// Hour options 0-23 |
| 97 |
var hourOpts = []; |
| 98 |
for (var i = 0; i < 24; i++) { hourOpts.push({ label: fmt12(i, 0).replace(/:\d\d /, ' '), value: String(i) }); } |
| 99 |
// Minute options 0-55 step 5 |
| 100 |
var minOpts = []; |
| 101 |
for (var j = 0; j < 60; j += 5) { minOpts.push({ label: pad2(j), value: String(j) }); } |
| 102 |
|
| 103 |
return el('div', { className: 'bkbg-sleep-editor', style: containerStyle }, |
| 104 |
el('div', { style: cardStyle }, |
| 105 |
|
| 106 |
// Title / subtitle |
| 107 |
(a.showTitle || a.showSubtitle) && el('div', { style: { marginBottom: '20px' } }, |
| 108 |
a.showTitle && el('div', { |
| 109 |
className: 'bkbg-sleep-title', |
| 110 |
style: { color: a.titleColor, marginBottom: '6px', contentEditable: true, suppressContentEditableWarning: true, outline: 'none' }, |
| 111 |
onBlur: function(e) { sa({ title: e.target.innerText }); }, |
| 112 |
dangerouslySetInnerHTML: { __html: a.title } |
| 113 |
}), |
| 114 |
a.showSubtitle && el('div', { |
| 115 |
className: 'bkbg-sleep-subtitle', |
| 116 |
style: { color: a.subtitleColor, contentEditable: true, suppressContentEditableWarning: true, outline: 'none' }, |
| 117 |
onBlur: function(e) { sa({ subtitle: e.target.innerText }); }, |
| 118 |
dangerouslySetInnerHTML: { __html: a.subtitle } |
| 119 |
}) |
| 120 |
), |
| 121 |
|
| 122 |
// Mode tabs |
| 123 |
el('div', { style: { display: 'flex', gap: '10px', marginBottom: '24px' } }, |
| 124 |
el('button', { |
| 125 |
className: 'bkbg-sleep-mode-btn' + (mode === 'wake' ? ' active' : ''), |
| 126 |
style: { flex: 1, padding: '10px', borderRadius: '8px', border: '2px solid ' + (mode === 'wake' ? a.accentColor : '#e5e7eb'), background: mode === 'wake' ? a.accentColor : 'transparent', color: mode === 'wake' ? '#fff' : a.accentColor, fontWeight: 700, fontSize: '14px', cursor: 'pointer' }, |
| 127 |
onClick: function() { setMode('wake'); } |
| 128 |
}, '🌙 I want to wake up at…'), |
| 129 |
el('button', { |
| 130 |
className: 'bkbg-sleep-mode-btn' + (mode === 'sleep' ? ' active' : ''), |
| 131 |
style: { flex: 1, padding: '10px', borderRadius: '8px', border: '2px solid ' + (mode === 'sleep' ? a.accentColor : '#e5e7eb'), background: mode === 'sleep' ? a.accentColor : 'transparent', color: mode === 'sleep' ? '#fff' : a.accentColor, fontWeight: 700, fontSize: '14px', cursor: 'pointer' }, |
| 132 |
onClick: function() { setMode('sleep'); } |
| 133 |
}, '⏰ I need to wake up at…') |
| 134 |
), |
| 135 |
|
| 136 |
// Time picker |
| 137 |
el('div', { style: { marginBottom: '24px' } }, |
| 138 |
el('div', { style: { fontSize: '13px', fontWeight: 600, color: a.labelColor, marginBottom: '8px' } }, |
| 139 |
mode === 'wake' ? 'I plan to go to bed at:' : 'I need to wake up at:' |
| 140 |
), |
| 141 |
el('div', { style: { display: 'flex', gap: '10px', alignItems: 'center' } }, |
| 142 |
el(SelectControl, { |
| 143 |
value: String(selH), |
| 144 |
options: hourOpts, |
| 145 |
onChange: function(v) { setH(parseInt(v, 10)); }, |
| 146 |
style: { fontSize: '18px', padding: '10px' } |
| 147 |
}), |
| 148 |
el('span', { style: { fontSize: '22px', fontWeight: 700, color: a.labelColor } }, ':'), |
| 149 |
el(SelectControl, { |
| 150 |
value: String(selM), |
| 151 |
options: minOpts, |
| 152 |
onChange: function(v) { setM(parseInt(v, 10)); }, |
| 153 |
style: { fontSize: '18px', padding: '10px' } |
| 154 |
}) |
| 155 |
) |
| 156 |
), |
| 157 |
|
| 158 |
// Results |
| 159 |
el('div', { className: 'bkbg-sleep-results', style: { marginBottom: a.showTips ? '20px' : '0' } }, |
| 160 |
el('div', { style: { fontSize: '13px', fontWeight: 600, color: a.labelColor, marginBottom: '12px' } }, |
| 161 |
mode === 'wake' ? 'Set your alarm for one of these times:' : 'Try to fall asleep at one of these times:' |
| 162 |
), |
| 163 |
results.map(function(r, i) { |
| 164 |
var q = cycleQuality(r.cycles); |
| 165 |
var borderL = i === 0 ? '4px solid ' + qualColor(q) : '4px solid transparent'; |
| 166 |
var totalH = Math.floor(r.totalSleep / 60); |
| 167 |
var totalM = r.totalSleep % 60; |
| 168 |
return el('div', { |
| 169 |
key: r.cycles, |
| 170 |
className: 'bkbg-sleep-result-row', |
| 171 |
style: { |
| 172 |
display: 'flex', alignItems: 'center', justifyContent: 'space-between', |
| 173 |
padding: '14px 18px', marginBottom: '8px', |
| 174 |
background: a.resultBg, border: '1.5px solid ' + a.resultBorder, |
| 175 |
borderLeft: borderL, |
| 176 |
borderRadius: a.cardRadius + 'px' |
| 177 |
} |
| 178 |
}, |
| 179 |
el('div', null, |
| 180 |
el('div', { style: { fontSize: a.timeSize + 'px', fontWeight: 800, color: qualColor(q), lineHeight: 1.1 } }, fmt12(r.h, r.m)), |
| 181 |
el('div', { style: { fontSize: '12px', color: a.labelColor, marginTop: '2px' } }, |
| 182 |
totalH + 'h ' + (totalM ? totalM + 'm ' : '') + 'of sleep' |
| 183 |
) |
| 184 |
), |
| 185 |
el('div', { style: { textAlign: 'right' } }, |
| 186 |
el('div', { style: { fontWeight: 700, fontSize: '12px', background: a.cycleTagBg, color: a.cycleTagColor, padding: '3px 10px', borderRadius: '100px', marginBottom: '3px' } }, |
| 187 |
r.cycles + ' cycles' |
| 188 |
), |
| 189 |
i === 0 && el('div', { style: { fontSize: '11px', color: qualColor(q), fontWeight: 600 } }, '� |
| 190 |
Best') |
| 191 |
) |
| 192 |
) }) |
| 193 |
), |
| 194 |
|
| 195 |
// Tips |
| 196 |
a.showTips && el('div', { style: { |
| 197 |
background: a.tipBg, color: a.tipColor, |
| 198 |
borderRadius: a.cardRadius + 'px', |
| 199 |
padding: '14px 18px', fontSize: '13px', lineHeight: 1.6 |
| 200 |
}}, |
| 201 |
el('strong', null, '💡 Tip: '), |
| 202 |
'Sleep comes in 90-minute cycles. Waking at the end of a cycle helps you feel more refreshed. Allow ' + a.onsetDelay + ' minutes to fall asleep.' |
| 203 |
) |
| 204 |
) |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
registerBlockType('blockenberg/sleep-calculator', { |
| 209 |
edit: function(props) { |
| 210 |
var a = props.attributes; |
| 211 |
var set = props.setAttributes; |
| 212 |
var blockProps = useBlockProps((function () { |
| 213 |
var fn = getTV(); |
| 214 |
var s = {}; |
| 215 |
if (fn) { |
| 216 |
Object.assign(s, fn(a.titleTypo || {}, '--bkslp-tt-')); |
| 217 |
Object.assign(s, fn(a.subtitleTypo || {}, '--bkslp-st-')); |
| 218 |
} |
| 219 |
return { style: s }; |
| 220 |
})()); |
| 221 |
|
| 222 |
var colorSettings = [ |
| 223 |
{ value: a.accentColor, onChange: function(v) { set({ accentColor: v }); }, label: 'Accent / Mode Tabs' }, |
| 224 |
{ value: a.cardBg, onChange: function(v) { set({ cardBg: v }); }, label: 'Card Background' }, |
| 225 |
{ value: a.resultBg, onChange: function(v) { set({ resultBg: v }); }, label: 'Result Row Background' }, |
| 226 |
{ value: a.resultBorder, onChange: function(v) { set({ resultBorder: v }); }, label: 'Result Row Border' }, |
| 227 |
{ value: a.bestColor, onChange: function(v) { set({ bestColor: v }); }, label: 'Best Time Color (6 cycles)' }, |
| 228 |
{ value: a.goodColor, onChange: function(v) { set({ goodColor: v }); }, label: 'Good Time Color (5 cycles)' }, |
| 229 |
{ value: a.okColor, onChange: function(v) { set({ okColor: v }); }, label: 'OK Time Color (4 cycles)' }, |
| 230 |
{ value: a.cycleTagBg, onChange: function(v) { set({ cycleTagBg: v }); }, label: 'Cycle Tag Background' }, |
| 231 |
{ value: a.cycleTagColor, onChange: function(v) { set({ cycleTagColor: v }); }, label: 'Cycle Tag Text' }, |
| 232 |
{ value: a.tipBg, onChange: function(v) { set({ tipBg: v }); }, label: 'Tip Background' }, |
| 233 |
{ value: a.tipColor, onChange: function(v) { set({ tipColor: v }); }, label: 'Tip Text' }, |
| 234 |
{ value: a.titleColor, onChange: function(v) { set({ titleColor: v }); }, label: 'Title Color' }, |
| 235 |
{ value: a.subtitleColor, onChange: function(v) { set({ subtitleColor: v }); }, label: 'Subtitle Color' }, |
| 236 |
{ value: a.labelColor, onChange: function(v) { set({ labelColor: v }); }, label: 'Label Color' }, |
| 237 |
{ value: a.sectionBg, onChange: function(v) { set({ sectionBg: v }); }, label: 'Section Background' } |
| 238 |
]; |
| 239 |
|
| 240 |
return el(Fragment, null, |
| 241 |
el(InspectorControls, null, |
| 242 |
|
| 243 |
el(PanelBody, { title: 'Header', initialOpen: false }, |
| 244 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Title', checked: a.showTitle, onChange: function(v) { set({ showTitle: v }); } }), |
| 245 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Subtitle', checked: a.showSubtitle, onChange: function(v) { set({ showSubtitle: v }); } }), |
| 246 |
el(TextControl, { label: 'Title', value: a.title, onChange: function(v) { set({ title: v }); } }), |
| 247 |
el(TextControl, { label: 'Subtitle', value: a.subtitle, onChange: function(v) { set({ subtitle: v }); } }) |
| 248 |
), |
| 249 |
|
| 250 |
el(PanelBody, { title: 'Sleep Settings', initialOpen: true }, |
| 251 |
el(SelectControl, { |
| 252 |
label: 'Default Mode', |
| 253 |
value: a.mode, |
| 254 |
options: [ |
| 255 |
{ label: 'Find wake time (set bedtime)', value: 'wake' }, |
| 256 |
{ label: 'Find bedtime (set wake time)', value: 'sleep' } |
| 257 |
], |
| 258 |
onChange: function(v) { set({ mode: v }); } |
| 259 |
}), |
| 260 |
el(RangeControl, { label: 'Sleep Onset Delay (minutes)', value: a.onsetDelay, min: 0, max: 60, step: 1, onChange: function(v) { set({ onsetDelay: v }); } }), |
| 261 |
el(RangeControl, { label: 'Minimum Cycles to Show', value: a.cyclesMin, min: 2, max: 5, step: 1, onChange: function(v) { set({ cyclesMin: Math.min(v, a.cyclesMax) }); } }), |
| 262 |
el(RangeControl, { label: 'Maximum Cycles to Show', value: a.cyclesMax, min: 4, max: 8, step: 1, onChange: function(v) { set({ cyclesMax: Math.max(v, a.cyclesMin) }); } }), |
| 263 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Sleep Tips', checked: a.showTips, onChange: function(v) { set({ showTips: v }); } }) |
| 264 |
), |
| 265 |
|
| 266 |
|
| 267 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 268 |
el( getTC(), { label: __( 'Title', 'blockenberg' ), value: a.titleTypo || {}, onChange: function( v ) { set({ titleTypo: v }); } } ), |
| 269 |
el( getTC(), { label: __( 'Subtitle', 'blockenberg' ), value: a.subtitleTypo || {}, onChange: function( v ) { set({ subtitleTypo: v }); } } ), |
| 270 |
), |
| 271 |
el(PanelColorSettings, { title: 'Colors', initialOpen: false, colorSettings: colorSettings }), |
| 272 |
|
| 273 |
el(PanelBody, { title: 'Sizing & Layout', initialOpen: false }, |
| 274 |
el(RangeControl, { label: 'Time Display Size', value: a.timeSize, min: 20, max: 72, step: 2, onChange: function(v) { set({ timeSize: v }); } }), |
| 275 |
el(RangeControl, { label: 'Card Border Radius', value: a.cardRadius, min: 0, max: 40, step: 1, onChange: function(v) { set({ cardRadius: v }); } }), |
| 276 |
el(RangeControl, { label: 'Input Border Radius', value: a.inputRadius, min: 0, max: 24, step: 1, onChange: function(v) { set({ inputRadius: v }); } }), |
| 277 |
el(RangeControl, { label: 'Max Width (px)', value: a.maxWidth, min: 300, max: 900, step: 10, onChange: function(v) { set({ maxWidth: v }); } }), |
| 278 |
el(RangeControl, { label: 'Padding Top (px)', value: a.paddingTop, min: 0, max: 160, step: 4, onChange: function(v) { set({ paddingTop: v }); } }), |
| 279 |
el(RangeControl, { label: 'Padding Bottom (px)', value: a.paddingBottom, min: 0, max: 160, step: 4, onChange: function(v) { set({ paddingBottom: v }); } }) |
| 280 |
) |
| 281 |
), |
| 282 |
el('div', blockProps, |
| 283 |
el(SleepPreview, { attributes: a, setAttributes: set }) |
| 284 |
) |
| 285 |
); |
| 286 |
}, |
| 287 |
save: function(props) { |
| 288 |
var a = props.attributes; |
| 289 |
var blockProps = wp.blockEditor.useBlockProps.save(); |
| 290 |
return el('div', blockProps, |
| 291 |
el('div', { className: 'bkbg-sleep-app', 'data-opts': JSON.stringify(a) }) |
| 292 |
); |
| 293 |
} |
| 294 |
}); |
| 295 |
}() ); |
| 296 |
|