@vonaffenfels/slate-editor 1.2.42 → 1.2.44

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