@lexical/code-core 0.41.1-nightly.20260309.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CodeExtension.d.ts +4 -0
- package/CodeHighlightNode.d.ts +34 -0
- package/CodeNode.d.ts +47 -0
- package/FlatStructureUtils.d.ts +25 -0
- package/LICENSE +21 -0
- package/LexicalCodeCore.dev.js +664 -0
- package/LexicalCodeCore.dev.mjs +649 -0
- package/LexicalCodeCore.js +11 -0
- package/LexicalCodeCore.js.flow +107 -0
- package/LexicalCodeCore.mjs +25 -0
- package/LexicalCodeCore.node.mjs +23 -0
- package/LexicalCodeCore.prod.js +9 -0
- package/LexicalCodeCore.prod.mjs +9 -0
- package/README.md +7 -0
- package/index.d.ts +12 -0
- package/package.json +41 -0
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
var lexical = require('lexical');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
15
|
+
*
|
|
16
|
+
* This source code is licensed under the MIT license found in the
|
|
17
|
+
* LICENSE file in the root directory of this source tree.
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
// Do not require this module directly! Use normal `invariant` calls.
|
|
22
|
+
|
|
23
|
+
function formatDevErrorMessage(message) {
|
|
24
|
+
throw new Error(message);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function $getLastMatchingCodeNode(anchor, direction) {
|
|
28
|
+
let matchingNode = anchor;
|
|
29
|
+
for (let caret = lexical.$getSiblingCaret(anchor, direction); caret && ($isCodeHighlightNode(caret.origin) || lexical.$isTabNode(caret.origin)); caret = caret.getAdjacentCaret()) {
|
|
30
|
+
matchingNode = caret.origin;
|
|
31
|
+
}
|
|
32
|
+
return matchingNode;
|
|
33
|
+
}
|
|
34
|
+
function $getFirstCodeNodeOfLine(anchor) {
|
|
35
|
+
return $getLastMatchingCodeNode(anchor, 'previous');
|
|
36
|
+
}
|
|
37
|
+
function $getLastCodeNodeOfLine(anchor) {
|
|
38
|
+
return $getLastMatchingCodeNode(anchor, 'next');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Determines the visual writing direction of a code line.
|
|
43
|
+
*
|
|
44
|
+
* Scans the line segments (CodeHighlightNode/TabNode) from start to end
|
|
45
|
+
* and returns the first strong direction found ("ltr" or "rtl").
|
|
46
|
+
* If no strong character is found, falls back to the parent element's
|
|
47
|
+
* direction. Returns null if indeterminate.
|
|
48
|
+
*/
|
|
49
|
+
function $getCodeLineDirection(anchor) {
|
|
50
|
+
const start = $getFirstCodeNodeOfLine(anchor);
|
|
51
|
+
const end = $getLastCodeNodeOfLine(anchor);
|
|
52
|
+
let node = start;
|
|
53
|
+
while (node !== null) {
|
|
54
|
+
if ($isCodeHighlightNode(node)) {
|
|
55
|
+
const direction = lexical.getTextDirection(node.getTextContent());
|
|
56
|
+
if (direction !== null) {
|
|
57
|
+
return direction;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (node === end) {
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
node = node.getNextSibling();
|
|
64
|
+
}
|
|
65
|
+
const parent = start.getParent();
|
|
66
|
+
if (lexical.$isElementNode(parent)) {
|
|
67
|
+
const parentDirection = parent.getDirection();
|
|
68
|
+
if (parentDirection === 'ltr' || parentDirection === 'rtl') {
|
|
69
|
+
return parentDirection;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
function $getStartOfCodeInLine(anchor, offset) {
|
|
75
|
+
let last = null;
|
|
76
|
+
let lastNonBlank = null;
|
|
77
|
+
let node = anchor;
|
|
78
|
+
let nodeOffset = offset;
|
|
79
|
+
let nodeTextContent = anchor.getTextContent();
|
|
80
|
+
// eslint-disable-next-line no-constant-condition
|
|
81
|
+
while (true) {
|
|
82
|
+
if (nodeOffset === 0) {
|
|
83
|
+
node = node.getPreviousSibling();
|
|
84
|
+
if (node === null) {
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
if (!($isCodeHighlightNode(node) || lexical.$isTabNode(node) || lexical.$isLineBreakNode(node))) {
|
|
88
|
+
formatDevErrorMessage(`Expected a valid Code Node: CodeHighlightNode, TabNode, LineBreakNode`);
|
|
89
|
+
}
|
|
90
|
+
if (lexical.$isLineBreakNode(node)) {
|
|
91
|
+
last = {
|
|
92
|
+
node,
|
|
93
|
+
offset: 1
|
|
94
|
+
};
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
nodeOffset = Math.max(0, node.getTextContentSize() - 1);
|
|
98
|
+
nodeTextContent = node.getTextContent();
|
|
99
|
+
} else {
|
|
100
|
+
nodeOffset--;
|
|
101
|
+
}
|
|
102
|
+
const character = nodeTextContent[nodeOffset];
|
|
103
|
+
if ($isCodeHighlightNode(node) && character !== ' ') {
|
|
104
|
+
lastNonBlank = {
|
|
105
|
+
node,
|
|
106
|
+
offset: nodeOffset
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// lastNonBlank !== null: anchor in the middle of code; move to line beginning
|
|
111
|
+
if (lastNonBlank !== null) {
|
|
112
|
+
return lastNonBlank;
|
|
113
|
+
}
|
|
114
|
+
// Spaces, tabs or nothing ahead of anchor
|
|
115
|
+
let codeCharacterAtAnchorOffset = null;
|
|
116
|
+
if (offset < anchor.getTextContentSize()) {
|
|
117
|
+
if ($isCodeHighlightNode(anchor)) {
|
|
118
|
+
codeCharacterAtAnchorOffset = anchor.getTextContent()[offset];
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
const nextSibling = anchor.getNextSibling();
|
|
122
|
+
if ($isCodeHighlightNode(nextSibling)) {
|
|
123
|
+
codeCharacterAtAnchorOffset = nextSibling.getTextContent()[0];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (codeCharacterAtAnchorOffset !== null && codeCharacterAtAnchorOffset !== ' ') {
|
|
127
|
+
// Borderline whitespace and code, move to line beginning
|
|
128
|
+
return last;
|
|
129
|
+
} else {
|
|
130
|
+
const nextNonBlank = findNextNonBlankInLine(anchor, offset);
|
|
131
|
+
if (nextNonBlank !== null) {
|
|
132
|
+
return nextNonBlank;
|
|
133
|
+
} else {
|
|
134
|
+
return last;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function findNextNonBlankInLine(anchor, offset) {
|
|
139
|
+
let node = anchor;
|
|
140
|
+
let nodeOffset = offset;
|
|
141
|
+
let nodeTextContent = anchor.getTextContent();
|
|
142
|
+
let nodeTextContentSize = anchor.getTextContentSize();
|
|
143
|
+
// eslint-disable-next-line no-constant-condition
|
|
144
|
+
while (true) {
|
|
145
|
+
if (!$isCodeHighlightNode(node) || nodeOffset === nodeTextContentSize) {
|
|
146
|
+
node = node.getNextSibling();
|
|
147
|
+
if (node === null || lexical.$isLineBreakNode(node)) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
if ($isCodeHighlightNode(node)) {
|
|
151
|
+
nodeOffset = 0;
|
|
152
|
+
nodeTextContent = node.getTextContent();
|
|
153
|
+
nodeTextContentSize = node.getTextContentSize();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if ($isCodeHighlightNode(node)) {
|
|
157
|
+
if (nodeTextContent[nodeOffset] !== ' ') {
|
|
158
|
+
return {
|
|
159
|
+
node,
|
|
160
|
+
offset: nodeOffset
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
nodeOffset++;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function $getEndOfCodeInLine(anchor) {
|
|
168
|
+
const lastNode = $getLastCodeNodeOfLine(anchor);
|
|
169
|
+
if (!!lexical.$isLineBreakNode(lastNode)) {
|
|
170
|
+
formatDevErrorMessage(`Unexpected lineBreakNode in getEndOfCodeInLine`);
|
|
171
|
+
}
|
|
172
|
+
return lastNode;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
177
|
+
*
|
|
178
|
+
* This source code is licensed under the MIT license found in the
|
|
179
|
+
* LICENSE file in the root directory of this source tree.
|
|
180
|
+
*
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
const DEFAULT_CODE_LANGUAGE = 'javascript';
|
|
184
|
+
const getDefaultCodeLanguage = () => DEFAULT_CODE_LANGUAGE;
|
|
185
|
+
function hasChildDOMNodeTag(node, tagName) {
|
|
186
|
+
for (const child of node.childNodes) {
|
|
187
|
+
if (lexical.isHTMLElement(child) && child.tagName === tagName) {
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
hasChildDOMNodeTag(child, tagName);
|
|
191
|
+
}
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
const LANGUAGE_DATA_ATTRIBUTE = 'data-language';
|
|
195
|
+
const HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE = 'data-highlight-language';
|
|
196
|
+
const THEME_DATA_ATTRIBUTE = 'data-theme';
|
|
197
|
+
|
|
198
|
+
/** @noInheritDoc */
|
|
199
|
+
class CodeNode extends lexical.ElementNode {
|
|
200
|
+
/** @internal */
|
|
201
|
+
__language;
|
|
202
|
+
/** @internal */
|
|
203
|
+
__theme;
|
|
204
|
+
/** @internal */
|
|
205
|
+
__isSyntaxHighlightSupported;
|
|
206
|
+
static getType() {
|
|
207
|
+
return 'code';
|
|
208
|
+
}
|
|
209
|
+
static clone(node) {
|
|
210
|
+
return new CodeNode(node.__language, node.__key);
|
|
211
|
+
}
|
|
212
|
+
constructor(language, key) {
|
|
213
|
+
super(key);
|
|
214
|
+
this.__language = language || undefined;
|
|
215
|
+
this.__isSyntaxHighlightSupported = false;
|
|
216
|
+
this.__theme = undefined;
|
|
217
|
+
}
|
|
218
|
+
afterCloneFrom(prevNode) {
|
|
219
|
+
super.afterCloneFrom(prevNode);
|
|
220
|
+
this.__language = prevNode.__language;
|
|
221
|
+
this.__theme = prevNode.__theme;
|
|
222
|
+
this.__isSyntaxHighlightSupported = prevNode.__isSyntaxHighlightSupported;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// View
|
|
226
|
+
createDOM(config) {
|
|
227
|
+
const element = document.createElement('code');
|
|
228
|
+
lexical.addClassNamesToElement(element, config.theme.code);
|
|
229
|
+
element.setAttribute('spellcheck', 'false');
|
|
230
|
+
const language = this.getLanguage();
|
|
231
|
+
if (language) {
|
|
232
|
+
element.setAttribute(LANGUAGE_DATA_ATTRIBUTE, language);
|
|
233
|
+
if (this.getIsSyntaxHighlightSupported()) {
|
|
234
|
+
element.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const theme = this.getTheme();
|
|
238
|
+
if (theme) {
|
|
239
|
+
element.setAttribute(THEME_DATA_ATTRIBUTE, theme);
|
|
240
|
+
}
|
|
241
|
+
const style = this.getStyle();
|
|
242
|
+
if (style) {
|
|
243
|
+
element.setAttribute('style', style);
|
|
244
|
+
}
|
|
245
|
+
return element;
|
|
246
|
+
}
|
|
247
|
+
updateDOM(prevNode, dom, config) {
|
|
248
|
+
const language = this.__language;
|
|
249
|
+
const prevLanguage = prevNode.__language;
|
|
250
|
+
if (language) {
|
|
251
|
+
if (language !== prevLanguage) {
|
|
252
|
+
dom.setAttribute(LANGUAGE_DATA_ATTRIBUTE, language);
|
|
253
|
+
}
|
|
254
|
+
} else if (prevLanguage) {
|
|
255
|
+
dom.removeAttribute(LANGUAGE_DATA_ATTRIBUTE);
|
|
256
|
+
}
|
|
257
|
+
const isSyntaxHighlightSupported = this.__isSyntaxHighlightSupported;
|
|
258
|
+
const prevIsSyntaxHighlightSupported = prevNode.__isSyntaxHighlightSupported;
|
|
259
|
+
if (prevIsSyntaxHighlightSupported && prevLanguage) {
|
|
260
|
+
if (isSyntaxHighlightSupported && language) {
|
|
261
|
+
if (language !== prevLanguage) {
|
|
262
|
+
dom.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
|
|
263
|
+
}
|
|
264
|
+
} else {
|
|
265
|
+
dom.removeAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE);
|
|
266
|
+
}
|
|
267
|
+
} else if (isSyntaxHighlightSupported && language) {
|
|
268
|
+
dom.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
|
|
269
|
+
}
|
|
270
|
+
const theme = this.__theme;
|
|
271
|
+
const prevTheme = prevNode.__theme;
|
|
272
|
+
if (theme) {
|
|
273
|
+
if (theme !== prevTheme) {
|
|
274
|
+
dom.setAttribute(THEME_DATA_ATTRIBUTE, theme);
|
|
275
|
+
}
|
|
276
|
+
} else if (prevTheme) {
|
|
277
|
+
dom.removeAttribute(THEME_DATA_ATTRIBUTE);
|
|
278
|
+
}
|
|
279
|
+
const style = this.__style;
|
|
280
|
+
const prevStyle = prevNode.__style;
|
|
281
|
+
if (style) {
|
|
282
|
+
if (style !== prevStyle) {
|
|
283
|
+
dom.setAttribute('style', style);
|
|
284
|
+
}
|
|
285
|
+
} else if (prevStyle) {
|
|
286
|
+
dom.removeAttribute('style');
|
|
287
|
+
}
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
exportDOM(editor) {
|
|
291
|
+
const element = document.createElement('pre');
|
|
292
|
+
lexical.addClassNamesToElement(element, editor._config.theme.code);
|
|
293
|
+
element.setAttribute('spellcheck', 'false');
|
|
294
|
+
const language = this.getLanguage();
|
|
295
|
+
if (language) {
|
|
296
|
+
element.setAttribute(LANGUAGE_DATA_ATTRIBUTE, language);
|
|
297
|
+
if (this.getIsSyntaxHighlightSupported()) {
|
|
298
|
+
element.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
const theme = this.getTheme();
|
|
302
|
+
if (theme) {
|
|
303
|
+
element.setAttribute(THEME_DATA_ATTRIBUTE, theme);
|
|
304
|
+
}
|
|
305
|
+
const style = this.getStyle();
|
|
306
|
+
if (style) {
|
|
307
|
+
element.setAttribute('style', style);
|
|
308
|
+
}
|
|
309
|
+
return {
|
|
310
|
+
element
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
static importDOM() {
|
|
314
|
+
return {
|
|
315
|
+
// Typically <pre> is used for code blocks, and <code> for inline code styles
|
|
316
|
+
// but if it's a multi line <code> we'll create a block. Pass through to
|
|
317
|
+
// inline format handled by TextNode otherwise.
|
|
318
|
+
code: node => {
|
|
319
|
+
const isMultiLine = node.textContent != null && (/\r?\n/.test(node.textContent) || hasChildDOMNodeTag(node, 'BR'));
|
|
320
|
+
return isMultiLine ? {
|
|
321
|
+
conversion: $convertPreElement,
|
|
322
|
+
priority: 1
|
|
323
|
+
} : null;
|
|
324
|
+
},
|
|
325
|
+
div: () => ({
|
|
326
|
+
conversion: $convertDivElement,
|
|
327
|
+
priority: 1
|
|
328
|
+
}),
|
|
329
|
+
pre: () => ({
|
|
330
|
+
conversion: $convertPreElement,
|
|
331
|
+
priority: 0
|
|
332
|
+
}),
|
|
333
|
+
table: node => {
|
|
334
|
+
const table = node;
|
|
335
|
+
// domNode is a <table> since we matched it by nodeName
|
|
336
|
+
if (isGitHubCodeTable(table)) {
|
|
337
|
+
return {
|
|
338
|
+
conversion: $convertTableElement,
|
|
339
|
+
priority: 3
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
return null;
|
|
343
|
+
},
|
|
344
|
+
td: node => {
|
|
345
|
+
// element is a <td> since we matched it by nodeName
|
|
346
|
+
const td = node;
|
|
347
|
+
const table = td.closest('table');
|
|
348
|
+
if (isGitHubCodeCell(td) || table && isGitHubCodeTable(table)) {
|
|
349
|
+
// Return a no-op if it's a table cell in a code table, but not a code line.
|
|
350
|
+
// Otherwise it'll fall back to the T
|
|
351
|
+
return {
|
|
352
|
+
conversion: convertCodeNoop,
|
|
353
|
+
priority: 3
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
return null;
|
|
357
|
+
},
|
|
358
|
+
tr: node => {
|
|
359
|
+
// element is a <tr> since we matched it by nodeName
|
|
360
|
+
const tr = node;
|
|
361
|
+
const table = tr.closest('table');
|
|
362
|
+
if (table && isGitHubCodeTable(table)) {
|
|
363
|
+
return {
|
|
364
|
+
conversion: convertCodeNoop,
|
|
365
|
+
priority: 3
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
static importJSON(serializedNode) {
|
|
373
|
+
return $createCodeNode().updateFromJSON(serializedNode);
|
|
374
|
+
}
|
|
375
|
+
updateFromJSON(serializedNode) {
|
|
376
|
+
return super.updateFromJSON(serializedNode).setLanguage(serializedNode.language).setTheme(serializedNode.theme);
|
|
377
|
+
}
|
|
378
|
+
exportJSON() {
|
|
379
|
+
return {
|
|
380
|
+
...super.exportJSON(),
|
|
381
|
+
language: this.getLanguage(),
|
|
382
|
+
theme: this.getTheme()
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Mutation
|
|
387
|
+
insertNewAfter(selection, restoreSelection = true) {
|
|
388
|
+
const children = this.getChildren();
|
|
389
|
+
const childrenLength = children.length;
|
|
390
|
+
if (childrenLength >= 2 && children[childrenLength - 1].getTextContent() === '\n' && children[childrenLength - 2].getTextContent() === '\n' && selection.isCollapsed() && selection.anchor.key === this.__key && selection.anchor.offset === childrenLength) {
|
|
391
|
+
children[childrenLength - 1].remove();
|
|
392
|
+
children[childrenLength - 2].remove();
|
|
393
|
+
const newElement = lexical.$createParagraphNode();
|
|
394
|
+
this.insertAfter(newElement, restoreSelection);
|
|
395
|
+
return newElement;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// If the selection is within the codeblock, find all leading tabs and
|
|
399
|
+
// spaces of the current line. Create a new line that has all those
|
|
400
|
+
// tabs and spaces, such that leading indentation is preserved.
|
|
401
|
+
const {
|
|
402
|
+
anchor,
|
|
403
|
+
focus
|
|
404
|
+
} = selection;
|
|
405
|
+
const firstPoint = anchor.isBefore(focus) ? anchor : focus;
|
|
406
|
+
const firstSelectionNode = firstPoint.getNode();
|
|
407
|
+
if (lexical.$isTextNode(firstSelectionNode)) {
|
|
408
|
+
let node = $getFirstCodeNodeOfLine(firstSelectionNode);
|
|
409
|
+
const insertNodes = [];
|
|
410
|
+
// eslint-disable-next-line no-constant-condition
|
|
411
|
+
while (true) {
|
|
412
|
+
if (lexical.$isTabNode(node)) {
|
|
413
|
+
insertNodes.push(lexical.$createTabNode());
|
|
414
|
+
node = node.getNextSibling();
|
|
415
|
+
} else if ($isCodeHighlightNode(node)) {
|
|
416
|
+
let spaces = 0;
|
|
417
|
+
const text = node.getTextContent();
|
|
418
|
+
const textSize = node.getTextContentSize();
|
|
419
|
+
while (spaces < textSize && text[spaces] === ' ') {
|
|
420
|
+
spaces++;
|
|
421
|
+
}
|
|
422
|
+
if (spaces !== 0) {
|
|
423
|
+
insertNodes.push($createCodeHighlightNode(' '.repeat(spaces)));
|
|
424
|
+
}
|
|
425
|
+
if (spaces !== textSize) {
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
node = node.getNextSibling();
|
|
429
|
+
} else {
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
const split = firstSelectionNode.splitText(anchor.offset)[0];
|
|
434
|
+
const x = anchor.offset === 0 ? 0 : 1;
|
|
435
|
+
const index = split.getIndexWithinParent() + x;
|
|
436
|
+
const codeNode = firstSelectionNode.getParentOrThrow();
|
|
437
|
+
const nodesToInsert = [lexical.$createLineBreakNode(), ...insertNodes];
|
|
438
|
+
codeNode.splice(index, 0, nodesToInsert);
|
|
439
|
+
const last = insertNodes[insertNodes.length - 1];
|
|
440
|
+
if (last) {
|
|
441
|
+
last.select();
|
|
442
|
+
} else if (anchor.offset === 0) {
|
|
443
|
+
split.selectPrevious();
|
|
444
|
+
} else {
|
|
445
|
+
split.getNextSibling().selectNext(0, 0);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
if ($isCodeNode(firstSelectionNode)) {
|
|
449
|
+
const {
|
|
450
|
+
offset
|
|
451
|
+
} = selection.anchor;
|
|
452
|
+
firstSelectionNode.splice(offset, 0, [lexical.$createLineBreakNode()]);
|
|
453
|
+
firstSelectionNode.select(offset + 1, offset + 1);
|
|
454
|
+
}
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
canIndent() {
|
|
458
|
+
return false;
|
|
459
|
+
}
|
|
460
|
+
collapseAtStart() {
|
|
461
|
+
const paragraph = lexical.$createParagraphNode();
|
|
462
|
+
const children = this.getChildren();
|
|
463
|
+
children.forEach(child => paragraph.append(child));
|
|
464
|
+
this.replace(paragraph);
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
setLanguage(language) {
|
|
468
|
+
const writable = this.getWritable();
|
|
469
|
+
writable.__language = language || undefined;
|
|
470
|
+
return writable;
|
|
471
|
+
}
|
|
472
|
+
getLanguage() {
|
|
473
|
+
return this.getLatest().__language;
|
|
474
|
+
}
|
|
475
|
+
setIsSyntaxHighlightSupported(isSupported) {
|
|
476
|
+
const writable = this.getWritable();
|
|
477
|
+
writable.__isSyntaxHighlightSupported = isSupported;
|
|
478
|
+
return writable;
|
|
479
|
+
}
|
|
480
|
+
getIsSyntaxHighlightSupported() {
|
|
481
|
+
return this.getLatest().__isSyntaxHighlightSupported;
|
|
482
|
+
}
|
|
483
|
+
setTheme(theme) {
|
|
484
|
+
const writable = this.getWritable();
|
|
485
|
+
writable.__theme = theme || undefined;
|
|
486
|
+
return writable;
|
|
487
|
+
}
|
|
488
|
+
getTheme() {
|
|
489
|
+
return this.getLatest().__theme;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
function $createCodeNode(language, theme) {
|
|
493
|
+
return lexical.$create(CodeNode).setLanguage(language).setTheme(theme);
|
|
494
|
+
}
|
|
495
|
+
function $isCodeNode(node) {
|
|
496
|
+
return node instanceof CodeNode;
|
|
497
|
+
}
|
|
498
|
+
function $convertPreElement(domNode) {
|
|
499
|
+
const language = domNode.getAttribute(LANGUAGE_DATA_ATTRIBUTE);
|
|
500
|
+
return {
|
|
501
|
+
node: $createCodeNode(language)
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
function $convertDivElement(domNode) {
|
|
505
|
+
// domNode is a <div> since we matched it by nodeName
|
|
506
|
+
const div = domNode;
|
|
507
|
+
const isCode = isCodeElement(div);
|
|
508
|
+
if (!isCode && !isCodeChildElement(div)) {
|
|
509
|
+
return {
|
|
510
|
+
node: null
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
return {
|
|
514
|
+
node: isCode ? $createCodeNode() : null
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
function $convertTableElement() {
|
|
518
|
+
return {
|
|
519
|
+
node: $createCodeNode()
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
function convertCodeNoop() {
|
|
523
|
+
return {
|
|
524
|
+
node: null
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
function isCodeElement(div) {
|
|
528
|
+
return div.style.fontFamily.match('monospace') !== null;
|
|
529
|
+
}
|
|
530
|
+
function isCodeChildElement(node) {
|
|
531
|
+
let parent = node.parentElement;
|
|
532
|
+
while (parent !== null) {
|
|
533
|
+
if (isCodeElement(parent)) {
|
|
534
|
+
return true;
|
|
535
|
+
}
|
|
536
|
+
parent = parent.parentElement;
|
|
537
|
+
}
|
|
538
|
+
return false;
|
|
539
|
+
}
|
|
540
|
+
function isGitHubCodeCell(cell) {
|
|
541
|
+
return cell.classList.contains('js-file-line');
|
|
542
|
+
}
|
|
543
|
+
function isGitHubCodeTable(table) {
|
|
544
|
+
return table.classList.contains('js-file-line-container');
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
549
|
+
*
|
|
550
|
+
* This source code is licensed under the MIT license found in the
|
|
551
|
+
* LICENSE file in the root directory of this source tree.
|
|
552
|
+
*
|
|
553
|
+
*/
|
|
554
|
+
|
|
555
|
+
/** @noInheritDoc */
|
|
556
|
+
class CodeHighlightNode extends lexical.TextNode {
|
|
557
|
+
/** @internal */
|
|
558
|
+
__highlightType;
|
|
559
|
+
constructor(text = '', highlightType, key) {
|
|
560
|
+
super(text, key);
|
|
561
|
+
this.__highlightType = highlightType;
|
|
562
|
+
}
|
|
563
|
+
static getType() {
|
|
564
|
+
return 'code-highlight';
|
|
565
|
+
}
|
|
566
|
+
static clone(node) {
|
|
567
|
+
return new CodeHighlightNode(node.__text, node.__highlightType || undefined, node.__key);
|
|
568
|
+
}
|
|
569
|
+
getHighlightType() {
|
|
570
|
+
const self = this.getLatest();
|
|
571
|
+
return self.__highlightType;
|
|
572
|
+
}
|
|
573
|
+
setHighlightType(highlightType) {
|
|
574
|
+
const self = this.getWritable();
|
|
575
|
+
self.__highlightType = highlightType || undefined;
|
|
576
|
+
return self;
|
|
577
|
+
}
|
|
578
|
+
canHaveFormat() {
|
|
579
|
+
return false;
|
|
580
|
+
}
|
|
581
|
+
createDOM(config) {
|
|
582
|
+
const element = super.createDOM(config);
|
|
583
|
+
const className = getHighlightThemeClass(config.theme, this.__highlightType);
|
|
584
|
+
lexical.addClassNamesToElement(element, className);
|
|
585
|
+
return element;
|
|
586
|
+
}
|
|
587
|
+
updateDOM(prevNode, dom, config) {
|
|
588
|
+
const update = super.updateDOM(prevNode, dom, config);
|
|
589
|
+
const prevClassName = getHighlightThemeClass(config.theme, prevNode.__highlightType);
|
|
590
|
+
const nextClassName = getHighlightThemeClass(config.theme, this.__highlightType);
|
|
591
|
+
if (prevClassName !== nextClassName) {
|
|
592
|
+
if (prevClassName) {
|
|
593
|
+
lexical.removeClassNamesFromElement(dom, prevClassName);
|
|
594
|
+
}
|
|
595
|
+
if (nextClassName) {
|
|
596
|
+
lexical.addClassNamesToElement(dom, nextClassName);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return update;
|
|
600
|
+
}
|
|
601
|
+
static importJSON(serializedNode) {
|
|
602
|
+
return $createCodeHighlightNode().updateFromJSON(serializedNode);
|
|
603
|
+
}
|
|
604
|
+
updateFromJSON(serializedNode) {
|
|
605
|
+
return super.updateFromJSON(serializedNode).setHighlightType(serializedNode.highlightType);
|
|
606
|
+
}
|
|
607
|
+
exportJSON() {
|
|
608
|
+
return {
|
|
609
|
+
...super.exportJSON(),
|
|
610
|
+
highlightType: this.getHighlightType()
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// Prevent formatting (bold, underline, etc)
|
|
615
|
+
setFormat(format) {
|
|
616
|
+
return this;
|
|
617
|
+
}
|
|
618
|
+
isParentRequired() {
|
|
619
|
+
return true;
|
|
620
|
+
}
|
|
621
|
+
createParentElementNode() {
|
|
622
|
+
return $createCodeNode();
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
function getHighlightThemeClass(theme, highlightType) {
|
|
626
|
+
return highlightType && theme && theme.codeHighlight && theme.codeHighlight[highlightType];
|
|
627
|
+
}
|
|
628
|
+
function $createCodeHighlightNode(text = '', highlightType) {
|
|
629
|
+
return lexical.$applyNodeReplacement(new CodeHighlightNode(text, highlightType));
|
|
630
|
+
}
|
|
631
|
+
function $isCodeHighlightNode(node) {
|
|
632
|
+
return node instanceof CodeHighlightNode;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
637
|
+
*
|
|
638
|
+
* This source code is licensed under the MIT license found in the
|
|
639
|
+
* LICENSE file in the root directory of this source tree.
|
|
640
|
+
*
|
|
641
|
+
*/
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Add code blocks to the editor (syntax highlighting provided separately)
|
|
645
|
+
*/
|
|
646
|
+
const CodeExtension = lexical.defineExtension({
|
|
647
|
+
name: '@lexical/code',
|
|
648
|
+
nodes: () => [CodeNode, CodeHighlightNode]
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
exports.$createCodeHighlightNode = $createCodeHighlightNode;
|
|
652
|
+
exports.$createCodeNode = $createCodeNode;
|
|
653
|
+
exports.$getCodeLineDirection = $getCodeLineDirection;
|
|
654
|
+
exports.$getEndOfCodeInLine = $getEndOfCodeInLine;
|
|
655
|
+
exports.$getFirstCodeNodeOfLine = $getFirstCodeNodeOfLine;
|
|
656
|
+
exports.$getLastCodeNodeOfLine = $getLastCodeNodeOfLine;
|
|
657
|
+
exports.$getStartOfCodeInLine = $getStartOfCodeInLine;
|
|
658
|
+
exports.$isCodeHighlightNode = $isCodeHighlightNode;
|
|
659
|
+
exports.$isCodeNode = $isCodeNode;
|
|
660
|
+
exports.CodeExtension = CodeExtension;
|
|
661
|
+
exports.CodeHighlightNode = CodeHighlightNode;
|
|
662
|
+
exports.CodeNode = CodeNode;
|
|
663
|
+
exports.DEFAULT_CODE_LANGUAGE = DEFAULT_CODE_LANGUAGE;
|
|
664
|
+
exports.getDefaultCodeLanguage = getDefaultCodeLanguage;
|