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 / book-shelf / index.js

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

425 lines 24.9 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 __ = wp.i18n.__;
6 var InspectorControls = wp.blockEditor.InspectorControls;
7 var PanelColorSettings = wp.blockEditor.PanelColorSettings;
8 var useBlockProps = wp.blockEditor.useBlockProps;
9 var PanelBody = wp.components.PanelBody;
10 var TextControl = wp.components.TextControl;
11 var ToggleControl = wp.components.ToggleControl;
12 var RangeControl = wp.components.RangeControl;
13 var SelectControl = wp.components.SelectControl;
14 var Button = wp.components.Button;
15
16 function getTypographyControl() {
17 return (window.bkbgTypographyControl || function () { return null; });
18 }
19
20 function _tv(typo, prefix) {
21 if (!typo) return {};
22 var s = {};
23 if (typo.family) s[prefix + 'font-family'] = "'" + typo.family + "', sans-serif";
24 if (typo.weight) s[prefix + 'font-weight'] = typo.weight;
25 if (typo.transform) s[prefix + 'text-transform'] = typo.transform;
26 if (typo.style) s[prefix + 'font-style'] = typo.style;
27 if (typo.decoration) s[prefix + 'text-decoration'] = typo.decoration;
28 var su = typo.sizeUnit || 'px';
29 if (typo.sizeDesktop !== '' && typo.sizeDesktop != null) s[prefix + 'font-size-d'] = typo.sizeDesktop + su;
30 if (typo.sizeTablet !== '' && typo.sizeTablet != null) s[prefix + 'font-size-t'] = typo.sizeTablet + su;
31 if (typo.sizeMobile !== '' && typo.sizeMobile != null) s[prefix + 'font-size-m'] = typo.sizeMobile + su;
32 var lhu = typo.lineHeightUnit || '';
33 if (typo.lineHeightDesktop !== '' && typo.lineHeightDesktop != null) s[prefix + 'line-height-d'] = typo.lineHeightDesktop + lhu;
34 if (typo.lineHeightTablet !== '' && typo.lineHeightTablet != null) s[prefix + 'line-height-t'] = typo.lineHeightTablet + lhu;
35 if (typo.lineHeightMobile !== '' && typo.lineHeightMobile != null) s[prefix + 'line-height-m'] = typo.lineHeightMobile + lhu;
36 var lsu = typo.letterSpacingUnit || 'px';
37 if (typo.letterSpacingDesktop !== '' && typo.letterSpacingDesktop != null) s[prefix + 'letter-spacing-d'] = typo.letterSpacingDesktop + lsu;
38 if (typo.letterSpacingTablet !== '' && typo.letterSpacingTablet != null) s[prefix + 'letter-spacing-t'] = typo.letterSpacingTablet + lsu;
39 if (typo.letterSpacingMobile !== '' && typo.letterSpacingMobile != null) s[prefix + 'letter-spacing-m'] = typo.letterSpacingMobile + lsu;
40 var wsu = typo.wordSpacingUnit || 'px';
41 if (typo.wordSpacingDesktop !== '' && typo.wordSpacingDesktop != null) s[prefix + 'word-spacing-d'] = typo.wordSpacingDesktop + wsu;
42 if (typo.wordSpacingTablet !== '' && typo.wordSpacingTablet != null) s[prefix + 'word-spacing-t'] = typo.wordSpacingTablet + wsu;
43 if (typo.wordSpacingMobile !== '' && typo.wordSpacingMobile != null) s[prefix + 'word-spacing-m'] = typo.wordSpacingMobile + wsu;
44 return s;
45 }
46
47 // ── helpers ────────────────────────────────────────────────────
48 function upd(arr, idx, field, val) {
49 return arr.map(function (e, i) {
50 if (i !== idx) return e;
51 var u = {}; u[field] = val;
52 return Object.assign({}, e, u);
53 });
54 }
55
56 var LAYOUT_OPTIONS = [
57 { value: 'shelf', label: 'Shelf (spines)' },
58 { value: 'cards', label: 'Cards (covers)' },
59 { value: 'list', label: 'List' },
60 ];
61 var STATUS_OPTIONS = [
62 { value: 'read', label: '✓ Read' },
63 { value: 'reading', label: '▶ Reading' },
64 { value: 'want', label: '○ Want to read' },
65 ];
66 var SPINE_COLORS = [
67 '#1d4ed8','#7c3aed','#b45309','#0f766e','#b91c1c',
68 '#d97706','#065f46','#831843','#0e7490','#4338ca'
69 ];
70
71 // Stars renderer
72 function renderStars(rating, color) {
73 var out = [];
74 for (var i = 1; i <= 5; i++) {
75 out.push(el('span', { key: i, style: { color: i <= rating ? color : '#334155', fontSize: '12px' } }, '�
76 '));
77 }
78 return el('span', {}, out);
79 }
80
81 // Status badge
82 function statusBadge(status, a) {
83 var bg = status === 'read' ? a.statusReadBg : status === 'reading' ? a.statusReadingBg : a.statusWantBg;
84 var label = status === 'read' ? '✓ Read' : status === 'reading' ? '▶ Reading' : '○ Want';
85 return el('span', {
86 style: {
87 background: bg, color: '#fff',
88 borderRadius: '4px', padding: '1px 7px',
89 fontSize: '10px', fontWeight: '700', letterSpacing: '.04em'
90 }
91 }, label);
92 }
93
94 // ── SHELF LAYOUT ───────────────────────────────────────────────
95 function renderShelf(a) {
96 var books = a.books;
97 var sw = a.spineWidth || 46;
98 var sh = a.spineHeight || 190;
99
100 var spineEls = books.map(function (b, idx) {
101 // Main spine rect
102 var spine = el('div', {
103 key: idx,
104 title: b.title + '' + b.author,
105 style: {
106 width: sw + 'px',
107 height: sh + 'px',
108 background: b.coverColor || '#334155',
109 borderRadius: '3px 3px 3px 3px',
110 display: 'flex', alignItems: 'center', justifyContent: 'center',
111 overflow: 'hidden', flexShrink: '0',
112 position: 'relative',
113 boxShadow: '-2px 2px 4px rgba(0,0,0,0.4), inset 2px 0 4px rgba(255,255,255,.15)',
114 cursor: 'default'
115 }
116 },
117 // Title text — vertical
118 el('div', {
119 style: {
120 writingMode: 'vertical-rl',
121 textOrientation: 'mixed',
122 color: a.spineTextColor || '#ffffff',
123 fontSize: (a.spineFontSize || 11) + 'px',
124 fontWeight: '700', letterSpacing: '0.04em',
125 padding: '8px 4px',
126 maxHeight: (sh - 16) + 'px',
127 overflow: 'hidden',
128 textOverflow: 'ellipsis',
129 lineHeight: '1.2',
130 textShadow: '0 1px 2px rgba(0,0,0,.5)'
131 }
132 }, b.title),
133 // Status dot at top
134 el('div', {
135 style: {
136 position: 'absolute', top: '5px', right: '4px',
137 width: '6px', height: '6px', borderRadius: '50%',
138 background: b.status === 'read' ? '#4ade80' : b.status === 'reading' ? '#60a5fa' : '#fbbf24'
139 }
140 })
141 );
142 return spine;
143 });
144
145 // Shelf row (books + plank)
146 var bookRow = el('div', {
147 style: {
148 display: 'flex', alignItems: 'flex-end', gap: '3px',
149 padding: '12px 20px 0',
150 flexWrap: 'nowrap', overflowX: 'auto'
151 }
152 }, spineEls);
153
154 var plank = el('div', {
155 style: {
156 height: '16px', background: a.shelfColor || '#8B5E3C',
157 boxShadow: '0 4px 8px ' + (a.shelfShadow || '#5c3d1e'),
158 margin: '0 8px', borderRadius: '2px'
159 }
160 });
161
162 return el(Fragment, {}, bookRow, plank);
163 }
164
165 // ── CARDS LAYOUT ───────────────────────────────────────────────
166 function renderCards(a) {
167 return el('div', {
168 style: { display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(130px, 1fr))', gap: '16px', padding: '20px' }
169 },
170 a.books.map(function (b, idx) {
171 // Book cover
172 var cover = el('div', {
173 style: {
174 width: '100%', paddingTop: '140%', position: 'relative',
175 background: b.coverColor || '#334155',
176 borderRadius: '4px 8px 8px 4px',
177 boxShadow: '-3px 3px 6px rgba(0,0,0,.4), inset 3px 0 6px rgba(255,255,255,.1)',
178 marginBottom: '10px'
179 }
180 },
181 el('div', {
182 style: {
183 position: 'absolute', inset: '0',
184 display: 'flex', alignItems: 'center', justifyContent: 'center',
185 flexDirection: 'column', gap: '8px', padding: '12px'
186 }
187 },
188 el('div', { style: { color: '#ffffff', fontSize: '10px', fontWeight: '700', textAlign: 'center', lineHeight: '1.3', textShadow: '0 1px 3px rgba(0,0,0,.5)' } }, b.title),
189 el('div', { style: { color: 'rgba(255,255,255,.7)', fontSize: '9px', textAlign: 'center' } }, b.author)
190 )
191 );
192 var stars = a.showRating ? renderStars(b.rating, a.ratingColor || '#f59e0b') : null;
193 var status = a.showStatus ? statusBadge(b.status, a) : null;
194 var genre = (a.showGenre && b.genre) ? el('span', {
195 style: { background: a.genreBg || '#1e293b', color: a.genreColor || '#94a3b8', borderRadius: '4px', padding: '1px 6px', fontSize: '10px', fontWeight: '600' }
196 }, b.genre) : null;
197
198 return el('div', {
199 key: idx,
200 style: {
201 background: a.cardBg || '#1e293b',
202 border: '1px solid ' + (a.cardBorderColor || '#334155'),
203 borderRadius: '10px', padding: '10px'
204 }
205 },
206 cover,
207 el('div', { style: { display: 'flex', flexDirection: 'column', gap: '6px' } },
208 stars, genre, status
209 )
210 );
211 })
212 );
213 }
214
215 // ── LIST LAYOUT ────────────────────────────────────────────────
216 function renderList(a) {
217 return el('div', { style: { padding: '16px 20px', display: 'flex', flexDirection: 'column', gap: '8px' } },
218 a.books.map(function (b, idx) {
219 var colorDot = el('div', {
220 style: { width: '36px', height: '52px', background: b.coverColor || '#334155', borderRadius: '2px 4px 4px 2px', flexShrink: '0', boxShadow: 'inset 2px 0 4px rgba(255,255,255,.1)' }
221 });
222 var meta = el('div', { style: { flex: '1', minWidth: '0' } },
223 el('div', { className: 'bkbg-bsh-body', style: { color: a.titleColor || '#f5deb3', marginBottom: '2px' } }, b.title),
224 a.showAuthor ? el('div', { className: 'bkbg-bsh-body-author', style: { color: a.authorColor || '#94a3b8', marginBottom: '4px' } }, b.author) : null,
225 el('div', { style: { display: 'flex', alignItems: 'center', gap: '8px', flexWrap: 'wrap' } },
226 a.showRating ? renderStars(b.rating, a.ratingColor || '#f59e0b') : null,
227 a.showGenre && b.genre ? el('span', { style: { background: a.genreBg || '#1e293b', color: a.genreColor || '#94a3b8', borderRadius: '4px', padding: '1px 6px', fontSize: '10px' } }, b.genre) : null,
228 a.showStatus ? statusBadge(b.status, a) : null,
229 (a.showYear && b.year) ? el('span', { style: { color: a.authorColor || '#94a3b8', fontSize: '11px' } }, String(b.year)) : null
230 )
231 );
232 return el('div', { key: idx, style: { display: 'flex', gap: '12px', alignItems: 'center' } }, colorDot, meta);
233 })
234 );
235 }
236
237 // ── EDIT ──────────────────────────────────────────────────────
238 function Edit(props) {
239 var a = props.attributes;
240 var set = props.setAttributes;
241
242 var colorSettings = [
243 { label: __('Background'), value: a.bgColor, onChange: function (v) { set({ bgColor: v || '#1c1410' }); } },
244 { label: __('Header BG'), value: a.headerBg, onChange: function (v) { set({ headerBg: v || '#2d1f14' }); } },
245 { label: __('Title'), value: a.titleColor, onChange: function (v) { set({ titleColor: v || '#f5deb3' }); } },
246 { label: __('Shelf wood'), value: a.shelfColor, onChange: function (v) { set({ shelfColor: v || '#8B5E3C' }); } },
247 { label: __('Shelf shadow'), value: a.shelfShadow, onChange: function (v) { set({ shelfShadow: v || '#5c3d1e' }); } },
248 { label: __('Spine text'), value: a.spineTextColor, onChange: function (v) { set({ spineTextColor: v || '#ffffff' }); } },
249 { label: __('Card BG'), value: a.cardBg, onChange: function (v) { set({ cardBg: v || '#1e293b' }); } },
250 { label: __('Card border'), value: a.cardBorderColor, onChange: function (v) { set({ cardBorderColor: v || '#334155' }); } },
251 { label: __('Author text'), value: a.authorColor, onChange: function (v) { set({ authorColor: v || '#94a3b8' }); } },
252 { label: __('Rating stars'), value: a.ratingColor, onChange: function (v) { set({ ratingColor: v || '#f59e0b' }); } },
253 { label: __('Genre BG'), value: a.genreBg, onChange: function (v) { set({ genreBg: v || '#1e293b' }); } },
254 { label: __('Genre text'), value: a.genreColor, onChange: function (v) { set({ genreColor: v || '#94a3b8' }); } },
255 { label: __('Status: Read'), value: a.statusReadBg, onChange: function (v) { set({ statusReadBg: v || '#166534' }); } },
256 { label: __('Status: Reading'), value: a.statusReadingBg, onChange: function (v) { set({ statusReadingBg: v || '#1e40af' }); } },
257 { label: __('Status: Want'), value: a.statusWantBg, onChange: function (v) { set({ statusWantBg: v || '#78350f' }); } },
258 ];
259
260 var inspector = el(InspectorControls, {},
261 el(PanelBody, { title: __('Shelf Settings'), initialOpen: true },
262 el(TextControl, {
263 label: __('Title'), value: a.shelfTitle, __nextHasNoMarginBottom: true,
264 onChange: function (v) { set({ shelfTitle: v }); }
265 }),
266 el(ToggleControl, {
267 label: __('Show title'), checked: a.showTitle, __nextHasNoMarginBottom: true,
268 onChange: function (v) { set({ showTitle: v }); }
269 }),
270 el(SelectControl, {
271 label: __('Layout'), value: a.layout, options: LAYOUT_OPTIONS, __nextHasNoMarginBottom: true,
272 onChange: function (v) { set({ layout: v }); }
273 }),
274 el(ToggleControl, {
275 label: __('Show rating'), checked: a.showRating, __nextHasNoMarginBottom: true,
276 onChange: function (v) { set({ showRating: v }); }
277 }),
278 el(ToggleControl, {
279 label: __('Show status'), checked: a.showStatus, __nextHasNoMarginBottom: true,
280 onChange: function (v) { set({ showStatus: v }); }
281 }),
282 el(ToggleControl, {
283 label: __('Show genre'), checked: a.showGenre, __nextHasNoMarginBottom: true,
284 onChange: function (v) { set({ showGenre: v }); }
285 }),
286 el(ToggleControl, {
287 label: __('Show author'), checked: a.showAuthor, __nextHasNoMarginBottom: true,
288 onChange: function (v) { set({ showAuthor: v }); }
289 }),
290 el(ToggleControl, {
291 label: __('Show year'), checked: a.showYear, __nextHasNoMarginBottom: true,
292 onChange: function (v) { set({ showYear: v }); }
293 })
294 ),
295 // Spine options (shelf only)
296 a.layout === 'shelf' ? el(PanelBody, { title: __('Spine Size'), initialOpen: false },
297 el(RangeControl, {
298 label: __('Spine width (px)'), value: a.spineWidth, min: 28, max: 80, __nextHasNoMarginBottom: true,
299 onChange: function (v) { set({ spineWidth: v }); }
300 }),
301 el(RangeControl, {
302 label: __('Spine height (px)'), value: a.spineHeight, min: 80, max: 320, __nextHasNoMarginBottom: true,
303 onChange: function (v) { set({ spineHeight: v }); }
304 }),
305 ) : null,
306 el(PanelBody, { title: __('Typography'), initialOpen: false },
307 el(getTypographyControl(), { label: __('Title', 'blockenberg'), value: a.typoTitle, onChange: function (v) { set({ typoTitle: v }); } }),
308 el(getTypographyControl(), { label: __('Body Text', 'blockenberg'), value: a.typoBody, onChange: function (v) { set({ typoBody: v }); } }),
309 el(RangeControl, {
310 label: __('Border radius'), value: a.borderRadius, min: 0, max: 32, __nextHasNoMarginBottom: true,
311 onChange: function (v) { set({ borderRadius: v }); }
312 }),
313 el(RangeControl, {
314 label: __('Spine font size'), value: a.spineFontSize, min: 8, max: 18, __nextHasNoMarginBottom: true,
315 onChange: function (v) { set({ spineFontSize: v }); }
316 })
317 ),
318 // Books editor
319 el(PanelBody, { title: __('Books (' + a.books.length + ')'), initialOpen: false },
320 a.books.map(function (b, idx) {
321 return el(PanelBody, {
322 key: idx,
323 title: (idx + 1) + '. ' + b.title.substring(0, 30),
324 initialOpen: false
325 },
326 el(TextControl, {
327 label: __('Title'), value: b.title, __nextHasNoMarginBottom: true,
328 onChange: function (v) { set({ books: upd(a.books, idx, 'title', v) }); }
329 }),
330 el(TextControl, {
331 label: __('Author'), value: b.author, __nextHasNoMarginBottom: true,
332 onChange: function (v) { set({ books: upd(a.books, idx, 'author', v) }); }
333 }),
334 el(TextControl, {
335 label: __('Genre'), value: b.genre || '', __nextHasNoMarginBottom: true,
336 onChange: function (v) { set({ books: upd(a.books, idx, 'genre', v) }); }
337 }),
338 el(TextControl, {
339 label: __('Year'), value: String(b.year || ''), __nextHasNoMarginBottom: true,
340 onChange: function (v) { var n = parseInt(v, 10); set({ books: upd(a.books, idx, 'year', isNaN(n) ? 0 : n) }); }
341 }),
342 el(SelectControl, {
343 label: __('Status'), value: b.status, options: STATUS_OPTIONS, __nextHasNoMarginBottom: true,
344 onChange: function (v) { set({ books: upd(a.books, idx, 'status', v) }); }
345 }),
346 el(RangeControl, {
347 label: __('Rating (0–5)'), value: b.rating, min: 0, max: 5, __nextHasNoMarginBottom: true,
348 onChange: function (v) { set({ books: upd(a.books, idx, 'rating', v) }); }
349 }),
350 // Color swatches
351 el('div', { style: { marginBottom: '10px' } },
352 el('p', { style: { fontSize: '11px', fontWeight: '600', textTransform: 'uppercase', margin: '0 0 8px' } }, __('Cover color')),
353 el('div', { style: { display: 'flex', gap: '6px', flexWrap: 'wrap' } },
354 SPINE_COLORS.map(function (c) {
355 return el('button', {
356 key: c,
357 onClick: function () { set({ books: upd(a.books, idx, 'coverColor', c) }); },
358 style: {
359 width: '26px', height: '26px', borderRadius: '4px', border: b.coverColor === c ? '3px solid #f8fafc' : '1px solid #475569',
360 background: c, cursor: 'pointer', padding: '0'
361 }
362 });
363 })
364 )
365 ),
366 el(Button, {
367 variant: 'secondary', isDestructive: true, __nextHasNoMarginBottom: true,
368 onClick: function () { var n = a.books.slice(); n.splice(idx, 1); set({ books: n }); }
369 }, __('Remove book'))
370 );
371 }),
372 el(Button, {
373 variant: 'primary', __nextHasNoMarginBottom: true,
374 onClick: function () {
375 var cols = SPINE_COLORS;
376 set({ books: a.books.concat([{
377 title: 'New Book', author: 'Author', genre: '', year: new Date().getFullYear(),
378 coverColor: cols[a.books.length % cols.length], status: 'want', rating: 0
379 }]) });
380 }
381 }, __('+ Add book'))
382 ),
383 el(PanelColorSettings, {
384 title: __('Colors'),
385 initialOpen: false,
386 colorSettings: colorSettings
387 })
388 );
389
390 // Header
391 var header = a.showTitle ? el('div', {
392 style: { padding: '20px 20px 10px', background: a.headerBg || '#2d1f14' }
393 },
394 el('h2', { className: 'bkbg-bsh-title', style: { color: a.titleColor || '#f5deb3', margin: '0' } }, a.shelfTitle)
395 ) : null;
396
397 // Shelf body
398 var body = a.layout === 'shelf' ? renderShelf(a)
399 : a.layout === 'cards' ? renderCards(a)
400 : renderList(a);
401
402 return el(Fragment, {},
403 inspector,
404 el('div', Object.assign({}, useBlockProps(), {
405 style: Object.assign({
406 background: a.bgColor || '#1c1410',
407 borderRadius: a.borderRadius + 'px',
408 overflow: 'hidden',
409 fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif',
410 minHeight: '100px'
411 }, _tv(a.typoTitle, '--bkbg-bsh-title-'), _tv(a.typoBody, '--bkbg-bsh-body-'))
412 }), header, body)
413 );
414 }
415
416 registerBlockType('blockenberg/book-shelf', {
417 edit: Edit,
418 save: function (props) {
419 return el('div', useBlockProps.save(),
420 el('div', { className: 'bkbg-book-shelf-app', 'data-opts': JSON.stringify(props.attributes) })
421 );
422 }
423 });
424 })();
425