| 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, ToggleControl, TextControl, Button } = window.wp.components; |
| 6 |
const { __ } = window.wp.i18n; |
| 7 |
|
| 8 |
var _dbTC, _dbTV; |
| 9 |
function _tc() { return _dbTC || (_dbTC = window.bkbgTypographyControl); } |
| 10 |
function _tv(t, p) { return (_dbTV || (_dbTV = window.bkbgTypoCssVars)) ? _dbTV(t, p) : {}; } |
| 11 |
|
| 12 |
// ── Renderer ────────────────────────────────────────────────────────────── |
| 13 |
function renderDumbbell( a ) { |
| 14 |
const items = a.items || []; |
| 15 |
const W = a.svgWidth; |
| 16 |
const RH = a.rowHeight; |
| 17 |
const PT = a.padTop; |
| 18 |
const PL = a.padLeft; |
| 19 |
const PR = a.padRight; |
| 20 |
const DR = a.dotRadius; |
| 21 |
|
| 22 |
const allVals = items.flatMap( it => [ it.a || 0, it.b || 0 ] ); |
| 23 |
const minVal = Math.min( 0, ...allVals ); |
| 24 |
const maxVal = Math.max( ...allVals, 1 ); |
| 25 |
const chartW = W - PL - PR; |
| 26 |
const legH = a.showLegend ? 36 : 0; |
| 27 |
const H = PT + items.length * RH + legH + 20; |
| 28 |
|
| 29 |
function xScale( v ) { |
| 30 |
return PL + ( ( v - minVal ) / ( maxVal - minVal ) ) * chartW; |
| 31 |
} |
| 32 |
|
| 33 |
const svgEls = []; |
| 34 |
svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) ); |
| 35 |
|
| 36 |
// Grid lines & tick labels |
| 37 |
if ( a.showGridLines ) { |
| 38 |
const ticks = 5; |
| 39 |
for ( let i = 0; i <= ticks; i++ ) { |
| 40 |
const v = minVal + ( maxVal - minVal ) * i / ticks; |
| 41 |
const gx = xScale( v ); |
| 42 |
svgEls.push( el( 'line', { key: 'gl' + i, x1: gx, y1: PT - 8, x2: gx, y2: H - legH - 10, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) ); |
| 43 |
svgEls.push( el( 'text', { key: 'gt' + i, x: gx, y: PT - 14, textAnchor: 'middle', fill: '#9ca3af', fontSize: 10, fontFamily: 'inherit' }, String( parseFloat( v.toFixed( 1 ) ) ) ) ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// Rows |
| 48 |
items.forEach( ( item, i ) => { |
| 49 |
const cy = PT + i * RH + RH / 2; |
| 50 |
const xA = xScale( item.a || 0 ); |
| 51 |
const xB = xScale( item.b || 0 ); |
| 52 |
const x0 = Math.min( xA, xB ); |
| 53 |
const x1 = Math.max( xA, xB ); |
| 54 |
|
| 55 |
// Connector line |
| 56 |
svgEls.push( el( 'line', { key: 'con' + i, x1: x0, y1: cy, x2: x1, y2: cy, stroke: a.connectorColor || '#e5e7eb', strokeWidth: 4, strokeLinecap: 'round' } ) ); |
| 57 |
|
| 58 |
// Dot A (before) |
| 59 |
svgEls.push( el( 'circle', { key: 'dA' + i, cx: xA, cy, r: DR, fill: a.colorA || '#9ca3af' } ) ); |
| 60 |
// Dot B (after) |
| 61 |
svgEls.push( el( 'circle', { key: 'dB' + i, cx: xB, cy, r: DR, fill: a.colorB || '#4f46e5' } ) ); |
| 62 |
|
| 63 |
// Value labels on dots |
| 64 |
if ( a.showValues ) { |
| 65 |
const fmtA = String( parseFloat( ( item.a || 0 ).toFixed( 1 ) ) ); |
| 66 |
const fmtB = String( parseFloat( ( item.b || 0 ).toFixed( 1 ) ) ); |
| 67 |
svgEls.push( el( 'text', { key: 'vA' + i, x: xA, y: cy, textAnchor: 'middle', dominantBaseline: 'middle', fill: '#ffffff', fontSize: a.valueFontSize, fontWeight: a.valueFontWeight || '700', fontFamily: 'inherit' }, fmtA ) ); |
| 68 |
svgEls.push( el( 'text', { key: 'vB' + i, x: xB, y: cy, textAnchor: 'middle', dominantBaseline: 'middle', fill: '#ffffff', fontSize: a.valueFontSize, fontWeight: a.valueFontWeight || '700', fontFamily: 'inherit' }, fmtB ) ); |
| 69 |
} |
| 70 |
|
| 71 |
// Row label |
| 72 |
svgEls.push( el( 'text', { key: 'lbl' + i, x: PL - 10, y: cy, textAnchor: 'end', dominantBaseline: 'middle', fill: a.labelColor || '#374151', fontSize: a.labelFontSize, fontWeight: a.labelFontWeight || '400', fontFamily: 'inherit' }, item.label || '' ) ); |
| 73 |
} ); |
| 74 |
|
| 75 |
// Legend |
| 76 |
if ( a.showLegend ) { |
| 77 |
const ly = H - legH + 8; |
| 78 |
const lcx = W / 2; |
| 79 |
svgEls.push( el( 'circle', { key: 'lgA', cx: lcx - 60, cy: ly + 8, r: 8, fill: a.colorA } ) ); |
| 80 |
svgEls.push( el( 'text', { key: 'ltA', x: lcx - 48, y: ly + 8, dominantBaseline: 'middle', fill: a.labelColor, fontSize: a.legendFontSize || 12, fontWeight: a.legendFontWeight || '400', fontFamily: 'inherit' }, a.labelA || 'Before' ) ); |
| 81 |
svgEls.push( el( 'circle', { key: 'lgB', cx: lcx + 30, cy: ly + 8, r: 8, fill: a.colorB } ) ); |
| 82 |
svgEls.push( el( 'text', { key: 'ltB', x: lcx + 42, y: ly + 8, dominantBaseline: 'middle', fill: a.labelColor, fontSize: a.legendFontSize || 12, fontWeight: a.legendFontWeight || '400', fontFamily: 'inherit' }, a.labelB || 'After' ) ); |
| 83 |
} |
| 84 |
|
| 85 |
return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls ); |
| 86 |
} |
| 87 |
|
| 88 |
function updItem( setAttributes, items, idx, field, val ) { |
| 89 |
setAttributes( { items: items.map( ( it, i ) => i === idx ? { ...it, [field]: val } : it ) } ); |
| 90 |
} |
| 91 |
|
| 92 |
registerBlockType( 'blockenberg/dumbbell-chart', { |
| 93 |
edit: function ( props ) { |
| 94 |
const { attributes: a, setAttributes } = props; |
| 95 |
const blockProps = useBlockProps( { className: 'bkbg-dumbbell-wrap', style: Object.assign( |
| 96 |
{ '--bkbg-db-ttl-fs': (a.titleFontSize || 20) + 'px' }, |
| 97 |
_tv(a.typoTitle || {}, '--bkbg-db-ttl-') |
| 98 |
) } ); |
| 99 |
|
| 100 |
return el( 'div', blockProps, |
| 101 |
el( InspectorControls, {}, |
| 102 |
|
| 103 |
el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true }, |
| 104 |
el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ), |
| 105 |
el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ), |
| 106 |
el( ToggleControl, { label: __( 'Show Values', 'blockenberg' ), checked: a.showValues, onChange: v => setAttributes( { showValues: v } ) } ), |
| 107 |
el( ToggleControl, { label: __( 'Show Grid Lines', 'blockenberg' ), checked: a.showGridLines, onChange: v => setAttributes( { showGridLines: v } ) } ), |
| 108 |
el( ToggleControl, { label: __( 'Show Legend', 'blockenberg' ), checked: a.showLegend, onChange: v => setAttributes( { showLegend: v } ) } ), |
| 109 |
el( TextControl, { label: __( 'Label A (Before)', 'blockenberg' ), value: a.labelA, onChange: v => setAttributes( { labelA: v } ) } ), |
| 110 |
el( TextControl, { label: __( 'Label B (After)', 'blockenberg' ), value: a.labelB, onChange: v => setAttributes( { labelB: v } ) } ), |
| 111 |
), |
| 112 |
|
| 113 |
el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false }, |
| 114 |
el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ), |
| 115 |
el( RangeControl, { label: __( 'Row Height', 'blockenberg' ), value: a.rowHeight, onChange: v => setAttributes( { rowHeight: v } ), min: 28, max: 100 } ), |
| 116 |
el( RangeControl, { label: __( 'Label Column Width', 'blockenberg' ), value: a.padLeft, onChange: v => setAttributes( { padLeft: v } ), min: 60, max: 320 } ), |
| 117 |
el( RangeControl, { label: __( 'Dot Radius', 'blockenberg' ), value: a.dotRadius, onChange: v => setAttributes( { dotRadius: v } ), min: 5, max: 28 } ), |
| 118 |
), |
| 119 |
|
| 120 |
|
| 121 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 122 |
_tc() && el(_tc(), { label: __('Title'), typo: a.typoTitle || {}, onChange: v => setAttributes( { typoTitle: v } ), defaultSize: a.titleFontSize || 20 }), |
| 123 |
el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 9, max: 22 } ), |
| 124 |
el( wp.components.SelectControl, { label: __( 'Label Font Weight', 'blockenberg' ), value: a.labelFontWeight || '400', |
| 125 |
options: [{label:'Normal (400)',value:'400'},{label:'Medium (500)',value:'500'},{label:'Bold (700)',value:'700'}], |
| 126 |
onChange: v => setAttributes( { labelFontWeight: v } ) } ), |
| 127 |
el( RangeControl, { label: __( 'Value Font Size', 'blockenberg' ), value: a.valueFontSize, onChange: v => setAttributes( { valueFontSize: v } ), min: 7, max: 16 } ), |
| 128 |
el( wp.components.SelectControl, { label: __( 'Value Font Weight', 'blockenberg' ), value: a.valueFontWeight || '700', |
| 129 |
options: [{label:'Normal (400)',value:'400'},{label:'Bold (700)',value:'700'},{label:'Extra-Bold (800)',value:'800'}], |
| 130 |
onChange: v => setAttributes( { valueFontWeight: v } ) } ), |
| 131 |
el( RangeControl, { label: __( 'Legend Font Size', 'blockenberg' ), value: a.legendFontSize, onChange: v => setAttributes( { legendFontSize: v } ), min: 9, max: 18, __nextHasNoMarginBottom: true } ), |
| 132 |
el( wp.components.SelectControl, { label: __( 'Legend Font Weight', 'blockenberg' ), value: a.legendFontWeight || '400', |
| 133 |
options: [{label:'Normal (400)',value:'400'},{label:'Medium (500)',value:'500'},{label:'Bold (700)',value:'700'}], |
| 134 |
onChange: v => setAttributes( { legendFontWeight: v } ) } ) |
| 135 |
), |
| 136 |
el( PanelColorSettings, { |
| 137 |
title: __( 'Colors', 'blockenberg' ), initialOpen: false, |
| 138 |
colorSettings: [ |
| 139 |
{ label: __( 'Dot A Color', 'blockenberg' ), value: a.colorA, onChange: v => setAttributes( { colorA: v || '#9ca3af' } ) }, |
| 140 |
{ label: __( 'Dot B Color', 'blockenberg' ), value: a.colorB, onChange: v => setAttributes( { colorB: v || '#4f46e5' } ) }, |
| 141 |
{ label: __( 'Connector Color', 'blockenberg' ), value: a.connectorColor, onChange: v => setAttributes( { connectorColor: v || '#e5e7eb' } ) }, |
| 142 |
{ label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) }, |
| 143 |
{ label: __( 'Grid Lines', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) }, |
| 144 |
{ label: __( 'Title Color', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) }, |
| 145 |
{ label: __( 'Label Color', 'blockenberg' ), value: a.labelColor, onChange: v => setAttributes( { labelColor: v || '#374151' } ) }, |
| 146 |
] |
| 147 |
} ), |
| 148 |
|
| 149 |
el( PanelBody, { title: __( 'Data', 'blockenberg' ), initialOpen: false }, |
| 150 |
el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { items: [ ...a.items, { label: 'New Item', a: 30, b: 60 } ] } ) }, __( '+ Add Row', 'blockenberg' ) ), |
| 151 |
a.items.map( ( item, i ) => |
| 152 |
el( PanelBody, { key: i, title: item.label || `Row ${ i + 1 }`, initialOpen: false }, |
| 153 |
el( TextControl, { label: __( 'Label', 'blockenberg' ), value: item.label, onChange: v => updItem( setAttributes, a.items, i, 'label', v ) } ), |
| 154 |
el( RangeControl, { label: `${ a.labelA || 'A' } Value`, value: item.a, onChange: v => updItem( setAttributes, a.items, i, 'a', v ), min: -1000, max: 10000, step: 0.1 } ), |
| 155 |
el( RangeControl, { label: `${ a.labelB || 'B' } Value`, value: item.b, onChange: v => updItem( setAttributes, a.items, i, 'b', v ), min: -1000, max: 10000, step: 0.1 } ), |
| 156 |
el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { items: a.items.filter( ( _, x ) => x !== i ) } ) }, __( 'Remove', 'blockenberg' ) ), |
| 157 |
) |
| 158 |
), |
| 159 |
), |
| 160 |
), |
| 161 |
|
| 162 |
a.showTitle && a.title && el( 'h3', { className: 'bkbg-db-title', style: { color: a.titleColor } }, a.title ), |
| 163 |
el( 'div', { className: 'bkbg-db-svg' }, renderDumbbell( a ) ), |
| 164 |
); |
| 165 |
}, |
| 166 |
|
| 167 |
save: function ( { attributes: a } ) { |
| 168 |
const blockProps = useBlockProps.save( { className: 'bkbg-dumbbell-wrap', style: Object.assign( |
| 169 |
{ '--bkbg-db-ttl-fs': (a.titleFontSize || 20) + 'px' }, |
| 170 |
_tv(a.typoTitle || {}, '--bkbg-db-ttl-') |
| 171 |
) } ); |
| 172 |
return el( 'div', blockProps, |
| 173 |
a.showTitle && a.title ? el( 'h3', { className: 'bkbg-db-title', style: { color: a.titleColor } }, a.title ) : null, |
| 174 |
el( 'div', { className: 'bkbg-db-svg' }, renderDumbbell( a ) ), |
| 175 |
); |
| 176 |
}, |
| 177 |
} ); |
| 178 |
}() ); |
| 179 |
|