| 1 |
( function () { |
| 2 |
const el = window.wp.element.createElement; |
| 3 |
const { registerBlockType } = window.wp.blocks; |
| 4 |
const { InspectorControls, useBlockProps, PanelColorSettings } = window.wp.blockEditor; |
| 5 |
const { PanelBody, RangeControl, SelectControl, ToggleControl, TextControl, Button } = window.wp.components; |
| 6 |
const { __ } = window.wp.i18n; |
| 7 |
function getTypographyControl() { |
| 8 |
return (window.bkbgTypographyControl || function () { return null; }); |
| 9 |
} |
| 10 |
var _tv = (function () { |
| 11 |
var fn = window.bkbgTypoCssVars; |
| 12 |
return fn ? fn : function () { return {}; }; |
| 13 |
})(); |
| 14 |
|
| 15 |
// ── Helpers ─────────────────────────────────────────────────────────────── |
| 16 |
function hexToRgb( hex ) { |
| 17 |
const r = parseInt( hex.slice( 1, 3 ), 16 ); |
| 18 |
const g = parseInt( hex.slice( 3, 5 ), 16 ); |
| 19 |
const b = parseInt( hex.slice( 5, 7 ), 16 ); |
| 20 |
if ( isNaN( r ) ) return ''; |
| 21 |
return `rgb(${ r }, ${ g }, ${ b })`; |
| 22 |
} |
| 23 |
|
| 24 |
function isLight( hex ) { |
| 25 |
const r = parseInt( hex.slice( 1, 3 ), 16 ); |
| 26 |
const g = parseInt( hex.slice( 3, 5 ), 16 ); |
| 27 |
const b = parseInt( hex.slice( 5, 7 ), 16 ); |
| 28 |
return ( r * 299 + g * 587 + b * 114 ) / 1000 > 155; |
| 29 |
} |
| 30 |
|
| 31 |
// ── Swatch card ─────────────────────────────────────────────────────────── |
| 32 |
function SwatchCard( props ) { |
| 33 |
const { swatch, a } = props; |
| 34 |
const hex = swatch.hex || '#6366f1'; |
| 35 |
const onSwatch = isLight( hex ) ? '#000000' : '#ffffff'; |
| 36 |
|
| 37 |
return el( 'div', { |
| 38 |
className: 'bkbg-cs-card', |
| 39 |
style: { |
| 40 |
background: a.cardBg, |
| 41 |
border: `1px solid ${ a.borderColor }`, |
| 42 |
borderRadius: a.borderRadius + 'px', |
| 43 |
overflow: 'hidden', |
| 44 |
boxSizing: 'border-box', |
| 45 |
} |
| 46 |
}, |
| 47 |
// Color tile |
| 48 |
el( 'div', { className: 'bkbg-cs-tile', style: { height: a.swatchHeight + 'px', background: hex, display: 'flex', alignItems: 'flex-end', justifyContent: 'flex-start', padding: '8px 10px' } }, |
| 49 |
a.showTag && swatch.tag && el( 'span', { style: { fontSize: 10, fontWeight: 700, textTransform: 'uppercase', letterSpacing: '0.06em', color: onSwatch, background: 'rgba(0,0,0,0.18)', borderRadius: 99 + 'px', padding: '2px 7px' } }, swatch.tag ), |
| 50 |
), |
| 51 |
|
| 52 |
// Info section |
| 53 |
el( 'div', { className: 'bkbg-cs-info', style: { padding: '12px 14px', display: 'flex', flexDirection: 'column', gap: 4 } }, |
| 54 |
a.showName && swatch.name && el( 'div', { className: 'bkbg-cs-name', style: { color: a.nameColor } }, swatch.name ), |
| 55 |
a.showHex && el( 'div', { className: 'bkbg-cs-hex', style: { color: a.metaColor } }, hex.toUpperCase() + ' · ' + hexToRgb( hex ) ), |
| 56 |
a.showUsage && swatch.usage && el( 'div', { className: 'bkbg-cs-usage', style: { color: a.metaColor, marginTop: 2 } }, swatch.usage ), |
| 57 |
), |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
function renderSwatches( a ) { |
| 62 |
return el( 'div', { className: 'bkbg-cs-grid', style: { display: 'grid', gridTemplateColumns: `repeat(${ a.columns }, 1fr)`, gap: 16 } }, |
| 63 |
( a.swatches || [] ).map( ( sw, i ) => el( SwatchCard, { key: i, swatch: sw, a } ) ) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
function updSwatch( setAttributes, swatches, si, field, val ) { |
| 68 |
setAttributes( { swatches: swatches.map( ( s, i ) => i === si ? { ...s, [field]: val } : s ) } ); |
| 69 |
} |
| 70 |
|
| 71 |
// ── Block ───────────────────────────────────────────────────────────────── |
| 72 |
registerBlockType( 'blockenberg/color-swatch', { |
| 73 |
edit: function ( props ) { |
| 74 |
const { attributes: a, setAttributes } = props; |
| 75 |
var bpStyle = { background: a.bgColor, boxSizing: 'border-box' }; |
| 76 |
Object.assign(bpStyle, _tv(a.typoTitle, '--bkbg-cs-tt')); |
| 77 |
Object.assign(bpStyle, _tv(a.typoLabel, '--bkbg-cs-lb')); |
| 78 |
const blockProps = useBlockProps( { className: 'bkbg-cs-wrap', style: bpStyle } ); |
| 79 |
|
| 80 |
return el( 'div', blockProps, |
| 81 |
el( InspectorControls, {}, |
| 82 |
|
| 83 |
el( PanelBody, { title: __( 'Settings', 'blockenberg' ), initialOpen: true }, |
| 84 |
el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ), |
| 85 |
el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ), |
| 86 |
el( TextControl, { label: __( 'Subtitle', 'blockenberg' ), value: a.subtitle, onChange: v => setAttributes( { subtitle: v } ) } ), |
| 87 |
el( ToggleControl, { label: __( 'Show Subtitle', 'blockenberg' ), checked: a.showSubtitle, onChange: v => setAttributes( { showSubtitle: v } ) } ), |
| 88 |
el( RangeControl, { label: __( 'Columns', 'blockenberg' ), value: a.columns, onChange: v => setAttributes( { columns: v } ), min: 1, max: 8 } ), |
| 89 |
el( ToggleControl, { label: __( 'Show Name', 'blockenberg' ), checked: a.showName, onChange: v => setAttributes( { showName: v } ) } ), |
| 90 |
el( ToggleControl, { label: __( 'Show HEX/RGB', 'blockenberg' ), checked: a.showHex, onChange: v => setAttributes( { showHex: v } ) } ), |
| 91 |
el( ToggleControl, { label: __( 'Show Usage', 'blockenberg' ), checked: a.showUsage, onChange: v => setAttributes( { showUsage: v } ) } ), |
| 92 |
el( ToggleControl, { label: __( 'Show Tag', 'blockenberg' ), checked: a.showTag, onChange: v => setAttributes( { showTag: v } ) } ), |
| 93 |
), |
| 94 |
|
| 95 |
el( PanelBody, { title: __( 'Style', 'blockenberg' ), initialOpen: false }, |
| 96 |
el( RangeControl, { label: __( 'Swatch Height', 'blockenberg' ), value: a.swatchHeight, onChange: v => setAttributes( { swatchHeight: v } ), min: 40, max: 200 } ), |
| 97 |
el( RangeControl, { label: __( 'Border Radius', 'blockenberg' ), value: a.borderRadius, onChange: v => setAttributes( { borderRadius: v } ), min: 0, max: 28 } ), |
| 98 |
), |
| 99 |
|
| 100 |
|
| 101 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 102 |
(function () { |
| 103 |
var TC = getTypographyControl(); |
| 104 |
return el( window.wp.element.Fragment, {}, |
| 105 |
el( TC, { label: __( 'Title', 'blockenberg' ), value: a.typoTitle || {}, onChange: function (v) { setAttributes({ typoTitle: v }); } }), |
| 106 |
el( TC, { label: __( 'Label', 'blockenberg' ), value: a.typoLabel || {}, onChange: function (v) { setAttributes({ typoLabel: v }); } }) |
| 107 |
); |
| 108 |
})() |
| 109 |
), |
| 110 |
el( PanelColorSettings, { |
| 111 |
title: __( 'Colors', 'blockenberg' ), initialOpen: false, |
| 112 |
colorSettings: [ |
| 113 |
{ label: __( 'Wrapper BG', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) }, |
| 114 |
{ label: __( 'Card BG', 'blockenberg' ), value: a.cardBg, onChange: v => setAttributes( { cardBg: v || '#f9fafb' } ) }, |
| 115 |
{ label: __( 'Border', 'blockenberg' ), value: a.borderColor, onChange: v => setAttributes( { borderColor: v || '#e5e7eb' } ) }, |
| 116 |
{ label: __( 'Heading', 'blockenberg' ), value: a.headingColor, onChange: v => setAttributes( { headingColor: v || '#111827' } ) }, |
| 117 |
{ label: __( 'Subtitle', 'blockenberg' ), value: a.subtitleColor,onChange: v => setAttributes( { subtitleColor:v || '#6b7280' } ) }, |
| 118 |
{ label: __( 'Name', 'blockenberg' ), value: a.nameColor, onChange: v => setAttributes( { nameColor: v || '#111827' } ) }, |
| 119 |
{ label: __( 'Meta/HEX', 'blockenberg' ), value: a.metaColor, onChange: v => setAttributes( { metaColor: v || '#6b7280' } ) }, |
| 120 |
] |
| 121 |
} ), |
| 122 |
|
| 123 |
el( PanelBody, { title: __( 'Swatches', 'blockenberg' ), initialOpen: false }, |
| 124 |
el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { swatches: [ ...( a.swatches || [] ), { name: 'New Color', hex: '#6366f1', tag: '', usage: '' } ] } ) }, __( '+ Add Swatch', 'blockenberg' ) ), |
| 125 |
a.swatches.map( ( sw, si ) => |
| 126 |
el( PanelBody, { key: si, title: `${ sw.name } — ${ sw.hex }`, initialOpen: false }, |
| 127 |
el( TextControl, { label: __( 'Name', 'blockenberg' ), value: sw.name, onChange: v => updSwatch( setAttributes, a.swatches, si, 'name', v ) } ), |
| 128 |
el( 'div', { style: { display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 } }, |
| 129 |
el( 'label', { style: { fontSize: 12 } }, __( 'Color', 'blockenberg' ) ), |
| 130 |
el( 'input', { type: 'color', value: sw.hex, style: { width: 40, height: 28, border: 'none', borderRadius: 4, cursor: 'pointer' }, onChange: e => updSwatch( setAttributes, a.swatches, si, 'hex', e.target.value ) } ), |
| 131 |
el( TextControl, { value: sw.hex, onChange: v => updSwatch( setAttributes, a.swatches, si, 'hex', v ), style: { flex: 1, fontFamily: 'monospace' } } ), |
| 132 |
), |
| 133 |
el( TextControl, { label: __( 'Tag', 'blockenberg' ), value: sw.tag, onChange: v => updSwatch( setAttributes, a.swatches, si, 'tag', v ) } ), |
| 134 |
el( TextControl, { label: __( 'Usage', 'blockenberg' ), value: sw.usage, onChange: v => updSwatch( setAttributes, a.swatches, si, 'usage', v ) } ), |
| 135 |
el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { swatches: a.swatches.filter( ( _, x ) => x !== si ) } ) }, __( 'Remove', 'blockenberg' ) ), |
| 136 |
) |
| 137 |
), |
| 138 |
), |
| 139 |
), |
| 140 |
|
| 141 |
el( 'div', { style: { marginBottom: 24 } }, |
| 142 |
a.showTitle && a.title && el( 'h2', { className: 'bkbg-cs-title', style: { color: a.headingColor, margin: '0 0 6px' } }, a.title ), |
| 143 |
a.showSubtitle && a.subtitle && el( 'p', { className: 'bkbg-cs-subtitle', style: { color: a.subtitleColor, margin: 0 } }, a.subtitle ), |
| 144 |
), |
| 145 |
|
| 146 |
renderSwatches( a ), |
| 147 |
); |
| 148 |
}, |
| 149 |
|
| 150 |
save: function ( { attributes: a } ) { |
| 151 |
var bpStyle = { background: a.bgColor, boxSizing: 'border-box' }; |
| 152 |
Object.assign(bpStyle, _tv(a.typoTitle, '--bkbg-cs-tt')); |
| 153 |
Object.assign(bpStyle, _tv(a.typoLabel, '--bkbg-cs-lb')); |
| 154 |
const blockProps = useBlockProps.save( { className: 'bkbg-cs-wrap', style: bpStyle } ); |
| 155 |
return el( 'div', blockProps, |
| 156 |
el( 'div', { style: { marginBottom: 24 } }, |
| 157 |
a.showTitle && a.title ? el( 'h2', { className: 'bkbg-cs-title', style: { color: a.headingColor, margin: '0 0 6px' } }, a.title ) : null, |
| 158 |
a.showSubtitle && a.subtitle ? el( 'p', { className: 'bkbg-cs-subtitle', style: { color: a.subtitleColor, margin: 0 } }, a.subtitle ) : null, |
| 159 |
), |
| 160 |
renderSwatches( a ), |
| 161 |
); |
| 162 |
}, |
| 163 |
} ); |
| 164 |
}() ); |
| 165 |
|