@vonaffenfels/slate-editor 1.1.71 → 1.2.10

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 (96) hide show
  1. package/.babelrc +43 -43
  2. package/README.md +5 -5
  3. package/componentLoader.js +93 -93
  4. package/dist/BlockEditor.css +4 -1871
  5. package/dist/BlockEditor.js +336 -3960
  6. package/dist/BlockEditor.js.LICENSE.txt +61 -0
  7. package/dist/Renderer.js +2 -2081
  8. package/dist/Renderer.js.LICENSE.txt +15 -0
  9. package/dist/fromHTML.js +1 -78
  10. package/dist/index.css +4 -1871
  11. package/dist/index.js +336 -4037
  12. package/dist/index.js.LICENSE.txt +69 -0
  13. package/dist/toHTML.js +2 -1689
  14. package/dist/toHTML.js.LICENSE.txt +23 -0
  15. package/dist/toText.js +2 -1689
  16. package/dist/toText.js.LICENSE.txt +23 -0
  17. package/package.json +2 -2
  18. package/postcss.config.js +6 -6
  19. package/scss/demo.scss +148 -148
  20. package/scss/editor.scss +17 -5
  21. package/scss/sidebarEditor.scss +185 -181
  22. package/scss/toolbar.scss +161 -161
  23. package/src/BlockEditor.js +6 -3
  24. package/src/Blocks/EmptyBlock.js +11 -11
  25. package/src/Blocks/EmptyWrapper.js +4 -4
  26. package/src/Blocks/ErrorBoundary.js +40 -40
  27. package/src/Blocks/LayoutBlock.js +274 -274
  28. package/src/Blocks/LayoutSlot.js +90 -90
  29. package/src/CollapsableMenu/CollapsableMenu.js +48 -48
  30. package/src/Context/StorybookContext.js +6 -6
  31. package/src/ElementAutocomplete.js +133 -133
  32. package/src/Loader.js +137 -137
  33. package/src/Nodes/Default.js +158 -158
  34. package/src/Nodes/Leaf.js +54 -54
  35. package/src/Nodes/Text.js +97 -97
  36. package/src/ObjectId.js +3 -3
  37. package/src/Renderer.js +73 -73
  38. package/src/Serializer/Html.js +42 -42
  39. package/src/Serializer/Serializer.js +371 -371
  40. package/src/Serializer/Text.js +17 -17
  41. package/src/Serializer/ads.js +174 -174
  42. package/src/Serializer/index.js +3 -3
  43. package/src/SidebarEditor/AssetList.js +181 -181
  44. package/src/SidebarEditor/Fields/CloudinaryContentSelect.js +89 -89
  45. package/src/SidebarEditor/Fields/ColorPicker.js +89 -89
  46. package/src/SidebarEditor/Fields/ContentfulContentSelect.js +62 -62
  47. package/src/SidebarEditor/Fields/DateTime.js +55 -55
  48. package/src/SidebarEditor/Fields/MVP.js +66 -66
  49. package/src/SidebarEditor/Fields/MultiSelect.js +13 -13
  50. package/src/SidebarEditor/Fields/RemoteMultiSelect.js +40 -40
  51. package/src/SidebarEditor/Fields/RemoteSelect.js +39 -39
  52. package/src/SidebarEditor/Fields/Select.js +47 -47
  53. package/src/SidebarEditor/Fields/StreamSelect.js +15 -15
  54. package/src/SidebarEditor/Fields/Switch.js +34 -34
  55. package/src/SidebarEditor/Fields/Textarea.js +21 -21
  56. package/src/SidebarEditor/Resizable.js +85 -85
  57. package/src/Storybook.js +151 -151
  58. package/src/Toolbar/Align.js +64 -64
  59. package/src/Toolbar/Anchor.js +94 -94
  60. package/src/Toolbar/Block.js +135 -135
  61. package/src/Toolbar/Element.js +44 -44
  62. package/src/Toolbar/Formats.js +71 -71
  63. package/src/Toolbar/Insert.js +28 -28
  64. package/src/Toolbar/Layout.js +399 -399
  65. package/src/Toolbar/Link.js +164 -164
  66. package/src/Toolbar/Toolbar.js +235 -235
  67. package/src/Tools/Margin.js +51 -51
  68. package/src/Translation/TranslationToolbarButton.js +119 -115
  69. package/src/dev/draftToSlate.json +3147 -3147
  70. package/src/dev/index.css +2 -2
  71. package/src/dev/index.html +10 -10
  72. package/src/dev/index.js +4 -4
  73. package/src/dev/sampleValue1.json +4294 -4294
  74. package/src/dev/sampleValueValid.json +410 -410
  75. package/src/dev/testComponents/TestStory.js +74 -74
  76. package/src/dev/testComponents/TestStory.stories.js +216 -216
  77. package/src/dev/testComponents/TestStory2.js +74 -74
  78. package/src/dev/testComponents/TestStory2.stories.js +197 -197
  79. package/src/dev/testComponents/TestStory3.js +74 -74
  80. package/src/dev/testComponents/TestStory3.stories.js +197 -197
  81. package/src/dev/testSampleValue.json +746 -746
  82. package/src/fromHTML.js +4 -4
  83. package/src/helper/array.js +8 -8
  84. package/src/index.js +10 -10
  85. package/src/plugins/ListItem.js +48 -48
  86. package/src/plugins/SoftBreak.js +23 -23
  87. package/src/toHTML.js +6 -6
  88. package/src/toText.js +6 -6
  89. package/src/util/reduceContentfulResponse.js +64 -64
  90. package/src/util.js +19 -19
  91. package/storyLoader.js +47 -47
  92. package/tailwind.config.js +4 -4
  93. package/webpack.config.build.js +55 -55
  94. package/webpack.config.dev.js +60 -60
  95. package/webpack.config.js +130 -130
  96. package/webpack.config.watch.js +4 -4
@@ -1,372 +1,372 @@
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, 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;
372
372
  }