@lexical/code 0.3.8 → 0.3.11
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/CodeHighlightNode.d.ts +48 -0
- package/CodeHighlighter.d.ts +29 -0
- package/CodeNode.d.ts +46 -0
- package/LexicalCode.dev.js +120 -99
- package/LexicalCode.prod.js +18 -19
- package/index.d.ts +3 -73
- package/package.json +3 -3
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
import { EditorConfig, LexicalNode, NodeKey, SerializedTextNode, Spread, TextNode } from 'lexical';
|
|
9
|
+
import 'prismjs/components/prism-clike';
|
|
10
|
+
import 'prismjs/components/prism-javascript';
|
|
11
|
+
import 'prismjs/components/prism-markup';
|
|
12
|
+
import 'prismjs/components/prism-markdown';
|
|
13
|
+
import 'prismjs/components/prism-c';
|
|
14
|
+
import 'prismjs/components/prism-css';
|
|
15
|
+
import 'prismjs/components/prism-objectivec';
|
|
16
|
+
import 'prismjs/components/prism-sql';
|
|
17
|
+
import 'prismjs/components/prism-python';
|
|
18
|
+
import 'prismjs/components/prism-rust';
|
|
19
|
+
import 'prismjs/components/prism-swift';
|
|
20
|
+
export declare const DEFAULT_CODE_LANGUAGE = "javascript";
|
|
21
|
+
declare type SerializedCodeHighlightNode = Spread<{
|
|
22
|
+
highlightType: string | null | undefined;
|
|
23
|
+
type: 'code-highlight';
|
|
24
|
+
version: 1;
|
|
25
|
+
}, SerializedTextNode>;
|
|
26
|
+
export declare const CODE_LANGUAGE_FRIENDLY_NAME_MAP: Record<string, string>;
|
|
27
|
+
export declare const CODE_LANGUAGE_MAP: Record<string, string>;
|
|
28
|
+
export declare function normalizeCodeLang(lang: string): string;
|
|
29
|
+
export declare function getLanguageFriendlyName(lang: string): string;
|
|
30
|
+
export declare const getDefaultCodeLanguage: () => string;
|
|
31
|
+
export declare const getCodeLanguages: () => Array<string>;
|
|
32
|
+
export declare class CodeHighlightNode extends TextNode {
|
|
33
|
+
__highlightType: string | null | undefined;
|
|
34
|
+
constructor(text: string, highlightType?: string | null | undefined, key?: NodeKey);
|
|
35
|
+
static getType(): string;
|
|
36
|
+
static clone(node: CodeHighlightNode): CodeHighlightNode;
|
|
37
|
+
getHighlightType(): string | null | undefined;
|
|
38
|
+
createDOM(config: EditorConfig): HTMLElement;
|
|
39
|
+
updateDOM(prevNode: CodeHighlightNode, dom: HTMLElement, config: EditorConfig): boolean;
|
|
40
|
+
static importJSON(serializedNode: SerializedCodeHighlightNode): CodeHighlightNode;
|
|
41
|
+
exportJSON(): SerializedCodeHighlightNode;
|
|
42
|
+
setFormat(format: number): this;
|
|
43
|
+
}
|
|
44
|
+
export declare function $createCodeHighlightNode(text: string, highlightType?: string | null | undefined): CodeHighlightNode;
|
|
45
|
+
export declare function $isCodeHighlightNode(node: LexicalNode | CodeHighlightNode | null | undefined): node is CodeHighlightNode;
|
|
46
|
+
export declare function getFirstCodeHighlightNodeOfLine(anchor: LexicalNode): CodeHighlightNode | null | undefined;
|
|
47
|
+
export declare function getLastCodeHighlightNodeOfLine(anchor: LexicalNode): CodeHighlightNode | null | undefined;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
import type { LexicalEditor, LexicalNode } from 'lexical';
|
|
9
|
+
import 'prismjs/components/prism-clike';
|
|
10
|
+
import 'prismjs/components/prism-javascript';
|
|
11
|
+
import 'prismjs/components/prism-markup';
|
|
12
|
+
import 'prismjs/components/prism-markdown';
|
|
13
|
+
import 'prismjs/components/prism-c';
|
|
14
|
+
import 'prismjs/components/prism-css';
|
|
15
|
+
import 'prismjs/components/prism-objectivec';
|
|
16
|
+
import 'prismjs/components/prism-sql';
|
|
17
|
+
import 'prismjs/components/prism-python';
|
|
18
|
+
import 'prismjs/components/prism-rust';
|
|
19
|
+
import 'prismjs/components/prism-swift';
|
|
20
|
+
import { TextNode } from 'lexical';
|
|
21
|
+
export declare function getStartOfCodeInLine(anchor: LexicalNode): {
|
|
22
|
+
node: TextNode | null;
|
|
23
|
+
offset: number;
|
|
24
|
+
};
|
|
25
|
+
export declare function getEndOfCodeInLine(anchor: LexicalNode): {
|
|
26
|
+
node: TextNode | null;
|
|
27
|
+
offset: number;
|
|
28
|
+
};
|
|
29
|
+
export declare function registerCodeHighlighting(editor: LexicalEditor): () => void;
|
package/CodeNode.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
import type { DOMConversionMap, EditorConfig, LexicalNode, NodeKey, ParagraphNode, RangeSelection, SerializedElementNode, Spread } from 'lexical';
|
|
9
|
+
import type { CodeHighlightNode } from '@lexical/code';
|
|
10
|
+
import 'prismjs/components/prism-clike';
|
|
11
|
+
import 'prismjs/components/prism-javascript';
|
|
12
|
+
import 'prismjs/components/prism-markup';
|
|
13
|
+
import 'prismjs/components/prism-markdown';
|
|
14
|
+
import 'prismjs/components/prism-c';
|
|
15
|
+
import 'prismjs/components/prism-css';
|
|
16
|
+
import 'prismjs/components/prism-objectivec';
|
|
17
|
+
import 'prismjs/components/prism-sql';
|
|
18
|
+
import 'prismjs/components/prism-python';
|
|
19
|
+
import 'prismjs/components/prism-rust';
|
|
20
|
+
import 'prismjs/components/prism-swift';
|
|
21
|
+
import { ElementNode } from 'lexical';
|
|
22
|
+
declare type SerializedCodeNode = Spread<{
|
|
23
|
+
language: string | null | undefined;
|
|
24
|
+
type: 'code';
|
|
25
|
+
version: 1;
|
|
26
|
+
}, SerializedElementNode>;
|
|
27
|
+
export declare class CodeNode extends ElementNode {
|
|
28
|
+
__language: string | null | undefined;
|
|
29
|
+
static getType(): string;
|
|
30
|
+
static clone(node: CodeNode): CodeNode;
|
|
31
|
+
constructor(language?: string | null | undefined, key?: NodeKey);
|
|
32
|
+
createDOM(config: EditorConfig): HTMLElement;
|
|
33
|
+
updateDOM(prevNode: CodeNode, dom: HTMLElement): boolean;
|
|
34
|
+
static importDOM(): DOMConversionMap | null;
|
|
35
|
+
static importJSON(serializedNode: SerializedCodeNode): CodeNode;
|
|
36
|
+
exportJSON(): SerializedCodeNode;
|
|
37
|
+
insertNewAfter(selection: RangeSelection): null | ParagraphNode | CodeHighlightNode;
|
|
38
|
+
canInsertTab(): boolean;
|
|
39
|
+
canIndent(): false;
|
|
40
|
+
collapseAtStart(): true;
|
|
41
|
+
setLanguage(language: string): void;
|
|
42
|
+
getLanguage(): string | null | undefined;
|
|
43
|
+
}
|
|
44
|
+
export declare function $createCodeNode(language?: string | null | undefined): CodeNode;
|
|
45
|
+
export declare function $isCodeNode(node: LexicalNode | null | undefined): node is CodeNode;
|
|
46
|
+
export {};
|
package/LexicalCode.dev.js
CHANGED
|
@@ -51,17 +51,14 @@ const CODE_LANGUAGE_MAP = {
|
|
|
51
51
|
python: 'py',
|
|
52
52
|
text: 'plain'
|
|
53
53
|
};
|
|
54
|
+
function normalizeCodeLang(lang) {
|
|
55
|
+
return CODE_LANGUAGE_MAP[lang] || lang;
|
|
56
|
+
}
|
|
54
57
|
function getLanguageFriendlyName(lang) {
|
|
55
|
-
const _lang =
|
|
58
|
+
const _lang = normalizeCodeLang(lang);
|
|
56
59
|
|
|
57
60
|
return CODE_LANGUAGE_FRIENDLY_NAME_MAP[_lang] || _lang;
|
|
58
61
|
}
|
|
59
|
-
|
|
60
|
-
const mapToPrismLanguage = language => {
|
|
61
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
62
|
-
return language != null && Prism.languages.hasOwnProperty(language) ? language : undefined;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
62
|
const getDefaultCodeLanguage = () => DEFAULT_CODE_LANGUAGE;
|
|
66
63
|
const getCodeLanguages = () => Object.keys(Prism.languages).filter( // Prism has several language helpers mixed into languages object
|
|
67
64
|
// so filtering them out here to get langs list
|
|
@@ -144,6 +141,58 @@ function $createCodeHighlightNode(text, highlightType) {
|
|
|
144
141
|
function $isCodeHighlightNode(node) {
|
|
145
142
|
return node instanceof CodeHighlightNode;
|
|
146
143
|
}
|
|
144
|
+
function getFirstCodeHighlightNodeOfLine(anchor) {
|
|
145
|
+
let currentNode = null;
|
|
146
|
+
const previousSiblings = anchor.getPreviousSiblings();
|
|
147
|
+
previousSiblings.push(anchor);
|
|
148
|
+
|
|
149
|
+
while (previousSiblings.length > 0) {
|
|
150
|
+
const node = previousSiblings.pop();
|
|
151
|
+
|
|
152
|
+
if ($isCodeHighlightNode(node)) {
|
|
153
|
+
currentNode = node;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (lexical.$isLineBreakNode(node)) {
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return currentNode;
|
|
162
|
+
}
|
|
163
|
+
function getLastCodeHighlightNodeOfLine(anchor) {
|
|
164
|
+
let currentNode = null;
|
|
165
|
+
const nextSiblings = anchor.getNextSiblings();
|
|
166
|
+
nextSiblings.unshift(anchor);
|
|
167
|
+
|
|
168
|
+
while (nextSiblings.length > 0) {
|
|
169
|
+
const node = nextSiblings.shift();
|
|
170
|
+
|
|
171
|
+
if ($isCodeHighlightNode(node)) {
|
|
172
|
+
currentNode = node;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (lexical.$isLineBreakNode(node)) {
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return currentNode;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
185
|
+
*
|
|
186
|
+
* This source code is licensed under the MIT license found in the
|
|
187
|
+
* LICENSE file in the root directory of this source tree.
|
|
188
|
+
*
|
|
189
|
+
*/
|
|
190
|
+
|
|
191
|
+
const mapToPrismLanguage = language => {
|
|
192
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
193
|
+
return language != null && Prism.languages.hasOwnProperty(language) ? language : undefined;
|
|
194
|
+
};
|
|
195
|
+
|
|
147
196
|
const LANGUAGE_DATA_ATTRIBUTE = 'data-highlight-language';
|
|
148
197
|
class CodeNode extends lexical.ElementNode {
|
|
149
198
|
static getType() {
|
|
@@ -354,45 +403,78 @@ function $createCodeNode(language) {
|
|
|
354
403
|
function $isCodeNode(node) {
|
|
355
404
|
return node instanceof CodeNode;
|
|
356
405
|
}
|
|
357
|
-
function getFirstCodeHighlightNodeOfLine(anchor) {
|
|
358
|
-
let currentNode = null;
|
|
359
|
-
const previousSiblings = anchor.getPreviousSiblings();
|
|
360
|
-
previousSiblings.push(anchor);
|
|
361
406
|
|
|
362
|
-
|
|
363
|
-
|
|
407
|
+
function convertPreElement(domNode) {
|
|
408
|
+
return {
|
|
409
|
+
node: $createCodeNode()
|
|
410
|
+
};
|
|
411
|
+
}
|
|
364
412
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
413
|
+
function convertDivElement(domNode) {
|
|
414
|
+
// domNode is a <div> since we matched it by nodeName
|
|
415
|
+
const div = domNode;
|
|
416
|
+
return {
|
|
417
|
+
after: childLexicalNodes => {
|
|
418
|
+
const domParent = domNode.parentNode;
|
|
368
419
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
}
|
|
420
|
+
if (domParent != null && domNode !== domParent.lastChild) {
|
|
421
|
+
childLexicalNodes.push(lexical.$createLineBreakNode());
|
|
422
|
+
}
|
|
373
423
|
|
|
374
|
-
|
|
424
|
+
return childLexicalNodes;
|
|
425
|
+
},
|
|
426
|
+
node: isCodeElement(div) ? $createCodeNode() : null
|
|
427
|
+
};
|
|
375
428
|
}
|
|
376
|
-
function getLastCodeHighlightNodeOfLine(anchor) {
|
|
377
|
-
let currentNode = null;
|
|
378
|
-
const nextSiblings = anchor.getNextSiblings();
|
|
379
|
-
nextSiblings.unshift(anchor);
|
|
380
429
|
|
|
381
|
-
|
|
382
|
-
|
|
430
|
+
function convertTableElement() {
|
|
431
|
+
return {
|
|
432
|
+
node: $createCodeNode()
|
|
433
|
+
};
|
|
434
|
+
}
|
|
383
435
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
436
|
+
function convertCodeNoop() {
|
|
437
|
+
return {
|
|
438
|
+
node: null
|
|
439
|
+
};
|
|
440
|
+
}
|
|
387
441
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
442
|
+
function convertTableCellElement(domNode) {
|
|
443
|
+
// domNode is a <td> since we matched it by nodeName
|
|
444
|
+
const cell = domNode;
|
|
445
|
+
return {
|
|
446
|
+
after: childLexicalNodes => {
|
|
447
|
+
if (cell.parentNode && cell.parentNode.nextSibling) {
|
|
448
|
+
// Append newline between code lines
|
|
449
|
+
childLexicalNodes.push(lexical.$createLineBreakNode());
|
|
450
|
+
}
|
|
392
451
|
|
|
393
|
-
|
|
452
|
+
return childLexicalNodes;
|
|
453
|
+
},
|
|
454
|
+
node: null
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function isCodeElement(div) {
|
|
459
|
+
return div.style.fontFamily.match('monospace') !== null;
|
|
394
460
|
}
|
|
395
461
|
|
|
462
|
+
function isGitHubCodeCell(cell) {
|
|
463
|
+
return cell.classList.contains('js-file-line');
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function isGitHubCodeTable(table) {
|
|
467
|
+
return table.classList.contains('js-file-line-container');
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
472
|
+
*
|
|
473
|
+
* This source code is licensed under the MIT license found in the
|
|
474
|
+
* LICENSE file in the root directory of this source tree.
|
|
475
|
+
*
|
|
476
|
+
*/
|
|
477
|
+
|
|
396
478
|
function isSpaceOrTabChar(char) {
|
|
397
479
|
return char === ' ' || char === '\t';
|
|
398
480
|
}
|
|
@@ -529,69 +611,6 @@ function getEndOfCodeInLine(anchor) {
|
|
|
529
611
|
};
|
|
530
612
|
}
|
|
531
613
|
|
|
532
|
-
function convertPreElement(domNode) {
|
|
533
|
-
return {
|
|
534
|
-
node: $createCodeNode()
|
|
535
|
-
};
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
function convertDivElement(domNode) {
|
|
539
|
-
// domNode is a <div> since we matched it by nodeName
|
|
540
|
-
const div = domNode;
|
|
541
|
-
return {
|
|
542
|
-
after: childLexicalNodes => {
|
|
543
|
-
const domParent = domNode.parentNode;
|
|
544
|
-
|
|
545
|
-
if (domParent != null && domNode !== domParent.lastChild) {
|
|
546
|
-
childLexicalNodes.push(lexical.$createLineBreakNode());
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
return childLexicalNodes;
|
|
550
|
-
},
|
|
551
|
-
node: isCodeElement(div) ? $createCodeNode() : null
|
|
552
|
-
};
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
function convertTableElement() {
|
|
556
|
-
return {
|
|
557
|
-
node: $createCodeNode()
|
|
558
|
-
};
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
function convertCodeNoop() {
|
|
562
|
-
return {
|
|
563
|
-
node: null
|
|
564
|
-
};
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
function convertTableCellElement(domNode) {
|
|
568
|
-
// domNode is a <td> since we matched it by nodeName
|
|
569
|
-
const cell = domNode;
|
|
570
|
-
return {
|
|
571
|
-
after: childLexicalNodes => {
|
|
572
|
-
if (cell.parentNode && cell.parentNode.nextSibling) {
|
|
573
|
-
// Append newline between code lines
|
|
574
|
-
childLexicalNodes.push(lexical.$createLineBreakNode());
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
return childLexicalNodes;
|
|
578
|
-
},
|
|
579
|
-
node: null
|
|
580
|
-
};
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
function isCodeElement(div) {
|
|
584
|
-
return div.style.fontFamily.match('monospace') !== null;
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
function isGitHubCodeCell(cell) {
|
|
588
|
-
return cell.classList.contains('js-file-line');
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
function isGitHubCodeTable(table) {
|
|
592
|
-
return table.classList.contains('js-file-line-container');
|
|
593
|
-
}
|
|
594
|
-
|
|
595
614
|
function textNodeTransform(node, editor) {
|
|
596
615
|
// Since CodeNode has flat children structure we only need to check
|
|
597
616
|
// if node's parent is a code node and run highlighting if so
|
|
@@ -637,7 +656,7 @@ function updateCodeGutter(node, editor) {
|
|
|
637
656
|
// will not affect code block content itself.
|
|
638
657
|
//
|
|
639
658
|
// Using extra flag (`isHighlighting`) since both CodeNode and CodeHighlightNode
|
|
640
|
-
//
|
|
659
|
+
// transforms might be called at the same time (e.g. new CodeHighlight node inserted) and
|
|
641
660
|
// in both cases we'll rerun whole reformatting over CodeNode, which is redundant.
|
|
642
661
|
// Especially when pasting code into CodeBlock.
|
|
643
662
|
|
|
@@ -1065,6 +1084,7 @@ exports.CODE_LANGUAGE_FRIENDLY_NAME_MAP = CODE_LANGUAGE_FRIENDLY_NAME_MAP;
|
|
|
1065
1084
|
exports.CODE_LANGUAGE_MAP = CODE_LANGUAGE_MAP;
|
|
1066
1085
|
exports.CodeHighlightNode = CodeHighlightNode;
|
|
1067
1086
|
exports.CodeNode = CodeNode;
|
|
1087
|
+
exports.DEFAULT_CODE_LANGUAGE = DEFAULT_CODE_LANGUAGE;
|
|
1068
1088
|
exports.getCodeLanguages = getCodeLanguages;
|
|
1069
1089
|
exports.getDefaultCodeLanguage = getDefaultCodeLanguage;
|
|
1070
1090
|
exports.getEndOfCodeInLine = getEndOfCodeInLine;
|
|
@@ -1072,4 +1092,5 @@ exports.getFirstCodeHighlightNodeOfLine = getFirstCodeHighlightNodeOfLine;
|
|
|
1072
1092
|
exports.getLanguageFriendlyName = getLanguageFriendlyName;
|
|
1073
1093
|
exports.getLastCodeHighlightNodeOfLine = getLastCodeHighlightNodeOfLine;
|
|
1074
1094
|
exports.getStartOfCodeInLine = getStartOfCodeInLine;
|
|
1095
|
+
exports.normalizeCodeLang = normalizeCodeLang;
|
|
1075
1096
|
exports.registerCodeHighlighting = registerCodeHighlighting;
|
package/LexicalCode.prod.js
CHANGED
|
@@ -5,29 +5,28 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
'use strict';var e=require("prismjs");require("prismjs/components/prism-clike");require("prismjs/components/prism-javascript");require("prismjs/components/prism-markup");require("prismjs/components/prism-markdown");require("prismjs/components/prism-c");require("prismjs/components/prism-css");require("prismjs/components/prism-objectivec");require("prismjs/components/prism-sql");require("prismjs/components/prism-python");require("prismjs/components/prism-rust");require("prismjs/components/prism-swift");
|
|
8
|
-
var m=require("@lexical/utils"),r=require("lexical");let t={c:"C",clike:"C-like",css:"CSS",html:"HTML",js:"JavaScript",markdown:"Markdown",objc:"Objective-C",plain:"Plain Text",py:"Python",rust:"Rust",sql:"SQL",swift:"Swift",xml:"XML"},u={javascript:"js",md:"markdown",plaintext:"plain",python:"py",text:"plain"}
|
|
8
|
+
var m=require("@lexical/utils"),r=require("lexical");let t={c:"C",clike:"C-like",css:"CSS",html:"HTML",js:"JavaScript",markdown:"Markdown",objc:"Objective-C",plain:"Plain Text",py:"Python",rust:"Rust",sql:"SQL",swift:"Swift",xml:"XML"},u={javascript:"js",md:"markdown",plaintext:"plain",python:"py",text:"plain"};function v(a){return u[a]||a}
|
|
9
9
|
class y extends r.TextNode{constructor(a,b,c){super(a,c);this.__highlightType=b}static getType(){return"code-highlight"}static clone(a){return new y(a.__text,a.__highlightType||void 0,a.__key)}getHighlightType(){return this.getLatest().__highlightType}createDOM(a){let b=super.createDOM(a);a=z(a.theme,this.__highlightType);m.addClassNamesToElement(b,a);return b}updateDOM(a,b,c){let d=super.updateDOM(a,b,c);a=z(c.theme,a.__highlightType);c=z(c.theme,this.__highlightType);a!==c&&(a&&m.removeClassNamesFromElement(b,
|
|
10
10
|
a),c&&m.addClassNamesToElement(b,c));return d}static importJSON(a){let b=A(a.text,a.highlightType);b.setFormat(a.format);b.setDetail(a.detail);b.setMode(a.mode);b.setStyle(a.style);return b}exportJSON(){return{...super.exportJSON(),highlightType:this.getHighlightType(),type:"code-highlight",version:1}}setFormat(){return this}}function z(a,b){return b&&a&&a.codeHighlight&&a.codeHighlight[b]}function A(a,b){return new y(a,b)}function C(a){return a instanceof y}
|
|
11
|
-
|
|
12
|
-
return
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
function
|
|
11
|
+
function D(a){let b=null,c=a.getPreviousSiblings();for(c.push(a);0<c.length&&(a=c.pop(),C(a)&&(b=a),!r.$isLineBreakNode(a)););return b}function E(a){let b=null,c=a.getNextSiblings();for(c.unshift(a);0<c.length&&(a=c.shift(),C(a)&&(b=a),!r.$isLineBreakNode(a)););return b}let F=a=>null!=a&&e.languages.hasOwnProperty(a)?a:void 0;
|
|
12
|
+
class G extends r.ElementNode{static getType(){return"code"}static clone(a){return new G(a.__language,a.__key)}constructor(a,b){super(b);this.__language=F(a)}createDOM(a){let b=document.createElement("code");m.addClassNamesToElement(b,a.theme.code);b.setAttribute("spellcheck","false");(a=this.getLanguage())&&b.setAttribute("data-highlight-language",a);return b}updateDOM(a,b){let c=this.__language;a=a.__language;c?c!==a&&b.setAttribute("data-highlight-language",c):a&&b.removeAttribute("data-highlight-language");
|
|
13
|
+
return!1}static importDOM(){return{code:a=>null!=a.textContent&&/\r?\n/.test(a.textContent)?{conversion:H,priority:1}:null,div:()=>({conversion:I,priority:1}),pre:()=>({conversion:H,priority:0}),table:a=>J(a)?{conversion:aa,priority:4}:null,td:a=>{let b=a.closest("table");return a.classList.contains("js-file-line")?{conversion:ba,priority:4}:b&&J(b)?{conversion:K,priority:4}:null},tr:a=>(a=a.closest("table"))&&J(a)?{conversion:K,priority:4}:null}}static importJSON(a){let b=L(a.language);b.setFormat(a.format);
|
|
14
|
+
b.setIndent(a.indent);b.setDirection(a.direction);return b}exportJSON(){return{...super.exportJSON(),language:this.getLanguage(),type:"code",version:1}}insertNewAfter(a){var b=this.getChildren(),c=b.length;if(2<=c&&"\n"===b[c-1].getTextContent()&&"\n"===b[c-2].getTextContent()&&a.isCollapsed()&&a.anchor.key===this.__key&&a.anchor.offset===c)return b[c-1].remove(),b[c-2].remove(),a=r.$createParagraphNode(),this.insertAfter(a),a;b=a.anchor.getNode();var d=D(b);if(null!=d){c=0;for(d=d.getTextContent();c<
|
|
15
|
+
d.length&&/[\t ]/.test(d[c]);)c+=1;if(0<c)return c=d.substring(0,c),c=A(c),b.insertAfter(c),a.insertNodes([r.$createLineBreakNode()]),c.select(),c}return null}canInsertTab(){let a=r.$getSelection();return r.$isRangeSelection(a)&&a.isCollapsed()?!0:!1}canIndent(){return!1}collapseAtStart(){let a=r.$createParagraphNode();this.getChildren().forEach(b=>a.append(b));this.replace(a);return!0}setLanguage(a){this.getWritable().__language=F(a)}getLanguage(){return this.getLatest().__language}}
|
|
16
|
+
function L(a){return new G(a)}function M(a){return a instanceof G}function H(){return{node:L()}}function I(a){return{after:b=>{let c=a.parentNode;null!=c&&a!==c.lastChild&&b.push(r.$createLineBreakNode());return b},node:null!==a.style.fontFamily.match("monospace")?L():null}}function aa(){return{node:L()}}function K(){return{node:null}}function ba(a){return{after:b=>{a.parentNode&&a.parentNode.nextSibling&&b.push(r.$createLineBreakNode());return b},node:null}}
|
|
17
|
+
function J(a){return a.classList.contains("js-file-line-container")}function N(a,b){var c=a.length;let d=-1;if(b)for(b=0;b<c;b++){let f=a[b];if(" "!==f&&"\t"!==f){d=b;break}}else for(--c;-1<c;c--)if(b=a[c]," "!==b&&"\t"!==b){d=c;break}return d}
|
|
17
18
|
function O(a){let b=null,c=-1;var d=a.getPreviousSiblings();for(d.push(a);0<d.length;){var f=d.pop();if(C(f)){var g=f.getTextContent();g=N(g,!0);-1!==g&&(b=f,c=g)}if(r.$isLineBreakNode(f))break}if(null===b)for(a=a.getNextSiblings();0<a.length;){d=a.shift();if(C(d)&&(f=d.getTextContent(),f=N(f,!0),-1!==f)){b=d;c=f;break}if(r.$isLineBreakNode(d))break}return{node:b,offset:c}}
|
|
18
|
-
function P(a){let b=null,c=-1;var d=a.getNextSiblings();for(d.unshift(a);0<d.length;){var f=d.shift();if(C(f)){var g=f.getTextContent();g=N(g,!1);-1!==g&&(b=f,c=g+1)}if(r.$isLineBreakNode(f))break}if(null===b)for(a=a.getPreviousSiblings();0<a.length;){d=a.pop();if(C(d)&&(f=d.getTextContent(),f=N(f,!1),-1!==f)){b=d;c=f+1;break}if(r.$isLineBreakNode(d))break}return{node:b,offset:c}}function
|
|
19
|
-
function
|
|
20
|
-
function Q(a,b){let c=a.getParent();L(c)?R(c,b):C(a)&&a.replace(r.$createTextNode(a.__text))}let S=!1;
|
|
21
|
-
function R(a,b){S||(S=!0,void 0===a.getLanguage()&&a.setLanguage("javascript"),b.update(()=>{ba(a,()=>{var c=a.getTextContent();c=e.tokenize(c,e.languages[a.getLanguage()||""]||e.languages.javascript);c=T(c);var d=a.getChildren();let f=0;for(;f<d.length&&U(d[f],c[f]);)f++;var g=d.length;let l=c.length,h=Math.min(g,l)-f,k=0;for(;k<h;)if(k++,!U(d[g-k],c[l-k])){k--;break}d=f;g-=k;c=c.slice(f,l-k);let {from:n,to:p,nodesForReplacement:w}={from:d,nodesForReplacement:c,to:g};return n!==p||w.length?(a.splice(n,
|
|
19
|
+
function P(a){let b=null,c=-1;var d=a.getNextSiblings();for(d.unshift(a);0<d.length;){var f=d.shift();if(C(f)){var g=f.getTextContent();g=N(g,!1);-1!==g&&(b=f,c=g+1)}if(r.$isLineBreakNode(f))break}if(null===b)for(a=a.getPreviousSiblings();0<a.length;){d=a.pop();if(C(d)&&(f=d.getTextContent(),f=N(f,!1),-1!==f)){b=d;c=f+1;break}if(r.$isLineBreakNode(d))break}return{node:b,offset:c}}function Q(a,b){let c=a.getParent();M(c)?R(c,b):C(a)&&a.replace(r.$createTextNode(a.__text))}let S=!1;
|
|
20
|
+
function R(a,b){S||(S=!0,void 0===a.getLanguage()&&a.setLanguage("javascript"),b.update(()=>{ca(a,()=>{var c=a.getTextContent();c=e.tokenize(c,e.languages[a.getLanguage()||""]||e.languages.javascript);c=T(c);var d=a.getChildren();let f=0;for(;f<d.length&&U(d[f],c[f]);)f++;var g=d.length;let l=c.length,h=Math.min(g,l)-f,k=0;for(;k<h;)if(k++,!U(d[g-k],c[l-k])){k--;break}d=f;g-=k;c=c.slice(f,l-k);let {from:n,to:p,nodesForReplacement:w}={from:d,nodesForReplacement:c,to:g};return n!==p||w.length?(a.splice(n,
|
|
22
21
|
p-n,w),!0):!1})},{onUpdate:()=>{S=!1},skipTransforms:!0}))}function T(a){let b=[];a.forEach(c=>{if("string"===typeof c){c=c.split("\n");for(var d=0;d<c.length;d++){let f=c[d];f.length&&b.push(A(f));d<c.length-1&&b.push(r.$createLineBreakNode())}}else({content:d}=c),"string"===typeof d?b.push(A(d,c.type)):Array.isArray(d)&&1===d.length&&"string"===typeof d[0]?b.push(A(d[0],c.type)):Array.isArray(d)&&b.push(...T(d))});return b}
|
|
23
|
-
function
|
|
24
|
-
function U(a,b){return C(a)&&C(b)?a.__text===b.__text&&a.__highlightType===b.__highlightType:r.$isLineBreakNode(a)&&r.$isLineBreakNode(b)?!0:!1}function V(a){var b=r.$getSelection();if(!r.$isRangeSelection(b)||b.isCollapsed())return!1;b=b.getNodes();for(var c=0;c<b.length;c++){var d=b[c];if(!C(d)&&!r.$isLineBreakNode(d))return!1}c=
|
|
25
|
-
function
|
|
22
|
+
function ca(a,b){var c=r.$getSelection();if(r.$isRangeSelection(c)&&c.anchor){c=c.anchor;var d=c.offset,f="element"===c.type&&r.$isLineBreakNode(a.getChildAtIndex(c.offset-1)),g=0;if(!f){let l=c.getNode();g=d+l.getPreviousSiblings().reduce((h,k)=>h+(r.$isLineBreakNode(k)?0:k.getTextContentSize()),0)}b()&&(f?c.getNode().select(d,d):a.getChildren().some(l=>{if(r.$isTextNode(l)){let h=l.getTextContentSize();if(h>=g)return l.select(g,g),!0;g-=h}return!1}))}}
|
|
23
|
+
function U(a,b){return C(a)&&C(b)?a.__text===b.__text&&a.__highlightType===b.__highlightType:r.$isLineBreakNode(a)&&r.$isLineBreakNode(b)?!0:!1}function V(a){var b=r.$getSelection();if(!r.$isRangeSelection(b)||b.isCollapsed())return!1;b=b.getNodes();for(var c=0;c<b.length;c++){var d=b[c];if(!C(d)&&!r.$isLineBreakNode(d))return!1}c=D(b[0]);null!=c&&W(c,a);for(c=1;c<b.length;c++)d=b[c],r.$isLineBreakNode(b[c-1])&&C(d)&&W(d,a);return!0}
|
|
24
|
+
function W(a,b){let c=a.getTextContent();b===r.INDENT_CONTENT_COMMAND?0<c.length&&/\s/.test(c[0])?a.setTextContent("\t"+c):(b=A("\t"),a.insertBefore(b)):0===c.indexOf("\t")&&(1===c.length?a.remove():a.setTextContent(c.substring(1)))}
|
|
26
25
|
function Y(a,b){let c=r.$getSelection();if(!r.$isRangeSelection(c))return!1;let {anchor:d,focus:f}=c,g=d.offset,l=f.offset,h=d.getNode(),k=f.getNode();var n=a===r.KEY_ARROW_UP_COMMAND;if(!C(h)||!C(k))return!1;if(!b.altKey){if(c.isCollapsed())if(a=h.getParentOrThrow(),n&&0===g&&null===h.getPreviousSibling()){if(null===a.getPreviousSibling())return a.selectPrevious(),b.preventDefault(),!0}else if(!n&&g===h.getTextContentSize()&&null===h.getNextSibling()&&null===a.getNextSibling())return a.selectNext(),
|
|
27
|
-
b.preventDefault(),!0;return!1}var p=
|
|
26
|
+
b.preventDefault(),!0;return!1}var p=D(h);let w=E(k);if(null==p||null==w)return!1;let B=p.getNodesBetween(w);for(let q=0;q<B.length;q++){let X=B[q];if(!C(X)&&!r.$isLineBreakNode(X))return!1}b.preventDefault();b.stopPropagation();b=n?p.getPreviousSibling():w.getNextSibling();if(!r.$isLineBreakNode(b))return!0;p=n?b.getPreviousSibling():b.getNextSibling();if(null==p)return!0;n=n?D(p):E(p);let x=null!=n?n:p;b.remove();B.forEach(q=>q.remove());a===r.KEY_ARROW_UP_COMMAND?(B.forEach(q=>x.insertBefore(q)),
|
|
28
27
|
x.insertBefore(b)):(x.insertAfter(b),x=b,B.forEach(q=>{x.insertAfter(q);x=q}));c.setTextNodeRange(h,g,k,l);return!0}function Z(a,b){let c=r.$getSelection();if(!r.$isRangeSelection(c))return!1;let {anchor:d,focus:f}=c,g=d.getNode(),l=f.getNode();a=a===r.MOVE_TO_START;if(!C(g)||!C(l))return!1;let h,k;a?{node:h,offset:k}=O(l):{node:h,offset:k}=P(l);null!==h&&-1!==k&&c.setTextNodeRange(h,k,h,k);b.preventDefault();b.stopPropagation();return!0}exports.$createCodeHighlightNode=A;
|
|
29
|
-
exports.$createCodeNode=
|
|
30
|
-
exports.getLastCodeHighlightNodeOfLine=
|
|
31
|
-
exports.registerCodeHighlighting=function(a){if(!a.hasNodes([
|
|
32
|
-
(h+="\n"+ ++k);c.setAttribute("data-gutter",h)}}})}),a.registerNodeTransform(
|
|
28
|
+
exports.$createCodeNode=L;exports.$isCodeHighlightNode=C;exports.$isCodeNode=M;exports.CODE_LANGUAGE_FRIENDLY_NAME_MAP=t;exports.CODE_LANGUAGE_MAP=u;exports.CodeHighlightNode=y;exports.CodeNode=G;exports.DEFAULT_CODE_LANGUAGE="javascript";exports.getCodeLanguages=()=>Object.keys(e.languages).filter(a=>"function"!==typeof e.languages[a]).sort();exports.getDefaultCodeLanguage=()=>"javascript";exports.getEndOfCodeInLine=P;exports.getFirstCodeHighlightNodeOfLine=D;
|
|
29
|
+
exports.getLanguageFriendlyName=function(a){a=v(a);return t[a]||a};exports.getLastCodeHighlightNodeOfLine=E;exports.getStartOfCodeInLine=O;exports.normalizeCodeLang=v;
|
|
30
|
+
exports.registerCodeHighlighting=function(a){if(!a.hasNodes([G,y]))throw Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");return m.mergeRegister(a.registerMutationListener(G,b=>{a.update(()=>{for(let [f,g]of b)if("destroyed"!==g){var c=r.$getNodeByKey(f);if(null!==c)a:{var d=c;c=a.getElementByKey(d.getKey());if(null===c)break a;d=d.getChildren();let l=d.length;if(l===c.__cachedChildrenLength)break a;c.__cachedChildrenLength=l;let h="1",k=1;for(let n=0;n<l;n++)r.$isLineBreakNode(d[n])&&
|
|
31
|
+
(h+="\n"+ ++k);c.setAttribute("data-gutter",h)}}})}),a.registerNodeTransform(G,b=>R(b,a)),a.registerNodeTransform(r.TextNode,b=>Q(b,a)),a.registerNodeTransform(y,b=>Q(b,a)),a.registerCommand(r.INDENT_CONTENT_COMMAND,()=>V(r.INDENT_CONTENT_COMMAND),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.OUTDENT_CONTENT_COMMAND,()=>V(r.OUTDENT_CONTENT_COMMAND),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.KEY_ARROW_UP_COMMAND,b=>Y(r.KEY_ARROW_UP_COMMAND,b),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.KEY_ARROW_DOWN_COMMAND,
|
|
33
32
|
b=>Y(r.KEY_ARROW_DOWN_COMMAND,b),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.MOVE_TO_END,b=>Z(r.MOVE_TO_END,b),r.COMMAND_PRIORITY_LOW),a.registerCommand(r.MOVE_TO_START,b=>Z(r.MOVE_TO_START,b),r.COMMAND_PRIORITY_LOW))}
|
package/index.d.ts
CHANGED
|
@@ -5,76 +5,6 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import 'prismjs/components/prism-markup';
|
|
12
|
-
import 'prismjs/components/prism-markdown';
|
|
13
|
-
import 'prismjs/components/prism-c';
|
|
14
|
-
import 'prismjs/components/prism-css';
|
|
15
|
-
import 'prismjs/components/prism-objectivec';
|
|
16
|
-
import 'prismjs/components/prism-sql';
|
|
17
|
-
import 'prismjs/components/prism-python';
|
|
18
|
-
import 'prismjs/components/prism-rust';
|
|
19
|
-
import 'prismjs/components/prism-swift';
|
|
20
|
-
import { ElementNode, TextNode } from 'lexical';
|
|
21
|
-
declare type SerializedCodeNode = Spread<{
|
|
22
|
-
language: string | null | undefined;
|
|
23
|
-
type: 'code';
|
|
24
|
-
version: 1;
|
|
25
|
-
}, SerializedElementNode>;
|
|
26
|
-
declare type SerializedCodeHighlightNode = Spread<{
|
|
27
|
-
highlightType: string | null | undefined;
|
|
28
|
-
type: 'code-highlight';
|
|
29
|
-
version: 1;
|
|
30
|
-
}, SerializedTextNode>;
|
|
31
|
-
export declare const CODE_LANGUAGE_FRIENDLY_NAME_MAP: Record<string, string>;
|
|
32
|
-
export declare const CODE_LANGUAGE_MAP: Record<string, string>;
|
|
33
|
-
export declare function getLanguageFriendlyName(lang: string): string;
|
|
34
|
-
export declare const getDefaultCodeLanguage: () => string;
|
|
35
|
-
export declare const getCodeLanguages: () => Array<string>;
|
|
36
|
-
export declare class CodeHighlightNode extends TextNode {
|
|
37
|
-
__highlightType: string | null | undefined;
|
|
38
|
-
constructor(text: string, highlightType?: string | null | undefined, key?: NodeKey);
|
|
39
|
-
static getType(): string;
|
|
40
|
-
static clone(node: CodeHighlightNode): CodeHighlightNode;
|
|
41
|
-
getHighlightType(): string | null | undefined;
|
|
42
|
-
createDOM(config: EditorConfig): HTMLElement;
|
|
43
|
-
updateDOM(prevNode: CodeHighlightNode, dom: HTMLElement, config: EditorConfig): boolean;
|
|
44
|
-
static importJSON(serializedNode: SerializedCodeHighlightNode): CodeHighlightNode;
|
|
45
|
-
exportJSON(): SerializedCodeHighlightNode;
|
|
46
|
-
setFormat(format: number): this;
|
|
47
|
-
}
|
|
48
|
-
export declare function $createCodeHighlightNode(text: string, highlightType?: string | null | undefined): CodeHighlightNode;
|
|
49
|
-
export declare function $isCodeHighlightNode(node: LexicalNode | CodeHighlightNode | null | undefined): node is CodeHighlightNode;
|
|
50
|
-
export declare class CodeNode extends ElementNode {
|
|
51
|
-
__language: string | null | undefined;
|
|
52
|
-
static getType(): string;
|
|
53
|
-
static clone(node: CodeNode): CodeNode;
|
|
54
|
-
constructor(language?: string | null | undefined, key?: NodeKey);
|
|
55
|
-
createDOM(config: EditorConfig): HTMLElement;
|
|
56
|
-
updateDOM(prevNode: CodeNode, dom: HTMLElement): boolean;
|
|
57
|
-
static importDOM(): DOMConversionMap | null;
|
|
58
|
-
static importJSON(serializedNode: SerializedCodeNode): CodeNode;
|
|
59
|
-
exportJSON(): SerializedCodeNode;
|
|
60
|
-
insertNewAfter(selection: RangeSelection): null | ParagraphNode | CodeHighlightNode;
|
|
61
|
-
canInsertTab(): boolean;
|
|
62
|
-
canIndent(): false;
|
|
63
|
-
collapseAtStart(): true;
|
|
64
|
-
setLanguage(language: string): void;
|
|
65
|
-
getLanguage(): string | null | undefined;
|
|
66
|
-
}
|
|
67
|
-
export declare function $createCodeNode(language?: string | null | undefined): CodeNode;
|
|
68
|
-
export declare function $isCodeNode(node: LexicalNode | null | undefined): node is CodeNode;
|
|
69
|
-
export declare function getFirstCodeHighlightNodeOfLine(anchor: LexicalNode): CodeHighlightNode | null | undefined;
|
|
70
|
-
export declare function getLastCodeHighlightNodeOfLine(anchor: LexicalNode): CodeHighlightNode | null | undefined;
|
|
71
|
-
export declare function getStartOfCodeInLine(anchor: LexicalNode): {
|
|
72
|
-
node: TextNode | null;
|
|
73
|
-
offset: number;
|
|
74
|
-
};
|
|
75
|
-
export declare function getEndOfCodeInLine(anchor: LexicalNode): {
|
|
76
|
-
node: TextNode | null;
|
|
77
|
-
offset: number;
|
|
78
|
-
};
|
|
79
|
-
export declare function registerCodeHighlighting(editor: LexicalEditor): () => void;
|
|
80
|
-
export {};
|
|
8
|
+
export { getEndOfCodeInLine, getStartOfCodeInLine, registerCodeHighlighting, } from './CodeHighlighter';
|
|
9
|
+
export { $createCodeHighlightNode, $isCodeHighlightNode, CODE_LANGUAGE_FRIENDLY_NAME_MAP, CODE_LANGUAGE_MAP, CodeHighlightNode, DEFAULT_CODE_LANGUAGE, getCodeLanguages, getDefaultCodeLanguage, getFirstCodeHighlightNodeOfLine, getLanguageFriendlyName, getLastCodeHighlightNodeOfLine, normalizeCodeLang, } from './CodeHighlightNode';
|
|
10
|
+
export { $createCodeNode, $isCodeNode, CodeNode } from './CodeNode';
|
package/package.json
CHANGED
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
"code"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.3.
|
|
11
|
+
"version": "0.3.11",
|
|
12
12
|
"main": "LexicalCode.js",
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"lexical": "0.3.
|
|
14
|
+
"lexical": "0.3.11"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/utils": "0.3.
|
|
17
|
+
"@lexical/utils": "0.3.11",
|
|
18
18
|
"prismjs": "^1.27.0"
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|