PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.12
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.12
2.0.14 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 / sankey-diagram / index.js

index.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.12, at blocks/sankey-diagram/index.js

337 lines 19.6 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, SelectControl, 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 // ── Sankey layout engine ──────────────────────────────────────────────────
14 // Assigns columns (x-layers) by BFS traversal, then distributes nodes
15 // vertically and draws cubic-bezier flow paths.
16
17 function buildSankey( nodes, links, W, H, nodeWidth, nodePad ) {
18 if ( !nodes.length || !links.length ) return { svgNodes: [], svgLinks: [] };
19
20 const nMap = {};
21 nodes.forEach( n => nMap[n.id] = { ...n, inValue: 0, outValue: 0, col: -1 } );
22
23 // Sum flows
24 links.forEach( lk => {
25 if ( nMap[lk.source] ) nMap[lk.source].outValue += lk.value || 0;
26 if ( nMap[lk.target] ) nMap[lk.target].inValue += lk.value || 0;
27 } );
28
29 // BFS column assignment — nodes with no in-links are col 0
30 const hasIn = new Set( links.map( lk => lk.target ) );
31 const colMap = {};
32 const queue = nodes.filter( n => !hasIn.has( n.id ) ).map( n => n.id );
33 queue.forEach( id => { colMap[id] = 0; } );
34
35 let head = 0;
36 while ( head < queue.length ) {
37 const id = queue[head++];
38 links.forEach( lk => {
39 if ( lk.source === id ) {
40 const newCol = ( colMap[id] || 0 ) + 1;
41 if ( colMap[lk.target] === undefined || colMap[lk.target] < newCol ) {
42 colMap[lk.target] = newCol;
43 if ( !queue.includes( lk.target ) ) queue.push( lk.target );
44 }
45 }
46 } );
47 }
48 // Fallback for isolated nodes
49 nodes.forEach( n => { if ( colMap[n.id] === undefined ) colMap[n.id] = 0; } );
50
51 const maxCol = Math.max( ...nodes.map( n => colMap[n.id] ), 0 );
52 const colCnt = maxCol + 1;
53
54 // Group nodes per column
55 const byCols = Array.from( { length: colCnt }, () => [] );
56 nodes.forEach( n => byCols[colCnt - 1 - colMap[n.id]].push( n.id ) ); // reverse: sinks on right
57
58 // Actually keep sources on left
59 nodes.forEach( n => {
60 byCols[ colMap[n.id] ] = byCols[ colMap[n.id] ] || [];
61 } );
62 // Redo groups
63 for ( let i = 0; i <= maxCol; i++ ) byCols[i] = [];
64 nodes.forEach( n => byCols[ colMap[n.id] ].push( n.id ) );
65
66 // Column x positions
67 const colX = {};
68 const colW = ( W - nodeWidth ) / Math.max( maxCol, 1 );
69 for ( let c = 0; c <= maxCol; c++ ) {
70 colX[c] = c * colW;
71 }
72
73 // Node value = max( inValue, outValue ), min 1 for display
74 nodes.forEach( n => {
75 const nd = nMap[n.id];
76 nd.totalValue = Math.max( nd.inValue, nd.outValue, 1 );
77 } );
78
79 // For each column: distribute heights proportionally
80 const totalH = H - nodePad;
81 const nodePos = {}; // id → { x, y, h }
82
83 for ( let c = 0; c <= maxCol; c++ ) {
84 const colNodes = byCols[c];
85 if ( !colNodes.length ) continue;
86 const colTotal = colNodes.reduce( ( s, id ) => s + nMap[id].totalValue, 0 );
87 const available = totalH - ( colNodes.length - 1 ) * nodePad;
88 let y = nodePad / 2;
89 colNodes.forEach( id => {
90 const h = Math.max( 6, ( nMap[id].totalValue / colTotal ) * available );
91 nodePos[id] = { x: colX[c], y, h };
92 y += h + nodePad;
93 } );
94 }
95
96 // ── Build SVG nodes list ──────────────────────────────────────────────
97 const svgNodes = nodes.map( ( n, i ) => {
98 const pos = nodePos[n.id];
99 if ( !pos ) return null;
100 const color = n.color || '#4f46e5';
101 const labelX = colMap[n.id] === maxCol ? pos.x - 6 : pos.x + nodeWidth + 6;
102 const anchor = colMap[n.id] === maxCol ? 'end' : 'start';
103
104 return {
105 key: 'node_' + n.id + i,
106 rect: { x: pos.x, y: pos.y, w: nodeWidth, h: pos.h, color },
107 label: { x: labelX, y: pos.y + pos.h / 2, text: n.label || n.id, color, anchor },
108 value: nMap[n.id].totalValue,
109 };
110 } ).filter( Boolean );
111
112 // ── Build SVG flow paths ──────────────────────────────────────────────
113 // Track how much of each node's height is consumed by flows
114 const nodeOutY = {};
115 const nodeInY = {};
116 nodes.forEach( n => {
117 const pos = nodePos[n.id];
118 if ( pos ) { nodeOutY[n.id] = pos.y; nodeInY[n.id] = pos.y; }
119 } );
120
121 // Total in/out per node
122 const totalOut = {};
123 const totalIn = {};
124 links.forEach( lk => {
125 totalOut[lk.source] = ( totalOut[lk.source] || 0 ) + ( lk.value || 0 );
126 totalIn[lk.target] = ( totalIn[lk.target] || 0 ) + ( lk.value || 0 );
127 } );
128
129 const svgLinks = links.map( ( lk, i ) => {
130 const src = nodePos[lk.source];
131 const tgt = nodePos[lk.target];
132 if ( !src || !tgt ) return null;
133
134 const srcNode = nMap[lk.source];
135 const tgtNode = nMap[lk.target];
136
137 const srcH = src.h * ( ( lk.value || 0 ) / Math.max( totalOut[lk.source] || 1, 1 ) );
138 const tgtH = src.h * ( ( lk.value || 0 ) / Math.max( totalIn[lk.target] || 1, 1 ) );
139
140 const sy0 = nodeOutY[lk.source] || src.y;
141 const sy1 = sy0 + srcH;
142 const ty0 = nodeInY[lk.target] || tgt.y;
143 const ty1 = ty0 + tgtH;
144
145 nodeOutY[lk.source] = sy1;
146 nodeInY[lk.target] = ty1;
147
148 const x0 = src.x + nodeWidth;
149 const x1 = tgt.x;
150 const midX = ( x0 + x1 ) / 2;
151
152 const d = `M ${ x0 } ${ sy0 } C ${ midX } ${ sy0 }, ${ midX } ${ ty0 }, ${ x1 } ${ ty0 } L ${ x1 } ${ ty1 } C ${ midX } ${ ty1 }, ${ midX } ${ sy1 }, ${ x0 } ${ sy1 } Z`;
153
154 return {
155 key: 'link_' + lk.source + '_' + lk.target + '_' + i,
156 d,
157 color: srcNode.color || '#4f46e5',
158 value: lk.value,
159 };
160 } ).filter( Boolean );
161
162 return { svgNodes, svgLinks };
163 }
164
165 // ── Render ────────────────────────────────────────────────────────────────
166 function renderSankey( a ) {
167 const W = a.svgWidth, H = a.svgHeight;
168 const { svgNodes, svgLinks } = buildSankey( a.nodes, a.links, W, H, a.nodeWidth, a.nodePad );
169 const op = ( a.flowOpacity || 40 ) / 100;
170
171 const svgEls = [];
172 svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) );
173
174 // Links (behind nodes)
175 svgLinks.forEach( lk => {
176 svgEls.push( el( 'path', { key: lk.key, d: lk.d, fill: lk.color, opacity: op } ) );
177 } );
178
179 // Nodes
180 svgNodes.forEach( n => {
181 svgEls.push( el( 'rect', { key: n.key + 'r', x: n.rect.x, y: n.rect.y, width: n.rect.w, height: n.rect.h, fill: n.rect.color, rx: 3 } ) );
182 svgEls.push( el( 'text', {
183 key: n.key + 'l', x: n.label.x, y: n.label.y,
184 textAnchor: n.label.anchor, dominantBaseline: 'middle',
185 fill: a.textColor || '#374151', fontSize: a.labelFontSize, fontWeight: a.labelFontWeight, fontFamily: 'inherit',
186 }, n.label.text ) );
187 if ( a.showValues ) {
188 svgEls.push( el( 'text', {
189 key: n.key + 'v', x: n.label.x, y: n.label.y + a.labelFontSize + 2,
190 textAnchor: n.label.anchor, dominantBaseline: 'middle',
191 fill: n.rect.color, fontSize: a.valueFontSize, fontFamily: 'inherit', opacity: 0.85,
192 }, String( n.value ) ) );
193 }
194 } );
195
196 return el( 'svg', {
197 viewBox: `0 0 ${ W } ${ H }`, width: '100%',
198 style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' }
199 }, ...svgEls );
200 }
201
202 // ── Helpers ───────────────────────────────────────────────────────────────
203 let _nid = 100;
204 function nid() { return 'n' + ( ++_nid ); }
205
206 // ── Block ───────────────────────────────────────────────────────────────── /* ── colour-swatch + popover ── */
207 function BkbgColorSwatch(p) {
208 var st = useState(false), open = st[0], setOpen = st[1];
209 return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } },
210 el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label),
211 el('div', { style:{ position:'relative', flexShrink:0 } },
212 el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); },
213 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 } }),
214 open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } },
215 el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } },
216 el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } },
217 el('strong', { style:{ fontSize:'12px' } }, p.label),
218 el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } })
219 ),
220 el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange })
221 )
222 )
223 )
224 );
225 }
226 registerBlockType( 'blockenberg/sankey-diagram', {
227 edit: function ( props ) {
228 const { attributes: a, setAttributes } = props;
229 const blockProps = useBlockProps((function () {
230 var _tv = getTypoCssVars();
231 var s = {};
232 if (_tv) {
233 Object.assign(s, _tv(a.titleTypo, '--bksk-tt-'));
234 }
235 return { className: 'bkbg-sankey-wrap', style: s };
236 })());
237
238 const nodeOptions = [
239 { label: __( '— Select —', 'blockenberg' ), value: '' },
240 ...a.nodes.map( n => ( { label: n.label || n.id, value: n.id } ) ),
241 ];
242
243 function updNode( idx, field, val ) {
244 setAttributes( { nodes: a.nodes.map( ( n, i ) => i === idx ? { ...n, [field]: val } : n ) } );
245 }
246 function updLink( idx, field, val ) {
247 setAttributes( { links: a.links.map( ( lk, i ) => i === idx ? { ...lk, [field]: val } : lk ) } );
248 }
249
250 return el( 'div', blockProps,
251 el( InspectorControls, {},
252
253 el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true },
254 el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ),
255 el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ),
256 el( ToggleControl, { label: __( 'Show Values', 'blockenberg' ), checked: a.showValues, onChange: v => setAttributes( { showValues: v } ) } ),
257 el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 50 } ),
258 el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 200, max: 900, step: 50 } ),
259 el( RangeControl, { label: __( 'Node Width', 'blockenberg' ), value: a.nodeWidth, onChange: v => setAttributes( { nodeWidth: v } ), min: 8, max: 60 } ),
260 el( RangeControl, { label: __( 'Node Padding', 'blockenberg' ), value: a.nodePad, onChange: v => setAttributes( { nodePad: v } ), min: 4, max: 60 } ),
261 el( RangeControl, { label: __( 'Flow Opacity %', 'blockenberg' ), value: a.flowOpacity, onChange: v => setAttributes( { flowOpacity: v } ), min: 5, max: 95 } ),
262 ),
263
264
265 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
266 getTypoControl() && el( getTypoControl(), { label: __('Title Typography'), value: a.titleTypo || {}, onChange: function(v){ setAttributes({ titleTypo: v }); } }),
267 el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 22, __nextHasNoMarginBottom: true } ),
268 el( RangeControl, { label: __( 'Label Font Weight', 'blockenberg' ), value: a.labelFontWeight, onChange: v => setAttributes( { labelFontWeight: v } ), min: 100, max: 900, step: 100, __nextHasNoMarginBottom: true } ),
269 el( RangeControl, { label: __( 'Label Line Height', 'blockenberg' ), value: a.labelLineHeight, onChange: v => setAttributes( { labelLineHeight: v } ), min: 1, max: 3, step: 0.1, __nextHasNoMarginBottom: true } ),
270 el( RangeControl, { label: __( 'Value Font Size', 'blockenberg' ), value: a.valueFontSize, onChange: v => setAttributes( { valueFontSize: v } ), min: 8, max: 18, __nextHasNoMarginBottom: true } ),
271 el( RangeControl, { label: __( 'Value Font Weight', 'blockenberg' ), value: a.valueFontWeight, onChange: v => setAttributes( { valueFontWeight: v } ), min: 100, max: 900, step: 100, __nextHasNoMarginBottom: true } ),
272 el( RangeControl, { label: __( 'Value Line Height', 'blockenberg' ), value: a.valueLineHeight, onChange: v => setAttributes( { valueLineHeight: v } ), min: 1, max: 3, step: 0.1, __nextHasNoMarginBottom: true } )
273 ),
274 el( PanelColorSettings, {
275 title: __( 'Colors', 'blockenberg' ), initialOpen: false,
276 colorSettings: [
277 { label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) },
278 { label: __( 'Title Color', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) },
279 { label: __( 'Text Color', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) },
280 ]
281 } ),
282
283 el( PanelBody, { title: __( 'Nodes', 'blockenberg' ), initialOpen: false },
284 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { nodes: [ ...a.nodes, { id: nid(), label: 'New Node', color: '#6b7280' } ] } ) }, __( '+ Add Node', 'blockenberg' ) ),
285 a.nodes.map( ( n, i ) =>
286 el( PanelBody, { key: n.id, title: n.label || `Node ${ i + 1 }`, initialOpen: false },
287 el( TextControl, { label: __( 'ID (unique)', 'blockenberg' ), value: n.id, onChange: v => updNode( i, 'id', v.replace( /\s/g, '-' ) ) } ),
288 el( TextControl, { label: __( 'Label', 'blockenberg' ), value: n.label, onChange: v => updNode( i, 'label', v ) } ),
289 el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: n.color, onChange: v => updNode( i, 'color', v ) } ),
290 el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { nodes: a.nodes.filter( ( _, x ) => x !== i ) } ) }, __( 'Remove Node', 'blockenberg' ) ),
291 )
292 ),
293 ),
294
295 el( PanelBody, { title: __( 'Flows (Links)', 'blockenberg' ), initialOpen: false },
296 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { links: [ ...a.links, { source: '', target: '', value: 100 } ] } ) }, __( '+ Add Flow', 'blockenberg' ) ),
297 a.links.map( ( lk, i ) =>
298 el( PanelBody, { key: i, title: `${ lk.source || '?' } → ${ lk.target || '?' } (${ lk.value })`, initialOpen: false },
299 el( SelectControl, { label: __( 'From (source)', 'blockenberg' ), value: lk.source, options: nodeOptions, onChange: v => updLink( i, 'source', v ) } ),
300 el( SelectControl, { label: __( 'To (target)', 'blockenberg' ), value: lk.target, options: nodeOptions, onChange: v => updLink( i, 'target', v ) } ),
301 el( RangeControl, { label: __( 'Flow Value', 'blockenberg' ), value: lk.value, onChange: v => updLink( i, 'value', v ), min: 1, max: 100000, step: 1 } ),
302 el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { links: a.links.filter( ( _, x ) => x !== i ) } ) }, __( 'Remove Flow', 'blockenberg' ) ),
303 )
304 ),
305 ),
306 ),
307
308 a.showTitle && a.title && el( 'h3', { className: 'bkbg-sk-title', style: { color: a.titleColor } }, a.title ),
309 el( 'div', { className: 'bkbg-sk-svg' }, renderSankey( a ) ),
310 );
311 },
312
313 deprecated: [{
314 save: function ( { attributes: a } ) {
315 const blockProps = useBlockProps.save( { className: 'bkbg-sankey-wrap' } );
316 return el( 'div', blockProps,
317 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-sk-title', style: { color: a.titleColor } }, a.title ) : null,
318 el( 'div', { className: 'bkbg-sk-svg' }, renderSankey( a ) ),
319 );
320 },
321 }],
322
323 save: function ( { attributes: a } ) {
324 var _tv = getTypoCssVars();
325 var s = {};
326 if (_tv) {
327 Object.assign(s, _tv(a.titleTypo, '--bksk-tt-'));
328 }
329 const blockProps = useBlockProps.save( { className: 'bkbg-sankey-wrap', style: s } );
330 return el( 'div', blockProps,
331 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-sk-title', style: { color: a.titleColor } }, a.title ) : null,
332 el( 'div', { className: 'bkbg-sk-svg' }, renderSankey( a ) ),
333 );
334 },
335 } );
336 }() );
337