| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var Fragment = wp.element.Fragment; |
| 4 |
var __ = wp.i18n.__; |
| 5 |
var registerBlockType = wp.blocks.registerBlockType; |
| 6 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 7 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 8 |
var RichText = wp.blockEditor.RichText; |
| 9 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 10 |
var PanelBody = wp.components.PanelBody; |
| 11 |
var ToggleControl = wp.components.ToggleControl; |
| 12 |
var RangeControl = wp.components.RangeControl; |
| 13 |
var SelectControl = wp.components.SelectControl; |
| 14 |
var TextControl = wp.components.TextControl; |
| 15 |
|
| 16 |
var _glTC, _glTV; |
| 17 |
function _tc() { return _glTC || (_glTC = window.bkbgTypographyControl); } |
| 18 |
function _tv() { return _glTV || (_glTV = window.bkbgTypoCssVars); } |
| 19 |
|
| 20 |
var GLITCH_TYPES = [ |
| 21 |
{ label: 'Chromatic (RGB split)', value: 'chromatic' }, |
| 22 |
{ label: 'Noise (character shuffle)', value: 'noise' }, |
| 23 |
{ label: 'Digital (chromatic + noise)', value: 'digital' }, |
| 24 |
{ label: 'Flicker', value: 'flicker' }, |
| 25 |
{ label: 'Horizontal shake', value: 'shake' }, |
| 26 |
]; |
| 27 |
|
| 28 |
var TRIGGERS = [ |
| 29 |
{ label: 'Always on', value: 'always' }, |
| 30 |
{ label: 'On hover', value: 'hover' }, |
| 31 |
{ label: 'On click', value: 'click' }, |
| 32 |
{ label: 'Interval', value: 'interval' }, |
| 33 |
]; |
| 34 |
|
| 35 |
var SPEEDS = [ |
| 36 |
{ label: 'Slow', value: 'slow' }, |
| 37 |
{ label: 'Medium', value: 'medium' }, |
| 38 |
{ label: 'Fast', value: 'fast' }, |
| 39 |
{ label: 'Custom', value: 'custom' }, |
| 40 |
]; |
| 41 |
|
| 42 |
var TRANSFORMS = [ |
| 43 |
{ label: 'Uppercase', value: 'uppercase' }, |
| 44 |
{ label: 'Lowercase', value: 'lowercase' }, |
| 45 |
{ label: 'Capitalize', value: 'capitalize' }, |
| 46 |
{ label: 'None', value: 'none' }, |
| 47 |
]; |
| 48 |
|
| 49 |
var TAGS = [ |
| 50 |
{ label: 'H1', value: 'h1' }, { label: 'H2', value: 'h2' }, { label: 'H3', value: 'h3' }, |
| 51 |
{ label: 'H4', value: 'h4' }, { label: 'H5', value: 'h5' }, { label: 'H6', value: 'h6' }, |
| 52 |
{ label: 'p', value: 'p' }, { label: 'div', value: 'div' }, |
| 53 |
]; |
| 54 |
|
| 55 |
function speedMs(a) { |
| 56 |
if (a.speed === 'custom') { return a.customDuration; } |
| 57 |
if (a.speed === 'slow') { return 1400; } |
| 58 |
if (a.speed === 'fast') { return 300; } |
| 59 |
return 800; |
| 60 |
} |
| 61 |
|
| 62 |
/* ── wrapper style ───────────────────────────────────────────────────────── */ |
| 63 |
function buildWrapStyle(a) { |
| 64 |
return { |
| 65 |
background: a.backgroundColor, |
| 66 |
padding: a.paddingV + 'px ' + a.paddingH + 'px', |
| 67 |
textAlign: a.textAlign, |
| 68 |
borderRadius: a.borderRadius + 'px', |
| 69 |
position: 'relative', |
| 70 |
overflow: 'hidden', |
| 71 |
}; |
| 72 |
} |
| 73 |
|
| 74 |
/* ── text style ─────────────────────────────────────────────────────────── */ |
| 75 |
function buildTextStyle(a) { |
| 76 |
return { |
| 77 |
color: a.textColor, |
| 78 |
margin: 0, |
| 79 |
display: 'block', |
| 80 |
}; |
| 81 |
} |
| 82 |
|
| 83 |
/* ── edit ───────────────────────────────────────────────────────────────── */ |
| 84 |
function Edit(props) { |
| 85 |
var attributes = props.attributes; |
| 86 |
var setAttributes = props.setAttributes; |
| 87 |
var a = attributes; |
| 88 |
|
| 89 |
var blockProps = useBlockProps({ style: Object.assign(buildWrapStyle(a), _tv()(a.typoText, '--bkgl-tt-'), _tv()(a.typoSubtext, '--bkgl-st-')) }); |
| 90 |
var ms = speedMs(a); |
| 91 |
|
| 92 |
return el(Fragment, null, |
| 93 |
el(InspectorControls, null, |
| 94 |
|
| 95 |
/* Effect */ |
| 96 |
el(PanelBody, { title: __('Glitch Effect', 'blockenberg'), initialOpen: true }, |
| 97 |
el(SelectControl, { label: __('Effect Type', 'blockenberg'), value: a.glitchType, options: GLITCH_TYPES, onChange: function (v) { setAttributes({ glitchType: v }); } }), |
| 98 |
el(SelectControl, { label: __('Trigger', 'blockenberg'), value: a.trigger, options: TRIGGERS, onChange: function (v) { setAttributes({ trigger: v }); } }), |
| 99 |
a.trigger === 'interval' && el(Fragment, null, |
| 100 |
el(RangeControl, { label: __('Interval (ms)', 'blockenberg'), value: a.glitchIntervalMs, min: 500, max: 20000, step: 500, onChange: function (v) { setAttributes({ glitchIntervalMs: v }); } }), |
| 101 |
el(RangeControl, { label: __('Burst Duration (ms)', 'blockenberg'), value: a.glitchBurstDuration, min: 100, max: 2000, step: 100, onChange: function (v) { setAttributes({ glitchBurstDuration: v }); } }) |
| 102 |
), |
| 103 |
el(SelectControl, { label: __('Speed', 'blockenberg'), value: a.speed, options: SPEEDS, onChange: function (v) { setAttributes({ speed: v }); } }), |
| 104 |
a.speed === 'custom' && el(RangeControl, { label: __('Duration (ms)', 'blockenberg'), value: a.customDuration, min: 100, max: 5000, step: 50, onChange: function (v) { setAttributes({ customDuration: v }); } }), |
| 105 |
el(RangeControl, { label: __('Intensity', 'blockenberg'), value: a.intensity, min: 1, max: 20, onChange: function (v) { setAttributes({ intensity: v }); } }), |
| 106 |
(a.glitchType === 'noise' || a.glitchType === 'digital') && el(TextControl, { |
| 107 |
label: __('Noise Charset', 'blockenberg'), value: a.noiseCharset, |
| 108 |
onChange: function (v) { setAttributes({ noiseCharset: v }); } |
| 109 |
}), |
| 110 |
el(ToggleControl, { label: __('Show Scanlines overlay', 'blockenberg'), checked: a.showScanlines, onChange: function (v) { setAttributes({ showScanlines: v }); }, __nextHasNoMarginBottom: true }), |
| 111 |
a.showScanlines && el(RangeControl, { label: __('Scanline Opacity (%)', 'blockenberg'), value: a.scanlineOpacity, min: 5, max: 80, onChange: function (v) { setAttributes({ scanlineOpacity: v }); } }), |
| 112 |
el(ToggleControl, { label: __('Cursor Blink', 'blockenberg'), checked: a.showCursorBlink, onChange: function (v) { setAttributes({ showCursorBlink: v }); }, __nextHasNoMarginBottom: true }) |
| 113 |
), |
| 114 |
|
| 115 |
/* Text */ |
| 116 |
el(PanelBody, { title: __('Text', 'blockenberg'), initialOpen: false }, |
| 117 |
el(SelectControl, { label: __('HTML Tag', 'blockenberg'), value: a.tag, options: TAGS, onChange: function (v) { setAttributes({ tag: v }); } }), |
| 118 |
el(SelectControl, { |
| 119 |
label: __('Text Align', 'blockenberg'), value: a.textAlign, |
| 120 |
options: [ |
| 121 |
{ label: __('Left', 'blockenberg'), value: 'left' }, |
| 122 |
{ label: __('Center', 'blockenberg'), value: 'center' }, |
| 123 |
{ label: __('Right', 'blockenberg'), value: 'right' }, |
| 124 |
], |
| 125 |
onChange: function (v) { setAttributes({ textAlign: v }); } |
| 126 |
}) |
| 127 |
), |
| 128 |
|
| 129 |
/* Subtext */ |
| 130 |
el(PanelBody, { title: __('Subtext', 'blockenberg'), initialOpen: false }, |
| 131 |
el(ToggleControl, { label: __('Show Subtext', 'blockenberg'), checked: a.showSubtext, onChange: function (v) { setAttributes({ showSubtext: v }); }, __nextHasNoMarginBottom: true }), |
| 132 |
a.showSubtext && el(Fragment, null, |
| 133 |
el(TextControl, { label: __('Subtext', 'blockenberg'), value: a.subtext, onChange: function (v) { setAttributes({ subtext: v }); } }), |
| 134 |
el(RangeControl, { label: __('Gap from text (px)', 'blockenberg'), value: a.subtextSpacing, min: 0, max: 60, onChange: function (v) { setAttributes({ subtextSpacing: v }); } }) |
| 135 |
) |
| 136 |
), |
| 137 |
|
| 138 |
/* Layout */ |
| 139 |
el(PanelBody, { title: __('Layout & Spacing', 'blockenberg'), initialOpen: false }, |
| 140 |
el(RangeControl, { label: __('Vertical Padding (px)', 'blockenberg'), value: a.paddingV, min: 0, max: 200, onChange: function (v) { setAttributes({ paddingV: v }); } }), |
| 141 |
el(RangeControl, { label: __('Horizontal Padding (px)', 'blockenberg'), value: a.paddingH, min: 0, max: 200, onChange: function (v) { setAttributes({ paddingH: v }); } }), |
| 142 |
el(RangeControl, { label: __('Border Radius (px)', 'blockenberg'), value: a.borderRadius, min: 0, max: 32, onChange: function (v) { setAttributes({ borderRadius: v }); } }) |
| 143 |
), |
| 144 |
|
| 145 |
/* Colors */ |
| 146 |
|
| 147 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 148 |
_tc()({ label: __('Text', 'blockenberg'), value: a.typoText, onChange: function (v) { setAttributes({ typoText: v }); } }), |
| 149 |
_tc()({ label: __('Subtext', 'blockenberg'), value: a.typoSubtext, onChange: function (v) { setAttributes({ typoSubtext: v }); } }) |
| 150 |
), |
| 151 |
el(PanelColorSettings, { |
| 152 |
title: __('Colors', 'blockenberg'), |
| 153 |
initialOpen: false, |
| 154 |
colorSettings: [ |
| 155 |
{ label: __('Text Color', 'blockenberg'), value: a.textColor, onChange: function (v) { setAttributes({ textColor: v || '#ffffff' }); } }, |
| 156 |
{ label: __('Background', 'blockenberg'), value: a.backgroundColor, onChange: function (v) { setAttributes({ backgroundColor: v || '#0d0d0d' }); } }, |
| 157 |
{ label: __('Channel Red / Left', 'blockenberg'), value: a.colorRed, onChange: function (v) { setAttributes({ colorRed: v || '#ff0040' }); } }, |
| 158 |
{ label: __('Channel Blue / Right', 'blockenberg'), value: a.colorBlue, onChange: function (v) { setAttributes({ colorBlue: v || '#00e5ff' }); } }, |
| 159 |
{ label: __('Subtext', 'blockenberg'), value: a.subtextColor, onChange: function (v) { setAttributes({ subtextColor: v || '#888888' }); } }, |
| 160 |
] |
| 161 |
}) |
| 162 |
), |
| 163 |
|
| 164 |
/* ── editor preview ── */ |
| 165 |
el('div', blockProps, |
| 166 |
/* scanlines overlay */ |
| 167 |
a.showScanlines && el('div', { |
| 168 |
style: { |
| 169 |
position: 'absolute', inset: 0, pointerEvents: 'none', zIndex: 2, |
| 170 |
backgroundImage: 'repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(0,0,0,' + (a.scanlineOpacity / 100) + ') 2px, rgba(0,0,0,' + (a.scanlineOpacity / 100) + ') 4px)', |
| 171 |
} |
| 172 |
}), |
| 173 |
|
| 174 |
/* glitch text */ |
| 175 |
el('div', { |
| 176 |
className: 'bkgl-outer', |
| 177 |
style: { position: 'relative', display: 'inline-block' }, |
| 178 |
'data-glitch': a.text, |
| 179 |
'data-type': a.glitchType, |
| 180 |
'data-trigger': a.trigger, |
| 181 |
'data-ms': ms, |
| 182 |
'data-intensity': a.intensity, |
| 183 |
'data-interval': a.glitchIntervalMs, |
| 184 |
'data-burst': a.glitchBurstDuration, |
| 185 |
'data-charset': a.noiseCharset, |
| 186 |
'data-color-red': a.colorRed, |
| 187 |
'data-color-blue': a.colorBlue, |
| 188 |
style: { position: 'relative', display: 'inline-block', '--bkgl-red': a.colorRed, '--bkgl-blue': a.colorBlue, '--bkgl-ms': ms + 'ms', '--bkgl-shift': a.intensity + 'px' }, |
| 189 |
}, |
| 190 |
el(a.tag, { |
| 191 |
className: 'bkgl-text', |
| 192 |
style: buildTextStyle(a), |
| 193 |
}, a.text), |
| 194 |
/* editor hint */ |
| 195 |
el('span', { |
| 196 |
style: { position: 'absolute', top: '-18px', left: 0, fontSize: '10px', color: '#6c3fb5', fontWeight: 600, whiteSpace: 'nowrap', pointerEvents: 'none' } |
| 197 |
}, '⚡ ' + GLITCH_TYPES.find(function (g) { return g.value === a.glitchType; }).label.split('(')[0].trim()) |
| 198 |
), |
| 199 |
a.showCursorBlink && el('span', { className: 'bkgl-cursor', style: { color: a.textColor } }, '|'), |
| 200 |
|
| 201 |
a.showSubtext && el('p', { |
| 202 |
className: 'bkgl-subtext', |
| 203 |
style: { margin: a.subtextSpacing + 'px 0 0', color: a.subtextColor } |
| 204 |
}, a.subtext) |
| 205 |
) |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
/* ── save ───────────────────────────────────────────────────────────────── */ |
| 210 |
function Save(props) { |
| 211 |
var a = props.attributes; |
| 212 |
var ms = speedMs(a); |
| 213 |
|
| 214 |
var blockProps = wp.blockEditor.useBlockProps.save({ |
| 215 |
className: 'bkgl-wrap', |
| 216 |
style: Object.assign(buildWrapStyle(a), _tv()(a.typoText, '--bkgl-tt-'), _tv()(a.typoSubtext, '--bkgl-st-')), |
| 217 |
}); |
| 218 |
|
| 219 |
return el('div', blockProps, |
| 220 |
a.showScanlines && el('div', { |
| 221 |
className: 'bkgl-scanlines', |
| 222 |
style: { |
| 223 |
position: 'absolute', inset: 0, pointerEvents: 'none', zIndex: 2, |
| 224 |
backgroundImage: 'repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(0,0,0,' + (a.scanlineOpacity / 100) + ') 2px, rgba(0,0,0,' + (a.scanlineOpacity / 100) + ') 4px)', |
| 225 |
} |
| 226 |
}), |
| 227 |
el('div', { |
| 228 |
className: 'bkgl-outer', |
| 229 |
'data-glitch': a.text, |
| 230 |
'data-type': a.glitchType, |
| 231 |
'data-trigger': a.trigger, |
| 232 |
'data-ms': ms, |
| 233 |
'data-intensity': a.intensity, |
| 234 |
'data-interval': a.glitchIntervalMs, |
| 235 |
'data-burst': a.glitchBurstDuration, |
| 236 |
'data-charset': a.noiseCharset, |
| 237 |
style: { |
| 238 |
position: 'relative', |
| 239 |
display: 'inline-block', |
| 240 |
'--bkgl-red': a.colorRed, |
| 241 |
'--bkgl-blue': a.colorBlue, |
| 242 |
'--bkgl-ms': ms + 'ms', |
| 243 |
'--bkgl-shift': a.intensity + 'px', |
| 244 |
} |
| 245 |
}, |
| 246 |
el(RichText.Content, { |
| 247 |
tagName: a.tag, |
| 248 |
className: 'bkgl-text', |
| 249 |
value: a.text, |
| 250 |
style: buildTextStyle(a), |
| 251 |
}) |
| 252 |
), |
| 253 |
a.showCursorBlink && el('span', { |
| 254 |
className: 'bkgl-cursor', |
| 255 |
style: { color: a.textColor } |
| 256 |
}, '|'), |
| 257 |
a.showSubtext && el('p', { |
| 258 |
className: 'bkgl-subtext', |
| 259 |
style: { margin: a.subtextSpacing + 'px 0 0', color: a.subtextColor } |
| 260 |
}, a.subtext) |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
registerBlockType('blockenberg/glitch-text', { |
| 265 |
edit: Edit, |
| 266 |
save: Save, |
| 267 |
}); |
| 268 |
}() ); |
| 269 |
|