| 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 MediaUpload = wp.blockEditor.MediaUpload; |
| 7 |
var MediaUploadCheck = wp.blockEditor.MediaUploadCheck; |
| 8 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 9 |
var RichText = wp.blockEditor.RichText; |
| 10 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 11 |
var PanelBody = wp.components.PanelBody; |
| 12 |
var ToggleControl = wp.components.ToggleControl; |
| 13 |
var RangeControl = wp.components.RangeControl; |
| 14 |
var SelectControl = wp.components.SelectControl; |
| 15 |
var TextControl = wp.components.TextControl; |
| 16 |
var Button = wp.components.Button; |
| 17 |
var ColorPicker = wp.components.ColorPicker; |
| 18 |
var Popover = wp.components.Popover; |
| 19 |
var useState = wp.element.useState; |
| 20 |
var Fragment = wp.element.Fragment; |
| 21 |
|
| 22 |
var _tc, _tv; |
| 23 |
function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 24 |
function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 25 |
var IP = function () { return window.bkbgIconPicker; }; |
| 26 |
|
| 27 |
function badgeShapeRadius(shape) { |
| 28 |
if (shape === 'circle') return '50%'; |
| 29 |
if (shape === 'square') return '4px'; |
| 30 |
return '10px'; /* rounded */ |
| 31 |
} |
| 32 |
|
| 33 |
var BkbgColorSwatch = function (props) { |
| 34 |
var _st = useState(false); var isOpen = _st[0]; var setOpen = _st[1]; |
| 35 |
return el(Fragment, {}, |
| 36 |
el('div', { style: { display: 'flex', alignItems: 'center', gap: '8px', marginBottom: '8px' } }, |
| 37 |
el('span', { style: { fontSize: '11px', fontWeight: 500, textTransform: 'uppercase', color: '#1e1e1e' } }, props.label), |
| 38 |
el('button', { |
| 39 |
type: 'button', |
| 40 |
onClick: function () { setOpen(!isOpen); }, |
| 41 |
style: { width: '28px', height: '28px', borderRadius: '4px', border: '1px solid #ccc', background: props.value || '#ffffff', cursor: 'pointer', padding: 0 } |
| 42 |
}) |
| 43 |
), |
| 44 |
isOpen && el(Popover, { onClose: function () { setOpen(false); } }, |
| 45 |
el('div', { style: { padding: '8px' } }, |
| 46 |
el(ColorPicker, { color: props.value, onChangeComplete: function (c) { props.onChange(c.hex); }, enableAlpha: true }) |
| 47 |
) |
| 48 |
) |
| 49 |
); |
| 50 |
}; |
| 51 |
|
| 52 |
registerBlockType('blockenberg/numbered-list', { |
| 53 |
icon: el('svg', { xmlns: 'http://www.w3.org/2000/svg', viewBox: '0 0 24 24', width: 24, height: 24 }, |
| 54 |
el('path', { d: 'M4 5.5a1.5 1.5 0 113 0 1.5 1.5 0 01-3 0zm5-.5h10v1H9V5zm-5 7a1.5 1.5 0 113 0 1.5 1.5 0 01-3 0zm5-.5h10v1H9v-1zm-5 7a1.5 1.5 0 113 0 1.5 1.5 0 01-3 0zm5-.5h10v1H9v-1z', fill: 'currentColor' }), |
| 55 |
el('text', { x: 4.5, y: 7, fontSize: 5, fontWeight: 700, fill: '#fff', textAnchor: 'middle', dominantBaseline: 'central' }, '1'), |
| 56 |
el('text', { x: 4.5, y: 13.5, fontSize: 5, fontWeight: 700, fill: '#fff', textAnchor: 'middle', dominantBaseline: 'central' }, '2'), |
| 57 |
el('text', { x: 4.5, y: 20, fontSize: 5, fontWeight: 700, fill: '#fff', textAnchor: 'middle', dominantBaseline: 'central' }, '3') |
| 58 |
), |
| 59 |
edit: function (props) { |
| 60 |
var attr = props.attributes; |
| 61 |
var set = props.setAttributes; |
| 62 |
var items = attr.items || []; |
| 63 |
|
| 64 |
function updateItem(idx, field, val) { |
| 65 |
var updated = items.map(function (it, i) { |
| 66 |
if (i !== idx) return it; |
| 67 |
var p = {}; p[field] = val; |
| 68 |
return Object.assign({}, it, p); |
| 69 |
}); |
| 70 |
set({ items: updated }); |
| 71 |
} |
| 72 |
|
| 73 |
function addItem() { |
| 74 |
set({ items: items.concat([{ customBadge: '', icon: '', iconType: 'custom-char', iconDashicon: '', iconImageUrl: '', heading: 'New Item', content: 'Description here…', imageUrl: '', imageId: 0, imageAlt: '', link: '', accentColor: attr.badgeBg }]) }); |
| 75 |
} |
| 76 |
|
| 77 |
function removeItem(idx) { set({ items: items.filter(function (_, i) { return i !== idx; }) }); } |
| 78 |
|
| 79 |
var inspector = el(InspectorControls, {}, |
| 80 |
el(PanelBody, { title: __('Layout', 'blockenberg'), initialOpen: true }, |
| 81 |
el(SelectControl, { |
| 82 |
label: __('Layout', 'blockenberg'), value: attr.layout, |
| 83 |
options: [ |
| 84 |
{ label: 'Vertical list', value: 'list' }, |
| 85 |
{ label: 'Grid (multi-column)', value: 'grid' }, |
| 86 |
{ label: 'Alternating (zigzag)', value: 'zigzag' } |
| 87 |
], |
| 88 |
onChange: function (v) { set({ layout: v }); }, __nextHasNoMarginBottom: true |
| 89 |
}), |
| 90 |
attr.layout === 'grid' && el(RangeControl, { label: __('Columns', 'blockenberg'), value: attr.columns, min: 2, max: 4, onChange: function (v) { set({ columns: v }); }, __nextHasNoMarginBottom: true }), |
| 91 |
el(SelectControl, { |
| 92 |
label: __('Text Alignment', 'blockenberg'), value: attr.align, |
| 93 |
options: [{ label: 'Left', value: 'left' }, { label: 'Center', value: 'center' }], |
| 94 |
onChange: function (v) { set({ align: v }); }, __nextHasNoMarginBottom: true |
| 95 |
}), |
| 96 |
el(RangeControl, { label: __('Gap (px)', 'blockenberg'), value: attr.gap, min: 16, max: 120, onChange: function (v) { set({ gap: v }); }, __nextHasNoMarginBottom: true }) |
| 97 |
), |
| 98 |
el(PanelBody, { title: __('Badge', 'blockenberg'), initialOpen: false }, |
| 99 |
el(SelectControl, { |
| 100 |
label: __('Badge Type', 'blockenberg'), value: attr.badgeType, |
| 101 |
options: [ |
| 102 |
{ label: 'Auto number', value: 'number' }, |
| 103 |
{ label: 'Custom (per item)', value: 'custom' }, |
| 104 |
{ label: 'Icon emoji (per item)', value: 'icon' }, |
| 105 |
{ label: 'None', value: 'none' } |
| 106 |
], |
| 107 |
onChange: function (v) { set({ badgeType: v }); }, __nextHasNoMarginBottom: true |
| 108 |
}), |
| 109 |
attr.badgeType !== 'none' && el(SelectControl, { |
| 110 |
label: __('Badge Shape', 'blockenberg'), value: attr.badgeShape, |
| 111 |
options: [{ label: 'Circle', value: 'circle' }, { label: 'Square', value: 'square' }, { label: 'Rounded', value: 'rounded' }], |
| 112 |
onChange: function (v) { set({ badgeShape: v }); }, __nextHasNoMarginBottom: true |
| 113 |
}), |
| 114 |
attr.layout === 'list' && el(ToggleControl, { label: __('Show Connector Line', 'blockenberg'), checked: attr.showConnector, onChange: function (v) { set({ showConnector: v }); }, __nextHasNoMarginBottom: true }), |
| 115 |
attr.showConnector && attr.layout === 'list' && el(SelectControl, { |
| 116 |
label: __('Connector Style', 'blockenberg'), value: attr.connectorStyle, |
| 117 |
options: [{ label: 'Dashed', value: 'dashed' }, { label: 'Solid', value: 'solid' }, { label: 'Dotted', value: 'dotted' }], |
| 118 |
onChange: function (v) { set({ connectorStyle: v }); }, __nextHasNoMarginBottom: true |
| 119 |
}) |
| 120 |
), |
| 121 |
el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false }, |
| 122 |
el(getTypoControl(), { label: __('Section Heading Typography', 'blockenberg'), value: attr.sectionHeadingTypo, onChange: function (v) { set({ sectionHeadingTypo: v }); } }), |
| 123 |
el(getTypoControl(), { label: __('Item Heading Typography', 'blockenberg'), value: attr.itemHeadingTypo, onChange: function (v) { set({ itemHeadingTypo: v }); } }), |
| 124 |
el(getTypoControl(), { label: __('Body Typography', 'blockenberg'), value: attr.bodyTypo, onChange: function (v) { set({ bodyTypo: v }); } }), |
| 125 |
el(getTypoControl(), { label: __('Section Subtext Typography', 'blockenberg'), value: attr.subtextTypo, onChange: function (v) { set({ subtextTypo: v }); } }), |
| 126 |
attr.badgeType !== 'none' && el(RangeControl, { label: __('Badge Size (px)', 'blockenberg'), value: attr.badgeSize, min: 28, max: 80, onChange: function (v) { set({ badgeSize: v }); }, __nextHasNoMarginBottom: true }), |
| 127 |
attr.badgeType !== 'none' && el(RangeControl, { label: __('Badge Font Size (px)', 'blockenberg'), value: attr.badgeFontSize, min: 12, max: 36, onChange: function (v) { set({ badgeFontSize: v }); }, __nextHasNoMarginBottom: true }) |
| 128 |
), |
| 129 |
el(PanelBody, { title: __('Image', 'blockenberg'), initialOpen: false }, |
| 130 |
el(ToggleControl, { label: __('Show Images', 'blockenberg'), checked: attr.showImage, onChange: function (v) { set({ showImage: v }); }, __nextHasNoMarginBottom: true }), |
| 131 |
attr.showImage && el(SelectControl, { |
| 132 |
label: __('Image Position', 'blockenberg'), value: attr.imagePosition, |
| 133 |
options: [{ label: 'Right', value: 'right' }, { label: 'Left', value: 'left' }, { label: 'Above', value: 'above' }], |
| 134 |
onChange: function (v) { set({ imagePosition: v }); }, __nextHasNoMarginBottom: true |
| 135 |
}), |
| 136 |
attr.showImage && el(RangeControl, { label: __('Image Width (px)', 'blockenberg'), value: attr.imageSize, min: 80, max: 500, onChange: function (v) { set({ imageSize: v }); }, __nextHasNoMarginBottom: true }), |
| 137 |
attr.showImage && el(RangeControl, { label: __('Image Radius (px)', 'blockenberg'), value: attr.imageRadius, min: 0, max: 40, onChange: function (v) { set({ imageRadius: v }); }, __nextHasNoMarginBottom: true }) |
| 138 |
), |
| 139 |
el(PanelBody, { title: __('Section Heading', 'blockenberg'), initialOpen: false }, |
| 140 |
el(ToggleControl, { label: __('Show Heading', 'blockenberg'), checked: attr.showHeading, onChange: function (v) { set({ showHeading: v }); }, __nextHasNoMarginBottom: true }), |
| 141 |
el(ToggleControl, { label: __('Show Subtext', 'blockenberg'), checked: attr.showSubtext, onChange: function (v) { set({ showSubtext: v }); }, __nextHasNoMarginBottom: true }) |
| 142 |
), |
| 143 |
el(PanelBody, { title: __('Items', 'blockenberg'), initialOpen: true }, |
| 144 |
items.map(function (it, idx) { |
| 145 |
return el('div', { key: idx, style: { border: '1px solid #e5e7eb', borderRadius: '8px', padding: '10px', marginBottom: '10px' } }, |
| 146 |
el('div', { style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: '8px' } }, |
| 147 |
el('strong', { style: { fontSize: '12px' } }, 'Item ' + (idx + 1)), |
| 148 |
el(Button, { onClick: function () { removeItem(idx); }, variant: 'link', isDestructive: true, isSmall: true }, '✕') |
| 149 |
), |
| 150 |
(attr.badgeType === 'custom') && el(TextControl, { label: __('Badge Text', 'blockenberg'), value: it.customBadge || '', onChange: function (v) { updateItem(idx, 'customBadge', v); }, __nextHasNoMarginBottom: true }), |
| 151 |
(attr.badgeType === 'icon') && el(IP().IconPickerControl, { iconType: it.iconType || 'custom-char', customChar: it.icon || '', dashicon: it.iconDashicon || '', imageUrl: it.iconImageUrl || '', onChangeType: function (v) { updateItem(idx, 'iconType', v); }, onChangeChar: function (v) { updateItem(idx, 'icon', v); }, onChangeDashicon: function (v) { updateItem(idx, 'iconDashicon', v); }, onChangeImageUrl: function (v) { updateItem(idx, 'iconImageUrl', v); } }), |
| 152 |
el(TextControl, { label: __('Heading', 'blockenberg'), value: it.heading, onChange: function (v) { updateItem(idx, 'heading', v); }, __nextHasNoMarginBottom: true }), |
| 153 |
el(TextControl, { label: __('Link (optional)', 'blockenberg'), value: it.link, onChange: function (v) { updateItem(idx, 'link', v); }, placeholder: 'https://', __nextHasNoMarginBottom: true }), |
| 154 |
el(BkbgColorSwatch, { label: __('Accent Color', 'blockenberg'), value: it.accentColor, onChange: function (v) { updateItem(idx, 'accentColor', v); } }), |
| 155 |
attr.showImage && el(MediaUploadCheck, {}, |
| 156 |
el(MediaUpload, { |
| 157 |
onSelect: function (m) { updateItem(idx, 'imageUrl', m.url); updateItem(idx, 'imageId', m.id); updateItem(idx, 'imageAlt', m.alt || ''); }, |
| 158 |
allowedTypes: ['image'], value: it.imageId, |
| 159 |
render: function (ref) { return el(Button, { onClick: ref.open, variant: 'secondary', isSmall: true }, it.imageUrl ? __('Replace Image', 'blockenberg') : __('Upload Image', 'blockenberg')); } |
| 160 |
}) |
| 161 |
) |
| 162 |
); |
| 163 |
}), |
| 164 |
el(Button, { onClick: addItem, variant: 'primary', isSmall: true }, __('+ Add Item', 'blockenberg')) |
| 165 |
), |
| 166 |
el(PanelBody, { title: __('Spacing', 'blockenberg'), initialOpen: false }, |
| 167 |
el(RangeControl, { label: __('Padding Top (px)', 'blockenberg'), value: attr.paddingTop, min: 0, max: 240, onChange: function (v) { set({ paddingTop: v }); }, __nextHasNoMarginBottom: true }), |
| 168 |
el(RangeControl, { label: __('Padding Bottom (px)', 'blockenberg'), value: attr.paddingBottom, min: 0, max: 240, onChange: function (v) { set({ paddingBottom: v }); }, __nextHasNoMarginBottom: true }) |
| 169 |
), |
| 170 |
el(PanelColorSettings, { |
| 171 |
title: __('Colors', 'blockenberg'), initialOpen: false, |
| 172 |
colorSettings: [ |
| 173 |
{ value: attr.bgColor, onChange: function (v) { set({ bgColor: v || '' }); }, label: __('Background', 'blockenberg') }, |
| 174 |
{ value: attr.badgeBg, onChange: function (v) { set({ badgeBg: v || '#6366f1' }); }, label: __('Badge BG', 'blockenberg') }, |
| 175 |
{ value: attr.badgeColor, onChange: function (v) { set({ badgeColor: v || '#ffffff' }); }, label: __('Badge Text', 'blockenberg') }, |
| 176 |
{ value: attr.headingColor, onChange: function (v) { set({ headingColor: v || '#111827' }); }, label: __('Item Heading', 'blockenberg') }, |
| 177 |
{ value: attr.textColor, onChange: function (v) { set({ textColor: v || '#4b5563' }); }, label: __('Item Text', 'blockenberg') }, |
| 178 |
{ value: attr.connectorColor,onChange: function (v) { set({ connectorColor: v || '#d1d5db' }); }, label: __('Connector Line', 'blockenberg') }, |
| 179 |
{ value: attr.sectionHeadingColor, onChange: function (v) { set({ sectionHeadingColor: v || '#111827' }); }, label: __('Section Heading', 'blockenberg') }, |
| 180 |
{ value: attr.subtextColor, onChange: function (v) { set({ subtextColor: v || '#6b7280' }); }, label: __('Section Subtext', 'blockenberg') } |
| 181 |
] |
| 182 |
}) |
| 183 |
); |
| 184 |
|
| 185 |
/* Preview */ |
| 186 |
var isGrid = attr.layout === 'grid'; |
| 187 |
var containerStyle = { |
| 188 |
background: attr.bgColor || 'transparent', |
| 189 |
paddingTop: attr.paddingTop + 'px', |
| 190 |
paddingBottom: attr.paddingBottom + 'px', |
| 191 |
textAlign: attr.align |
| 192 |
}; |
| 193 |
|
| 194 |
var gridStyle = isGrid |
| 195 |
? { display: 'grid', gridTemplateColumns: 'repeat(' + attr.columns + ',1fr)', gap: attr.gap + 'px' } |
| 196 |
: { display: 'flex', flexDirection: 'column', gap: attr.gap + 'px' }; |
| 197 |
|
| 198 |
var bRadius = badgeShapeRadius(attr.badgeShape); |
| 199 |
|
| 200 |
var itemEls = items.map(function (it, idx) { |
| 201 |
var badgeBg = it.accentColor || attr.badgeBg; |
| 202 |
var badgeLabel = attr.badgeType === 'number' ? String(idx + 1) |
| 203 |
: attr.badgeType === 'icon' ? ((it.iconType || 'custom-char') !== 'custom-char' ? IP().buildEditorIcon(it.iconType, it.icon, it.iconDashicon, it.iconImageUrl, it.iconDashiconColor) : (it.icon || '� |
| 204 |
')) |
| 205 |
: attr.badgeType === 'custom' ? (it.customBadge || String(idx + 1)) |
| 206 |
: null; |
| 207 |
|
| 208 |
var hasImage = attr.showImage && it.imageUrl; |
| 209 |
var isHoriz = hasImage && (attr.imagePosition === 'left' || attr.imagePosition === 'right'); |
| 210 |
var itemFlexDir = isHoriz ? (attr.imagePosition === 'right' ? 'row' : 'row-reverse') : 'column'; |
| 211 |
|
| 212 |
var imgEl = hasImage ? el('img', { src: it.imageUrl, alt: it.imageAlt || '', style: { width: attr.imageSize + 'px', height: 'auto', borderRadius: attr.imageRadius + 'px', objectFit: 'cover', flexShrink: 0 } }) : null; |
| 213 |
|
| 214 |
return el('div', { key: idx, style: { position: 'relative', display: isHoriz ? 'flex' : 'block', flexDirection: itemFlexDir, gap: '24px', alignItems: isHoriz ? 'center' : 'initial' } }, |
| 215 |
/* Connector */ |
| 216 |
attr.showConnector && attr.layout === 'list' && idx < items.length - 1 && attr.badgeType !== 'none' && el('div', { style: { position: 'absolute', left: (attr.badgeSize / 2 - 1) + 'px', top: (attr.badgeSize + 6) + 'px', width: '2px', bottom: ('-' + (attr.gap - 4) + 'px'), borderLeft: '2px ' + attr.connectorStyle + ' ' + attr.connectorColor } }), |
| 217 |
el('div', { style: { display: 'flex', gap: '16px', flexDirection: attr.align === 'center' ? 'column' : 'row', alignItems: attr.align === 'center' ? 'center' : 'flex-start', flex: 1 } }, |
| 218 |
attr.hasImage && attr.imagePosition === 'above' && imgEl, |
| 219 |
badgeLabel !== null && el('div', { style: { background: badgeBg, color: attr.badgeColor, width: attr.badgeSize + 'px', height: attr.badgeSize + 'px', borderRadius: bRadius, display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: attr.badgeFontSize + 'px', fontWeight: '800', flexShrink: 0 } }, badgeLabel), |
| 220 |
el('div', { style: { flex: 1 } }, |
| 221 |
el(RichText, { |
| 222 |
tagName: 'h4', value: it.heading, |
| 223 |
onChange: function (v) { updateItem(idx, 'heading', v); }, |
| 224 |
className: 'bkbg-nl-item-heading', |
| 225 |
style: { color: attr.headingColor, margin: '0 0 6px' } |
| 226 |
}), |
| 227 |
el(RichText, { |
| 228 |
tagName: 'p', value: it.content, |
| 229 |
onChange: function (v) { updateItem(idx, 'content', v); }, |
| 230 |
placeholder: __('Description…', 'blockenberg'), |
| 231 |
className: 'bkbg-nl-item-text', |
| 232 |
style: { color: attr.textColor, margin: 0 } |
| 233 |
}) |
| 234 |
), |
| 235 |
isHoriz && imgEl |
| 236 |
) |
| 237 |
); |
| 238 |
}); |
| 239 |
|
| 240 |
return el(Fragment, {}, |
| 241 |
inspector, |
| 242 |
el('div', useBlockProps((function () { |
| 243 |
var _tvf = getTypoCssVars(); |
| 244 |
var s = { |
| 245 |
background: attr.bgColor || 'transparent', |
| 246 |
paddingTop: attr.paddingTop + 'px', |
| 247 |
paddingBottom: attr.paddingBottom + 'px', |
| 248 |
textAlign: attr.align |
| 249 |
}; |
| 250 |
if (_tvf) { Object.assign(s, _tvf(attr.sectionHeadingTypo, '--bkbg-nl-sh-')); Object.assign(s, _tvf(attr.itemHeadingTypo, '--bkbg-nl-ih-')); Object.assign(s, _tvf(attr.bodyTypo, '--bkbg-nl-bd-')); Object.assign(s, _tvf(attr.subtextTypo, '--bkbg-nl-st-')); } |
| 251 |
return { className: 'bkbg-nl-wrap', style: s }; |
| 252 |
})()), |
| 253 |
attr.showHeading && el(RichText, { |
| 254 |
tagName: 'h2', value: attr.heading, onChange: function (v) { set({ heading: v }); }, |
| 255 |
placeholder: __('Section heading…', 'blockenberg'), |
| 256 |
className: 'bkbg-nl-heading', |
| 257 |
style: { color: attr.sectionHeadingColor, marginBottom: '8px' } |
| 258 |
}), |
| 259 |
attr.showSubtext && el(RichText, { |
| 260 |
tagName: 'p', value: attr.subtext, onChange: function (v) { set({ subtext: v }); }, |
| 261 |
placeholder: __('Subtext…', 'blockenberg'), |
| 262 |
className: 'bkbg-nl-subtext', |
| 263 |
style: { color: attr.subtextColor, marginBottom: '32px' } |
| 264 |
}), |
| 265 |
el('div', { style: gridStyle }, itemEls) |
| 266 |
) |
| 267 |
); |
| 268 |
}, |
| 269 |
|
| 270 |
save: function (props) { |
| 271 |
var el2 = wp.element.createElement; |
| 272 |
var ubp = wp.blockEditor.useBlockProps; |
| 273 |
return el2('div', ubp.save(), |
| 274 |
el2('div', { className: 'bkbg-nl-app', 'data-opts': JSON.stringify(props.attributes) }) |
| 275 |
); |
| 276 |
} |
| 277 |
}); |
| 278 |
}() ); |
| 279 |
|