PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / trunk
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts vtrunk
2.8.14 2.8.13 2.8.12 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.10 2.8.11 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 trunk 1.0.1 1.0.2 1.1.0 1.1.1 All 147 releases
content-blocks-builder / src / custom-blocks / transforms.js

transforms.js in Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts trunk, at src/custom-blocks/transforms.js

296 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * External dependencies
3 */
4
5 /**
6 * WordPress dependencies
7 */
8 import {
9 createBlock,
10 createBlocksFromInnerBlocksTemplate,
11 } from "@wordpress/blocks";
12
13 /**
14 * Internal dependencies
15 */
16 import { boldblocksHasSupport } from "../utils";
17
18 const isRepeaterLayout = (layoutType) =>
19 layoutType && ["grid", "carousel", "vstack"].includes(layoutType);
20
21 const convertAttributes = (attributes) => {
22 // Layout: align, layout
23 // Color: backgroundColor, textColor, gradient, borderColor
24 // Style: border: {radius: {bottomRight...}, style, width, color}, color: {background, text, gradient}, spacing: {padding, margin, blockGap}, typography: {fontSize, fontStyle, fontWeight, letterSpacing, lineHeight, textTransform}
25 // fontSize: 'predefined'
26 const {
27 align,
28 layout,
29 backgroundColor,
30 textColor,
31 borderColor,
32 gradient,
33 fontFamily,
34 fontSize,
35 style,
36 boldblocks,
37 } = attributes;
38
39 return {
40 align,
41 layout,
42 backgroundColor,
43 textColor,
44 borderColor,
45 gradient,
46 fontFamily,
47 fontSize,
48 style,
49 boldblocks,
50 };
51 };
52
53 const convertToSingleBlock = (blockTypeName, blocks) => {
54 const alignments = ["wide", "full"];
55
56 // Determine the widest setting of all the blocks to be grouped
57 const widestAlignment = blocks.reduce((accumulator, block) => {
58 const { align } = block.attributes;
59 return alignments.indexOf(align) > alignments.indexOf(accumulator)
60 ? align
61 : accumulator;
62 }, undefined);
63
64 return createBlock(
65 blockTypeName,
66 {
67 align: widestAlignment,
68 },
69 createBlocksFromInnerBlocksTemplate(blocks),
70 );
71 };
72
73 const convertToRepeater = (
74 blockTypeName,
75 layoutType,
76 childBlockTypeName,
77 blocks,
78 ) => {
79 let attributes = {};
80 if (layoutType === "grid") {
81 attributes = {
82 boldblocks: {
83 grid: {
84 columns: {
85 lg: {
86 value: `repeat(${Math.min(6, blocks.length)}, minmax(0, 1fr))`,
87 inherit: null,
88 },
89 md: { inherit: "lg" },
90 sm: { inherit: "lg" },
91 },
92 gap: {
93 lg: {
94 value: { row: "1rem", column: "1rem" },
95 inherit: null,
96 },
97 md: { inherit: "lg" },
98 sm: { inherit: "lg" },
99 },
100 },
101 },
102 };
103 }
104 const innerBlocksTemplate = blocks.map(
105 ({ name, attributes, innerBlocks }) => [
106 childBlockTypeName,
107 {},
108 [[name, { ...attributes }, innerBlocks]],
109 ],
110 );
111
112 return createBlock(
113 blockTypeName,
114 attributes,
115 createBlocksFromInnerBlocksTemplate(innerBlocksTemplate),
116 );
117 };
118
119 const convertFromCoreGroup = (blockTypeName, { attributes, innerBlocks }) => {
120 if (attributes?.layout?.type === "flex") {
121 delete attributes?.layout;
122 }
123
124 return createBlock(blockTypeName, convertAttributes(attributes), innerBlocks);
125 };
126
127 const convertFromColumns = (
128 blockTypeName,
129 layoutType,
130 childBlockTypeName,
131 { attributes, innerBlocks },
132 ) => {
133 const innerBlocksTemplate = innerBlocks.map(({ attributes, innerBlocks }) => [
134 childBlockTypeName,
135 convertAttributes(attributes),
136 innerBlocks,
137 ]);
138
139 let newAttributes = convertAttributes(attributes);
140
141 if (layoutType === "grid") {
142 newAttributes = {
143 ...newAttributes,
144 boldblocks: {
145 grid: {
146 columns: {
147 lg: {
148 value: `repeat(${innerBlocks.length}, minmax(0, 1fr))`,
149 inherit: null,
150 },
151 md: { inherit: "lg" },
152 sm: { inherit: null },
153 },
154 gap: {
155 lg: {
156 value: { row: "1rem", column: "1rem" },
157 inherit: null,
158 },
159 md: { inherit: "lg" },
160 sm: { inherit: "lg" },
161 },
162 },
163 },
164 };
165 }
166
167 return createBlock(
168 blockTypeName,
169 newAttributes,
170 createBlocksFromInnerBlocksTemplate(innerBlocksTemplate),
171 );
172 };
173
174 const convertFromRepeaterToRepeater = (
175 blockTypeName,
176 childBlockTypeName,
177 toLayoutType,
178 fromLayoutType,
179 block,
180 ) => {
181 const { attributes, innerBlocks, clientId } = block;
182 if (block.name === blockTypeName) {
183 return block;
184 }
185
186 const innerBlocksTemplate = innerBlocks.map(({ attributes, innerBlocks }) => [
187 childBlockTypeName,
188 { ...attributes },
189 innerBlocks,
190 ]);
191
192 let newAttributes = {};
193 if (toLayoutType === "grid") {
194 newAttributes = {
195 ...attributes,
196 boldblocks: {
197 ...(attributes?.boldblocks ?? {}),
198 grid: {
199 columns: {
200 lg: {
201 value: `repeat(${Math.min(
202 6,
203 innerBlocks.length,
204 )}, minmax(0, 1fr))`,
205 inherit: null,
206 },
207 md: { inherit: "lg" },
208 sm: { inherit: null },
209 },
210 gap: {
211 lg: {
212 value: { row: "1rem", column: "1rem" },
213 inherit: null,
214 },
215 md: { inherit: "lg" },
216 sm: { inherit: "lg" },
217 },
218 },
219 },
220 };
221 }
222
223 return createBlock(
224 blockTypeName,
225 newAttributes,
226 createBlocksFromInnerBlocksTemplate(innerBlocksTemplate),
227 );
228 };
229
230 const buildTransforms = (
231 blockTypeName,
232 layoutType = false,
233 childBlockTypeName,
234 ) => {
235 const convert =
236 (blockTypeName, layoutType, childBlockTypeName) => (blocks) => {
237 if (blocks.length === 1) {
238 const block = blocks[0];
239 if (block.name === "core/group") {
240 return convertFromCoreGroup(blockTypeName, block);
241 } else if (block.name === "core/columns") {
242 if (isRepeaterLayout(layoutType)) {
243 return convertFromColumns(
244 blockTypeName,
245 layoutType,
246 childBlockTypeName,
247 block,
248 );
249 }
250 } else {
251 if (isRepeaterLayout(layoutType)) {
252 const layout = boldblocksHasSupport(block.name, "layoutType");
253 if (isRepeaterLayout(layout)) {
254 return convertFromRepeaterToRepeater(
255 blockTypeName,
256 childBlockTypeName,
257 layoutType,
258 layout,
259 block,
260 );
261 }
262 }
263 }
264 }
265
266 if (childBlockTypeName && isRepeaterLayout(layoutType)) {
267 return convertToRepeater(
268 blockTypeName,
269 layoutType,
270 childBlockTypeName,
271 blocks,
272 );
273 } else {
274 return convertToSingleBlock(blockTypeName, blocks);
275 }
276 };
277
278 // Default
279 return {
280 from: [
281 {
282 type: "block",
283 isMultiBlock: true,
284 blocks: ["*"],
285 __experimentalConvert: convert(
286 blockTypeName,
287 layoutType,
288 childBlockTypeName,
289 ),
290 },
291 ],
292 };
293 };
294
295 export default buildTransforms;
296