| 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 } = window.wp.components; |
| 6 |
const { __ } = window.wp.i18n; |
| 7 |
|
| 8 |
// ── Typography helpers ────────────────────────────────────────────────── |
| 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 renderCandlestick( a ) { |
| 42 |
const candles = a.candles || []; |
| 43 |
const W = a.svgWidth; |
| 44 |
const H = a.svgHeight; |
| 45 |
const PT = a.padTop; |
| 46 |
const PL = a.padLeft; |
| 47 |
const PR = a.padRight; |
| 48 |
const PB = a.padBottom; |
| 49 |
const n = candles.length; |
| 50 |
if ( ! n ) return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%' } ); |
| 51 |
|
| 52 |
const volumeH = a.showVolume ? Math.round( ( H - PT - PB ) * 0.22 ) : 0; |
| 53 |
const priceH = H - PT - PB - volumeH - ( volumeH ? 8 : 0 ); |
| 54 |
const chartW = W - PL - PR; |
| 55 |
const CW = a.candleWidth; |
| 56 |
const WW = a.wickWidth; |
| 57 |
|
| 58 |
const prices = candles.flatMap( c => [ c.high, c.low ] ); |
| 59 |
const minP = Math.min( ...prices ); |
| 60 |
const maxP = Math.max( ...prices ); |
| 61 |
const pRange = ( maxP - minP ) || 1; |
| 62 |
const pPad = pRange * 0.05; |
| 63 |
|
| 64 |
function py( v ) { return PT + priceH - ( ( v - ( minP - pPad ) ) / ( pRange + pPad * 2 ) ) * priceH; } |
| 65 |
|
| 66 |
const maxVol = a.showVolume ? Math.max( ...candles.map( c => c.volume || 0 ) ) || 1 : 1; |
| 67 |
const volTop = PT + priceH + 8; |
| 68 |
|
| 69 |
const step = chartW / n; |
| 70 |
|
| 71 |
const svgEls = []; |
| 72 |
svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) ); |
| 73 |
|
| 74 |
// Price grid lines |
| 75 |
if ( a.showGrid ) { |
| 76 |
const ticks = 5; |
| 77 |
for ( let t = 0; t <= ticks; t++ ) { |
| 78 |
const gy = PT + ( t / ticks ) * priceH; |
| 79 |
const yV = ( minP - pPad ) + ( 1 - t / ticks ) * ( pRange + pPad * 2 ); |
| 80 |
svgEls.push( el( 'line', { key: `gl${ t }`, x1: PL, y1: gy, x2: W - PR, y2: gy, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) ); |
| 81 |
svgEls.push( el( 'text', { key: `gy${ t }`, x: PL - 6, y: gy, textAnchor: 'end', dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontFamily: 'inherit' }, yV.toFixed(1) ) ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// Candles |
| 86 |
candles.forEach( ( c, i ) => { |
| 87 |
const cx = PL + i * step + step / 2; |
| 88 |
const bull = c.close >= c.open; |
| 89 |
const clr = bull ? ( a.bullColor || '#10b981' ) : ( a.bearColor || '#ef4444' ); |
| 90 |
const bodyT = py( Math.max( c.open, c.close ) ); |
| 91 |
const bodyB = py( Math.min( c.open, c.close ) ); |
| 92 |
const bodyH = Math.max( 1, bodyB - bodyT ); |
| 93 |
|
| 94 |
// Wick |
| 95 |
svgEls.push( el( 'line', { key: `wick${ i }`, x1: cx, y1: py( c.high ), x2: cx, y2: py( c.low ), stroke: clr, strokeWidth: WW } ) ); |
| 96 |
// Body |
| 97 |
svgEls.push( el( 'rect', { key: `body${ i }`, x: cx - CW / 2, y: bodyT, width: CW, height: bodyH, fill: bull ? clr : clr, stroke: clr, strokeWidth: 1, fillOpacity: bull ? 1 : 0.85, rx: 1 } ) ); |
| 98 |
|
| 99 |
// Volume bar |
| 100 |
if ( a.showVolume ) { |
| 101 |
const vol = c.volume || 0; |
| 102 |
const vH = ( vol / maxVol ) * ( volumeH * 0.9 ); |
| 103 |
svgEls.push( el( 'rect', { key: `vol${ i }`, x: cx - CW / 2, y: volTop + ( volumeH * 0.9 ) - vH, width: CW, height: vH, fill: clr, fillOpacity: 0.5, rx: 1 } ) ); |
| 104 |
} |
| 105 |
|
| 106 |
// X label |
| 107 |
svgEls.push( el( 'text', { key: `xl${ i }`, x: cx, y: H - PB + 16, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontFamily: 'inherit', transform: n > 7 ? `rotate(-35,${ cx },${ H - PB + 16 })` : undefined }, c.label ) ); |
| 108 |
} ); |
| 109 |
|
| 110 |
// Axes |
| 111 |
svgEls.push( el( 'line', { key: 'xax', x1: PL, y1: PT + priceH, x2: W - PR, y2: PT + priceH, stroke: a.textColor, strokeWidth: 1, strokeOpacity: 0.3 } ) ); |
| 112 |
svgEls.push( el( 'line', { key: 'yax', x1: PL, y1: PT, x2: PL, y2: PT + priceH, stroke: a.textColor, strokeWidth: 1, strokeOpacity: 0.3 } ) ); |
| 113 |
|
| 114 |
// Volume axis separator |
| 115 |
if ( a.showVolume ) { |
| 116 |
svgEls.push( el( 'line', { key: 'volax', x1: PL, y1: volTop, x2: W - PR, y2: volTop, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) ); |
| 117 |
svgEls.push( el( 'text', { key: 'voltitle', x: PL - 6, y: volTop + 4, textAnchor: 'end', fill: a.textColor, fontSize: a.labelFontSize - 2, fontFamily: 'inherit' }, 'Vol' ) ); |
| 118 |
} |
| 119 |
|
| 120 |
// Legend |
| 121 |
svgEls.push( el( 'rect', { key: 'bull-lgn', x: PL, y: 4, width: 12, height: 12, fill: a.bullColor || '#10b981', rx: 2 } ) ); |
| 122 |
svgEls.push( el( 'text', { key: 'bull-lgt', x: PL + 16, y: 12, dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontFamily: 'inherit' }, __( 'Bullish', 'blockenberg' ) ) ); |
| 123 |
svgEls.push( el( 'rect', { key: 'bear-lgn', x: PL + 70, y: 4, width: 12, height: 12, fill: a.bearColor || '#ef4444', rx: 2 } ) ); |
| 124 |
svgEls.push( el( 'text', { key: 'bear-lgt', x: PL + 86, y: 12, dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontFamily: 'inherit' }, __( 'Bearish', 'blockenberg' ) ) ); |
| 125 |
|
| 126 |
return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls ); |
| 127 |
} |
| 128 |
|
| 129 |
function updCandle( setAttributes, candles, ci, field, val ) { |
| 130 |
setAttributes( { candles: candles.map( ( c, i ) => i === ci ? { ...c, [field]: val } : c ) } ); |
| 131 |
} |
| 132 |
|
| 133 |
registerBlockType( 'blockenberg/candlestick-chart', { |
| 134 |
edit: function ( props ) { |
| 135 |
const { attributes: a, setAttributes } = props; |
| 136 |
const blockProps = useBlockProps( { className: 'bkbg-candle-wrap', style: _tv(a.typoTitle, '--bkbg-candle-tt-') } ); |
| 137 |
|
| 138 |
return el( 'div', blockProps, |
| 139 |
el( InspectorControls, {}, |
| 140 |
|
| 141 |
el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true }, |
| 142 |
el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ), |
| 143 |
el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ), |
| 144 |
el( ToggleControl, { label: __( 'Show Grid', 'blockenberg' ), checked: a.showGrid, onChange: v => setAttributes( { showGrid: v } ) } ), |
| 145 |
el( ToggleControl, { label: __( 'Show Volume Bars', 'blockenberg' ), checked: a.showVolume, onChange: v => setAttributes( { showVolume: v } ) } ), |
| 146 |
), |
| 147 |
|
| 148 |
el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false }, |
| 149 |
el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ), |
| 150 |
el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 200, max: 800 } ), |
| 151 |
el( RangeControl, { label: __( 'Candle Width', 'blockenberg' ), value: a.candleWidth, onChange: v => setAttributes( { candleWidth: v } ), min: 4, max: 40 } ), |
| 152 |
el( RangeControl, { label: __( 'Wick Width', 'blockenberg' ), value: a.wickWidth, onChange: v => setAttributes( { wickWidth: v } ), min: 1, max: 6 } ), |
| 153 |
el( RangeControl, { label: __( 'Left Padding', 'blockenberg' ), value: a.padLeft, onChange: v => setAttributes( { padLeft: v } ), min: 30, max: 120 } ), |
| 154 |
), |
| 155 |
|
| 156 |
|
| 157 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 158 |
el( 'p', { style: { margin: '0 0 4px', fontWeight: 600, fontSize: '11px', textTransform: 'uppercase', color: '#888' } }, __( 'Title', 'blockenberg' ) ), |
| 159 |
el( getTypographyControl(), { |
| 160 |
label: __( 'Title Typography', 'blockenberg' ), |
| 161 |
value: a.typoTitle, |
| 162 |
onChange: v => setAttributes( { typoTitle: v } ) |
| 163 |
} ), |
| 164 |
el( 'hr', {} ), |
| 165 |
el( RangeControl, { label: __( 'SVG Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 18 } ) |
| 166 |
), |
| 167 |
el( PanelColorSettings, { |
| 168 |
title: __( 'Colors', 'blockenberg' ), initialOpen: false, |
| 169 |
colorSettings: [ |
| 170 |
{ label: __( 'Bullish (up)', 'blockenberg' ), value: a.bullColor, onChange: v => setAttributes( { bullColor: v || '#10b981' } ) }, |
| 171 |
{ label: __( 'Bearish (down)','blockenberg' ), value: a.bearColor, onChange: v => setAttributes( { bearColor: v || '#ef4444' } ) }, |
| 172 |
{ label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) }, |
| 173 |
{ label: __( 'Grid', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) }, |
| 174 |
{ label: __( 'Title', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) }, |
| 175 |
{ label: __( 'Text/Axes', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) }, |
| 176 |
] |
| 177 |
} ), |
| 178 |
|
| 179 |
el( PanelBody, { title: __( 'Candles', 'blockenberg' ), initialOpen: false }, |
| 180 |
el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => { |
| 181 |
const last = a.candles[ a.candles.length - 1 ] || {}; |
| 182 |
setAttributes( { candles: [ ...a.candles, { label: 'New', open: last.close || 150, high: ( last.close || 150 ) + 5, low: ( last.close || 150 ) - 4, close: ( last.close || 150 ) + 2, volume: 300 } ] } ); |
| 183 |
} }, __( '+ Add Candle', 'blockenberg' ) ), |
| 184 |
a.candles.map( ( c, ci ) => |
| 185 |
el( PanelBody, { key: ci, title: c.label || `Candle ${ ci + 1 }`, initialOpen: false }, |
| 186 |
el( TextControl, { label: __( 'Label', 'blockenberg' ), value: c.label, onChange: v => updCandle( setAttributes, a.candles, ci, 'label', v ) } ), |
| 187 |
el( TextControl, { label: __( 'Open', 'blockenberg' ), value: String( c.open ), type: 'number', onChange: v => updCandle( setAttributes, a.candles, ci, 'open', parseFloat( v ) || 0 ) } ), |
| 188 |
el( TextControl, { label: __( 'High', 'blockenberg' ), value: String( c.high ), type: 'number', onChange: v => updCandle( setAttributes, a.candles, ci, 'high', parseFloat( v ) || 0 ) } ), |
| 189 |
el( TextControl, { label: __( 'Low', 'blockenberg' ), value: String( c.low ), type: 'number', onChange: v => updCandle( setAttributes, a.candles, ci, 'low', parseFloat( v ) || 0 ) } ), |
| 190 |
el( TextControl, { label: __( 'Close', 'blockenberg' ), value: String( c.close ), type: 'number', onChange: v => updCandle( setAttributes, a.candles, ci, 'close', parseFloat( v ) || 0 ) } ), |
| 191 |
el( TextControl, { label: __( 'Volume','blockenberg' ), value: String( c.volume || 0 ), type: 'number', onChange: v => updCandle( setAttributes, a.candles, ci, 'volume', parseInt( v ) || 0 ) } ), |
| 192 |
el( Button, { isDestructive: true, isSmall: true, style: { marginTop: 6 }, onClick: () => setAttributes( { candles: a.candles.filter( ( _, x ) => x !== ci ) } ) }, __( 'Remove', 'blockenberg' ) ), |
| 193 |
) |
| 194 |
), |
| 195 |
), |
| 196 |
), |
| 197 |
|
| 198 |
a.showTitle && a.title && el( 'h3', { className: 'bkbg-candle-title', style: { color: a.titleColor } }, a.title ), |
| 199 |
el( 'div', { className: 'bkbg-candle-svg' }, renderCandlestick( a ) ), |
| 200 |
); |
| 201 |
}, |
| 202 |
|
| 203 |
save: function ( { attributes: a } ) { |
| 204 |
const blockProps = useBlockProps.save( { className: 'bkbg-candle-wrap', style: _tv(a.typoTitle, '--bkbg-candle-tt-') } ); |
| 205 |
return el( 'div', blockProps, |
| 206 |
a.showTitle && a.title ? el( 'h3', { className: 'bkbg-candle-title', style: { color: a.titleColor } }, a.title ) : null, |
| 207 |
el( 'div', { className: 'bkbg-candle-svg' }, renderCandlestick( a ) ), |
| 208 |
); |
| 209 |
}, |
| 210 |
} ); |
| 211 |
}() ); |
| 212 |
|