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 / number-base-converter / index.js

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

161 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 TextControl = wp.components.TextControl;
13 var ToggleControl = wp.components.ToggleControl;
14
15 var _tc, _tv;
16 function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); }
17 function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); }
18
19 var BASES = [
20 {key:'binary', label:'Binary', base:2, prefix:'0b', color:'binaryColor', dfltColor:'#3b82f6', validate:/^[01]*$/, hint:'0–1'},
21 {key:'octal', label:'Octal', base:8, prefix:'0o', color:'octalColor', dfltColor:'#f59e0b', validate:/^[0-7]*$/, hint:'0–7'},
22 {key:'decimal', label:'Decimal', base:10, prefix:'', color:'decimalColor', dfltColor:'#6c3fb5', validate:/^[0-9]*$/, hint:'0–9'},
23 {key:'hex', label:'Hexadecimal', base:16, prefix:'0x', color:'hexColor', dfltColor:'#10b981', validate:/^[0-9A-Fa-f]*$/, hint:'0–9 A–F'}
24 ];
25
26 function toDecimal(str, base) { return str ? parseInt(str, base) : NaN; }
27 function fromDecimal(n, base) {
28 if (isNaN(n) || n < 0) return '';
29 return n.toString(base).toUpperCase();
30 }
31 function bitLen(n, bits) { return isNaN(n) ? '' : (n >>> 0).toString(2).padStart(bits, '0'); }
32
33 function NBCPreview(props) {
34 var a = props.attributes;
35 var _vals = useState({binary:'11111111', octal:'377', decimal:'255', hex:'FF'});
36 var vals = _vals[0]; var setVals = _vals[1];
37 var _active = useState('decimal');
38 var active = _active[0]; var setActive = _active[1];
39
40 function handleChange(key, base, raw) {
41 var clean = raw.replace(/\s/g,'').toUpperCase();
42 var binfo = BASES.find(function(b){ return b.key===key; });
43 if (!binfo.validate.test(clean) && clean !== '') return;
44 var dec = toDecimal(clean, base);
45 var next = {};
46 BASES.forEach(function(b){ next[b.key] = clean===''?'':fromDecimal(dec, b.base); });
47 if (clean === '') next = {binary:'',octal:'',decimal:'',hex:''};
48 next[key] = clean;
49 setVals(next);
50 }
51
52 var inputStyle = {flex:'1',padding:'9px 12px',borderRadius:'0 8px 8px 0',border:'1.5px solid '+(a.inputBorder||'#e5e7eb'),borderLeft:'none',fontSize:(a.valueFontSize||18)+'px',fontFamily:'monospace',outline:'none',background:'#fff',width:'1px',minWidth:0};
53
54 function row(binfo) {
55 var color = a[binfo.color] || binfo.dfltColor;
56 return el('div',{key:binfo.key,style:{display:'flex',alignItems:'center',background:a.rowBg||'#f3f4f6',border:'1.5px solid '+(a.rowBorder||'#e5e7eb'),borderRadius:(a.inputRadius||8)+'px',overflow:'hidden',marginBottom:'8px'}},
57 el('div',{style:{background:color,color:'#fff',padding:'0 14px',display:'flex',alignItems:'center',justifyContent:'center',flexDirection:'column',minWidth:'80px',height:'52px'}},
58 el('div',{style:{fontSize:'11px',fontWeight:700,opacity:.8,textTransform:'uppercase',letterSpacing:'.06em'}},binfo.hint),
59 el('div',{style:{fontSize:'13px',fontWeight:800}},binfo.label)
60 ),
61 el('input',{type:'text',value:vals[binfo.key],onChange:function(e){setActive(binfo.key);handleChange(binfo.key,binfo.base,e.target.value);},onFocus:function(){setActive(binfo.key);},style:inputStyle,placeholder:''}),
62 a.showCopyButtons && el('button',{onClick:function(){if(navigator.clipboard)navigator.clipboard.writeText(vals[binfo.key]||'');},type:'button',style:{border:'none',background:color+'22',color:color,padding:'0 12px',cursor:'pointer',fontSize:'13px',fontWeight:700,height:'52px',flexShrink:0}},'Copy')
63 );
64 }
65
66 var dec = toDecimal(vals.decimal, 10);
67
68 return el('div',{style:{paddingTop:(a.paddingTop||60)+'px',paddingBottom:(a.paddingBottom||60)+'px',background:a.sectionBg||undefined}},
69 el('div',{style:{background:a.cardBg||'#fff',borderRadius:(a.cardRadius||16)+'px',padding:'36px 32px',maxWidth:(a.maxWidth||560)+'px',margin:'0 auto',boxShadow:'0 4px 24px rgba(0,0,0,.09)'}},
70
71 (a.showTitle||a.showSubtitle) && el('div',{style:{marginBottom:'22px'}},
72 a.showTitle && el('div',{className:'bkbg-nbc-title',style:{color:a.titleColor,marginBottom:'6px'}},a.title),
73 a.showSubtitle && el('div',{className:'bkbg-nbc-subtitle',style:{color:a.subtitleColor,opacity:.75}},a.subtitle)
74 ),
75
76 a.showDecimal && row(BASES[2]),
77 a.showBinary && row(BASES[0]),
78 a.showOctal && row(BASES[1]),
79 a.showHex && row(BASES[3]),
80
81 a.showBitLength && !isNaN(dec) && el('div',{style:{marginTop:'16px',background:a.rowBg||'#f3f4f6',border:'1px solid '+(a.rowBorder||'#e5e7eb'),borderRadius:'10px',padding:'14px 16px'}},
82 el('div',{style:{fontSize:'12px',fontWeight:700,color:a.labelColor||'#374151',textTransform:'uppercase',letterSpacing:'.05em',marginBottom:'10px'}},'Bit Lengths'),
83 el('div',{style:{display:'flex',flexDirection:'column',gap:'6px',fontFamily:'monospace',fontSize:'13px'}},
84 [{bits:8,label:'8-bit'},{bits:16,label:'16-bit'},{bits:32,label:'32-bit'}].map(function(row){
85 return el('div',{key:row.label,style:{display:'flex',alignItems:'center',gap:'10px'}},
86 el('span',{style:{fontWeight:700,color:a.labelColor||'#374151',minWidth:'40px'}},row.label),
87 el('span',{style:{color:'#111827',wordBreak:'break-all'}},bitLen(dec, row.bits))
88 );
89 })
90 )
91 )
92 )
93 );
94 }
95
96 registerBlockType('blockenberg/number-base-converter', {
97 edit: function(props) {
98 var a = props.attributes; var set = props.setAttributes;
99 var blockProps = useBlockProps((function () {
100 var _tvf = getTypoCssVars();
101 var s = {};
102 if (_tvf) { Object.assign(s, _tvf(a.titleTypo, '--bkbg-nbc-tt-')); Object.assign(s, _tvf(a.subtitleTypo, '--bkbg-nbc-st-')); }
103 return { style: s };
104 })());
105 var colorSettings = [
106 {value:a.binaryColor, onChange:function(v){set({binaryColor:v});}, label:'Binary Color (row accent)'},
107 {value:a.octalColor, onChange:function(v){set({octalColor:v});}, label:'Octal Color'},
108 {value:a.decimalColor, onChange:function(v){set({decimalColor:v});}, label:'Decimal Color'},
109 {value:a.hexColor, onChange:function(v){set({hexColor:v});}, label:'Hex Color'},
110 {value:a.cardBg, onChange:function(v){set({cardBg:v});}, label:'Card Background'},
111 {value:a.rowBg, onChange:function(v){set({rowBg:v});}, label:'Row Background'},
112 {value:a.rowBorder, onChange:function(v){set({rowBorder:v});}, label:'Row Border'},
113 {value:a.inputBorder, onChange:function(v){set({inputBorder:v});}, label:'Input Border'},
114 {value:a.labelColor, onChange:function(v){set({labelColor:v});}, label:'Label Color'},
115 {value:a.titleColor, onChange:function(v){set({titleColor:v});}, label:'Title Color'},
116 {value:a.subtitleColor,onChange:function(v){set({subtitleColor:v});},label:'Subtitle Color'},
117 {value:a.sectionBg, onChange:function(v){set({sectionBg:v});}, label:'Section Background'}
118 ];
119 return el(Fragment, null,
120 el(InspectorControls, null,
121 el(PanelBody,{title:'Header',initialOpen:false},
122 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Title', checked:a.showTitle, onChange:function(v){set({showTitle:v});}}),
123 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Subtitle',checked:a.showSubtitle,onChange:function(v){set({showSubtitle:v});}}),
124 el(TextControl,{label:'Title', value:a.title, onChange:function(v){set({title:v});}}),
125 el(TextControl,{label:'Subtitle',value:a.subtitle,onChange:function(v){set({subtitle:v});}})
126 ),
127 el(PanelBody,{title:'Converter Settings',initialOpen:true},
128 el(TextControl,{label:'Default Value (decimal)',value:a.defaultValue,onChange:function(v){set({defaultValue:v});}}),
129 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Binary', checked:a.showBinary, onChange:function(v){set({showBinary:v});}}),
130 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Octal', checked:a.showOctal, onChange:function(v){set({showOctal:v});}}),
131 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Decimal', checked:a.showDecimal, onChange:function(v){set({showDecimal:v});}}),
132 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Hexadecimal', checked:a.showHex, onChange:function(v){set({showHex:v});}}),
133 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Bit Lengths', checked:a.showBitLength, onChange:function(v){set({showBitLength:v});}}),
134 el(ToggleControl,{__nextHasNoMarginBottom:true,label:'Show Copy Buttons', checked:a.showCopyButtons, onChange:function(v){set({showCopyButtons:v});}})
135 ),
136
137 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
138 el(getTypoControl(), {label:'Title Typography',value:a.titleTypo,onChange:function(v){set({titleTypo:v});}}),
139 el(getTypoControl(), {label:'Subtitle Typography',value:a.subtitleTypo,onChange:function(v){set({subtitleTypo:v});}}),
140 el(RangeControl,{label:'Value Font Size', value:a.valueFontSize, min:12,max:32,step:1, onChange:function(v){set({valueFontSize:v});}, __nextHasNoMarginBottom:true})
141 ),
142 el(PanelColorSettings,{title:'Colors',initialOpen:false,colorSettings:colorSettings}),
143 el(PanelBody,{title:'Sizing & Layout',initialOpen:false},
144 el(RangeControl,{label:'Card Border Radius', value:a.cardRadius, min:0, max:40,step:1, onChange:function(v){set({cardRadius:v});}}),
145 el(RangeControl,{label:'Input Border Radius',value:a.inputRadius, min:0, max:20,step:1, onChange:function(v){set({inputRadius:v});}}),
146 el(RangeControl,{label:'Max Width (px)', value:a.maxWidth, min:320,max:960,step:10,onChange:function(v){set({maxWidth:v});}}),
147 el(RangeControl,{label:'Padding Top (px)', value:a.paddingTop, min:0, max:160,step:4, onChange:function(v){set({paddingTop:v});}}),
148 el(RangeControl,{label:'Padding Bottom (px)',value:a.paddingBottom, min:0, max:160,step:4, onChange:function(v){set({paddingBottom:v});}})
149 )
150 ),
151 el('div', blockProps, el(NBCPreview, {attributes:a}))
152 );
153 },
154 save: function(props) {
155 var a = props.attributes;
156 return el('div', wp.blockEditor.useBlockProps.save(),
157 el('div', {className:'bkbg-nbc-app','data-opts':JSON.stringify(a)}));
158 }
159 });
160 }() );
161