| 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, TextareaControl, Button, ColorPicker, Popover } = window.wp.components; |
| 6 |
const { __ } = window.wp.i18n; |
| 7 |
const { useState } = window.wp.element; |
| 8 |
|
| 9 |
// ── Renderer ────────────────────────────────────────────────────────────── |
| 10 |
function renderStackedArea( a ) { |
| 11 |
const series = a.series || []; |
| 12 |
const xLabels = a.xLabels || []; |
| 13 |
const W = a.svgWidth; |
| 14 |
const H = a.svgHeight; |
| 15 |
const PT = a.padTop; |
| 16 |
const PL = a.padLeft; |
| 17 |
const PR = a.padRight; |
| 18 |
const PB = a.padBottom; |
| 19 |
const chartW = W - PL - PR; |
| 20 |
const chartH = H - PT - PB; |
| 21 |
const n = xLabels.length; |
| 22 |
|
| 23 |
if ( ! series.length || ! n ) return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%' } ); |
| 24 |
|
| 25 |
// Compute cumulative stacks at each x-index |
| 26 |
const stacks = []; // stacks[i][j] = cumulative value at x=j for series up to i |
| 27 |
series.forEach( ( s, si ) => { |
| 28 |
stacks[ si ] = []; |
| 29 |
for ( let j = 0; j < n; j++ ) { |
| 30 |
const prev = si === 0 ? 0 : stacks[ si - 1 ][ j ]; |
| 31 |
stacks[ si ][ j ] = prev + ( ( s.values || [] )[ j ] || 0 ); |
| 32 |
} |
| 33 |
} ); |
| 34 |
|
| 35 |
// Max across all stacks |
| 36 |
const maxVal = Math.max( ...stacks[ stacks.length - 1 ] ) || 1; |
| 37 |
|
| 38 |
function xPos( j ) { return PL + ( j / ( n - 1 ) ) * chartW; } |
| 39 |
function yPos( v ) { return PT + chartH - ( v / maxVal ) * chartH; } |
| 40 |
|
| 41 |
const opacity = ( a.fillOpacity || 80 ) / 100; |
| 42 |
const svgEls = []; |
| 43 |
|
| 44 |
svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) ); |
| 45 |
|
| 46 |
// Grid lines |
| 47 |
if ( a.showGridLines ) { |
| 48 |
const ticks = 5; |
| 49 |
for ( let t = 0; t <= ticks; t++ ) { |
| 50 |
const gy = PT + ( t / ticks ) * chartH; |
| 51 |
svgEls.push( el( 'line', { key: `gl${ t }`, x1: PL, y1: gy, x2: W - PR, y2: gy, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) ); |
| 52 |
const yLbl = Math.round( maxVal * ( 1 - t / ticks ) ); |
| 53 |
svgEls.push( el( 'text', { key: `yl${ t }`, x: PL - 6, y: gy, textAnchor: 'end', dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontFamily: 'inherit' }, yLbl ) ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Area fills (reverse order so bottom series paints on top correctly when overlapping) |
| 58 |
for ( let si = series.length - 1; si >= 0; si-- ) { |
| 59 |
const s = series[ si ]; |
| 60 |
const topY = stacks[ si ]; |
| 61 |
const botY = si === 0 ? Array( n ).fill( 0 ) : stacks[ si - 1 ]; |
| 62 |
|
| 63 |
// Build polygon points: top-edge L→R, bottom-edge R→L |
| 64 |
const pts = []; |
| 65 |
for ( let j = 0; j < n; j++ ) { |
| 66 |
pts.push( `${ xPos( j ) },${ yPos( topY[ j ] ) }` ); |
| 67 |
} |
| 68 |
for ( let j = n - 1; j >= 0; j-- ) { |
| 69 |
pts.push( `${ xPos( j ) },${ yPos( botY[ j ] ) }` ); |
| 70 |
} |
| 71 |
|
| 72 |
svgEls.push( el( 'polygon', { |
| 73 |
key: `area${ si }`, |
| 74 |
points: pts.join( ' ' ), |
| 75 |
fill: s.color || '#6366f1', |
| 76 |
fillOpacity: opacity, |
| 77 |
stroke: s.color || '#6366f1', |
| 78 |
strokeWidth: 1.5, |
| 79 |
strokeOpacity: 1, |
| 80 |
} ) ); |
| 81 |
|
| 82 |
// Dots |
| 83 |
if ( a.showDots ) { |
| 84 |
for ( let j = 0; j < n; j++ ) { |
| 85 |
svgEls.push( el( 'circle', { key: `dot${ si }${ j }`, cx: xPos( j ), cy: yPos( topY[ j ] ), r: 4, fill: s.color || '#6366f1', strokeWidth: 2, stroke: '#fff' } ) ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// Value labels |
| 90 |
if ( a.showValues ) { |
| 91 |
for ( let j = 0; j < n; j++ ) { |
| 92 |
const raw = ( s.values || [] )[ j ] || 0; |
| 93 |
svgEls.push( el( 'text', { key: `sv${ si }${ j }`, x: xPos( j ), y: yPos( topY[ j ] ) - 8, textAnchor: 'middle', fill: s.color || '#6366f1', fontSize: a.valueFontSize, fontWeight: 700, fontFamily: 'inherit' }, raw ) ); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// X-axis labels |
| 99 |
for ( let j = 0; j < n; j++ ) { |
| 100 |
svgEls.push( el( 'text', { key: `xl${ j }`, x: xPos( j ), y: H - PB + 16, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontFamily: 'inherit' }, xLabels[ j ] || '' ) ); |
| 101 |
} |
| 102 |
|
| 103 |
// X-axis baseline |
| 104 |
svgEls.push( el( 'line', { key: 'xax', x1: PL, y1: PT + chartH, x2: W - PR, y2: PT + chartH, stroke: a.textColor, strokeWidth: 1, strokeOpacity: 0.3 } ) ); |
| 105 |
svgEls.push( el( 'line', { key: 'yax', x1: PL, y1: PT, x2: PL, y2: PT + chartH, stroke: a.textColor, strokeWidth: 1, strokeOpacity: 0.3 } ) ); |
| 106 |
|
| 107 |
// Legend |
| 108 |
if ( a.showLegend ) { |
| 109 |
const legY = H - 10; |
| 110 |
const legStep = Math.min( chartW / series.length, 160 ); |
| 111 |
series.forEach( ( s, si ) => { |
| 112 |
const lx = PL + si * legStep; |
| 113 |
svgEls.push( el( 'rect', { key: `lgn${ si }`, x: lx, y: legY - 7, width: 18, height: 10, fill: s.color || '#6366f1', rx: 2, fillOpacity: opacity } ) ); |
| 114 |
svgEls.push( el( 'text', { key: `lgt${ si }`, x: lx + 22, y: legY, dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontFamily: 'inherit' }, s.label || `Series ${ si + 1 }` ) ); |
| 115 |
} ); |
| 116 |
} |
| 117 |
|
| 118 |
return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls ); |
| 119 |
} |
| 120 |
|
| 121 |
function updSer( setAttributes, series, si, field, val ) { |
| 122 |
setAttributes( { series: series.map( ( s, i ) => i === si ? { ...s, [field]: val } : s ) } ); |
| 123 |
} |
| 124 |
|
| 125 |
var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 126 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 127 |
|
| 128 |
function buildWrapStyle(a) { |
| 129 |
var tv = getTypoCssVars(); |
| 130 |
var s = {}; |
| 131 |
Object.assign(s, tv(a.titleTypo, '--bksa-tt-')); |
| 132 |
return s; |
| 133 |
} |
| 134 |
|
| 135 |
/* ── colour-swatch + popover ── */ |
| 136 |
function BkbgColorSwatch(p) { |
| 137 |
var st = useState(false), open = st[0], setOpen = st[1]; |
| 138 |
return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } }, |
| 139 |
el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label), |
| 140 |
el('div', { style:{ position:'relative', flexShrink:0 } }, |
| 141 |
el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); }, |
| 142 |
style:{ width:'28px', height:'28px', borderRadius:'4px', border: open ? '2px solid #007cba' : '2px solid #ddd', cursor:'pointer', padding:0, display:'block', background: p.value||'#ffffff', flexShrink:0 } }), |
| 143 |
open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } }, |
| 144 |
el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } }, |
| 145 |
el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } }, |
| 146 |
el('strong', { style:{ fontSize:'12px' } }, p.label), |
| 147 |
el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } }) |
| 148 |
), |
| 149 |
el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange }) |
| 150 |
) |
| 151 |
) |
| 152 |
) |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
registerBlockType( 'blockenberg/stacked-area', { |
| 157 |
edit: function ( props ) { |
| 158 |
const { attributes: a, setAttributes } = props; |
| 159 |
const blockProps = useBlockProps( { className: 'bkbg-stackedarea-wrap', style: buildWrapStyle(a) } ); |
| 160 |
|
| 161 |
return el( 'div', blockProps, |
| 162 |
el( InspectorControls, {}, |
| 163 |
|
| 164 |
el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true }, |
| 165 |
el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ), |
| 166 |
el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ), |
| 167 |
el( ToggleControl, { label: __( 'Show Grid Lines', 'blockenberg' ), checked: a.showGridLines, onChange: v => setAttributes( { showGridLines: v } ) } ), |
| 168 |
el( ToggleControl, { label: __( 'Show Legend', 'blockenberg' ), checked: a.showLegend, onChange: v => setAttributes( { showLegend: v } ) } ), |
| 169 |
el( ToggleControl, { label: __( 'Show Dots', 'blockenberg' ), checked: a.showDots, onChange: v => setAttributes( { showDots: v } ) } ), |
| 170 |
el( ToggleControl, { label: __( 'Show Values', 'blockenberg' ), checked: a.showValues, onChange: v => setAttributes( { showValues: v } ) } ), |
| 171 |
el( RangeControl, { label: __( 'Fill Opacity %', 'blockenberg' ), value: a.fillOpacity, onChange: v => setAttributes( { fillOpacity: v } ), min: 10, max: 100 } ), |
| 172 |
), |
| 173 |
|
| 174 |
el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false }, |
| 175 |
el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ), |
| 176 |
el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 160, max: 700 } ), |
| 177 |
el( RangeControl, { label: __( 'Left Padding (Y-axis)', 'blockenberg' ), value: a.padLeft, onChange: v => setAttributes( { padLeft: v } ), min: 20, max: 120 } ), |
| 178 |
el( RangeControl, { label: __( 'Bottom Padding', 'blockenberg' ), value: a.padBottom, onChange: v => setAttributes( { padBottom: v } ), min: 20, max: 100 } ), |
| 179 |
), |
| 180 |
|
| 181 |
|
| 182 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 183 |
getTypoControl()({ label: __('Chart Title', 'blockenberg'), value: a.titleTypo, onChange: v => setAttributes({ titleTypo: v }) }), |
| 184 |
el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 20 } ), |
| 185 |
el( RangeControl, { label: __( 'Value Font Size', 'blockenberg' ), value: a.valueFontSize, onChange: v => setAttributes( { valueFontSize: v } ), min: 7, max: 16 } ), |
| 186 |
el( SelectControl, { label: __( 'Label Font Weight', 'blockenberg' ), value: a.labelFontWeight, options: [{label:'400 Regular',value:'400'},{label:'500 Medium',value:'500'},{label:'600 Semi-bold',value:'600'},{label:'700 Bold',value:'700'}], __nextHasNoMarginBottom: true, onChange: v => setAttributes( { labelFontWeight: v } ) } ), |
| 187 |
el( RangeControl, { label: __( 'Label Line Height', 'blockenberg' ), value: a.labelLineHeight, min: 1.0, max: 2.5, step: 0.05, __nextHasNoMarginBottom: true, onChange: v => setAttributes( { labelLineHeight: v } ) } ) |
| 188 |
), |
| 189 |
el( PanelColorSettings, { |
| 190 |
title: __( 'Colors', 'blockenberg' ), initialOpen: false, |
| 191 |
colorSettings: [ |
| 192 |
{ label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) }, |
| 193 |
{ label: __( 'Grid', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) }, |
| 194 |
{ label: __( 'Title', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) }, |
| 195 |
{ label: __( 'Text / Axes','blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) }, |
| 196 |
] |
| 197 |
} ), |
| 198 |
|
| 199 |
el( PanelBody, { title: __( 'X-Axis Labels', 'blockenberg' ), initialOpen: false }, |
| 200 |
el( TextareaControl, { |
| 201 |
label: __( 'Labels (one per line)', 'blockenberg' ), |
| 202 |
value: ( a.xLabels || [] ).join( '\n' ), |
| 203 |
onChange: v => setAttributes( { xLabels: v.split( '\n' ).map( s => s.trim() ).filter( Boolean ) } ), |
| 204 |
} ), |
| 205 |
), |
| 206 |
|
| 207 |
el( PanelBody, { title: __( 'Series', 'blockenberg' ), initialOpen: false }, |
| 208 |
el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { series: [ ...a.series, { label: 'New Series', color: '#8b5cf6', values: ( a.xLabels || [] ).map( () => 50 ) } ] } ) }, __( '+ Add Series', 'blockenberg' ) ), |
| 209 |
a.series.map( ( s, si ) => |
| 210 |
el( PanelBody, { key: si, title: s.label || `Series ${ si + 1 }`, initialOpen: false }, |
| 211 |
el( TextControl, { label: __( 'Label', 'blockenberg' ), value: s.label, onChange: v => updSer( setAttributes, a.series, si, 'label', v ) } ), |
| 212 |
el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: s.color, onChange: v => updSer( setAttributes, a.series, si, 'color', v ) } ), |
| 213 |
el( TextareaControl, { |
| 214 |
label: __( 'Values (comma-separated)', 'blockenberg' ), |
| 215 |
value: ( s.values || [] ).join( ', ' ), |
| 216 |
onChange: v => updSer( setAttributes, a.series, si, 'values', v.split( ',' ).map( n => parseFloat( n.trim() ) || 0 ) ), |
| 217 |
} ), |
| 218 |
el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { series: a.series.filter( ( _, x ) => x !== si ) } ) }, __( 'Remove', 'blockenberg' ) ), |
| 219 |
) |
| 220 |
), |
| 221 |
), |
| 222 |
), |
| 223 |
|
| 224 |
a.showTitle && a.title && el( 'h3', { className: 'bkbg-stackedarea-title', style: { color: a.titleColor } }, a.title ), |
| 225 |
el( 'div', { className: 'bkbg-stackedarea-svg' }, renderStackedArea( a ) ), |
| 226 |
); |
| 227 |
}, |
| 228 |
|
| 229 |
save: function ( { attributes: a } ) { |
| 230 |
const blockProps = useBlockProps.save( { className: 'bkbg-stackedarea-wrap', style: buildWrapStyle(a) } ); |
| 231 |
return el( 'div', blockProps, |
| 232 |
a.showTitle && a.title ? el( 'h3', { className: 'bkbg-stackedarea-title', style: { color: a.titleColor } }, a.title ) : null, |
| 233 |
el( 'div', { className: 'bkbg-stackedarea-svg' }, renderStackedArea( a ) ), |
| 234 |
); |
| 235 |
}, |
| 236 |
} ); |
| 237 |
}() ); |
| 238 |
|