| 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 PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 8 |
var PanelBody = wp.components.PanelBody; |
| 9 |
var ToggleControl = wp.components.ToggleControl; |
| 10 |
var RangeControl = wp.components.RangeControl; |
| 11 |
var SelectControl = wp.components.SelectControl; |
| 12 |
var TextControl = wp.components.TextControl; |
| 13 |
|
| 14 |
var _cuTC, _cuTV; |
| 15 |
function _tc() { return _cuTC || (_cuTC = window.bkbgTypographyControl); } |
| 16 |
function _tv(obj, prefix) { var fn = _cuTV || (_cuTV = window.bkbgTypoCssVars); return fn ? fn(obj, prefix) : {}; } |
| 17 |
|
| 18 |
registerBlockType('blockenberg/count-up', { |
| 19 |
edit: function (props) { |
| 20 |
var attr = props.attributes; |
| 21 |
var setAttr = props.setAttributes; |
| 22 |
|
| 23 |
var blockProps = useBlockProps({ className: 'bkbg-cu-editor', style: Object.assign({}, _tv(attr.typoValue, '--bkbg-cu-val-'), _tv(attr.typoLabel, '--bkbg-cu-lbl-')) }); |
| 24 |
|
| 25 |
var previewStyle = { |
| 26 |
background: attr.bgColor || 'transparent', |
| 27 |
paddingTop: attr.paddingTop + 'px', |
| 28 |
paddingBottom: attr.paddingBottom + 'px', |
| 29 |
textAlign: attr.textAlign |
| 30 |
}; |
| 31 |
|
| 32 |
var preview = el('div', { style: previewStyle }, |
| 33 |
el('div', { |
| 34 |
style: { |
| 35 |
maxWidth: attr.maxWidth + 'px', |
| 36 |
margin: '0 auto', |
| 37 |
textAlign: attr.textAlign |
| 38 |
} |
| 39 |
}, |
| 40 |
attr.showIcon && el('div', { |
| 41 |
className: 'bkbg-cu-icon', |
| 42 |
style: { fontSize: attr.iconSize + 'px', color: attr.iconColor, marginBottom: '12px' } |
| 43 |
}, attr.icon), |
| 44 |
el('div', { |
| 45 |
className: 'bkbg-cu-value', |
| 46 |
style: { color: attr.valueColor, lineHeight: 1.1 } |
| 47 |
}, attr.prefix + attr.value.toLocaleString() + attr.suffix), |
| 48 |
attr.showLabel && el('div', { |
| 49 |
className: 'bkbg-cu-label', |
| 50 |
style: { color: attr.labelColor, marginTop: '8px' } |
| 51 |
}, attr.label) |
| 52 |
) |
| 53 |
); |
| 54 |
|
| 55 |
var controls = el(InspectorControls, {}, |
| 56 |
el(PanelBody, { title: __('Counter Settings', 'blockenberg'), initialOpen: true }, |
| 57 |
el(TextControl, { |
| 58 |
label: __('Target Number', 'blockenberg'), |
| 59 |
type: 'number', |
| 60 |
value: attr.value, |
| 61 |
onChange: function (v) { setAttr({ value: parseFloat(v) || 0 }); }, |
| 62 |
__nextHasNoMarginBottom: true |
| 63 |
}), |
| 64 |
el(TextControl, { |
| 65 |
label: __('Prefix', 'blockenberg'), |
| 66 |
value: attr.prefix, |
| 67 |
onChange: function (v) { setAttr({ prefix: v }); }, |
| 68 |
__nextHasNoMarginBottom: true |
| 69 |
}), |
| 70 |
el(TextControl, { |
| 71 |
label: __('Suffix', 'blockenberg'), |
| 72 |
value: attr.suffix, |
| 73 |
onChange: function (v) { setAttr({ suffix: v }); }, |
| 74 |
__nextHasNoMarginBottom: true |
| 75 |
}), |
| 76 |
el(SelectControl, { |
| 77 |
label: __('Thousands Separator', 'blockenberg'), |
| 78 |
value: attr.separator, |
| 79 |
options: [ |
| 80 |
{ label: 'Comma (1,000)', value: ',' }, |
| 81 |
{ label: 'Period (1.000)', value: '.' }, |
| 82 |
{ label: 'None (1000)', value: '' } |
| 83 |
], |
| 84 |
onChange: function (v) { setAttr({ separator: v }); }, |
| 85 |
__nextHasNoMarginBottom: true |
| 86 |
}), |
| 87 |
el(RangeControl, { |
| 88 |
label: __('Decimal Places', 'blockenberg'), |
| 89 |
value: attr.decimals, |
| 90 |
min: 0, max: 4, |
| 91 |
onChange: function (v) { setAttr({ decimals: v }); }, |
| 92 |
__nextHasNoMarginBottom: true |
| 93 |
}), |
| 94 |
el(RangeControl, { |
| 95 |
label: __('Animation Duration (ms)', 'blockenberg'), |
| 96 |
value: attr.duration, |
| 97 |
min: 500, max: 5000, step: 100, |
| 98 |
onChange: function (v) { setAttr({ duration: v }); }, |
| 99 |
__nextHasNoMarginBottom: true |
| 100 |
}) |
| 101 |
), |
| 102 |
el(PanelBody, { title: __('Icon & Label', 'blockenberg'), initialOpen: false }, |
| 103 |
el(ToggleControl, { |
| 104 |
label: __('Show Icon', 'blockenberg'), |
| 105 |
checked: attr.showIcon, |
| 106 |
onChange: function (v) { setAttr({ showIcon: v }); }, |
| 107 |
__nextHasNoMarginBottom: true |
| 108 |
}), |
| 109 |
attr.showIcon && el(TextControl, { |
| 110 |
label: __('Icon (emoji or text)', 'blockenberg'), |
| 111 |
value: attr.icon, |
| 112 |
onChange: function (v) { setAttr({ icon: v }); }, |
| 113 |
__nextHasNoMarginBottom: true |
| 114 |
}), |
| 115 |
attr.showIcon && el(RangeControl, { |
| 116 |
label: __('Icon Size (px)', 'blockenberg'), |
| 117 |
value: attr.iconSize, |
| 118 |
min: 16, max: 100, |
| 119 |
onChange: function (v) { setAttr({ iconSize: v }); }, |
| 120 |
__nextHasNoMarginBottom: true |
| 121 |
}), |
| 122 |
el(ToggleControl, { |
| 123 |
label: __('Show Label', 'blockenberg'), |
| 124 |
checked: attr.showLabel, |
| 125 |
onChange: function (v) { setAttr({ showLabel: v }); }, |
| 126 |
__nextHasNoMarginBottom: true |
| 127 |
}), |
| 128 |
attr.showLabel && el(TextControl, { |
| 129 |
label: __('Label Text', 'blockenberg'), |
| 130 |
value: attr.label, |
| 131 |
onChange: function (v) { setAttr({ label: v }); }, |
| 132 |
__nextHasNoMarginBottom: true |
| 133 |
}) |
| 134 |
), |
| 135 |
el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false }, |
| 136 |
_tc() && el(_tc(), { label: __('Value', 'blockenberg'), value: attr.typoValue, onChange: function (v) { setAttr({ typoValue: v }); } }), |
| 137 |
_tc() && el(_tc(), { label: __('Label', 'blockenberg'), value: attr.typoLabel, onChange: function (v) { setAttr({ typoLabel: v }); } }), |
| 138 |
el(SelectControl, { |
| 139 |
label: __('Text Align', 'blockenberg'), |
| 140 |
value: attr.textAlign, |
| 141 |
options: [ |
| 142 |
{ label: 'Left', value: 'left' }, |
| 143 |
{ label: 'Center', value: 'center' }, |
| 144 |
{ label: 'Right', value: 'right' } |
| 145 |
], |
| 146 |
onChange: function (v) { setAttr({ textAlign: v }); }, |
| 147 |
__nextHasNoMarginBottom: true |
| 148 |
}) |
| 149 |
), |
| 150 |
el(PanelColorSettings, { |
| 151 |
title: __('Colors', 'blockenberg'), |
| 152 |
initialOpen: false, |
| 153 |
colorSettings: [ |
| 154 |
{ label: __('Value Color', 'blockenberg'), value: attr.valueColor, onChange: function (v) { setAttr({ valueColor: v || '' }); } }, |
| 155 |
{ label: __('Label Color', 'blockenberg'), value: attr.labelColor, onChange: function (v) { setAttr({ labelColor: v || '' }); } }, |
| 156 |
{ label: __('Icon Color', 'blockenberg'), value: attr.iconColor, onChange: function (v) { setAttr({ iconColor: v || '' }); } }, |
| 157 |
{ label: __('Background', 'blockenberg'), value: attr.bgColor, onChange: function (v) { setAttr({ bgColor: v || '' }); } } |
| 158 |
] |
| 159 |
}), |
| 160 |
el(PanelBody, { title: __('Layout', 'blockenberg'), initialOpen: false }, |
| 161 |
el(RangeControl, { |
| 162 |
label: __('Max Width (px)', 'blockenberg'), |
| 163 |
value: attr.maxWidth, |
| 164 |
min: 200, max: 900, |
| 165 |
onChange: function (v) { setAttr({ maxWidth: v }); }, |
| 166 |
__nextHasNoMarginBottom: true |
| 167 |
}), |
| 168 |
el(RangeControl, { |
| 169 |
label: __('Padding Top (px)', 'blockenberg'), |
| 170 |
value: attr.paddingTop, |
| 171 |
min: 0, max: 200, |
| 172 |
onChange: function (v) { setAttr({ paddingTop: v }); }, |
| 173 |
__nextHasNoMarginBottom: true |
| 174 |
}), |
| 175 |
el(RangeControl, { |
| 176 |
label: __('Padding Bottom (px)', 'blockenberg'), |
| 177 |
value: attr.paddingBottom, |
| 178 |
min: 0, max: 200, |
| 179 |
onChange: function (v) { setAttr({ paddingBottom: v }); }, |
| 180 |
__nextHasNoMarginBottom: true |
| 181 |
}) |
| 182 |
) |
| 183 |
); |
| 184 |
|
| 185 |
return el('div', blockProps, |
| 186 |
controls, |
| 187 |
preview |
| 188 |
); |
| 189 |
}, |
| 190 |
|
| 191 |
save: function (props) { |
| 192 |
var attr = props.attributes; |
| 193 |
var blockProps = wp.blockEditor.useBlockProps.save(); |
| 194 |
return el('div', blockProps, |
| 195 |
el('div', { |
| 196 |
className: 'bkbg-cu-app', |
| 197 |
'data-opts': JSON.stringify(attr) |
| 198 |
}) |
| 199 |
); |
| 200 |
} |
| 201 |
}); |
| 202 |
}() ); |
| 203 |
|