PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.8
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.8
2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / dot-plot / index.js

index.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.8, at blocks/dot-plot/index.js

216 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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, ColorPicker, Popover } = window.wp.components;
6 const { __ } = window.wp.i18n;
7 const { useState } = window.wp.element;
8
9 var _dotplotTC, _dotplotTV;
10 function _tc() { return _dotplotTC || (_dotplotTC = window.bkbgTypographyControl); }
11 function _tv(t, p) { return (_dotplotTV || (_dotplotTV = window.bkbgTypoCssVars)) ? _dotplotTV(t, p) : {}; }
12
13 // ── Deterministic jitter (seeded by index so save() matches edit()) ───────
14 function pseudoJitter( seed, range ) {
15 const x = Math.sin( seed * 127.1 ) * 43758.5453123;
16 return ( x - Math.floor( x ) - 0.5 ) * 2 * range;
17 }
18
19 // ── Renderer ──────────────────────────────────────────────────────────────
20 function renderDotPlot( a ) {
21 const series = a.series || [];
22 const W = a.svgWidth;
23 const H = a.svgHeight;
24 const PT = a.padTop;
25 const PL = a.padLeft;
26 const PR = a.padRight;
27 const PB = a.padBottom;
28 const DR = a.dotRadius;
29 const jitter = a.jitter || 0;
30
31 const allPts = series.flatMap( s => s.points || [] );
32 const minVal = Math.min( ...allPts, 0 );
33 const maxVal = Math.max( ...allPts, 1 );
34 const chartW = W - PL - PR;
35 const chartH = H - PT - PB;
36 const rowH = series.length > 0 ? chartH / series.length : chartH;
37
38 function xScale( v ) { return PL + ( ( v - minVal ) / ( maxVal - minVal ) ) * chartW; }
39
40 const svgEls = [];
41 svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) );
42
43 // Grid lines
44 if ( a.showGridLines ) {
45 const ticks = 5;
46 for ( let i = 0; i <= ticks; i++ ) {
47 const v = minVal + ( maxVal - minVal ) * i / ticks;
48 const gx = xScale( v );
49 svgEls.push( el( 'line', { key: `gl${ i }`, x1: gx, y1: PT, x2: gx, y2: H - PB, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) );
50 svgEls.push( el( 'text', { key: `glt${ i }`, x: gx, y: H - PB + 14, textAnchor: 'middle', fill: '#9ca3af', fontSize: a.axisFontSize, fontWeight: a.axisFontWeight || '400', fontFamily: 'inherit' }, String( parseFloat( v.toFixed( 0 ) ) ) ) );
51 }
52 }
53
54 // Axis baseline
55 svgEls.push( el( 'line', { key: 'axis', x1: PL, y1: H - PB, x2: W - PR, y2: H - PB, stroke: '#d1d5db', strokeWidth: 1 } ) );
56
57 // Series rows
58 series.forEach( ( ser, si ) => {
59 const pts = ser.points || [];
60 const cy = PT + si * rowH + rowH / 2;
61 const clr = ser.color || '#6366f1';
62 const opac = ( a.dotOpacity || 75 ) / 100;
63
64 // Mean line
65 if ( a.showMeanLine && pts.length > 0 ) {
66 const mean = pts.reduce( ( s, v ) => s + v, 0 ) / pts.length;
67 const mx = xScale( mean );
68 svgEls.push( el( 'line', { key: `ml${ si }`, x1: mx, y1: cy - rowH * 0.38, x2: mx, y2: cy + rowH * 0.38, stroke: clr, strokeWidth: 2.5, strokeDasharray: '4 3', opacity: 0.9 } ) );
69 }
70
71 // Dots
72 pts.forEach( ( v, pi ) => {
73 const cx = xScale( v );
74 const jy = pseudoJitter( si * 1000 + pi, jitter );
75 svgEls.push( el( 'circle', { key: `d${ si }${ pi }`, cx, cy: cy + jy, r: DR, fill: clr, opacity: opac } ) );
76 } );
77
78 // Row label
79 svgEls.push( el( 'text', { key: `lbl${ si }`, x: PL - 8, y: cy, textAnchor: 'end', dominantBaseline: 'middle', fill: a.textColor || '#374151', fontSize: a.labelFontSize, fontWeight: a.labelFontWeight || '400', fontFamily: 'inherit' }, ser.label || '' ) );
80 } );
81
82 // Legend (top right)
83 if ( a.showLegend && series.length > 0 ) {
84 series.forEach( ( ser, si ) => {
85 const lx = W - PR - 10;
86 const ly = PT + si * 20;
87 svgEls.push( el( 'circle', { key: `lc${ si }`, cx: lx - 90, cy: ly, r: 6, fill: ser.color } ) );
88 svgEls.push( el( 'text', { key: `lt${ si }`, x: lx - 80, y: ly, dominantBaseline: 'middle', fill: a.textColor, fontSize: 11, fontFamily: 'inherit' }, ser.label ) );
89 } );
90 }
91
92 return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls );
93 }
94
95 function updPts( setAttributes, series, si, raw ) {
96 const pts = raw.split( /[\s,]+/ ).map( Number ).filter( v => !isNaN( v ) );
97 setAttributes( { series: series.map( ( s, i ) => i === si ? { ...s, points: pts } : s ) } );
98 }
99
100 function updSer( setAttributes, series, si, field, val ) {
101 setAttributes( { series: series.map( ( s, i ) => i === si ? { ...s, [field]: val } : s ) } );
102 }
103
104 /* ── colour-swatch + popover ── */
105 function BkbgColorSwatch(p) {
106 var st = useState(false), open = st[0], setOpen = st[1];
107 return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } },
108 el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label),
109 el('div', { style:{ position:'relative', flexShrink:0 } },
110 el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); },
111 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 } }),
112 open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } },
113 el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } },
114 el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } },
115 el('strong', { style:{ fontSize:'12px' } }, p.label),
116 el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } })
117 ),
118 el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange })
119 )
120 )
121 )
122 );
123 }
124
125 registerBlockType( 'blockenberg/dot-plot', {
126 icon: el('svg', { xmlns: 'http://www.w3.org/2000/svg', viewBox: '0 0 24 24', width: 24, height: 24 },
127 el('circle', { cx: 6, cy: 8, r: 2, fill: 'currentColor' }),
128 el('circle', { cx: 10, cy: 8, r: 2, fill: 'currentColor' }),
129 el('circle', { cx: 14, cy: 8, r: 2, fill: 'currentColor', opacity: 0.4 }),
130 el('circle', { cx: 6, cy: 14, r: 2, fill: 'currentColor' }),
131 el('circle', { cx: 10, cy: 14, r: 2, fill: 'currentColor', opacity: 0.4 }),
132 el('circle', { cx: 6, cy: 20, r: 2, fill: 'currentColor' }),
133 el('circle', { cx: 10, cy: 20, r: 2, fill: 'currentColor' }),
134 el('circle', { cx: 14, cy: 20, r: 2, fill: 'currentColor' }),
135 el('circle', { cx: 18, cy: 20, r: 2, fill: 'currentColor' })
136 ),
137 edit: function ( props ) {
138 const { attributes: a, setAttributes } = props;
139 const blockProps = useBlockProps( { className: 'bkbg-dotplot-wrap', style: Object.assign(
140 { '--bkbg-dotplot-ttl-fs': (a.titleFontSize || 18) + 'px' },
141 _tv(a.typoTitle || {}, '--bkbg-dotplot-ttl-')
142 ) } );
143
144 return el( 'div', blockProps,
145 el( InspectorControls, {},
146
147 el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true },
148 el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ),
149 el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ),
150 el( ToggleControl, { label: __( 'Show Grid Lines', 'blockenberg' ), checked: a.showGridLines, onChange: v => setAttributes( { showGridLines: v } ) } ),
151 el( ToggleControl, { label: __( 'Show Mean Line', 'blockenberg' ), checked: a.showMeanLine, onChange: v => setAttributes( { showMeanLine: v } ) } ),
152 el( ToggleControl, { label: __( 'Show Legend', 'blockenberg' ), checked: a.showLegend, onChange: v => setAttributes( { showLegend: v } ) } ),
153 ),
154
155 el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false },
156 el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ),
157 el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 140, max: 700 } ),
158 el( RangeControl, { label: __( 'Label Column Width', 'blockenberg' ), value: a.padLeft, onChange: v => setAttributes( { padLeft: v } ), min: 50, max: 280 } ),
159 el( RangeControl, { label: __( 'Dot Radius', 'blockenberg' ), value: a.dotRadius, onChange: v => setAttributes( { dotRadius: v } ), min: 2, max: 22 } ),
160 el( RangeControl, { label: __( 'Dot Opacity %', 'blockenberg' ), value: a.dotOpacity, onChange: v => setAttributes( { dotOpacity: v } ), min: 10, max: 100 } ),
161 el( RangeControl, { label: __( 'Jitter Range', 'blockenberg' ), value: a.jitter, onChange: v => setAttributes( { jitter: v } ), min: 0, max: 30 } ),
162 ),
163
164
165 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
166 _tc() && el(_tc(), { label: __('Title'), typo: a.typoTitle || {}, onChange: v => setAttributes( { typoTitle: v } ), defaultSize: a.titleFontSize || 18 }),
167 el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 9, max: 22 } ),
168 el( wp.components.SelectControl, { label: __( 'Label Font Weight', 'blockenberg' ), value: a.labelFontWeight || '400',
169 options: [{label:'Normal (400)',value:'400'},{label:'Medium (500)',value:'500'},{label:'Bold (700)',value:'700'}],
170 onChange: v => setAttributes( { labelFontWeight: v } ) } ),
171 el( RangeControl, { label: __( 'Axis Font Size (px)', 'blockenberg' ), value: a.axisFontSize, min: 8, max: 18, onChange: v => setAttributes( { axisFontSize: v } ) } ),
172 el( wp.components.SelectControl, { label: __( 'Axis Font Weight', 'blockenberg' ), value: a.axisFontWeight || '400',
173 options: [{label:'Normal (400)',value:'400'},{label:'Medium (500)',value:'500'},{label:'Bold (700)',value:'700'}],
174 onChange: v => setAttributes( { axisFontWeight: v } ) } )
175 ),
176 el( PanelColorSettings, {
177 title: __( 'Colors', 'blockenberg' ), initialOpen: false,
178 colorSettings: [
179 { label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) },
180 { label: __( 'Grid Lines', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) },
181 { label: __( 'Title Color', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) },
182 { label: __( 'Text Color', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) },
183 ]
184 } ),
185
186 el( PanelBody, { title: __( 'Series', 'blockenberg' ), initialOpen: false },
187 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { series: [ ...a.series, { label: 'New Series', color: '#6366f1', points: [50, 80, 60, 100, 70] } ] } ) }, __( '+ Add Series', 'blockenberg' ) ),
188 a.series.map( ( ser, si ) =>
189 el( PanelBody, { key: si, title: ser.label || `Series ${ si + 1 }`, initialOpen: false },
190 el( TextControl, { label: __( 'Label', 'blockenberg' ), value: ser.label, onChange: v => updSer( setAttributes, a.series, si, 'label', v ) } ),
191 el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: ser.color, onChange: v => updSer( setAttributes, a.series, si, 'color', v ) } ),
192 el( TextControl, { label: __( 'Data Points (comma/space separated)', 'blockenberg' ), value: ( ser.points || [] ).join( ', ' ), onChange: v => updPts( setAttributes, a.series, si, v ) } ),
193 el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { series: a.series.filter( ( _, x ) => x !== si ) } ) }, __( 'Remove', 'blockenberg' ) ),
194 )
195 ),
196 ),
197 ),
198
199 a.showTitle && a.title && el( 'h3', { className: 'bkbg-dotplot-title', style: { color: a.titleColor } }, a.title ),
200 el( 'div', { className: 'bkbg-dotplot-svg' }, renderDotPlot( a ) ),
201 );
202 },
203
204 save: function ( { attributes: a } ) {
205 const blockProps = useBlockProps.save( { className: 'bkbg-dotplot-wrap', style: Object.assign(
206 { '--bkbg-dotplot-ttl-fs': (a.titleFontSize || 18) + 'px' },
207 _tv(a.typoTitle || {}, '--bkbg-dotplot-ttl-')
208 ) } );
209 return el( 'div', blockProps,
210 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-dotplot-title', style: { color: a.titleColor } }, a.title ) : null,
211 el( 'div', { className: 'bkbg-dotplot-svg' }, renderDotPlot( a ) ),
212 );
213 },
214 } );
215 }() );
216