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

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

196 lines 13.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, ToggleControl, TextControl, SelectControl, 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 var _tvf = function (typo, prefix) { var fn = getTypoCssVars(); return fn ? fn(typo, prefix) : {}; };
12
13 // ── Renderer ──────────────────────────────────────────────────────────────
14 function renderTimeline( a ) {
15 const events = a.events || [];
16 const W = a.svgWidth;
17 const PT = a.padTop;
18 const PL = a.padLeft;
19 const PR = a.padRight;
20 const PB = a.padBottom;
21 const RH = a.rowHeight;
22 const BH = a.barHeight;
23 const tStart = a.timeStart;
24 const tEnd = a.timeEnd;
25 const tRange = ( tEnd - tStart ) || 1;
26 const chartW = W - PL - PR;
27 const H = PT + events.length * RH + PB;
28
29 function tx( v ) { return PL + ( ( v - tStart ) / tRange ) * chartW; }
30
31 const svgEls = [];
32 svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) );
33
34 // Vertical grid lines + time axis labels
35 const tickCount = Math.min( tRange, 12 );
36 for ( let t = 0; t <= tickCount; t++ ) {
37 const tv = tStart + ( t / tickCount ) * tRange;
38 const gx = tx( tv );
39 if ( a.showGrid ) {
40 svgEls.push( el( 'line', { key: `vg${ t }`, x1: gx, y1: PT, x2: gx, y2: H - PB, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) );
41 }
42 svgEls.push( el( 'line', { key: `tick${ t }`, x1: gx, y1: PT - 6, x2: gx, y2: PT, stroke: a.axisColor || '#e5e7eb', strokeWidth: 1 } ) );
43 svgEls.push( el( 'text', { key: `tlbl${ t }`, x: gx, y: PT - 10, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 2, fontFamily: 'inherit' }, Math.round( tv ) ) );
44 }
45
46 // Axis title (time unit)
47 svgEls.push( el( 'text', { key: 'tunit', x: PL + chartW / 2, y: PT - 28, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontFamily: 'inherit', fontWeight: 600 }, a.timeUnit ) );
48
49 // Axis baseline
50 svgEls.push( el( 'line', { key: 'xax', x1: PL, y1: PT, x2: W - PR, y2: PT, stroke: a.axisColor || '#e5e7eb', strokeWidth: 1.5 } ) );
51
52 // Event bars
53 events.forEach( ( ev, ei ) => {
54 const cy = PT + ei * RH + RH / 2;
55 const x1 = tx( Math.max( tStart, ev.start || 0 ) );
56 const x2 = tx( Math.min( tEnd, ev.end || tEnd ) );
57 const bw = Math.max( 2, x2 - x1 );
58 const by = cy - BH / 2;
59 const clr = ev.color || '#6366f1';
60
61 // Row stripe
62 if ( ei % 2 === 1 ) {
63 svgEls.push( el( 'rect', { key: `stripe${ ei }`, x: PL, y: PT + ei * RH, width: chartW, height: RH, fill: a.gridColor || '#f3f4f6', fillOpacity: 0.5 } ) );
64 }
65
66 // Row label
67 svgEls.push( el( 'text', { key: `rlbl${ ei }`, x: PL - 8, y: cy, textAnchor: 'end', dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontFamily: 'inherit', fontWeight: 500 }, ev.label ) );
68
69 // Bar
70 svgEls.push( el( 'rect', { key: `bar${ ei }`, x: x1, y: by, width: bw, height: BH, fill: clr, rx: 4, fillOpacity: 0.9 } ) );
71
72 // Bar label (inside bar if wide enough)
73 if ( a.showValues ) {
74 const durationLabel = `${ parseFloat( ( ev.end - ev.start ).toFixed(1) ) } ${ a.timeUnit }`;
75 if ( bw > 50 ) {
76 svgEls.push( el( 'text', { key: `bval${ ei }`, x: x1 + bw / 2, y: cy, textAnchor: 'middle', dominantBaseline: 'middle', fill: '#ffffff', fontSize: a.barFontSize, fontFamily: 'inherit', fontWeight: 600 }, durationLabel ) );
77 } else {
78 svgEls.push( el( 'text', { key: `bval${ ei }`, x: x2 + 4, y: cy, dominantBaseline: 'middle', fill: clr, fontSize: a.barFontSize, fontFamily: 'inherit', fontWeight: 600 }, durationLabel ) );
79 }
80 }
81 } );
82
83 // Horizontal row separators
84 for ( let i = 0; i <= events.length; i++ ) {
85 svgEls.push( el( 'line', { key: `hsep${ i }`, x1: 0, y1: PT + i * RH, x2: W, y2: PT + i * RH, stroke: a.axisColor || '#e5e7eb', strokeWidth: 0.5, strokeOpacity: 0.6 } ) );
86 }
87
88 return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls );
89 }
90
91 function updEv( setAttributes, events, ei, field, val ) {
92 setAttributes( { events: events.map( ( e, i ) => i === ei ? { ...e, [field]: val } : e ) } );
93 }
94
95 /* ── colour-swatch + popover ── */
96 function BkbgColorSwatch(p) {
97 var st = useState(false), open = st[0], setOpen = st[1];
98 return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } },
99 el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label),
100 el('div', { style:{ position:'relative', flexShrink:0 } },
101 el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); },
102 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 } }),
103 open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } },
104 el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } },
105 el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } },
106 el('strong', { style:{ fontSize:'12px' } }, p.label),
107 el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } })
108 ),
109 el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange })
110 )
111 )
112 )
113 );
114 }
115
116 registerBlockType( 'blockenberg/timeline-chart', {
117 edit: function ( props ) {
118 const { attributes: a, setAttributes } = props;
119 const blockProps = useBlockProps( (function () {
120 var tv = Object.assign({}, _tvf(a.titleTypo, '--bktlc-tt-'));
121 return { className: 'bkbg-timeline-wrap', style: tv };
122 })() );
123
124 return el( 'div', blockProps,
125 el( InspectorControls, {},
126
127 el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true },
128 el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ),
129 el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ),
130 el( ToggleControl, { label: __( 'Show Grid Lines', 'blockenberg' ), checked: a.showGrid, onChange: v => setAttributes( { showGrid: v } ) } ),
131 el( ToggleControl, { label: __( 'Show Duration Labels', 'blockenberg' ), checked: a.showValues, onChange: v => setAttributes( { showValues: v } ) } ),
132 el( TextControl, { label: __( 'Time Unit Label', 'blockenberg' ), value: a.timeUnit, onChange: v => setAttributes( { timeUnit: v } ) } ),
133 el( RangeControl, { label: __( 'Time Start', 'blockenberg' ), value: a.timeStart, onChange: v => setAttributes( { timeStart: v } ), min: -100, max: 10000 } ),
134 el( RangeControl, { label: __( 'Time End', 'blockenberg' ), value: a.timeEnd, onChange: v => setAttributes( { timeEnd: v } ), min: -100, max: 10000 } ),
135 ),
136
137 el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false },
138 el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ),
139 el( RangeControl, { label: __( 'Row Height', 'blockenberg' ), value: a.rowHeight, onChange: v => setAttributes( { rowHeight: v } ), min: 24, max: 80 } ),
140 el( RangeControl, { label: __( 'Bar Height', 'blockenberg' ), value: a.barHeight, onChange: v => setAttributes( { barHeight: v } ), min: 10, max: 50 } ),
141 el( RangeControl, { label: __( 'Label Column Width', 'blockenberg' ), value: a.padLeft, onChange: v => setAttributes( { padLeft: v } ), min: 60, max: 260 } ),
142 ),
143
144
145 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
146 getTypoControl() && el(getTypoControl(), {
147 label: __('Title Typography', 'blockenberg'),
148 value: a.titleTypo || {},
149 onChange: v => setAttributes( { titleTypo: v } )
150 }),
151 el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 20 } ),
152 el( RangeControl, { label: __( 'Bar Font Size', 'blockenberg' ), value: a.barFontSize, onChange: v => setAttributes( { barFontSize: v } ), min: 7, max: 16 } )
153 ),
154 el( PanelColorSettings, {
155 title: __( 'Colors', 'blockenberg' ), initialOpen: false,
156 colorSettings: [
157 { label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) },
158 { label: __( 'Grid', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) },
159 { label: __( 'Axis lines', 'blockenberg' ), value: a.axisColor, onChange: v => setAttributes( { axisColor: v || '#e5e7eb' } ) },
160 { label: __( 'Title', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) },
161 { label: __( 'Text', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) },
162 ]
163 } ),
164
165 el( PanelBody, { title: __( 'Events', 'blockenberg' ), initialOpen: false },
166 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { events: [ ...a.events, { label: 'New Event', color: '#8b5cf6', start: a.timeStart, end: a.timeEnd } ] } ) }, __( '+ Add Event', 'blockenberg' ) ),
167 a.events.map( ( ev, ei ) =>
168 el( PanelBody, { key: ei, title: ev.label || `Event ${ ei + 1 }`, initialOpen: false },
169 el( TextControl, { label: __( 'Label', 'blockenberg' ), value: ev.label, onChange: v => updEv( setAttributes, a.events, ei, 'label', v ) } ),
170 el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: ev.color, onChange: v => updEv( setAttributes, a.events, ei, 'color', v ) } ),
171 el( TextControl, { label: __( 'Start', 'blockenberg' ), value: String( ev.start ), type: 'number', onChange: v => updEv( setAttributes, a.events, ei, 'start', parseFloat( v ) || 0 ) } ),
172 el( TextControl, { label: __( 'End', 'blockenberg' ), value: String( ev.end ), type: 'number', onChange: v => updEv( setAttributes, a.events, ei, 'end', parseFloat( v ) || 0 ) } ),
173 el( Button, { isDestructive: true, isSmall: true, style: { marginTop: 6 }, onClick: () => setAttributes( { events: a.events.filter( ( _, x ) => x !== ei ) } ) }, __( 'Remove', 'blockenberg' ) ),
174 )
175 ),
176 ),
177 ),
178
179 a.showTitle && a.title && el( 'h3', { className: 'bkbg-timeline-title', style: { color: a.titleColor } }, a.title ),
180 el( 'div', { className: 'bkbg-timeline-svg' }, renderTimeline( a ) ),
181 );
182 },
183
184 save: function ( { attributes: a } ) {
185 const blockProps = useBlockProps.save( (function () {
186 var tv = Object.assign({}, _tvf(a.titleTypo, '--bktlc-tt-'));
187 return { className: 'bkbg-timeline-wrap', style: tv };
188 })() );
189 return el( 'div', blockProps,
190 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-timeline-title', style: { color: a.titleColor } }, a.title ) : null,
191 el( 'div', { className: 'bkbg-timeline-svg' }, renderTimeline( a ) ),
192 );
193 },
194 } );
195 }() );
196