/
index.ts
286 lines (258 loc) · 8.82 KB
/
index.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import './Collapsible.css';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {
$findMatchingParent,
$insertNodeToNearestRoot,
mergeRegister,
} from '@lexical/utils';
import {
$createParagraphNode,
$getSelection,
$isRangeSelection,
COMMAND_PRIORITY_LOW,
createCommand,
DELETE_CHARACTER_COMMAND,
ElementNode,
INSERT_PARAGRAPH_COMMAND,
KEY_ARROW_DOWN_COMMAND,
KEY_ARROW_LEFT_COMMAND,
KEY_ARROW_RIGHT_COMMAND,
KEY_ARROW_UP_COMMAND,
LexicalNode,
NodeKey,
} from 'lexical';
import {useEffect} from 'react';
import {
$createCollapsibleContainerNode,
$isCollapsibleContainerNode,
CollapsibleContainerNode,
} from './CollapsibleContainerNode';
import {
$createCollapsibleContentNode,
$isCollapsibleContentNode,
CollapsibleContentNode,
} from './CollapsibleContentNode';
import {
$createCollapsibleTitleNode,
$isCollapsibleTitleNode,
CollapsibleTitleNode,
} from './CollapsibleTitleNode';
export const INSERT_COLLAPSIBLE_COMMAND = createCommand<void>();
export const TOGGLE_COLLAPSIBLE_COMMAND = createCommand<NodeKey>();
export default function CollapsiblePlugin(): null {
const [editor] = useLexicalComposerContext();
useEffect(() => {
if (
!editor.hasNodes([
CollapsibleContainerNode,
CollapsibleTitleNode,
CollapsibleContentNode,
])
) {
throw new Error(
'CollapsiblePlugin: CollapsibleContainerNode, CollapsibleTitleNode, or CollapsibleContentNode not registered on editor',
);
}
const onEscapeUp = () => {
const selection = $getSelection();
if (
$isRangeSelection(selection) &&
selection.isCollapsed() &&
selection.anchor.offset === 0
) {
const container = $findMatchingParent(
selection.anchor.getNode(),
$isCollapsibleContainerNode,
);
if ($isCollapsibleContainerNode(container)) {
const parent = container.getParent<ElementNode>();
if (
parent !== null &&
parent.getFirstChild<LexicalNode>() === container &&
selection.anchor.key ===
container.getFirstDescendant<LexicalNode>()?.getKey()
) {
container.insertBefore($createParagraphNode());
}
}
}
return false;
};
const onEscapeDown = () => {
const selection = $getSelection();
if ($isRangeSelection(selection) && selection.isCollapsed()) {
const container = $findMatchingParent(
selection.anchor.getNode(),
$isCollapsibleContainerNode,
);
if ($isCollapsibleContainerNode(container)) {
const parent = container.getParent<ElementNode>();
if (
parent !== null &&
parent.getLastChild<LexicalNode>() === container
) {
const titleParagraph = container.getFirstDescendant<LexicalNode>();
const contentParagraph = container.getLastDescendant<LexicalNode>();
if (
(contentParagraph !== null &&
selection.anchor.key === contentParagraph.getKey() &&
selection.anchor.offset ===
contentParagraph.getTextContentSize()) ||
(titleParagraph !== null &&
selection.anchor.key === titleParagraph.getKey() &&
selection.anchor.offset === titleParagraph.getTextContentSize())
) {
container.insertAfter($createParagraphNode());
}
}
}
}
return false;
};
return mergeRegister(
// Structure enforcing transformers for each node type. In case nesting structure is not
// "Container > Title + Content" it'll unwrap nodes and convert it back
// to regular content.
editor.registerNodeTransform(CollapsibleContentNode, (node) => {
const parent = node.getParent<ElementNode>();
if (!$isCollapsibleContainerNode(parent)) {
const children = node.getChildren<LexicalNode>();
for (const child of children) {
node.insertBefore(child);
}
node.remove();
}
}),
editor.registerNodeTransform(CollapsibleTitleNode, (node) => {
const parent = node.getParent<ElementNode>();
if (!$isCollapsibleContainerNode(parent)) {
node.replace(
$createParagraphNode().append(...node.getChildren<LexicalNode>()),
);
return;
}
}),
editor.registerNodeTransform(CollapsibleContainerNode, (node) => {
const children = node.getChildren<LexicalNode>();
if (
children.length !== 2 ||
!$isCollapsibleTitleNode(children[0]) ||
!$isCollapsibleContentNode(children[1])
) {
for (const child of children) {
node.insertBefore(child);
}
node.remove();
}
}),
// This handles the case when container is collapsed and we delete its previous sibling
// into it, it would cause collapsed content deleted (since it's display: none, and selection
// swallows it when deletes single char). Instead we expand container, which is although
// not perfect, but avoids bigger problem
editor.registerCommand(
DELETE_CHARACTER_COMMAND,
() => {
const selection = $getSelection();
if (
!$isRangeSelection(selection) ||
!selection.isCollapsed() ||
selection.anchor.offset !== 0
) {
return false;
}
const anchorNode = selection.anchor.getNode();
const topLevelElement = anchorNode.getTopLevelElement();
if (topLevelElement === null) {
return false;
}
const container = topLevelElement.getPreviousSibling<LexicalNode>();
if (!$isCollapsibleContainerNode(container) || container.getOpen()) {
return false;
}
container.setOpen(true);
return true;
},
COMMAND_PRIORITY_LOW,
),
// When collapsible is the last child pressing down/right arrow will insert paragraph
// below it to allow adding more content. It's similar what $insertBlockNode
// (mainly for decorators), except it'll always be possible to continue adding
// new content even if trailing paragraph is accidentally deleted
editor.registerCommand(
KEY_ARROW_DOWN_COMMAND,
onEscapeDown,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
KEY_ARROW_RIGHT_COMMAND,
onEscapeDown,
COMMAND_PRIORITY_LOW,
),
// When collapsible is the first child pressing up/left arrow will insert paragraph
// above it to allow adding more content. It's similar what $insertBlockNode
// (mainly for decorators), except it'll always be possible to continue adding
// new content even if leading paragraph is accidentally deleted
editor.registerCommand(
KEY_ARROW_UP_COMMAND,
onEscapeUp,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
KEY_ARROW_LEFT_COMMAND,
onEscapeUp,
COMMAND_PRIORITY_LOW,
),
// Enter goes from Title to Content rather than a new line inside Title
editor.registerCommand(
INSERT_PARAGRAPH_COMMAND,
() => {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
const titleNode = $findMatchingParent(
selection.anchor.getNode(),
(node) => $isCollapsibleTitleNode(node),
);
if ($isCollapsibleTitleNode(titleNode)) {
const container = titleNode.getParent<ElementNode>();
if (container && $isCollapsibleContainerNode(container)) {
if (!container.getOpen()) {
container.toggleOpen();
}
titleNode.getNextSibling()?.selectEnd();
return true;
}
}
}
return false;
},
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
INSERT_COLLAPSIBLE_COMMAND,
() => {
editor.update(() => {
const title = $createCollapsibleTitleNode();
const paragraph = $createParagraphNode();
$insertNodeToNearestRoot(
$createCollapsibleContainerNode(true).append(
title.append(paragraph),
$createCollapsibleContentNode().append($createParagraphNode()),
),
);
paragraph.select();
});
return true;
},
COMMAND_PRIORITY_LOW,
),
);
}, [editor]);
return null;
}