-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathLexicalCodeBlockNode.ts
204 lines (170 loc) · 5.89 KB
/
LexicalCodeBlockNode.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
import {
DecoratorNode,
DOMConversion,
DOMConversionMap,
DOMConversionOutput, DOMExportOutput,
LexicalEditor, LexicalNode,
SerializedLexicalNode,
Spread
} from "lexical";
import type {EditorConfig} from "lexical/LexicalEditor";
import {EditorDecoratorAdapter} from "../../ui/framework/decorator";
import {CodeEditor} from "../../../components";
import {el} from "../../utils/dom";
export type SerializedCodeBlockNode = Spread<{
language: string;
id: string;
code: string;
}, SerializedLexicalNode>
const getLanguageFromClassList = (classes: string) => {
const langClasses = classes.split(' ').filter(cssClass => cssClass.startsWith('language-'));
return (langClasses[0] || '').replace('language-', '');
};
export class CodeBlockNode extends DecoratorNode<EditorDecoratorAdapter> {
__id: string = '';
__language: string = '';
__code: string = '';
static getType(): string {
return 'code-block';
}
static clone(node: CodeBlockNode): CodeBlockNode {
const newNode = new CodeBlockNode(node.__language, node.__code, node.__key);
newNode.__id = node.__id;
return newNode;
}
constructor(language: string = '', code: string = '', key?: string) {
super(key);
this.__language = language;
this.__code = code;
}
setLanguage(language: string): void {
const self = this.getWritable();
self.__language = language;
}
getLanguage(): string {
const self = this.getLatest();
return self.__language;
}
setCode(code: string): void {
const self = this.getWritable();
self.__code = code;
}
getCode(): string {
const self = this.getLatest();
return self.__code;
}
setId(id: string) {
const self = this.getWritable();
self.__id = id;
}
getId(): string {
const self = this.getLatest();
return self.__id;
}
decorate(editor: LexicalEditor, config: EditorConfig): EditorDecoratorAdapter {
return {
type: 'code',
getNode: () => this,
};
}
isInline(): boolean {
return false;
}
isIsolated() {
return true;
}
createDOM(_config: EditorConfig, _editor: LexicalEditor) {
const codeBlock = el('pre', {
id: this.__id || null,
}, [
el('code', {
class: this.__language ? `language-${this.__language}` : null,
}, [this.__code]),
]);
return el('div', {class: 'editor-code-block-wrap'}, [codeBlock]);
}
updateDOM(prevNode: CodeBlockNode, dom: HTMLElement) {
const code = dom.querySelector('code');
if (!code) return false;
if (prevNode.__language !== this.__language) {
code.className = this.__language ? `language-${this.__language}` : '';
}
if (prevNode.__id !== this.__id) {
dom.setAttribute('id', this.__id);
}
if (prevNode.__code !== this.__code) {
code.textContent = this.__code;
}
return false;
}
exportDOM(editor: LexicalEditor): DOMExportOutput {
const dom = this.createDOM(editor._config, editor);
return {
element: dom.querySelector('pre') as HTMLElement,
};
}
static importDOM(): DOMConversionMap|null {
return {
pre(node: HTMLElement): DOMConversion|null {
return {
conversion: (element: HTMLElement): DOMConversionOutput|null => {
const codeEl = element.querySelector('code');
const language = getLanguageFromClassList(element.className)
|| (codeEl && getLanguageFromClassList(codeEl.className))
|| '';
const code = codeEl ? (codeEl.textContent || '').trim() : (element.textContent || '').trim();
const node = $createCodeBlockNode(language, code);
if (element.id) {
node.setId(element.id);
}
return {
node,
after(childNodes): LexicalNode[] {
// Remove any child nodes that may get parsed since we're manually
// controlling the code contents.
return [];
},
};
},
priority: 3,
};
},
};
}
exportJSON(): SerializedCodeBlockNode {
return {
type: 'code-block',
version: 1,
id: this.__id,
language: this.__language,
code: this.__code,
};
}
static importJSON(serializedNode: SerializedCodeBlockNode): CodeBlockNode {
const node = $createCodeBlockNode(serializedNode.language, serializedNode.code);
node.setId(serializedNode.id || '');
return node;
}
}
export function $createCodeBlockNode(language: string = '', code: string = ''): CodeBlockNode {
return new CodeBlockNode(language, code);
}
export function $isCodeBlockNode(node: LexicalNode | null | undefined) {
return node instanceof CodeBlockNode;
}
export function $openCodeEditorForNode(editor: LexicalEditor, node: CodeBlockNode): void {
const code = node.getCode();
const language = node.getLanguage();
// @ts-ignore
const codeEditor = window.$components.first('code-editor') as CodeEditor;
// TODO - Handle direction
codeEditor.open(code, language, 'ltr', (newCode: string, newLang: string) => {
editor.update(() => {
node.setCode(newCode);
node.setLanguage(newLang);
});
// TODO - Re-focus
}, () => {
// TODO - Re-focus
});
}