-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdiffCommand.ts
More file actions
134 lines (126 loc) · 4.17 KB
/
Copy pathdiffCommand.ts
File metadata and controls
134 lines (126 loc) · 4.17 KB
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
132
133
134
import { mergeRegister } from '@lexical/utils';
import { $getNodeByKey, $isElementNode, COMMAND_PRIORITY_EDITOR, LexicalEditor } from 'lexical';
import { DiffNode } from '../node/DiffNode';
import { DiffAction, LITEXML_DIFFNODE_ALL_COMMAND, LITEXML_DIFFNODE_COMMAND } from './symbols';
function doAction(node: DiffNode, action: DiffAction) {
if (node.diffType === 'modify') {
const children = node.getChildren();
if (action === DiffAction.Accept) {
node.replace(children[1], false).selectEnd();
} else if (action === DiffAction.Reject) {
node.replace(children[0], false).selectEnd();
}
}
if (node.diffType === 'remove') {
if (action === DiffAction.Accept) {
node.remove();
} else if (action === DiffAction.Reject) {
const children = node.getChildren();
node.replace(children[0], false).selectEnd();
}
}
if (node.diffType === 'add') {
if (action === DiffAction.Accept) {
const children = node.getChildren();
node.replace(children[0], false).selectEnd();
} else if (action === DiffAction.Reject) {
node.remove();
}
}
if (node.diffType === 'listItemModify') {
const children = node.getChildren();
if (action === DiffAction.Accept) {
const lastChild = children[1];
if (!$isElementNode(lastChild)) {
throw new Error('Expected element node as child of DiffNode');
}
const nodeChildrens = lastChild.getChildren();
for (let i = nodeChildrens.length - 1; i >= 0; i--) {
node.insertAfter(nodeChildrens[i]);
}
const parent = node.getParentOrThrow();
node.remove();
parent.selectEnd();
} else if (action === DiffAction.Reject) {
const firstChild = children[0];
if (!$isElementNode(firstChild)) {
throw new Error('Expected element node as child of DiffNode');
}
const nodeChildrens = firstChild.getChildren();
for (let i = nodeChildrens.length - 1; i >= 0; i--) {
node.insertAfter(nodeChildrens[i]);
}
const parent = node.getParentOrThrow();
node.remove();
parent.selectEnd();
}
}
if (node.diffType === 'listItemRemove') {
if (action === DiffAction.Accept) {
node.getParentOrThrow().remove();
} else if (action === DiffAction.Reject) {
node.getChildren().forEach((child) => {
node.getParentOrThrow().append(child);
});
node.getParentOrThrow().selectEnd();
node.remove();
}
}
if (node.diffType === 'listItemAdd') {
if (action === DiffAction.Accept) {
const children = node.getChildren();
children.forEach((child) => {
node.getParentOrThrow().append(child);
});
node.getParentOrThrow().selectEnd();
node.remove();
} else if (action === DiffAction.Reject) {
node.remove();
}
}
}
export function registerLiteXMLDiffCommand(editor: LexicalEditor) {
return mergeRegister(
editor.registerCommand(
LITEXML_DIFFNODE_COMMAND,
(payload) => {
const { action, nodeKey } = payload;
const node = editor.getEditorState().read(() => {
return $getNodeByKey(nodeKey) as DiffNode | null;
});
if (!node) {
return false;
}
editor.update(() => {
doAction(node, action);
});
return false;
},
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
LITEXML_DIFFNODE_ALL_COMMAND,
(payload) => {
const { action } = payload;
const nodes = editor.getEditorState().read(() => {
return Array.from(editor._editorState._nodeMap.values()).filter(
(n) => n instanceof DiffNode && !!n.getParent(),
) as DiffNode[];
});
if (!nodes.length) {
return false;
}
editor.update(() => {
nodes.forEach((node) => {
doAction(node, action);
});
});
return false;
},
COMMAND_PRIORITY_EDITOR,
),
);
}
// Command identities and the `DiffAction` enum live in the side-effect-free
// `./symbols` module so they stay single-instance across the package bundles.
export { DiffAction, LITEXML_DIFFNODE_ALL_COMMAND, LITEXML_DIFFNODE_COMMAND } from './symbols';