| 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 RichText = wp.blockEditor.RichText; |
| 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 |
|
| 15 |
var DISCLOSURE_TYPES = [ |
| 16 |
{ value: 'sponsored', label: '📢 Sponsored' }, |
| 17 |
{ value: 'paid-partnership', label: '🤝 Paid Partnership' }, |
| 18 |
{ value: 'advertisement', label: '📰 Advertisement' }, |
| 19 |
{ value: 'native-ad', label: '🔗 Native Ad' }, |
| 20 |
{ value: 'affiliate', label: '💰 Affiliate' }, |
| 21 |
{ value: 'custom', label: '✏️ Custom Label' } |
| 22 |
]; |
| 23 |
|
| 24 |
var DEFAULT_LABELS = { |
| 25 |
'sponsored': 'Sponsored', |
| 26 |
'paid-partnership': 'Paid Partnership', |
| 27 |
'advertisement': 'Advertisement', |
| 28 |
'native-ad': 'Native Ad', |
| 29 |
'affiliate': 'Affiliate' |
| 30 |
}; |
| 31 |
|
| 32 |
function getLabel(attr) { |
| 33 |
if (attr.disclosureType === 'custom') return attr.customLabel || 'Custom'; |
| 34 |
return DEFAULT_LABELS[attr.disclosureType] || 'Sponsored'; |
| 35 |
} |
| 36 |
|
| 37 |
var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 38 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 39 |
|
| 40 |
var _IP; |
| 41 |
function IP() { return _IP || (_IP = window.bkbgIconPicker); } |
| 42 |
|
| 43 |
function buildWrapStyle(a) { |
| 44 |
var tv = getTypoCssVars(); |
| 45 |
var s = { |
| 46 |
paddingTop: a.paddingTop + 'px', |
| 47 |
paddingBottom: a.paddingBottom + 'px', |
| 48 |
textAlign: a.align || 'left', |
| 49 |
'--bksd-lb-sz': a.fontSize + 'px', |
| 50 |
'--bksd-lb-w': String(a.fontWeight || '600'), |
| 51 |
'--bksd-lb-lh': String(a.lineHeight || 1.5), |
| 52 |
'--bksd-tx-sz': a.fontSize + 'px', |
| 53 |
'--bksd-tx-w': String(a.fontWeight || '600'), |
| 54 |
'--bksd-tx-lh': String(a.lineHeight || 1.5) |
| 55 |
}; |
| 56 |
Object.assign(s, tv(a.labelTypo, '--bksd-lb-')); |
| 57 |
Object.assign(s, tv(a.textTypo, '--bksd-tx-')); |
| 58 |
return s; |
| 59 |
} |
| 60 |
|
| 61 |
registerBlockType('blockenberg/sponsored-disclosure', { |
| 62 |
edit: function (props) { |
| 63 |
var attr = props.attributes; |
| 64 |
var set = props.setAttributes; |
| 65 |
|
| 66 |
var blockProps = useBlockProps({ style: buildWrapStyle(attr) }); |
| 67 |
|
| 68 |
var label = getLabel(attr); |
| 69 |
|
| 70 |
var pillStyle = { |
| 71 |
display: 'inline-flex', |
| 72 |
alignItems: 'center', |
| 73 |
gap: '5px' |
| 74 |
}; |
| 75 |
|
| 76 |
var bannerStyle = { |
| 77 |
display: 'flex', |
| 78 |
alignItems: 'center', |
| 79 |
gap: '8px', |
| 80 |
padding: '8px 14px' |
| 81 |
}; |
| 82 |
|
| 83 |
var boxStyle = { |
| 84 |
display: 'flex', |
| 85 |
alignItems: 'flex-start', |
| 86 |
gap: '8px', |
| 87 |
padding: '10px 14px' |
| 88 |
}; |
| 89 |
|
| 90 |
var isBanner = attr.style === 'banner'; |
| 91 |
var isBox = attr.style === 'box'; |
| 92 |
var isPill = attr.style === 'pill'; |
| 93 |
|
| 94 |
var containerStyle = Object.assign( |
| 95 |
{}, |
| 96 |
isPill ? pillStyle : isBanner ? bannerStyle : boxStyle, |
| 97 |
{ |
| 98 |
background: attr.bgColor, |
| 99 |
border: '1px solid ' + attr.borderColor, |
| 100 |
borderRadius: (isPill ? attr.borderRadius : isBox ? 8 : 4) + 'px', |
| 101 |
color: attr.textColor |
| 102 |
} |
| 103 |
); |
| 104 |
|
| 105 |
var labelChip = el('span', { className: 'bkbg-spd-label-chip', style: { background: attr.labelBg, color: attr.labelColor, padding: isPill ? '2px 8px' : '1px 8px', borderRadius: '999px', display: 'inline-flex', alignItems: 'center', gap: '4px' } }, |
| 106 |
attr.showIcon && IP().buildEditorIcon(attr.iconType, attr.icon, attr.iconDashicon, attr.iconImageUrl, { dashiconColor: attr.iconDashiconColor, style: { display: 'inline-flex', alignItems: 'center', justifyContent: 'center', lineHeight: 1, fontSize: 'inherit' } }), |
| 107 |
label |
| 108 |
); |
| 109 |
|
| 110 |
var controls = el(InspectorControls, {}, |
| 111 |
el(PanelBody, { title: __('Disclosure', 'blockenberg'), initialOpen: true }, |
| 112 |
el(SelectControl, { |
| 113 |
label: __('Type', 'blockenberg'), value: attr.disclosureType, __nextHasNoMarginBottom: true, |
| 114 |
options: DISCLOSURE_TYPES, |
| 115 |
onChange: function (v) { set({ disclosureType: v }); } |
| 116 |
}), |
| 117 |
attr.disclosureType === 'custom' && el('div', { style: { marginTop: '8px' } }, |
| 118 |
el(TextControl, { label: __('Custom Label', 'blockenberg'), value: attr.customLabel, placeholder: __('Your label…', 'blockenberg'), __nextHasNoMarginBottom: true, onChange: function (v) { set({ customLabel: v }); } }) |
| 119 |
), |
| 120 |
el('div', { style: { marginTop: '8px' } }, |
| 121 |
el(ToggleControl, { label: __('Show Icon', 'blockenberg'), checked: attr.showIcon, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showIcon: v }); } }) |
| 122 |
), |
| 123 |
attr.showIcon && el('div', { style: { marginTop: '8px' } }, |
| 124 |
el(IP().IconPickerControl, IP().iconPickerProps(attr, set)) |
| 125 |
), |
| 126 |
el('div', { style: { marginTop: '8px' } }, |
| 127 |
el(ToggleControl, { label: __('Show Extra Text', 'blockenberg'), checked: attr.showText, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showText: v }); } }) |
| 128 |
) |
| 129 |
), |
| 130 |
el(PanelBody, { title: __('Style', 'blockenberg'), initialOpen: false }, |
| 131 |
el(SelectControl, { |
| 132 |
label: __('Style', 'blockenberg'), value: attr.style, __nextHasNoMarginBottom: true, |
| 133 |
options: [{ label: 'Pill (compact)', value: 'pill' }, { label: 'Banner (full bar)', value: 'banner' }, { label: 'Box (with text)', value: 'box' }], |
| 134 |
onChange: function (v) { set({ style: v }); } |
| 135 |
}), |
| 136 |
el('div', { style: { marginTop: '8px' } }, |
| 137 |
el(SelectControl, { |
| 138 |
label: __('Alignment', 'blockenberg'), value: attr.align, __nextHasNoMarginBottom: true, |
| 139 |
options: [{ label: 'Left', value: 'left' }, { label: 'Center', value: 'center' }, { label: 'Right', value: 'right' }], |
| 140 |
onChange: function (v) { set({ align: v }); } |
| 141 |
}) |
| 142 |
), |
| 143 |
isPill && el('div', { style: { marginTop: '12px' } }, |
| 144 |
el(RangeControl, { label: __('Border Radius', 'blockenberg'), value: attr.borderRadius, min: 0, max: 999, __nextHasNoMarginBottom: true, onChange: function (v) { set({ borderRadius: v }); } }) |
| 145 |
), |
| 146 |
el('div', { style: { marginTop: '12px' } }, |
| 147 |
el(RangeControl, { label: __('Padding Top', 'blockenberg'), value: attr.paddingTop, min: 0, max: 60, __nextHasNoMarginBottom: true, onChange: function (v) { set({ paddingTop: v }); } }) |
| 148 |
), |
| 149 |
el('div', { style: { marginTop: '12px' } }, |
| 150 |
el(RangeControl, { label: __('Padding Bottom', 'blockenberg'), value: attr.paddingBottom, min: 0, max: 60, __nextHasNoMarginBottom: true, onChange: function (v) { set({ paddingBottom: v }); } }) |
| 151 |
) |
| 152 |
), |
| 153 |
|
| 154 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 155 |
getTypoControl()({ label: __('Label', 'blockenberg'), value: attr.labelTypo, onChange: function (v) { set({ labelTypo: v }); } }), |
| 156 |
getTypoControl()({ label: __('Text', 'blockenberg'), value: attr.textTypo, onChange: function (v) { set({ textTypo: v }); } }) |
| 157 |
), |
| 158 |
el(PanelColorSettings, { |
| 159 |
title: __('Colors', 'blockenberg'), initialOpen: false, |
| 160 |
colorSettings: [ |
| 161 |
{ label: __('Container Background', 'blockenberg'), value: attr.bgColor, onChange: function (v) { set({ bgColor: v || '#fff7ed' }); } }, |
| 162 |
{ label: __('Container Border', 'blockenberg'), value: attr.borderColor, onChange: function (v) { set({ borderColor: v || '#fed7aa' }); } }, |
| 163 |
{ label: __('Text', 'blockenberg'), value: attr.textColor, onChange: function (v) { set({ textColor: v || '#9a3412' }); } }, |
| 164 |
{ label: __('Label Background', 'blockenberg'), value: attr.labelBg, onChange: function (v) { set({ labelBg: v || '#f97316' }); } }, |
| 165 |
{ label: __('Label Text', 'blockenberg'), value: attr.labelColor, onChange: function (v) { set({ labelColor: v || '#ffffff' }); } } |
| 166 |
] |
| 167 |
}) |
| 168 |
); |
| 169 |
|
| 170 |
return el('div', blockProps, |
| 171 |
controls, |
| 172 |
el('div', { style: containerStyle }, |
| 173 |
labelChip, |
| 174 |
attr.showText && el(RichText, { |
| 175 |
tagName: 'span', |
| 176 |
className: 'bkbg-spd-text', |
| 177 |
value: attr.text, |
| 178 |
allowedFormats: ['core/italic', 'core/link'], |
| 179 |
placeholder: __('Additional disclosure text…', 'blockenberg'), |
| 180 |
style: { color: attr.textColor }, |
| 181 |
onChange: function (v) { set({ text: v }); } |
| 182 |
}) |
| 183 |
) |
| 184 |
); |
| 185 |
}, |
| 186 |
save: function (props) { |
| 187 |
var attr = props.attributes; |
| 188 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 189 |
return el('div', useBlockProps.save(), |
| 190 |
el('div', { className: 'bkbg-spd-app', 'data-opts': JSON.stringify(attr) }) |
| 191 |
); |
| 192 |
} |
| 193 |
}); |
| 194 |
}() ); |
| 195 |
|