@vonaffenfels/slate-editor 1.2.30 → 1.2.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +43 -43
- package/README.md +5 -5
- package/componentLoader.js +93 -93
- package/dist/BlockEditor.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
- package/postcss.config.js +6 -6
- package/scss/demo.scss +148 -148
- package/scss/sidebarEditor.scss +185 -185
- package/scss/toolbar.scss +162 -162
- package/src/Blocks/EmptyBlock.js +11 -11
- package/src/Blocks/EmptyWrapper.js +4 -4
- package/src/Blocks/ErrorBoundary.js +40 -40
- package/src/Blocks/LayoutBlock.js +274 -274
- package/src/Blocks/LayoutSlot.js +90 -90
- package/src/CollapsableMenu/CollapsableMenu.js +48 -48
- package/src/Context/StorybookContext.js +6 -6
- package/src/ElementAutocomplete.js +134 -134
- package/src/Loader.js +137 -137
- package/src/Nodes/Default.js +162 -162
- package/src/Nodes/Leaf.js +54 -54
- package/src/Nodes/Text.js +97 -97
- package/src/ObjectId.js +3 -3
- package/src/Renderer.js +73 -73
- package/src/Serializer/Html.js +42 -42
- package/src/Serializer/Serializer.js +374 -374
- package/src/Serializer/Text.js +17 -17
- package/src/Serializer/ads.js +187 -187
- package/src/Serializer/index.js +3 -3
- package/src/SidebarEditor/AssetList.js +185 -181
- package/src/SidebarEditor/Fields/CloudinaryContentSelect.js +89 -89
- package/src/SidebarEditor/Fields/ColorPicker.js +89 -89
- package/src/SidebarEditor/Fields/ContentfulContentSelect.js +63 -62
- package/src/SidebarEditor/Fields/DateTime.js +55 -55
- package/src/SidebarEditor/Fields/MVP.js +66 -66
- package/src/SidebarEditor/Fields/MultiSelect.js +13 -13
- package/src/SidebarEditor/Fields/RemoteMultiSelect.js +40 -40
- package/src/SidebarEditor/Fields/RemoteSelect.js +39 -39
- package/src/SidebarEditor/Fields/Select.js +47 -47
- package/src/SidebarEditor/Fields/StreamSelect.js +15 -15
- package/src/SidebarEditor/Fields/Switch.js +34 -34
- package/src/SidebarEditor/Fields/Textarea.js +21 -21
- package/src/SidebarEditor/Resizable.js +85 -85
- package/src/Storybook.js +151 -151
- package/src/Toolbar/Align.js +64 -64
- package/src/Toolbar/Anchor.js +94 -94
- package/src/Toolbar/Block.js +135 -135
- package/src/Toolbar/Element.js +44 -44
- package/src/Toolbar/Formats.js +71 -71
- package/src/Toolbar/Insert.js +28 -28
- package/src/Toolbar/Layout.js +399 -399
- package/src/Toolbar/Link.js +164 -164
- package/src/Toolbar/Toolbar.js +235 -235
- package/src/Tools/Margin.js +51 -51
- package/src/Translation/TranslationToolbarButton.js +119 -119
- package/src/dev/draftToSlate.json +3147 -3147
- package/src/dev/index.css +2 -2
- package/src/dev/index.html +10 -10
- package/src/dev/index.js +4 -4
- package/src/dev/sampleValue1.json +4294 -4294
- package/src/dev/sampleValueValid.json +410 -410
- package/src/dev/testComponents/TestStory.js +74 -74
- package/src/dev/testComponents/TestStory.stories.js +216 -216
- package/src/dev/testComponents/TestStory2.js +74 -74
- package/src/dev/testComponents/TestStory2.stories.js +197 -197
- package/src/dev/testComponents/TestStory3.js +74 -74
- package/src/dev/testComponents/TestStory3.stories.js +197 -197
- package/src/dev/testSampleValue.json +746 -746
- package/src/fromHTML.js +4 -4
- package/src/helper/array.js +8 -8
- package/src/index.js +10 -10
- package/src/plugins/ListItem.js +48 -48
- package/src/plugins/SoftBreak.js +23 -23
- package/src/toHTML.js +6 -6
- package/src/toText.js +6 -6
- package/src/util/reduceContentfulResponse.js +64 -64
- package/src/util.js +19 -19
- package/storyLoader.js +47 -47
- package/tailwind.config.js +4 -4
- package/webpack.config.build.js +55 -55
- package/webpack.config.dev.js +60 -60
- package/webpack.config.js +130 -130
- package/webpack.config.watch.js +4 -4
|
@@ -1,375 +1,375 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
isValidElement,
|
|
3
|
-
Fragment, useMemo,
|
|
4
|
-
} from "react";
|
|
5
|
-
import {Default} from "../Nodes/Default";
|
|
6
|
-
import {Leaf} from "../Nodes/Leaf";
|
|
7
|
-
import {StorybookDisplay} from "../Nodes/StorybookDisplay";
|
|
8
|
-
import classNames from "classnames";
|
|
9
|
-
import {addAdsToValue} from "./ads";
|
|
10
|
-
import ErrorBoundary from "../Blocks/ErrorBoundary";
|
|
11
|
-
|
|
12
|
-
function isEmptyNode(node) {
|
|
13
|
-
if (!node.type) {
|
|
14
|
-
if (!String(node.text).trim()) {
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function getTextContent(node, text = "") {
|
|
23
|
-
if (!node) {
|
|
24
|
-
return text;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (!node.children || !node.children.length) {
|
|
28
|
-
if (!node.text) {
|
|
29
|
-
return text;
|
|
30
|
-
} else {
|
|
31
|
-
text += node.text;
|
|
32
|
-
}
|
|
33
|
-
} else {
|
|
34
|
-
return node.children.map(v => getTextContent(v, text)).join("").trim();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return text;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function isValidNodeForPlacement(node) {
|
|
41
|
-
return true;
|
|
42
|
-
// this is disabled, elements should just clear, otherwise too many ads are missing because of short intro texts and such ...
|
|
43
|
-
/*
|
|
44
|
-
if (node && node.type === "paragraph") {
|
|
45
|
-
const text = getTextContent(node);
|
|
46
|
-
|
|
47
|
-
if (text.length < 200) {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return true;
|
|
53
|
-
*/
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function Serializer({
|
|
57
|
-
value = [],
|
|
58
|
-
elementPropsMap,
|
|
59
|
-
specialClassMap,
|
|
60
|
-
transformAttributes,
|
|
61
|
-
storybookComponentLoader,
|
|
62
|
-
storybookComponentDataLoader,
|
|
63
|
-
defaultComponentReplacement,
|
|
64
|
-
modifyElementFunc,
|
|
65
|
-
filterContent = null,
|
|
66
|
-
isRenderer = false,
|
|
67
|
-
fixedPositions = {},
|
|
68
|
-
fixedPositionTypes = {},
|
|
69
|
-
adDefinitionDesktop = [],
|
|
70
|
-
adDefinitionMobile = [],
|
|
71
|
-
}) {
|
|
72
|
-
const processedValue = useMemo(() => {
|
|
73
|
-
let val = addAdsToValue(value, adDefinitionDesktop, adDefinitionMobile);
|
|
74
|
-
val = addFixedPositionsToValue(val, fixedPositions, fixedPositionTypes);
|
|
75
|
-
return val;
|
|
76
|
-
}, [value, adDefinitionDesktop, adDefinitionMobile, fixedPositions, fixedPositionTypes]);
|
|
77
|
-
|
|
78
|
-
const filteredValue = useMemo(() => {
|
|
79
|
-
const context = {content: processedValue};
|
|
80
|
-
return processedValue.filter(v => {
|
|
81
|
-
try {
|
|
82
|
-
return filterContent ? filterContent(v, context) : true;
|
|
83
|
-
} catch (e) {
|
|
84
|
-
console.error("[Serializer] Error in filterContent prop function: ", e);
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}, [processedValue, filterContent]);
|
|
89
|
-
|
|
90
|
-
const getPropsForType = (type, isInSlot = false) => {
|
|
91
|
-
if (!elementPropsMap) {
|
|
92
|
-
return {};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (isInSlot) {
|
|
96
|
-
if (elementPropsMap[type + "-in-slot"]) {
|
|
97
|
-
return elementPropsMap[type + "-in-slot"];
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return elementPropsMap[type] || {};
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
const renderElement = (props, isInSlot = false) => {
|
|
105
|
-
const typeProps = getPropsForType(props.type, isInSlot);
|
|
106
|
-
const Wrapper = typeProps.wrapper || function ({children}) {
|
|
107
|
-
return <ErrorBoundary>{children}</ErrorBoundary>;
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
switch (props.type) {
|
|
111
|
-
case 'storybook': {
|
|
112
|
-
const storyProps = {
|
|
113
|
-
element: {
|
|
114
|
-
...props,
|
|
115
|
-
attributes: {
|
|
116
|
-
...(props.attributes || {}),
|
|
117
|
-
isInSlot: isInSlot,
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
attributes: props.attributes,
|
|
121
|
-
storybookComponentLoader: storybookComponentLoader,
|
|
122
|
-
storybookComponentDataLoader: storybookComponentDataLoader,
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
return <ErrorBoundary><Wrapper
|
|
126
|
-
type={props.type}
|
|
127
|
-
block={props.block}><StorybookDisplay {...storyProps} /></Wrapper></ErrorBoundary>;
|
|
128
|
-
}
|
|
129
|
-
case 'layout': {
|
|
130
|
-
let isGrid = true;
|
|
131
|
-
|
|
132
|
-
if (Array.isArray(props.children)) {
|
|
133
|
-
props.children.forEach(child => {
|
|
134
|
-
if (child.type === "layout-slot" && ["grid-auto", "grid-fill"].includes(child.attributes.name)) {
|
|
135
|
-
isGrid = false;
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return <ErrorBoundary>
|
|
141
|
-
<Wrapper type={props.type}>
|
|
142
|
-
<div
|
|
143
|
-
data-cols={props.children.length}
|
|
144
|
-
className={classNames("block-editor-layout", {
|
|
145
|
-
"block-editor-layout-grid": isGrid,
|
|
146
|
-
"block-editor-layout-flex": !isGrid,
|
|
147
|
-
"block-editor-table-border-wrapper": props?.attributes?.tableBorder,
|
|
148
|
-
"md:grid-cols-1": props.children.length === 1,
|
|
149
|
-
"md:grid-cols-2": props.children.length === 2,
|
|
150
|
-
"md:grid-cols-3": props.children.length === 3,
|
|
151
|
-
"md:grid-cols-4": props.children.length === 4,
|
|
152
|
-
"md:grid-cols-5": props.children.length === 5,
|
|
153
|
-
"grid-cols-1": props?.attributes?.mobileColumnSpan === 1,
|
|
154
|
-
"grid-cols-2": props?.attributes?.mobileColumnSpan === 2,
|
|
155
|
-
"md:space-x-4": props.attributes?.spacing,
|
|
156
|
-
"mt-16": props?.attributes?.margin?.top,
|
|
157
|
-
"mr-16": props?.attributes?.margin?.right,
|
|
158
|
-
"mb-16": props?.attributes?.margin?.bottom,
|
|
159
|
-
"ml-16": props?.attributes?.margin?.left,
|
|
160
|
-
"justify-center": props?.attributes?.justifyCenter,
|
|
161
|
-
[typeProps.classNameSite + " site-width"]: !isInSlot && (props.attributes?.width === "site" || props.attributes?.width === "full"),
|
|
162
|
-
[typeProps.classNameArticle + " article-width"]: !isInSlot && (props.attributes?.width === "article" || !props.attributes?.width),
|
|
163
|
-
})}
|
|
164
|
-
>
|
|
165
|
-
{props.children.map((v, i) => <Fragment
|
|
166
|
-
key={"layout-pos-" + i}>{serializeNode(v, i, isInSlot)}</Fragment>)}
|
|
167
|
-
</div>
|
|
168
|
-
</Wrapper>
|
|
169
|
-
</ErrorBoundary>;
|
|
170
|
-
}
|
|
171
|
-
case 'layout-slot':
|
|
172
|
-
return <ErrorBoundary>
|
|
173
|
-
<Wrapper type={props.type}>
|
|
174
|
-
<div
|
|
175
|
-
className={classNames({
|
|
176
|
-
["block-editor-layout-" + props.attributes.name]: true,
|
|
177
|
-
"mt-4": props.attributes?.margin?.top,
|
|
178
|
-
"md:mr-4": props.attributes?.margin?.right,
|
|
179
|
-
"mb-4": props.attributes?.margin?.bottom,
|
|
180
|
-
"md:ml-4": props.attributes?.margin?.left,
|
|
181
|
-
})}
|
|
182
|
-
>
|
|
183
|
-
<div
|
|
184
|
-
style={{top: props.attributes?.sticky && "80px"}}
|
|
185
|
-
className={classNames({"sticky": props.attributes?.sticky})}>
|
|
186
|
-
{props.children.map((v, i) => <div
|
|
187
|
-
key={"layout-slot-" + i}
|
|
188
|
-
>{serializeNode(v, i, props.attributes.name)}</div>)}
|
|
189
|
-
</div>
|
|
190
|
-
</div>
|
|
191
|
-
</Wrapper>
|
|
192
|
-
</ErrorBoundary>;
|
|
193
|
-
default: {
|
|
194
|
-
const defaultProps = {
|
|
195
|
-
specialClassMap: specialClassMap,
|
|
196
|
-
elementPropsMap: elementPropsMap,
|
|
197
|
-
element: {
|
|
198
|
-
attributes: props.attributes || {},
|
|
199
|
-
align: props.align,
|
|
200
|
-
type: props.type,
|
|
201
|
-
},
|
|
202
|
-
attributes: props.htmlAttributes || {},
|
|
203
|
-
children: props.children.map((v, i) => <Fragment
|
|
204
|
-
key={props.type + "-pos-item-" + i}>{serializeNode(v, i, isInSlot)}</Fragment>),
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
return <ErrorBoundary>
|
|
208
|
-
<Default
|
|
209
|
-
{...defaultProps}
|
|
210
|
-
isInSlot={isInSlot}
|
|
211
|
-
isRenderer={isRenderer}
|
|
212
|
-
transformAttributes={transformAttributes}
|
|
213
|
-
defaultComponentReplacement={defaultComponentReplacement}
|
|
214
|
-
/>
|
|
215
|
-
</ErrorBoundary>;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
function serializeNode(node, i, isInSlot = false) {
|
|
222
|
-
if (isValidElement(node)) {
|
|
223
|
-
return node;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
if (typeof modifyElementFunc === "function") {
|
|
227
|
-
node = modifyElementFunc(node);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (node.type === "paragraph" && !node.text) {
|
|
231
|
-
if (!node.children || node.children.length === 0) {
|
|
232
|
-
return null;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
let children = node.children.filter((v) => !isEmptyNode(v));
|
|
236
|
-
|
|
237
|
-
if (children.length === 0) {
|
|
238
|
-
return null;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if (!node.children || !node.children.length) {
|
|
243
|
-
return serializeLeaf(node);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// unwrap elements that are inside of a paragraph (that has no text content) so they have been inserted "inline" by mistake
|
|
247
|
-
// this helps preventing a lot of hydration errors!
|
|
248
|
-
if (node?.type === "paragraph") {
|
|
249
|
-
const textContent = node.children.find(v => !v?.type && v?.text);
|
|
250
|
-
if (!textContent) {
|
|
251
|
-
const storybookNodes = node.children.filter(v => v?.type === "storybook");
|
|
252
|
-
if (storybookNodes.length === 1) {
|
|
253
|
-
// only unwrap if we only have exactly 1 node
|
|
254
|
-
return renderElement(storybookNodes.at(0), isInSlot);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
return renderElement(node, isInSlot);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
function serializeLeaf(props) {
|
|
263
|
-
return <Leaf leaf={props} isRenderer={true} typeProps={getPropsForType("leaf")}>{props.text || ""}</Leaf>;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
if (!value) {
|
|
267
|
-
return null;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return <>
|
|
271
|
-
{!!filteredValue && filteredValue.map((v, i) => {
|
|
272
|
-
return <Fragment key={v.block + "-pos-" + i}>{serializeNode(v, i)}</Fragment>;
|
|
273
|
-
})}
|
|
274
|
-
</>;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
function getFixedPosition(fixedPositions, i) {
|
|
278
|
-
return fixedPositions?.[i] || null;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function isShortParagraph(node) {
|
|
282
|
-
if (node?.type === "paragraph") {
|
|
283
|
-
let textLength = node.children.map(v => v?.text?.length || 0).reduce((pv, cv) => pv + cv, 0) || 0;
|
|
284
|
-
return textLength <= 60;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return false;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
function getFixedPositionPlacement(node, prevNode, fixedPosType) {
|
|
291
|
-
// inline has to be before
|
|
292
|
-
if (fixedPosType === "inline") {
|
|
293
|
-
return "before";
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
let placement = "before";
|
|
297
|
-
|
|
298
|
-
if (node?.type === "paragraph" && prevNode?.type === "heading" || isShortParagraph(prevNode)) {
|
|
299
|
-
placement = "after";
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return placement;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
function isNodeValidForCounter(node, counters = {}) {
|
|
306
|
-
let isValid = true;
|
|
307
|
-
|
|
308
|
-
if (node.type === "paragraph") {
|
|
309
|
-
let lengthCounterId = `paragraph_textlength`;
|
|
310
|
-
|
|
311
|
-
if (!counters[lengthCounterId]) {
|
|
312
|
-
counters[lengthCounterId] = 0;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
let textLength = node.children.map(v => v?.text?.length || 0).reduce((pv, cv) => pv + cv, 0) || 0;
|
|
316
|
-
let isHeadline = textLength <= 60;
|
|
317
|
-
|
|
318
|
-
counters[lengthCounterId] += textLength;
|
|
319
|
-
|
|
320
|
-
isValid = !isHeadline && counters[lengthCounterId] >= 200;
|
|
321
|
-
|
|
322
|
-
if (isValid) {
|
|
323
|
-
counters[lengthCounterId] = 0;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
return isValid;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
function addFixedPositionsToValue(
|
|
331
|
-
value, fixedPositions, fixedPositionTypes, counters = {},
|
|
332
|
-
) {
|
|
333
|
-
if (!Array.isArray(value)) {
|
|
334
|
-
return value;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
let processedValue = [];
|
|
338
|
-
|
|
339
|
-
for (let i = 0; i < value.length; i++) {
|
|
340
|
-
let node = value[i];
|
|
341
|
-
let prevNode = value?.[i - 1];
|
|
342
|
-
|
|
343
|
-
if (!node.type) {
|
|
344
|
-
processedValue.push(node);
|
|
345
|
-
continue;
|
|
346
|
-
}
|
|
347
|
-
if (!counters[node.type]) {
|
|
348
|
-
counters[node.type] = 0;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
let counterId = `${node.type}_${counters[node.type]}`;
|
|
352
|
-
|
|
353
|
-
// only count this node if its valid to be counted (dont count strong headlines and so on)
|
|
354
|
-
if (isNodeValidForCounter(node, counters)) {
|
|
355
|
-
counters[node.type]++;
|
|
356
|
-
let fixedPosType = fixedPositionTypes?.[counterId];
|
|
357
|
-
let fixedPos = getFixedPosition(fixedPositions, counterId);
|
|
358
|
-
let placement = getFixedPositionPlacement(node, prevNode, fixedPosType);
|
|
359
|
-
|
|
360
|
-
if (fixedPos && placement === "before") {
|
|
361
|
-
processedValue.push(fixedPos);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
processedValue.push(node);
|
|
365
|
-
|
|
366
|
-
if (fixedPos && placement === "after") {
|
|
367
|
-
processedValue.push(fixedPos);
|
|
368
|
-
}
|
|
369
|
-
} else {
|
|
370
|
-
processedValue.push(node);
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
return processedValue;
|
|
1
|
+
import React, {
|
|
2
|
+
isValidElement,
|
|
3
|
+
Fragment, useMemo,
|
|
4
|
+
} from "react";
|
|
5
|
+
import {Default} from "../Nodes/Default";
|
|
6
|
+
import {Leaf} from "../Nodes/Leaf";
|
|
7
|
+
import {StorybookDisplay} from "../Nodes/StorybookDisplay";
|
|
8
|
+
import classNames from "classnames";
|
|
9
|
+
import {addAdsToValue} from "./ads";
|
|
10
|
+
import ErrorBoundary from "../Blocks/ErrorBoundary";
|
|
11
|
+
|
|
12
|
+
function isEmptyNode(node) {
|
|
13
|
+
if (!node.type) {
|
|
14
|
+
if (!String(node.text).trim()) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getTextContent(node, text = "") {
|
|
23
|
+
if (!node) {
|
|
24
|
+
return text;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!node.children || !node.children.length) {
|
|
28
|
+
if (!node.text) {
|
|
29
|
+
return text;
|
|
30
|
+
} else {
|
|
31
|
+
text += node.text;
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
return node.children.map(v => getTextContent(v, text)).join("").trim();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return text;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isValidNodeForPlacement(node) {
|
|
41
|
+
return true;
|
|
42
|
+
// this is disabled, elements should just clear, otherwise too many ads are missing because of short intro texts and such ...
|
|
43
|
+
/*
|
|
44
|
+
if (node && node.type === "paragraph") {
|
|
45
|
+
const text = getTextContent(node);
|
|
46
|
+
|
|
47
|
+
if (text.length < 200) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return true;
|
|
53
|
+
*/
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function Serializer({
|
|
57
|
+
value = [],
|
|
58
|
+
elementPropsMap,
|
|
59
|
+
specialClassMap,
|
|
60
|
+
transformAttributes,
|
|
61
|
+
storybookComponentLoader,
|
|
62
|
+
storybookComponentDataLoader,
|
|
63
|
+
defaultComponentReplacement,
|
|
64
|
+
modifyElementFunc,
|
|
65
|
+
filterContent = null,
|
|
66
|
+
isRenderer = false,
|
|
67
|
+
fixedPositions = {},
|
|
68
|
+
fixedPositionTypes = {},
|
|
69
|
+
adDefinitionDesktop = [],
|
|
70
|
+
adDefinitionMobile = [],
|
|
71
|
+
}) {
|
|
72
|
+
const processedValue = useMemo(() => {
|
|
73
|
+
let val = addAdsToValue(value, adDefinitionDesktop, adDefinitionMobile);
|
|
74
|
+
val = addFixedPositionsToValue(val, fixedPositions, fixedPositionTypes);
|
|
75
|
+
return val;
|
|
76
|
+
}, [value, adDefinitionDesktop, adDefinitionMobile, fixedPositions, fixedPositionTypes]);
|
|
77
|
+
|
|
78
|
+
const filteredValue = useMemo(() => {
|
|
79
|
+
const context = {content: processedValue};
|
|
80
|
+
return processedValue.filter(v => {
|
|
81
|
+
try {
|
|
82
|
+
return filterContent ? filterContent(v, context) : true;
|
|
83
|
+
} catch (e) {
|
|
84
|
+
console.error("[Serializer] Error in filterContent prop function: ", e);
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}, [processedValue, filterContent]);
|
|
89
|
+
|
|
90
|
+
const getPropsForType = (type, isInSlot = false) => {
|
|
91
|
+
if (!elementPropsMap) {
|
|
92
|
+
return {};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (isInSlot) {
|
|
96
|
+
if (elementPropsMap[type + "-in-slot"]) {
|
|
97
|
+
return elementPropsMap[type + "-in-slot"];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return elementPropsMap[type] || {};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const renderElement = (props, isInSlot = false) => {
|
|
105
|
+
const typeProps = getPropsForType(props.type, isInSlot);
|
|
106
|
+
const Wrapper = typeProps.wrapper || function ({children}) {
|
|
107
|
+
return <ErrorBoundary>{children}</ErrorBoundary>;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
switch (props.type) {
|
|
111
|
+
case 'storybook': {
|
|
112
|
+
const storyProps = {
|
|
113
|
+
element: {
|
|
114
|
+
...props,
|
|
115
|
+
attributes: {
|
|
116
|
+
...(props.attributes || {}),
|
|
117
|
+
isInSlot: isInSlot,
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
attributes: props.attributes,
|
|
121
|
+
storybookComponentLoader: storybookComponentLoader,
|
|
122
|
+
storybookComponentDataLoader: storybookComponentDataLoader,
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
return <ErrorBoundary><Wrapper
|
|
126
|
+
type={props.type}
|
|
127
|
+
block={props.block}><StorybookDisplay {...storyProps} /></Wrapper></ErrorBoundary>;
|
|
128
|
+
}
|
|
129
|
+
case 'layout': {
|
|
130
|
+
let isGrid = true;
|
|
131
|
+
|
|
132
|
+
if (Array.isArray(props.children)) {
|
|
133
|
+
props.children.forEach(child => {
|
|
134
|
+
if (child.type === "layout-slot" && ["grid-auto", "grid-fill"].includes(child.attributes.name)) {
|
|
135
|
+
isGrid = false;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return <ErrorBoundary>
|
|
141
|
+
<Wrapper type={props.type}>
|
|
142
|
+
<div
|
|
143
|
+
data-cols={props.children.length}
|
|
144
|
+
className={classNames("block-editor-layout", {
|
|
145
|
+
"block-editor-layout-grid": isGrid,
|
|
146
|
+
"block-editor-layout-flex": !isGrid,
|
|
147
|
+
"block-editor-table-border-wrapper": props?.attributes?.tableBorder,
|
|
148
|
+
"md:grid-cols-1": props.children.length === 1,
|
|
149
|
+
"md:grid-cols-2": props.children.length === 2,
|
|
150
|
+
"md:grid-cols-3": props.children.length === 3,
|
|
151
|
+
"md:grid-cols-4": props.children.length === 4,
|
|
152
|
+
"md:grid-cols-5": props.children.length === 5,
|
|
153
|
+
"grid-cols-1": props?.attributes?.mobileColumnSpan === 1,
|
|
154
|
+
"grid-cols-2": props?.attributes?.mobileColumnSpan === 2,
|
|
155
|
+
"md:space-x-4": props.attributes?.spacing,
|
|
156
|
+
"mt-16": props?.attributes?.margin?.top,
|
|
157
|
+
"mr-16": props?.attributes?.margin?.right,
|
|
158
|
+
"mb-16": props?.attributes?.margin?.bottom,
|
|
159
|
+
"ml-16": props?.attributes?.margin?.left,
|
|
160
|
+
"justify-center": props?.attributes?.justifyCenter,
|
|
161
|
+
[typeProps.classNameSite + " site-width"]: !isInSlot && (props.attributes?.width === "site" || props.attributes?.width === "full"),
|
|
162
|
+
[typeProps.classNameArticle + " article-width"]: !isInSlot && (props.attributes?.width === "article" || !props.attributes?.width),
|
|
163
|
+
})}
|
|
164
|
+
>
|
|
165
|
+
{props.children.map((v, i) => <Fragment
|
|
166
|
+
key={"layout-pos-" + i}>{serializeNode(v, i, isInSlot)}</Fragment>)}
|
|
167
|
+
</div>
|
|
168
|
+
</Wrapper>
|
|
169
|
+
</ErrorBoundary>;
|
|
170
|
+
}
|
|
171
|
+
case 'layout-slot':
|
|
172
|
+
return <ErrorBoundary>
|
|
173
|
+
<Wrapper type={props.type}>
|
|
174
|
+
<div
|
|
175
|
+
className={classNames({
|
|
176
|
+
["block-editor-layout-" + props.attributes.name]: true,
|
|
177
|
+
"mt-4": props.attributes?.margin?.top,
|
|
178
|
+
"md:mr-4": props.attributes?.margin?.right,
|
|
179
|
+
"mb-4": props.attributes?.margin?.bottom,
|
|
180
|
+
"md:ml-4": props.attributes?.margin?.left,
|
|
181
|
+
})}
|
|
182
|
+
>
|
|
183
|
+
<div
|
|
184
|
+
style={{top: props.attributes?.sticky && "80px"}}
|
|
185
|
+
className={classNames({"sticky": props.attributes?.sticky})}>
|
|
186
|
+
{props.children.map((v, i) => <div
|
|
187
|
+
key={"layout-slot-" + i}
|
|
188
|
+
>{serializeNode(v, i, props.attributes.name)}</div>)}
|
|
189
|
+
</div>
|
|
190
|
+
</div>
|
|
191
|
+
</Wrapper>
|
|
192
|
+
</ErrorBoundary>;
|
|
193
|
+
default: {
|
|
194
|
+
const defaultProps = {
|
|
195
|
+
specialClassMap: specialClassMap,
|
|
196
|
+
elementPropsMap: elementPropsMap,
|
|
197
|
+
element: {
|
|
198
|
+
attributes: props.attributes || {},
|
|
199
|
+
align: props.align,
|
|
200
|
+
type: props.type,
|
|
201
|
+
},
|
|
202
|
+
attributes: props.htmlAttributes || {},
|
|
203
|
+
children: props.children.map((v, i) => <Fragment
|
|
204
|
+
key={props.type + "-pos-item-" + i}>{serializeNode(v, i, isInSlot)}</Fragment>),
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
return <ErrorBoundary>
|
|
208
|
+
<Default
|
|
209
|
+
{...defaultProps}
|
|
210
|
+
isInSlot={isInSlot}
|
|
211
|
+
isRenderer={isRenderer}
|
|
212
|
+
transformAttributes={transformAttributes}
|
|
213
|
+
defaultComponentReplacement={defaultComponentReplacement}
|
|
214
|
+
/>
|
|
215
|
+
</ErrorBoundary>;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
function serializeNode(node, i, isInSlot = false) {
|
|
222
|
+
if (isValidElement(node)) {
|
|
223
|
+
return node;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (typeof modifyElementFunc === "function") {
|
|
227
|
+
node = modifyElementFunc(node);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (node.type === "paragraph" && !node.text) {
|
|
231
|
+
if (!node.children || node.children.length === 0) {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
let children = node.children.filter((v) => !isEmptyNode(v));
|
|
236
|
+
|
|
237
|
+
if (children.length === 0) {
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (!node.children || !node.children.length) {
|
|
243
|
+
return serializeLeaf(node);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// unwrap elements that are inside of a paragraph (that has no text content) so they have been inserted "inline" by mistake
|
|
247
|
+
// this helps preventing a lot of hydration errors!
|
|
248
|
+
if (node?.type === "paragraph") {
|
|
249
|
+
const textContent = node.children.find(v => !v?.type && v?.text);
|
|
250
|
+
if (!textContent) {
|
|
251
|
+
const storybookNodes = node.children.filter(v => v?.type === "storybook");
|
|
252
|
+
if (storybookNodes.length === 1) {
|
|
253
|
+
// only unwrap if we only have exactly 1 node
|
|
254
|
+
return renderElement(storybookNodes.at(0), isInSlot);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return renderElement(node, isInSlot);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function serializeLeaf(props) {
|
|
263
|
+
return <Leaf leaf={props} isRenderer={true} typeProps={getPropsForType("leaf")}>{props.text || ""}</Leaf>;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (!value) {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return <>
|
|
271
|
+
{!!filteredValue && filteredValue.map((v, i) => {
|
|
272
|
+
return <Fragment key={v.block + "-pos-" + i}>{serializeNode(v, i)}</Fragment>;
|
|
273
|
+
})}
|
|
274
|
+
</>;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function getFixedPosition(fixedPositions, i) {
|
|
278
|
+
return fixedPositions?.[i] || null;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function isShortParagraph(node) {
|
|
282
|
+
if (node?.type === "paragraph") {
|
|
283
|
+
let textLength = node.children.map(v => v?.text?.length || 0).reduce((pv, cv) => pv + cv, 0) || 0;
|
|
284
|
+
return textLength <= 60;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function getFixedPositionPlacement(node, prevNode, fixedPosType) {
|
|
291
|
+
// inline has to be before
|
|
292
|
+
if (fixedPosType === "inline") {
|
|
293
|
+
return "before";
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
let placement = "before";
|
|
297
|
+
|
|
298
|
+
if (node?.type === "paragraph" && prevNode?.type === "heading" || isShortParagraph(prevNode)) {
|
|
299
|
+
placement = "after";
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return placement;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function isNodeValidForCounter(node, counters = {}) {
|
|
306
|
+
let isValid = true;
|
|
307
|
+
|
|
308
|
+
if (node.type === "paragraph") {
|
|
309
|
+
let lengthCounterId = `paragraph_textlength`;
|
|
310
|
+
|
|
311
|
+
if (!counters[lengthCounterId]) {
|
|
312
|
+
counters[lengthCounterId] = 0;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
let textLength = node.children.map(v => v?.text?.length || 0).reduce((pv, cv) => pv + cv, 0) || 0;
|
|
316
|
+
let isHeadline = textLength <= 60;
|
|
317
|
+
|
|
318
|
+
counters[lengthCounterId] += textLength;
|
|
319
|
+
|
|
320
|
+
isValid = !isHeadline && counters[lengthCounterId] >= 200;
|
|
321
|
+
|
|
322
|
+
if (isValid) {
|
|
323
|
+
counters[lengthCounterId] = 0;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return isValid;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function addFixedPositionsToValue(
|
|
331
|
+
value, fixedPositions, fixedPositionTypes, counters = {},
|
|
332
|
+
) {
|
|
333
|
+
if (!Array.isArray(value)) {
|
|
334
|
+
return value;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
let processedValue = [];
|
|
338
|
+
|
|
339
|
+
for (let i = 0; i < value.length; i++) {
|
|
340
|
+
let node = value[i];
|
|
341
|
+
let prevNode = value?.[i - 1];
|
|
342
|
+
|
|
343
|
+
if (!node.type) {
|
|
344
|
+
processedValue.push(node);
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (!counters[node.type]) {
|
|
348
|
+
counters[node.type] = 0;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
let counterId = `${node.type}_${counters[node.type]}`;
|
|
352
|
+
|
|
353
|
+
// only count this node if its valid to be counted (dont count strong headlines and so on)
|
|
354
|
+
if (isNodeValidForCounter(node, counters)) {
|
|
355
|
+
counters[node.type]++;
|
|
356
|
+
let fixedPosType = fixedPositionTypes?.[counterId];
|
|
357
|
+
let fixedPos = getFixedPosition(fixedPositions, counterId);
|
|
358
|
+
let placement = getFixedPositionPlacement(node, prevNode, fixedPosType);
|
|
359
|
+
|
|
360
|
+
if (fixedPos && placement === "before") {
|
|
361
|
+
processedValue.push(fixedPos);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
processedValue.push(node);
|
|
365
|
+
|
|
366
|
+
if (fixedPos && placement === "after") {
|
|
367
|
+
processedValue.push(fixedPos);
|
|
368
|
+
}
|
|
369
|
+
} else {
|
|
370
|
+
processedValue.push(node);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return processedValue;
|
|
375
375
|
}
|