-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
splitNode.ts
59 lines (47 loc) · 1.58 KB
/
splitNode.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
import { SplitNodeOperation } from 'slate';
import invariant from 'tiny-invariant';
import { SharedType, SyncNode } from '../../model';
import { getParent } from '../../path';
import cloneSyncElement from '../../utils/clone';
/**
* Applies a split node operation to a SharedType
*
* @param doc
* @param op
*/
export default function splitNode(
doc: SharedType,
op: SplitNodeOperation
): SharedType {
const [parent, index]: [SyncNode, number] = getParent(doc, op.path);
const children = SyncNode.getChildren(parent);
invariant(children, 'Parent of node should have children');
const target = children.get(index);
const inject = cloneSyncElement(target);
children.insert(index + 1, [inject]);
Object.entries(op.properties).forEach(([key, value]) =>
inject.set(key, value)
);
if (SyncNode.getText(target) !== undefined) {
const targetText = SyncNode.getText(target);
const injectText = SyncNode.getText(inject);
invariant(targetText);
invariant(injectText);
if (targetText.length > op.position) {
targetText.delete(op.position, targetText.length - op.position);
}
if (injectText !== undefined && op.position !== undefined) {
injectText.delete(0, op.position);
}
} else {
const targetChildren = SyncNode.getChildren(target);
const injectChildren = SyncNode.getChildren(inject);
invariant(targetChildren);
invariant(injectChildren);
targetChildren.delete(op.position, targetChildren.length - op.position);
if (op.position !== undefined) {
injectChildren.delete(0, op.position);
}
}
return doc;
}