@vonaffenfels/slate-editor 1.2.19 → 1.2.29

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