| 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 |
|
| 18 |
function getTypographyControl() { return (window.bkbgTypographyControl || function () { return null; }); } |
| 19 |
function getTypoCssVars() { return (window.bkbgTypoCssVars || function () { return {}; }); } |
| 20 |
function _tv(typo, prefix) { var fn = getTypoCssVars(); return fn(typo || {}, prefix); } |
| 21 |
|
| 22 |
registerBlockType('blockenberg/certification-wall', { |
| 23 |
edit: function (props) { |
| 24 |
var attr = props.attributes; |
| 25 |
var setAttr = props.setAttributes; |
| 26 |
|
| 27 |
function setCert(idx, key, val) { |
| 28 |
var certs = attr.certs.map(function (c, i) { |
| 29 |
return i === idx ? Object.assign({}, c, { [key]: val }) : c; |
| 30 |
}); |
| 31 |
setAttr({ certs: certs }); |
| 32 |
} |
| 33 |
function addCert() { |
| 34 |
setAttr({ certs: attr.certs.concat([{ name: 'New Certificate', issuer: 'Issuer', year: '2024', imageUrl: '', verifyUrl: '#' }]) }); |
| 35 |
} |
| 36 |
function removeCert(idx) { |
| 37 |
setAttr({ certs: attr.certs.filter(function (_, i) { return i !== idx; }) }); |
| 38 |
} |
| 39 |
|
| 40 |
var isGrid = attr.layout === 'grid'; |
| 41 |
var isLarge = attr.layout === 'large'; |
| 42 |
|
| 43 |
return el(wp.element.Fragment, null, |
| 44 |
el(InspectorControls, null, |
| 45 |
el(PanelBody, { title: __('Content', 'blockenberg'), initialOpen: true }, |
| 46 |
el(TextControl, { label: __('Eyebrow', 'blockenberg'), value: attr.eyebrow, onChange: function (v) { setAttr({ eyebrow: v }); }, __nextHasNoMarginBottom: true }), |
| 47 |
el(TextControl, { label: __('Heading', 'blockenberg'), value: attr.heading, onChange: function (v) { setAttr({ heading: v }); }, __nextHasNoMarginBottom: true }), |
| 48 |
el(TextControl, { label: __('Subtext', 'blockenberg'), value: attr.subtext, onChange: function (v) { setAttr({ subtext: v }); }, __nextHasNoMarginBottom: true }) |
| 49 |
), |
| 50 |
el(PanelBody, { title: __('Layout', 'blockenberg'), initialOpen: false }, |
| 51 |
el(SelectControl, { label: __('Layout', 'blockenberg'), value: attr.layout, options: [{ label: 'Grid', value: 'grid' }, { label: 'List', value: 'list' }, { label: 'Large Cards', value: 'large' }], onChange: function (v) { setAttr({ layout: v }); }, __nextHasNoMarginBottom: true }), |
| 52 |
(isGrid || isLarge) && el(RangeControl, { label: __('Columns', 'blockenberg'), value: attr.columns, min: 2, max: 6, onChange: function (v) { setAttr({ columns: v }); }, __nextHasNoMarginBottom: true }), |
| 53 |
el(RangeControl, { label: __('Padding Top', 'blockenberg'), value: attr.paddingTop, min: 0, max: 200, onChange: function (v) { setAttr({ paddingTop: v }); }, __nextHasNoMarginBottom: true }), |
| 54 |
el(RangeControl, { label: __('Padding Bottom', 'blockenberg'), value: attr.paddingBottom, min: 0, max: 200, onChange: function (v) { setAttr({ paddingBottom: v }); }, __nextHasNoMarginBottom: true }), |
| 55 |
el(RangeControl, { label: __('Max Width', 'blockenberg'), value: attr.maxWidth, min: 600, max: 1600, step: 20, onChange: function (v) { setAttr({ maxWidth: v }); }, __nextHasNoMarginBottom: true }) |
| 56 |
), |
| 57 |
el(PanelBody, { title: __('Display Options', 'blockenberg'), initialOpen: false }, |
| 58 |
el(ToggleControl, { label: __('Show Year', 'blockenberg'), checked: attr.showYear, onChange: function (v) { setAttr({ showYear: v }); }, __nextHasNoMarginBottom: true }), |
| 59 |
el(ToggleControl, { label: __('Show Issuer', 'blockenberg'), checked: attr.showIssuer, onChange: function (v) { setAttr({ showIssuer: v }); }, __nextHasNoMarginBottom: true }), |
| 60 |
el(ToggleControl, { label: __('Show Verify Link', 'blockenberg'), checked: attr.showVerify, onChange: function (v) { setAttr({ showVerify: v }); }, __nextHasNoMarginBottom: true }), |
| 61 |
attr.showVerify && el(TextControl, { label: __('Verify Link Label', 'blockenberg'), value: attr.verifyLabel, onChange: function (v) { setAttr({ verifyLabel: v }); }, __nextHasNoMarginBottom: true }) |
| 62 |
), |
| 63 |
el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false }, |
| 64 |
el(getTypographyControl(), { label: __('Heading', 'blockenberg'), value: attr.typoHeading, onChange: function (v) { setAttr({ typoHeading: v }); } }), |
| 65 |
el(getTypographyControl(), { label: __('Subtext', 'blockenberg'), value: attr.typoSubtext, onChange: function (v) { setAttr({ typoSubtext: v }); } }), |
| 66 |
el(getTypographyControl(), { label: __('Certificate Name', 'blockenberg'), value: attr.typoCertName, onChange: function (v) { setAttr({ typoCertName: v }); } }) |
| 67 |
), |
| 68 |
el(PanelColorSettings, { |
| 69 |
title: __('Colors', 'blockenberg'), initialOpen: false, |
| 70 |
colorSettings: [ |
| 71 |
{ label: __('Background', 'blockenberg'), value: attr.bgColor, onChange: function (v) { setAttr({ bgColor: v || '#f8fafc' }); } }, |
| 72 |
{ label: __('Heading', 'blockenberg'), value: attr.headingColor, onChange: function (v) { setAttr({ headingColor: v || '#111827' }); } }, |
| 73 |
{ label: __('Subtext', 'blockenberg'), value: attr.subColor, onChange: function (v) { setAttr({ subColor: v || '#6b7280' }); } }, |
| 74 |
{ label: __('Eyebrow', 'blockenberg'), value: attr.eyebrowColor, onChange: function (v) { setAttr({ eyebrowColor: v || '#6366f1' }); } }, |
| 75 |
{ label: __('Card Background', 'blockenberg'), value: attr.cardBg, onChange: function (v) { setAttr({ cardBg: v || '#ffffff' }); } }, |
| 76 |
{ label: __('Card Border', 'blockenberg'), value: attr.cardBorder, onChange: function (v) { setAttr({ cardBorder: v || '#e2e8f0' }); } }, |
| 77 |
{ label: __('Cert Name', 'blockenberg'), value: attr.nameColor, onChange: function (v) { setAttr({ nameColor: v || '#111827' }); } }, |
| 78 |
{ label: __('Issuer', 'blockenberg'), value: attr.issuerColor, onChange: function (v) { setAttr({ issuerColor: v || '#6b7280' }); } }, |
| 79 |
{ label: __('Accent', 'blockenberg'), value: attr.accentColor, onChange: function (v) { setAttr({ accentColor: v || '#6366f1' }); } }, |
| 80 |
{ label: __('Verify Link', 'blockenberg'), value: attr.verifyColor, onChange: function (v) { setAttr({ verifyColor: v || '#6366f1' }); } } |
| 81 |
] |
| 82 |
}), |
| 83 |
/* Per-cert panels */ |
| 84 |
el(PanelBody, { title: __('Certificates', 'blockenberg'), initialOpen: false }, |
| 85 |
attr.certs.map(function (cert, idx) { |
| 86 |
return el(PanelBody, { key: idx, title: (cert.name || 'Certificate ' + (idx + 1)), initialOpen: false }, |
| 87 |
el(TextControl, { label: __('Name', 'blockenberg'), value: cert.name, onChange: function (v) { setCert(idx, 'name', v); }, __nextHasNoMarginBottom: true }), |
| 88 |
el(TextControl, { label: __('Issuer', 'blockenberg'), value: cert.issuer, onChange: function (v) { setCert(idx, 'issuer', v); }, __nextHasNoMarginBottom: true }), |
| 89 |
el(TextControl, { label: __('Year', 'blockenberg'), value: cert.year, onChange: function (v) { setCert(idx, 'year', v); }, __nextHasNoMarginBottom: true }), |
| 90 |
el(TextControl, { label: __('Verify URL', 'blockenberg'), value: cert.verifyUrl, onChange: function (v) { setCert(idx, 'verifyUrl', v); }, __nextHasNoMarginBottom: true }), |
| 91 |
el('p', { style: { marginBottom: 4, fontSize: 11, textTransform: 'uppercase', letterSpacing: 1, color: '#888' } }, __('Badge Image', 'blockenberg')), |
| 92 |
el(MediaUploadCheck, null, |
| 93 |
el(MediaUpload, { |
| 94 |
onSelect: function (media) { setCert(idx, 'imageUrl', media.url); }, |
| 95 |
allowedTypes: ['image'], value: cert.imageUrl, |
| 96 |
render: function (rp) { |
| 97 |
return el('div', { style: { marginBottom: 8 } }, |
| 98 |
cert.imageUrl && el('img', { src: cert.imageUrl, style: { maxHeight: 48, display: 'block', marginBottom: 6 } }), |
| 99 |
el(Button, { onClick: rp.open, variant: 'secondary', size: 'small' }, cert.imageUrl ? __('Change', 'blockenberg') : __('Upload Badge', 'blockenberg')), |
| 100 |
cert.imageUrl && el(Button, { onClick: function () { setCert(idx, 'imageUrl', ''); }, variant: 'link', isDestructive: true, size: 'small', style: { marginLeft: 8 } }, __('Remove', 'blockenberg')) |
| 101 |
); |
| 102 |
} |
| 103 |
}) |
| 104 |
), |
| 105 |
el(Button, { onClick: function () { removeCert(idx); }, variant: 'link', isDestructive: true, __nextHasNoMarginBottom: true }, __('Remove Certificate', 'blockenberg')) |
| 106 |
); |
| 107 |
}), |
| 108 |
el(Button, { onClick: addCert, variant: 'secondary', style: { marginTop: 8 } }, __('+ Add Certificate', 'blockenberg')) |
| 109 |
) |
| 110 |
), |
| 111 |
/* Editor preview */ |
| 112 |
el('div', Object.assign(useBlockProps({ className: 'bkbg-crt-editor', style: Object.assign({ background: attr.bgColor }, _tv(attr.typoHeading, '--bkbg-crt-h-'), _tv(attr.typoSubtext, '--bkbg-crt-s-'), _tv(attr.typoCertName, '--bkbg-crt-cn-')) })), |
| 113 |
el('div', { className: 'bkbg-crt-inner', style: { maxWidth: attr.maxWidth + 'px', margin: '0 auto', padding: attr.paddingTop + 'px 24px ' + attr.paddingBottom + 'px' } }, |
| 114 |
el('div', { className: 'bkbg-crt-header', style: { textAlign: 'center', marginBottom: 48 } }, |
| 115 |
el(RichText, { tagName: 'p', className: 'bkbg-crt-eyebrow', style: { color: attr.eyebrowColor }, value: attr.eyebrow, onChange: function (v) { setAttr({ eyebrow: v }); }, placeholder: __('Eyebrow text…', 'blockenberg') }), |
| 116 |
el(RichText, { tagName: 'h2', className: 'bkbg-crt-heading', style: { color: attr.headingColor }, value: attr.heading, onChange: function (v) { setAttr({ heading: v }); }, placeholder: __('Heading…', 'blockenberg') }), |
| 117 |
el(RichText, { tagName: 'p', className: 'bkbg-crt-sub', style: { color: attr.subColor }, value: attr.subtext, onChange: function (v) { setAttr({ subtext: v }); }, placeholder: __('Subtext…', 'blockenberg') }) |
| 118 |
), |
| 119 |
el('div', { className: 'bkbg-crt-grid layout-' + attr.layout, style: { gridTemplateColumns: 'repeat(' + attr.columns + ',1fr)' } }, |
| 120 |
attr.certs.map(function (cert, idx) { |
| 121 |
return el('div', { key: idx, className: 'bkbg-crt-card', style: { background: attr.cardBg, borderColor: attr.cardBorder } }, |
| 122 |
cert.imageUrl |
| 123 |
? el('img', { src: cert.imageUrl, className: 'bkbg-crt-badge' }) |
| 124 |
: el('div', { className: 'bkbg-crt-badge-placeholder', style: { background: attr.accentColor + '22', color: attr.accentColor } }, '🏆'), |
| 125 |
el('div', { className: 'bkbg-crt-card-body' }, |
| 126 |
el('div', { className: 'bkbg-crt-name', style: { color: attr.nameColor } }, cert.name), |
| 127 |
attr.showIssuer && el('div', { className: 'bkbg-crt-issuer', style: { color: attr.issuerColor } }, cert.issuer), |
| 128 |
attr.showYear && el('div', { className: 'bkbg-crt-year', style: { color: attr.issuerColor } }, cert.year), |
| 129 |
attr.showVerify && cert.verifyUrl && el('a', { className: 'bkbg-crt-verify', style: { color: attr.verifyColor }, href: cert.verifyUrl, target: '_blank', rel: 'noopener' }, attr.verifyLabel) |
| 130 |
) |
| 131 |
); |
| 132 |
}) |
| 133 |
) |
| 134 |
) |
| 135 |
) |
| 136 |
); |
| 137 |
}, |
| 138 |
save: function (props) { |
| 139 |
var attr = props.attributes; |
| 140 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 141 |
return el('div', useBlockProps.save(), |
| 142 |
el('div', { className: 'bkbg-crt-app', 'data-opts': JSON.stringify(attr) }) |
| 143 |
); |
| 144 |
} |
| 145 |
}); |
| 146 |
}() ); |
| 147 |
|