| 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 |
function getTypographyControl() { |
| 10 |
return (window.bkbgTypographyControl || function () { return null; }); |
| 11 |
} |
| 12 |
|
| 13 |
function _tv(typo, prefix) { |
| 14 |
if (!typo) return {}; |
| 15 |
var s = {}; |
| 16 |
if (typo.family) s[prefix + 'font-family'] = "'" + typo.family + "', sans-serif"; |
| 17 |
if (typo.weight) s[prefix + 'font-weight'] = typo.weight; |
| 18 |
if (typo.transform) s[prefix + 'text-transform'] = typo.transform; |
| 19 |
if (typo.style) s[prefix + 'font-style'] = typo.style; |
| 20 |
if (typo.decoration) s[prefix + 'text-decoration'] = typo.decoration; |
| 21 |
var su = typo.sizeUnit || 'px'; |
| 22 |
if (typo.sizeDesktop !== '' && typo.sizeDesktop != null) s[prefix + 'font-size-d'] = typo.sizeDesktop + su; |
| 23 |
if (typo.sizeTablet !== '' && typo.sizeTablet != null) s[prefix + 'font-size-t'] = typo.sizeTablet + su; |
| 24 |
if (typo.sizeMobile !== '' && typo.sizeMobile != null) s[prefix + 'font-size-m'] = typo.sizeMobile + su; |
| 25 |
var lhu = typo.lineHeightUnit || ''; |
| 26 |
if (typo.lineHeightDesktop !== '' && typo.lineHeightDesktop != null) s[prefix + 'line-height-d'] = typo.lineHeightDesktop + lhu; |
| 27 |
if (typo.lineHeightTablet !== '' && typo.lineHeightTablet != null) s[prefix + 'line-height-t'] = typo.lineHeightTablet + lhu; |
| 28 |
if (typo.lineHeightMobile !== '' && typo.lineHeightMobile != null) s[prefix + 'line-height-m'] = typo.lineHeightMobile + lhu; |
| 29 |
var lsu = typo.letterSpacingUnit || 'px'; |
| 30 |
if (typo.letterSpacingDesktop !== '' && typo.letterSpacingDesktop != null) s[prefix + 'letter-spacing-d'] = typo.letterSpacingDesktop + lsu; |
| 31 |
if (typo.letterSpacingTablet !== '' && typo.letterSpacingTablet != null) s[prefix + 'letter-spacing-t'] = typo.letterSpacingTablet + lsu; |
| 32 |
if (typo.letterSpacingMobile !== '' && typo.letterSpacingMobile != null) s[prefix + 'letter-spacing-m'] = typo.letterSpacingMobile + lsu; |
| 33 |
var wsu = typo.wordSpacingUnit || 'px'; |
| 34 |
if (typo.wordSpacingDesktop !== '' && typo.wordSpacingDesktop != null) s[prefix + 'word-spacing-d'] = typo.wordSpacingDesktop + wsu; |
| 35 |
if (typo.wordSpacingTablet !== '' && typo.wordSpacingTablet != null) s[prefix + 'word-spacing-t'] = typo.wordSpacingTablet + wsu; |
| 36 |
if (typo.wordSpacingMobile !== '' && typo.wordSpacingMobile != null) s[prefix + 'word-spacing-m'] = typo.wordSpacingMobile + wsu; |
| 37 |
return s; |
| 38 |
} |
| 39 |
|
| 40 |
// ── Renderer ────────────────────────────────────────────────────────────── |
| 41 |
function renderBumpChart( a ) { |
| 42 |
const series = a.series || []; |
| 43 |
const xLabels = a.xLabels || []; |
| 44 |
const W = a.svgWidth; |
| 45 |
const H = a.svgHeight; |
| 46 |
const PT = a.padTop; |
| 47 |
const PL = a.padLeft; |
| 48 |
const PR = a.padRight; |
| 49 |
const DR = a.dotRadius; |
| 50 |
const LT = a.lineThickness; |
| 51 |
|
| 52 |
const nCols = Math.max( ...series.map( s => ( s.ranks || [] ).length ), xLabels.length, 2 ); |
| 53 |
const maxRank = Math.max( ...series.flatMap( s => s.ranks || [] ), 1 ); |
| 54 |
const chartW = W - PL - PR; |
| 55 |
const chartH = H - PT - 20; |
| 56 |
|
| 57 |
function xAt( col ) { return PL + col * ( chartW / ( nCols - 1 ) ); } |
| 58 |
function yAt( rank ) { return PT + ( ( rank - 1 ) / Math.max( maxRank - 1, 1 ) ) * chartH; } |
| 59 |
|
| 60 |
// Cubic bezier S-curve between two rank points |
| 61 |
function curvePath( x0, y0, x1, y1 ) { |
| 62 |
const cpX = ( x0 + x1 ) / 2; |
| 63 |
return `M ${ x0 } ${ y0 } C ${ cpX } ${ y0 }, ${ cpX } ${ y1 }, ${ x1 } ${ y1 }`; |
| 64 |
} |
| 65 |
|
| 66 |
const svgEls = []; |
| 67 |
svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) ); |
| 68 |
|
| 69 |
// Vertical column guide lines |
| 70 |
if ( a.showColumnLabels ) { |
| 71 |
for ( let c = 0; c < nCols; c++ ) { |
| 72 |
const gx = xAt( c ); |
| 73 |
svgEls.push( el( 'line', { key: `col${ c }`, x1: gx, y1: PT - 14, x2: gx, y2: H - 10, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) ); |
| 74 |
const lbl = xLabels[c] !== undefined ? String( xLabels[c] ) : String( c + 1 ); |
| 75 |
svgEls.push( el( 'text', { key: `clt${ c }`, x: gx, y: PT - 20, textAnchor: 'middle', fill: a.textColor || '#374151', fontSize: a.labelFontSize, fontFamily: 'inherit', fontWeight: 600 }, lbl ) ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// Series lines and dots |
| 80 |
series.forEach( ( ser, si ) => { |
| 81 |
const ranks = ser.ranks || []; |
| 82 |
const clr = ser.color || '#6366f1'; |
| 83 |
|
| 84 |
// Bezier segments between consecutive points |
| 85 |
for ( let c = 0; c < ranks.length - 1; c++ ) { |
| 86 |
const x0 = xAt( c ); |
| 87 |
const y0 = yAt( ranks[c] ); |
| 88 |
const x1 = xAt( c + 1 ); |
| 89 |
const y1 = yAt( ranks[c + 1] ); |
| 90 |
svgEls.push( el( 'path', { key: `seg${ si }${ c }`, d: curvePath( x0, y0, x1, y1 ), fill: 'none', stroke: clr, strokeWidth: LT, strokeLinecap: 'round' } ) ); |
| 91 |
} |
| 92 |
|
| 93 |
// Dots and rank numbers |
| 94 |
ranks.forEach( ( rank, c ) => { |
| 95 |
const cx = xAt( c ); |
| 96 |
const cy = yAt( rank ); |
| 97 |
svgEls.push( el( 'circle', { key: `dot${ si }${ c }`, cx, cy, r: DR, fill: clr } ) ); |
| 98 |
if ( a.showRankLabels ) { |
| 99 |
svgEls.push( el( 'text', { key: `rk${ si }${ c }`, x: cx, y: cy, textAnchor: 'middle', dominantBaseline: 'middle', fill: '#ffffff', fontSize: a.rankFontSize, fontWeight: 700, fontFamily: 'inherit' }, String( rank ) ) ); |
| 100 |
} |
| 101 |
} ); |
| 102 |
|
| 103 |
// Left label |
| 104 |
if ( a.showEndLabels && ranks.length > 0 ) { |
| 105 |
svgEls.push( el( 'text', { key: `lblL${ si }`, x: xAt( 0 ) - DR - 6, y: yAt( ranks[0] ), textAnchor: 'end', dominantBaseline: 'middle', fill: clr, fontSize: a.labelFontSize, fontWeight: 600, fontFamily: 'inherit' }, ser.label || '' ) ); |
| 106 |
} |
| 107 |
// Right label |
| 108 |
if ( a.showEndLabels && ranks.length > 0 ) { |
| 109 |
svgEls.push( el( 'text', { key: `lblR${ si }`, x: xAt( ranks.length - 1 ) + DR + 6, y: yAt( ranks[ranks.length - 1] ), textAnchor: 'start', dominantBaseline: 'middle', fill: clr, fontSize: a.labelFontSize, fontWeight: 600, fontFamily: 'inherit' }, ser.label || '' ) ); |
| 110 |
} |
| 111 |
} ); |
| 112 |
|
| 113 |
return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls ); |
| 114 |
} |
| 115 |
|
| 116 |
function updSer( setAttributes, series, si, field, val ) { |
| 117 |
setAttributes( { series: series.map( ( s, i ) => i === si ? { ...s, [field]: val } : s ) } ); |
| 118 |
} |
| 119 |
function parseRanks( raw ) { |
| 120 |
return raw.split( /[\s,]+/ ).map( Number ).filter( v => !isNaN( v ) && v > 0 ); |
| 121 |
} |
| 122 |
|
| 123 |
/* ── colour-swatch + popover ── */ |
| 124 |
function BkbgColorSwatch(p) { |
| 125 |
var st = useState(false), open = st[0], setOpen = st[1]; |
| 126 |
return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } }, |
| 127 |
el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label), |
| 128 |
el('div', { style:{ position:'relative', flexShrink:0 } }, |
| 129 |
el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); }, |
| 130 |
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 } }), |
| 131 |
open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } }, |
| 132 |
el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } }, |
| 133 |
el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } }, |
| 134 |
el('strong', { style:{ fontSize:'12px' } }, p.label), |
| 135 |
el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } }) |
| 136 |
), |
| 137 |
el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange }) |
| 138 |
) |
| 139 |
) |
| 140 |
) |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
registerBlockType( 'blockenberg/bump-chart', { |
| 145 |
edit: function ( props ) { |
| 146 |
const { attributes: a, setAttributes } = props; |
| 147 |
const blockProps = useBlockProps( { className: 'bkbg-bump-wrap', style: Object.assign( {}, _tv( a.typoTitle, '--bkbg-bump-title-' ) ) } ); |
| 148 |
|
| 149 |
return el( 'div', blockProps, |
| 150 |
el( InspectorControls, {}, |
| 151 |
|
| 152 |
el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true }, |
| 153 |
el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ), |
| 154 |
el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ), |
| 155 |
el( ToggleControl, { label: __( 'Show Column Labels', 'blockenberg' ), checked: a.showColumnLabels, onChange: v => setAttributes( { showColumnLabels: v } ) } ), |
| 156 |
el( ToggleControl, { label: __( 'Show Rank Numbers', 'blockenberg' ), checked: a.showRankLabels, onChange: v => setAttributes( { showRankLabels: v } ) } ), |
| 157 |
el( ToggleControl, { label: __( 'Show Series Labels', 'blockenberg' ), checked: a.showEndLabels, onChange: v => setAttributes( { showEndLabels: v } ) } ), |
| 158 |
el( TextControl, { label: __( 'Column Labels (comma separated)', 'blockenberg' ), value: ( a.xLabels || [] ).join( ', ' ), onChange: v => setAttributes( { xLabels: v.split( /[\s,]+/ ).filter( Boolean ) } ) } ), |
| 159 |
), |
| 160 |
|
| 161 |
el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false }, |
| 162 |
el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ), |
| 163 |
el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 150, max: 700 } ), |
| 164 |
el( RangeControl, { label: __( 'Dot Radius', 'blockenberg' ), value: a.dotRadius, onChange: v => setAttributes( { dotRadius: v } ), min: 8, max: 36 } ), |
| 165 |
el( RangeControl, { label: __( 'Line Thickness', 'blockenberg' ), value: a.lineThickness, onChange: v => setAttributes( { lineThickness: v } ), min: 1, max: 10 } ), |
| 166 |
el( RangeControl, { label: __( 'Left Pad (label space)', 'blockenberg' ), value: a.padLeft, onChange: v => setAttributes( { padLeft: v } ), min: 40, max: 280 } ), |
| 167 |
el( RangeControl, { label: __( 'Right Pad (label space)', 'blockenberg' ), value: a.padRight, onChange: v => setAttributes( { padRight: v } ), min: 40, max: 280 } ), |
| 168 |
), |
| 169 |
|
| 170 |
|
| 171 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 172 |
el(getTypographyControl(), { label: __('Title', 'blockenberg'), value: a.typoTitle, onChange: function (v) { setAttributes({ typoTitle: v }); } }), |
| 173 |
el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 9, max: 22 } ), |
| 174 |
el( RangeControl, { label: __( 'Rank Font Size', 'blockenberg' ), value: a.rankFontSize, onChange: v => setAttributes( { rankFontSize: v } ), min: 7, max: 18 } ) |
| 175 |
), |
| 176 |
el( PanelColorSettings, { |
| 177 |
title: __( 'Colors', 'blockenberg' ), initialOpen: false, |
| 178 |
colorSettings: [ |
| 179 |
{ label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) }, |
| 180 |
{ label: __( 'Column Lines', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) }, |
| 181 |
{ label: __( 'Title Color', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) }, |
| 182 |
{ label: __( 'Text Color', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) }, |
| 183 |
] |
| 184 |
} ), |
| 185 |
|
| 186 |
el( PanelBody, { title: __( 'Series', 'blockenberg' ), initialOpen: false }, |
| 187 |
el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { series: [ ...a.series, { label: 'New Series', color: '#6366f1', ranks: [1, 2, 3, 2, 1] } ] } ) }, __( '+ Add Series', 'blockenberg' ) ), |
| 188 |
a.series.map( ( ser, si ) => |
| 189 |
el( PanelBody, { key: si, title: ser.label || `Series ${ si + 1 }`, initialOpen: false }, |
| 190 |
el( TextControl, { label: __( 'Label', 'blockenberg' ), value: ser.label, onChange: v => updSer( setAttributes, a.series, si, 'label', v ) } ), |
| 191 |
el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: ser.color, onChange: v => updSer( setAttributes, a.series, si, 'color', v ) } ), |
| 192 |
el( TextControl, { label: __( 'Ranks (comma separated, 1=top)', 'blockenberg' ), value: ( ser.ranks || [] ).join( ', ' ), onChange: v => updSer( setAttributes, a.series, si, 'ranks', parseRanks( v ) ) } ), |
| 193 |
el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { series: a.series.filter( ( _, x ) => x !== si ) } ) }, __( 'Remove', 'blockenberg' ) ), |
| 194 |
) |
| 195 |
), |
| 196 |
), |
| 197 |
), |
| 198 |
|
| 199 |
a.showTitle && a.title && el( 'h3', { className: 'bkbg-bump-title', style: { color: a.titleColor } }, a.title ), |
| 200 |
el( 'div', { className: 'bkbg-bump-svg' }, renderBumpChart( a ) ), |
| 201 |
); |
| 202 |
}, |
| 203 |
|
| 204 |
save: function ( { attributes: a } ) { |
| 205 |
const blockProps = useBlockProps.save( { className: 'bkbg-bump-wrap', style: Object.assign( {}, _tv( a.typoTitle, '--bkbg-bump-title-' ) ) } ); |
| 206 |
return el( 'div', blockProps, |
| 207 |
a.showTitle && a.title ? el( 'h3', { className: 'bkbg-bump-title', style: { color: a.titleColor } }, a.title ) : null, |
| 208 |
el( 'div', { className: 'bkbg-bump-svg' }, renderBumpChart( a ) ), |
| 209 |
); |
| 210 |
}, |
| 211 |
} ); |
| 212 |
}() ); |
| 213 |
|