PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.11
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.11
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 / kb-article / index.js

index.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.11, at blocks/kb-article/index.js

314 lines 23.5 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 Fragment = wp.element.Fragment;
4 var registerBlockType = wp.blocks.registerBlockType;
5 var InspectorControls = wp.blockEditor.InspectorControls;
6 var useBlockProps = wp.blockEditor.useBlockProps;
7 var PanelBody = wp.components.PanelBody;
8 var PanelColorSettings = wp.blockEditor.PanelColorSettings;
9 var ToggleControl = wp.components.ToggleControl;
10 var RangeControl = wp.components.RangeControl;
11 var SelectControl = wp.components.SelectControl;
12 var TextControl = wp.components.TextControl;
13 var TextareaControl = wp.components.TextareaControl;
14 var Button = wp.components.Button;
15
16 var _TypographyControl, _typoCssVars;
17 function getTypographyControl() { return _TypographyControl || (_TypographyControl = window.bkbgTypographyControl); }
18 function getTypoCssVars() { return _typoCssVars || (_typoCssVars = window.bkbgTypoCssVars); }
19
20 // ── helpers ────────────────────────────────────────────────────
21 function updateSection(sections, idx, field, val) {
22 return sections.map(function (s, i) {
23 if (i !== idx) return s;
24 var u = {}; u[field] = val;
25 return Object.assign({}, s, u);
26 });
27 }
28 function addSection(sections) {
29 return sections.concat([{ heading: 'New Section', content: 'Section content goes here.', type: 'normal' }]);
30 }
31 function removeSection(sections, idx) {
32 return sections.filter(function (_, i) { return i !== idx; });
33 }
34 function updateRelated(arr, idx, field, val) {
35 return arr.map(function (r, i) {
36 if (i !== idx) return r;
37 var u = {}; u[field] = val;
38 return Object.assign({}, r, u);
39 });
40 }
41
42 function difficultyBadge(diff) {
43 var map = { beginner: { label: '🟢 Beginner', bg: '#dcfce7', color: '#14532d' }, intermediate: { label: '🟡 Intermediate', bg: '#fef9c3', color: '#713f12' }, advanced: { label: '🟠 Advanced', bg: '#ffedd5', color: '#9a3412' }, expert: { label: '🔴 Expert', bg: '#fee2e2', color: '#991b1b' } };
44 return map[diff] || map.beginner;
45 }
46
47 var sectionTypeOptions = [
48 { label: 'Normal', value: 'normal' },
49 { label: 'Step', value: 'step' },
50 { label: 'Info / Tip', value: 'info' },
51 { label: 'Warning', value: 'warning' }
52 ];
53
54 var difficultyOptions = [
55 { label: '🟢 Beginner', value: 'beginner' },
56 { label: '🟡 Intermediate', value: 'intermediate' },
57 { label: '🟠 Advanced', value: 'advanced' },
58 { label: '🔴 Expert', value: 'expert' }
59 ];
60
61 registerBlockType('blockenberg/kb-article', {
62 edit: function (props) {
63 var a = props.attributes;
64 var set = props.setAttributes;
65
66 var db = difficultyBadge(a.difficulty);
67 var stepNum = 0;
68
69 function sectionPreview(s, idx) {
70 var isStep = s.type === 'step';
71 var isInfo = s.type === 'info';
72 var isWarn = s.type === 'warning';
73 if (isStep) stepNum++;
74
75 var secStyle = {
76 marginBottom: 16,
77 padding: isInfo || isWarn ? '14px 16px' : '0',
78 borderLeft: isInfo ? '4px solid ' + a.infoBorderColor : isWarn ? '4px solid ' + a.warningBorderColor : 'none',
79 background: isInfo ? a.infoBg : isWarn ? a.warningBg : 'transparent',
80 borderRadius: isInfo || isWarn ? 6 : 0
81 };
82
83 return el('div', { key: idx, style: secStyle },
84 el('div', { style: { display: 'flex', alignItems: 'center', gap: 10, marginBottom: 6 } },
85 isStep && el('span', { style: { width: 26, height: 26, borderRadius: '50%', background: a.stepNumberBg, color: a.stepNumberColor, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontSize: 12, fontWeight: 700, flexShrink: 0 } }, stepNum),
86 el('h4', { style: { margin: 0, fontSize: 16, fontWeight: 700, color: isInfo ? a.infoColor : isWarn ? a.warningColor : a.headingColor } },
87 isInfo ? '💡 ' : isWarn ? '⚠️ ' : '',
88 s.heading
89 )
90 ),
91 el('p', { style: { margin: 0, color: isInfo ? a.infoColor : isWarn ? a.warningColor : a.textColor } }, s.content)
92 );
93 }
94
95 var blockProps = useBlockProps((function () {
96 var _tv = getTypoCssVars();
97 var s = {};
98 Object.assign(s, _tv(a.titleTypo, '--bkbg-kba-tt-'));
99 Object.assign(s, _tv(a.bodyTypo, '--bkbg-kba-bd-'));
100 return { className: 'bkbg-kba-editor-wrap', style: s };
101 })());
102
103 return el(Fragment, null,
104 el('div', blockProps,
105 // ── Header ─────────────────────────────────────
106 el('div', { style: { background: a.headerBg, padding: '20px 24px', borderRadius: (a.borderRadius || 10) + 'px ' + (a.borderRadius || 10) + 'px 0 0', borderBottom: '1px solid ' + a.borderColor } },
107 a.showCategory && el('div', { style: { fontSize: 12, color: a.categoryColor, marginBottom: 6 } },
108 a.category, a.subcategory ? el('span', null, '' + a.subcategory) : null
109 ),
110 el('h2', { className: 'bkbg-kba-title', style: { margin: '0 0 10px', color: a.titleColor } }, a.articleTitle),
111 el('div', { style: { display: 'flex', flexWrap: 'wrap', gap: 12, alignItems: 'center', fontSize: 13, color: a.categoryColor } },
112 a.showDifficulty && el('span', { style: { background: db.bg, color: db.color, padding: '2px 10px', borderRadius: 100, fontWeight: 600, fontSize: 12 } }, db.label),
113 a.showReadTime && el('span', null, '' + a.readTime),
114 a.showLastUpdated && el('span', null, '🗓 Updated ' + a.lastUpdated),
115 a.showAppliesTo && el('span', null, '�
116 ' + a.appliesTo)
117 )
118 ),
119 // ── Body ───────────────────────────────────────
120 el('div', { style: { padding: '20px 24px', background: a.bgColor, maxWidth: (a.maxWidth || 820) + 'px' } },
121 // Intro
122 a.showIntro && el('p', { className: 'bkbg-kba-intro', style: { margin: '0 0 18px', color: a.textColor } }, a.intro),
123 // TOC
124 a.showToc && a.sections.length > 0 && el('div', { style: { background: a.tocBg, borderRadius: 8, padding: '14px 18px', marginBottom: 20 } },
125 el('div', { style: { fontWeight: 700, fontSize: 13, color: a.tocColor, marginBottom: 8 } }, a.tocLabel || 'In this article'),
126 el('ol', { style: { margin: 0, paddingLeft: 20, color: a.tocLinkColor } },
127 a.sections.map(function (s, i) {
128 return el('li', { key: i, style: { marginBottom: 4, fontSize: 14 } }, s.heading);
129 })
130 )
131 ),
132 // Sections
133 (function () { stepNum = 0; return a.sections.map(function (s, i) { return sectionPreview(s, i); }); })()
134 )
135 ),
136 // ── Inspector ──────────────────────────────────────
137 el(InspectorControls, null,
138 // Article Info
139 el(PanelBody, { title: 'Article Info', initialOpen: true },
140 el(TextControl, { label: 'Article Title', value: a.articleTitle, __nextHasNoMarginBottom: true, onChange: function (v) { set({ articleTitle: v }); } }),
141 el('div', { style: { marginTop: 8 } },
142 el(TextControl, { label: 'Category', value: a.category, __nextHasNoMarginBottom: true, onChange: function (v) { set({ category: v }); } })
143 ),
144 el('div', { style: { marginTop: 8 } },
145 el(TextControl, { label: 'Subcategory', value: a.subcategory, __nextHasNoMarginBottom: true, onChange: function (v) { set({ subcategory: v }); } })
146 ),
147 el('div', { style: { marginTop: 8 } },
148 el(TextControl, { label: 'Last Updated', value: a.lastUpdated, __nextHasNoMarginBottom: true, onChange: function (v) { set({ lastUpdated: v }); } })
149 ),
150 el('div', { style: { marginTop: 8 } },
151 el(TextControl, { label: 'Read Time', value: a.readTime, __nextHasNoMarginBottom: true, onChange: function (v) { set({ readTime: v }); } })
152 ),
153 el('div', { style: { marginTop: 8 } },
154 el(TextControl, { label: 'Applies To', value: a.appliesTo, __nextHasNoMarginBottom: true, onChange: function (v) { set({ appliesTo: v }); } })
155 ),
156 el('div', { style: { marginTop: 8 } },
157 el(SelectControl, { label: 'Difficulty', value: a.difficulty, options: difficultyOptions, __nextHasNoMarginBottom: true, onChange: function (v) { set({ difficulty: v }); } })
158 ),
159 el('div', { style: { marginTop: 8, display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 4 } },
160 el(ToggleControl, { label: 'Category', checked: a.showCategory, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showCategory: v }); } }),
161 el(ToggleControl, { label: 'Difficulty', checked: a.showDifficulty, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showDifficulty: v }); } }),
162 el(ToggleControl, { label: 'Read Time', checked: a.showReadTime, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showReadTime: v }); } }),
163 el(ToggleControl, { label: 'Last Updated', checked: a.showLastUpdated, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showLastUpdated: v }); } }),
164 el(ToggleControl, { label: 'Applies To', checked: a.showAppliesTo, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showAppliesTo: v }); } })
165 )
166 ),
167 // Intro
168 el(PanelBody, { title: 'Introduction', initialOpen: false },
169 el(ToggleControl, { label: 'Show Introduction', checked: a.showIntro, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showIntro: v }); } }),
170 el('div', { style: { marginTop: 8 } },
171 el(TextareaControl, { label: 'Intro Paragraph', value: a.intro, rows: 4, __nextHasNoMarginBottom: true, onChange: function (v) { set({ intro: v }); } })
172 )
173 ),
174 // Table of Contents
175 el(PanelBody, { title: 'Table of Contents', initialOpen: false },
176 el(ToggleControl, { label: 'Show Table of Contents', checked: a.showToc, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showToc: v }); } }),
177 el('div', { style: { marginTop: 8 } },
178 el(TextControl, { label: 'TOC Label', value: a.tocLabel, __nextHasNoMarginBottom: true, onChange: function (v) { set({ tocLabel: v }); } })
179 )
180 ),
181 // Sections
182 el(PanelBody, { title: 'Sections (' + a.sections.length + ')', initialOpen: false },
183 a.sections.map(function (s, idx) {
184 return el('div', { key: idx, style: { marginBottom: 16, paddingBottom: 16, borderBottom: '1px solid #e5e7eb' } },
185 el(TextControl, { label: 'Heading ' + (idx + 1), value: s.heading, __nextHasNoMarginBottom: true, onChange: function (v) { set({ sections: updateSection(a.sections, idx, 'heading', v) }); } }),
186 el('div', { style: { marginTop: 6 } },
187 el(TextareaControl, { label: 'Content', value: s.content, rows: 3, __nextHasNoMarginBottom: true, onChange: function (v) { set({ sections: updateSection(a.sections, idx, 'content', v) }); } })
188 ),
189 el('div', { style: { marginTop: 6 } },
190 el(SelectControl, { label: 'Type', value: s.type, options: sectionTypeOptions, __nextHasNoMarginBottom: true, onChange: function (v) { set({ sections: updateSection(a.sections, idx, 'type', v) }); } })
191 ),
192 el(Button, { isDestructive: true, variant: 'link', style: { fontSize: 11, marginTop: 4 }, onClick: function () { set({ sections: removeSection(a.sections, idx) }); } }, '✕ Remove section')
193 );
194 }),
195 el(Button, { variant: 'secondary', onClick: function () { set({ sections: addSection(a.sections) }); } }, '+ Add Section')
196 ),
197 // Tags & Related
198 el(PanelBody, { title: 'Tags & Related Articles', initialOpen: false },
199 el(ToggleControl, { label: 'Show Tags', checked: a.showTags, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showTags: v }); } }),
200 el('div', { style: { marginTop: 8 } },
201 el(TextControl, { label: 'Tags (comma-separated)', value: a.tags.join(', '), __nextHasNoMarginBottom: true, onChange: function (v) { set({ tags: v.split(',').map(function (t) { return t.trim(); }).filter(Boolean) }); } })
202 ),
203 el('div', { style: { marginTop: 12, borderTop: '1px solid #e5e7eb', paddingTop: 12 } },
204 el(ToggleControl, { label: 'Show Related Articles', checked: a.showRelated, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showRelated: v }); } }),
205 el('div', { style: { marginTop: 8 } },
206 el(TextControl, { label: 'Related Section Label', value: a.relatedLabel, __nextHasNoMarginBottom: true, onChange: function (v) { set({ relatedLabel: v }); } })
207 ),
208 a.relatedArticles.map(function (r, i) {
209 return el('div', { key: i, style: { marginTop: 8, display: 'flex', gap: 4, alignItems: 'center' } },
210 el('div', { style: { flex: 1 } },
211 el(TextControl, { placeholder: 'Article title', value: r.title, __nextHasNoMarginBottom: true, onChange: function (v) { set({ relatedArticles: updateRelated(a.relatedArticles, i, 'title', v) }); } })
212 ),
213 el(Button, { isDestructive: true, variant: 'link', onClick: function () { set({ relatedArticles: a.relatedArticles.filter(function (_, j) { return j !== i; }) }); } }, '')
214 );
215 }),
216 el(Button, { variant: 'secondary', style: { marginTop: 8 }, onClick: function () { set({ relatedArticles: a.relatedArticles.concat([{ title: 'Related article', url: '#' }]) }); } }, '+ Add Related')
217 )
218 ),
219 // Helpful Votes
220 el(PanelBody, { title: 'Helpful Votes', initialOpen: false },
221 el(ToggleControl, { label: 'Show Helpful Votes Section', checked: a.showHelpful, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showHelpful: v }); } }),
222 el('div', { style: { marginTop: 8 } },
223 el(TextControl, { label: 'Helpful Label', value: a.helpfulLabel, __nextHasNoMarginBottom: true, onChange: function (v) { set({ helpfulLabel: v }); } })
224 ),
225 el('div', { style: { marginTop: 8, display: 'flex', gap: 8 } },
226 el('div', { style: { flex: 1 } },
227 el(RangeControl, { label: '👍 Yes', value: a.helpfulYes, min: 0, max: 999, __nextHasNoMarginBottom: true, onChange: function (v) { set({ helpfulYes: v }); } })
228 ),
229 el('div', { style: { flex: 1 } },
230 el(RangeControl, { label: '👎 No', value: a.helpfulNo, min: 0, max: 999, __nextHasNoMarginBottom: true, onChange: function (v) { set({ helpfulNo: v }); } })
231 )
232 )
233 ),
234 // Typography
235 el(PanelBody, { title: 'Typography', initialOpen: false },
236 el(getTypographyControl(), { label: 'Title', value: a.titleTypo, onChange: function (v) { set({ titleTypo: v }); } }),
237 el(getTypographyControl(), { label: 'Body', value: a.bodyTypo, onChange: function (v) { set({ bodyTypo: v }); } })
238 ),
239 el(PanelBody, { title: 'Appearance', initialOpen: false },
240 el(RangeControl, { label: 'Max Width (px)', value: a.maxWidth, min: 500, max: 1200, step: 10, __nextHasNoMarginBottom: true, onChange: function (v) { set({ maxWidth: v }); } }),
241 el('div', { style: { marginTop: 8 } },
242 el(RangeControl, { label: 'Border Radius (px)', value: a.borderRadius, min: 0, max: 24, __nextHasNoMarginBottom: true, onChange: function (v) { set({ borderRadius: v }); } })
243 )
244 ),
245 // Colors — Header
246 el(PanelColorSettings, {
247 title: 'Header Colors',
248 initialOpen: false,
249 colorSettings: [
250 { label: 'Header Background', value: a.headerBg, onChange: function (v) { set({ headerBg: v || '#f8fafc' }); } },
251 { label: 'Title Color', value: a.titleColor, onChange: function (v) { set({ titleColor: v || '#0f172a' }); } },
252 { label: 'Category Text', value: a.categoryColor, onChange: function (v) { set({ categoryColor: v || '#64748b' }); } },
253 { label: 'Border', value: a.borderColor, onChange: function (v) { set({ borderColor: v || '#e2e8f0' }); } }
254 ]
255 }),
256 // Colors — Body
257 el(PanelColorSettings, {
258 title: 'Body Colors',
259 initialOpen: false,
260 colorSettings: [
261 { label: 'Background', value: a.bgColor, onChange: function (v) { set({ bgColor: v || '#ffffff' }); } },
262 { label: 'Text', value: a.textColor, onChange: function (v) { set({ textColor: v || '#374151' }); } },
263 { label: 'Heading', value: a.headingColor, onChange: function (v) { set({ headingColor: v || '#0f172a' }); } },
264 { label: 'Accent', value: a.accentColor, onChange: function (v) { set({ accentColor: v || '#2563eb' }); } },
265 { label: 'Section Divider', value: a.sectionBorderColor,onChange: function (v) { set({ sectionBorderColor: v || '#e5e7eb' }); } },
266 { label: 'TOC Background', value: a.tocBg, onChange: function (v) { set({ tocBg: v || '#f1f5f9' }); } },
267 { label: 'TOC Text', value: a.tocColor, onChange: function (v) { set({ tocColor: v || '#1e293b' }); } },
268 { label: 'TOC Links', value: a.tocLinkColor, onChange: function (v) { set({ tocLinkColor: v || '#2563eb' }); } }
269 ]
270 }),
271 // Colors — Section Types
272 el(PanelColorSettings, {
273 title: 'Section Type Colors',
274 initialOpen: false,
275 colorSettings: [
276 { label: 'Step # Background', value: a.stepNumberBg, onChange: function (v) { set({ stepNumberBg: v || '#2563eb' }); } },
277 { label: 'Step # Text', value: a.stepNumberColor, onChange: function (v) { set({ stepNumberColor: v || '#ffffff' }); } },
278 { label: 'Info Background', value: a.infoBg, onChange: function (v) { set({ infoBg: v || '#eff6ff' }); } },
279 { label: 'Info Text', value: a.infoColor, onChange: function (v) { set({ infoColor: v || '#1e40af' }); } },
280 { label: 'Info Border', value: a.infoBorderColor, onChange: function (v) { set({ infoBorderColor: v || '#93c5fd' }); } },
281 { label: 'Warning Background', value: a.warningBg, onChange: function (v) { set({ warningBg: v || '#fffbeb' }); } },
282 { label: 'Warning Text', value: a.warningColor, onChange: function (v) { set({ warningColor: v || '#92400e' }); } },
283 { label: 'Warning Border', value: a.warningBorderColor,onChange: function (v) { set({ warningBorderColor: v || '#fcd34d' }); } }
284 ]
285 }),
286 // Colors — Tags & Helpful
287 el(PanelColorSettings, {
288 title: 'Tags & Helpful Colors',
289 initialOpen: false,
290 colorSettings: [
291 { label: 'Tag Background', value: a.tagBg, onChange: function (v) { set({ tagBg: v || '#f1f5f9' }); } },
292 { label: 'Tag Text', value: a.tagColor, onChange: function (v) { set({ tagColor: v || '#475569' }); } },
293 { label: '👍 Yes Background', value: a.helpfulYesBg, onChange: function (v) { set({ helpfulYesBg: v || '#dcfce7' }); } },
294 { label: '👍 Yes Text', value: a.helpfulYesColor, onChange: function (v) { set({ helpfulYesColor: v || '#14532d' }); } },
295 { label: '👎 No Background', value: a.helpfulNoBg, onChange: function (v) { set({ helpfulNoBg: v || '#fee2e2' }); } },
296 { label: '👎 No Text', value: a.helpfulNoColor, onChange: function (v) { set({ helpfulNoColor: v || '#991b1b' }); } }
297 ]
298 })
299 )
300 );
301 },
302
303 save: function (props) {
304 var useBlockProps = wp.blockEditor.useBlockProps;
305 return wp.element.createElement('div', useBlockProps.save(),
306 wp.element.createElement('div', {
307 className: 'bkbg-kb-article-app',
308 'data-opts': JSON.stringify(props.attributes)
309 })
310 );
311 }
312 });
313 }() );
314