-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathCollapsibleTitleNode.ts
126 lines (113 loc) · 3.29 KB
/
CollapsibleTitleNode.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
/**
* 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 {
$createParagraphNode,
$isElementNode,
DOMConversionMap,
DOMConversionOutput,
EditorConfig,
ElementNode,
LexicalEditor,
LexicalNode,
RangeSelection,
StaticNodeConfigRecord,
} from 'lexical';
import {IS_CHROME} from 'shared/environment';
import invariant from 'shared/invariant';
import {$isCollapsibleContainerNode} from './CollapsibleContainerNode';
import {$isCollapsibleContentNode} from './CollapsibleContentNode';
export function $convertSummaryElement(
domNode: HTMLElement,
): DOMConversionOutput | null {
const node = $createCollapsibleTitleNode();
return {
node,
};
}
/** @noInheritDoc */
export class CollapsibleTitleNode extends ElementNode {
/** @internal */
$config(): StaticNodeConfigRecord<
'collapsible-title',
{$transform: (node: CollapsibleTitleNode) => void}
> {
return this.config('collapsible-title', {
$transform(node: CollapsibleTitleNode) {
if (node.isEmpty()) {
node.remove();
}
},
});
}
createDOM(config: EditorConfig, editor: LexicalEditor): HTMLElement {
const dom = document.createElement('summary');
dom.classList.add('Collapsible__title');
if (IS_CHROME) {
dom.addEventListener('click', () => {
editor.update(() => {
const collapsibleContainer = this.getLatest().getParentOrThrow();
invariant(
$isCollapsibleContainerNode(collapsibleContainer),
'Expected parent node to be a CollapsibleContainerNode',
);
collapsibleContainer.toggleOpen();
});
});
}
return dom;
}
updateDOM(prevNode: this, dom: HTMLElement): boolean {
return false;
}
static importDOM(): DOMConversionMap | null {
return {
summary: (domNode: HTMLElement) => {
return {
conversion: $convertSummaryElement,
priority: 1,
};
},
};
}
insertNewAfter(_: RangeSelection, restoreSelection = true): ElementNode {
const containerNode = this.getParentOrThrow();
if (!$isCollapsibleContainerNode(containerNode)) {
throw new Error(
'CollapsibleTitleNode expects to be child of CollapsibleContainerNode',
);
}
if (containerNode.getOpen()) {
const contentNode = this.getNextSibling();
if (!$isCollapsibleContentNode(contentNode)) {
throw new Error(
'CollapsibleTitleNode expects to have CollapsibleContentNode sibling',
);
}
const firstChild = contentNode.getFirstChild();
if ($isElementNode(firstChild)) {
return firstChild;
} else {
const paragraph = $createParagraphNode();
contentNode.append(paragraph);
return paragraph;
}
} else {
const paragraph = $createParagraphNode();
containerNode.insertAfter(paragraph, restoreSelection);
return paragraph;
}
}
}
export function $createCollapsibleTitleNode(): CollapsibleTitleNode {
return new CollapsibleTitleNode();
}
export function $isCollapsibleTitleNode(
node: LexicalNode | null | undefined,
): node is CollapsibleTitleNode {
return node instanceof CollapsibleTitleNode;
}