| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var __ = wp.i18n.__; |
| 4 |
var registerBlockType = wp.blocks.registerBlockType; |
| 5 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 6 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 7 |
var RichText = wp.blockEditor.RichText; |
| 8 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 9 |
var PanelBody = wp.components.PanelBody; |
| 10 |
var ToggleControl = wp.components.ToggleControl; |
| 11 |
var RangeControl = wp.components.RangeControl; |
| 12 |
var SelectControl = wp.components.SelectControl; |
| 13 |
var TextControl = wp.components.TextControl; |
| 14 |
|
| 15 |
var _cdpTC, _cdpTV; |
| 16 |
function _tc() { return _cdpTC || (_cdpTC = window.bkbgTypographyControl); } |
| 17 |
function _tv(obj, prefix) { var fn = _cdpTV || (_cdpTV = window.bkbgTypoCssVars); return fn ? fn(obj, prefix) : {}; } |
| 18 |
|
| 19 |
/* Helpers for the editor SVG preview */ |
| 20 |
function svgRing(pct, radius, strokeWidth, ringBg, ringColor, num, unit, labelColor, numColor, showLabel) { |
| 21 |
var cx = radius + strokeWidth; |
| 22 |
var cy = radius + strokeWidth; |
| 23 |
var size = (radius + strokeWidth) * 2; |
| 24 |
var circumference = 2 * Math.PI * radius; |
| 25 |
var dash = pct * circumference; |
| 26 |
return el('div', { className: 'bkbg-cdp-ring-wrap' }, |
| 27 |
el('svg', { width: size, height: size, viewBox: '0 0 ' + size + ' ' + size, xmlns: 'http://www.w3.org/2000/svg' }, |
| 28 |
el('circle', { cx: cx, cy: cy, r: radius, stroke: ringBg, strokeWidth: strokeWidth, fill: 'none', strokeLinecap: 'round' }), |
| 29 |
el('circle', { cx: cx, cy: cy, r: radius, stroke: ringColor, strokeWidth: strokeWidth, fill: 'none', strokeLinecap: 'round', strokeDasharray: circumference, strokeDashoffset: circumference - dash, style: { transform: 'rotate(-90deg)', transformOrigin: 'center' } }) |
| 30 |
), |
| 31 |
el('div', { className: 'bkbg-cdp-ring-labels', style: { color: numColor } }, |
| 32 |
el('span', { className: 'bkbg-cdp-num' }, String(num).padStart(2, '0')), |
| 33 |
showLabel && el('span', { className: 'bkbg-cdp-unit', style: { color: labelColor } }, unit) |
| 34 |
) |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
function barUnit(pct, barBg, barColor, num, unit, labelColor, numColor, showLabel) { |
| 39 |
return el('div', { className: 'bkbg-cdp-bar-unit' }, |
| 40 |
el('div', { className: 'bkbg-cdp-bar-num', style: { color: numColor } }, String(num).padStart(2, '0')), |
| 41 |
showLabel && el('div', { className: 'bkbg-cdp-bar-label', style: { color: labelColor } }, unit), |
| 42 |
el('div', { className: 'bkbg-cdp-bar-track', style: { background: barBg } }, |
| 43 |
el('div', { className: 'bkbg-cdp-bar-fill', style: { background: barColor, width: (pct * 100) + '%' } }) |
| 44 |
) |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
registerBlockType('blockenberg/countdown-progress', { |
| 49 |
edit: function (props) { |
| 50 |
var attr = props.attributes; |
| 51 |
var setAttr = props.setAttributes; |
| 52 |
|
| 53 |
/* Static preview: show a sample of 14d 6h 30m 45s */ |
| 54 |
var previewData = [ |
| 55 |
{ show: attr.showDays, pct: 14/30, num: 14, unit: 'Days' }, |
| 56 |
{ show: attr.showHours, pct: 6/24, num: 6, unit: 'Hours' }, |
| 57 |
{ show: attr.showMins, pct: 30/60, num: 30, unit: 'Mins' }, |
| 58 |
{ show: attr.showSecs, pct: 45/60, num: 45, unit: 'Secs' } |
| 59 |
]; |
| 60 |
|
| 61 |
return el(wp.element.Fragment, null, |
| 62 |
el(InspectorControls, null, |
| 63 |
el(PanelBody, { title: __('Content', 'blockenberg'), initialOpen: true }, |
| 64 |
el(TextControl, { label: __('Heading', 'blockenberg'), value: attr.heading, onChange: function (v) { setAttr({ heading: v }); }, __nextHasNoMarginBottom: true }), |
| 65 |
el(TextControl, { label: __('Subtext', 'blockenberg'), value: attr.subtext, onChange: function (v) { setAttr({ subtext: v }); }, __nextHasNoMarginBottom: true }), |
| 66 |
el(TextControl, { label: __('End Date (YYYY-MM-DDTHH:mm:ss)', 'blockenberg'), value: attr.endDate, onChange: function (v) { setAttr({ endDate: v }); }, placeholder: '2025-12-31T23:59:59', __nextHasNoMarginBottom: true }) |
| 67 |
), |
| 68 |
el(PanelBody, { title: __('Style & Layout', 'blockenberg'), initialOpen: false }, |
| 69 |
el(SelectControl, { label: __('Display Style', 'blockenberg'), value: attr.style, options: [{ label: 'Rings (SVG)', value: 'ring' }, { label: 'Bars', value: 'bar' }], onChange: function (v) { setAttr({ style: v }); }, __nextHasNoMarginBottom: true }), |
| 70 |
attr.style === 'ring' && el(RangeControl, { label: __('Ring Radius', 'blockenberg'), value: attr.radius, min: 30, max: 100, onChange: function (v) { setAttr({ radius: v }); }, __nextHasNoMarginBottom: true }), |
| 71 |
attr.style === 'ring' && el(RangeControl, { label: __('Stroke Width', 'blockenberg'), value: attr.strokeWidth, min: 4, max: 20, onChange: function (v) { setAttr({ strokeWidth: v }); }, __nextHasNoMarginBottom: true }), |
| 72 |
el(RangeControl, { label: __('Max Width', 'blockenberg'), value: attr.maxWidth, min: 400, max: 1400, step: 20, onChange: function (v) { setAttr({ maxWidth: v }); }, __nextHasNoMarginBottom: true }), |
| 73 |
el(RangeControl, { label: __('Padding Top', 'blockenberg'), value: attr.paddingTop, min: 0, max: 200, onChange: function (v) { setAttr({ paddingTop: v }); }, __nextHasNoMarginBottom: true }), |
| 74 |
el(RangeControl, { label: __('Padding Bottom', 'blockenberg'), value: attr.paddingBottom, min: 0, max: 200, onChange: function (v) { setAttr({ paddingBottom: v }); }, __nextHasNoMarginBottom: true }) |
| 75 |
), |
| 76 |
el(PanelBody, { title: __('Units', 'blockenberg'), initialOpen: false }, |
| 77 |
el(ToggleControl, { label: __('Show Days', 'blockenberg'), checked: attr.showDays, onChange: function (v) { setAttr({ showDays: v }); }, __nextHasNoMarginBottom: true }), |
| 78 |
el(ToggleControl, { label: __('Show Hours', 'blockenberg'), checked: attr.showHours, onChange: function (v) { setAttr({ showHours: v }); }, __nextHasNoMarginBottom: true }), |
| 79 |
el(ToggleControl, { label: __('Show Minutes', 'blockenberg'), checked: attr.showMins, onChange: function (v) { setAttr({ showMins: v }); }, __nextHasNoMarginBottom: true }), |
| 80 |
el(ToggleControl, { label: __('Show Seconds', 'blockenberg'), checked: attr.showSecs, onChange: function (v) { setAttr({ showSecs: v }); }, __nextHasNoMarginBottom: true }), |
| 81 |
el(ToggleControl, { label: __('Show Labels', 'blockenberg'), checked: attr.showLabels, onChange: function (v) { setAttr({ showLabels: v }); }, __nextHasNoMarginBottom: true }) |
| 82 |
), |
| 83 |
el(PanelColorSettings, { |
| 84 |
title: __('Colors', 'blockenberg'), initialOpen: false, |
| 85 |
colorSettings: [ |
| 86 |
{ label: __('Background', 'blockenberg'), value: attr.bgColor, onChange: function (v) { setAttr({ bgColor: v || '#0f172a' }); } }, |
| 87 |
{ label: __('Heading', 'blockenberg'), value: attr.headingColor, onChange: function (v) { setAttr({ headingColor: v || '#ffffff' }); } }, |
| 88 |
{ label: __('Subtext', 'blockenberg'), value: attr.subColor, onChange: function (v) { setAttr({ subColor: v || '#94a3b8' }); } }, |
| 89 |
{ label: __('Ring / Bar Fill', 'blockenberg'), value: attr.ringColor, onChange: function (v) { setAttr({ ringColor: v || '#6366f1', barColor: v || '#6366f1' }); } }, |
| 90 |
{ label: __('Ring / Bar Track', 'blockenberg'), value: attr.ringBg, onChange: function (v) { setAttr({ ringBg: v || '#1e293b', barBg: v || '#1e293b' }); } }, |
| 91 |
{ label: __('Number', 'blockenberg'), value: attr.numColor, onChange: function (v) { setAttr({ numColor: v || '#ffffff' }); } }, |
| 92 |
{ label: __('Unit Label', 'blockenberg'), value: attr.labelColor, onChange: function (v) { setAttr({ labelColor: v || '#94a3b8' }); } } |
| 93 |
] |
| 94 |
}), |
| 95 |
el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false }, |
| 96 |
_tc() && el(_tc(), { label: __('Heading', 'blockenberg'), value: attr.typoHeading, onChange: function (v) { setAttr({ typoHeading: v }); } }), |
| 97 |
_tc() && el(_tc(), { label: __('Subtext', 'blockenberg'), value: attr.typoSub, onChange: function (v) { setAttr({ typoSub: v }); } }), |
| 98 |
_tc() && el(_tc(), { label: __('Number', 'blockenberg'), value: attr.typoNum, onChange: function (v) { setAttr({ typoNum: v }); } }), |
| 99 |
_tc() && el(_tc(), { label: __('Unit Label', 'blockenberg'), value: attr.typoUnit, onChange: function (v) { setAttr({ typoUnit: v }); } }) |
| 100 |
) |
| 101 |
), |
| 102 |
el('div', Object.assign(useBlockProps({ className: 'bkbg-cdp-editor', style: Object.assign({ background: attr.bgColor }, _tv(attr.typoHeading, '--bkbg-cdp-h-'), _tv(attr.typoSub, '--bkbg-cdp-sub-'), _tv(attr.typoNum, '--bkbg-cdp-num-'), _tv(attr.typoUnit, '--bkbg-cdp-unit-')) })), |
| 103 |
el('div', { className: 'bkbg-cdp-inner', style: { maxWidth: attr.maxWidth + 'px', margin: '0 auto', padding: attr.paddingTop + 'px 24px ' + attr.paddingBottom + 'px', textAlign: 'center' } }, |
| 104 |
el(RichText, { tagName: 'h2', className: 'bkbg-cdp-heading', style: { color: attr.headingColor }, value: attr.heading, onChange: function (v) { setAttr({ heading: v }); }, placeholder: __('Heading…', 'blockenberg') }), |
| 105 |
el(RichText, { tagName: 'p', className: 'bkbg-cdp-sub', style: { color: attr.subColor }, value: attr.subtext, onChange: function (v) { setAttr({ subtext: v }); }, placeholder: __('Subtext…', 'blockenberg') }), |
| 106 |
el('div', { className: 'bkbg-cdp-units style-' + attr.style }, |
| 107 |
previewData.map(function (d, i) { |
| 108 |
if (!d.show) return null; |
| 109 |
return attr.style === 'ring' |
| 110 |
? svgRing(d.pct, attr.radius, attr.strokeWidth, attr.ringBg, attr.ringColor, d.num, d.unit, attr.labelColor, attr.numColor, attr.showLabels) |
| 111 |
: barUnit(d.pct, attr.barBg, attr.barColor, d.num, d.unit, attr.labelColor, attr.numColor, attr.showLabels); |
| 112 |
}) |
| 113 |
) |
| 114 |
) |
| 115 |
) |
| 116 |
); |
| 117 |
}, |
| 118 |
save: function (props) { |
| 119 |
var attr = props.attributes; |
| 120 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 121 |
return el('div', useBlockProps.save(), |
| 122 |
el('div', { className: 'bkbg-cdp-app', 'data-opts': JSON.stringify(attr) }) |
| 123 |
); |
| 124 |
} |
| 125 |
}); |
| 126 |
}() ); |
| 127 |
|