| 1 |
/* ==================================================== |
| 2 |
Announcement Strip — editor (index.js) |
| 3 |
Block: blockenberg/announcement-strip |
| 4 |
==================================================== */ |
| 5 |
( function () { |
| 6 |
var el = wp.element.createElement; |
| 7 |
var Fragment = wp.element.Fragment; |
| 8 |
var useState = wp.element.useState; |
| 9 |
var RichText = wp.blockEditor.RichText; |
| 10 |
var registerBlockType = wp.blocks.registerBlockType; |
| 11 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 12 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 13 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 14 |
var PanelBody = wp.components.PanelBody; |
| 15 |
var RangeControl = wp.components.RangeControl; |
| 16 |
var SelectControl = wp.components.SelectControl; |
| 17 |
var ToggleControl = wp.components.ToggleControl; |
| 18 |
var TextControl = wp.components.TextControl; |
| 19 |
var ColorPicker = wp.components.ColorPicker; |
| 20 |
var Popover = wp.components.Popover; |
| 21 |
var Button = wp.components.Button; |
| 22 |
var GradientPicker = wp.components.__experimentalGradientPicker; |
| 23 |
var __ = wp.i18n.__; |
| 24 |
|
| 25 |
var _TypographyControl, _typoCssVars; |
| 26 |
function getTypographyControl() { return _TypographyControl || (_TypographyControl = window.bkbgTypographyControl); } |
| 27 |
function getTypoCssVars() { return _typoCssVars || (_typoCssVars = window.bkbgTypoCssVars); } |
| 28 |
|
| 29 |
/* Preset backgrounds */ |
| 30 |
var PRESETS = { |
| 31 |
gradient: 'linear-gradient(90deg,#6c3fb5 0%,#2563eb 100%)', |
| 32 |
dark: '#0f172a', |
| 33 |
promo: '#6c3fb5', |
| 34 |
info: '#0ea5e9', |
| 35 |
warning: '#f59e0b', |
| 36 |
success: '#059669', |
| 37 |
}; |
| 38 |
|
| 39 |
function getBg(a) { |
| 40 |
if (a.style === 'gradient') return a.bgGradient || PRESETS.gradient; |
| 41 |
if (a.style === 'custom') return a.bgColor; |
| 42 |
return PRESETS[a.style] || PRESETS.promo; |
| 43 |
} |
| 44 |
|
| 45 |
function wrapStyle(a) { |
| 46 |
var _tv = getTypoCssVars(); |
| 47 |
var s = { |
| 48 |
'--bkbg-anst-text-c': a.textColor, |
| 49 |
'--bkbg-anst-link-c': a.linkColor, |
| 50 |
'--bkbg-anst-py': a.paddingY + 'px', |
| 51 |
'--bkbg-anst-fs': a.fontSize + 'px', |
| 52 |
'--bkbg-anst-badge-bg':a.badgeBg, |
| 53 |
'--bkbg-anst-badge-c': a.badgeColor, |
| 54 |
background: getBg(a), |
| 55 |
}; |
| 56 |
Object.assign(s, _tv(a.textTypo || {}, '--bkbg-anst-')); |
| 57 |
return s; |
| 58 |
} |
| 59 |
|
| 60 |
/* ── colour-swatch + popover ─────────────────────────────── */ |
| 61 |
function BkbgColorSwatch(p) { |
| 62 |
var st = useState(false); |
| 63 |
var open = st[0], setOpen = st[1]; |
| 64 |
return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } }, |
| 65 |
el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label), |
| 66 |
el('div', { style:{ position:'relative', flexShrink:0 } }, |
| 67 |
el('button', { |
| 68 |
type:'button', title: p.value || 'none', |
| 69 |
onClick: function(){ setOpen(!open); }, |
| 70 |
style:{ width:'28px', height:'28px', borderRadius:'4px', |
| 71 |
border: open ? '2px solid #007cba' : '2px solid #ddd', |
| 72 |
cursor:'pointer', padding:0, display:'block', |
| 73 |
background: p.value || '#ffffff', flexShrink:0 } |
| 74 |
}), |
| 75 |
open && el(Popover, { position:'bottom left', onClose:function(){ setOpen(false); } }, |
| 76 |
el('div', { style:{ padding:'8px' }, onMouseDown:function(e){ e.stopPropagation(); } }, |
| 77 |
el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } }, |
| 78 |
el('strong', { style:{ fontSize:'12px' } }, p.label), |
| 79 |
el(Button, { icon:'no-alt', isSmall:true, onClick:function(){ setOpen(false); } }) |
| 80 |
), |
| 81 |
el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange }) |
| 82 |
) |
| 83 |
) |
| 84 |
) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
registerBlockType('blockenberg/announcement-strip', { |
| 89 |
edit: function (props) { |
| 90 |
var a = props.attributes; |
| 91 |
var setAttr = props.setAttributes; |
| 92 |
|
| 93 |
var blockProps = useBlockProps({ |
| 94 |
className: 'bkbg-anst-wrap bkbg-anst-style--' + a.style + ' bkbg-anst-align--' + a.textAlign, |
| 95 |
style: wrapStyle(a) |
| 96 |
}); |
| 97 |
|
| 98 |
var inspector = el(InspectorControls, null, |
| 99 |
el(PanelBody, { title: __('Content','blockenberg'), initialOpen: true }, |
| 100 |
el(ToggleControl, { label:__('Show badge','blockenberg'), checked:a.showBadge, onChange:function(v){ setAttr({showBadge:v}); } }), |
| 101 |
el(TextControl, { label:__('Badge text','blockenberg'), value:a.badgeText, onChange:function(v){ setAttr({badgeText:v}); } }), |
| 102 |
el(ToggleControl, { label:__('Show link','blockenberg'), checked:a.showLink, onChange:function(v){ setAttr({showLink:v}); } }), |
| 103 |
el(TextControl, { label:__('Link label','blockenberg'), value:a.linkLabel, onChange:function(v){ setAttr({linkLabel:v}); } }), |
| 104 |
el(TextControl, { label:__('Link URL','blockenberg'), value:a.linkUrl, onChange:function(v){ setAttr({linkUrl:v}); } }), |
| 105 |
el(SelectControl, { label:__('Link target','blockenberg'), value:a.linkTarget, options:[{label:'Same tab',value:'_self'},{label:'New tab',value:'_blank'}], onChange:function(v){ setAttr({linkTarget:v}); } }), |
| 106 |
el(ToggleControl, { label:__('Show close button','blockenberg'), checked:a.showClose, onChange:function(v){ setAttr({showClose:v}); } }), |
| 107 |
el(TextControl, { label:__('Close ID (unique per bar)','blockenberg'), value:a.closeId, onChange:function(v){ setAttr({closeId:v}); } }), |
| 108 |
el(RangeControl, { label:__('Remember close (days)','blockenberg'), value:a.closeDays, min:0, max:365, onChange:function(v){ setAttr({closeDays:v}); } }) |
| 109 |
), |
| 110 |
el(PanelBody, { title: __('Countdown'), initialOpen: false }, |
| 111 |
el(ToggleControl, { label:__('Show countdown','blockenberg'), checked:a.showCountdown, onChange:function(v){ setAttr({showCountdown:v}); } }), |
| 112 |
el(TextControl, { label:__('End date (YYYY-MM-DD HH:MM)','blockenberg'), value:a.countdownDate, onChange:function(v){ setAttr({countdownDate:v}); }, help:__('e.g. 2026-12-31 23:59','blockenberg') }) |
| 113 |
), |
| 114 |
el(PanelBody, { title: __('Style','blockenberg'), initialOpen: false }, |
| 115 |
el(SelectControl, { label:__('Preset style','blockenberg'), value:a.style, options:[ |
| 116 |
{label:'Gradient (purple→blue)', value:'gradient'}, |
| 117 |
{label:'Purple', value:'promo'}, |
| 118 |
{label:'Dark', value:'dark'}, |
| 119 |
{label:'Info (blue)', value:'info'}, |
| 120 |
{label:'Warning (amber)', value:'warning'}, |
| 121 |
{label:'Success (green)', value:'success'}, |
| 122 |
{label:'Custom color', value:'custom'}, |
| 123 |
], onChange:function(v){ setAttr({style:v}); } }), |
| 124 |
a.style === 'gradient' ? el('div', { style:{ marginBottom:'12px' } }, |
| 125 |
el('p', { style:{ fontSize:'11px', fontWeight:600, margin:'0 0 6px', textTransform:'uppercase', color:'#757575' } }, __('Custom gradient','blockenberg')), |
| 126 |
el(GradientPicker, { value: a.bgGradient, onChange:function(v){ setAttr({bgGradient:v}); } }) |
| 127 |
) : null, |
| 128 |
a.style === 'custom' ? el(BkbgColorSwatch, { label:__('Background color','blockenberg'), value:a.bgColor, onChange:function(v){ setAttr({bgColor:v}); } }) : null, |
| 129 |
el(SelectControl, { label:__('Text align','blockenberg'), value:a.textAlign, options:[{label:'Center',value:'center'},{label:'Left',value:'left'}], onChange:function(v){ setAttr({textAlign:v}); } }), |
| 130 |
el(RangeControl, { label:__('Vertical padding (px)','blockenberg'), value:a.paddingY, min:4, max:32, onChange:function(v){ setAttr({paddingY:v}); } }), |
| 131 |
), |
| 132 |
|
| 133 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 134 |
el(getTypographyControl(), { label: __('Text Typography', 'blockenberg'), value: a.textTypo || {}, onChange: function (v) { setAttr({ textTypo: v }); } }) |
| 135 |
), |
| 136 |
el(PanelColorSettings, { title:__('Colors','blockenberg'), initialOpen:false, colorSettings:[ |
| 137 |
{label:__('Text color','blockenberg'), value:a.textColor, onChange:function(v){ setAttr({textColor:v||''}); }}, |
| 138 |
{label:__('Link color','blockenberg'), value:a.linkColor, onChange:function(v){ setAttr({linkColor:v||''}); }}, |
| 139 |
{label:__('Badge bg','blockenberg'), value:a.badgeBg, onChange:function(v){ setAttr({badgeBg:v||''}); }}, |
| 140 |
{label:__('Badge text','blockenberg'), value:a.badgeColor, onChange:function(v){ setAttr({badgeColor:v||''}); }}, |
| 141 |
] }) |
| 142 |
); |
| 143 |
|
| 144 |
return el(Fragment, null, |
| 145 |
inspector, |
| 146 |
el('div', blockProps, |
| 147 |
el('div', { className:'bkbg-anst-inner' }, |
| 148 |
a.showBadge ? el('span', { className:'bkbg-anst-badge' }, a.badgeText) : null, |
| 149 |
el(RichText, { |
| 150 |
tagName: 'span', |
| 151 |
className: 'bkbg-anst-text', |
| 152 |
value: a.text, |
| 153 |
onChange: function(v){ setAttr({text:v}); }, |
| 154 |
placeholder: __('Announcement message…','blockenberg'), |
| 155 |
allowedFormats: ['core/bold','core/italic'], |
| 156 |
}), |
| 157 |
a.showLink ? el('a', { href:'#', className:'bkbg-anst-link', onClick:function(e){ e.preventDefault(); } }, a.linkLabel) : null, |
| 158 |
a.showCountdown ? el('span', { className:'bkbg-anst-countdown', 'data-countdown':a.countdownDate }, el('span', { className:'bkbg-anst-cd-timer' }, '0d 00:00:00')) : null |
| 159 |
), |
| 160 |
a.showClose ? el('button', { className:'bkbg-anst-close', type:'button', 'aria-label':__('Close','blockenberg'), disabled:true }, '×') : null |
| 161 |
) |
| 162 |
); |
| 163 |
}, |
| 164 |
|
| 165 |
save: function (props) { |
| 166 |
var a = props.attributes; |
| 167 |
var blockProps = wp.blockEditor.useBlockProps.save({ |
| 168 |
className: 'bkbg-anst-wrap bkbg-anst-style--' + a.style + ' bkbg-anst-align--' + a.textAlign, |
| 169 |
style: wrapStyle(a), |
| 170 |
'data-close-id': a.showClose ? a.closeId : undefined, |
| 171 |
'data-close-days': a.showClose ? a.closeDays : undefined, |
| 172 |
}); |
| 173 |
return el('div', blockProps, |
| 174 |
el('div', { className:'bkbg-anst-inner' }, |
| 175 |
a.showBadge ? el('span', { className:'bkbg-anst-badge' }, a.badgeText) : null, |
| 176 |
el(RichText.Content, { tagName:'span', className:'bkbg-anst-text', value:a.text }), |
| 177 |
a.showLink ? el('a', { href:a.linkUrl, className:'bkbg-anst-link', target:a.linkTarget, rel:a.linkTarget==='_blank'?'noopener noreferrer':undefined }, a.linkLabel) : null, |
| 178 |
a.showCountdown ? el('span', { className:'bkbg-anst-countdown', 'data-countdown':a.countdownDate }, el('span', { className:'bkbg-anst-cd-timer' }, '…')) : null |
| 179 |
), |
| 180 |
a.showClose ? el('button', { className:'bkbg-anst-close', type:'button', 'aria-label':'Close' }, '×') : null |
| 181 |
); |
| 182 |
} |
| 183 |
}); |
| 184 |
}() ); |
| 185 |
|