| 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 TextControl = wp.components.TextControl; |
| 13 |
var ToggleControl = wp.components.ToggleControl; |
| 14 |
|
| 15 |
var _cbTC, _cbTV; |
| 16 |
function _tc() { return _cbTC || (_cbTC = window.bkbgTypographyControl); } |
| 17 |
function _tv(obj, prefix) { var fn = _cbTV || (_cbTV = window.bkbgTypoCssVars); return fn ? fn(obj, prefix) : {}; } |
| 18 |
|
| 19 |
var PRESETS = [ |
| 20 |
{ label: 'Every minute', expr: '* * * * *' }, |
| 21 |
{ label: 'Every hour', expr: '0 * * * *' }, |
| 22 |
{ label: 'Daily 9 AM', expr: '0 9 * * *' }, |
| 23 |
{ label: 'Weekdays 9 AM', expr: '0 9 * * 1-5' }, |
| 24 |
{ label: 'Weekly Monday', expr: '0 9 * * 1' }, |
| 25 |
{ label: 'Monthly 1st', expr: '0 9 1 * *' }, |
| 26 |
{ label: 'Yearly Jan 1st', expr: '0 0 1 1 *' } |
| 27 |
]; |
| 28 |
|
| 29 |
var FIELDS = ['Minute', 'Hour', 'Day', 'Month', 'Weekday']; |
| 30 |
|
| 31 |
registerBlockType('blockenberg/cron-builder', { |
| 32 |
edit: function (props) { |
| 33 |
var a = props.attributes; |
| 34 |
var setAttributes = props.setAttributes; |
| 35 |
var blockProps = useBlockProps({ className: 'bkbg-cb-editor-wrap', style: Object.assign({}, _tv(a.typoTitle, '--bkbg-cb-ttl-'), _tv(a.typoSubtitle, '--bkbg-cb-sub-')) }); |
| 36 |
|
| 37 |
var parts = (a.defaultExpression || '0 9 * * *').split(' '); |
| 38 |
while (parts.length < 5) parts.push('*'); |
| 39 |
|
| 40 |
var containerStyle = { |
| 41 |
background: a.sectionBg, |
| 42 |
borderRadius: '12px', |
| 43 |
padding: '28px', |
| 44 |
maxWidth: a.contentMaxWidth + 'px', |
| 45 |
margin: '0 auto', |
| 46 |
fontFamily: 'system-ui, sans-serif' |
| 47 |
}; |
| 48 |
|
| 49 |
var cardStyle = { |
| 50 |
background: a.cardBg, |
| 51 |
borderRadius: '10px', |
| 52 |
padding: '20px', |
| 53 |
marginBottom: '16px', |
| 54 |
boxShadow: '0 2px 8px rgba(0,0,0,0.06)' |
| 55 |
}; |
| 56 |
|
| 57 |
var btnStyle = { |
| 58 |
background: a.accentColor, |
| 59 |
color: '#fff', |
| 60 |
border: 'none', |
| 61 |
borderRadius: '8px', |
| 62 |
padding: '10px 20px', |
| 63 |
cursor: 'pointer', |
| 64 |
fontWeight: '600', |
| 65 |
fontSize: '14px' |
| 66 |
}; |
| 67 |
|
| 68 |
var fieldStyle = { |
| 69 |
background: a.fieldBg, |
| 70 |
border: '2px solid transparent', |
| 71 |
borderRadius: '8px', |
| 72 |
padding: '10px', |
| 73 |
textAlign: 'center', |
| 74 |
fontSize: '20px', |
| 75 |
fontFamily: 'monospace', |
| 76 |
fontWeight: '700', |
| 77 |
width: '70px', |
| 78 |
color: a.labelColor, |
| 79 |
outline: 'none' |
| 80 |
}; |
| 81 |
|
| 82 |
return el( |
| 83 |
Fragment, |
| 84 |
null, |
| 85 |
el( |
| 86 |
InspectorControls, |
| 87 |
null, |
| 88 |
el( |
| 89 |
PanelBody, |
| 90 |
{ title: __('Content', 'blockenberg'), initialOpen: true }, |
| 91 |
el(TextControl, { |
| 92 |
label: __('Title', 'blockenberg'), |
| 93 |
value: a.title, |
| 94 |
onChange: function (v) { setAttributes({ title: v }); } |
| 95 |
}), |
| 96 |
el(TextControl, { |
| 97 |
label: __('Subtitle', 'blockenberg'), |
| 98 |
value: a.subtitle, |
| 99 |
onChange: function (v) { setAttributes({ subtitle: v }); } |
| 100 |
}), |
| 101 |
el(ToggleControl, { |
| 102 |
label: __('Show Title', 'blockenberg'), |
| 103 |
checked: a.showTitle, |
| 104 |
onChange: function (v) { setAttributes({ showTitle: v }); }, |
| 105 |
__nextHasNoMarginBottom: true |
| 106 |
}), |
| 107 |
el(ToggleControl, { |
| 108 |
label: __('Show Subtitle', 'blockenberg'), |
| 109 |
checked: a.showSubtitle, |
| 110 |
onChange: function (v) { setAttributes({ showSubtitle: v }); }, |
| 111 |
__nextHasNoMarginBottom: true |
| 112 |
}) |
| 113 |
), |
| 114 |
el( |
| 115 |
PanelBody, |
| 116 |
{ title: __('Cron Settings', 'blockenberg'), initialOpen: false }, |
| 117 |
el(TextControl, { |
| 118 |
label: __('Default Expression', 'blockenberg'), |
| 119 |
value: a.defaultExpression, |
| 120 |
onChange: function (v) { setAttributes({ defaultExpression: v }); } |
| 121 |
}), |
| 122 |
el(ToggleControl, { |
| 123 |
label: __('Show Presets', 'blockenberg'), |
| 124 |
checked: a.showPresets, |
| 125 |
onChange: function (v) { setAttributes({ showPresets: v }); }, |
| 126 |
__nextHasNoMarginBottom: true |
| 127 |
}), |
| 128 |
el(ToggleControl, { |
| 129 |
label: __('Show Human Description', 'blockenberg'), |
| 130 |
checked: a.showDescription, |
| 131 |
onChange: function (v) { setAttributes({ showDescription: v }); }, |
| 132 |
__nextHasNoMarginBottom: true |
| 133 |
}), |
| 134 |
el(ToggleControl, { |
| 135 |
label: __('Show Next Run Times', 'blockenberg'), |
| 136 |
checked: a.showNextRuns, |
| 137 |
onChange: function (v) { setAttributes({ showNextRuns: v }); }, |
| 138 |
__nextHasNoMarginBottom: true |
| 139 |
}), |
| 140 |
el(RangeControl, { |
| 141 |
label: __('Next Runs Count', 'blockenberg'), |
| 142 |
value: a.nextRunsCount, |
| 143 |
min: 1, |
| 144 |
max: 10, |
| 145 |
onChange: function (v) { setAttributes({ nextRunsCount: v }); } |
| 146 |
}) |
| 147 |
), |
| 148 |
el( |
| 149 |
PanelBody, |
| 150 |
{ title: __('Typography', 'blockenberg'), initialOpen: false }, |
| 151 |
_tc() && el(_tc(), { label: __('Title', 'blockenberg'), value: a.typoTitle, onChange: function (v) { setAttributes({ typoTitle: v }); } }), |
| 152 |
_tc() && el(_tc(), { label: __('Subtitle', 'blockenberg'), value: a.typoSubtitle, onChange: function (v) { setAttributes({ typoSubtitle: v }); } }), |
| 153 |
el(RangeControl, { |
| 154 |
label: __('Cron expression font size (px)', 'blockenberg'), |
| 155 |
value: a.expressionFontSize, |
| 156 |
min: 14, |
| 157 |
max: 36, |
| 158 |
onChange: function (v) { setAttributes({ expressionFontSize: v }); }, |
| 159 |
__nextHasNoMarginBottom: true |
| 160 |
}) |
| 161 |
), |
| 162 |
el( |
| 163 |
PanelBody, |
| 164 |
{ title: __('Appearance', 'blockenberg'), initialOpen: false }, |
| 165 |
el(RangeControl, { |
| 166 |
label: __('Max Width (px)', 'blockenberg'), |
| 167 |
value: a.contentMaxWidth, |
| 168 |
min: 320, |
| 169 |
max: 1200, |
| 170 |
onChange: function (v) { setAttributes({ contentMaxWidth: v }); } |
| 171 |
}) |
| 172 |
), |
| 173 |
el( |
| 174 |
PanelColorSettings, |
| 175 |
{ |
| 176 |
title: __('Colors', 'blockenberg'), |
| 177 |
initialOpen: false, |
| 178 |
colorSettings: [ |
| 179 |
{ value: a.accentColor, onChange: function (v) { setAttributes({ accentColor: v || '#0ea5e9' }); }, label: __('Accent / Button', 'blockenberg') }, |
| 180 |
{ value: a.sectionBg, onChange: function (v) { setAttributes({ sectionBg: v || '#f0f9ff' }); }, label: __('Section Background', 'blockenberg') }, |
| 181 |
{ value: a.cardBg, onChange: function (v) { setAttributes({ cardBg: v || '#ffffff' }); }, label: __('Card Background', 'blockenberg') }, |
| 182 |
{ value: a.fieldBg, onChange: function (v) { setAttributes({ fieldBg: v || '#e0f2fe' }); }, label: __('Field Background', 'blockenberg') }, |
| 183 |
{ value: a.titleColor, onChange: function (v) { setAttributes({ titleColor: v || '#0c4a6e' }); }, label: __('Title Color', 'blockenberg') }, |
| 184 |
{ value: a.subtitleColor, onChange: function (v) { setAttributes({ subtitleColor: v || '#6b7280' }); }, label: __('Subtitle Color', 'blockenberg') }, |
| 185 |
{ value: a.labelColor, onChange: function (v) { setAttributes({ labelColor: v || '#374151' }); }, label: __('Label Color', 'blockenberg') } |
| 186 |
] |
| 187 |
} |
| 188 |
) |
| 189 |
), |
| 190 |
el( |
| 191 |
'div', |
| 192 |
blockProps, |
| 193 |
el( |
| 194 |
'div', |
| 195 |
{ style: containerStyle }, |
| 196 |
a.showTitle && el('div', { className: 'bkbg-cb-title', style: { color: a.titleColor, marginBottom: '6px' } }, a.title), |
| 197 |
a.showSubtitle && el('div', { className: 'bkbg-cb-subtitle', style: { color: a.subtitleColor, marginBottom: '20px' } }, a.subtitle), |
| 198 |
// Presets |
| 199 |
a.showPresets && el('div', { style: Object.assign({}, cardStyle, { display: 'flex', gap: '8px', flexWrap: 'wrap' }) }, |
| 200 |
PRESETS.map(function (p) { |
| 201 |
return el('button', { |
| 202 |
key: p.expr, |
| 203 |
style: { |
| 204 |
background: p.expr === a.defaultExpression ? a.accentColor : '#e5e7eb', |
| 205 |
color: p.expr === a.defaultExpression ? '#fff' : '#374151', |
| 206 |
border: 'none', borderRadius: '6px', padding: '6px 12px', |
| 207 |
cursor: 'pointer', fontSize: '12px', fontWeight: '500' |
| 208 |
} |
| 209 |
}, p.label); |
| 210 |
}) |
| 211 |
), |
| 212 |
// Fields |
| 213 |
el('div', { style: cardStyle }, |
| 214 |
el('div', { style: { display: 'flex', gap: '10px', justifyContent: 'center', flexWrap: 'wrap', marginBottom: '14px' } }, |
| 215 |
FIELDS.map(function (f, i) { |
| 216 |
return el('div', { key: f, style: { textAlign: 'center' } }, |
| 217 |
el('input', { style: fieldStyle, type: 'text', value: parts[i], readOnly: true }), |
| 218 |
el('div', { style: { fontSize: '11px', color: a.subtitleColor, marginTop: '4px' } }, f) |
| 219 |
); |
| 220 |
}) |
| 221 |
), |
| 222 |
// Expression display |
| 223 |
el('div', { style: { background: a.fieldBg, borderRadius: '8px', padding: '14px', textAlign: 'center', fontFamily: 'monospace', fontSize: (a.expressionFontSize || 22) + 'px', fontWeight: '700', color: a.accentColor, letterSpacing: '0.1em', marginBottom: '12px' } }, |
| 224 |
parts.join(' ') |
| 225 |
), |
| 226 |
// Copy |
| 227 |
el('div', { style: { display: 'flex', gap: '10px', justifyContent: 'center' } }, |
| 228 |
el('button', { style: btnStyle }, 'Copy Expression') |
| 229 |
) |
| 230 |
), |
| 231 |
// Description |
| 232 |
a.showDescription && el('div', { style: Object.assign({}, cardStyle, { borderLeft: '4px solid ' + a.accentColor }) }, |
| 233 |
el('div', { style: { fontSize: '12px', fontWeight: '600', color: a.subtitleColor, marginBottom: '4px' } }, 'DESCRIPTION'), |
| 234 |
el('div', { style: { fontSize: '16px', color: a.labelColor } }, 'At 09:00 AM, every day') |
| 235 |
), |
| 236 |
// Next runs |
| 237 |
a.showNextRuns && el('div', { style: cardStyle }, |
| 238 |
el('div', { style: { fontSize: '14px', fontWeight: '700', color: a.labelColor, marginBottom: '10px' } }, 'Next ' + a.nextRunsCount + ' Run Times'), |
| 239 |
[0,1,2,3,4].slice(0, a.nextRunsCount).map(function (i) { |
| 240 |
var d = new Date(Date.now() + i * 86400000); |
| 241 |
d.setHours(9, 0, 0, 0); |
| 242 |
return el('div', { key: i, style: { padding: '8px 0', borderBottom: i < a.nextRunsCount - 1 ? '1px solid #f3f4f6' : 'none', fontSize: '14px', color: a.labelColor, display: 'flex', alignItems: 'center', gap: '8px' } }, |
| 243 |
el('span', { style: { color: a.accentColor, fontWeight: '600' } }, '→'), |
| 244 |
el('span', null, d.toLocaleString()) |
| 245 |
); |
| 246 |
}) |
| 247 |
) |
| 248 |
) |
| 249 |
) |
| 250 |
); |
| 251 |
}, |
| 252 |
save: function (props) { |
| 253 |
var a = props.attributes; |
| 254 |
var blockProps = wp.blockEditor.useBlockProps.save({ style: Object.assign({}, _tv(a.typoTitle, '--bkbg-cb-ttl-'), _tv(a.typoSubtitle, '--bkbg-cb-sub-')) }); |
| 255 |
return el('div', blockProps, |
| 256 |
el('div', { |
| 257 |
className: 'bkbg-cb-app', |
| 258 |
'data-opts': JSON.stringify(a) |
| 259 |
}) |
| 260 |
); |
| 261 |
} |
| 262 |
}); |
| 263 |
}() ); |
| 264 |
|