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

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

220 lines 13.7 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, _tv;
10 function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); }
11 function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); }
12
13 var IP = function () { return window.bkbgIconPicker; };
14
15 // ── Renderer ──────────────────────────────────────────────────────────────
16 function renderPictograph( a ) {
17 const segs = a.segments || [];
18 const total = a.totalUnits || 100;
19 const cols = a.gridCols || 10;
20 const cs = a.cellSize || 36;
21 const gap = a.cellGap || 4;
22 const fs = a.iconFontSize || 22;
23 const emptyClr = a.emptyColor || '#e5e7eb';
24
25 // ── Largest-remainder allocation ──────────────────────────────────────
26 const sumVals = segs.reduce( ( s, seg ) => s + ( seg.value || 0 ), 0 ) || 1;
27 const exact = segs.map( seg => ( seg.value || 0 ) / sumVals * total );
28 const floored = exact.map( v => Math.floor( v ) );
29 let remaining = total - floored.reduce( ( s, v ) => s + v, 0 );
30 const order = exact.map( ( v, i ) => [ i, v - floored[i] ] ).sort( ( a, b ) => b[1] - a[1] );
31 order.forEach( ( [ i ] ) => { if ( remaining > 0 ) { floored[i]++; remaining--; } } );
32
33 // Build flat colour array for each cell
34 const palette = [];
35 floored.forEach( ( count, si ) => {
36 for ( let k = 0; k < count; k++ ) palette.push( segs[si] ? segs[si].color : '#cccccc' );
37 } );
38 while ( palette.length < total ) palette.push( emptyClr );
39
40 const rows = Math.ceil( total / cols );
41 const W = cols * ( cs + gap ) - gap;
42 const gridH = rows * ( cs + gap ) - gap;
43 const legH = a.showLegend ? Math.ceil( segs.length / 3 ) * 26 + 16 : 0;
44 const H = gridH + legH;
45
46 const svgEls = [];
47 svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) );
48
49 for ( let idx = 0; idx < total; idx++ ) {
50 const col = idx % cols;
51 const row = Math.floor( idx / cols );
52 const cx = col * ( cs + gap ) + cs / 2;
53 const cy = row * ( cs + gap ) + cs / 2;
54 const clr = palette[idx] || emptyClr;
55
56 svgEls.push(
57 el( 'text', {
58 key: `ic${ idx }`,
59 x: cx, y: cy,
60 textAnchor: 'middle', dominantBaseline: 'middle',
61 fontSize: fs,
62 fill: clr,
63 style: { filter: clr === emptyClr ? 'grayscale(1) opacity(0.35)' : 'none' },
64 fontFamily: 'inherit',
65 }, a.icon || '' )
66 );
67 }
68
69 // Legend
70 if ( a.showLegend ) {
71 const perRow = 3;
72 const legY = gridH + 16;
73 const legFS = a.legendFontSize || 13;
74
75 segs.forEach( ( seg, si ) => {
76 const lx = ( si % perRow ) * ( W / perRow );
77 const ly = legY + Math.floor( si / perRow ) * 26;
78 svgEls.push( el( 'circle', { key: `lc${ si }`, cx: lx + 7, cy: ly + 7, r: 7, fill: seg.color } ) );
79 svgEls.push( el( 'text', { key: `lt${ si }`, x: lx + 18, y: ly + 7, dominantBaseline: 'middle', fill: a.textColor || '#374151', fontSize: legFS, fontFamily: 'inherit' }, `${ seg.label } (${ seg.value || 0 })` ) );
80 } );
81 }
82
83 return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls );
84 }
85
86 function updSeg( setAttributes, segs, idx, field, val ) {
87 setAttributes( { segments: segs.map( ( s, i ) => i === idx ? { ...s, [field]: val } : s ) } );
88 }
89
90 /* ── colour-swatch + popover ── */
91 function BkbgColorSwatch(p) {
92 var st = useState(false), open = st[0], setOpen = st[1];
93 return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } },
94 el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label),
95 el('div', { style:{ position:'relative', flexShrink:0 } },
96 el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); },
97 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 } }),
98 open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } },
99 el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } },
100 el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } },
101 el('strong', { style:{ fontSize:'12px' } }, p.label),
102 el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } })
103 ),
104 el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange })
105 )
106 )
107 )
108 );
109 }
110
111 registerBlockType( 'blockenberg/pictograph', {
112 edit: function ( props ) {
113 const { attributes: a, setAttributes } = props;
114 const blockProps = useBlockProps((function () {
115 var tv = getTypoCssVars();
116 var s = {};
117 Object.assign(s, tv(a.titleTypo, '--bkbg-picto-tt-'));
118 return { className: 'bkbg-picto-wrap', style: s };
119 })());
120
121 return el( 'div', blockProps,
122 el( InspectorControls, {},
123
124 el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true },
125 el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ),
126 el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ),
127 el( ToggleControl, { label: __( 'Show Legend', 'blockenberg' ), checked: a.showLegend, onChange: v => setAttributes( { showLegend: v } ) } ),
128 el( IP().IconPickerControl, IP().iconPickerProps( a, setAttributes, { charAttr: 'icon', typeAttr: 'iconType', dashiconAttr: 'iconDashicon', imageUrlAttr: 'iconImageUrl' } ) ),
129 el( RangeControl, { label: __( 'Total Units', 'blockenberg' ), value: a.totalUnits, onChange: v => setAttributes( { totalUnits: v } ), min: 10, max: 200 } ),
130 ),
131
132 el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false },
133 el( RangeControl, { label: __( 'Columns', 'blockenberg' ), value: a.gridCols, onChange: v => setAttributes( { gridCols: v } ), min: 4, max: 25 } ),
134 el( RangeControl, { label: __( 'Cell Gap (px)', 'blockenberg' ), value: a.cellGap, onChange: v => setAttributes( { cellGap: v } ), min: 0, max: 24 } ),
135 ),
136
137
138 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
139 el(getTypoControl(), { label: __('Title', 'blockenberg'), value: a.titleTypo, onChange: v => setAttributes({ titleTypo: v }) }),
140 el( RangeControl, { label: __( 'Cell Size (px)', 'blockenberg' ), value: a.cellSize, onChange: v => setAttributes( { cellSize: v } ), min: 16, max: 80 } ),
141 el( RangeControl, { label: __( 'Icon Font Size', 'blockenberg' ), value: a.iconFontSize, onChange: v => setAttributes( { iconFontSize: v } ), min: 8, max: 60 } ),
142 el( RangeControl, { label: __( 'Legend Font Size', 'blockenberg' ), value: a.legendFontSize, onChange: v => setAttributes( { legendFontSize: v } ), min: 9, max: 22 } )
143 ),
144 el( PanelColorSettings, {
145 title: __( 'Colors', 'blockenberg' ), initialOpen: false,
146 colorSettings: [
147 { label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) },
148 { label: __( 'Title Color', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) },
149 { label: __( 'Text Color', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) },
150 { label: __( 'Empty Icon', 'blockenberg' ), value: a.emptyColor, onChange: v => setAttributes( { emptyColor: v || '#e5e7eb' } ) },
151 ]
152 } ),
153
154 el( PanelBody, { title: __( 'Segments', 'blockenberg' ), initialOpen: false },
155 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { segments: [ ...a.segments, { label: 'New Segment', value: 10, color: '#6366f1' } ] } ) }, __( '+ Add Segment', 'blockenberg' ) ),
156 a.segments.map( ( seg, i ) =>
157 el( PanelBody, { key: i, title: seg.label || `Segment ${ i + 1 }`, initialOpen: false },
158 el( TextControl, { label: __( 'Label', 'blockenberg' ), value: seg.label, onChange: v => updSeg( setAttributes, a.segments, i, 'label', v ) } ),
159 el( RangeControl, { label: __( 'Value', 'blockenberg' ), value: seg.value, onChange: v => updSeg( setAttributes, a.segments, i, 'value', v ), min: 0, max: a.totalUnits } ),
160 el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: seg.color, onChange: v => updSeg( setAttributes, a.segments, i, 'color', v ) } ),
161 el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { segments: a.segments.filter( ( _, x ) => x !== i ) } ) }, __( 'Remove', 'blockenberg' ) ),
162 )
163 ),
164 ),
165 ),
166
167 a.showTitle && a.title && el( 'h3', { className: 'bkbg-picto-title', style: { color: a.titleColor } }, a.title ),
168 el( 'div', { className: 'bkbg-picto-svg' }, renderPictograph( a ) ),
169 );
170 },
171
172 deprecated: [{
173 attributes: {
174 title: { type: "string", default: "Population by Age Group" },
175 showTitle: { type: "boolean", default: true },
176 icon: { type: "string", default: "\ud83d\udc64" },
177 gridCols: { type: "integer", default: 10 },
178 totalUnits: { type: "integer", default: 100 },
179 cellSize: { type: "integer", default: 36 },
180 cellGap: { type: "integer", default: 4 },
181 iconFontSize: { type: "integer", default: 22 },
182 showLegend: { type: "boolean", default: true },
183 legendFontSize: { type: "integer", default: 13 },
184 bgColor: { type: "string", default: "#ffffff" },
185 titleColor: { type: "string", default: "#111827" },
186 textColor: { type: "string", default: "#374151" },
187 emptyColor: { type: "string", default: "#e5e7eb" },
188 segments: { type: "array", default: [
189 { label: "Under 18", value: 22, color: "#6366f1" },
190 { label: "18 \u2013 34", value: 28, color: "#0ea5e9" },
191 { label: "35 \u2013 54", value: 30, color: "#10b981" },
192 { label: "55 \u2013 74", value: 14, color: "#f59e0b" },
193 { label: "75+", value: 6, color: "#ef4444" }
194 ]},
195 iconFontWeight: { type: "string", default: "400" },
196 legendFontWeight: { type: "string", default: "400" }
197 },
198 save: function ( { attributes: a } ) {
199 const blockProps = useBlockProps.save( { className: 'bkbg-picto-wrap' } );
200 return el( 'div', blockProps,
201 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-picto-title', style: { color: a.titleColor } }, a.title ) : null,
202 el( 'div', { className: 'bkbg-picto-svg' }, renderPictograph( a ) ),
203 );
204 }
205 }],
206 save: function ( { attributes: a } ) {
207 const blockProps = useBlockProps.save((function () {
208 var tv = getTypoCssVars();
209 var s = {};
210 Object.assign(s, tv(a.titleTypo, '--bkbg-picto-tt-'));
211 return { className: 'bkbg-picto-wrap', style: s };
212 })());
213 return el( 'div', blockProps,
214 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-picto-title', style: { color: a.titleColor } }, a.title ) : null,
215 el( 'div', { className: 'bkbg-picto-svg' }, renderPictograph( a ) ),
216 );
217 },
218 } );
219 }() );
220