PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.14
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.14
2.0.14 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.14, at blocks/word-counter/index.js

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