| 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; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 10 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 11 |
|
| 12 |
// ── Helpers ─────────────────────────────────────────────────────────────── |
| 13 |
function degToRad( d ) { return d * Math.PI / 180; } |
| 14 |
|
| 15 |
// Arc path for thick stroke — returns SVG 'd' for a stroked path |
| 16 |
function arcD( cx, cy, r, startDeg, endDeg ) { |
| 17 |
const s = degToRad( startDeg ); |
| 18 |
const e = degToRad( endDeg ); |
| 19 |
const x1 = cx + r * Math.cos( s ); |
| 20 |
const y1 = cy + r * Math.sin( s ); |
| 21 |
const x2 = cx + r * Math.cos( e ); |
| 22 |
const y2 = cy + r * Math.sin( e ); |
| 23 |
const large = ( endDeg - startDeg ) > 180 ? 1 : 0; |
| 24 |
return `M ${ x1 } ${ y1 } A ${ r } ${ r } 0 ${ large } 1 ${ x2 } ${ y2 }`; |
| 25 |
} |
| 26 |
|
| 27 |
// ── Renderer ────────────────────────────────────────────────────────────── |
| 28 |
function renderSpeedometer( a ) { |
| 29 |
const zones = a.zones || []; |
| 30 |
const W = a.svgWidth; |
| 31 |
const H = a.svgHeight; |
| 32 |
const cx = W / 2; |
| 33 |
const cy = H * 0.62; |
| 34 |
const r = Math.min( W, H ) * 0.36; |
| 35 |
const AT = a.arcThickness; |
| 36 |
const range = ( a.maxValue - a.minValue ) || 1; |
| 37 |
|
| 38 |
// Gauge spans 180° — from 180° to 0° (left → right in SVG coords) |
| 39 |
const START_DEG = 180; |
| 40 |
const END_DEG = 0; |
| 41 |
|
| 42 |
function valToDeg( v ) { |
| 43 |
const pct = Math.min( 1, Math.max( 0, ( v - a.minValue ) / range ) ); |
| 44 |
return START_DEG + pct * 180; // 180 → 360 (=0) |
| 45 |
} |
| 46 |
|
| 47 |
const svgEls = []; |
| 48 |
svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) ); |
| 49 |
|
| 50 |
// Track background |
| 51 |
svgEls.push( el( 'path', { key: 'track', d: arcD( cx, cy, r, 180, 360 ), fill: 'none', stroke: '#e5e7eb', strokeWidth: AT, strokeLinecap: 'butt' } ) ); |
| 52 |
|
| 53 |
// Zone arcs |
| 54 |
zones.forEach( ( z, zi ) => { |
| 55 |
const fromDeg = valToDeg( z.from ); |
| 56 |
const toDeg = valToDeg( z.to ); |
| 57 |
if ( toDeg <= fromDeg ) return; |
| 58 |
svgEls.push( el( 'path', { key: `zone${ zi }`, d: arcD( cx, cy, r, fromDeg, toDeg ), fill: 'none', stroke: z.color || '#6366f1', strokeWidth: AT, strokeLinecap: 'butt' } ) ); |
| 59 |
} ); |
| 60 |
|
| 61 |
// Zone labels (optional, outside arc) |
| 62 |
if ( a.showZoneLabels ) { |
| 63 |
zones.forEach( ( z, zi ) => { |
| 64 |
const midDeg = valToDeg( ( z.from + z.to ) / 2 ); |
| 65 |
const rad = degToRad( midDeg ); |
| 66 |
const lx = cx + ( r + AT + 14 ) * Math.cos( rad ); |
| 67 |
const ly = cy + ( r + AT + 14 ) * Math.sin( rad ); |
| 68 |
svgEls.push( el( 'text', { key: `zlbl${ zi }`, x: lx, y: ly, textAnchor: 'middle', dominantBaseline: 'middle', fill: z.color, fontSize: a.labelFontSize - 2, fontFamily: 'inherit' }, z.label ) ); |
| 69 |
} ); |
| 70 |
} |
| 71 |
|
| 72 |
// Tick marks |
| 73 |
if ( a.showTicks ) { |
| 74 |
const tickCount = 10; |
| 75 |
for ( let t = 0; t <= tickCount; t++ ) { |
| 76 |
const td = START_DEG + ( t / tickCount ) * 180; |
| 77 |
const rad = degToRad( td ); |
| 78 |
const major = ( t % 2 === 0 ); |
| 79 |
const r1 = r - AT / 2 - 2; |
| 80 |
const r2 = r1 - ( major ? 10 : 6 ); |
| 81 |
svgEls.push( el( 'line', { |
| 82 |
key: `tick${ t }`, |
| 83 |
x1: cx + r1 * Math.cos( rad ), y1: cy + r1 * Math.sin( rad ), |
| 84 |
x2: cx + r2 * Math.cos( rad ), y2: cy + r2 * Math.sin( rad ), |
| 85 |
stroke: a.textColor, strokeWidth: major ? 2 : 1, |
| 86 |
} ) ); |
| 87 |
if ( major ) { |
| 88 |
const tv = a.minValue + ( t / tickCount ) * range; |
| 89 |
const r3 = r2 - 12; |
| 90 |
svgEls.push( el( 'text', { |
| 91 |
key: `ticklbl${ t }`, |
| 92 |
x: cx + r3 * Math.cos( rad ), y: cy + r3 * Math.sin( rad ), |
| 93 |
textAnchor: 'middle', dominantBaseline: 'middle', |
| 94 |
fill: a.textColor, fontSize: a.labelFontSize - 2, fontFamily: 'inherit', |
| 95 |
}, Math.round( tv ) ) ); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// Needle |
| 101 |
const needleDeg = valToDeg( a.value ); |
| 102 |
const needleRad = degToRad( needleDeg ); |
| 103 |
const NL = a.needleLength; |
| 104 |
const NW = a.needleWidth; |
| 105 |
const tip = { x: cx + NL * Math.cos( needleRad ), y: cy + NL * Math.sin( needleRad ) }; |
| 106 |
const lft = { x: cx + ( NW / 2 ) * Math.cos( needleRad + Math.PI / 2 ), y: cy + ( NW / 2 ) * Math.sin( needleRad + Math.PI / 2 ) }; |
| 107 |
const rgt = { x: cx + ( NW / 2 ) * Math.cos( needleRad - Math.PI / 2 ), y: cy + ( NW / 2 ) * Math.sin( needleRad - Math.PI / 2 ) }; |
| 108 |
svgEls.push( el( 'polygon', { |
| 109 |
key: 'needle', |
| 110 |
points: `${ tip.x },${ tip.y } ${ lft.x },${ lft.y } ${ rgt.x },${ rgt.y }`, |
| 111 |
fill: a.needleColor || '#111827', |
| 112 |
} ) ); |
| 113 |
|
| 114 |
// Centre hub |
| 115 |
svgEls.push( el( 'circle', { key: 'hub', cx, cy, r: NW * 1.6, fill: a.needleColor || '#111827' } ) ); |
| 116 |
svgEls.push( el( 'circle', { key: 'hubInner', cx, cy, r: NW * 0.8, fill: a.centerColor || '#ffffff' } ) ); |
| 117 |
|
| 118 |
// Value text |
| 119 |
if ( a.showValue ) { |
| 120 |
svgEls.push( el( 'text', { key: 'valText', x: cx, y: cy + 36, textAnchor: 'middle', dominantBaseline: 'middle', fill: a.textColor, fontSize: a.valueFontSize, fontWeight: a.valueFontWeight, fontFamily: 'inherit' }, `${ a.value }${ a.unit }` ) ); |
| 121 |
} |
| 122 |
|
| 123 |
// Min / Max labels |
| 124 |
if ( a.showLabel ) { |
| 125 |
const minRad = degToRad( 180 ); |
| 126 |
const maxRad = degToRad( 360 ); |
| 127 |
const lOff = AT + 10; |
| 128 |
svgEls.push( el( 'text', { key: 'minLbl', x: cx + ( r - lOff ) * Math.cos( minRad ), y: cy + ( r - lOff ) * Math.sin( minRad ) - 4, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontWeight: a.labelFontWeight, fontFamily: 'inherit' }, a.minValue ) ); |
| 129 |
svgEls.push( el( 'text', { key: 'maxLbl', x: cx + ( r - lOff ) * Math.cos( maxRad ), y: cy + ( r - lOff ) * Math.sin( maxRad ) - 4, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontWeight: a.labelFontWeight, fontFamily: 'inherit' }, a.maxValue ) ); |
| 130 |
} |
| 131 |
|
| 132 |
return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls ); |
| 133 |
} |
| 134 |
|
| 135 |
function updZone( setAttributes, zones, zi, field, val ) { |
| 136 |
setAttributes( { zones: zones.map( ( z, i ) => i === zi ? { ...z, [field]: val } : z ) } ); |
| 137 |
} |
| 138 |
|
| 139 |
/* ── colour-swatch + popover ── */ |
| 140 |
function BkbgColorSwatch(p) { |
| 141 |
var st = useState(false), open = st[0], setOpen = st[1]; |
| 142 |
return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } }, |
| 143 |
el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label), |
| 144 |
el('div', { style:{ position:'relative', flexShrink:0 } }, |
| 145 |
el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); }, |
| 146 |
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 } }), |
| 147 |
open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } }, |
| 148 |
el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } }, |
| 149 |
el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } }, |
| 150 |
el('strong', { style:{ fontSize:'12px' } }, p.label), |
| 151 |
el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } }) |
| 152 |
), |
| 153 |
el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange }) |
| 154 |
) |
| 155 |
) |
| 156 |
) |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
registerBlockType( 'blockenberg/speedometer', { |
| 161 |
edit: function ( props ) { |
| 162 |
const { attributes: a, setAttributes } = props; |
| 163 |
const blockProps = useBlockProps((function () { |
| 164 |
var s = { className: 'bkbg-speedometer-wrap' }; |
| 165 |
var tv = getTypoCssVars(); |
| 166 |
if (tv) { s.style = Object.assign({}, tv(a.titleTypo, '--bkspm-tt-')); } |
| 167 |
return s; |
| 168 |
})()); |
| 169 |
|
| 170 |
return el( 'div', blockProps, |
| 171 |
el( InspectorControls, {}, |
| 172 |
|
| 173 |
el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true }, |
| 174 |
el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ), |
| 175 |
el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ), |
| 176 |
el( ToggleControl, { label: __( 'Show Value', 'blockenberg' ), checked: a.showValue, onChange: v => setAttributes( { showValue: v } ) } ), |
| 177 |
el( ToggleControl, { label: __( 'Show Min/Max Labels', 'blockenberg' ), checked: a.showLabel, onChange: v => setAttributes( { showLabel: v } ) } ), |
| 178 |
el( ToggleControl, { label: __( 'Show Ticks', 'blockenberg' ), checked: a.showTicks, onChange: v => setAttributes( { showTicks: v } ) } ), |
| 179 |
el( ToggleControl, { label: __( 'Show Zone Labels', 'blockenberg' ), checked: a.showZoneLabels, onChange: v => setAttributes( { showZoneLabels: v } ) } ), |
| 180 |
el( RangeControl, { label: __( 'Value', 'blockenberg' ), value: a.value, onChange: v => setAttributes( { value: v } ), min: a.minValue, max: a.maxValue, step: 0.1 } ), |
| 181 |
el( RangeControl, { label: __( 'Min Value', 'blockenberg' ), value: a.minValue, onChange: v => setAttributes( { minValue: v } ), min: -10000, max: 0 } ), |
| 182 |
el( RangeControl, { label: __( 'Max Value', 'blockenberg' ), value: a.maxValue, onChange: v => setAttributes( { maxValue: v } ), min: 1, max: 10000 } ), |
| 183 |
el( TextControl, { label: __( 'Unit suffix', 'blockenberg' ), value: a.unit, onChange: v => setAttributes( { unit: v } ) } ), |
| 184 |
), |
| 185 |
|
| 186 |
el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false }, |
| 187 |
el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 200, max: 900, step: 20 } ), |
| 188 |
el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 140, max: 600 } ), |
| 189 |
el( RangeControl, { label: __( 'Arc Thickness', 'blockenberg' ), value: a.arcThickness, onChange: v => setAttributes( { arcThickness: v } ), min: 8, max: 60 } ), |
| 190 |
el( RangeControl, { label: __( 'Needle Length', 'blockenberg' ), value: a.needleLength, onChange: v => setAttributes( { needleLength: v } ), min: 40, max: 220 } ), |
| 191 |
el( RangeControl, { label: __( 'Needle Width', 'blockenberg' ), value: a.needleWidth, onChange: v => setAttributes( { needleWidth: v } ), min: 2, max: 18 } ), |
| 192 |
), |
| 193 |
|
| 194 |
|
| 195 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 196 |
getTypoControl() ? el(getTypoControl(), { label: __('Title Typography', 'blockenberg'), value: a.titleTypo || {}, onChange: function (v) { setAttributes({ titleTypo: v }); } }) : null, |
| 197 |
el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 22 } ), |
| 198 |
el( RangeControl, { label: __( 'Label Font Weight', 'blockenberg' ), value: a.labelFontWeight, onChange: v => setAttributes( { labelFontWeight: v } ), min: 300, max: 900, step: 100 } ), |
| 199 |
el( RangeControl, { label: __( 'Value Font Size', 'blockenberg' ), value: a.valueFontSize, onChange: v => setAttributes( { valueFontSize: v } ), min: 14, max: 72 } ), |
| 200 |
el( RangeControl, { label: __( 'Value Font Weight', 'blockenberg' ), value: a.valueFontWeight, onChange: v => setAttributes( { valueFontWeight: v } ), min: 300, max: 900, step: 100, __nextHasNoMarginBottom: true } ) |
| 201 |
), |
| 202 |
el( PanelColorSettings, { |
| 203 |
title: __( 'Colors', 'blockenberg' ), initialOpen: false, |
| 204 |
colorSettings: [ |
| 205 |
{ label: __( 'Needle', 'blockenberg' ), value: a.needleColor, onChange: v => setAttributes( { needleColor: v || '#111827' } ) }, |
| 206 |
{ label: __( 'Hub centre', 'blockenberg' ), value: a.centerColor, onChange: v => setAttributes( { centerColor: v || '#ffffff' } ) }, |
| 207 |
{ label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) }, |
| 208 |
{ label: __( 'Title', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) }, |
| 209 |
{ label: __( 'Text', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) }, |
| 210 |
] |
| 211 |
} ), |
| 212 |
|
| 213 |
el( PanelBody, { title: __( 'Zones', 'blockenberg' ), initialOpen: false }, |
| 214 |
el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { zones: [ ...a.zones, { label: 'Zone', from: 0, to: 100, color: '#8b5cf6' } ] } ) }, __( '+ Add Zone', 'blockenberg' ) ), |
| 215 |
a.zones.map( ( z, zi ) => |
| 216 |
el( PanelBody, { key: zi, title: z.label || `Zone ${ zi + 1 }`, initialOpen: false }, |
| 217 |
el( TextControl, { label: __( 'Label', 'blockenberg' ), value: z.label, onChange: v => updZone( setAttributes, a.zones, zi, 'label', v ) } ), |
| 218 |
el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: z.color, onChange: v => updZone( setAttributes, a.zones, zi, 'color', v ) } ), |
| 219 |
el( RangeControl, { label: __( 'From', 'blockenberg' ), value: z.from, onChange: v => updZone( setAttributes, a.zones, zi, 'from', v ), min: a.minValue, max: a.maxValue } ), |
| 220 |
el( RangeControl, { label: __( 'To', 'blockenberg' ), value: z.to, onChange: v => updZone( setAttributes, a.zones, zi, 'to', v ), min: a.minValue, max: a.maxValue } ), |
| 221 |
el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { zones: a.zones.filter( ( _, x ) => x !== zi ) } ) }, __( 'Remove', 'blockenberg' ) ), |
| 222 |
) |
| 223 |
), |
| 224 |
), |
| 225 |
), |
| 226 |
|
| 227 |
a.showTitle && a.title && el( 'h3', { className: 'bkbg-speedometer-title', style: { color: a.titleColor } }, a.title ), |
| 228 |
el( 'div', { className: 'bkbg-speedometer-svg' }, renderSpeedometer( a ) ), |
| 229 |
); |
| 230 |
}, |
| 231 |
|
| 232 |
save: function ( { attributes: a } ) { |
| 233 |
const blockProps = useBlockProps.save( { className: 'bkbg-speedometer-wrap' } ); |
| 234 |
return el( 'div', blockProps, |
| 235 |
a.showTitle && a.title ? el( 'h3', { className: 'bkbg-speedometer-title', style: { color: a.titleColor } }, a.title ) : null, |
| 236 |
el( 'div', { className: 'bkbg-speedometer-svg' }, renderSpeedometer( a ) ), |
| 237 |
); |
| 238 |
}, |
| 239 |
} ); |
| 240 |
}() ); |
| 241 |
|