| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var useState = wp.element.useState; |
| 4 |
var Fragment = wp.element.Fragment; |
| 5 |
var registerBlockType = wp.blocks.registerBlockType; |
| 6 |
var __ = wp.i18n.__; |
| 7 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 8 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 9 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 10 |
var PanelBody = wp.components.PanelBody; |
| 11 |
var RangeControl = wp.components.RangeControl; |
| 12 |
var SelectControl = wp.components.SelectControl; |
| 13 |
var TextControl = wp.components.TextControl; |
| 14 |
var ToggleControl = wp.components.ToggleControl; |
| 15 |
|
| 16 |
function getTypographyControl() { return window.bkbgTypographyControl; } |
| 17 |
function getTypoCssVars() { return window.bkbgTypoCssVars; } |
| 18 |
|
| 19 |
var TABS = [ |
| 20 |
{id:'binary', label:'Binary'}, |
| 21 |
{id:'hex', label:'Hex'}, |
| 22 |
{id:'octal', label:'Octal'} |
| 23 |
]; |
| 24 |
|
| 25 |
registerBlockType('blockenberg/binary-text-converter', { |
| 26 |
edit: function (props) { |
| 27 |
var attributes = props.attributes; |
| 28 |
var setAttributes = props.setAttributes; |
| 29 |
var activeTab = useState(attributes.defaultTab || 'binary'); |
| 30 |
var tab = activeTab[0]; var setTab = activeTab[1]; |
| 31 |
|
| 32 |
var _tv = getTypoCssVars(); |
| 33 |
var bpStyle = { |
| 34 |
background: attributes.sectionBg, |
| 35 |
padding: attributes.paddingTop+'px 24px '+attributes.paddingBottom+'px' |
| 36 |
}; |
| 37 |
if (_tv) { |
| 38 |
Object.assign(bpStyle, _tv(attributes.titleTypo || {}, '--bkbg-btc-title-')); |
| 39 |
Object.assign(bpStyle, _tv(attributes.subtitleTypo || {}, '--bkbg-btc-sub-')); |
| 40 |
} |
| 41 |
var blockProps = useBlockProps({ style: bpStyle }); |
| 42 |
|
| 43 |
function toggle(key){ return el(ToggleControl,{__nextHasNoMarginBottom:true,label:key,checked:attributes[key],onChange:function(v){var o={};o[key]=v;setAttributes(o);}}); } |
| 44 |
function range(label,key,min,max,step){ return el(RangeControl,{__nextHasNoMarginBottom:true,label:label,value:attributes[key],min:min,max:max,step:step||1,onChange:function(v){var o={};o[key]=v;setAttributes(o);}}); } |
| 45 |
|
| 46 |
var tabColors = { binary: attributes.binaryColor, hex: attributes.hexColor, octal: attributes.octalColor }; |
| 47 |
|
| 48 |
var colors = [ |
| 49 |
{label:'Accent', value:attributes.accentColor, onChange:function(v){setAttributes({accentColor:v});}}, |
| 50 |
{label:'Binary Tab', value:attributes.binaryColor, onChange:function(v){setAttributes({binaryColor:v});}}, |
| 51 |
{label:'Hex Tab', value:attributes.hexColor, onChange:function(v){setAttributes({hexColor:v});}}, |
| 52 |
{label:'Octal Tab', value:attributes.octalColor, onChange:function(v){setAttributes({octalColor:v});}}, |
| 53 |
{label:'ASCII Tab', value:attributes.asciiColor, onChange:function(v){setAttributes({asciiColor:v});}}, |
| 54 |
{label:'Section Background', value:attributes.sectionBg, onChange:function(v){setAttributes({sectionBg:v});}}, |
| 55 |
{label:'Card Background', value:attributes.cardBg, onChange:function(v){setAttributes({cardBg:v});}}, |
| 56 |
{label:'Title', value:attributes.titleColor, onChange:function(v){setAttributes({titleColor:v});}}, |
| 57 |
{label:'Subtitle', value:attributes.subtitleColor, onChange:function(v){setAttributes({subtitleColor:v});}}, |
| 58 |
{label:'Label', value:attributes.labelColor, onChange:function(v){setAttributes({labelColor:v});}}, |
| 59 |
{label:'Input Background',value:attributes.inputBg, onChange:function(v){setAttributes({inputBg:v});}}, |
| 60 |
{label:'Input Border', value:attributes.inputBorder, onChange:function(v){setAttributes({inputBorder:v});}}, |
| 61 |
{label:'Output Background',value:attributes.outputBg, onChange:function(v){setAttributes({outputBg:v});}}, |
| 62 |
{label:'Output Border', value:attributes.outputBorder, onChange:function(v){setAttributes({outputBorder:v});}}, |
| 63 |
{label:'Error Text', value:attributes.errorColor, onChange:function(v){setAttributes({errorColor:v})}}, |
| 64 |
]; |
| 65 |
|
| 66 |
var activeColor = tabColors[tab] || attributes.accentColor; |
| 67 |
|
| 68 |
return el(Fragment, null, |
| 69 |
el(InspectorControls, null, |
| 70 |
el(PanelBody, {title:'Content', initialOpen:true}, |
| 71 |
toggle('showTitle'), |
| 72 |
attributes.showTitle && el(TextControl,{__nextHasNoMarginBottom:true,label:'Title',value:attributes.title,onChange:function(v){setAttributes({title:v});}}), |
| 73 |
toggle('showSubtitle'), |
| 74 |
attributes.showSubtitle && el(TextControl,{__nextHasNoMarginBottom:true,label:'Subtitle',value:attributes.subtitle,onChange:function(v){setAttributes({subtitle:v});}}), |
| 75 |
el(SelectControl,{__nextHasNoMarginBottom:true,label:'Default Tab',value:attributes.defaultTab, |
| 76 |
options:TABS.map(function(t){return {label:t.label,value:t.id};}), |
| 77 |
onChange:function(v){setAttributes({defaultTab:v});}}), |
| 78 |
el(SelectControl,{__nextHasNoMarginBottom:true,label:'Default Mode',value:attributes.defaultMode, |
| 79 |
options:[{label:'Encode (Text → Code)',value:'encode'},{label:'Decode (Code → Text)',value:'decode'}], |
| 80 |
onChange:function(v){setAttributes({defaultMode:v});}}) |
| 81 |
), |
| 82 |
el(PanelBody, {title:'ASCII Table', initialOpen:false}, |
| 83 |
toggle('showAsciiTable'), |
| 84 |
range('Group by (columns)', 'groupAscii', 4, 16) |
| 85 |
), |
| 86 |
el(PanelColorSettings, {title:'Colors', initialOpen:false, colorSettings:colors}), |
| 87 |
el(PanelBody, {title:'Sizing', initialOpen:false}, |
| 88 |
range('Max Width (px)', 'maxWidth', 400, 1200, 10), |
| 89 |
range('Card Radius (px)', 'cardRadius', 0, 32), |
| 90 |
range('Tab Radius (px)', 'tabRadius', 0, 24), |
| 91 |
range('Input Radius (px)', 'inputRadius', 0, 24), |
| 92 |
range('Padding Top (px)', 'paddingTop', 0, 120, 4), |
| 93 |
range('Padding Bottom (px)', 'paddingBottom', 0, 120, 4) |
| 94 |
), |
| 95 |
el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false }, |
| 96 |
(function () { var TC = getTypographyControl(); return TC ? el(TC, { label: __('Title', 'blockenberg'), value: attributes.titleTypo || {}, onChange: function (v) { setAttributes({ titleTypo: v }); } }) : null; })(), |
| 97 |
(function () { var TC = getTypographyControl(); return TC ? el(TC, { label: __('Subtitle', 'blockenberg'), value: attributes.subtitleTypo || {}, onChange: function (v) { setAttributes({ subtitleTypo: v }); } }) : null; })() |
| 98 |
) |
| 99 |
), |
| 100 |
el('div', blockProps, |
| 101 |
el('div', { |
| 102 |
style:{ |
| 103 |
background:attributes.cardBg, borderRadius:attributes.cardRadius+'px', |
| 104 |
padding:'40px', maxWidth:attributes.maxWidth+'px', |
| 105 |
margin:'0 auto', boxShadow:'0 4px 24px rgba(0,0,0,0.08)' |
| 106 |
} |
| 107 |
}, |
| 108 |
attributes.showTitle && el('div',{className:'bkbg-btc-title',style:{color:attributes.titleColor}},attributes.title), |
| 109 |
attributes.showSubtitle && el('div',{className:'bkbg-btc-subtitle',style:{color:attributes.subtitleColor}},attributes.subtitle), |
| 110 |
// Tabs row |
| 111 |
el('div',{style:{display:'flex',gap:'8px',marginBottom:'24px'}}, |
| 112 |
TABS.map(function(t){ |
| 113 |
var isActive=t.id===tab; |
| 114 |
var c=tabColors[t.id]||activeColor; |
| 115 |
return el('button',{ |
| 116 |
key:t.id, |
| 117 |
onClick:function(){setTab(t.id);}, |
| 118 |
style:{ |
| 119 |
flex:'1',padding:'10px',fontSize:'14px',fontWeight:'700', |
| 120 |
border:'2px solid '+(isActive?c:attributes.inputBorder), |
| 121 |
borderRadius:attributes.tabRadius+'px', |
| 122 |
background:isActive?c:'transparent', |
| 123 |
color:isActive?'#fff':c,cursor:'pointer',transition:'all 0.2s' |
| 124 |
} |
| 125 |
},t.label); |
| 126 |
}) |
| 127 |
), |
| 128 |
// Preview areas |
| 129 |
el('div',{style:{background:attributes.inputBg,border:'1.5px solid '+attributes.inputBorder,borderRadius:attributes.inputRadius+'px',padding:'14px',marginBottom:'12px',fontFamily:'monospace',fontSize:'14px',color:'#9ca3af',minHeight:'60px'}}, |
| 130 |
'Input text will appear here…'), |
| 131 |
el('div',{style:{textAlign:'center',color:activeColor,fontWeight:'700',fontSize:'14px',marginBottom:'12px'}},'↓ '+tab.toUpperCase()), |
| 132 |
el('div',{style:{background:attributes.outputBg,border:'1.5px solid '+attributes.outputBorder,borderRadius:attributes.inputRadius+'px',padding:'14px',fontFamily:'monospace',fontSize:'13px',color:'#6b7280',minHeight:'60px'}}, |
| 133 |
'Encoded/decoded result will appear here…') |
| 134 |
) |
| 135 |
) |
| 136 |
); |
| 137 |
}, |
| 138 |
save: function (props) { |
| 139 |
var a = props.attributes; |
| 140 |
return el('div', wp.blockEditor.useBlockProps.save(), |
| 141 |
el('div', { |
| 142 |
className: 'bkbg-btc-app', |
| 143 |
'data-opts': JSON.stringify(a) |
| 144 |
}) |
| 145 |
); |
| 146 |
} |
| 147 |
}); |
| 148 |
}() ); |
| 149 |
|