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

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

211 lines 14.3 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 } = window.wp.components;
6 const { __ } = window.wp.i18n;
7
8 var _tc, _tv;
9 function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); }
10 function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); }
11
12 // ── Renderer ──────────────────────────────────────────────────────────────
13 function renderNetwork( a ) {
14 const nodes = a.nodes || [];
15 const links = a.links || [];
16 const W = a.svgWidth;
17 const H = a.svgHeight;
18 const R = a.nodeRadius;
19 const AZ = a.arrowSize;
20
21 const nodeMap = {};
22 nodes.forEach( n => { nodeMap[ n.id ] = n; } );
23
24 const svgEls = [];
25 svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor, rx: 10 } ) );
26
27 // Arrow marker definition
28 if ( a.showArrows ) {
29 svgEls.push( el( 'defs', { key: 'defs' },
30 el( 'marker', { id: 'bkbg-nd-arrow', markerWidth: AZ, markerHeight: AZ, refX: R + AZ * a.linkWidth * 0.5, refY: AZ / 2, orient: 'auto', markerUnits: 'userSpaceOnUse' },
31 el( 'polygon', { points: `0 0, ${ AZ } ${ AZ / 2 }, 0 ${ AZ }`, fill: a.linkColor } )
32 )
33 ) );
34 }
35
36 // Links
37 links.forEach( ( lnk, li ) => {
38 const src = nodeMap[ lnk.from ];
39 const tgt = nodeMap[ lnk.to ];
40 if ( ! src || ! tgt ) return;
41 const dx = tgt.x - src.x;
42 const dy = tgt.y - src.y;
43 const dist = Math.sqrt( dx * dx + dy * dy ) || 1;
44 const ux = dx / dist;
45 const uy = dy / dist;
46 const x1 = src.x + ux * R;
47 const y1 = src.y + uy * R;
48 const x2 = tgt.x - ux * R;
49 const y2 = tgt.y - uy * R;
50
51 svgEls.push( el( 'line', {
52 key: `lnk${ li }`,
53 x1, y1, x2, y2,
54 stroke: a.linkColor,
55 strokeWidth: a.linkWidth,
56 markerEnd: a.showArrows ? 'url(#bkbg-nd-arrow)' : undefined,
57 } ) );
58
59 // Edge label
60 if ( lnk.label ) {
61 svgEls.push( el( 'text', { key: `le${ li }`, x: ( x1 + x2 ) / 2, y: ( y1 + y2 ) / 2 - 5, textAnchor: 'middle', fill: '#64748b', fontSize: a.labelFontSize - 1, fontFamily: 'inherit' }, lnk.label ) );
62 }
63 } );
64
65 // Nodes
66 nodes.forEach( ( nd, ni ) => {
67 // Drop shadow
68 svgEls.push( el( 'circle', { key: `sh${ ni }`, cx: nd.x + 2, cy: nd.y + 3, r: R, fill: 'rgba(0,0,0,0.12)' } ) );
69 svgEls.push( el( 'circle', { key: `nd${ ni }`, cx: nd.x, cy: nd.y, r: R, fill: nd.color || '#6366f1', stroke: '#fff', strokeWidth: a.nodeStroke } ) );
70
71 if ( a.showLabels ) {
72 // Wrap long labels
73 const words = ( nd.label || '' ).split( ' ' );
74 if ( words.length > 1 ) {
75 svgEls.push( el( 'text', { key: `nl${ ni }`, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontFamily: 'inherit', fontWeight: a.labelFontWeight || 600 },
76 el( 'tspan', { x: nd.x, y: nd.y - a.labelFontSize * 0.5, dy: '0' }, words[0] ),
77 el( 'tspan', { x: nd.x, dy: a.labelFontSize + 2 }, words.slice( 1 ).join( ' ' ) ),
78 ) );
79 } else {
80 svgEls.push( el( 'text', { key: `nl${ ni }`, x: nd.x, y: nd.y, textAnchor: 'middle', dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontFamily: 'inherit', fontWeight: a.labelFontWeight || 600 }, nd.label ) );
81 }
82 }
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 updNode( setAttributes, nodes, ni, field, val ) {
89 setAttributes( { nodes: nodes.map( ( n, i ) => i === ni ? { ...n, [field]: val } : n ) } );
90 }
91 function updLink( setAttributes, links, li, field, val ) {
92 setAttributes( { links: links.map( ( l, i ) => i === li ? { ...l, [field]: val } : l ) } );
93 }
94
95 registerBlockType( 'blockenberg/network-diagram', {
96 edit: function ( props ) {
97 const { attributes: a, setAttributes } = props;
98 const blockProps = useBlockProps((function () {
99 var _tvf = getTypoCssVars();
100 var s = {};
101 if (_tvf) { Object.assign(s, _tvf(a.titleTypo, '--bkbg-nd-tt-')); }
102 return { className: 'bkbg-network-wrap', style: s };
103 })());
104
105 return el( 'div', blockProps,
106 el( InspectorControls, {},
107
108 el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true },
109 el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ),
110 el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ),
111 el( ToggleControl, { label: __( 'Show Labels', 'blockenberg' ), checked: a.showLabels, onChange: v => setAttributes( { showLabels: v } ) } ),
112 el( ToggleControl, { label: __( 'Show Arrows', 'blockenberg' ), checked: a.showArrows, onChange: v => setAttributes( { showArrows: v } ) } ),
113 ),
114
115 el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false },
116 el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ),
117 el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 200, max: 800 } ),
118 el( RangeControl, { label: __( 'Node Radius', 'blockenberg' ), value: a.nodeRadius, onChange: v => setAttributes( { nodeRadius: v } ), min: 12, max: 60 } ),
119 el( RangeControl, { label: __( 'Node Border', 'blockenberg' ), value: a.nodeStroke, onChange: v => setAttributes( { nodeStroke: v } ), min: 1, max: 6 } ),
120 el( RangeControl, { label: __( 'Link Width', 'blockenberg' ), value: a.linkWidth, onChange: v => setAttributes( { linkWidth: v } ), min: 1, max: 8 } ),
121 el( RangeControl, { label: __( 'Arrow Size', 'blockenberg' ), value: a.arrowSize, onChange: v => setAttributes( { arrowSize: v } ), min: 5, max: 24 } ),
122 ),
123
124
125 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
126 getTypoControl() && el(getTypoControl(), { label: __('Title Typography', 'blockenberg'), value: a.titleTypo, onChange: v => setAttributes({ titleTypo: v }) }),
127 el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 20, __nextHasNoMarginBottom: true } ),
128 el( RangeControl, { label: __( 'Label Font Weight', 'blockenberg' ), value: a.labelFontWeight || 600, onChange: v => setAttributes( { labelFontWeight: v } ), min: 300, max: 900, step: 100, __nextHasNoMarginBottom: true } )
129 ),
130 el( PanelColorSettings, {
131 title: __( 'Colors', 'blockenberg' ), initialOpen: false,
132 colorSettings: [
133 { label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#f8fafc' } ) },
134 { label: __( 'Link Color', 'blockenberg' ), value: a.linkColor, onChange: v => setAttributes( { linkColor: v || '#cbd5e1' } ) },
135 { label: __( 'Label Text', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#ffffff' } ) },
136 { label: __( 'Title', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) },
137 ]
138 } ),
139
140 el( PanelBody, { title: __( 'Nodes', 'blockenberg' ), initialOpen: false },
141 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => {
142 const ids = ( a.nodes || [] ).map( n => parseInt( n.id.replace( 'n', '' ) ) || 0 );
143 const nextId = 'n' + ( Math.max( 0, ...ids ) + 1 );
144 setAttributes( { nodes: [ ...( a.nodes || [] ), { id: nextId, label: 'New Node', color: '#6366f1', x: 200, y: 200 } ] } );
145 } }, __( '+ Add Node', 'blockenberg' ) ),
146 a.nodes.map( ( nd, ni ) =>
147 el( PanelBody, { key: ni, title: `${ nd.label } (${ nd.id })`, initialOpen: false },
148 el( TextControl, { label: __( 'Label', 'blockenberg' ), value: nd.label, onChange: v => updNode( setAttributes, a.nodes, ni, 'label', v ) } ),
149 el( TextControl, { label: __( 'ID', 'blockenberg' ), value: nd.id, onChange: v => updNode( setAttributes, a.nodes, ni, 'id', v ) } ),
150 el( 'div', { style: { display: 'flex', gap: 8, alignItems: 'center', marginBottom: 8 } },
151 el( 'label', { style: { fontSize: 12 } }, __( 'Color', 'blockenberg' ) ),
152 el( 'input', { type: 'color', value: nd.color, style: { width: 40, height: 28, border: 'none', borderRadius: 4, cursor: 'pointer' }, onChange: e => updNode( setAttributes, a.nodes, ni, 'color', e.target.value ) } ),
153 ),
154 el( 'div', { style: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8, marginBottom: 8 } },
155 el( TextControl, { label: 'X', value: String( nd.x ), type: 'number', onChange: v => updNode( setAttributes, a.nodes, ni, 'x', parseFloat( v ) || 0 ) } ),
156 el( TextControl, { label: 'Y', value: String( nd.y ), type: 'number', onChange: v => updNode( setAttributes, a.nodes, ni, 'y', parseFloat( v ) || 0 ) } ),
157 ),
158 el( Button, { isDestructive: true, isSmall: true, onClick: () => {
159 const filtered = a.nodes.filter( ( _, x ) => x !== ni );
160 setAttributes( { nodes: filtered, links: a.links.filter( l => l.from !== nd.id && l.to !== nd.id ) } );
161 } }, __( 'Remove Node', 'blockenberg' ) ),
162 )
163 ),
164 ),
165
166 el( PanelBody, { title: __( 'Links', 'blockenberg' ), initialOpen: false },
167 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { links: [ ...( a.links || [] ), { from: '', to: '', label: '' } ] } ) }, __( '+ Add Link', 'blockenberg' ) ),
168 a.links.map( ( lnk, li ) =>
169 el( 'div', { key: li, style: { display: 'flex', gap: 6, alignItems: 'flex-end', marginBottom: 6, flexWrap: 'wrap' } },
170 el( TextControl, { label: li === 0 ? 'From ID' : undefined, value: lnk.from, onChange: v => updLink( setAttributes, a.links, li, 'from', v ), style: { flex: 1, minWidth: 80 } } ),
171 el( TextControl, { label: li === 0 ? 'To ID' : undefined, value: lnk.to, onChange: v => updLink( setAttributes, a.links, li, 'to', v ), style: { flex: 1, minWidth: 80 } } ),
172 el( TextControl, { label: li === 0 ? 'Label' : undefined, value: lnk.label || '', onChange: v => updLink( setAttributes, a.links, li, 'label', v ), style: { flex: 1, minWidth: 60 } } ),
173 el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { links: a.links.filter( ( _, x ) => x !== li ) } ) }, '' ),
174 )
175 ),
176 ),
177 ),
178
179 a.showTitle && a.title && el( 'h3', { className: 'bkbg-network-title', style: { color: a.titleColor } }, a.title ),
180 el( 'div', { className: 'bkbg-network-svg' }, renderNetwork( a ) ),
181 );
182 },
183
184 deprecated: [
185 {
186 attributes: wp.blocks.getBlockType('blockenberg/network-diagram') ? wp.blocks.getBlockType('blockenberg/network-diagram').attributes : {},
187 save: function ( { attributes: a } ) {
188 const blockProps = useBlockProps.save( { className: 'bkbg-network-wrap' } );
189 return el( 'div', blockProps,
190 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-network-title', style: { color: a.titleColor, fontSize: ( a.titleFontSize || 20 ) + 'px', fontWeight: a.titleFontWeight || 700 } }, a.title ) : null,
191 el( 'div', { className: 'bkbg-network-svg' }, renderNetwork( a ) ),
192 );
193 }
194 }
195 ],
196
197 save: function ( { attributes: a } ) {
198 const blockProps = useBlockProps.save((function () {
199 var _tvf = getTypoCssVars();
200 var s = {};
201 if (_tvf) { Object.assign(s, _tvf(a.titleTypo, '--bkbg-nd-tt-')); }
202 return { className: 'bkbg-network-wrap', style: s };
203 })());
204 return el( 'div', blockProps,
205 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-network-title', style: { color: a.titleColor } }, a.title ) : null,
206 el( 'div', { className: 'bkbg-network-svg' }, renderNetwork( a ) ),
207 );
208 },
209 } );
210 }() );
211