| 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 RichText = wp.blockEditor.RichText; |
| 11 |
var PanelBody = wp.components.PanelBody; |
| 12 |
var RangeControl = wp.components.RangeControl; |
| 13 |
var TextControl = wp.components.TextControl; |
| 14 |
var ToggleControl = wp.components.ToggleControl; |
| 15 |
|
| 16 |
var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 17 |
var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 18 |
|
| 19 |
var SAMPLE_TEXT = 'The quick brown fox jumps over the lazy dog. A fast, nimble fox leaps gracefully. The dog watches the fox, the fox watches the dog. Quick movements define the fox. Lazy afternoons define the dog.'; |
| 20 |
|
| 21 |
function analyze(text) { |
| 22 |
var trimmed = text.trim(); |
| 23 |
var words = trimmed ? trimmed.split(/\s+/).filter(function (w) { return w.length > 0; }) : []; |
| 24 |
var sentences = trimmed ? trimmed.split(/[.!?]+/).filter(function (s) { return s.trim().length > 0; }) : []; |
| 25 |
var paragraphs = trimmed ? trimmed.split(/\n\s*\n/).filter(function (p) { return p.trim().length > 0; }) : []; |
| 26 |
var chars = trimmed.length; |
| 27 |
var charsNoSp = trimmed.replace(/\s/g, '').length; |
| 28 |
var avgWordLen = words.length ? (words.map(function (w) { return w.replace(/[^a-zA-Z]/g,'').length; }).reduce(function (a,b){return a+b;},0) / words.length) : 0; |
| 29 |
var avgSentLen = sentences.length ? (words.length / sentences.length) : 0; |
| 30 |
var readingMins = words.length / 200; |
| 31 |
var uniqueWords = (function () { |
| 32 |
var s = {}; |
| 33 |
words.forEach(function (w) { s[w.toLowerCase().replace(/[^a-z]/g,'')] = true; }); |
| 34 |
return Object.keys(s).length; |
| 35 |
})(); |
| 36 |
var vocabRichness = words.length ? Math.round((uniqueWords / words.length) * 100) : 0; |
| 37 |
var longestWord = words.reduce(function (a, w) { return w.length > a.length ? w : a; }, ''); |
| 38 |
// Frequency: skip stop words for freq chart |
| 39 |
var STOP = { the:1,a:1,an:1,and:1,or:1,but:1,in:1,on:1,at:1,to:1,for:1,of:1,with:1,by:1,is:1,it:1,its:1,was:1,are:1,be:1,this:1,that:1,as:1 }; |
| 40 |
var freq = {}; |
| 41 |
words.forEach(function (w) { |
| 42 |
var k = w.toLowerCase().replace(/[^a-z']/g,''); |
| 43 |
if (k.length > 1 && !STOP[k]) freq[k] = (freq[k] || 0) + 1; |
| 44 |
}); |
| 45 |
var freqList = Object.keys(freq).map(function (k) { return { word:k, count:freq[k] }; }) |
| 46 |
.sort(function (a,b){ return b.count - a.count; }); |
| 47 |
return { words:words.length, sentences:sentences.length, paragraphs:paragraphs.length, chars:chars, charsNoSp:charsNoSp, avgWordLen:avgWordLen, avgSentLen:avgSentLen, readingMins:readingMins, uniqueWords:uniqueWords, vocabRichness:vocabRichness, longestWord:longestWord, freqList:freqList }; |
| 48 |
} |
| 49 |
|
| 50 |
function EditorPreview(props) { |
| 51 |
var a = props.attributes; |
| 52 |
var res = analyze(SAMPLE_TEXT); |
| 53 |
var topN = Math.min(a.freqTopN || 8, res.freqList.length); |
| 54 |
var maxFreq = topN > 0 ? res.freqList[0].count : 1; |
| 55 |
|
| 56 |
var wrapStyle = { |
| 57 |
background: a.sectionBg, |
| 58 |
borderRadius:'16px', |
| 59 |
padding: '32px 24px', |
| 60 |
maxWidth: a.contentMaxWidth + 'px', |
| 61 |
margin: '0 auto', |
| 62 |
fontFamily: 'inherit', |
| 63 |
boxSizing: 'border-box' |
| 64 |
}; |
| 65 |
|
| 66 |
function statCard(label, value, unit) { |
| 67 |
return el('div', { style:{ background:a.statBg, borderRadius:'12px', padding:'12px 16px', textAlign:'center', minWidth:'90px' } }, |
| 68 |
el('div', { style:{ fontSize:'24px', fontWeight:'800', color:a.statNumColor, lineHeight:'1.1' } }, value + (unit||'')), |
| 69 |
el('div', { style:{ fontSize:'11px', fontWeight:'600', textTransform:'uppercase', letterSpacing:'.06em', color:a.labelColor, marginTop:'4px' } }, label) |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
return el('div', { style: wrapStyle }, |
| 74 |
a.showTitle && el(RichText, { |
| 75 |
tagName:'h3', className:'bkbg-ta-title', value:a.title, |
| 76 |
onChange:function(v){ props.setAttributes({title:v}); }, |
| 77 |
style:{ color:a.titleColor, margin:'0 0 5px 0' }, |
| 78 |
placeholder:'Title…' |
| 79 |
}), |
| 80 |
a.showSubtitle && el(RichText, { |
| 81 |
tagName:'p', className:'bkbg-ta-subtitle', value:a.subtitle, |
| 82 |
onChange:function(v){ props.setAttributes({subtitle:v}); }, |
| 83 |
style:{ color:a.subtitleColor, margin:'0 0 14px 0' }, |
| 84 |
placeholder:'Subtitle…' |
| 85 |
}), |
| 86 |
|
| 87 |
// Textarea |
| 88 |
el('textarea', { rows:6, readOnly:true, defaultValue:SAMPLE_TEXT, |
| 89 |
style:{ width:'100%', boxSizing:'border-box', border:'1.5px solid #e5e7eb', borderRadius:'12px', padding:'14px 16px', fontSize:'14px', color:a.labelColor, background:a.inputBg, fontFamily:'inherit', lineHeight:'1.6', resize:'vertical', outline:'none', marginBottom:'18px' } |
| 90 |
}), |
| 91 |
|
| 92 |
// Basic stats |
| 93 |
a.showBasicStats && el('div', null, |
| 94 |
el('div', { style:{ fontSize:'12px', fontWeight:'700', textTransform:'uppercase', letterSpacing:'.06em', color:a.labelColor, marginBottom:'8px' } }, '📊 Basic Statistics'), |
| 95 |
el('div', { style:{ display:'flex', gap:'10px', flexWrap:'wrap', marginBottom:'18px' } }, |
| 96 |
statCard('Words', res.words, ''), |
| 97 |
statCard('Characters', res.chars, ''), |
| 98 |
statCard('No Spaces', res.charsNoSp, ''), |
| 99 |
statCard('Sentences', res.sentences, ''), |
| 100 |
statCard('Paragraphs', res.paragraphs, ''), |
| 101 |
statCard('Read', (res.readingMins < 1 ? '<1' : Math.ceil(res.readingMins)), ' min') |
| 102 |
) |
| 103 |
), |
| 104 |
|
| 105 |
// Vocab stats |
| 106 |
a.showVocabStats && el('div', null, |
| 107 |
el('div', { style:{ fontSize:'12px', fontWeight:'700', textTransform:'uppercase', letterSpacing:'.06em', color:a.labelColor, marginBottom:'8px' } }, '📚 Vocabulary Insights'), |
| 108 |
el('div', { style:{ display:'flex', gap:'10px', flexWrap:'wrap', marginBottom:'18px' } }, |
| 109 |
statCard('Unique Words', res.uniqueWords, ''), |
| 110 |
statCard('Vocabulary %', res.vocabRichness, '%'), |
| 111 |
statCard('Avg Word Len', res.avgWordLen.toFixed(1), ' ch'), |
| 112 |
statCard('Avg Sent Len', res.avgSentLen.toFixed(1), ' wds'), |
| 113 |
a.showLongestWord && statCard('Longest Word', res.longestWord, '') |
| 114 |
) |
| 115 |
), |
| 116 |
|
| 117 |
// Frequency |
| 118 |
a.showFrequency && res.freqList.length > 0 && el('div', null, |
| 119 |
el('div', { style:{ fontSize:'12px', fontWeight:'700', textTransform:'uppercase', letterSpacing:'.06em', color:a.labelColor, marginBottom:'10px' } }, '🔠 Top Words'), |
| 120 |
el('div', { style:{ display:'flex', flexDirection:'column', gap:'6px' } }, |
| 121 |
res.freqList.slice(0, topN).map(function (item) { |
| 122 |
return el('div', { key:item.word, style:{ display:'flex', alignItems:'center', gap:'10px' } }, |
| 123 |
el('div', { style:{ width:'90px', fontSize:'12px', fontWeight:'600', color:a.labelColor, textAlign:'right', flexShrink:0 } }, item.word), |
| 124 |
el('div', { style:{ flex:'1', background:'#f3f4f6', borderRadius:'6px', height:'18px', overflow:'hidden' } }, |
| 125 |
el('div', { style:{ width:Math.round((item.count/maxFreq)*100)+'%', height:'100%', background:a.barColor, borderRadius:'6px', transition:'width .3s' } }) |
| 126 |
), |
| 127 |
el('div', { style:{ width:'24px', fontSize:'12px', fontWeight:'700', color:a.statNumColor } }, item.count) |
| 128 |
); |
| 129 |
}) |
| 130 |
) |
| 131 |
) |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
registerBlockType('blockenberg/text-analyzer', { |
| 136 |
edit: function (props) { |
| 137 |
var a = props.attributes; |
| 138 |
var set = props.setAttributes; |
| 139 |
|
| 140 |
var colorSettings = [ |
| 141 |
{ value:a.accentColor, onChange:function(v){set({accentColor: v||'#8b5cf6'});}, label:'Accent color' }, |
| 142 |
{ value:a.barColor, onChange:function(v){set({barColor: v||'#8b5cf6'});}, label:'Frequency bar color' }, |
| 143 |
{ value:a.statBg, onChange:function(v){set({statBg: v||'#f5f3ff'});}, label:'Stat card background'}, |
| 144 |
{ value:a.statNumColor, onChange:function(v){set({statNumColor: v||'#6d28d9'});}, label:'Stat number color' }, |
| 145 |
{ value:a.sectionBg, onChange:function(v){set({sectionBg: v||'#faf5ff'});}, label:'Section background' }, |
| 146 |
{ value:a.cardBg, onChange:function(v){set({cardBg: v||'#ffffff'});}, label:'Card background' }, |
| 147 |
{ value:a.inputBg, onChange:function(v){set({inputBg: v||'#f8fafc'});}, label:'Textarea background' }, |
| 148 |
{ value:a.titleColor, onChange:function(v){set({titleColor: v||'#111827'});}, label:'Title color' }, |
| 149 |
{ value:a.subtitleColor, onChange:function(v){set({subtitleColor:v||'#6b7280'});}, label:'Subtitle color' }, |
| 150 |
{ value:a.labelColor, onChange:function(v){set({labelColor: v||'#374151'});}, label:'Label color' } |
| 151 |
]; |
| 152 |
|
| 153 |
return el(Fragment, null, |
| 154 |
el(InspectorControls, null, |
| 155 |
el(PanelBody, { title:'Analyzer Settings', initialOpen:true }, |
| 156 |
el(ToggleControl, { __nextHasNoMarginBottom:true, label:'Show title', checked:a.showTitle, onChange:function(v){set({showTitle:v});} }), |
| 157 |
el(ToggleControl, { __nextHasNoMarginBottom:true, label:'Show subtitle', checked:a.showSubtitle, onChange:function(v){set({showSubtitle:v});} }), |
| 158 |
el(ToggleControl, { __nextHasNoMarginBottom:true, label:'Show basic statistics', checked:a.showBasicStats, onChange:function(v){set({showBasicStats:v});} }), |
| 159 |
el(ToggleControl, { __nextHasNoMarginBottom:true, label:'Show vocabulary insights',checked:a.showVocabStats, onChange:function(v){set({showVocabStats:v});} }), |
| 160 |
el(ToggleControl, { __nextHasNoMarginBottom:true, label:'Show longest word', checked:a.showLongestWord,onChange:function(v){set({showLongestWord:v});} }), |
| 161 |
el(ToggleControl, { __nextHasNoMarginBottom:true, label:'Show word frequency chart',checked:a.showFrequency, onChange:function(v){set({showFrequency:v});} }), |
| 162 |
el(RangeControl, { label:'Top N words in chart', value:a.freqTopN, min:3, max:20, onChange:function(v){set({freqTopN:v});} }), |
| 163 |
el(TextControl, { label:'Placeholder text', value:a.placeholder, onChange:function(v){set({placeholder:v});} }) |
| 164 |
), |
| 165 |
el(PanelBody, { title:'Sizing', initialOpen:false }, |
| 166 |
el(RangeControl, { label:'Max width (px)', value:a.contentMaxWidth, min:400, max:1200, step:20, onChange:function(v){set({contentMaxWidth:v});} }) |
| 167 |
), |
| 168 |
|
| 169 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 170 |
getTypoControl() && getTypoControl()({ |
| 171 |
label: __('Title', 'blockenberg'), |
| 172 |
value: a.titleTypo || {}, |
| 173 |
onChange: function (v) { set({ titleTypo: v }); } |
| 174 |
}), |
| 175 |
getTypoControl() && getTypoControl()({ |
| 176 |
label: __('Subtitle', 'blockenberg'), |
| 177 |
value: a.subtitleTypo || {}, |
| 178 |
onChange: function (v) { set({ subtitleTypo: v }); } |
| 179 |
}) |
| 180 |
), |
| 181 |
el(PanelColorSettings, { title:'Colors', initialOpen:false, disableCustomGradients:true, colorSettings:colorSettings }) |
| 182 |
), |
| 183 |
el('div', (function () { |
| 184 |
var s = {}; |
| 185 |
var _tvf = getTypoCssVars(); |
| 186 |
if (_tvf) { Object.assign(s, _tvf(a.titleTypo, '--bkta-tt-'), _tvf(a.subtitleTypo, '--bkta-st-')); } |
| 187 |
return useBlockProps({ style: s }); |
| 188 |
})(), |
| 189 |
el(EditorPreview, { attributes:a, setAttributes:set }) |
| 190 |
) |
| 191 |
); |
| 192 |
}, |
| 193 |
|
| 194 |
save: function (props) { |
| 195 |
var a = props.attributes; |
| 196 |
var _tvf = getTypoCssVars(); |
| 197 |
return el('div', (function () { |
| 198 |
var s = {}; |
| 199 |
if (_tvf) { Object.assign(s, _tvf(a.titleTypo, '--bkta-tt-'), _tvf(a.subtitleTypo, '--bkta-st-')); } |
| 200 |
return useBlockProps.save({ style: s }); |
| 201 |
})(), |
| 202 |
el('div', { |
| 203 |
className: 'bkbg-ta-app', |
| 204 |
'data-opts': JSON.stringify({ |
| 205 |
title: a.title, |
| 206 |
subtitle: a.subtitle, |
| 207 |
showTitle: a.showTitle, |
| 208 |
showSubtitle: a.showSubtitle, |
| 209 |
showBasicStats: a.showBasicStats, |
| 210 |
showVocabStats: a.showVocabStats, |
| 211 |
showFrequency: a.showFrequency, |
| 212 |
showLongestWord:a.showLongestWord, |
| 213 |
freqTopN: a.freqTopN, |
| 214 |
defaultText: a.defaultText, |
| 215 |
placeholder: a.placeholder, |
| 216 |
accentColor: a.accentColor, |
| 217 |
barColor: a.barColor, |
| 218 |
statBg: a.statBg, |
| 219 |
statNumColor: a.statNumColor, |
| 220 |
sectionBg: a.sectionBg, |
| 221 |
cardBg: a.cardBg, |
| 222 |
inputBg: a.inputBg, |
| 223 |
titleColor: a.titleColor, |
| 224 |
subtitleColor: a.subtitleColor, |
| 225 |
labelColor: a.labelColor, |
| 226 |
contentMaxWidth:a.contentMaxWidth |
| 227 |
}) |
| 228 |
}) |
| 229 |
); |
| 230 |
} |
| 231 |
}); |
| 232 |
}() ); |
| 233 |
|