| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var __ = wp.i18n.__; |
| 4 |
var useState = wp.element.useState; |
| 5 |
var registerBlockType = wp.blocks.registerBlockType; |
| 6 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 7 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 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 |
var Button = wp.components.Button; |
| 15 |
|
| 16 |
function getTypographyControl() { return (window.bkbgTypographyControl || function () { return null; }); } |
| 17 |
function getTypoCssVars() { return (window.bkbgTypoCssVars || function () { return {}; }); } |
| 18 |
function _tv(typo, prefix) { var fn = getTypoCssVars(); return fn(typo || {}, prefix); } |
| 19 |
|
| 20 |
registerBlockType('blockenberg/comparison-checklist', { |
| 21 |
title: __('Comparison Checklist', 'blockenberg'), |
| 22 |
icon: 'yes-alt', |
| 23 |
category: 'bkbg-marketing', |
| 24 |
|
| 25 |
edit: function (props) { |
| 26 |
var a = props.attributes; |
| 27 |
var set = props.setAttributes; |
| 28 |
|
| 29 |
// -- helpers -- |
| 30 |
function wrapStyle(a) { |
| 31 |
return Object.assign({ |
| 32 |
'--bkbg-cc-col-a': a.colAColor, |
| 33 |
'--bkbg-cc-col-b': a.colBColor, |
| 34 |
'--bkbg-cc-check': a.checkColor, |
| 35 |
'--bkbg-cc-cross': a.crossColor, |
| 36 |
'--bkbg-cc-bg': a.bgColor, |
| 37 |
'--bkbg-cc-text': a.textColor, |
| 38 |
'--bkbg-cc-border': a.borderColor, |
| 39 |
'--bkbg-cc-row-alt': a.rowBgAlt, |
| 40 |
'--bkbg-cc-font-sz': a.fontSize + 'px', |
| 41 |
'--bkbg-cc-font-w': a.fontWeight, |
| 42 |
'--bkbg-cc-icon-sz': a.iconSize + 'px', |
| 43 |
'--bkbg-cc-head-sz': a.headerSize + 'px', |
| 44 |
'--bkbg-cc-head-w': a.headerWeight, |
| 45 |
'--bkbg-cc-radius': a.borderRadius + 'px', |
| 46 |
'--bkbg-cc-pad': a.cardPadding + 'px', |
| 47 |
'--bkbg-cc-row-pad': a.rowPadding + 'px', |
| 48 |
'--bkbg-cc-heading-c': a.headingColor, |
| 49 |
'--bkbg-cc-heading-sz': a.headingSize + 'px', |
| 50 |
'--bkbg-cc-pt': a.paddingTop + 'px', |
| 51 |
'--bkbg-cc-pb': a.paddingBottom + 'px', |
| 52 |
'--bkbg-cc-line-h': a.lineHeight, |
| 53 |
}, _tv(a.typoHeading, '--bkcc-heading-'), _tv(a.typoRow, '--bkcc-row-'), _tv(a.typoHeader, '--bkcc-header-')); |
| 54 |
} |
| 55 |
|
| 56 |
function iconEl(yes) { |
| 57 |
return el('span', { |
| 58 |
className: 'dashicons ' + (yes ? 'dashicons-yes-alt bkbg-cc-icon--yes' : 'dashicons-dismiss bkbg-cc-icon--no') |
| 59 |
}); |
| 60 |
} |
| 61 |
|
| 62 |
function updateItem(index, key, value) { |
| 63 |
var arr = a.items.slice(); |
| 64 |
arr[index] = Object.assign({}, arr[index]); |
| 65 |
arr[index][key] = value; |
| 66 |
set({ items: arr }); |
| 67 |
} |
| 68 |
|
| 69 |
function addItem() { |
| 70 |
set({ items: a.items.concat([{ text: __('New benefit or comparison point', 'blockenberg'), colA: true, colB: false }]) }); |
| 71 |
} |
| 72 |
|
| 73 |
function removeItem(idx) { |
| 74 |
set({ items: a.items.filter(function (_, i) { return i !== idx; }) }); |
| 75 |
} |
| 76 |
|
| 77 |
function moveItem(idx, dir) { |
| 78 |
var ni = idx + dir; |
| 79 |
if (ni < 0 || ni >= a.items.length) return; |
| 80 |
var arr = a.items.slice(); |
| 81 |
var tmp = arr[idx]; arr[idx] = arr[ni]; arr[ni] = tmp; |
| 82 |
set({ items: arr }); |
| 83 |
} |
| 84 |
|
| 85 |
var styleOptions = [ |
| 86 |
{ label: __('Split Cards', 'blockenberg'), value: 'split' }, |
| 87 |
{ label: __('Single Card', 'blockenberg'), value: 'single' }, |
| 88 |
{ label: __('Minimal', 'blockenberg'), value: 'minimal' }, |
| 89 |
{ label: __('Dark', 'blockenberg'), value: 'dark' }, |
| 90 |
]; |
| 91 |
|
| 92 |
var weightOptions = [ |
| 93 |
{ label: '400', value: 400 }, { label: '500', value: 500 }, |
| 94 |
{ label: '600', value: 600 }, { label: '700', value: 700 } |
| 95 |
]; |
| 96 |
|
| 97 |
// -- inspector -- |
| 98 |
var inspector = el(InspectorControls, {}, |
| 99 |
|
| 100 |
el(PanelBody, { title: __('Column Headings', 'blockenberg'), initialOpen: true }, |
| 101 |
el(ToggleControl, { |
| 102 |
label: __('Show Column Headers', 'blockenberg'), |
| 103 |
checked: a.showHeader, |
| 104 |
__nextHasNoMarginBottom: true, |
| 105 |
onChange: function (v) { set({ showHeader: v }); } |
| 106 |
}), |
| 107 |
el(TextControl, { label: __('Column A Title', 'blockenberg'), value: a.colATitle, onChange: function (v) { set({ colATitle: v }); } }), |
| 108 |
el(TextControl, { label: __('Column B Title', 'blockenberg'), value: a.colBTitle, onChange: function (v) { set({ colBTitle: v }); } }) |
| 109 |
), |
| 110 |
|
| 111 |
el(PanelBody, { title: __('Heading', 'blockenberg'), initialOpen: false }, |
| 112 |
el(ToggleControl, { |
| 113 |
label: __('Show Heading', 'blockenberg'), |
| 114 |
checked: a.showHeading, |
| 115 |
__nextHasNoMarginBottom: true, |
| 116 |
onChange: function (v) { set({ showHeading: v }); } |
| 117 |
}), |
| 118 |
a.showHeading && el(TextControl, { label: __('Heading Text', 'blockenberg'), value: a.headingText, onChange: function (v) { set({ headingText: v }); } }), |
| 119 |
a.showHeading && el(SelectControl, { |
| 120 |
label: __('Heading Align', 'blockenberg'), value: a.headingAlign, |
| 121 |
options: [ |
| 122 |
{ label: __('Left', 'blockenberg'), value: 'left' }, |
| 123 |
{ label: __('Center', 'blockenberg'), value: 'center' } |
| 124 |
], |
| 125 |
onChange: function (v) { set({ headingAlign: v }); } |
| 126 |
}) |
| 127 |
), |
| 128 |
|
| 129 |
el(PanelBody, { title: __('Style & Layout', 'blockenberg'), initialOpen: false }, |
| 130 |
el(SelectControl, { |
| 131 |
label: __('Style', 'blockenberg'), value: a.style, |
| 132 |
options: styleOptions, |
| 133 |
onChange: function (v) { set({ style: v }); } |
| 134 |
}), |
| 135 |
el(ToggleControl, { |
| 136 |
label: __('Alternating Row Background', 'blockenberg'), |
| 137 |
checked: a.rowAlternate, |
| 138 |
__nextHasNoMarginBottom: true, |
| 139 |
onChange: function (v) { set({ rowAlternate: v }); } |
| 140 |
}), |
| 141 |
el(ToggleControl, { |
| 142 |
label: __('Show Icons', 'blockenberg'), |
| 143 |
checked: a.showIcons, |
| 144 |
__nextHasNoMarginBottom: true, |
| 145 |
onChange: function (v) { set({ showIcons: v }); } |
| 146 |
}), |
| 147 |
el(RangeControl, { label: __('Icon Size', 'blockenberg'), value: a.iconSize, onChange: function (v) { set({ iconSize: v }); }, min: 12, max: 32 }), |
| 148 |
el(RangeControl, { label: __('Border Radius', 'blockenberg'), value: a.borderRadius, onChange: function (v) { set({ borderRadius: v }); }, min: 0, max: 32 }), |
| 149 |
el(RangeControl, { label: __('Card Padding', 'blockenberg'), value: a.cardPadding, onChange: function (v) { set({ cardPadding: v }); }, min: 8, max: 64 }), |
| 150 |
el(RangeControl, { label: __('Row Padding', 'blockenberg'), value: a.rowPadding, onChange: function (v) { set({ rowPadding: v }); }, min: 4, max: 32 }), |
| 151 |
el(RangeControl, { label: __('Column Gap', 'blockenberg'), value: a.gap, onChange: function (v) { set({ gap: v }); }, min: 4, max: 48 }) |
| 152 |
), |
| 153 |
|
| 154 |
el(PanelBody, { title: __('Spacing', 'blockenberg'), initialOpen: false }, |
| 155 |
el(RangeControl, { label: __('Padding Top', 'blockenberg'), value: a.paddingTop, onChange: function (v) { set({ paddingTop: v }); }, min: 0, max: 200 }), |
| 156 |
el(RangeControl, { label: __('Padding Bottom', 'blockenberg'), value: a.paddingBottom, onChange: function (v) { set({ paddingBottom: v }); }, min: 0, max: 200 }) |
| 157 |
), |
| 158 |
|
| 159 |
el(PanelBody, { title: __('Comparison Items', 'blockenberg'), initialOpen: false }, |
| 160 |
a.items.map(function (item, idx) { |
| 161 |
return el('div', { key: idx, className: 'bkbg-cc-item-ctrl' }, |
| 162 |
el('div', { className: 'bkbg-cc-item-head' }, |
| 163 |
el('small', { style: { fontWeight: 600 } }, __('Row', 'blockenberg') + ' ' + (idx + 1)), |
| 164 |
el('div', { className: 'bkbg-cc-item-actions' }, |
| 165 |
el(Button, { icon: 'arrow-up-alt2', size: 'small', disabled: idx === 0, onClick: function () { moveItem(idx, -1); } }), |
| 166 |
el(Button, { icon: 'arrow-down-alt2', size: 'small', disabled: idx === a.items.length - 1, onClick: function () { moveItem(idx, 1); } }), |
| 167 |
el(Button, { icon: 'trash', size: 'small', isDestructive: true, onClick: function () { removeItem(idx); } }) |
| 168 |
) |
| 169 |
), |
| 170 |
el(TextControl, { |
| 171 |
label: __('Row text', 'blockenberg'), value: item.text, |
| 172 |
onChange: function (v) { updateItem(idx, 'text', v); } |
| 173 |
}), |
| 174 |
el('div', { style: { display: 'flex', gap: 16, marginBottom: 12 } }, |
| 175 |
el(ToggleControl, { |
| 176 |
label: a.colATitle || 'Col A', |
| 177 |
checked: item.colA, __nextHasNoMarginBottom: true, |
| 178 |
onChange: function (v) { updateItem(idx, 'colA', v); } |
| 179 |
}), |
| 180 |
el(ToggleControl, { |
| 181 |
label: a.colBTitle || 'Col B', |
| 182 |
checked: item.colB, __nextHasNoMarginBottom: true, |
| 183 |
onChange: function (v) { updateItem(idx, 'colB', v); } |
| 184 |
}) |
| 185 |
) |
| 186 |
); |
| 187 |
}), |
| 188 |
el(Button, { variant: 'secondary', onClick: addItem }, __('+ Add Row', 'blockenberg')) |
| 189 |
), |
| 190 |
|
| 191 |
|
| 192 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 193 |
el(getTypographyControl(), { label: __('Heading Typography', 'blockenberg'), value: a.typoHeading, onChange: function (v) { set({ typoHeading: v }); } }), |
| 194 |
el(getTypographyControl(), { label: __('Row Typography', 'blockenberg'), value: a.typoRow, onChange: function (v) { set({ typoRow: v }); } }), |
| 195 |
el(getTypographyControl(), { label: __('Header Typography', 'blockenberg'), value: a.typoHeader, onChange: function (v) { set({ typoHeader: v }); } }) |
| 196 |
), |
| 197 |
el(PanelColorSettings, { |
| 198 |
title: __('Colors', 'blockenberg'), initialOpen: false, |
| 199 |
colorSettings: [ |
| 200 |
{ value: a.colAColor, onChange: function (v) { set({ colAColor: v || '#6c3fb5' }); }, label: __('Column A Accent', 'blockenberg') }, |
| 201 |
{ value: a.colBColor, onChange: function (v) { set({ colBColor: v || '#ef4444' }); }, label: __('Column B Accent', 'blockenberg') }, |
| 202 |
{ value: a.checkColor, onChange: function (v) { set({ checkColor: v || '#22c55e' }); }, label: __('Check Icon Color', 'blockenberg') }, |
| 203 |
{ value: a.crossColor, onChange: function (v) { set({ crossColor: v || '#ef4444' }); }, label: __('Cross Icon Color', 'blockenberg') }, |
| 204 |
{ value: a.bgColor, onChange: function (v) { set({ bgColor: v || '#ffffff' }); }, label: __('Background', 'blockenberg') }, |
| 205 |
{ value: a.textColor, onChange: function (v) { set({ textColor: v || '#1e293b' }); }, label: __('Text Color', 'blockenberg') }, |
| 206 |
{ value: a.borderColor, onChange: function (v) { set({ borderColor: v || '#e2e8f0' }); }, label: __('Border Color', 'blockenberg') }, |
| 207 |
{ value: a.rowBgAlt, onChange: function (v) { set({ rowBgAlt: v || '#f8fafc' }); }, label: __('Alternate Row Color', 'blockenberg') }, |
| 208 |
{ value: a.headingColor, onChange: function (v) { set({ headingColor: v || '#0f172a' }); }, label: __('Heading Color', 'blockenberg') }, |
| 209 |
] |
| 210 |
}) |
| 211 |
); |
| 212 |
|
| 213 |
// -- render canvas -- |
| 214 |
var blockProps = useBlockProps({ className: 'bkbg-cc-wrap bkbg-cc-style--' + a.style, style: wrapStyle(a) }); |
| 215 |
|
| 216 |
function renderTable() { |
| 217 |
return el('div', { className: 'bkbg-cc-table' }, |
| 218 |
// Header row |
| 219 |
a.showHeader && el('div', { className: 'bkbg-cc-header-row' }, |
| 220 |
el('div', { className: 'bkbg-cc-header-text' }), |
| 221 |
el('div', { className: 'bkbg-cc-header-col bkbg-cc-header-col--a' }, |
| 222 |
el('span', { className: 'bkbg-cc-col-title bkbg-cc-col-title--a' }, a.colATitle) |
| 223 |
), |
| 224 |
el('div', { className: 'bkbg-cc-header-col bkbg-cc-header-col--b' }, |
| 225 |
el('span', { className: 'bkbg-cc-col-title bkbg-cc-col-title--b' }, a.colBTitle) |
| 226 |
) |
| 227 |
), |
| 228 |
// Rows |
| 229 |
a.items.map(function (item, idx) { |
| 230 |
var isAlt = a.rowAlternate && idx % 2 === 1; |
| 231 |
return el('div', { |
| 232 |
key: idx, |
| 233 |
className: 'bkbg-cc-row' + (isAlt ? ' bkbg-cc-row--alt' : '') |
| 234 |
}, |
| 235 |
el('div', { className: 'bkbg-cc-row-text' }, item.text), |
| 236 |
el('div', { className: 'bkbg-cc-row-cell bkbg-cc-row-cell--a' }, |
| 237 |
a.showIcons ? iconEl(item.colA) : el('span', {}, item.colA ? '✓' : '—') |
| 238 |
), |
| 239 |
el('div', { className: 'bkbg-cc-row-cell bkbg-cc-row-cell--b' }, |
| 240 |
a.showIcons ? iconEl(item.colB) : el('span', {}, item.colB ? '✓' : '—') |
| 241 |
) |
| 242 |
); |
| 243 |
}) |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
return el('div', blockProps, |
| 248 |
inspector, |
| 249 |
a.showHeading && a.headingText && el('h2', { |
| 250 |
className: 'bkbg-cc-heading', |
| 251 |
style: { textAlign: a.headingAlign } |
| 252 |
}, a.headingText), |
| 253 |
renderTable() |
| 254 |
); |
| 255 |
}, |
| 256 |
|
| 257 |
save: function (props) { |
| 258 |
var a = props.attributes; |
| 259 |
var el = wp.element.createElement; |
| 260 |
|
| 261 |
function wrapStyle(a) { |
| 262 |
return Object.assign({ |
| 263 |
'--bkbg-cc-col-a': a.colAColor, |
| 264 |
'--bkbg-cc-col-b': a.colBColor, |
| 265 |
'--bkbg-cc-check': a.checkColor, |
| 266 |
'--bkbg-cc-cross': a.crossColor, |
| 267 |
'--bkbg-cc-bg': a.bgColor, |
| 268 |
'--bkbg-cc-text': a.textColor, |
| 269 |
'--bkbg-cc-border': a.borderColor, |
| 270 |
'--bkbg-cc-row-alt': a.rowBgAlt, |
| 271 |
'--bkbg-cc-font-sz': a.fontSize + 'px', |
| 272 |
'--bkbg-cc-font-w': a.fontWeight, |
| 273 |
'--bkbg-cc-icon-sz': a.iconSize + 'px', |
| 274 |
'--bkbg-cc-head-sz': a.headerSize + 'px', |
| 275 |
'--bkbg-cc-head-w': a.headerWeight, |
| 276 |
'--bkbg-cc-radius': a.borderRadius + 'px', |
| 277 |
'--bkbg-cc-pad': a.cardPadding + 'px', |
| 278 |
'--bkbg-cc-row-pad': a.rowPadding + 'px', |
| 279 |
'--bkbg-cc-heading-c': a.headingColor, |
| 280 |
'--bkbg-cc-heading-sz': a.headingSize + 'px', |
| 281 |
'--bkbg-cc-pt': a.paddingTop + 'px', |
| 282 |
'--bkbg-cc-pb': a.paddingBottom + 'px', |
| 283 |
'--bkbg-cc-line-h': a.lineHeight, |
| 284 |
}, _tv(a.typoHeading, '--bkcc-heading-'), _tv(a.typoRow, '--bkcc-row-'), _tv(a.typoHeader, '--bkcc-header-')); |
| 285 |
} |
| 286 |
|
| 287 |
var blockProps = wp.blockEditor.useBlockProps.save({ |
| 288 |
className: 'bkbg-cc-wrap bkbg-cc-style--' + a.style, |
| 289 |
style: wrapStyle(a) |
| 290 |
}); |
| 291 |
|
| 292 |
function iconEl(yes) { |
| 293 |
return el('span', { |
| 294 |
className: 'dashicons ' + (yes ? 'dashicons-yes-alt bkbg-cc-icon--yes' : 'dashicons-dismiss bkbg-cc-icon--no') |
| 295 |
}); |
| 296 |
} |
| 297 |
|
| 298 |
return el('div', blockProps, |
| 299 |
a.showHeading && a.headingText && el('h2', { |
| 300 |
className: 'bkbg-cc-heading', |
| 301 |
style: { textAlign: a.headingAlign } |
| 302 |
}, a.headingText), |
| 303 |
el('div', { className: 'bkbg-cc-table' }, |
| 304 |
a.showHeader && el('div', { className: 'bkbg-cc-header-row' }, |
| 305 |
el('div', { className: 'bkbg-cc-header-text' }), |
| 306 |
el('div', { className: 'bkbg-cc-header-col bkbg-cc-header-col--a' }, |
| 307 |
el('span', { className: 'bkbg-cc-col-title bkbg-cc-col-title--a' }, a.colATitle) |
| 308 |
), |
| 309 |
el('div', { className: 'bkbg-cc-header-col bkbg-cc-header-col--b' }, |
| 310 |
el('span', { className: 'bkbg-cc-col-title bkbg-cc-col-title--b' }, a.colBTitle) |
| 311 |
) |
| 312 |
), |
| 313 |
a.items.map(function (item, idx) { |
| 314 |
var isAlt = a.rowAlternate && idx % 2 === 1; |
| 315 |
return el('div', { |
| 316 |
key: idx, |
| 317 |
className: 'bkbg-cc-row' + (isAlt ? ' bkbg-cc-row--alt' : '') |
| 318 |
}, |
| 319 |
el('div', { className: 'bkbg-cc-row-text' }, item.text), |
| 320 |
el('div', { className: 'bkbg-cc-row-cell bkbg-cc-row-cell--a' }, |
| 321 |
a.showIcons ? iconEl(item.colA) : el('span', {}, item.colA ? '✓' : '—') |
| 322 |
), |
| 323 |
el('div', { className: 'bkbg-cc-row-cell bkbg-cc-row-cell--b' }, |
| 324 |
a.showIcons ? iconEl(item.colB) : el('span', {}, item.colB ? '✓' : '—') |
| 325 |
) |
| 326 |
); |
| 327 |
}) |
| 328 |
) |
| 329 |
); |
| 330 |
} |
| 331 |
}); |
| 332 |
}() ); |
| 333 |
|