-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcollapsible-node.tsx
More file actions
260 lines (212 loc) · 6.5 KB
/
collapsible-node.tsx
File metadata and controls
260 lines (212 loc) · 6.5 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
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
"use client";
import type {
DOMExportOutput,
LexicalNode,
NodeKey,
SerializedElementNode,
Spread,
} from "lexical";
import {
$applyNodeReplacement,
$createParagraphNode,
ElementNode,
} from "lexical";
// --- CollapsibleContainerNode ---
export type SerializedCollapsibleContainerNode = Spread<
{ open: boolean },
SerializedElementNode
>;
export class CollapsibleContainerNode extends ElementNode {
__open: boolean;
static getType(): string {
return "collapsible-container";
}
static clone(node: CollapsibleContainerNode): CollapsibleContainerNode {
return new CollapsibleContainerNode(node.__open, node.__key);
}
static importJSON(
serializedNode: SerializedCollapsibleContainerNode
): CollapsibleContainerNode {
const node = $createCollapsibleContainerNode(serializedNode.open);
return node;
}
constructor(open?: boolean, key?: NodeKey) {
super(key);
this.__open = open ?? true;
}
exportJSON(): SerializedCollapsibleContainerNode {
return {
...super.exportJSON(),
type: "collapsible-container",
version: 1,
open: this.__open,
};
}
createDOM(): HTMLElement {
const details = document.createElement("details");
details.className =
"mt-3 border border-white/[0.06] text-sm rounded-sm";
if (this.__open) {
details.open = true;
}
return details;
}
updateDOM(
prevNode: CollapsibleContainerNode,
dom: HTMLDetailsElement
): boolean {
if (prevNode.__open !== this.__open) {
dom.open = this.__open;
}
return false;
}
exportDOM(): DOMExportOutput {
const element = document.createElement("details");
if (this.__open) element.open = true;
return { element };
}
getOpen(): boolean {
return this.getLatest().__open;
}
setOpen(open: boolean): void {
const writable = this.getWritable();
writable.__open = open;
}
toggleOpen(): void {
this.setOpen(!this.getOpen());
}
isInline(): boolean {
return false;
}
}
// --- CollapsibleTitleNode ---
export type SerializedCollapsibleTitleNode = SerializedElementNode;
export class CollapsibleTitleNode extends ElementNode {
static getType(): string {
return "collapsible-title";
}
static clone(node: CollapsibleTitleNode): CollapsibleTitleNode {
return new CollapsibleTitleNode(node.__key);
}
static importJSON(): CollapsibleTitleNode {
return $createCollapsibleTitleNode();
}
exportJSON(): SerializedCollapsibleTitleNode {
return {
...super.exportJSON(),
type: "collapsible-title",
version: 1,
};
}
createDOM(): HTMLElement {
const summary = document.createElement("summary");
summary.className =
"flex items-center gap-1.5 p-3 text-sm font-medium text-foreground hover:bg-white/[0.04] list-none";
// Add a toggle chevron button as the visual affordance.
// The chevron rotates when the parent <details> is open.
const chevron = document.createElement("button");
chevron.type = "button";
chevron.contentEditable = "false";
chevron.className =
"collapsible-toggle flex-shrink-0 flex items-center justify-center w-5 h-5 rounded-sm text-muted-foreground hover:text-foreground hover:bg-white/[0.08] transition-transform duration-150";
chevron.setAttribute("aria-label", "Toggle section");
chevron.innerHTML =
'<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"></polyline></svg>';
summary.prepend(chevron);
return summary;
}
updateDOM(): boolean {
return false;
}
exportDOM(): DOMExportOutput {
const element = document.createElement("summary");
return { element };
}
isInline(): boolean {
return false;
}
collapseAtStart(): boolean {
// When backspacing at the start of the title, unwrap the collapsible
const container = this.getParent();
if (container instanceof CollapsibleContainerNode) {
const contentNode = this.getNextSibling();
const paragraph = $createParagraphNode();
const children = this.getChildren();
children.forEach((child) => paragraph.append(child));
container.insertBefore(paragraph);
// Move content children out of the container
if (contentNode instanceof CollapsibleContentNode) {
const contentChildren = contentNode.getChildren();
contentChildren.forEach((child) => {
container.insertBefore(child);
});
}
container.remove();
paragraph.selectStart();
return true;
}
return false;
}
}
// --- CollapsibleContentNode ---
export type SerializedCollapsibleContentNode = SerializedElementNode;
export class CollapsibleContentNode extends ElementNode {
static getType(): string {
return "collapsible-content";
}
static clone(node: CollapsibleContentNode): CollapsibleContentNode {
return new CollapsibleContentNode(node.__key);
}
static importJSON(): CollapsibleContentNode {
return $createCollapsibleContentNode();
}
exportJSON(): SerializedCollapsibleContentNode {
return {
...super.exportJSON(),
type: "collapsible-content",
version: 1,
};
}
createDOM(): HTMLElement {
const div = document.createElement("div");
div.className = "border-t border-white/[0.06] p-3 text-sm";
return div;
}
updateDOM(): boolean {
return false;
}
exportDOM(): DOMExportOutput {
const element = document.createElement("div");
return { element };
}
isInline(): boolean {
return false;
}
}
// --- Factory functions ---
export function $createCollapsibleContainerNode(
open?: boolean
): CollapsibleContainerNode {
return $applyNodeReplacement(new CollapsibleContainerNode(open));
}
export function $createCollapsibleTitleNode(): CollapsibleTitleNode {
return $applyNodeReplacement(new CollapsibleTitleNode());
}
export function $createCollapsibleContentNode(): CollapsibleContentNode {
return $applyNodeReplacement(new CollapsibleContentNode());
}
export function $isCollapsibleContainerNode(
node: LexicalNode | null | undefined
): node is CollapsibleContainerNode {
return node instanceof CollapsibleContainerNode;
}
export function $isCollapsibleTitleNode(
node: LexicalNode | null | undefined
): node is CollapsibleTitleNode {
return node instanceof CollapsibleTitleNode;
}
export function $isCollapsibleContentNode(
node: LexicalNode | null | undefined
): node is CollapsibleContentNode {
return node instanceof CollapsibleContentNode;
}