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 / treemap-chart / index.js

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

258 lines 14.9 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 _tc, _tvf;
10 Object.defineProperty(window, '_bkbgTypoCtrlCache', { get: function () { if (!_tc) { _tc = window.bkbgTypographyControl; } return _tc; } });
11 Object.defineProperty(window, '_bkbgTypoVarsCache', { get: function () { if (!_tvf) { _tvf = window.bkbgTypoCssVars; } return _tvf; } });
12 function getTypoControl(props, attrName, label) { return window._bkbgTypoCtrlCache(props, attrName, label); }
13
14 // ── Binary partition treemap algorithm ───────────────────────────────────
15 // Recursively splits the rectangle between items based on value ratio.
16 // Alternates split direction (horizontal → vertical) based on aspect ratio.
17 function partition( items, x, y, w, h ) {
18 if ( !items || items.length === 0 ) return [];
19 if ( items.length === 1 ) return [ { ...items[0], x, y, w, h } ];
20
21 const total = items.reduce( ( s, it ) => s + ( it.value || 0 ), 0 );
22 if ( total === 0 ) return [];
23
24 // Find balanced split index (left total ≥ right total)
25 let cumul = 0, splitIdx = Math.floor( items.length / 2 );
26 for ( let i = 0; i < items.length - 1; i++ ) {
27 cumul += ( items[i].value || 0 );
28 if ( cumul * 2 >= total ) { splitIdx = i + 1; break; }
29 }
30
31 const leftItems = items.slice( 0, splitIdx );
32 const rightItems = items.slice( splitIdx );
33 const leftTotal = leftItems.reduce( ( s, it ) => s + ( it.value || 0 ), 0 );
34 const ratio = leftTotal / total;
35
36 if ( w >= h ) {
37 // Split horizontally
38 const leftW = w * ratio;
39 return [
40 ...partition( leftItems, x, y, leftW, h ),
41 ...partition( rightItems, x + leftW, y, w - leftW, h ),
42 ];
43 } else {
44 // Split vertically
45 const topH = h * ratio;
46 return [
47 ...partition( leftItems, x, y, w, topH ),
48 ...partition( rightItems, x, y + topH, w, h - topH ),
49 ];
50 }
51 }
52
53 // ── Apply gap inset to a cell ─────────────────────────────────────────────
54 function inset( cell, gap ) {
55 const half = gap / 2;
56 return {
57 x: cell.x + half, y: cell.y + half,
58 w: Math.max( 0, cell.w - gap ),
59 h: Math.max( 0, cell.h - gap ),
60 };
61 }
62
63 // ── Helper to check if text fits in a cell ───────────────────────────────
64 function fits( str, cell, fontSize ) {
65 const charW = fontSize * 0.65;
66 return cell.w >= str.length * charW && cell.h >= fontSize * 2;
67 }
68
69 // ── Render treemap SVG ───────────────────────────────────────────────────
70 function renderTreemap( a ) {
71 const W = a.svgWidth, H = a.svgHeight;
72 const sorted = [ ...( a.items || [] ) ].sort( ( a, b ) => ( b.value || 0 ) - ( a.value || 0 ) );
73 const cells = partition( sorted, 0, 0, W, H );
74 const total = sorted.reduce( ( s, it ) => s + ( it.value || 0 ), 0 );
75
76 const svgEls = [];
77
78 // Background
79 svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 6 } ) );
80
81 cells.forEach( ( cell, i ) => {
82 const pad = inset( cell, a.gap );
83 const rx = a.cornerRadius;
84 const pct = total > 0 ? ( ( cell.value / total ) * 100 ).toFixed( 1 ) : '0';
85 const labelVisible = a.showLabels && fits( cell.label || '', pad, a.labelFontSize );
86 const valueVisible = a.showValues && pad.h >= a.labelFontSize * 2.5 && pad.w > 24;
87
88 // Cell background
89 svgEls.push( el( 'rect', {
90 key: 'r' + i,
91 x: pad.x, y: pad.y, width: pad.w, height: pad.h,
92 fill: cell.color || '#4f46e5', rx,
93 } ) );
94
95 // Overlay for text contrast
96 svgEls.push( el( 'rect', {
97 key: 'ov' + i,
98 x: pad.x, y: pad.y, width: pad.w, height: pad.h,
99 fill: 'rgba(0,0,0,0.12)', rx,
100 } ) );
101
102 if ( labelVisible ) {
103 const midX = pad.x + pad.w / 2;
104 const midY = pad.y + pad.h / 2;
105 const offset = valueVisible ? -( a.labelFontSize * 0.7 ) : 0;
106
107 svgEls.push( el( 'text', {
108 key: 'lbl' + i,
109 x: midX, y: midY + offset,
110 textAnchor: 'middle', dominantBaseline: 'middle',
111 fill: '#ffffff', fontSize: a.labelFontSize, fontWeight: 700,
112 fontFamily: 'inherit',
113 }, cell.label || '' ) );
114 }
115
116 if ( valueVisible ) {
117 const midX = pad.x + pad.w / 2;
118 const midY = pad.y + pad.h / 2;
119 const offset = labelVisible ? a.valueFontSize * 0.9 : 0;
120 const text = a.showPercent ? pct + '%' : ( cell.value || 0 );
121
122 svgEls.push( el( 'text', {
123 key: 'val' + i,
124 x: midX, y: midY + offset,
125 textAnchor: 'middle', dominantBaseline: 'middle',
126 fill: 'rgba(255,255,255,0.85)', fontSize: a.valueFontSize,
127 fontFamily: 'inherit',
128 }, String( text ) ) );
129 }
130 } );
131
132 return el( 'svg', {
133 viewBox: `0 0 ${ W } ${ H }`, width: '100%',
134 style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' }
135 }, ...svgEls );
136 }
137
138 // ── Legend row ────────────────────────────────────────────────────────────
139 function renderLegend( a ) {
140 const total = ( a.items || [] ).reduce( ( s, it ) => s + ( it.value || 0 ), 0 );
141 const sorted = [ ...( a.items || [] ) ].sort( ( a, b ) => ( b.value || 0 ) - ( a.value || 0 ) );
142 return el( 'div', { className: 'bkbg-tm-legend' },
143 sorted.map( ( item, i ) => {
144 const pct = total > 0 ? ( ( item.value / total ) * 100 ).toFixed( 1 ) : '0';
145 return el( 'div', { key: i, className: 'bkbg-tm-legend-item' },
146 el( 'span', { className: 'bkbg-tm-swatch', style: { background: item.color } } ),
147 el( 'span', { className: 'bkbg-tm-legend-label' }, item.label || '' ),
148 el( 'span', { className: 'bkbg-tm-legend-val' }, `${ item.value } (${ pct }%)` ),
149 );
150 } )
151 );
152 }
153
154 // ── Block registration ──────────────────────────────────────────────────── /* ── colour-swatch + popover ── */
155 function BkbgColorSwatch(p) {
156 var st = useState(false), open = st[0], setOpen = st[1];
157 return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } },
158 el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label),
159 el('div', { style:{ position:'relative', flexShrink:0 } },
160 el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); },
161 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 } }),
162 open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } },
163 el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } },
164 el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } },
165 el('strong', { style:{ fontSize:'12px' } }, p.label),
166 el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } })
167 ),
168 el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange })
169 )
170 )
171 )
172 );
173 }
174 registerBlockType( 'blockenberg/treemap-chart', {
175 edit: function ( props ) {
176 const { attributes: a, setAttributes } = props;
177 const blockProps = ( function () {
178 var s = {};
179 Object.assign( s, window._bkbgTypoVarsCache( a.titleTypo, '--bktmc-tt-' ) );
180 return useBlockProps( { className: 'bkbg-treemap-wrap', style: Object.keys( s ).length ? s : undefined } );
181 } )();
182
183 function updItem( idx, field, val ) {
184 const next = a.items.map( ( it, i ) => i === idx ? { ...it, [field]: val } : it );
185 setAttributes( { items: next } );
186 }
187
188 return el( 'div', blockProps,
189 el( InspectorControls, {},
190
191 el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true },
192 el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ),
193 el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ),
194 el( ToggleControl, { label: __( 'Show Labels', 'blockenberg' ), checked: a.showLabels, onChange: v => setAttributes( { showLabels: v } ) } ),
195 el( ToggleControl, { label: __( 'Show Values', 'blockenberg' ), checked: a.showValues, onChange: v => setAttributes( { showValues: v } ) } ),
196 el( ToggleControl, { label: __( 'Show as Percent', 'blockenberg' ), checked: a.showPercent, onChange: v => setAttributes( { showPercent: v } ) } ),
197 ),
198
199 el( PanelBody, { title: __( 'Canvas & Style', 'blockenberg' ), initialOpen: false },
200 el( RangeControl, { label: __( 'Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 50 } ),
201 el( RangeControl, { label: __( 'Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 200, max: 800, step: 50 } ),
202 el( RangeControl, { label: __( 'Cell Gap', 'blockenberg' ), value: a.gap, onChange: v => setAttributes( { gap: v } ), min: 0, max: 16 } ),
203 el( RangeControl, { label: __( 'Corner Radius', 'blockenberg' ), value: a.cornerRadius, onChange: v => setAttributes( { cornerRadius: v } ), min: 0, max: 20 } ),
204 ),
205
206
207 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
208 getTypoControl( props, 'titleTypo', __( 'Title', 'blockenberg' ) ),
209 el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 24 } ),
210 el( RangeControl, { label: __( 'Value Font Size', 'blockenberg' ), value: a.valueFontSize, onChange: v => setAttributes( { valueFontSize: v } ), min: 8, max: 20 } )
211 ),
212 el( PanelColorSettings, {
213 title: __( 'Colors', 'blockenberg' ),
214 initialOpen: false,
215 colorSettings: [
216 { label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) },
217 { label: __( 'Title Color', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) },
218 ]
219 } ),
220
221 el( PanelBody, { title: __( 'Data Items', 'blockenberg' ), initialOpen: false },
222 el( Button, {
223 variant: 'secondary', style: { marginBottom: 10 },
224 onClick: () => setAttributes( { items: [ ...a.items, { label: 'New Item', value: 5, color: '#6b7280' } ] } )
225 }, __( '+ Add Item', 'blockenberg' ) ),
226
227 a.items.map( ( item, i ) =>
228 el( PanelBody, { key: i, title: ( item.label || `Item ${ i + 1 }` ) + ` (${ item.value })`, initialOpen: false },
229 el( TextControl, { label: __( 'Label', 'blockenberg' ), value: item.label, onChange: v => updItem( i, 'label', v ) } ),
230 el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: item.color, onChange: v => updItem( i, 'color', v ) } ),
231 el( RangeControl, { label: __( 'Value', 'blockenberg' ), value: item.value, onChange: v => updItem( i, 'value', v ), min: 1, max: 1000 } ),
232 el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { items: a.items.filter( ( _, x ) => x !== i ) } ) }, __( 'Remove', 'blockenberg' ) ),
233 )
234 ),
235 ),
236 ),
237
238 a.showTitle && a.title && el( 'h3', { className: 'bkbg-tm-title', style: { color: a.titleColor } }, a.title ),
239 el( 'div', { className: 'bkbg-tm-svg' }, renderTreemap( a ) ),
240 renderLegend( a ),
241 );
242 },
243
244 save: function ( { attributes: a } ) {
245 const blockProps = ( function () {
246 var tv = window._bkbgTypoVarsCache( a.titleTypo, '--bktmc-tt-' );
247 var parts = []; Object.keys( tv ).forEach( function ( k ) { parts.push( k + ':' + tv[k] ); } );
248 return useBlockProps.save( { className: 'bkbg-treemap-wrap', style: parts.length ? parts.join( ';' ) : undefined } );
249 } )();
250 return el( 'div', blockProps,
251 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-tm-title', style: { color: a.titleColor } }, a.title ) : null,
252 el( 'div', { className: 'bkbg-tm-svg' }, renderTreemap( a ) ),
253 renderLegend( a ),
254 );
255 },
256 } );
257 }() );
258