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 / word-counter / index.js

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

237 lines 16.0 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 SelectControl = wp.components.SelectControl;
13 var TextControl = wp.components.TextControl;
14 var ToggleControl = wp.components.ToggleControl;
15 var Button = wp.components.Button;
16
17 var _tc, _tvf;
18 Object.defineProperty(window, '_tc', { get: function () { return _tc || (_tc = window.bkbgTypographyControl); } });
19 Object.defineProperty(window, '_tvf', { get: function () { return _tvf || (_tvf = window.bkbgTypoCssVars); } });
20 function getTypoControl(label, key, attrs, setA) { return _tc(label, key, attrs, setA); }
21 function getTypoCssVars(attrs) {
22 var v = {};
23 _tvf(v, 'titleTypo', attrs, '--bkwrd-tt-');
24 return v;
25 }
26
27 function analyzeText(text, readingWPM, speakingWPM) {
28 var words = text.trim() === '' ? 0 : text.trim().split(/\s+/).length;
29 var chars = text.length;
30 var charsNoSp = text.replace(/\s/g, '').length;
31 var sentences = text.trim() === '' ? 0 : (text.match(/[^.!?]+[.!?]+/g) || []).length;
32 var paragraphs = text.trim() === '' ? 0 : text.trim().split(/\n\s*\n+/).filter(function(p) { return p.trim() !== ''; }).length || (text.trim() !== '' ? 1 : 0);
33 var readSec = words > 0 ? Math.ceil((words / readingWPM) * 60) : 0;
34 var speakSec = words > 0 ? Math.ceil((words / speakingWPM) * 60) : 0;
35 return { words: words, chars: chars, charsNoSp: charsNoSp, sentences: sentences, paragraphs: paragraphs, readSec: readSec, speakSec: speakSec };
36 }
37
38 function fmtTime(secs) {
39 if (secs < 60) return secs + 's';
40 var m = Math.floor(secs / 60);
41 var s = secs % 60;
42 return m + 'm' + (s ? ' ' + s + 's' : '');
43 }
44
45 function WordCounterPreview(props) {
46 var a = props.attributes;
47 var sa = props.setAttributes;
48
49 var _text = useState(a.defaultText || ''); var text = _text[0]; var setText = _text[1];
50
51 var stats = analyzeText(text, a.readingWPM || 238, a.speakingWPM || 130);
52 var charPct = a.showCharLimit && a.charLimit > 0 ? Math.min(100, (stats.chars / a.charLimit) * 100) : 0;
53 var isOver = stats.chars > a.charLimit;
54
55 var containerStyle = {
56 background: a.sectionBg || undefined,
57 paddingTop: a.paddingTop + 'px',
58 paddingBottom: a.paddingBottom + 'px',
59 fontFamily: 'inherit'
60 };
61 var cardStyle = {
62 background: a.cardBg,
63 borderRadius: a.cardRadius + 'px',
64 padding: '36px 32px',
65 maxWidth: a.maxWidth + 'px',
66 margin: '0 auto',
67 boxShadow: '0 4px 24px rgba(0,0,0,0.09)'
68 };
69
70 var statDefs = [
71 { key: 'words', label: 'Words', val: stats.words, color: a.wordCountColor, show: true },
72 { key: 'chars', label: 'Characters', val: stats.chars, color: a.charCountColor, show: true },
73 { key: 'charsNoSp', label: 'Chars (no spaces)', val: stats.charsNoSp, color: a.charCountColor, show: a.showCharNoSpaces },
74 { key: 'sentences', label: 'Sentences', val: stats.sentences, color: a.sentenceColor, show: a.showSentences },
75 { key: 'paragraphs', label: 'Paragraphs', val: stats.paragraphs, color: a.paragraphColor, show: a.showParagraphs },
76 { key: 'readTime', label: 'Reading Time', val: fmtTime(stats.readSec), color: a.readingColor, show: a.showReadingTime },
77 { key: 'speakTime', label: 'Speaking Time', val: fmtTime(stats.speakSec), color: a.speakingColor, show: a.showSpeakingTime }
78 ].filter(function(s) { return s.show; });
79
80 return el('div', { className: 'bkbg-wrd-editor', style: containerStyle },
81 el('div', { style: cardStyle },
82
83 // Title / subtitle
84 (a.showTitle || a.showSubtitle) && el('div', { style: { marginBottom: '20px' } },
85 a.showTitle && el('div', {
86 className: 'bkbg-wrd-title',
87 style: { color: a.titleColor, contentEditable: true, suppressContentEditableWarning: true, outline: 'none' },
88 onBlur: function(e) { sa({ title: e.target.innerText }); },
89 dangerouslySetInnerHTML: { __html: a.title }
90 }),
91 a.showSubtitle && el('div', {
92 className: 'bkbg-wrd-subtitle',
93 style: { color: a.subtitleColor, contentEditable: true, suppressContentEditableWarning: true, outline: 'none' },
94 onBlur: function(e) { sa({ subtitle: e.target.innerText }); },
95 dangerouslySetInnerHTML: { __html: a.subtitle }
96 })
97 ),
98
99 // Textarea
100 el('textarea', {
101 className: 'bkbg-wrd-textarea',
102 placeholder: a.placeholder || 'Start typing or paste your text here…',
103 value: text,
104 style: {
105 width: '100%',
106 height: a.textareaHeight + 'px',
107 padding: '14px 16px',
108 borderRadius: a.inputRadius + 'px',
109 border: '2px solid ' + (isOver && a.showCharLimit ? a.limitBarOver : a.textareaBorder),
110 background: a.textareaBg,
111 fontSize: '15px',
112 lineHeight: 1.6,
113 resize: 'vertical',
114 outline: 'none',
115 boxSizing: 'border-box',
116 marginBottom: '16px',
117 fontFamily: 'inherit',
118 transition: 'border-color .2s'
119 },
120 onChange: function(e) { setText(e.target.value); }
121 }),
122
123 // Char limit bar
124 a.showCharLimit && el('div', { style: { marginBottom: '16px' } },
125 el('div', { style: { display: 'flex', justifyContent: 'space-between', fontSize: '12px', color: a.labelColor, marginBottom: '5px' } },
126 el('span', null, 'Character Limit'),
127 el('span', { style: { color: isOver ? a.limitBarOver : a.labelColor, fontWeight: isOver ? 700 : 400 } },
128 stats.chars + ' / ' + a.charLimit
129 )
130 ),
131 el('div', { style: { height: '8px', borderRadius: '100px', background: a.limitBarBg, overflow: 'hidden' } },
132 el('div', { style: { height: '100%', width: charPct + '%', background: isOver ? a.limitBarOver : a.limitBarFill, borderRadius: '100px', transition: 'width .3s, background .3s' } })
133 )
134 ),
135
136 // Stats grid
137 el('div', { style: { display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(130px, 1fr))', gap: '10px' } },
138 statDefs.map(function(s) {
139 return el('div', { key: s.key, className: 'bkbg-wrd-stat-card', style: {
140 background: a.statCardBg,
141 border: '1.5px solid ' + a.statCardBorder,
142 borderRadius: a.cardRadius + 'px',
143 padding: '16px 14px',
144 textAlign: 'center'
145 }},
146 el('div', { style: { fontSize: a.statValueSize + 'px', fontWeight: 800, color: s.color, lineHeight: 1.1 } }, String(s.val)),
147 el('div', { style: { fontSize: '12px', color: a.labelColor, marginTop: '4px', fontWeight: 600 } }, s.label)
148 );
149 })
150 )
151 )
152 );
153 }
154
155 registerBlockType('blockenberg/word-counter', {
156 edit: function(props) {
157 var a = props.attributes;
158 var set = props.setAttributes;
159 var blockProps = useBlockProps({ style: getTypoCssVars(a) });
160
161 var colorSettings = [
162 { value: a.accentColor, onChange: function(v) { set({ accentColor: v }); }, label: 'Accent Color' },
163 { value: a.cardBg, onChange: function(v) { set({ cardBg: v }); }, label: 'Card Background' },
164 { value: a.textareaBg, onChange: function(v) { set({ textareaBg: v }); }, label: 'Textarea Background' },
165 { value: a.textareaBorder, onChange: function(v) { set({ textareaBorder: v }); }, label: 'Textarea Border' },
166 { value: a.statCardBg, onChange: function(v) { set({ statCardBg: v }); }, label: 'Stat Card Background' },
167 { value: a.statCardBorder, onChange: function(v) { set({ statCardBorder: v }); }, label: 'Stat Card Border' },
168 { value: a.wordCountColor, onChange: function(v) { set({ wordCountColor: v }); }, label: 'Word Count Color' },
169 { value: a.charCountColor, onChange: function(v) { set({ charCountColor: v }); }, label: 'Character Count Color' },
170 { value: a.sentenceColor, onChange: function(v) { set({ sentenceColor: v }); }, label: 'Sentence Count Color' },
171 { value: a.paragraphColor, onChange: function(v) { set({ paragraphColor: v }); }, label: 'Paragraph Count Color' },
172 { value: a.readingColor, onChange: function(v) { set({ readingColor: v }); }, label: 'Reading Time Color' },
173 { value: a.speakingColor, onChange: function(v) { set({ speakingColor: v }); }, label: 'Speaking Time Color' },
174 { value: a.limitBarFill, onChange: function(v) { set({ limitBarFill: v }); }, label: 'Limit Bar Fill' },
175 { value: a.limitBarOver, onChange: function(v) { set({ limitBarOver: v }); }, label: 'Limit Bar (over limit)' },
176 { value: a.limitBarBg, onChange: function(v) { set({ limitBarBg: v }); }, label: 'Limit Bar Background' },
177 { value: a.titleColor, onChange: function(v) { set({ titleColor: v }); }, label: 'Title Color' },
178 { value: a.subtitleColor, onChange: function(v) { set({ subtitleColor: v }); }, label: 'Subtitle Color' },
179 { value: a.labelColor, onChange: function(v) { set({ labelColor: v }); }, label: 'Label Color' },
180 { value: a.sectionBg, onChange: function(v) { set({ sectionBg: v }); }, label: 'Section Background' }
181 ];
182
183 return el(Fragment, null,
184 el(InspectorControls, null,
185
186 el(PanelBody, { title: 'Header', initialOpen: false },
187 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Title', checked: a.showTitle, onChange: function(v) { set({ showTitle: v }); } }),
188 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Subtitle', checked: a.showSubtitle, onChange: function(v) { set({ showSubtitle: v }); } }),
189 el(TextControl, { label: 'Title', value: a.title, onChange: function(v) { set({ title: v }); } }),
190 el(TextControl, { label: 'Subtitle', value: a.subtitle, onChange: function(v) { set({ subtitle: v }); } })
191 ),
192
193 el(PanelBody, { title: 'Counter Settings', initialOpen: true },
194 el(TextControl, { label: 'Textarea Placeholder', value: a.placeholder, onChange: function(v) { set({ placeholder: v }); } }),
195 el(TextControl, { label: 'Default Text (pre-filled)', value: a.defaultText, onChange: function(v) { set({ defaultText: v }); } }),
196 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Characters Without Spaces', checked: a.showCharNoSpaces, onChange: function(v) { set({ showCharNoSpaces: v }); } }),
197 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Sentence Count', checked: a.showSentences, onChange: function(v) { set({ showSentences: v }); } }),
198 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Paragraph Count', checked: a.showParagraphs, onChange: function(v) { set({ showParagraphs: v }); } }),
199 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Reading Time', checked: a.showReadingTime, onChange: function(v) { set({ showReadingTime: v }); } }),
200 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Show Speaking Time', checked: a.showSpeakingTime, onChange: function(v) { set({ showSpeakingTime: v }); } }),
201 el(RangeControl, { label: 'Reading Speed (WPM)', value: a.readingWPM, min: 100, max: 600, step: 10, onChange: function(v) { set({ readingWPM: v }); } }),
202 el(RangeControl, { label: 'Speaking Speed (WPM)', value: a.speakingWPM, min: 80, max: 400, step: 10, onChange: function(v) { set({ speakingWPM: v }); } }),
203 el(ToggleControl, { __nextHasNoMarginBottom: true, label: 'Enable Character Limit', checked: a.showCharLimit, onChange: function(v) { set({ showCharLimit: v }); } }),
204 a.showCharLimit && el(RangeControl, { label: 'Character Limit', value: a.charLimit, min: 50, max: 5000, step: 50, onChange: function(v) { set({ charLimit: v }); } })
205 ),
206
207
208 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
209 getTypoControl(__('Title', 'blockenberg'), 'titleTypo', a, set),
210 el(RangeControl, { label: 'Stat Value Size', value: a.statValueSize, min: 20, max: 72, step: 2, onChange: function(v) { set({ statValueSize: v }); } })
211 ),
212 el(PanelColorSettings, { title: 'Colors', initialOpen: false, colorSettings: colorSettings }),
213
214 el(PanelBody, { title: 'Sizing & Layout', initialOpen: false },
215 el(RangeControl, { label: 'Textarea Height (px)', value: a.textareaHeight, min: 80, max: 600, step: 10, onChange: function(v) { set({ textareaHeight: v }); } }),
216 el(RangeControl, { label: 'Card Border Radius', value: a.cardRadius, min: 0, max: 40, step: 1, onChange: function(v) { set({ cardRadius: v }); } }),
217 el(RangeControl, { label: 'Input Border Radius', value: a.inputRadius, min: 0, max: 24, step: 1, onChange: function(v) { set({ inputRadius: v }); } }),
218 el(RangeControl, { label: 'Max Width (px)', value: a.maxWidth, min: 300, max: 1000, step: 10, onChange: function(v) { set({ maxWidth: v }); } }),
219 el(RangeControl, { label: 'Padding Top (px)', value: a.paddingTop, min: 0, max: 160, step: 4, onChange: function(v) { set({ paddingTop: v }); } }),
220 el(RangeControl, { label: 'Padding Bottom (px)', value: a.paddingBottom, min: 0, max: 160, step: 4, onChange: function(v) { set({ paddingBottom: v }); } })
221 )
222 ),
223 el('div', blockProps,
224 el(WordCounterPreview, { attributes: a, setAttributes: set })
225 )
226 );
227 },
228 save: function(props) {
229 var a = props.attributes;
230 var blockProps = wp.blockEditor.useBlockProps.save({ style: getTypoCssVars(props.attributes) });
231 return el('div', blockProps,
232 el('div', { className: 'bkbg-wrd-app', 'data-opts': JSON.stringify(a) })
233 );
234 }
235 });
236 }() );
237