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

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

241 lines 16.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, SelectControl, ToggleControl, TextControl, Button, ColorPicker, Popover } = window.wp.components;
6 const { __ } = window.wp.i18n;
7 const { useState } = window.wp.element;
8
9 var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); }
10 var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); }
11
12 // ── Arc path helpers ──────────────────────────────────────────────────────
13 function polarXY( cx, cy, r, angle ) {
14 return [ cx + r * Math.cos( angle ), cy + r * Math.sin( angle ) ];
15 }
16
17 function arcPath( cx, cy, r1, r2, startAngle, endAngle ) {
18 const large = endAngle - startAngle > Math.PI ? 1 : 0;
19 const [ x1, y1 ] = polarXY( cx, cy, r2, startAngle );
20 const [ x2, y2 ] = polarXY( cx, cy, r2, endAngle );
21 const [ x3, y3 ] = polarXY( cx, cy, r1, endAngle );
22 const [ x4, y4 ] = polarXY( cx, cy, r1, startAngle );
23 const f = v => v.toFixed( 3 );
24 return `M ${ f(x1) } ${ f(y1) } A ${ r2 } ${ r2 } 0 ${ large } 1 ${ f(x2) } ${ f(y2) } L ${ f(x3) } ${ f(y3) } A ${ r1 } ${ r1 } 0 ${ large } 0 ${ f(x4) } ${ f(y4) } Z`;
25 }
26
27 // Lighten a hex colour (percentage 0-1)
28 function lighten( hex, amount ) {
29 const num = parseInt( ( hex || '#6366f1' ).replace( '#', '' ), 16 );
30 const r = Math.min( 255, ( ( num >> 16 ) & 0xff ) + Math.round( 255 * amount ) );
31 const g = Math.min( 255, ( ( num >> 8 ) & 0xff ) + Math.round( 255 * amount ) );
32 const b = Math.min( 255, ( num & 0xff ) + Math.round( 255 * amount ) );
33 return `#${ [ r, g, b ].map( v => v.toString( 16 ).padStart( 2, '0' ) ).join( '' ) }`;
34 }
35
36 // ── Renderer ──────────────────────────────────────────────────────────────
37 function renderSunburst( a ) {
38 const segs = a.segments || [];
39 const S = a.svgSize;
40 const cx = S / 2;
41 const cy = S / 2;
42 const r1 = a.innerRadius; // donut hole
43 const r2 = a.midRadius; // inner ring outer edge
44 const r3 = a.outerRadius; // outer ring outer edge
45
46 const TAU = 2 * Math.PI;
47 const START = -Math.PI / 2; // start from top
48
49 // Total of all leaf values
50 const totalVal = segs.reduce( ( s, seg ) =>
51 s + ( seg.children || [] ).reduce( ( cs, ch ) => cs + ( ch.value || 0 ), 0 ), 0 ) || 1;
52
53 // Each segment's arc span = sum of its children / total
54 const segTotals = segs.map( seg => ( seg.children || [] ).reduce( ( s, ch ) => s + ( ch.value || 0 ), 0 ) );
55 const segAngleFns = []; // will hold { start, end } per segment
56
57 let cursor = START;
58 segTotals.forEach( ( st, si ) => {
59 const span = ( st / totalVal ) * TAU;
60 segAngleFns.push( { start: cursor, end: cursor + span } );
61 cursor += span;
62 } );
63
64 const svgEls = [];
65 svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: S, height: S, fill: a.bgColor || '#ffffff', rx: 8 } ) );
66
67 // Inner ring (segments) + outer ring (children)
68 segs.forEach( ( seg, si ) => {
69 const { start: sa, end: ea } = segAngleFns[si];
70 const midA = ( sa + ea ) / 2;
71 const clr = seg.color || '#6366f1';
72 const children = seg.children || [];
73 const childTotal = segTotals[si] || 1;
74
75 // Inner ring arc
76 svgEls.push( el( 'path', { key: `inner${ si }`, d: arcPath( cx, cy, r1, r2, sa, ea ), fill: clr, stroke: '#ffffff', strokeWidth: 2 } ) );
77
78 // Inner label
79 if ( a.showLabels ) {
80 const [ lx, ly ] = polarXY( cx, cy, ( r1 + r2 ) / 2, midA );
81 svgEls.push( el( 'text', { key: `ilbl${ si }`, x: lx, y: ly, textAnchor: 'middle', dominantBaseline: 'middle', fill: '#ffffff', fontSize: a.labelFontSize, fontWeight: 700, fontFamily: 'inherit' }, seg.label || '' ) );
82 }
83
84 // Outer ring: each child
85 let childCursor = sa;
86 children.forEach( ( child, ci ) => {
87 const span = ( ( child.value || 0 ) / childTotal ) * ( ea - sa );
88 const cs = childCursor;
89 const ce = childCursor + span;
90 childCursor = ce;
91 const cMid = ( cs + ce ) / 2;
92
93 // Alternate lightened colours for children
94 const childClr = lighten( clr, ci * 0.15 );
95 svgEls.push( el( 'path', { key: `outer${ si }${ ci }`, d: arcPath( cx, cy, r2 + 4, r3, cs, ce ), fill: childClr, stroke: '#ffffff', strokeWidth: 2 } ) );
96
97 if ( a.showLabels && ( ce - cs ) > 0.15 ) {
98 const [ lx, ly ] = polarXY( cx, cy, ( r2 + 4 + r3 ) / 2, cMid );
99 svgEls.push( el( 'text', { key: `olbl${ si }${ ci }`, x: lx, y: ly - ( a.showValues ? 5 : 0 ), textAnchor: 'middle', dominantBaseline: 'middle', fill: '#ffffff', fontSize: a.labelFontSize, fontFamily: 'inherit' }, child.label || '' ) );
100 if ( a.showValues ) {
101 svgEls.push( el( 'text', { key: `oval${ si }${ ci }`, x: lx, y: ly + 8, textAnchor: 'middle', dominantBaseline: 'middle', fill: '#ffffffcc', fontSize: a.valueFontSize, fontFamily: 'inherit' }, String( child.value || 0 ) ) );
102 }
103 }
104 } );
105 } );
106
107 // Centre label — total
108 svgEls.push( el( 'text', { key: 'total', x: cx, y: cy - 8, textAnchor: 'middle', dominantBaseline: 'middle', fill: a.textColor, fontSize: 22, fontWeight: 700, fontFamily: 'inherit' }, String( totalVal ) ) );
109 svgEls.push( el( 'text', { key: 'totlbl', x: cx, y: cy + 12, textAnchor: 'middle', fill: a.textColor, fontSize: 11, fontFamily: 'inherit' }, 'Total' ) );
110
111 // Legend
112 if ( a.showLegend ) {
113 const perRow = 2;
114 const legY = S - Math.ceil( segs.length / perRow ) * 22 - 12;
115 segs.forEach( ( seg, si ) => {
116 const lx = 10 + ( si % perRow ) * ( S / perRow );
117 const ly = legY + Math.floor( si / perRow ) * 22;
118 svgEls.push( el( 'rect', { key: `lr${ si }`, x: lx, y: ly, width: 14, height: 14, fill: seg.color || '#6366f1', rx: 3 } ) );
119 svgEls.push( el( 'text', { key: `lt${ si }`, x: lx + 18, y: ly + 7, dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontFamily: 'inherit' }, `${ seg.label }${ segTotals[si] }` ) );
120 } );
121 }
122
123 return el( 'svg', { viewBox: `0 0 ${ S } ${ S }`, width: '100%', style: { display: 'block', maxWidth: S + 'px', margin: '0 auto' } }, ...svgEls );
124 }
125
126 function updSeg( setAttributes, segs, si, field, val ) {
127 setAttributes( { segments: segs.map( ( s, i ) => i === si ? { ...s, [field]: val } : s ) } );
128 }
129 function updChild( setAttributes, segs, si, ci, field, val ) {
130 setAttributes( { segments: segs.map( ( s, i ) => i !== si ? s : {
131 ...s,
132 children: ( s.children || [] ).map( ( ch, k ) => k === ci ? { ...ch, [field]: val } : ch )
133 } ) } );
134 }
135
136 /* ── colour-swatch + popover ── */
137 function BkbgColorSwatch(p) {
138 var st = useState(false), open = st[0], setOpen = st[1];
139 return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } },
140 el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label),
141 el('div', { style:{ position:'relative', flexShrink:0 } },
142 el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); },
143 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 } }),
144 open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } },
145 el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } },
146 el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } },
147 el('strong', { style:{ fontSize:'12px' } }, p.label),
148 el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } })
149 ),
150 el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange })
151 )
152 )
153 )
154 );
155 }
156
157 registerBlockType( 'blockenberg/sunburst-chart', {
158 edit: function ( props ) {
159 const { attributes: a, setAttributes } = props;
160 const blockProps = useBlockProps((function () {
161 var _tvf = getTypoCssVars();
162 var s = {};
163 if (_tvf) Object.assign(s, _tvf(a.titleTypo, '--bksbc-tt-'));
164 return { className: 'bkbg-sunburst-wrap', style: s };
165 })());
166
167 return el( 'div', blockProps,
168 el( InspectorControls, {},
169
170 el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true },
171 el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ),
172 el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ),
173 el( ToggleControl, { label: __( 'Show Labels', 'blockenberg' ), checked: a.showLabels, onChange: v => setAttributes( { showLabels: v } ) } ),
174 el( ToggleControl, { label: __( 'Show Values', 'blockenberg' ), checked: a.showValues, onChange: v => setAttributes( { showValues: v } ) } ),
175 el( ToggleControl, { label: __( 'Show Legend', 'blockenberg' ), checked: a.showLegend, onChange: v => setAttributes( { showLegend: v } ) } ),
176 ),
177
178 el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false },
179 el( RangeControl, { label: __( 'Canvas Size (px)', 'blockenberg' ), value: a.svgSize, onChange: v => setAttributes( { svgSize: v } ), min: 300, max: 800 } ),
180 el( RangeControl, { label: __( 'Donut Hole Radius', 'blockenberg' ), value: a.innerRadius, onChange: v => setAttributes( { innerRadius: v } ), min: 20, max: 200 } ),
181 el( RangeControl, { label: __( 'Inner Ring Radius', 'blockenberg' ), value: a.midRadius, onChange: v => setAttributes( { midRadius: v } ), min: 60, max: 350 } ),
182 el( RangeControl, { label: __( 'Outer Ring Radius', 'blockenberg' ), value: a.outerRadius, onChange: v => setAttributes( { outerRadius: v } ), min: 80, max: 400 } ),
183 ),
184
185
186 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
187 getTypoControl()({ label: __('Title', 'blockenberg'), value: a.titleTypo, onChange: v => setAttributes({ titleTypo: v }) }),
188 el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 7, max: 20 } ),
189 el( RangeControl, { label: __( 'Value Font Size', 'blockenberg' ), value: a.valueFontSize, onChange: v => setAttributes( { valueFontSize: v } ), min: 6, max: 16 } ),
190 el( SelectControl, { label: __( 'Label Font Weight', 'blockenberg' ), value: a.labelFontWeight, options: [{label:'400 Regular',value:'400'},{label:'500 Medium',value:'500'},{label:'600 Semi-bold',value:'600'},{label:'700 Bold',value:'700'}], __nextHasNoMarginBottom: true, onChange: v => setAttributes( { labelFontWeight: v } ) } ),
191 el( RangeControl, { label: __( 'Label Line Height', 'blockenberg' ), value: a.labelLineHeight, min: 1.0, max: 2.5, step: 0.05, __nextHasNoMarginBottom: true, onChange: v => setAttributes( { labelLineHeight: v } ) } )
192 ),
193 el( PanelColorSettings, {
194 title: __( 'Colors', 'blockenberg' ), initialOpen: false,
195 colorSettings: [
196 { label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) },
197 { label: __( 'Title Color', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) },
198 { label: __( 'Text Color', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) },
199 ]
200 } ),
201
202 el( PanelBody, { title: __( 'Segments', 'blockenberg' ), initialOpen: false },
203 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { segments: [ ...a.segments, { label: 'New Segment', color: '#6366f1', children: [ { label: 'Item 1', value: 20 } ] } ] } ) }, __( '+ Add Segment', 'blockenberg' ) ),
204 a.segments.map( ( seg, si ) =>
205 el( PanelBody, { key: si, title: seg.label || `Segment ${ si + 1 }`, initialOpen: false },
206 el( TextControl, { label: __( 'Label', 'blockenberg' ), value: seg.label, onChange: v => updSeg( setAttributes, a.segments, si, 'label', v ) } ),
207 el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: seg.color, onChange: v => updSeg( setAttributes, a.segments, si, 'color', v ) } ),
208 el( Button, { variant: 'tertiary', isSmall: true, style: { marginBottom: 6 }, onClick: () => updSeg( setAttributes, a.segments, si, 'children', [ ...( seg.children || [] ), { label: 'Item', value: 10 } ] ) }, __( '+ Child', 'blockenberg' ) ),
209 ( seg.children || [] ).map( ( ch, ci ) =>
210 el( 'div', { key: ci, style: { paddingLeft: 12, borderLeft: '2px solid #e5e7eb', marginBottom: 8 } },
211 el( TextControl, { label: `Child ${ ci + 1 } Label`, value: ch.label, onChange: v => updChild( setAttributes, a.segments, si, ci, 'label', v ) } ),
212 el( RangeControl, { label: 'Value', value: ch.value, onChange: v => updChild( setAttributes, a.segments, si, ci, 'value', v ), min: 0, max: 9999 } ),
213 el( Button, { isDestructive: true, isSmall: true, onClick: () => updSeg( setAttributes, a.segments, si, 'children', seg.children.filter( ( _, ki ) => ki !== ci ) ) }, __( '', 'blockenberg' ) ),
214 )
215 ),
216 el( Button, { isDestructive: true, isSmall: true, style: { marginTop: 6 }, onClick: () => setAttributes( { segments: a.segments.filter( ( _, x ) => x !== si ) } ) }, __( 'Remove Segment', 'blockenberg' ) ),
217 )
218 ),
219 ),
220 ),
221
222 a.showTitle && a.title && el( 'h3', { className: 'bkbg-sunburst-title', style: { color: a.titleColor } }, a.title ),
223 el( 'div', { className: 'bkbg-sunburst-svg' }, renderSunburst( a ) ),
224 );
225 },
226
227 save: function ( { attributes: a } ) {
228 const blockProps = useBlockProps.save((function () {
229 var _tvf = getTypoCssVars();
230 var s = {};
231 if (_tvf) Object.assign(s, _tvf(a.titleTypo, '--bksbc-tt-'));
232 return { className: 'bkbg-sunburst-wrap', style: s };
233 })());
234 return el( 'div', blockProps,
235 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-sunburst-title', style: { color: a.titleColor } }, a.title ) : null,
236 el( 'div', { className: 'bkbg-sunburst-svg' }, renderSunburst( a ) ),
237 );
238 },
239 } );
240 }() );
241