| 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; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 16 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 17 |
|
| 18 |
function toTitleCase(s) { |
| 19 |
return s.replace(/\w\S*/g, function(w){ return w.charAt(0).toUpperCase() + w.substr(1).toLowerCase(); }); |
| 20 |
} |
| 21 |
function toSentenceCase(s) { |
| 22 |
return s.toLowerCase().replace(/(^\s*\w|[.!?]\s*\w)/g, function(c){ return c.toUpperCase(); }); |
| 23 |
} |
| 24 |
function toCamelCase(s) { |
| 25 |
return s.replace(/[^a-zA-Z0-9]+(.)/g, function(_, c){ return c.toUpperCase(); }) |
| 26 |
.replace(/^[A-Z]/, function(c){ return c.toLowerCase(); }); |
| 27 |
} |
| 28 |
function toPascalCase(s) { |
| 29 |
var c = toCamelCase(s); |
| 30 |
return c.charAt(0).toUpperCase() + c.slice(1); |
| 31 |
} |
| 32 |
function toSnakeCase(s) { |
| 33 |
return s.replace(/\s+/g, '_').replace(/[^a-zA-Z0-9_]/g, '').replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase(); |
| 34 |
} |
| 35 |
function toKebabCase(s) { |
| 36 |
return s.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9-]/g, '').replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); |
| 37 |
} |
| 38 |
|
| 39 |
var CASES = [ |
| 40 |
{ key: 'showUppercase', label: 'UPPERCASE', fn: function(s){ return s.toUpperCase(); } }, |
| 41 |
{ key: 'showLowercase', label: 'lowercase', fn: function(s){ return s.toLowerCase(); } }, |
| 42 |
{ key: 'showTitleCase', label: 'Title Case', fn: toTitleCase }, |
| 43 |
{ key: 'showSentenceCase', label: 'Sentence case', fn: toSentenceCase }, |
| 44 |
{ key: 'showCamelCase', label: 'camelCase', fn: toCamelCase }, |
| 45 |
{ key: 'showPascalCase', label: 'PascalCase', fn: toPascalCase }, |
| 46 |
{ key: 'showSnakeCase', label: 'snake_case', fn: toSnakeCase }, |
| 47 |
{ key: 'showKebabCase', label: 'kebab-case', fn: toKebabCase } |
| 48 |
]; |
| 49 |
|
| 50 |
function CaseConverterPreview(props) { |
| 51 |
var a = props.attributes; |
| 52 |
var accent = a.accentColor || '#6c3fb5'; |
| 53 |
var _t = useState('The quick brown fox jumps over the lazy dog'); var text = _t[0]; var setText = _t[1]; |
| 54 |
var _copied = useState(null); var copied = _copied[0]; var setCopied = _copied[1]; |
| 55 |
|
| 56 |
function copyText(key, val) { |
| 57 |
if (!val) return; |
| 58 |
try { navigator.clipboard.writeText(val).then(function(){ setCopied(key); setTimeout(function(){ setCopied(null); }, 1500); }); } catch(e){} |
| 59 |
} |
| 60 |
|
| 61 |
return el('div', { style:{paddingTop:a.paddingTop+'px',paddingBottom:a.paddingBottom+'px',background:a.sectionBg||undefined}}, |
| 62 |
el('div', { style:{background:a.cardBg,borderRadius:(a.cardRadius||16)+'px',padding:'36px 32px',maxWidth:(a.maxWidth||640)+'px',margin:'0 auto',boxShadow:'0 4px 24px rgba(0,0,0,0.09)'}}, |
| 63 |
|
| 64 |
(a.showTitle||a.showSubtitle) && el('div', { style:{marginBottom:'22px'}}, |
| 65 |
a.showTitle && el('div', { className:'bkbg-tcc-title', style:{color:a.titleColor,marginBottom:'6px'}}, a.title), |
| 66 |
a.showSubtitle && el('div', { className:'bkbg-tcc-subtitle', style:{color:a.subtitleColor,opacity:.75}}, a.subtitle) |
| 67 |
), |
| 68 |
|
| 69 |
el('textarea', { |
| 70 |
value: text, |
| 71 |
placeholder: a.placeholder, |
| 72 |
onChange: function(e){ setText(e.target.value); }, |
| 73 |
rows: 4, |
| 74 |
style:{width:'100%',padding:'14px',fontSize:'15px',fontFamily:'inherit',borderRadius:(a.rowRadius||10)+'px',border:'1.5px solid '+(a.inputBorder||'#e5e7eb'),background:a.inputBg||'#f9fafb',outline:'none',resize:'vertical',boxSizing:'border-box',marginBottom:'20px'} |
| 75 |
}), |
| 76 |
|
| 77 |
el('div', { style:{display:'flex',flexDirection:'column',gap:'10px'}}, |
| 78 |
CASES.filter(function(c){ return a[c.key]; }).map(function(c) { |
| 79 |
var result = text ? c.fn(text) : ''; |
| 80 |
var isCopied = copied === c.key; |
| 81 |
return el('div', { key:c.key, style:{display:'flex',alignItems:'center',gap:'12px',background:a.rowBg||'#f9fafb',border:'1.5px solid '+(a.rowBorder||'#e5e7eb'),borderRadius:(a.rowRadius||10)+'px',padding:'12px 16px'}}, |
| 82 |
el('div', { style:{flex:'0 0 110px',fontSize:'12px',fontWeight:700,color:a.rowLabelColor||'#6b7280',textTransform:'uppercase',letterSpacing:'.04em'}}, c.label), |
| 83 |
el('div', { style:{flex:1,fontSize:'14px',color:a.rowValueColor||'#1f2937',wordBreak:'break-all',fontFamily:'monospace'}}, result || el('span', { style:{color:'#d1d5db',fontStyle:'italic'}}, 'No text entered')), |
| 84 |
a.showCopyButtons && el('button', { |
| 85 |
onClick: function(){ copyText(c.key, result); }, |
| 86 |
style:{flexShrink:0,padding:'6px 14px',borderRadius:'6px',border:'none',background:isCopied?'#10b981':(a.copyBg||accent),color:a.copyColor||'#fff',fontSize:'12px',fontWeight:600,cursor:'pointer',fontFamily:'inherit',transition:'background .2s'} |
| 87 |
}, isCopied ? '✓ Copied' : 'Copy') |
| 88 |
); |
| 89 |
}) |
| 90 |
) |
| 91 |
) |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
registerBlockType('blockenberg/text-case-converter', { |
| 96 |
edit: function(props) { |
| 97 |
var a = props.attributes; var set = props.setAttributes; |
| 98 |
var blockProps = useBlockProps((function () { |
| 99 |
var s = {}; |
| 100 |
var _tvf = getTypoCssVars(); |
| 101 |
if (_tvf) { Object.assign(s, _tvf(a.titleTypo, '--bktcc-tt-'), _tvf(a.subtitleTypo, '--bktcc-st-')); } |
| 102 |
return { style: s }; |
| 103 |
})()); |
| 104 |
var colorSettings = [ |
| 105 |
{ value: a.accentColor, onChange: function(v){ set({accentColor:v}); }, label: 'Accent / Copy Button' }, |
| 106 |
{ value: a.cardBg, onChange: function(v){ set({cardBg:v}); }, label: 'Card Background' }, |
| 107 |
{ value: a.inputBg, onChange: function(v){ set({inputBg:v}); }, label: 'Input Background' }, |
| 108 |
{ value: a.inputBorder, onChange: function(v){ set({inputBorder:v}); }, label: 'Input Border' }, |
| 109 |
{ value: a.rowBg, onChange: function(v){ set({rowBg:v}); }, label: 'Row Background' }, |
| 110 |
{ value: a.rowBorder, onChange: function(v){ set({rowBorder:v}); }, label: 'Row Border' }, |
| 111 |
{ value: a.rowLabelColor, onChange: function(v){ set({rowLabelColor:v}); }, label: 'Row Label' }, |
| 112 |
{ value: a.rowValueColor, onChange: function(v){ set({rowValueColor:v}); }, label: 'Row Value' }, |
| 113 |
{ value: a.copyBg, onChange: function(v){ set({copyBg:v}); }, label: 'Copy Button Background' }, |
| 114 |
{ value: a.copyColor, onChange: function(v){ set({copyColor:v}); }, label: 'Copy Button Text' }, |
| 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 |
el(TextControl, { label: 'Input Placeholder', value: a.placeholder, onChange: function(v){ set({placeholder:v}); } }) |
| 127 |
), |
| 128 |
el(PanelBody, { title: 'Case Options', initialOpen: true }, |
| 129 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'UPPERCASE', checked: a.showUppercase, onChange: function(v){ set({showUppercase:v}); } }), |
| 130 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'lowercase', checked: a.showLowercase, onChange: function(v){ set({showLowercase:v}); } }), |
| 131 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Title Case', checked: a.showTitleCase, onChange: function(v){ set({showTitleCase:v}); } }), |
| 132 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Sentence case', checked: a.showSentenceCase, onChange: function(v){ set({showSentenceCase:v}); } }), |
| 133 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'camelCase', checked: a.showCamelCase, onChange: function(v){ set({showCamelCase:v}); } }), |
| 134 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'PascalCase', checked: a.showPascalCase, onChange: function(v){ set({showPascalCase:v}); } }), |
| 135 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'snake_case', checked: a.showSnakeCase, onChange: function(v){ set({showSnakeCase:v}); } }), |
| 136 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'kebab-case', checked: a.showKebabCase, onChange: function(v){ set({showKebabCase:v}); } }), |
| 137 |
el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Copy Buttons', checked: a.showCopyButtons, onChange: function(v){ set({showCopyButtons:v}); } }) |
| 138 |
), |
| 139 |
|
| 140 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 141 |
getTypoControl() && getTypoControl()({ |
| 142 |
label: __('Title', 'blockenberg'), |
| 143 |
value: a.titleTypo || {}, |
| 144 |
onChange: function (v) { set({ titleTypo: v }); } |
| 145 |
}), |
| 146 |
getTypoControl() && getTypoControl()({ |
| 147 |
label: __('Subtitle', 'blockenberg'), |
| 148 |
value: a.subtitleTypo || {}, |
| 149 |
onChange: function (v) { set({ subtitleTypo: v }); } |
| 150 |
}) |
| 151 |
), |
| 152 |
el(PanelColorSettings, { title: 'Colors', initialOpen: false, colorSettings: colorSettings }), |
| 153 |
el(PanelBody, { title: 'Sizing & Layout', initialOpen: false }, |
| 154 |
el(RangeControl, { label: 'Card Border Radius', value: a.cardRadius, min: 0, max: 40, step: 1, onChange: function(v){ set({cardRadius:v}); } }), |
| 155 |
el(RangeControl, { label: 'Row Border Radius', value: a.rowRadius, min: 0, max: 24, step: 1, onChange: function(v){ set({rowRadius:v}); } }), |
| 156 |
el(RangeControl, { label: 'Max Width (px)', value: a.maxWidth, min: 340,max: 960, step: 10, onChange: function(v){ set({maxWidth:v}); } }), |
| 157 |
el(RangeControl, { label: 'Padding Top (px)', value: a.paddingTop, min: 0, max: 160, step: 4, onChange: function(v){ set({paddingTop:v}); } }), |
| 158 |
el(RangeControl, { label: 'Padding Bottom (px)',value: a.paddingBottom, min: 0, max: 160, step: 4, onChange: function(v){ set({paddingBottom:v}); } }) |
| 159 |
) |
| 160 |
), |
| 161 |
el('div', blockProps, el(CaseConverterPreview, { attributes: a, setAttributes: set })) |
| 162 |
); |
| 163 |
}, |
| 164 |
save: function(props) { |
| 165 |
var a = props.attributes; |
| 166 |
var _tvf = getTypoCssVars(); |
| 167 |
return el('div', (function () { |
| 168 |
var s = {}; |
| 169 |
if (_tvf) { Object.assign(s, _tvf(a.titleTypo, '--bktcc-tt-'), _tvf(a.subtitleTypo, '--bktcc-st-')); } |
| 170 |
return wp.blockEditor.useBlockProps.save({ style: s }); |
| 171 |
})(), el('div', { className: 'bkbg-tcc-app', 'data-opts': JSON.stringify(a) })); |
| 172 |
} |
| 173 |
}); |
| 174 |
}() ); |
| 175 |
|