| 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 |
var BADGE_PRESETS = ['My Take', 'Hot Take', 'Expert View', 'Analysis', 'Opinion', 'Editor\'s Note', 'Insider View']; |
| 23 |
|
| 24 |
registerBlockType('blockenberg/opinion-box', { |
| 25 |
edit: function (props) { |
| 26 |
var attr = props.attributes; |
| 27 |
var set = props.setAttributes; |
| 28 |
|
| 29 |
var TypoCtrl = getTypoControl(); |
| 30 |
var blockProps = (function () { |
| 31 |
var bp = useBlockProps({ className: 'bkbg-opb-wrap', style: { padding: attr.paddingTop * 8 + 'px 0 ' + attr.paddingBottom * 8 + 'px' } }); |
| 32 |
var _tvf = getTypoCssVars(); |
| 33 |
var s = {}; |
| 34 |
if (_tvf) { |
| 35 |
Object.assign(s, _tvf(attr.opinionTypo, '--bkbg-opb-op-')); |
| 36 |
Object.assign(s, _tvf(attr.authorNameTypo, '--bkbg-opb-an-')); |
| 37 |
Object.assign(s, _tvf(attr.authorTitleTypo, '--bkbg-opb-at-')); |
| 38 |
} |
| 39 |
if (Object.keys(s).length) bp.style = Object.assign(s, bp.style || {}); |
| 40 |
return bp; |
| 41 |
}()); |
| 42 |
|
| 43 |
/* Avatar initials */ |
| 44 |
var initials = (attr.authorName || 'A').split(' ').map(function (w) { return w[0]; }).slice(0, 2).join('').toUpperCase(); |
| 45 |
|
| 46 |
var avatarEl = attr.showAvatar && el('div', { style: { flexShrink: 0 } }, |
| 47 |
el(MediaUploadCheck, {}, |
| 48 |
el(MediaUpload, { |
| 49 |
onSelect: function (m) { set({ avatarUrl: m.url, avatarId: m.id, avatarAlt: m.alt || '' }); }, |
| 50 |
allowedTypes: ['image'], |
| 51 |
value: attr.avatarId, |
| 52 |
render: function (ref) { |
| 53 |
return el('div', { onClick: ref.open, style: { cursor: 'pointer', width: '44px', height: '44px', borderRadius: '50%', overflow: 'hidden', background: attr.avatarBg, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0, border: '2px solid ' + attr.accentColor } }, |
| 54 |
attr.avatarUrl |
| 55 |
? el('img', { src: attr.avatarUrl, alt: attr.avatarAlt, style: { width: '100%', height: '100%', objectFit: 'cover', display: 'block' } }) |
| 56 |
: el('span', { style: { color: attr.avatarColor, fontWeight: 700, fontSize: '16px' } }, initials) |
| 57 |
); |
| 58 |
} |
| 59 |
}) |
| 60 |
) |
| 61 |
); |
| 62 |
|
| 63 |
/* Badge */ |
| 64 |
var badgeEl = attr.showBadge && el('span', { style: { background: attr.badgeBg, color: attr.badgeColor, fontSize: '11px', padding: '3px 10px', borderRadius: '20px', fontWeight: 700, letterSpacing: '0.05em', textTransform: 'uppercase', display: 'inline-block', marginBottom: '10px' } }, attr.badgeText); |
| 65 |
|
| 66 |
/* Opinion text */ |
| 67 |
var opinionEl = el(RichText, { tagName: 'p', className: 'bkbg-opb-text', value: attr.opinion, allowedFormats: ['core/bold', 'core/italic', 'core/link'], placeholder: __('Share your opinion or analysis…', 'blockenberg'), style: { color: attr.textColor, margin: '0 0 12px' }, onChange: function (v) { set({ opinion: v }); } }); |
| 68 |
|
| 69 |
/* Author row */ |
| 70 |
var authorRow = (attr.authorName || attr.authorTitle) && el('div', { style: { display: 'flex', alignItems: 'center', gap: '10px' } }, |
| 71 |
avatarEl, |
| 72 |
el('div', {}, |
| 73 |
attr.authorName && el('div', { className: 'bkbg-opb-author-name', style: { color: attr.authorColor } }, attr.authorName), |
| 74 |
attr.authorTitle && el('div', { className: 'bkbg-opb-author-title', style: { color: attr.titleColor } }, attr.authorTitle) |
| 75 |
) |
| 76 |
); |
| 77 |
|
| 78 |
/* Wrap style by variant */ |
| 79 |
var wrapStyle; |
| 80 |
if (attr.style === 'featured') { |
| 81 |
wrapStyle = { background: attr.bgColor, border: '1px solid ' + attr.borderColor, borderRadius: attr.borderRadius + 'px', padding: '20px 24px', position: 'relative', overflow: 'hidden' }; |
| 82 |
} else if (attr.style === 'inline') { |
| 83 |
wrapStyle = { background: 'transparent', borderLeft: '4px solid ' + attr.accentColor, paddingLeft: '20px', paddingRight: '16px', paddingTop: '4px', paddingBottom: '4px' }; |
| 84 |
} else { /* quote */ |
| 85 |
wrapStyle = { background: attr.bgColor, borderRadius: attr.borderRadius + 'px', padding: '24px 28px 20px', position: 'relative' }; |
| 86 |
} |
| 87 |
|
| 88 |
/* Quote mark decoration for quote style */ |
| 89 |
var quoteMark = attr.style === 'quote' && el('div', { style: { fontSize: '80px', lineHeight: 0.8, color: attr.quoteMarkColor, fontFamily: 'Georgia, serif', marginBottom: '12px', userSelect: 'none' } }, '"'); |
| 90 |
|
| 91 |
var controls = el(InspectorControls, {}, |
| 92 |
el(PanelBody, { title: __('Badge', 'blockenberg'), initialOpen: true }, |
| 93 |
el(ToggleControl, { label: __('Show Badge', 'blockenberg'), checked: attr.showBadge, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showBadge: v }); } }), |
| 94 |
attr.showBadge && el('div', { style: { marginTop: '8px' } }, |
| 95 |
el(TextControl, { label: __('Badge Text', 'blockenberg'), value: attr.badgeText, __nextHasNoMarginBottom: true, onChange: function (v) { set({ badgeText: v }); } }), |
| 96 |
el('div', { style: { marginTop: '6px', display: 'flex', flexWrap: 'wrap', gap: '4px' } }, |
| 97 |
BADGE_PRESETS.map(function (p) { |
| 98 |
return el(Button, { key: p, variant: attr.badgeText === p ? 'primary' : 'secondary', isSmall: true, onClick: function () { set({ badgeText: p }); } }, p); |
| 99 |
}) |
| 100 |
) |
| 101 |
) |
| 102 |
), |
| 103 |
el(PanelBody, { title: __('Author', 'blockenberg'), initialOpen: false }, |
| 104 |
el(ToggleControl, { label: __('Show Avatar', 'blockenberg'), checked: attr.showAvatar, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showAvatar: v }); } }), |
| 105 |
el('div', { style: { marginTop: '8px' } }, |
| 106 |
el(TextControl, { label: __('Author Name', 'blockenberg'), value: attr.authorName, __nextHasNoMarginBottom: true, onChange: function (v) { set({ authorName: v }); } }) |
| 107 |
), |
| 108 |
el('div', { style: { marginTop: '8px' } }, |
| 109 |
el(TextControl, { label: __('Author Title / Role', 'blockenberg'), value: attr.authorTitle, __nextHasNoMarginBottom: true, onChange: function (v) { set({ authorTitle: v }); } }) |
| 110 |
), |
| 111 |
attr.showAvatar && attr.avatarUrl && el('div', { style: { marginTop: '8px' } }, |
| 112 |
el(Button, { variant: 'link', isDestructive: true, onClick: function () { set({ avatarUrl: '', avatarId: 0, avatarAlt: '' }); } }, __('Remove Avatar', 'blockenberg')) |
| 113 |
) |
| 114 |
), |
| 115 |
el(PanelBody, { title: __('Style', 'blockenberg'), initialOpen: false }, |
| 116 |
el(SelectControl, { |
| 117 |
label: __('Style', 'blockenberg'), value: attr.style, __nextHasNoMarginBottom: true, |
| 118 |
options: [{ label: 'Featured (card with background)', value: 'featured' }, { label: 'Inline (left accent bar)', value: 'inline' }, { label: 'Quote (large quote mark)', value: 'quote' }], |
| 119 |
onChange: function (v) { set({ style: v }); } |
| 120 |
}), |
| 121 |
el('div', { style: { marginTop: '12px' } }, |
| 122 |
el(RangeControl, { label: __('Border Radius', 'blockenberg'), value: attr.borderRadius, min: 0, max: 24, __nextHasNoMarginBottom: true, onChange: function (v) { set({ borderRadius: v }); } }) |
| 123 |
), |
| 124 |
el('div', { style: { marginTop: '12px' } }, |
| 125 |
el(RangeControl, { label: __('Padding Top (×8px)', 'blockenberg'), value: attr.paddingTop, min: 0, max: 10, __nextHasNoMarginBottom: true, onChange: function (v) { set({ paddingTop: v }); } }) |
| 126 |
), |
| 127 |
el('div', { style: { marginTop: '12px' } }, |
| 128 |
el(RangeControl, { label: __('Padding Bottom (×8px)', 'blockenberg'), value: attr.paddingBottom, min: 0, max: 10, __nextHasNoMarginBottom: true, onChange: function (v) { set({ paddingBottom: v }); } }) |
| 129 |
) |
| 130 |
), |
| 131 |
el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false }, |
| 132 |
TypoCtrl && el(TypoCtrl, { label: __('Opinion Text', 'blockenberg'), value: attr.opinionTypo, onChange: function (v) { set({ opinionTypo: v }); } }), |
| 133 |
TypoCtrl && el(TypoCtrl, { label: __('Author Name', 'blockenberg'), value: attr.authorNameTypo, onChange: function (v) { set({ authorNameTypo: v }); } }), |
| 134 |
TypoCtrl && el(TypoCtrl, { label: __('Author Title', 'blockenberg'), value: attr.authorTitleTypo, onChange: function (v) { set({ authorTitleTypo: v }); } }) |
| 135 |
), |
| 136 |
el(PanelColorSettings, { |
| 137 |
title: __('Colors', 'blockenberg'), initialOpen: false, |
| 138 |
colorSettings: [ |
| 139 |
{ label: __('Background', 'blockenberg'), value: attr.bgColor, onChange: function (v) { set({ bgColor: v || '#fefce8' }); } }, |
| 140 |
{ label: __('Border', 'blockenberg'), value: attr.borderColor, onChange: function (v) { set({ borderColor: v || '#fde047' }); } }, |
| 141 |
{ label: __('Accent', 'blockenberg'), value: attr.accentColor, onChange: function (v) { set({ accentColor: v || '#ca8a04' }); } }, |
| 142 |
{ label: __('Badge BG', 'blockenberg'), value: attr.badgeBg, onChange: function (v) { set({ badgeBg: v || '#ca8a04' }); } }, |
| 143 |
{ label: __('Badge Text', 'blockenberg'), value: attr.badgeColor, onChange: function (v) { set({ badgeColor: v || '#ffffff' }); } }, |
| 144 |
{ label: __('Opinion Text', 'blockenberg'), value: attr.textColor, onChange: function (v) { set({ textColor: v || '#1c1917' }); } }, |
| 145 |
{ label: __('Author Name', 'blockenberg'), value: attr.authorColor, onChange: function (v) { set({ authorColor: v || '#57534e' }); } }, |
| 146 |
{ label: __('Author Title', 'blockenberg'), value: attr.titleColor, onChange: function (v) { set({ titleColor: v || '#78716c' }); } }, |
| 147 |
{ label: __('Avatar BG', 'blockenberg'), value: attr.avatarBg, onChange: function (v) { set({ avatarBg: v || '#d97706' }); } }, |
| 148 |
{ label: __('Avatar Initials', 'blockenberg'), value: attr.avatarColor, onChange: function (v) { set({ avatarColor: v || '#ffffff' }); } }, |
| 149 |
{ label: __('Quote Mark', 'blockenberg'), value: attr.quoteMarkColor, onChange: function (v) { set({ quoteMarkColor: v || '#fde047' }); } } |
| 150 |
] |
| 151 |
}) |
| 152 |
); |
| 153 |
|
| 154 |
return el('div', blockProps, |
| 155 |
controls, |
| 156 |
el('div', { style: wrapStyle }, |
| 157 |
badgeEl, |
| 158 |
quoteMark, |
| 159 |
opinionEl, |
| 160 |
authorRow |
| 161 |
) |
| 162 |
); |
| 163 |
}, |
| 164 |
save: function (props) { |
| 165 |
var attr = props.attributes; |
| 166 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 167 |
return el('div', useBlockProps.save(), |
| 168 |
el('div', { className: 'bkbg-opb-app', 'data-opts': JSON.stringify(attr) }) |
| 169 |
); |
| 170 |
} |
| 171 |
}); |
| 172 |
}() ); |
| 173 |
|