lkb-fields-document 1.0.0 → 1.0.1

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 (109) hide show
  1. package/component-blocks/dist/lkb-fields-document-component-blocks.cjs.d.ts +2 -2
  2. package/component-blocks/dist/lkb-fields-document-component-blocks.cjs.js +16 -306
  3. package/component-blocks/dist/lkb-fields-document-component-blocks.node.cjs.js +16 -306
  4. package/dist/lkb-fields-document.cjs.d.ts +2 -2
  5. package/dist/lkb-fields-document.cjs.js +16 -1167
  6. package/dist/lkb-fields-document.node.cjs.js +16 -1167
  7. package/package.json +6 -6
  8. package/structure-views/dist/lkb-fields-document-structure-views.cjs.d.ts +2 -2
  9. package/structure-views/dist/lkb-fields-document-structure-views.cjs.js +16 -138
  10. package/structure-views/dist/lkb-fields-document-structure-views.node.cjs.js +16 -138
  11. package/views/dist/lkb-fields-document-views.cjs.d.ts +2 -2
  12. package/views/dist/lkb-fields-document-views.cjs.js +13 -111
  13. package/views/dist/lkb-fields-document-views.node.cjs.js +13 -111
  14. package/component-blocks/dist/lkb-fields-document-component-blocks.esm.js +0 -300
  15. package/component-blocks/dist/lkb-fields-document-component-blocks.node.esm.js +0 -300
  16. package/dist/Cell-0ac0ac66.node.cjs.js +0 -21
  17. package/dist/Cell-242f7404.esm.js +0 -17
  18. package/dist/Cell-3103f73d.node.esm.js +0 -17
  19. package/dist/Cell-bfb56d74.cjs.js +0 -21
  20. package/dist/Field-0e0f75ed.node.cjs.js +0 -1628
  21. package/dist/Field-28177061.cjs.js +0 -1628
  22. package/dist/Field-35b79e6b.node.esm.js +0 -1619
  23. package/dist/Field-92d13205.esm.js +0 -1619
  24. package/dist/api-2f524611.esm.js +0 -502
  25. package/dist/api-73636987.cjs.js +0 -506
  26. package/dist/api-8e2b20b8.node.cjs.js +0 -506
  27. package/dist/api-c32e360e.node.esm.js +0 -502
  28. package/dist/callout-ui-2aded278.cjs.js +0 -131
  29. package/dist/callout-ui-3e5ca544.node.esm.js +0 -126
  30. package/dist/callout-ui-8b5f2376.esm.js +0 -126
  31. package/dist/callout-ui-ad50f301.node.cjs.js +0 -131
  32. package/dist/declarations/src/component-blocks.d.ts +0 -4
  33. package/dist/declarations/src/component-blocks.d.ts.map +0 -1
  34. package/dist/declarations/src/document-editor/component-blocks/api.d.ts +0 -120
  35. package/dist/declarations/src/document-editor/component-blocks/api.d.ts.map +0 -1
  36. package/dist/declarations/src/document-editor/component-blocks/types.d.ts +0 -241
  37. package/dist/declarations/src/document-editor/component-blocks/types.d.ts.map +0 -1
  38. package/dist/declarations/src/document-editor/toolset/relationship/relationship-shared.d.ts +0 -10
  39. package/dist/declarations/src/document-editor/toolset/relationship/relationship-shared.d.ts.map +0 -1
  40. package/dist/declarations/src/index.d.ts +0 -7
  41. package/dist/declarations/src/index.d.ts.map +0 -1
  42. package/dist/declarations/src/my-component-blocks/index.d.ts +0 -46
  43. package/dist/declarations/src/my-component-blocks/index.d.ts.map +0 -1
  44. package/dist/declarations/src/structure/Cell.d.ts +0 -5
  45. package/dist/declarations/src/structure/Cell.d.ts.map +0 -1
  46. package/dist/declarations/src/structure/Field.d.ts +0 -5
  47. package/dist/declarations/src/structure/Field.d.ts.map +0 -1
  48. package/dist/declarations/src/structure/controller.d.ts +0 -10
  49. package/dist/declarations/src/structure/controller.d.ts.map +0 -1
  50. package/dist/declarations/src/structure/structure.d.ts +0 -4
  51. package/dist/declarations/src/structure/structure.d.ts.map +0 -1
  52. package/dist/declarations/src/structure-views.d.ts +0 -5
  53. package/dist/declarations/src/structure-views.d.ts.map +0 -1
  54. package/dist/declarations/src/types/DocumentFeatures.d.ts +0 -33
  55. package/dist/declarations/src/types/DocumentFeatures.d.ts.map +0 -1
  56. package/dist/declarations/src/types/DocumentFieldConfig.d.ts +0 -18
  57. package/dist/declarations/src/types/DocumentFieldConfig.d.ts.map +0 -1
  58. package/dist/declarations/src/types/FormattingConfig.d.ts +0 -28
  59. package/dist/declarations/src/types/FormattingConfig.d.ts.map +0 -1
  60. package/dist/declarations/src/types/RelationshipsConfig.d.ts +0 -9
  61. package/dist/declarations/src/types/RelationshipsConfig.d.ts.map +0 -1
  62. package/dist/declarations/src/types/StructureFieldConfig.d.ts +0 -10
  63. package/dist/declarations/src/types/StructureFieldConfig.d.ts.map +0 -1
  64. package/dist/declarations/src/validation/structure-validation.d.ts +0 -218
  65. package/dist/declarations/src/validation/structure-validation.d.ts.map +0 -1
  66. package/dist/declarations/src/views/Cell.d.ts +0 -5
  67. package/dist/declarations/src/views/Cell.d.ts.map +0 -1
  68. package/dist/declarations/src/views/Field.d.ts +0 -5
  69. package/dist/declarations/src/views/Field.d.ts.map +0 -1
  70. package/dist/declarations/src/views/controller.d.ts +0 -15
  71. package/dist/declarations/src/views/controller.d.ts.map +0 -1
  72. package/dist/declarations/src/views/document.d.ts +0 -4
  73. package/dist/declarations/src/views/document.d.ts.map +0 -1
  74. package/dist/declarations/src/views.d.ts +0 -7
  75. package/dist/declarations/src/views.d.ts.map +0 -1
  76. package/dist/editor-shared-a6e340e6.node.esm.js +0 -1993
  77. package/dist/editor-shared-a997ae98.node.cjs.js +0 -2007
  78. package/dist/editor-shared-cc1293ed.cjs.js +0 -2007
  79. package/dist/editor-shared-da518ba3.esm.js +0 -1993
  80. package/dist/form-from-preview-2042b9ef.cjs.js +0 -512
  81. package/dist/form-from-preview-5df6e492.node.esm.js +0 -508
  82. package/dist/form-from-preview-9e501058.node.cjs.js +0 -512
  83. package/dist/form-from-preview-b3a66f37.esm.js +0 -508
  84. package/dist/index-06c36775.cjs.js +0 -14
  85. package/dist/index-586adb8f.node.esm.js +0 -11
  86. package/dist/index-67d52357.esm.js +0 -11
  87. package/dist/index-c3223fdc.node.cjs.js +0 -14
  88. package/dist/layouts-6412fa2a.esm.js +0 -189
  89. package/dist/layouts-a4a3cf0b.node.cjs.js +0 -196
  90. package/dist/layouts-ba9a558b.cjs.js +0 -196
  91. package/dist/layouts-e653b908.node.esm.js +0 -189
  92. package/dist/lkb-fields-document.esm.js +0 -1162
  93. package/dist/lkb-fields-document.node.esm.js +0 -1162
  94. package/dist/shared-0533009e.cjs.js +0 -594
  95. package/dist/shared-4684cc24.node.cjs.js +0 -594
  96. package/dist/shared-5e864055.node.esm.js +0 -579
  97. package/dist/shared-aaba5901.esm.js +0 -579
  98. package/dist/toolbar-state-3359e2f3.cjs.js +0 -994
  99. package/dist/toolbar-state-945823b8.node.esm.js +0 -971
  100. package/dist/toolbar-state-9611743f.node.cjs.js +0 -994
  101. package/dist/toolbar-state-bc8fe661.esm.js +0 -971
  102. package/dist/utils-06bcddc4.node.cjs.js +0 -747
  103. package/dist/utils-200ff260.node.esm.js +0 -722
  104. package/dist/utils-6409f730.cjs.js +0 -747
  105. package/dist/utils-bc6a0b82.esm.js +0 -722
  106. package/structure-views/dist/lkb-fields-document-structure-views.esm.js +0 -131
  107. package/structure-views/dist/lkb-fields-document-structure-views.node.esm.js +0 -131
  108. package/views/dist/lkb-fields-document-views.esm.js +0 -95
  109. package/views/dist/lkb-fields-document-views.node.esm.js +0 -95
@@ -1,579 +0,0 @@
1
- import { Text, Element, Transforms, Editor, Node, Range, Path, Point } from 'slate';
2
- import { n as nodeTypeMatcher, l as getSchemaAtPropPath, b as getDocumentFeaturesForChildField, o as allMarks, j as isElementActive, m as moveChildren, k as insertNodesButReplaceIfSelectionIsAtEmptyParagraphOrHeading } from './utils-bc6a0b82.esm.js';
3
-
4
- function areArraysEqual(a, b) {
5
- return a.length === b.length && a.every((x, i) => x === b[i]);
6
- }
7
- function normalizeTextBasedOnInlineMarksAndSoftBreaks([node, path], editor, inlineMarks, softBreaks) {
8
- const marksToRemove = Object.keys(node).filter(x => x !== 'text' && x !== 'insertMenu' && inlineMarks[x] !== true);
9
- if (marksToRemove.length) {
10
- Transforms.unsetNodes(editor, marksToRemove, {
11
- at: path
12
- });
13
- return true;
14
- }
15
- if (!softBreaks) {
16
- const hasSoftBreaks = node.text.includes('\n');
17
- if (hasSoftBreaks) {
18
- const [parentNode] = Editor.parent(editor, path);
19
- if (parentNode.type !== 'code') {
20
- for (const position of Editor.positions(editor, {
21
- at: path
22
- })) {
23
- const character = Node.get(editor, position.path).text[position.offset];
24
- if (character === '\n') {
25
- Transforms.delete(editor, {
26
- at: position
27
- });
28
- return true;
29
- }
30
- }
31
- }
32
- }
33
- }
34
- return false;
35
- }
36
- function normalizeInlineBasedOnLinksAndRelationships([node, path], editor, links, relationshipsEnabled, relationships) {
37
- if (node.type === 'link' && !links) {
38
- Transforms.insertText(editor, ` (${node.href})`, {
39
- at: Editor.end(editor, path)
40
- });
41
- Transforms.unwrapNodes(editor, {
42
- at: path
43
- });
44
- return true;
45
- }
46
- if (node.type === 'relationship' && (!relationshipsEnabled || relationships[node.relationship] === undefined)) {
47
- const data = node.data;
48
- if (data) {
49
- const relationship = relationships[node.relationship];
50
- Transforms.insertText(editor, `${data.label || data.id || ''} (${(relationship === null || relationship === void 0 ? void 0 : relationship.label) || node.relationship}:${data.id || ''})`, {
51
- at: Editor.before(editor, path)
52
- });
53
- }
54
- Transforms.removeNodes(editor, {
55
- at: path
56
- });
57
- return true;
58
- }
59
- return false;
60
- }
61
- function normalizeElementBasedOnDocumentFeatures([node, path], editor, {
62
- formatting,
63
- dividers,
64
- layouts,
65
- links,
66
- relationships: relationshipsEnabled
67
- }, relationships) {
68
- if (node.type === 'heading' && (!formatting.headingLevels.length || !formatting.headingLevels.includes(node.level)) || node.type === 'ordered-list' && !formatting.listTypes.ordered || node.type === 'unordered-list' && !formatting.listTypes.unordered || node.type === 'code' && !formatting.blockTypes.code || node.type === 'blockquote' && !formatting.blockTypes.blockquote || node.type === 'layout' && (layouts.length === 0 || !layouts.some(layout => areArraysEqual(layout, node.layout)))) {
69
- Transforms.unwrapNodes(editor, {
70
- at: path
71
- });
72
- return true;
73
- }
74
- if ((node.type === 'paragraph' || node.type === 'heading') && (!formatting.alignment.center && node.textAlign === 'center' || !formatting.alignment.end && node.textAlign === 'end' || 'textAlign' in node && node.textAlign !== 'center' && node.textAlign !== 'end')) {
75
- Transforms.unsetNodes(editor, 'textAlign', {
76
- at: path
77
- });
78
- return true;
79
- }
80
- if (node.type === 'divider' && !dividers) {
81
- Transforms.removeNodes(editor, {
82
- at: path
83
- });
84
- return true;
85
- }
86
- return normalizeInlineBasedOnLinksAndRelationships([node, path], editor, links, relationshipsEnabled, relationships);
87
- }
88
- function withDocumentFeaturesNormalization(documentFeatures, relationships, editor) {
89
- const {
90
- normalizeNode
91
- } = editor;
92
- const documentFeaturesForNormalization = {
93
- ...documentFeatures,
94
- relationships: true
95
- };
96
- editor.normalizeNode = ([node, path]) => {
97
- if (Text.isText(node)) {
98
- normalizeTextBasedOnInlineMarksAndSoftBreaks([node, path], editor, documentFeatures.formatting.inlineMarks, documentFeatures.formatting.softBreaks);
99
- } else if (Element.isElement(node)) {
100
- normalizeElementBasedOnDocumentFeatures([node, path], editor, documentFeaturesForNormalization, relationships);
101
- }
102
- normalizeNode([node, path]);
103
- };
104
- return editor;
105
- }
106
-
107
- const paragraphElement = () => ({
108
- type: 'paragraph',
109
- children: [{
110
- text: ''
111
- }]
112
- });
113
- function withParagraphs(editor) {
114
- const {
115
- normalizeNode
116
- } = editor;
117
- editor.normalizeNode = entry => {
118
- const [node, path] = entry;
119
- if (Editor.isEditor(node)) {
120
- const lastNode = node.children[node.children.length - 1];
121
- if ((lastNode === null || lastNode === void 0 ? void 0 : lastNode.type) !== 'paragraph') {
122
- Transforms.insertNodes(editor, paragraphElement(), {
123
- at: [...path, node.children.length]
124
- });
125
- return;
126
- }
127
- }
128
- normalizeNode(entry);
129
- };
130
- return editor;
131
- }
132
-
133
- // component blocks are not in the ToolbarState because they're inserted in the closest available place and the selected state is not shown in the toolbar
134
-
135
- // note that isDisabled being false here does not mean the action should be allowed
136
- // it means that the action should be allowed if isDisabled is false AND the relevant document feature is enabled
137
- // (because things are hidden if they're not enabled in the editor document features)
138
-
139
- function getAncestorComponentChildFieldDocumentFeatures(editor, editorDocumentFeatures, componentBlocks) {
140
- const ancestorComponentProp = Editor.above(editor, {
141
- match: nodeTypeMatcher('component-block-prop', 'component-inline-prop')
142
- });
143
- if (ancestorComponentProp) {
144
- const propPath = ancestorComponentProp[0].propPath;
145
- const ancestorComponent = Editor.parent(editor, ancestorComponentProp[1]);
146
- if (ancestorComponent[0].type === 'component-block') {
147
- const component = ancestorComponent[0].component;
148
- const componentBlock = componentBlocks[component];
149
- if (componentBlock && propPath) {
150
- const childField = getSchemaAtPropPath(propPath, ancestorComponent[0].props, componentBlock.schema);
151
- if ((childField === null || childField === void 0 ? void 0 : childField.kind) === 'child') {
152
- return getDocumentFeaturesForChildField(editorDocumentFeatures, childField.options);
153
- }
154
- }
155
- }
156
- }
157
- }
158
- const createToolbarState = (editor, componentBlocks, editorDocumentFeatures) => {
159
- const locationDocumentFeatures = getAncestorComponentChildFieldDocumentFeatures(editor, editorDocumentFeatures, componentBlocks) || {
160
- kind: 'block',
161
- inlineMarks: 'inherit',
162
- documentFeatures: {
163
- dividers: true,
164
- formatting: {
165
- alignment: {
166
- center: true,
167
- end: true
168
- },
169
- blockTypes: {
170
- blockquote: true,
171
- code: true
172
- },
173
- headingLevels: [1, 2, 3, 4, 5, 6],
174
- listTypes: {
175
- ordered: true,
176
- unordered: true
177
- }
178
- },
179
- layouts: editorDocumentFeatures.layouts,
180
- links: true,
181
- relationships: true
182
- },
183
- softBreaks: true,
184
- componentBlocks: true
185
- };
186
- const [maybeCodeBlockEntry] = Editor.nodes(editor, {
187
- match: node => node.type !== 'code' && Element.isElement(node) && Editor.isBlock(editor, node)
188
- });
189
- const editorMarks = Editor.marks(editor) || {};
190
- const marks = Object.fromEntries(allMarks.map(mark => [mark, {
191
- isDisabled: locationDocumentFeatures.inlineMarks !== 'inherit' && !locationDocumentFeatures.inlineMarks[mark] || !maybeCodeBlockEntry,
192
- isSelected: !!editorMarks[mark]
193
- }]));
194
-
195
- // Editor.marks is "what are the marks that would be applied if text was inserted now"
196
- // that's not really the UX we want, if we have some a document like this
197
- // <paragraph>
198
- // <text>
199
- // <anchor />
200
- // content
201
- // </text>
202
- // <text bold>bold</text>
203
- // <text>
204
- // content
205
- // <focus />
206
- // </text>
207
- // </paragraph>
208
-
209
- // we want bold to be shown as selected even though if you inserted text from that selection, it wouldn't be bold
210
- // so we look at all the text nodes in the selection to get their marks
211
- // but only if the selection is expanded because if you're in the middle of some text
212
- // with your selection collapsed with a mark but you've removed it(i.e. editor.removeMark)
213
- // the text nodes you're in will have the mark but the ui should show the mark as not being selected
214
- if (editor.selection && Range.isExpanded(editor.selection)) {
215
- for (const node of Editor.nodes(editor, {
216
- match: Text.isText
217
- })) {
218
- for (const key of Object.keys(node[0])) {
219
- if (key === 'insertMenu' || key === 'text') {
220
- continue;
221
- }
222
- if (key in marks) {
223
- marks[key].isSelected = true;
224
- }
225
- }
226
- }
227
- }
228
- const [headingEntry] = Editor.nodes(editor, {
229
- match: nodeTypeMatcher('heading')
230
- });
231
- const [listEntry] = Editor.nodes(editor, {
232
- match: isListNode
233
- });
234
- const [alignableEntry] = Editor.nodes(editor, {
235
- match: nodeTypeMatcher('paragraph', 'heading')
236
- });
237
-
238
- // (we're gonna use markdown here because the equivelant slate structure is quite large and doesn't add value here)
239
- // let's imagine a document that looks like this:
240
- // - thing
241
- // 1. something<cursor />
242
- // in the toolbar, you don't want to see that both ordered and unordered lists are selected
243
- // you want to see only ordered list selected, because
244
- // - you want to know what list you're actually in, you don't really care about the outer list
245
- // - when you want to change the list to a unordered list, the unordered list button should be inactive to show you can change to it
246
- const listTypeAbove = getListTypeAbove(editor);
247
- return {
248
- marks,
249
- textStyles: {
250
- selected: headingEntry ? headingEntry[0].level : 'normal',
251
- allowedHeadingLevels: locationDocumentFeatures.kind === 'block' && !listEntry ? locationDocumentFeatures.documentFeatures.formatting.headingLevels : []
252
- },
253
- relationships: {
254
- isDisabled: !locationDocumentFeatures.documentFeatures.relationships
255
- },
256
- code: {
257
- isSelected: isElementActive(editor, 'code'),
258
- isDisabled: !(locationDocumentFeatures.kind === 'block' && locationDocumentFeatures.documentFeatures.formatting.blockTypes.code)
259
- },
260
- lists: {
261
- ordered: {
262
- isSelected: isElementActive(editor, 'ordered-list') && (listTypeAbove === 'none' || listTypeAbove === 'ordered-list'),
263
- isDisabled: !(locationDocumentFeatures.kind === 'block' && locationDocumentFeatures.documentFeatures.formatting.listTypes.ordered && !headingEntry)
264
- },
265
- unordered: {
266
- isSelected: isElementActive(editor, 'unordered-list') && (listTypeAbove === 'none' || listTypeAbove === 'unordered-list'),
267
- isDisabled: !(locationDocumentFeatures.kind === 'block' && locationDocumentFeatures.documentFeatures.formatting.listTypes.unordered && !headingEntry)
268
- }
269
- },
270
- alignment: {
271
- isDisabled: !alignableEntry && !(locationDocumentFeatures.kind === 'block' && locationDocumentFeatures.documentFeatures.formatting.alignment),
272
- selected: (alignableEntry === null || alignableEntry === void 0 ? void 0 : alignableEntry[0].textAlign) || 'start'
273
- },
274
- blockquote: {
275
- isDisabled: !(locationDocumentFeatures.kind === 'block' && locationDocumentFeatures.documentFeatures.formatting.blockTypes.blockquote),
276
- isSelected: isElementActive(editor, 'blockquote')
277
- },
278
- layouts: {
279
- isSelected: isElementActive(editor, 'layout')
280
- },
281
- links: {
282
- isDisabled: !editor.selection || Range.isCollapsed(editor.selection) || !locationDocumentFeatures.documentFeatures.links,
283
- isSelected: isElementActive(editor, 'link')
284
- },
285
- editor,
286
- dividers: {
287
- isDisabled: locationDocumentFeatures.kind === 'inline' || !locationDocumentFeatures.documentFeatures.dividers
288
- },
289
- clearFormatting: {
290
- isDisabled: !(Object.values(marks).some(x => x.isSelected) || !!hasBlockThatClearsOnClearFormatting(editor))
291
- },
292
- editorDocumentFeatures
293
- };
294
- };
295
- function hasBlockThatClearsOnClearFormatting(editor) {
296
- const [node] = Editor.nodes(editor, {
297
- match: node => node.type === 'heading' || node.type === 'code' || node.type === 'blockquote'
298
- });
299
- return !!node;
300
- }
301
- function getListTypeAbove(editor) {
302
- const listAbove = Editor.above(editor, {
303
- match: isListNode
304
- });
305
- if (!listAbove) {
306
- return 'none';
307
- }
308
- return listAbove[0].type;
309
- }
310
-
311
- const isListType = type => type === 'ordered-list' || type === 'unordered-list';
312
- const isListNode = node => isListType(node.type);
313
- const toggleList = (editor, format) => {
314
- const listAbove = getListTypeAbove(editor);
315
- const isActive = isElementActive(editor, format) && (listAbove === 'none' || listAbove === format);
316
- Editor.withoutNormalizing(editor, () => {
317
- Transforms.unwrapNodes(editor, {
318
- match: isListNode,
319
- split: true,
320
- mode: isActive ? 'all' : 'lowest'
321
- });
322
- if (!isActive) {
323
- Transforms.wrapNodes(editor, {
324
- type: format,
325
- children: []
326
- }, {
327
- match: x => x.type !== 'list-item-content' && Element.isElement(x) && Editor.isBlock(editor, x)
328
- });
329
- }
330
- });
331
- };
332
- function getAncestorList(editor) {
333
- if (editor.selection) {
334
- const listItem = Editor.above(editor, {
335
- match: nodeTypeMatcher('list-item')
336
- });
337
- const list = Editor.above(editor, {
338
- match: isListNode
339
- });
340
- if (listItem && list) {
341
- return {
342
- isInside: true,
343
- listItem,
344
- list
345
- };
346
- }
347
- }
348
- return {
349
- isInside: false
350
- };
351
- }
352
- function withList(editor) {
353
- const {
354
- insertBreak,
355
- normalizeNode,
356
- deleteBackward
357
- } = editor;
358
- editor.deleteBackward = unit => {
359
- if (editor.selection) {
360
- const ancestorList = getAncestorList(editor);
361
- if (ancestorList.isInside && Range.isCollapsed(editor.selection) && Editor.isStart(editor, editor.selection.anchor, ancestorList.list[1])) {
362
- Transforms.unwrapNodes(editor, {
363
- match: isListNode,
364
- split: true
365
- });
366
- return;
367
- }
368
- }
369
- deleteBackward(unit);
370
- };
371
- editor.insertBreak = () => {
372
- const [listItem] = Editor.nodes(editor, {
373
- match: node => node.type === 'list-item',
374
- mode: 'lowest'
375
- });
376
- if (listItem && Node.string(listItem[0]) === '') {
377
- Transforms.unwrapNodes(editor, {
378
- match: isListNode,
379
- split: true
380
- });
381
- return;
382
- }
383
- insertBreak();
384
- };
385
- editor.normalizeNode = entry => {
386
- const [node, path] = entry;
387
- if (Element.isElement(node) || Editor.isEditor(node)) {
388
- const isElementBeingNormalizedAList = isListNode(node);
389
- for (const [childNode, childPath] of Node.children(editor, path)) {
390
- const index = childPath[childPath.length - 1];
391
- // merge sibling lists
392
- if (isListNode(childNode)) {
393
- var _node$children;
394
- if (((_node$children = node.children[childPath[childPath.length - 1] + 1]) === null || _node$children === void 0 ? void 0 : _node$children.type) === childNode.type) {
395
- const siblingNodePath = Path.next(childPath);
396
- moveChildren(editor, siblingNodePath, [...childPath, childNode.children.length]);
397
- Transforms.removeNodes(editor, {
398
- at: siblingNodePath
399
- });
400
- return;
401
- }
402
- if (isElementBeingNormalizedAList) {
403
- const previousChild = node.children[index - 1];
404
- if (Element.isElement(previousChild)) {
405
- Transforms.moveNodes(editor, {
406
- at: childPath,
407
- to: [...Path.previous(childPath), previousChild.children.length - 1]
408
- });
409
- } else {
410
- Transforms.unwrapNodes(editor, {
411
- at: childPath
412
- });
413
- }
414
- return;
415
- }
416
- }
417
- if (node.type === 'list-item' && childNode.type !== 'list-item-content' && index === 0 && Element.isElement(childNode) && Editor.isBlock(editor, childNode)) {
418
- if (path[path.length - 1] !== 0) {
419
- const previousChild = Node.get(editor, Path.previous(path));
420
- if (Element.isElement(previousChild)) {
421
- Transforms.moveNodes(editor, {
422
- at: path,
423
- to: [...Path.previous(path), previousChild.children.length]
424
- });
425
- return;
426
- }
427
- }
428
- Transforms.unwrapNodes(editor, {
429
- at: childPath
430
- });
431
- return;
432
- }
433
- if (node.type === 'list-item' && childNode.type === 'list-item-content' && index !== 0) {
434
- Transforms.splitNodes(editor, {
435
- at: childPath
436
- });
437
- return;
438
- }
439
- }
440
- }
441
- normalizeNode(entry);
442
- };
443
- return editor;
444
- }
445
- function nestList(editor) {
446
- const block = Editor.above(editor, {
447
- match: n => Element.isElement(n) && Editor.isBlock(editor, n)
448
- });
449
- if (!block || block[0].type !== 'list-item-content') {
450
- return false;
451
- }
452
- const listItemPath = Path.parent(block[1]);
453
- // we're the first item in the list therefore we can't nest
454
- if (listItemPath[listItemPath.length - 1] === 0) {
455
- return false;
456
- }
457
- const previousListItemPath = Path.previous(listItemPath);
458
- const previousListItemNode = Node.get(editor, previousListItemPath);
459
- if (previousListItemNode.children.length !== 1) {
460
- // there's a list nested inside our previous sibling list item so move there
461
- Transforms.moveNodes(editor, {
462
- at: listItemPath,
463
- to: [...previousListItemPath, previousListItemNode.children.length - 1, previousListItemNode.children[previousListItemNode.children.length - 1].children.length]
464
- });
465
- return true;
466
- }
467
- const type = Editor.parent(editor, Path.parent(block[1]))[0].type;
468
- Editor.withoutNormalizing(editor, () => {
469
- Transforms.wrapNodes(editor, {
470
- type,
471
- children: []
472
- }, {
473
- at: listItemPath
474
- });
475
- Transforms.moveNodes(editor, {
476
- to: [...previousListItemPath, previousListItemNode.children.length],
477
- at: listItemPath
478
- });
479
- });
480
- return true;
481
- }
482
- function unnestList(editor) {
483
- const block = Editor.above(editor, {
484
- match: n => Element.isElement(n) && Editor.isBlock(editor, n)
485
- });
486
- if (block && block[0].type === 'list-item-content') {
487
- Transforms.unwrapNodes(editor, {
488
- match: isListNode,
489
- split: true
490
- });
491
- return true;
492
- }
493
- return false;
494
- }
495
-
496
- function insertLayout(editor, layout) {
497
- insertNodesButReplaceIfSelectionIsAtEmptyParagraphOrHeading(editor, [{
498
- type: 'layout',
499
- layout,
500
- children: [{
501
- type: 'layout-area',
502
- children: [{
503
- type: 'paragraph',
504
- children: [{
505
- text: ''
506
- }]
507
- }]
508
- }]
509
- }]);
510
- const layoutEntry = Editor.above(editor, {
511
- match: x => x.type === 'layout'
512
- });
513
- if (layoutEntry) {
514
- Transforms.select(editor, [...layoutEntry[1], 0]);
515
- }
516
- }
517
-
518
- // Plugin
519
- function withLayouts(editor) {
520
- const {
521
- normalizeNode,
522
- deleteBackward
523
- } = editor;
524
- editor.deleteBackward = unit => {
525
- if (editor.selection && Range.isCollapsed(editor.selection) &&
526
- // this is just an little optimisation
527
- // we're only doing things if we're at the start of a layout area
528
- // and the start of anything will always be offset 0
529
- // so we'll bailout if we're not at offset 0
530
- editor.selection.anchor.offset === 0) {
531
- const [aboveNode, abovePath] = Editor.above(editor, {
532
- match: node => node.type === 'layout-area'
533
- }) || [editor, []];
534
- if (aboveNode.type === 'layout-area' && Point.equals(Editor.start(editor, abovePath), editor.selection.anchor)) {
535
- return;
536
- }
537
- }
538
- deleteBackward(unit);
539
- };
540
- editor.normalizeNode = entry => {
541
- const [node, path] = entry;
542
- if (Element.isElement(node) && node.type === 'layout') {
543
- if (node.layout === undefined) {
544
- Transforms.unwrapNodes(editor, {
545
- at: path
546
- });
547
- return;
548
- }
549
- if (node.children.length < node.layout.length) {
550
- Transforms.insertNodes(editor, Array.from({
551
- length: node.layout.length - node.children.length
552
- }).map(() => ({
553
- type: 'layout-area',
554
- children: [paragraphElement()]
555
- })), {
556
- at: [...path, node.children.length]
557
- });
558
- return;
559
- }
560
- if (node.children.length > node.layout.length) {
561
- Array.from({
562
- length: node.children.length - node.layout.length
563
- }).map((_, i) => i).reverse().forEach(i => {
564
- const layoutAreaToRemovePath = [...path, i + node.layout.length];
565
- const child = node.children[i + node.layout.length];
566
- moveChildren(editor, layoutAreaToRemovePath, [...path, node.layout.length - 1, node.children[node.layout.length - 1].children.length], node => node.type !== 'paragraph' || Node.string(child) !== '');
567
- Transforms.removeNodes(editor, {
568
- at: layoutAreaToRemovePath
569
- });
570
- });
571
- return;
572
- }
573
- }
574
- normalizeNode(entry);
575
- };
576
- return editor;
577
- }
578
-
579
- export { areArraysEqual as a, normalizeElementBasedOnDocumentFeatures as b, normalizeInlineBasedOnLinksAndRelationships as c, withParagraphs as d, withLayouts as e, withDocumentFeaturesNormalization as f, getAncestorComponentChildFieldDocumentFeatures as g, nestList as h, insertLayout as i, createToolbarState as j, normalizeTextBasedOnInlineMarksAndSoftBreaks as n, toggleList as t, unnestList as u, withList as w };