lkb-fields-document 1.0.0

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