-
Notifications
You must be signed in to change notification settings - Fork 817
/
Copy pathnormalizeDescendantsToDocumentFragment.ts
131 lines (110 loc) · 3.14 KB
/
normalizeDescendantsToDocumentFragment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import {
type Descendant,
type Editor,
ElementApi,
TextApi,
} from '@udecode/slate';
import type { SlateEditor } from '../editor';
import type { WithRequiredKey } from '../plugin';
import { BaseParagraphPlugin } from '../plugins';
const isInlineNode = (editor: Editor) => (node: Descendant) =>
TextApi.isText(node) ||
(ElementApi.isElement(node) && editor.api.isInline(node));
const makeBlockLazy = (type: string) => (): Descendant => ({
children: [],
type,
});
const hasDifferentChildNodes = (
descendants: Descendant[],
isInline: (node: Descendant) => boolean
): boolean => {
return descendants.some((descendant, index, arr) => {
const prevDescendant = arr[index - 1];
if (index !== 0) {
return isInline(descendant) !== isInline(prevDescendant);
}
return false;
});
};
/**
* Handles 3rd constraint: "Block nodes can only contain other blocks, or inline
* and text nodes."
*/
const normalizeDifferentNodeTypes = (
descendants: Descendant[],
isInline: (node: Descendant) => boolean,
makeDefaultBlock: () => Descendant
): Descendant[] => {
const hasDifferentNodes = hasDifferentChildNodes(descendants, isInline);
const { fragment } = descendants.reduce(
(memo, node) => {
if (hasDifferentNodes && isInline(node)) {
let block = memo.precedingBlock;
if (!block) {
block = makeDefaultBlock();
memo.precedingBlock = block;
memo.fragment.push(block);
}
(block.children as Descendant[]).push(node);
} else {
memo.fragment.push(node);
memo.precedingBlock = null;
}
return memo;
},
{
fragment: [] as Descendant[],
precedingBlock: null as Descendant | null,
}
);
return fragment;
};
/**
* Handles 1st constraint: "All Element nodes must contain at least one Text
* descendant."
*/
const normalizeEmptyChildren = (descendants: Descendant[]): Descendant[] => {
if (descendants.length === 0) {
return [{ text: '' } as Descendant];
}
return descendants;
};
const normalize = (
descendants: Descendant[],
isInline: (node: Descendant) => boolean,
makeDefaultBlock: () => Descendant
): Descendant[] => {
descendants = normalizeEmptyChildren(descendants);
descendants = normalizeDifferentNodeTypes(
descendants,
isInline,
makeDefaultBlock
);
descendants = descendants.map((node) => {
if (ElementApi.isElement(node)) {
return {
...node,
children: normalize(
node.children as Descendant[],
isInline,
makeDefaultBlock
),
};
}
return node;
});
return descendants;
};
/** Normalize the descendants to a valid document fragment. */
export const normalizeDescendantsToDocumentFragment = (
editor: SlateEditor,
{
defaultElementPlugin = BaseParagraphPlugin,
descendants,
}: { descendants: Descendant[]; defaultElementPlugin?: WithRequiredKey }
): Descendant[] => {
const isInline = isInlineNode(editor);
const defaultType = editor.getType(defaultElementPlugin);
const makeDefaultBlock = makeBlockLazy(defaultType);
return normalize(descendants, isInline, makeDefaultBlock as any);
};