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 / image-text-rows / index.js

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

210 lines 15.7 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 __ = wp.i18n.__;
4 var registerBlockType = wp.blocks.registerBlockType;
5 var InspectorControls = wp.blockEditor.InspectorControls;
6 var MediaUpload = wp.blockEditor.MediaUpload;
7 var MediaUploadCheck = wp.blockEditor.MediaUploadCheck;
8 var useBlockProps = wp.blockEditor.useBlockProps;
9 var RichText = wp.blockEditor.RichText;
10 var PanelColorSettings = wp.blockEditor.PanelColorSettings;
11 var PanelBody = wp.components.PanelBody;
12 var ToggleControl = wp.components.ToggleControl;
13 var RangeControl = wp.components.RangeControl;
14 var SelectControl = wp.components.SelectControl;
15 var TextControl = wp.components.TextControl;
16 var Button = wp.components.Button;
17
18 var _TypographyControl, _typoCssVars;
19 function getTypographyControl() { return _TypographyControl || (_TypographyControl = window.bkbgTypographyControl); }
20 function getTypoCssVars() { return _typoCssVars || (_typoCssVars = window.bkbgTypoCssVars); }
21
22 var POSITION_OPTIONS = [
23 { value: 'alternate', label: 'Alternate (image switches sides)' },
24 { value: 'left', label: 'Always Left' },
25 { value: 'right', label: 'Always Right' }
26 ];
27 var RATIO_OPTIONS = [
28 { value: '16:9', label: '16:9 Wide' },
29 { value: '4:3', label: '4:3 Standard' },
30 { value: '1:1', label: '1:1 Square' },
31 { value: '3:4', label: '3:4 Portrait' }
32 ];
33 var VALIGN_OPTIONS = [
34 { value: 'flex-start', label: 'Top' },
35 { value: 'center', label: 'Center' },
36 { value: 'flex-end', label: 'Bottom' }
37 ];
38
39 var RATIO_PAD = { '16:9': '56.25%', '4:3': '75%', '1:1': '100%', '3:4': '133.333%' };
40
41 function emptyRow() {
42 return { title: '', badge: '', description: '', features: [], btnLabel: 'Learn More', btnUrl: '', imageUrl: '', imageId: 0, imageAlt: '' };
43 }
44
45 registerBlockType('blockenberg/image-text-rows', {
46 edit: function (props) {
47 var attr = props.attributes;
48 var set = props.setAttributes;
49 var rows = attr.rows || [emptyRow()];
50
51 var _tv = getTypoCssVars();
52 var bpStyle = {
53 background: attr.bgColor || undefined,
54 paddingTop: attr.paddingTop + 'px',
55 paddingBottom: attr.paddingBottom + 'px'
56 };
57 Object.assign(bpStyle, _tv(attr.headingTypo, '--bkbg-itr-h-'));
58 Object.assign(bpStyle, _tv(attr.bodyTypo, '--bkbg-itr-bd-'));
59 var blockProps = useBlockProps({ style: bpStyle });
60
61 function updateRow(idx, field, val) {
62 var updated = rows.map(function (r, i) {
63 if (i !== idx) return r;
64 var p = {}; p[field] = val;
65 return Object.assign({}, r, p);
66 });
67 set({ rows: updated });
68 }
69
70 function updateFeature(rowIdx, fIdx, val) {
71 var updated = rows.map(function (r, i) {
72 if (i !== rowIdx) return r;
73 var newF = r.features.map(function (f, j) { return j === fIdx ? val : f; });
74 return Object.assign({}, r, { features: newF });
75 });
76 set({ rows: updated });
77 }
78
79 var controls = el(InspectorControls, {},
80 el(PanelBody, { title: __('Layout', 'blockenberg'), initialOpen: true },
81 el(SelectControl, { label: __('Image Position', 'blockenberg'), value: attr.imagePosition, options: POSITION_OPTIONS, __nextHasNoMarginBottom: true, onChange: function (v) { set({ imagePosition: v }); } }),
82 el('div', { style: { marginTop: '10px' } },
83 el(SelectControl, { label: __('Image Ratio', 'blockenberg'), value: attr.imageRatio, options: RATIO_OPTIONS, __nextHasNoMarginBottom: true, onChange: function (v) { set({ imageRatio: v }); } })
84 ),
85 el('div', { style: { marginTop: '10px' } },
86 el(RangeControl, { label: __('Image Width %', 'blockenberg'), value: attr.imageWidth, min: 30, max: 65, __nextHasNoMarginBottom: true, onChange: function (v) { set({ imageWidth: v }); } })
87 ),
88 el('div', { style: { marginTop: '10px' } },
89 el(RangeControl, { label: __('Image Radius', 'blockenberg'), value: attr.imageRadius, min: 0, max: 40, __nextHasNoMarginBottom: true, onChange: function (v) { set({ imageRadius: v }); } })
90 ),
91 el('div', { style: { marginTop: '8px' } },
92 el(ToggleControl, { label: __('Image Shadow', 'blockenberg'), checked: attr.imageShadow, __nextHasNoMarginBottom: true, onChange: function (v) { set({ imageShadow: v }); } })
93 ),
94 el('div', { style: { marginTop: '10px' } },
95 el(SelectControl, { label: __('Vertical Alignment', 'blockenberg'), value: attr.verticalAlign, options: VALIGN_OPTIONS, __nextHasNoMarginBottom: true, onChange: function (v) { set({ verticalAlign: v }); } })
96 ),
97 el('div', { style: { marginTop: '10px' } },
98 el(RangeControl, { label: __('Gap Between Rows', 'blockenberg'), value: attr.gap, min: 20, max: 160, __nextHasNoMarginBottom: true, onChange: function (v) { set({ gap: v }); } })
99 ),
100 el('div', { style: { marginTop: '8px' } },
101 el(ToggleControl, { label: __('Show Badges', 'blockenberg'), checked: attr.showBadges, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showBadges: v }); } })
102 ),
103 el('div', { style: { marginTop: '8px' } },
104 el(ToggleControl, { label: __('Show Feature List', 'blockenberg'), checked: attr.showFeatures, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showFeatures: v }); } })
105 ),
106 el('div', { style: { marginTop: '8px' } },
107 el(ToggleControl, { label: __('Show Buttons', 'blockenberg'), checked: attr.showBtns, __nextHasNoMarginBottom: true, onChange: function (v) { set({ showBtns: v }); } })
108 ),
109 el('div', { style: { marginTop: '10px' } },
110 el(RangeControl, { label: __('Padding Top', 'blockenberg'), value: attr.paddingTop, min: 0, max: 120, __nextHasNoMarginBottom: true, onChange: function (v) { set({ paddingTop: v }); } })
111 ),
112 el('div', { style: { marginTop: '10px' } },
113 el(RangeControl, { label: __('Padding Bottom', 'blockenberg'), value: attr.paddingBottom, min: 0, max: 120, __nextHasNoMarginBottom: true, onChange: function (v) { set({ paddingBottom: v }); } })
114 )
115 ),
116 el(PanelColorSettings, {
117 title: __('Colors', 'blockenberg'), initialOpen: false,
118 colorSettings: [
119 { label: __('Section Background', 'blockenberg'), value: attr.bgColor, onChange: function (v) { set({ bgColor: v || '' }); } },
120 { label: __('Badge Background', 'blockenberg'), value: attr.badgeBg, onChange: function (v) { set({ badgeBg: v || '#ede9fe' }); } },
121 { label: __('Badge Text', 'blockenberg'), value: attr.badgeColor, onChange: function (v) { set({ badgeColor: v || '#7c3aed' }); } },
122 { label: __('Title', 'blockenberg'), value: attr.titleColor, onChange: function (v) { set({ titleColor: v || '#111827' }); } },
123 { label: __('Description', 'blockenberg'), value: attr.descColor, onChange: function (v) { set({ descColor: v || '#374151' }); } },
124 { label: __('Feature Text', 'blockenberg'), value: attr.featureColor, onChange: function (v) { set({ featureColor: v || '#374151' }); } },
125 { label: __('Feature Dot', 'blockenberg'), value: attr.featureDot, onChange: function (v) { set({ featureDot: v || '#7c3aed' }); } },
126 { label: __('Button Background', 'blockenberg'), value: attr.btnBg, onChange: function (v) { set({ btnBg: v || '#7c3aed' }); } },
127 { label: __('Button Text', 'blockenberg'), value: attr.btnColor, onChange: function (v) { set({ btnColor: v || '#ffffff' }); } }
128 ]
129 }),
130 el(PanelBody, { title: 'Typography', initialOpen: false },
131 el(getTypographyControl(), { label: 'Heading', value: attr.headingTypo, onChange: function(v){ set({ headingTypo: v }); } }),
132 el(getTypographyControl(), { label: 'Body', value: attr.bodyTypo, onChange: function(v){ set({ bodyTypo: v }); } })
133 ),
134
135 );
136
137 return el('div', blockProps,
138 controls,
139 el('div', { style: { display: 'flex', flexDirection: 'column', gap: attr.gap + 'px' } },
140 rows.map(function (row, idx) {
141 var imgLeft = attr.imagePosition === 'left' || (attr.imagePosition === 'alternate' && idx % 2 === 0);
142 var features = row.features || [];
143
144 var imgCol = el('div', { style: { flex: '0 0 ' + attr.imageWidth + '%', maxWidth: attr.imageWidth + '%' } },
145 el('div', { style: { position: 'relative', paddingBottom: RATIO_PAD[attr.imageRatio] || '75%', background: '#f3f4f6', borderRadius: attr.imageRadius + 'px', overflow: 'hidden', boxShadow: attr.imageShadow ? '0 8px 30px rgba(0,0,0,0.12)' : 'none' } },
146 row.imageUrl
147 ? el('img', { src: row.imageUrl, alt: row.imageAlt, style: { position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover' } })
148 : el(MediaUploadCheck, {},
149 el(MediaUpload, {
150 onSelect: function (m) { updateRow(idx, 'imageUrl', m.url); updateRow(idx, 'imageId', m.id); updateRow(idx, 'imageAlt', m.alt || ''); },
151 allowedTypes: ['image'], value: row.imageId,
152 render: function (r) { return el(Button, { onClick: r.open, style: { position: 'absolute', inset: 0, width: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center', background: 'transparent', border: 'none', cursor: 'pointer', color: '#9ca3af', fontSize: '13px' } }, '+ Add Image'); }
153 })
154 ),
155 row.imageUrl && el('div', { style: { position: 'absolute', bottom: '8px', right: '8px' } },
156 el(Button, { isSmall: true, variant: 'secondary', onClick: function () { updateRow(idx, 'imageUrl', ''); updateRow(idx, 'imageId', 0); } }, '')
157 )
158 )
159 );
160
161 var textCol = el('div', { style: { flex: 1, minWidth: 0 } },
162 attr.showBadges && el(TextControl, { value: row.badge, label: '', placeholder: __('Badge text…', 'blockenberg'), __nextHasNoMarginBottom: true, style: { fontSize: '12px', marginBottom: '10px', width: '160px' }, onChange: function (v) { updateRow(idx, 'badge', v); } }),
163 el(RichText, { tagName: 'h2', className: 'bkbg-itr-title', value: row.title, allowedFormats: ['core/bold', 'core/italic'], placeholder: __('Row title…', 'blockenberg'), style: { margin: '0 0 14px', color: attr.titleColor }, onChange: function (v) { updateRow(idx, 'title', v); } }),
164 el(RichText, { tagName: 'p', className: 'bkbg-itr-desc', value: row.description, allowedFormats: ['core/bold', 'core/italic', 'core/link'], placeholder: __('Row description…', 'blockenberg'), style: { margin: '0 0 16px', color: attr.descColor }, onChange: function (v) { updateRow(idx, 'description', v); } }),
165
166 attr.showFeatures && el('div', { style: { marginBottom: '16px' } },
167 features.map(function (f, fi) {
168 return el('div', { key: fi, style: { display: 'flex', alignItems: 'center', gap: '8px', marginBottom: '6px' } },
169 el('span', { style: { width: '8px', height: '8px', borderRadius: '50%', background: attr.featureDot, flexShrink: 0 } }),
170 el(TextControl, { value: f, label: '', placeholder: __('Feature…', 'blockenberg'), __nextHasNoMarginBottom: true, style: { flex: 1, fontSize: '14px', color: attr.featureColor }, onChange: function (v) { updateFeature(idx, fi, v); } }),
171 el(Button, { isSmall: true, isDestructive: true, variant: 'tertiary', onClick: function () {
172 updateRow(idx, 'features', features.filter(function (_, j) { return j !== fi; }));
173 } }, '×')
174 );
175 }),
176 el(Button, { variant: 'link', style: { fontSize: '11px' }, onClick: function () { updateRow(idx, 'features', features.concat([''])); } }, '+ Add Feature')
177 ),
178
179 attr.showBtns && el('div', { style: { display: 'flex', gap: '10px', alignItems: 'center', flexWrap: 'wrap' } },
180 el(TextControl, { value: row.btnLabel, label: '', placeholder: __('Button label…', 'blockenberg'), __nextHasNoMarginBottom: true, style: { width: '130px', fontSize: '13px' }, onChange: function (v) { updateRow(idx, 'btnLabel', v); } }),
181 el(TextControl, { value: row.btnUrl, label: '', placeholder: __('https://…', 'blockenberg'), __nextHasNoMarginBottom: true, style: { flex: 1, fontSize: '13px' }, onChange: function (v) { updateRow(idx, 'btnUrl', v); } }),
182 row.btnLabel && el('span', { style: { background: attr.btnBg, color: attr.btnColor, padding: '8px 18px', borderRadius: '6px', fontSize: '14px', fontWeight: 600, whiteSpace: 'nowrap' } }, row.btnLabel)
183 )
184 );
185
186 return el('div', { key: idx, style: { display: 'flex', alignItems: attr.verticalAlign, gap: '40px', flexWrap: 'wrap' } },
187 imgLeft ? imgCol : textCol,
188 imgLeft ? textCol : imgCol,
189 el('div', { style: { width: '100%', display: 'flex', justifyContent: 'flex-end', gap: '6px', marginTop: '4px' } },
190 el(Button, { isSmall: true, variant: 'secondary', onClick: function () { set({ rows: rows.concat([Object.assign({}, row)]) }); } }, '⧉ Duplicate Row'),
191 el(Button, { isSmall: true, isDestructive: true, variant: 'tertiary', onClick: function () { set({ rows: rows.filter(function (_, i) { return i !== idx; }) }); } }, '× Remove Row')
192 )
193 );
194 })
195 ),
196 el('div', { style: { marginTop: '20px', textAlign: 'center' } },
197 el(Button, { variant: 'primary', onClick: function () { set({ rows: rows.concat([emptyRow()]) }); } }, '+ Add Row')
198 )
199 );
200 },
201 save: function (props) {
202 var attr = props.attributes;
203 var useBlockProps = wp.blockEditor.useBlockProps;
204 return el('div', useBlockProps.save(),
205 el('div', { className: 'bkbg-itr-app', 'data-opts': JSON.stringify(attr) })
206 );
207 }
208 });
209 }() );
210