| 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, TextareaControl, Button, ColorPicker, Popover } = window.wp.components; |
| 6 |
const { __ } = window.wp.i18n; |
| 7 |
const { useState } = window.wp.element; |
| 8 |
|
| 9 |
var _tc, _tv; |
| 10 |
function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 11 |
function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 12 |
|
| 13 |
// ── Renderer ────────────────────────────────────────────────────────────── |
| 14 |
function renderScatterPlot( a ) { |
| 15 |
const series = a.series || []; |
| 16 |
const W = a.svgWidth; |
| 17 |
const H = a.svgHeight; |
| 18 |
const PT = a.padTop; |
| 19 |
const PL = a.padLeft; |
| 20 |
const PR = a.padRight; |
| 21 |
const PB = a.padBottom; |
| 22 |
const chartW = W - PL - PR; |
| 23 |
const chartH = H - PT - PB; |
| 24 |
const xMin = a.xMin, xMax = a.xMax, yMin = a.yMin, yMax = a.yMax; |
| 25 |
const xRange = ( xMax - xMin ) || 1; |
| 26 |
const yRange = ( yMax - yMin ) || 1; |
| 27 |
const DR = a.dotRadius; |
| 28 |
|
| 29 |
function sx( v ) { return PL + ( ( v - xMin ) / xRange ) * chartW; } |
| 30 |
function sy( v ) { return PT + chartH - ( ( v - yMin ) / yRange ) * chartH; } |
| 31 |
|
| 32 |
const svgEls = []; |
| 33 |
svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) ); |
| 34 |
|
| 35 |
// Grid |
| 36 |
if ( a.showGrid ) { |
| 37 |
const ticks = 5; |
| 38 |
for ( let t = 0; t <= ticks; t++ ) { |
| 39 |
const gx = PL + ( t / ticks ) * chartW; |
| 40 |
const gy = PT + ( t / ticks ) * chartH; |
| 41 |
svgEls.push( el( 'line', { key: `gx${ t }`, x1: gx, y1: PT, x2: gx, y2: PT + chartH, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) ); |
| 42 |
svgEls.push( el( 'line', { key: `gy${ t }`, x1: PL, y1: gy, x2: PL + chartW, y2: gy, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) ); |
| 43 |
const xV = xMin + ( t / ticks ) * xRange; |
| 44 |
const yV = yMax - ( t / ticks ) * yRange; |
| 45 |
svgEls.push( el( 'text', { key: `xl${ t }`, x: gx, y: PT + chartH + 16, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontWeight: a.labelFontWeight, fontFamily: 'inherit' }, Math.round( xV ) ) ); |
| 46 |
svgEls.push( el( 'text', { key: `yl${ t }`, x: PL - 8, y: gy, textAnchor: 'end', dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontWeight: a.labelFontWeight, fontFamily: 'inherit' }, Math.round( yV ) ) ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Axes |
| 51 |
svgEls.push( el( 'line', { key: 'xax', x1: PL, y1: PT + chartH, x2: W - PR, y2: PT + chartH, stroke: a.textColor, strokeWidth: 1, strokeOpacity: 0.4 } ) ); |
| 52 |
svgEls.push( el( 'line', { key: 'yax', x1: PL, y1: PT, x2: PL, y2: PT + chartH, stroke: a.textColor, strokeWidth: 1, strokeOpacity: 0.4 } ) ); |
| 53 |
|
| 54 |
// Axis labels |
| 55 |
svgEls.push( el( 'text', { key: 'xaxlbl', x: PL + chartW / 2, y: H - 8, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontWeight: a.labelFontWeight, fontFamily: 'inherit' }, a.xAxisLabel ) ); |
| 56 |
svgEls.push( el( 'text', { key: 'yaxlbl', x: 14, y: PT + chartH / 2, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontWeight: a.labelFontWeight, fontFamily: 'inherit', transform: `rotate(-90, 14, ${ PT + chartH / 2 })` }, a.yAxisLabel ) ); |
| 57 |
|
| 58 |
// Series dots |
| 59 |
series.forEach( ( s, si ) => { |
| 60 |
( s.points || [] ).forEach( ( p, pi ) => { |
| 61 |
const px = sx( p.x || 0 ); |
| 62 |
const py = sy( p.y || 0 ); |
| 63 |
svgEls.push( el( 'circle', { key: `dot${ si }${ pi }`, cx: px, cy: py, r: DR, fill: s.color || '#6366f1', stroke: '#fff', strokeWidth: 1.5, fillOpacity: 0.85 } ) ); |
| 64 |
if ( a.showLabels && p.label ) { |
| 65 |
svgEls.push( el( 'text', { key: `lbl${ si }${ pi }`, x: px, y: py - DR - 3, textAnchor: 'middle', fill: s.color || '#6366f1', fontSize: a.labelFontSize - 2, fontWeight: a.labelFontWeight, fontFamily: 'inherit' }, p.label ) ); |
| 66 |
} |
| 67 |
} ); |
| 68 |
} ); |
| 69 |
|
| 70 |
// Legend |
| 71 |
if ( a.showLegend ) { |
| 72 |
const legY = H - PB + 34; |
| 73 |
const legStep = Math.min( 150, chartW / series.length ); |
| 74 |
series.forEach( ( s, si ) => { |
| 75 |
const lx = PL + si * legStep; |
| 76 |
svgEls.push( el( 'circle', { key: `lgc${ si }`, cx: lx + 6, cy: legY, r: 6, fill: s.color || '#6366f1' } ) ); |
| 77 |
svgEls.push( el( 'text', { key: `lgt${ si }`, x: lx + 16, y: legY, dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontWeight: a.labelFontWeight, fontFamily: 'inherit' }, s.label || `Series ${ si + 1 }` ) ); |
| 78 |
} ); |
| 79 |
} |
| 80 |
|
| 81 |
return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls ); |
| 82 |
} |
| 83 |
|
| 84 |
function updSer( setAttributes, series, si, field, val ) { |
| 85 |
setAttributes( { series: series.map( ( s, i ) => i === si ? { ...s, [field]: val } : s ) } ); |
| 86 |
} |
| 87 |
|
| 88 |
/* ── colour-swatch + popover ── */ |
| 89 |
function BkbgColorSwatch(p) { |
| 90 |
var st = useState(false), open = st[0], setOpen = st[1]; |
| 91 |
return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } }, |
| 92 |
el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label), |
| 93 |
el('div', { style:{ position:'relative', flexShrink:0 } }, |
| 94 |
el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); }, |
| 95 |
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 } }), |
| 96 |
open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } }, |
| 97 |
el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } }, |
| 98 |
el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } }, |
| 99 |
el('strong', { style:{ fontSize:'12px' } }, p.label), |
| 100 |
el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } }) |
| 101 |
), |
| 102 |
el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange }) |
| 103 |
) |
| 104 |
) |
| 105 |
) |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
registerBlockType( 'blockenberg/scatter-plot', { |
| 110 |
icon: el('svg', { xmlns: 'http://www.w3.org/2000/svg', viewBox: '0 0 24 24', width: 24, height: 24 }, |
| 111 |
el('path', { d: 'M3 3v18h18', stroke: 'currentColor', strokeWidth: 1.5, fill: 'none' }), |
| 112 |
el('circle', { cx: 8, cy: 16, r: 1.5, fill: 'currentColor' }), |
| 113 |
el('circle', { cx: 11, cy: 11, r: 1.5, fill: 'currentColor' }), |
| 114 |
el('circle', { cx: 15, cy: 14, r: 1.5, fill: 'currentColor' }), |
| 115 |
el('circle', { cx: 14, cy: 8, r: 1.5, fill: 'currentColor' }), |
| 116 |
el('circle', { cx: 18, cy: 6, r: 1.5, fill: 'currentColor' }), |
| 117 |
el('circle', { cx: 7, cy: 10, r: 1.5, fill: 'currentColor' }) |
| 118 |
), |
| 119 |
edit: function ( props ) { |
| 120 |
const { attributes: a, setAttributes } = props; |
| 121 |
const blockProps = useBlockProps( (function() { |
| 122 |
var _tv = getTypoCssVars(); |
| 123 |
var s = {}; |
| 124 |
if (_tv) Object.assign(s, _tv(a.titleTypo || {}, '--bksp-tt-')); |
| 125 |
return { className: 'bkbg-scatter-wrap', style: s }; |
| 126 |
})() ); |
| 127 |
|
| 128 |
return el( 'div', blockProps, |
| 129 |
el( InspectorControls, {}, |
| 130 |
|
| 131 |
el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true }, |
| 132 |
el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ), |
| 133 |
el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ), |
| 134 |
el( ToggleControl, { label: __( 'Show Grid', 'blockenberg' ), checked: a.showGrid, onChange: v => setAttributes( { showGrid: v } ) } ), |
| 135 |
el( ToggleControl, { label: __( 'Show Legend', 'blockenberg' ), checked: a.showLegend, onChange: v => setAttributes( { showLegend: v } ) } ), |
| 136 |
el( ToggleControl, { label: __( 'Show Point Labels', 'blockenberg' ), checked: a.showLabels, onChange: v => setAttributes( { showLabels: v } ) } ), |
| 137 |
el( TextControl, { label: __( 'X Axis Label', 'blockenberg' ), value: a.xAxisLabel, onChange: v => setAttributes( { xAxisLabel: v } ) } ), |
| 138 |
el( TextControl, { label: __( 'Y Axis Label', 'blockenberg' ), value: a.yAxisLabel, onChange: v => setAttributes( { yAxisLabel: v } ) } ), |
| 139 |
), |
| 140 |
|
| 141 |
el( PanelBody, { title: __( 'Axis Range', 'blockenberg' ), initialOpen: false }, |
| 142 |
el( RangeControl, { label: __( 'X Min', 'blockenberg' ), value: a.xMin, onChange: v => setAttributes( { xMin: v } ), min: -10000, max: 10000 } ), |
| 143 |
el( RangeControl, { label: __( 'X Max', 'blockenberg' ), value: a.xMax, onChange: v => setAttributes( { xMax: v } ), min: -10000, max: 10000 } ), |
| 144 |
el( RangeControl, { label: __( 'Y Min', 'blockenberg' ), value: a.yMin, onChange: v => setAttributes( { yMin: v } ), min: -10000, max: 10000 } ), |
| 145 |
el( RangeControl, { label: __( 'Y Max', 'blockenberg' ), value: a.yMax, onChange: v => setAttributes( { yMax: v } ), min: -10000, max: 10000 } ), |
| 146 |
), |
| 147 |
|
| 148 |
el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false }, |
| 149 |
el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ), |
| 150 |
el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 160, max: 700 } ), |
| 151 |
el( RangeControl, { label: __( 'Dot Radius', 'blockenberg' ), value: a.dotRadius, onChange: v => setAttributes( { dotRadius: v } ), min: 2, max: 20 } ), |
| 152 |
el( RangeControl, { label: __( 'Left Padding', 'blockenberg' ), value: a.padLeft, onChange: v => setAttributes( { padLeft: v } ), min: 20, max: 120 } ), |
| 153 |
el( RangeControl, { label: __( 'Bottom Padding', 'blockenberg' ),value: a.padBottom, onChange: v => setAttributes( { padBottom: v } ), min: 30, max: 120 } ), |
| 154 |
), |
| 155 |
|
| 156 |
|
| 157 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 158 |
getTypoControl() && el( getTypoControl(), { label: __( 'Title Typography', 'blockenberg' ), value: a.titleTypo || {}, onChange: function(v){ setAttributes({ titleTypo: v }); } }), |
| 159 |
el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 20 } ), |
| 160 |
el( RangeControl, { label: __( 'Label Font Weight', 'blockenberg' ), value: a.labelFontWeight, onChange: v => setAttributes( { labelFontWeight: v } ), min: 300, max: 900, step: 100, __nextHasNoMarginBottom: true } ) |
| 161 |
), |
| 162 |
el( PanelColorSettings, { |
| 163 |
title: __( 'Colors', 'blockenberg' ), initialOpen: false, |
| 164 |
colorSettings: [ |
| 165 |
{ label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) }, |
| 166 |
{ label: __( 'Grid', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) }, |
| 167 |
{ label: __( 'Title', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) }, |
| 168 |
{ label: __( 'Text/Axes', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) }, |
| 169 |
] |
| 170 |
} ), |
| 171 |
|
| 172 |
el( PanelBody, { title: __( 'Series', 'blockenberg' ), initialOpen: false }, |
| 173 |
el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { series: [ ...a.series, { label: 'New Series', color: '#8b5cf6', points: [ { x: 50, y: 50, label: 'Point' } ] } ] } ) }, __( '+ Add Series', 'blockenberg' ) ), |
| 174 |
a.series.map( ( s, si ) => |
| 175 |
el( PanelBody, { key: si, title: s.label || `Series ${ si + 1 }`, initialOpen: false }, |
| 176 |
el( TextControl, { label: __( 'Label', 'blockenberg' ), value: s.label, onChange: v => updSer( setAttributes, a.series, si, 'label', v ) } ), |
| 177 |
el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: s.color, onChange: v => updSer( setAttributes, a.series, si, 'color', v ) } ), |
| 178 |
el( 'p', { style: { fontSize: 12, color: '#6b7280', marginTop: 8, marginBottom: 4 } }, __( 'Points — one per line: x, y, Label', 'blockenberg' ) ), |
| 179 |
el( TextareaControl, { |
| 180 |
label: __( 'Points (x, y, Label)', 'blockenberg' ), |
| 181 |
value: ( s.points || [] ).map( p => `${ p.x }, ${ p.y }, ${ p.label || '' }` ).join( '\n' ), |
| 182 |
onChange: v => updSer( setAttributes, a.series, si, 'points', |
| 183 |
v.split( '\n' ).filter( Boolean ).map( line => { |
| 184 |
const parts = line.split( ',' ); |
| 185 |
return { x: parseFloat( parts[0] ) || 0, y: parseFloat( parts[1] ) || 0, label: ( parts[2] || '' ).trim() }; |
| 186 |
} ) |
| 187 |
), |
| 188 |
rows: 6, |
| 189 |
} ), |
| 190 |
el( Button, { isDestructive: true, isSmall: true, style: { marginTop: 6 }, onClick: () => setAttributes( { series: a.series.filter( ( _, x ) => x !== si ) } ) }, __( 'Remove Series', 'blockenberg' ) ), |
| 191 |
) |
| 192 |
), |
| 193 |
), |
| 194 |
), |
| 195 |
|
| 196 |
a.showTitle && a.title && el( 'h3', { className: 'bkbg-scatter-title', style: { color: a.titleColor } }, a.title ), |
| 197 |
el( 'div', { className: 'bkbg-scatter-svg' }, renderScatterPlot( a ) ), |
| 198 |
); |
| 199 |
}, |
| 200 |
|
| 201 |
save: function ( { attributes: a } ) { |
| 202 |
var _tv = getTypoCssVars(); |
| 203 |
var s = {}; |
| 204 |
if (_tv) Object.assign(s, _tv(a.titleTypo || {}, '--bksp-tt-')); |
| 205 |
const blockProps = useBlockProps.save( { className: 'bkbg-scatter-wrap', style: s } ); |
| 206 |
return el( 'div', blockProps, |
| 207 |
a.showTitle && a.title ? el( 'h3', { className: 'bkbg-scatter-title', style: { color: a.titleColor } }, a.title ) : null, |
| 208 |
el( 'div', { className: 'bkbg-scatter-svg' }, renderScatterPlot( a ) ), |
| 209 |
); |
| 210 |
}, |
| 211 |
deprecated: [ { |
| 212 |
attributes: {"title":{"type":"string","default":"Performance vs Satisfaction"},"showTitle":{"type":"boolean","default":true},"svgWidth":{"type":"number","default":680},"svgHeight":{"type":"number","default":380},"padTop":{"type":"number","default":30},"padLeft":{"type":"number","default":60},"padRight":{"type":"number","default":30},"padBottom":{"type":"number","default":60},"dotRadius":{"type":"number","default":6},"showGrid":{"type":"boolean","default":true},"showLegend":{"type":"boolean","default":true},"showLabels":{"type":"boolean","default":false},"xAxisLabel":{"type":"string","default":"Performance Score"},"yAxisLabel":{"type":"string","default":"Satisfaction Index"},"xMin":{"type":"number","default":0},"xMax":{"type":"number","default":100},"yMin":{"type":"number","default":0},"yMax":{"type":"number","default":100},"labelFontSize":{"type":"number","default":12},"labelFontWeight":{"type":"number","default":400},"bgColor":{"type":"string","default":"#ffffff"},"gridColor":{"type":"string","default":"#f3f4f6"},"textColor":{"type":"string","default":"#374151"},"titleColor":{"type":"string","default":"#111827"},"series":{"type":"array","default":[{"label":"Team A","color":"#6366f1","points":[{"x":72,"y":68,"label":"Alice"},{"x":85,"y":80,"label":"Bob"},{"x":61,"y":74,"label":"Carol"},{"x":90,"y":88,"label":"Dave"},{"x":78,"y":72,"label":"Eve"}]},{"label":"Team B","color":"#10b981","points":[{"x":45,"y":52,"label":"Frank"},{"x":58,"y":64,"label":"Grace"},{"x":39,"y":45,"label":"Henry"},{"x":67,"y":70,"label":"Iris"},{"x":53,"y":58,"label":"Jack"}]},{"label":"Team C","color":"#f59e0b","points":[{"x":20,"y":35,"label":"Kim"},{"x":32,"y":28,"label":"Leo"},{"x":15,"y":42,"label":"Mia"},{"x":28,"y":31,"label":"Ned"},{"x":40,"y":38,"label":"Ora"}]}],"items":{"type":"object"}}}, |
| 213 |
save: function ( { attributes: a } ) { |
| 214 |
const blockProps = useBlockProps.save( { className: 'bkbg-scatter-wrap' } ); |
| 215 |
return el( 'div', blockProps, |
| 216 |
a.showTitle && a.title ? el( 'h3', { className: 'bkbg-scatter-title', style: { color: a.titleColor } }, a.title ) : null, |
| 217 |
el( 'div', { className: 'bkbg-scatter-svg' }, renderScatterPlot( a ) ), |
| 218 |
); |
| 219 |
}, |
| 220 |
} ], |
| 221 |
} ); |
| 222 |
}() ); |
| 223 |
|