| 1 |
/* ==================================================== |
| 2 |
Gradient CTA — editor (index.js) |
| 3 |
Block: blockenberg/gradient-cta |
| 4 |
==================================================== */ |
| 5 |
( function () { |
| 6 |
var el = wp.element.createElement; |
| 7 |
var Fragment = wp.element.Fragment; |
| 8 |
var RichText = wp.blockEditor.RichText; |
| 9 |
var registerBlockType = wp.blocks.registerBlockType; |
| 10 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 11 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 12 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 13 |
var PanelBody = wp.components.PanelBody; |
| 14 |
var RangeControl = wp.components.RangeControl; |
| 15 |
var SelectControl = wp.components.SelectControl; |
| 16 |
var ToggleControl = wp.components.ToggleControl; |
| 17 |
var TextControl = wp.components.TextControl; |
| 18 |
var GradientPicker = wp.components.__experimentalGradientPicker; |
| 19 |
var __ = wp.i18n.__; |
| 20 |
|
| 21 |
/* ── Typography lazy getters ── */ |
| 22 |
function _tc() { return window.bkbgTypographyControl || null; } |
| 23 |
function _tv() { return window.bkbgTypoCssVars || function () { return {}; }; } |
| 24 |
|
| 25 |
var PRESETS = { |
| 26 |
purple: 'linear-gradient(135deg,#6c3fb5 0%,#4f24a0 100%)', |
| 27 |
ocean: 'linear-gradient(135deg,#0ea5e9 0%,#2563eb 100%)', |
| 28 |
sunset: 'linear-gradient(135deg,#f97316 0%,#ec4899 100%)', |
| 29 |
dark: 'linear-gradient(135deg,#0f172a 0%,#1e293b 100%)', |
| 30 |
forest: 'linear-gradient(135deg,#059669 0%,#0284c7 100%)', |
| 31 |
rose: 'linear-gradient(135deg,#e11d48 0%,#be123c 100%)', |
| 32 |
}; |
| 33 |
|
| 34 |
function getBg(a) { |
| 35 |
if (a.preset === 'custom') return a.customGradient || PRESETS.purple; |
| 36 |
return PRESETS[a.preset] || PRESETS.purple; |
| 37 |
} |
| 38 |
|
| 39 |
function wrapStyle(a) { |
| 40 |
return Object.assign({ |
| 41 |
'--bkbg-gcta-pt': a.paddingTop + 'px', |
| 42 |
'--bkbg-gcta-pb': a.paddingBottom + 'px', |
| 43 |
'--bkbg-gcta-hl-c': a.headlineColor, |
| 44 |
'--bkbg-gcta-sub-c': a.subtitleColor, |
| 45 |
'--bkbg-gcta-badge-bg':a.badgeBg, |
| 46 |
'--bkbg-gcta-badge-c': a.badgeColor, |
| 47 |
'--bkbg-gcta-primary-bg': a.primaryBg, |
| 48 |
'--bkbg-gcta-primary-c': a.primaryColor, |
| 49 |
'--bkbg-gcta-sec-c': a.secondaryColor, |
| 50 |
'--bkbg-gcta-r': a.btnRadius + 'px', |
| 51 |
'--bkbg-gcta-mw': a.contentMaxWidth + 'px', |
| 52 |
background: getBg(a), |
| 53 |
}, _tv()(a.typoHeadline,'--bkbg-gcta-hl-'), _tv()(a.typoSubtitle,'--bkbg-gcta-sub-'), _tv()(a.typoBadge,'--bkbg-gcta-bg-')); |
| 54 |
} |
| 55 |
|
| 56 |
registerBlockType('blockenberg/gradient-cta', { |
| 57 |
edit: function (props) { |
| 58 |
var a = props.attributes; |
| 59 |
var setAttr = props.setAttributes; |
| 60 |
|
| 61 |
var blockProps = useBlockProps({ |
| 62 |
className: 'bkbg-gcta-wrap bkbg-gcta-align--' + a.textAlign, |
| 63 |
style: wrapStyle(a) |
| 64 |
}); |
| 65 |
|
| 66 |
var inspector = el(InspectorControls, null, |
| 67 |
el(PanelBody, { title:__('Content','blockenberg'), initialOpen:true }, |
| 68 |
el(ToggleControl, { label:__('Show badge','blockenberg'), checked:a.showBadge, onChange:function(v){ setAttr({showBadge:v}); } }), |
| 69 |
el(TextControl, { label:__('Badge text','blockenberg'), value:a.badge, onChange:function(v){ setAttr({badge:v}); } }), |
| 70 |
el(ToggleControl, { label:__('Show subtitle','blockenberg'), checked:a.showSubtitle, onChange:function(v){ setAttr({showSubtitle:v}); } }), |
| 71 |
el(TextControl, { label:__('Primary button label','blockenberg'), value:a.primaryLabel, onChange:function(v){ setAttr({primaryLabel:v}); } }), |
| 72 |
el(TextControl, { label:__('Primary button URL','blockenberg'), value:a.primaryUrl, onChange:function(v){ setAttr({primaryUrl:v}); } }), |
| 73 |
el(SelectControl, { label:__('Primary target','blockenberg'), value:a.primaryTarget, options:[{label:'Same tab',value:'_self'},{label:'New tab',value:'_blank'}], onChange:function(v){ setAttr({primaryTarget:v}); } }), |
| 74 |
el(ToggleControl, { label:__('Show secondary button','blockenberg'), checked:a.showSecondary, onChange:function(v){ setAttr({showSecondary:v}); } }), |
| 75 |
el(TextControl, { label:__('Secondary label','blockenberg'), value:a.secondaryLabel, onChange:function(v){ setAttr({secondaryLabel:v}); } }), |
| 76 |
el(TextControl, { label:__('Secondary URL','blockenberg'), value:a.secondaryUrl, onChange:function(v){ setAttr({secondaryUrl:v}); } }), |
| 77 |
el(SelectControl, { label:__('Secondary target','blockenberg'), value:a.secondaryTarget, options:[{label:'Same tab',value:'_self'},{label:'New tab',value:'_blank'}], onChange:function(v){ setAttr({secondaryTarget:v}); } }) |
| 78 |
), |
| 79 |
el(PanelBody, { title:__('Style','blockenberg'), initialOpen:false }, |
| 80 |
el(SelectControl, { label:__('Gradient preset','blockenberg'), value:a.preset, options:[ |
| 81 |
{label:'Purple', value:'purple'}, |
| 82 |
{label:'Ocean (blue)', value:'ocean'}, |
| 83 |
{label:'Sunset', value:'sunset'}, |
| 84 |
{label:'Dark', value:'dark'}, |
| 85 |
{label:'Forest (green)', value:'forest'}, |
| 86 |
{label:'Rose (red)', value:'rose'}, |
| 87 |
{label:'Custom CSS', value:'custom'}, |
| 88 |
], onChange:function(v){ setAttr({preset:v}); } }), |
| 89 |
a.preset === 'custom' ? el('div', { style:{ marginBottom:'12px' } }, |
| 90 |
el('p', { style:{ fontSize:'11px', fontWeight:600, margin:'0 0 6px', textTransform:'uppercase', color:'#757575' } }, __('Custom gradient','blockenberg')), |
| 91 |
el(GradientPicker, { value: a.customGradient, onChange:function(v){ setAttr({customGradient:v}); } }) |
| 92 |
) : null, |
| 93 |
el(ToggleControl, { label:__('Show pattern overlay','blockenberg'), checked:a.showPattern, onChange:function(v){ setAttr({showPattern:v}); } }), |
| 94 |
el(SelectControl, { label:__('Pattern type','blockenberg'), value:a.patternType, options:[{label:'Dots',value:'dots'},{label:'Grid',value:'grid'},{label:'Noise',value:'noise'}], onChange:function(v){ setAttr({patternType:v}); } }), |
| 95 |
el(SelectControl, { label:__('Text align','blockenberg'), value:a.textAlign, options:[{label:'Center',value:'center'},{label:'Left',value:'left'}], onChange:function(v){ setAttr({textAlign:v}); } }), |
| 96 |
el(RangeControl, { label:__('Content max width (px)','blockenberg'), value:a.contentMaxWidth, min:400, max:960, onChange:function(v){ setAttr({contentMaxWidth:v}); } }) |
| 97 |
), |
| 98 |
el(PanelBody, { title:__('Typography','blockenberg'), initialOpen:false }, |
| 99 |
_tc() && el(_tc(), { label:__('Headline','blockenberg'), value:a.typoHeadline, onChange:function(v){ setAttr({typoHeadline:v}); } }), |
| 100 |
_tc() && el(_tc(), { label:__('Subtitle','blockenberg'), value:a.typoSubtitle, onChange:function(v){ setAttr({typoSubtitle:v}); } }), |
| 101 |
_tc() && el(_tc(), { label:__('Badge','blockenberg'), value:a.typoBadge, onChange:function(v){ setAttr({typoBadge:v}); } }) |
| 102 |
), |
| 103 |
el(PanelBody, { title:__('Spacing','blockenberg'), initialOpen:false }, |
| 104 |
el(RangeControl, { label:__('Padding top (px)','blockenberg'), value:a.paddingTop, min:0, max:200, onChange:function(v){ setAttr({paddingTop:v}); } }), |
| 105 |
el(RangeControl, { label:__('Padding bottom (px)','blockenberg'), value:a.paddingBottom, min:0, max:200, onChange:function(v){ setAttr({paddingBottom:v}); } }), |
| 106 |
el(RangeControl, { label:__('Button radius (px)','blockenberg'), value:a.btnRadius, min:0, max:50, onChange:function(v){ setAttr({btnRadius:v}); } }) |
| 107 |
), |
| 108 |
el(PanelColorSettings, { title:__('Colors','blockenberg'), initialOpen:false, colorSettings:[ |
| 109 |
{label:__('Headline','blockenberg'), value:a.headlineColor, onChange:function(v){ setAttr({headlineColor:v||''}); }}, |
| 110 |
{label:__('Subtitle','blockenberg'), value:a.subtitleColor, onChange:function(v){ setAttr({subtitleColor:v||''}); }}, |
| 111 |
{label:__('Badge bg','blockenberg'), value:a.badgeBg, onChange:function(v){ setAttr({badgeBg:v||''}); }}, |
| 112 |
{label:__('Badge text','blockenberg'), value:a.badgeColor, onChange:function(v){ setAttr({badgeColor:v||''}); }}, |
| 113 |
{label:__('Primary button bg','blockenberg'), value:a.primaryBg, onChange:function(v){ setAttr({primaryBg:v||''}); }}, |
| 114 |
{label:__('Primary button text','blockenberg'), value:a.primaryColor, onChange:function(v){ setAttr({primaryColor:v||''}); }}, |
| 115 |
{label:__('Secondary button text','blockenberg'), value:a.secondaryColor, onChange:function(v){ setAttr({secondaryColor:v||''}); }}, |
| 116 |
] }) |
| 117 |
); |
| 118 |
|
| 119 |
return el(Fragment, null, |
| 120 |
inspector, |
| 121 |
el('div', blockProps, |
| 122 |
a.showPattern ? el('div', { className:'bkbg-gcta-pattern bkbg-gcta-pattern--' + a.patternType }) : null, |
| 123 |
el('div', { className:'bkbg-gcta-inner' }, |
| 124 |
a.showBadge ? el('span', { className:'bkbg-gcta-badge' }, a.badge) : null, |
| 125 |
el(RichText, { tagName:'h2', className:'bkbg-gcta-headline', value:a.headline, onChange:function(v){ setAttr({headline:v}); }, placeholder:__('Your headline…','blockenberg') }), |
| 126 |
a.showSubtitle ? el(RichText, { tagName:'p', className:'bkbg-gcta-subtitle', value:a.subtitle, onChange:function(v){ setAttr({subtitle:v}); }, placeholder:__('Subtitle…','blockenberg') }) : null, |
| 127 |
el('div', { className:'bkbg-gcta-buttons' }, |
| 128 |
el('a', { href:'#', className:'bkbg-gcta-btn bkbg-gcta-btn--primary', onClick:function(e){ e.preventDefault(); } }, a.primaryLabel), |
| 129 |
a.showSecondary ? el('a', { href:'#', className:'bkbg-gcta-btn bkbg-gcta-btn--secondary', onClick:function(e){ e.preventDefault(); } }, a.secondaryLabel) : null |
| 130 |
) |
| 131 |
) |
| 132 |
) |
| 133 |
); |
| 134 |
}, |
| 135 |
|
| 136 |
save: function (props) { |
| 137 |
var a = props.attributes; |
| 138 |
var blockProps = wp.blockEditor.useBlockProps.save({ |
| 139 |
className: 'bkbg-gcta-wrap bkbg-gcta-align--' + a.textAlign, |
| 140 |
style: Object.assign({ |
| 141 |
'--bkbg-gcta-pt':a.paddingTop+'px','--bkbg-gcta-pb':a.paddingBottom+'px','--bkbg-gcta-hl-c':a.headlineColor,'--bkbg-gcta-sub-c':a.subtitleColor,'--bkbg-gcta-badge-bg':a.badgeBg,'--bkbg-gcta-badge-c':a.badgeColor,'--bkbg-gcta-primary-bg':a.primaryBg,'--bkbg-gcta-primary-c':a.primaryColor,'--bkbg-gcta-sec-c':a.secondaryColor,'--bkbg-gcta-r':a.btnRadius+'px','--bkbg-gcta-mw':a.contentMaxWidth+'px', |
| 142 |
background: getBg(a), |
| 143 |
}, _tv()(a.typoHeadline,'--bkbg-gcta-hl-'), _tv()(a.typoSubtitle,'--bkbg-gcta-sub-'), _tv()(a.typoBadge,'--bkbg-gcta-bg-')) |
| 144 |
}); |
| 145 |
return el('div', blockProps, |
| 146 |
a.showPattern ? el('div', { className:'bkbg-gcta-pattern bkbg-gcta-pattern--' + a.patternType }) : null, |
| 147 |
el('div', { className:'bkbg-gcta-inner' }, |
| 148 |
a.showBadge ? el('span', { className:'bkbg-gcta-badge' }, a.badge) : null, |
| 149 |
el(RichText.Content, { tagName:'h2', className:'bkbg-gcta-headline', value:a.headline }), |
| 150 |
a.showSubtitle ? el(RichText.Content, { tagName:'p', className:'bkbg-gcta-subtitle', value:a.subtitle }) : null, |
| 151 |
el('div', { className:'bkbg-gcta-buttons' }, |
| 152 |
el('a', { href:a.primaryUrl, className:'bkbg-gcta-btn bkbg-gcta-btn--primary', target:a.primaryTarget, rel:a.primaryTarget==='_blank'?'noopener noreferrer':undefined }, a.primaryLabel), |
| 153 |
a.showSecondary ? el('a', { href:a.secondaryUrl, className:'bkbg-gcta-btn bkbg-gcta-btn--secondary', target:a.secondaryTarget, rel:a.secondaryTarget==='_blank'?'noopener noreferrer':undefined }, a.secondaryLabel) : null |
| 154 |
) |
| 155 |
) |
| 156 |
); |
| 157 |
} |
| 158 |
}); |
| 159 |
}() ); |
| 160 |
|