| 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 |
var _tc, _tv; |
| 19 |
function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 20 |
function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 21 |
|
| 22 |
registerBlockType('blockenberg/lead-magnet', { |
| 23 |
edit: function (props) { |
| 24 |
var a = props.attributes; |
| 25 |
var set = props.setAttributes; |
| 26 |
var blockProps = useBlockProps((function () { |
| 27 |
var _tvFn = getTypoCssVars(); |
| 28 |
var s = {}; |
| 29 |
if (_tvFn) { |
| 30 |
Object.assign(s, _tvFn(a.headingTypo, '--bkbg-lgm-h-')); |
| 31 |
Object.assign(s, _tvFn(a.subtextTypo, '--bkbg-lgm-st-')); |
| 32 |
Object.assign(s, _tvFn(a.buttonTypo, '--bkbg-lgm-bt-')); |
| 33 |
} |
| 34 |
return { className: 'bkbg-lgm-editor', style: s }; |
| 35 |
})()); |
| 36 |
|
| 37 |
var isCentered = a.layout === 'centered'; |
| 38 |
var isLeft = a.layout === 'image-left'; |
| 39 |
|
| 40 |
function updateBenefit(idx, val) { |
| 41 |
var next = a.benefits.map(function (b, i) { return i === idx ? { text: val } : b; }); |
| 42 |
set({ benefits: next }); |
| 43 |
} |
| 44 |
|
| 45 |
/* --- Mockup image --- */ |
| 46 |
var mockupEl = el(MediaUploadCheck, {}, |
| 47 |
el(MediaUpload, { |
| 48 |
onSelect: function (media) { set({ imageUrl: media.url, imageId: media.id, imageAlt: media.alt || '' }); }, |
| 49 |
allowedTypes: ['image'], |
| 50 |
value: a.imageId, |
| 51 |
render: function (ref) { |
| 52 |
var imgContent = a.imageUrl |
| 53 |
? el('img', { src: a.imageUrl, alt: a.imageAlt, className: 'bkbg-lgm-mockup-img bkbg-lgm-mockup-img--' + a.imageStyle }) |
| 54 |
: el('div', { className: 'bkbg-lgm-mockup-placeholder' }, '📥'); |
| 55 |
return el('div', { |
| 56 |
className: 'bkbg-lgm-mockup-wrap bkbg-lgm-mockup-wrap--' + a.imageStyle, |
| 57 |
onClick: ref.open, |
| 58 |
style: { cursor: 'pointer', margin: isCentered ? '0 auto 32px' : '0' } |
| 59 |
}, imgContent); |
| 60 |
} |
| 61 |
}) |
| 62 |
); |
| 63 |
|
| 64 |
/* --- Text / form panel --- */ |
| 65 |
var textPanel = el('div', { className: 'bkbg-lgm-text' }, |
| 66 |
a.showBadge && el('div', { style: { marginBottom: '14px' } }, |
| 67 |
el('span', { style: { background: a.badgeBg, color: a.badgeColor, fontSize: '11px', fontWeight: '800', letterSpacing: '.1em', textTransform: 'uppercase', padding: '4px 12px', borderRadius: '999px' } }, |
| 68 |
el(RichText, { tagName: 'span', value: a.badge, onChange: function (v) { set({ badge: v }); } }) |
| 69 |
) |
| 70 |
), |
| 71 |
el(RichText, { tagName: 'h2', className: 'bkbg-lgm-heading', value: a.heading, style: { color: a.headingColor, margin: '0 0 12px' }, onChange: function (v) { set({ heading: v }); } }), |
| 72 |
el(RichText, { tagName: 'p', className: 'bkbg-lgm-subtext', value: a.subtext, style: { color: a.subtextColor, margin: '0 0 20px' }, onChange: function (v) { set({ subtext: v }); } }), |
| 73 |
a.showBenefits && el('ul', { style: { listStyle: 'none', padding: 0, margin: '0 0 24px', display: 'flex', flexDirection: 'column', gap: '10px' } }, |
| 74 |
a.benefits.map(function (b, idx) { |
| 75 |
return el('li', { key: idx, style: { display: 'flex', alignItems: 'center', gap: '10px' } }, |
| 76 |
el('span', { style: { color: a.checkColor, fontSize: '16px', fontWeight: '700', flexShrink: 0 } }, '✓'), |
| 77 |
el(TextControl, { value: b.text, onChange: function (v) { updateBenefit(idx, v); }, style: { flex: 1, margin: 0 }, __nextHasNoMarginBottom: true }), |
| 78 |
el(Button, { isDestructive: true, isSmall: true, onClick: function () { set({ benefits: a.benefits.filter(function (_, i) { return i !== idx; }) }); } }, '✕') |
| 79 |
) }), |
| 80 |
el(Button, { isSecondary: true, isSmall: true, onClick: function () { set({ benefits: a.benefits.concat([{ text: 'New benefit' }]) }); } }, '+ Add item') |
| 81 |
), |
| 82 |
a.formEnabled && el('div', { className: 'bkbg-lgm-form-preview' }, |
| 83 |
el('div', { style: { display: 'flex', gap: '8px', flexWrap: 'wrap' } }, |
| 84 |
el('input', { type: 'email', disabled: true, placeholder: a.formPlaceholder, style: { flex: '1 1 200px', padding: '14px 16px', border: '1.5px solid #d1d5db', borderRadius: '8px', fontSize: '15px', background: '#fff', minWidth: '0' } }), |
| 85 |
el('button', { disabled: true, className: 'bkbg-lgm-submit', style: { background: a.ctaBg, color: a.ctaColor, padding: '14px 20px', borderRadius: '8px', border: 'none', cursor: 'pointer', whiteSpace: 'nowrap' } }, a.formSubmitLabel) |
| 86 |
), |
| 87 |
a.showPrivacy && el('p', { style: { fontSize: '12px', color: a.privacyColor, marginTop: '8px', marginBottom: 0, textAlign: isCentered ? 'center' : 'left' } }, |
| 88 |
el(RichText, { tagName: 'span', value: a.privacyNote, onChange: function (v) { set({ privacyNote: v }); } }) |
| 89 |
) |
| 90 |
) |
| 91 |
); |
| 92 |
|
| 93 |
var innerStyle = isCentered |
| 94 |
? { display: 'flex', flexDirection: 'column', alignItems: 'center', textAlign: 'center' } |
| 95 |
: { display: 'flex', gap: '60px', alignItems: 'center', flexDirection: isLeft ? 'row' : 'row-reverse' }; |
| 96 |
|
| 97 |
var controls = el(InspectorControls, {}, |
| 98 |
el(PanelBody, { title: __('Layout & Content', 'blockenberg'), initialOpen: true }, |
| 99 |
el(SelectControl, { label: __('Layout', 'blockenberg'), value: a.layout, options: [{ label: 'Image Right', value: 'image-right' }, { label: 'Image Left', value: 'image-left' }, { label: 'Centered', value: 'centered' }], onChange: function (v) { set({ layout: v }); }, __nextHasNoMarginBottom: true }), |
| 100 |
el(SelectControl, { label: __('Image Style', 'blockenberg'), value: a.imageStyle, options: [{ label: 'Floating (shadow)', value: 'floating' }, { label: 'Book', value: 'book' }, { label: 'Flat', value: 'flat' }], onChange: function (v) { set({ imageStyle: v }); }, __nextHasNoMarginBottom: true }), |
| 101 |
el(ToggleControl, { label: __('Show Badge', 'blockenberg'), checked: a.showBadge, onChange: function (v) { set({ showBadge: v }); }, __nextHasNoMarginBottom: true }), |
| 102 |
el(ToggleControl, { label: __('Show Benefits', 'blockenberg'), checked: a.showBenefits, onChange: function (v) { set({ showBenefits: v }); }, __nextHasNoMarginBottom: true }), |
| 103 |
el(ToggleControl, { label: __('Show Form', 'blockenberg'), checked: a.formEnabled, onChange: function (v) { set({ formEnabled: v }); }, __nextHasNoMarginBottom: true }), |
| 104 |
a.formEnabled && el(TextControl, { label: __('Form Action URL (optional)', 'blockenberg'), value: a.formAction, onChange: function (v) { set({ formAction: v }); }, __nextHasNoMarginBottom: true }), |
| 105 |
el(ToggleControl, { label: __('Show Privacy Note', 'blockenberg'), checked: a.showPrivacy, onChange: function (v) { set({ showPrivacy: v }); }, __nextHasNoMarginBottom: true }), |
| 106 |
el(RangeControl, { label: __('Max Width (px)', 'blockenberg'), value: a.maxWidth, min: 600, max: 1400, onChange: function (v) { set({ maxWidth: v }); }, __nextHasNoMarginBottom: true }), |
| 107 |
el(RangeControl, { label: __('Padding Top', 'blockenberg'), value: a.paddingTop, min: 0, max: 160, onChange: function (v) { set({ paddingTop: v }); }, __nextHasNoMarginBottom: true }), |
| 108 |
el(RangeControl, { label: __('Padding Bottom', 'blockenberg'), value: a.paddingBottom, min: 0, max: 160, onChange: function (v) { set({ paddingBottom: v }); }, __nextHasNoMarginBottom: true }) |
| 109 |
), |
| 110 |
|
| 111 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 112 |
getTypoControl() && el(getTypoControl(), { label: __('Heading'), value: a.headingTypo || {}, onChange: function (v) { set({ headingTypo: v }); } }), |
| 113 |
getTypoControl() && el(getTypoControl(), { label: __('Subtext'), value: a.subtextTypo || {}, onChange: function (v) { set({ subtextTypo: v }); } }), |
| 114 |
getTypoControl() && el(getTypoControl(), { label: __('Button'), value: a.buttonTypo || {}, onChange: function (v) { set({ buttonTypo: v }); } }) |
| 115 |
), |
| 116 |
el(PanelColorSettings, { |
| 117 |
title: __('Colors', 'blockenberg'), initialOpen: false, |
| 118 |
colorSettings: [ |
| 119 |
{ label: __('Background', 'blockenberg'), value: a.bgColor, onChange: function (v) { set({ bgColor: v || '#f8f5ff' }); } }, |
| 120 |
{ label: __('Badge BG', 'blockenberg'), value: a.badgeBg, onChange: function (v) { set({ badgeBg: v || '#dcfce7' }); } }, |
| 121 |
{ label: __('Badge Text', 'blockenberg'), value: a.badgeColor, onChange: function (v) { set({ badgeColor: v || '#166534' }); } }, |
| 122 |
{ label: __('Heading', 'blockenberg'), value: a.headingColor, onChange: function (v) { set({ headingColor: v || '#111827' }); } }, |
| 123 |
{ label: __('Subtext', 'blockenberg'), value: a.subtextColor, onChange: function (v) { set({ subtextColor: v || '#4b5563' }); } }, |
| 124 |
{ label: __('Checkmark', 'blockenberg'), value: a.checkColor, onChange: function (v) { set({ checkColor: v || '#7c3aed' }); } }, |
| 125 |
{ label: __('CTA BG', 'blockenberg'), value: a.ctaBg, onChange: function (v) { set({ ctaBg: v || '#7c3aed' }); } }, |
| 126 |
{ label: __('CTA Text', 'blockenberg'), value: a.ctaColor, onChange: function (v) { set({ ctaColor: v || '#ffffff' }); } }, |
| 127 |
{ label: __('Privacy Text', 'blockenberg'), value: a.privacyColor, onChange: function (v) { set({ privacyColor: v || '#9ca3af' }); } } |
| 128 |
] |
| 129 |
}) |
| 130 |
); |
| 131 |
|
| 132 |
return el('div', blockProps, |
| 133 |
controls, |
| 134 |
el('div', { style: { background: a.bgColor, paddingTop: a.paddingTop + 'px', paddingBottom: a.paddingBottom + 'px' } }, |
| 135 |
el('div', { style: Object.assign({ maxWidth: a.maxWidth + 'px', margin: '0 auto', padding: '0 24px' }, innerStyle) }, |
| 136 |
mockupEl, |
| 137 |
textPanel |
| 138 |
) |
| 139 |
) |
| 140 |
); |
| 141 |
}, |
| 142 |
|
| 143 |
save: function (props) { |
| 144 |
return el('div', wp.blockEditor.useBlockProps.save(), |
| 145 |
el('div', { className: 'bkbg-lgm-app', 'data-opts': JSON.stringify(props.attributes) }) |
| 146 |
); |
| 147 |
} |
| 148 |
}); |
| 149 |
}() ); |
| 150 |
|