@lexical/code 0.14.2 → 0.14.4
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 +1 -14
- package/CodeHighlighter.d.ts +1 -14
- package/CodeHighlighterPrism.d.ts +22 -0
- package/CodeNode.d.ts +2 -15
- package/LexicalCode.dev.js +39 -5
- package/{LexicalCode.dev.esm.js → LexicalCode.dev.mjs} +55 -21
- package/{LexicalCode.esm.js → LexicalCode.mjs} +2 -2
- package/LexicalCode.node.mjs +26 -0
- package/LexicalCode.prod.js +35 -35
- package/LexicalCode.prod.mjs +7 -0
- package/index.d.ts +0 -2
- package/package.json +23 -7
- package/LexicalCode.prod.esm.js +0 -7
package/CodeHighlightNode.d.ts
CHANGED
|
@@ -6,20 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
import type { EditorConfig, LexicalNode, LineBreakNode, NodeKey, SerializedTextNode, Spread, TabNode } from 'lexical';
|
|
9
|
-
import '
|
|
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 'prismjs/components/prism-typescript';
|
|
21
|
-
import 'prismjs/components/prism-java';
|
|
22
|
-
import 'prismjs/components/prism-cpp';
|
|
9
|
+
import './CodeHighlighterPrism';
|
|
23
10
|
import { ElementNode, TextNode } from 'lexical';
|
|
24
11
|
export declare const DEFAULT_CODE_LANGUAGE = "javascript";
|
|
25
12
|
type SerializedCodeHighlightNode = Spread<{
|
package/CodeHighlighter.d.ts
CHANGED
|
@@ -6,20 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
import type { LexicalEditor, LineBreakNode } from 'lexical';
|
|
9
|
-
import '
|
|
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 'prismjs/components/prism-typescript';
|
|
21
|
-
import 'prismjs/components/prism-java';
|
|
22
|
-
import 'prismjs/components/prism-cpp';
|
|
9
|
+
import './CodeHighlighterPrism';
|
|
23
10
|
import { TabNode } from 'lexical';
|
|
24
11
|
import { CodeHighlightNode } from './CodeHighlightNode';
|
|
25
12
|
type TokenContent = string | Token | (string | Token)[];
|
|
@@ -0,0 +1,22 @@
|
|
|
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 'prismjs';
|
|
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 'prismjs/components/prism-typescript';
|
|
21
|
+
import 'prismjs/components/prism-java';
|
|
22
|
+
import 'prismjs/components/prism-cpp';
|
package/CodeNode.d.ts
CHANGED
|
@@ -5,22 +5,9 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
|
-
import type { DOMConversionMap, DOMExportOutput, EditorConfig, LexicalEditor, LexicalNode, NodeKey, ParagraphNode, RangeSelection, SerializedElementNode, Spread, TabNode } from 'lexical';
|
|
9
8
|
import type { CodeHighlightNode } from '@lexical/code';
|
|
10
|
-
import '
|
|
11
|
-
import '
|
|
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 'prismjs/components/prism-typescript';
|
|
22
|
-
import 'prismjs/components/prism-java';
|
|
23
|
-
import 'prismjs/components/prism-cpp';
|
|
9
|
+
import type { DOMConversionMap, DOMExportOutput, EditorConfig, LexicalEditor, LexicalNode, NodeKey, ParagraphNode, RangeSelection, SerializedElementNode, Spread, TabNode } from 'lexical';
|
|
10
|
+
import './CodeHighlighterPrism';
|
|
24
11
|
import { ElementNode } from 'lexical';
|
|
25
12
|
export type SerializedCodeNode = Spread<{
|
|
26
13
|
language: string | null | undefined;
|
package/LexicalCode.dev.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
require('prismjs');
|
|
10
10
|
require('prismjs/components/prism-clike');
|
|
11
11
|
require('prismjs/components/prism-javascript');
|
|
12
12
|
require('prismjs/components/prism-markup');
|
|
@@ -24,6 +24,32 @@ require('prismjs/components/prism-cpp');
|
|
|
24
24
|
var utils = require('@lexical/utils');
|
|
25
25
|
var lexical = require('lexical');
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
29
|
+
*
|
|
30
|
+
* This source code is licensed under the MIT license found in the
|
|
31
|
+
* LICENSE file in the root directory of this source tree.
|
|
32
|
+
*
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
37
|
+
*
|
|
38
|
+
* This source code is licensed under the MIT license found in the
|
|
39
|
+
* LICENSE file in the root directory of this source tree.
|
|
40
|
+
*
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
// invariant(condition, message) will refine types based on "condition", and
|
|
44
|
+
// if "condition" is false will throw an error. This function is special-cased
|
|
45
|
+
// in flow itself, so we can't name it anything else.
|
|
46
|
+
function invariant(cond, message, ...args) {
|
|
47
|
+
if (cond) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
throw new Error('Internal Lexical error: invariant() is meant to be replaced at compile ' + 'time. There is no runtime version. Error: ' + message);
|
|
51
|
+
}
|
|
52
|
+
|
|
27
53
|
/**
|
|
28
54
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
29
55
|
*
|
|
@@ -33,7 +59,7 @@ var lexical = require('lexical');
|
|
|
33
59
|
*/
|
|
34
60
|
const mapToPrismLanguage = language => {
|
|
35
61
|
// eslint-disable-next-line no-prototype-builtins
|
|
36
|
-
return language != null && Prism.languages.hasOwnProperty(language) ? language : undefined;
|
|
62
|
+
return language != null && window.Prism.languages.hasOwnProperty(language) ? language : undefined;
|
|
37
63
|
};
|
|
38
64
|
function hasChildDOMNodeTag(node, tagName) {
|
|
39
65
|
for (const child of node.childNodes) {
|
|
@@ -389,10 +415,10 @@ function getLanguageFriendlyName(lang) {
|
|
|
389
415
|
return CODE_LANGUAGE_FRIENDLY_NAME_MAP[_lang] || _lang;
|
|
390
416
|
}
|
|
391
417
|
const getDefaultCodeLanguage = () => DEFAULT_CODE_LANGUAGE;
|
|
392
|
-
const getCodeLanguages = () => Object.keys(Prism.languages).filter(
|
|
418
|
+
const getCodeLanguages = () => Object.keys(window.Prism.languages).filter(
|
|
393
419
|
// Prism has several language helpers mixed into languages object
|
|
394
420
|
// so filtering them out here to get langs list
|
|
395
|
-
language => typeof Prism.languages[language] !== 'function').sort();
|
|
421
|
+
language => typeof window.Prism.languages[language] !== 'function').sort();
|
|
396
422
|
|
|
397
423
|
/** @noInheritDoc */
|
|
398
424
|
class CodeHighlightNode extends lexical.TextNode {
|
|
@@ -501,7 +527,7 @@ function getLastCodeNodeOfLine(anchor) {
|
|
|
501
527
|
const PrismTokenizer = {
|
|
502
528
|
defaultLanguage: DEFAULT_CODE_LANGUAGE,
|
|
503
529
|
tokenize(code, language) {
|
|
504
|
-
return Prism.tokenize(code, Prism.languages[language || ''] || Prism.languages[this.defaultLanguage]);
|
|
530
|
+
return window.Prism.tokenize(code, window.Prism.languages[language || ''] || window.Prism.languages[this.defaultLanguage]);
|
|
505
531
|
}
|
|
506
532
|
};
|
|
507
533
|
function getStartOfCodeInLine(anchor, offset) {
|
|
@@ -1114,6 +1140,14 @@ function registerCodeHighlighting(editor, tokenizer) {
|
|
|
1114
1140
|
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.INDENT_CONTENT_COMMAND, payload => handleMultilineIndent(lexical.INDENT_CONTENT_COMMAND), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.OUTDENT_CONTENT_COMMAND, payload => handleMultilineIndent(lexical.OUTDENT_CONTENT_COMMAND), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND, payload => handleShiftLines(lexical.KEY_ARROW_UP_COMMAND, payload), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, payload => handleShiftLines(lexical.KEY_ARROW_DOWN_COMMAND, payload), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.MOVE_TO_END, payload => handleMoveTo(lexical.MOVE_TO_END, payload), lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.MOVE_TO_START, payload => handleMoveTo(lexical.MOVE_TO_START, payload), lexical.COMMAND_PRIORITY_LOW));
|
|
1115
1141
|
}
|
|
1116
1142
|
|
|
1143
|
+
/**
|
|
1144
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1145
|
+
*
|
|
1146
|
+
* This source code is licensed under the MIT license found in the
|
|
1147
|
+
* LICENSE file in the root directory of this source tree.
|
|
1148
|
+
*
|
|
1149
|
+
*/
|
|
1150
|
+
|
|
1117
1151
|
exports.$createCodeHighlightNode = $createCodeHighlightNode;
|
|
1118
1152
|
exports.$createCodeNode = $createCodeNode;
|
|
1119
1153
|
exports.$isCodeHighlightNode = $isCodeHighlightNode;
|
|
@@ -4,23 +4,49 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import
|
|
8
|
-
import 'prismjs/components/prism-clike';
|
|
9
|
-
import 'prismjs/components/prism-javascript';
|
|
10
|
-
import 'prismjs/components/prism-markup';
|
|
11
|
-
import 'prismjs/components/prism-markdown';
|
|
12
|
-
import 'prismjs/components/prism-c';
|
|
13
|
-
import 'prismjs/components/prism-css';
|
|
14
|
-
import 'prismjs/components/prism-objectivec';
|
|
15
|
-
import 'prismjs/components/prism-sql';
|
|
16
|
-
import 'prismjs/components/prism-python';
|
|
17
|
-
import 'prismjs/components/prism-rust';
|
|
18
|
-
import 'prismjs/components/prism-swift';
|
|
19
|
-
import 'prismjs/components/prism-typescript';
|
|
20
|
-
import 'prismjs/components/prism-java';
|
|
21
|
-
import 'prismjs/components/prism-cpp';
|
|
22
|
-
import {
|
|
23
|
-
import { ElementNode, $applyNodeReplacement, $createParagraphNode, $isTextNode, $isTabNode, $createTabNode, $createLineBreakNode, TextNode, $isLineBreakNode, $
|
|
7
|
+
import 'prismjs';
|
|
8
|
+
import 'prismjs/components/prism-clike.js';
|
|
9
|
+
import 'prismjs/components/prism-javascript.js';
|
|
10
|
+
import 'prismjs/components/prism-markup.js';
|
|
11
|
+
import 'prismjs/components/prism-markdown.js';
|
|
12
|
+
import 'prismjs/components/prism-c.js';
|
|
13
|
+
import 'prismjs/components/prism-css.js';
|
|
14
|
+
import 'prismjs/components/prism-objectivec.js';
|
|
15
|
+
import 'prismjs/components/prism-sql.js';
|
|
16
|
+
import 'prismjs/components/prism-python.js';
|
|
17
|
+
import 'prismjs/components/prism-rust.js';
|
|
18
|
+
import 'prismjs/components/prism-swift.js';
|
|
19
|
+
import 'prismjs/components/prism-typescript.js';
|
|
20
|
+
import 'prismjs/components/prism-java.js';
|
|
21
|
+
import 'prismjs/components/prism-cpp.js';
|
|
22
|
+
import { isHTMLElement, addClassNamesToElement, removeClassNamesFromElement, mergeRegister } from '@lexical/utils';
|
|
23
|
+
import { ElementNode, $applyNodeReplacement, $createParagraphNode, $isTextNode, $isTabNode, $createTabNode, $createLineBreakNode, TextNode, $isLineBreakNode, $createTextNode, $getNodeByKey, $getSelection, $isRangeSelection, INDENT_CONTENT_COMMAND, OUTDENT_CONTENT_COMMAND, INSERT_TAB_COMMAND, KEY_ARROW_UP_COMMAND, MOVE_TO_START, KEY_TAB_COMMAND, COMMAND_PRIORITY_LOW, $insertNodes, KEY_ARROW_DOWN_COMMAND, MOVE_TO_END } from 'lexical';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
27
|
+
*
|
|
28
|
+
* This source code is licensed under the MIT license found in the
|
|
29
|
+
* LICENSE file in the root directory of this source tree.
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
35
|
+
*
|
|
36
|
+
* This source code is licensed under the MIT license found in the
|
|
37
|
+
* LICENSE file in the root directory of this source tree.
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
// invariant(condition, message) will refine types based on "condition", and
|
|
42
|
+
// if "condition" is false will throw an error. This function is special-cased
|
|
43
|
+
// in flow itself, so we can't name it anything else.
|
|
44
|
+
function invariant(cond, message, ...args) {
|
|
45
|
+
if (cond) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
throw new Error('Internal Lexical error: invariant() is meant to be replaced at compile ' + 'time. There is no runtime version. Error: ' + message);
|
|
49
|
+
}
|
|
24
50
|
|
|
25
51
|
/**
|
|
26
52
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
@@ -31,7 +57,7 @@ import { ElementNode, $applyNodeReplacement, $createParagraphNode, $isTextNode,
|
|
|
31
57
|
*/
|
|
32
58
|
const mapToPrismLanguage = language => {
|
|
33
59
|
// eslint-disable-next-line no-prototype-builtins
|
|
34
|
-
return language != null && Prism.languages.hasOwnProperty(language) ? language : undefined;
|
|
60
|
+
return language != null && window.Prism.languages.hasOwnProperty(language) ? language : undefined;
|
|
35
61
|
};
|
|
36
62
|
function hasChildDOMNodeTag(node, tagName) {
|
|
37
63
|
for (const child of node.childNodes) {
|
|
@@ -387,10 +413,10 @@ function getLanguageFriendlyName(lang) {
|
|
|
387
413
|
return CODE_LANGUAGE_FRIENDLY_NAME_MAP[_lang] || _lang;
|
|
388
414
|
}
|
|
389
415
|
const getDefaultCodeLanguage = () => DEFAULT_CODE_LANGUAGE;
|
|
390
|
-
const getCodeLanguages = () => Object.keys(Prism.languages).filter(
|
|
416
|
+
const getCodeLanguages = () => Object.keys(window.Prism.languages).filter(
|
|
391
417
|
// Prism has several language helpers mixed into languages object
|
|
392
418
|
// so filtering them out here to get langs list
|
|
393
|
-
language => typeof Prism.languages[language] !== 'function').sort();
|
|
419
|
+
language => typeof window.Prism.languages[language] !== 'function').sort();
|
|
394
420
|
|
|
395
421
|
/** @noInheritDoc */
|
|
396
422
|
class CodeHighlightNode extends TextNode {
|
|
@@ -499,7 +525,7 @@ function getLastCodeNodeOfLine(anchor) {
|
|
|
499
525
|
const PrismTokenizer = {
|
|
500
526
|
defaultLanguage: DEFAULT_CODE_LANGUAGE,
|
|
501
527
|
tokenize(code, language) {
|
|
502
|
-
return Prism.tokenize(code, Prism.languages[language || ''] || Prism.languages[this.defaultLanguage]);
|
|
528
|
+
return window.Prism.tokenize(code, window.Prism.languages[language || ''] || window.Prism.languages[this.defaultLanguage]);
|
|
503
529
|
}
|
|
504
530
|
};
|
|
505
531
|
function getStartOfCodeInLine(anchor, offset) {
|
|
@@ -1112,4 +1138,12 @@ function registerCodeHighlighting(editor, tokenizer) {
|
|
|
1112
1138
|
}, COMMAND_PRIORITY_LOW), editor.registerCommand(INDENT_CONTENT_COMMAND, payload => handleMultilineIndent(INDENT_CONTENT_COMMAND), COMMAND_PRIORITY_LOW), editor.registerCommand(OUTDENT_CONTENT_COMMAND, payload => handleMultilineIndent(OUTDENT_CONTENT_COMMAND), COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_ARROW_UP_COMMAND, payload => handleShiftLines(KEY_ARROW_UP_COMMAND, payload), COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_ARROW_DOWN_COMMAND, payload => handleShiftLines(KEY_ARROW_DOWN_COMMAND, payload), COMMAND_PRIORITY_LOW), editor.registerCommand(MOVE_TO_END, payload => handleMoveTo(MOVE_TO_END, payload), COMMAND_PRIORITY_LOW), editor.registerCommand(MOVE_TO_START, payload => handleMoveTo(MOVE_TO_START, payload), COMMAND_PRIORITY_LOW));
|
|
1113
1139
|
}
|
|
1114
1140
|
|
|
1141
|
+
/**
|
|
1142
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1143
|
+
*
|
|
1144
|
+
* This source code is licensed under the MIT license found in the
|
|
1145
|
+
* LICENSE file in the root directory of this source tree.
|
|
1146
|
+
*
|
|
1147
|
+
*/
|
|
1148
|
+
|
|
1115
1149
|
export { $createCodeHighlightNode, $createCodeNode, $isCodeHighlightNode, $isCodeNode, CODE_LANGUAGE_FRIENDLY_NAME_MAP, CODE_LANGUAGE_MAP, CodeHighlightNode, CodeNode, DEFAULT_CODE_LANGUAGE, PrismTokenizer, getCodeLanguages, getDefaultCodeLanguage, getEndOfCodeInLine, getFirstCodeNodeOfLine, getLanguageFriendlyName, getLastCodeNodeOfLine, getStartOfCodeInLine, normalizeCodeLang, registerCodeHighlighting };
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import * as modDev from './LexicalCode.dev.
|
|
8
|
-
import * as modProd from './LexicalCode.prod.
|
|
7
|
+
import * as modDev from './LexicalCode.dev.mjs';
|
|
8
|
+
import * as modProd from './LexicalCode.prod.mjs';
|
|
9
9
|
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
10
10
|
export const $createCodeHighlightNode = mod.$createCodeHighlightNode;
|
|
11
11
|
export const $createCodeNode = mod.$createCodeNode;
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
const mod = await (process.env.NODE_ENV === 'development' ? import('./LexicalCode.dev.mjs') : import('./LexicalCode.prod.mjs'));
|
|
8
|
+
export const $createCodeHighlightNode = mod.$createCodeHighlightNode;
|
|
9
|
+
export const $createCodeNode = mod.$createCodeNode;
|
|
10
|
+
export const $isCodeHighlightNode = mod.$isCodeHighlightNode;
|
|
11
|
+
export const $isCodeNode = mod.$isCodeNode;
|
|
12
|
+
export const CODE_LANGUAGE_FRIENDLY_NAME_MAP = mod.CODE_LANGUAGE_FRIENDLY_NAME_MAP;
|
|
13
|
+
export const CODE_LANGUAGE_MAP = mod.CODE_LANGUAGE_MAP;
|
|
14
|
+
export const CodeHighlightNode = mod.CodeHighlightNode;
|
|
15
|
+
export const CodeNode = mod.CodeNode;
|
|
16
|
+
export const DEFAULT_CODE_LANGUAGE = mod.DEFAULT_CODE_LANGUAGE;
|
|
17
|
+
export const PrismTokenizer = mod.PrismTokenizer;
|
|
18
|
+
export const getCodeLanguages = mod.getCodeLanguages;
|
|
19
|
+
export const getDefaultCodeLanguage = mod.getDefaultCodeLanguage;
|
|
20
|
+
export const getEndOfCodeInLine = mod.getEndOfCodeInLine;
|
|
21
|
+
export const getFirstCodeNodeOfLine = mod.getFirstCodeNodeOfLine;
|
|
22
|
+
export const getLanguageFriendlyName = mod.getLanguageFriendlyName;
|
|
23
|
+
export const getLastCodeNodeOfLine = mod.getLastCodeNodeOfLine;
|
|
24
|
+
export const getStartOfCodeInLine = mod.getStartOfCodeInLine;
|
|
25
|
+
export const normalizeCodeLang = mod.normalizeCodeLang;
|
|
26
|
+
export const registerCodeHighlighting = mod.registerCodeHighlighting;
|
package/LexicalCode.prod.js
CHANGED
|
@@ -4,39 +4,39 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
'use strict';
|
|
8
|
-
require("prismjs/components/prism-
|
|
9
|
-
class
|
|
10
|
-
return!1}exportDOM(a){let b=document.createElement("pre");
|
|
11
|
-
return a.classList.contains("js-file-line")?{conversion:ca,priority:3}:b&&
|
|
12
|
-
2].getTextContent()&&a.isCollapsed()&&a.anchor.key===this.__key&&a.anchor.offset===e)return c[e-1].remove(),c[e-2].remove(),a=
|
|
13
|
-
e=b.splitText(f.offset)[0];h=0===f.offset?0:1;h=e.getIndexWithinParent()+h;k=b.getParentOrThrow();l=[
|
|
14
|
-
|
|
15
|
-
function
|
|
16
|
-
let
|
|
17
|
-
class
|
|
18
|
-
c&&(a&&
|
|
19
|
-
function
|
|
20
|
-
function
|
|
21
|
-
(f=a.getNextSibling(),
|
|
22
|
-
function
|
|
23
|
-
function
|
|
24
|
-
{from:h,nodesForReplacement:g,to:k};return p!==x||u.length?(a.splice(p,x-p,u),!0):!1})},{onUpdate:()=>{
|
|
25
|
-
function
|
|
26
|
-
function ea(a,b){a=
|
|
27
|
-
function
|
|
28
|
-
function
|
|
29
|
-
function fa(a){var b=
|
|
7
|
+
'use strict';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");require("prismjs/components/prism-typescript");
|
|
8
|
+
require("prismjs/components/prism-java");require("prismjs/components/prism-cpp");var d=require("@lexical/utils"),m=require("lexical");let q=a=>null!=a&&window.Prism.languages.hasOwnProperty(a)?a:void 0;function v(a,b){for(let c of a.childNodes){if(d.isHTMLElement(c)&&c.tagName===b)return!0;v(c,b)}return!1}
|
|
9
|
+
class w extends m.ElementNode{static getType(){return"code"}static clone(a){return new w(a.__language,a.__key)}constructor(a,b){super(b);this.__language=q(a)}createDOM(a){let b=document.createElement("code");d.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");
|
|
10
|
+
return!1}exportDOM(a){let b=document.createElement("pre");d.addClassNamesToElement(b,a._config.theme.code);b.setAttribute("spellcheck","false");(a=this.getLanguage())&&b.setAttribute("data-highlight-language",a);return{element:b}}static importDOM(){return{code:a=>null!=a.textContent&&(/\r?\n/.test(a.textContent)||v(a,"BR"))?{conversion:y,priority:1}:null,div:()=>({conversion:aa,priority:1}),pre:()=>({conversion:y,priority:0}),table:a=>z(a)?{conversion:ba,priority:3}:null,td:a=>{let b=a.closest("table");
|
|
11
|
+
return a.classList.contains("js-file-line")?{conversion:ca,priority:3}:b&&z(b)?{conversion:A,priority:3}:null},tr:a=>(a=a.closest("table"))&&z(a)?{conversion:A,priority:3}:null}}static importJSON(a){let b=B(a.language);b.setFormat(a.format);b.setIndent(a.indent);b.setDirection(a.direction);return b}exportJSON(){return{...super.exportJSON(),language:this.getLanguage(),type:"code",version:1}}insertNewAfter(a,b=!0){var c=this.getChildren(),e=c.length;if(2<=e&&"\n"===c[e-1].getTextContent()&&"\n"===c[e-
|
|
12
|
+
2].getTextContent()&&a.isCollapsed()&&a.anchor.key===this.__key&&a.anchor.offset===e)return c[e-1].remove(),c[e-2].remove(),a=m.$createParagraphNode(),this.insertAfter(a,b),a;let {anchor:f,focus:g}=a;b=(f.isBefore(g)?f:g).getNode();if(m.$isTextNode(b)){e=C(b);for(c=[];;)if(m.$isTabNode(e))c.push(m.$createTabNode()),e=e.getNextSibling();else if(D(e)){for(var h=0,k=e.getTextContent(),l=e.getTextContentSize();h<l&&" "===k[h];)h++;0!==h&&c.push(E(" ".repeat(h)));if(h!==l)break;e=e.getNextSibling()}else break;
|
|
13
|
+
e=b.splitText(f.offset)[0];h=0===f.offset?0:1;h=e.getIndexWithinParent()+h;k=b.getParentOrThrow();l=[m.$createLineBreakNode(),...c];k.splice(h,0,l);(c=c[c.length-1])?c.select():0===f.offset?e.selectPrevious():e.getNextSibling().selectNext(0,0)}F(b)&&({offset:a}=a.anchor,b.splice(a,0,[m.$createLineBreakNode()]),b.select(a+1,a+1));return null}canIndent(){return!1}collapseAtStart(){let a=m.$createParagraphNode();this.getChildren().forEach(b=>a.append(b));this.replace(a);return!0}setLanguage(a){this.getWritable().__language=
|
|
14
|
+
q(a)}getLanguage(){return this.getLatest().__language}}function B(a){return m.$applyNodeReplacement(new w(a))}function F(a){return a instanceof w}function y(a){let b;d.isHTMLElement(a)&&(b=a.getAttribute("data-highlight-language"));return{node:B(b)}}function aa(a){let b=null!==a.style.fontFamily.match("monospace");return b||da(a)?{after:c=>{let e=a.parentNode;null!=e&&a!==e.lastChild&&c.push(m.$createLineBreakNode());return c},node:b?B():null}:{node:null}}function ba(){return{node:B()}}
|
|
15
|
+
function A(){return{node:null}}function ca(a){return{after:b=>{a.parentNode&&a.parentNode.nextSibling&&b.push(m.$createLineBreakNode());return b},node:null}}function da(a){for(a=a.parentElement;null!==a;){if(null!==a.style.fontFamily.match("monospace"))return!0;a=a.parentElement}return!1}function z(a){return a.classList.contains("js-file-line-container")}
|
|
16
|
+
let G={c:"C",clike:"C-like",cpp:"C++",css:"CSS",html:"HTML",java:"Java",js:"JavaScript",markdown:"Markdown",objc:"Objective-C",plain:"Plain Text",py:"Python",rust:"Rust",sql:"SQL",swift:"Swift",typescript:"TypeScript",xml:"XML"},H={cpp:"cpp",java:"java",javascript:"js",md:"markdown",plaintext:"plain",python:"py",text:"plain",ts:"typescript"};function J(a){return H[a]||a}
|
|
17
|
+
class K extends m.TextNode{constructor(a,b,c){super(a,c);this.__highlightType=b}static getType(){return"code-highlight"}static clone(a){return new K(a.__text,a.__highlightType||void 0,a.__key)}getHighlightType(){return this.getLatest().__highlightType}canHaveFormat(){return!1}createDOM(a){let b=super.createDOM(a);a=L(a.theme,this.__highlightType);d.addClassNamesToElement(b,a);return b}updateDOM(a,b,c){let e=super.updateDOM(a,b,c);a=L(c.theme,a.__highlightType);c=L(c.theme,this.__highlightType);a!==
|
|
18
|
+
c&&(a&&d.removeClassNamesFromElement(b,a),c&&d.addClassNamesToElement(b,c));return e}static importJSON(a){let b=E(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}isParentRequired(){return!0}createParentElementNode(){return B()}}function L(a,b){return b&&a&&a.codeHighlight&&a.codeHighlight[b]}
|
|
19
|
+
function E(a,b){return m.$applyNodeReplacement(new K(a,b))}function D(a){return a instanceof K}function C(a){let b=a;for(;D(a)||m.$isTabNode(a);)b=a,a=a.getPreviousSibling();return b}function M(a){let b=a;for(;D(a)||m.$isTabNode(a);)b=a,a=a.getNextSibling();return b}let N={defaultLanguage:"javascript",tokenize(a,b){return window.Prism.tokenize(a,window.Prism.languages[b||""]||window.Prism.languages[this.defaultLanguage])}};
|
|
20
|
+
function O(a,b){let c=null;var e=null,f=a;let g=b,h=a.getTextContent();for(;;){if(0===g){f=f.getPreviousSibling();if(null===f)break;if(!(D(f)||m.$isTabNode(f)||m.$isLineBreakNode(f)))throw Error("Expected a valid Code Node: CodeHighlightNode, TabNode, LineBreakNode");if(m.$isLineBreakNode(f)){c={node:f,offset:1};break}g=Math.max(0,f.getTextContentSize()-1);h=f.getTextContent()}else g--;let k=h[g];D(f)&&" "!==k&&(e={node:f,offset:g})}if(null!==e)return e;e=null;b<a.getTextContentSize()?D(a)&&(e=a.getTextContent()[b]):
|
|
21
|
+
(f=a.getNextSibling(),D(f)&&(e=f.getTextContent()[0]));if(null!==e&&" "!==e)return c;a:for(e=a,f=a.getTextContent(),a=a.getTextContentSize();;){if(!D(e)||b===a){e=e.getNextSibling();if(null===e||m.$isLineBreakNode(e)){a=null;break a}D(e)&&(b=0,f=e.getTextContent(),a=e.getTextContentSize())}if(D(e)){if(" "!==f[b]){a={node:e,offset:b};break a}b++}}return null!==a?a:c}function P(a){a=M(a);if(m.$isLineBreakNode(a))throw Error("Unexpected lineBreakNode in getEndOfCodeInLine");return a}
|
|
22
|
+
function Q(a,b,c){let e=a.getParent();F(e)?R(e,b,c):D(a)&&a.replace(m.$createTextNode(a.__text))}let S=new Set;
|
|
23
|
+
function R(a,b,c){let e=a.getKey();S.has(e)||(S.add(e),void 0===a.getLanguage()&&a.setLanguage(c.defaultLanguage),b.update(()=>{ea(e,()=>{var f=m.$getNodeByKey(e);if(!F(f)||!f.isAttached())return!1;var g=f.getTextContent();g=c.tokenize(g,f.getLanguage()||c.defaultLanguage);g=T(g);var h=f.getChildren();for(f=0;f<h.length&&U(h[f],g[f]);)f++;var k=h.length;let l=g.length,r=Math.min(k,l)-f,n=0;for(;n<r;)if(n++,!U(h[k-n],g[l-n])){n--;break}h=f;k-=n;g=g.slice(f,l-n);let {from:p,to:x,nodesForReplacement:u}=
|
|
24
|
+
{from:h,nodesForReplacement:g,to:k};return p!==x||u.length?(a.splice(p,x-p,u),!0):!1})},{onUpdate:()=>{S.delete(e)},skipTransforms:!0}))}
|
|
25
|
+
function T(a,b){let c=[];for(let e of a)if("string"===typeof e){a=e.split(/(\n|\t)/);let f=a.length;for(let g=0;g<f;g++){let h=a[g];"\n"===h||"\r\n"===h?c.push(m.$createLineBreakNode()):"\t"===h?c.push(m.$createTabNode()):0<h.length&&c.push(E(h,b))}}else({content:a}=e),"string"===typeof a?c.push(...T([a],e.type)):Array.isArray(a)&&c.push(...T(a,e.type));return c}
|
|
26
|
+
function ea(a,b){a=m.$getNodeByKey(a);if(F(a)&&a.isAttached()){var c=m.$getSelection();if(m.$isRangeSelection(c)){c=c.anchor;var e=c.offset,f="element"===c.type&&m.$isLineBreakNode(a.getChildAtIndex(c.offset-1)),g=0;if(!f){let h=c.getNode();g=e+h.getPreviousSiblings().reduce((k,l)=>k+l.getTextContentSize(),0)}b()&&(f?c.getNode().select(e,e):a.getChildren().some(h=>{let k=m.$isTextNode(h);if(k||m.$isLineBreakNode(h)){let l=h.getTextContentSize();if(k&&l>=g)return h.select(g,g),!0;g-=l}return!1}))}else b()}}
|
|
27
|
+
function U(a,b){return D(a)&&D(b)&&a.__text===b.__text&&a.__highlightType===b.__highlightType||m.$isTabNode(a)&&m.$isTabNode(b)||m.$isLineBreakNode(a)&&m.$isLineBreakNode(b)}function V(a){if(!m.$isRangeSelection(a))return!1;var b=a.anchor.getNode();a=a.focus.getNode();if(b.is(a)&&F(b))return!0;b=b.getParent();return F(b)&&b.is(a.getParent())}
|
|
28
|
+
function W(a){a=a.getNodes();let b=[[]];if(1===a.length&&F(a[0]))return b;let c=b[0];for(let e=0;e<a.length;e++){let f=a[e];if(!(D(f)||m.$isTabNode(f)||m.$isLineBreakNode(f)))throw Error("Expected selection to be inside CodeBlock and consisting of CodeHighlightNode, TabNode and LineBreakNode");m.$isLineBreakNode(f)?0!==e&&0<c.length&&(c=[],b.push(c)):c.push(f)}return b}
|
|
29
|
+
function fa(a){var b=m.$getSelection();if(!m.$isRangeSelection(b)||!V(b))return null;let c=a?m.OUTDENT_CONTENT_COMMAND:m.INDENT_CONTENT_COMMAND;a=a?m.OUTDENT_CONTENT_COMMAND:m.INSERT_TAB_COMMAND;if(1<W(b).length)return c;var e=b.getNodes()[0];if(!(F(e)||D(e)||m.$isTabNode(e)||m.$isLineBreakNode(e)))throw Error("Expected selection firstNode to be CodeHighlightNode or TabNode");if(F(e))return c;let f=C(e);e=M(e);var g=b.anchor;let h=b.focus;h.isBefore(g)?b=h:(b=g,g=h);return null!==f&&null!==e&&b.key===
|
|
30
30
|
f.getKey()&&0===b.offset&&g.key===e.getKey()&&g.offset===e.getTextContentSize()?c:a}
|
|
31
|
-
function
|
|
32
|
-
b.insertNodes([
|
|
33
|
-
function
|
|
34
|
-
a.getNextSibling())return a.selectNext(),b.preventDefault(),!0;return!1}let n;if(k.isBefore(l)){var p=
|
|
35
|
-
r?
|
|
36
|
-
function
|
|
37
|
-
exports.$createCodeNode=
|
|
38
|
-
exports.getLanguageFriendlyName=function(a){a=
|
|
39
|
-
exports.registerCodeHighlighting=function(a,b){if(!a.hasNodes([
|
|
40
|
-
0;n<k;n++)
|
|
41
|
-
return!0},
|
|
42
|
-
a.registerCommand(
|
|
31
|
+
function X(a){var b=m.$getSelection();if(!m.$isRangeSelection(b)||!V(b))return!1;var c=W(b);let e=c.length;if(1<c.length){for(b=0;b<e;b++){var f=c[b];0<f.length&&(f=f[0],0===b&&(f=C(f)),null!==f&&(a===m.INDENT_CONTENT_COMMAND?f.insertBefore(m.$createTabNode()):m.$isTabNode(f)&&f.remove()))}return!0}c=b.getNodes()[0];if(!(F(c)||D(c)||m.$isTabNode(c)||m.$isLineBreakNode(c)))throw Error("Expected selection firstNode to be CodeHighlightNode or CodeTabNode");if(F(c))return a===m.INDENT_CONTENT_COMMAND&&
|
|
32
|
+
b.insertNodes([m.$createTabNode()]),!0;c=C(c);if(null===c)throw Error("Expected getFirstCodeNodeOfLine to return a valid Code Node");a===m.INDENT_CONTENT_COMMAND?m.$isLineBreakNode(c)?c.insertAfter(m.$createTabNode()):c.insertBefore(m.$createTabNode()):m.$isTabNode(c)&&c.remove();return!0}
|
|
33
|
+
function Y(a,b){let c=m.$getSelection();if(!m.$isRangeSelection(c))return!1;let {anchor:e,focus:f}=c,g=e.offset,h=f.offset,k=e.getNode(),l=f.getNode();var r=a===m.KEY_ARROW_UP_COMMAND;if(!V(c)||!D(k)&&!m.$isTabNode(k)||!D(l)&&!m.$isTabNode(l))return!1;if(!b.altKey){if(c.isCollapsed())if(a=k.getParentOrThrow(),r&&0===g&&null===k.getPreviousSibling()){if(null===a.getPreviousSibling())return a.selectPrevious(),b.preventDefault(),!0}else if(!r&&g===k.getTextContentSize()&&null===k.getNextSibling()&&null===
|
|
34
|
+
a.getNextSibling())return a.selectNext(),b.preventDefault(),!0;return!1}let n;if(k.isBefore(l)){var p=C(k);n=M(l)}else p=C(l),n=M(k);if(null==p||null==n)return!1;let x=p.getNodesBetween(n);for(let t=0;t<x.length;t++){let I=x[t];if(!D(I)&&!m.$isTabNode(I)&&!m.$isLineBreakNode(I))return!1}b.preventDefault();b.stopPropagation();b=r?p.getPreviousSibling():n.getNextSibling();if(!m.$isLineBreakNode(b))return!0;p=r?b.getPreviousSibling():b.getNextSibling();if(null==p)return!0;r=D(p)||m.$isTabNode(p)||m.$isLineBreakNode(p)?
|
|
35
|
+
r?C(p):M(p):null;let u=null!=r?r:p;b.remove();x.forEach(t=>t.remove());a===m.KEY_ARROW_UP_COMMAND?(x.forEach(t=>u.insertBefore(t)),u.insertBefore(b)):(u.insertAfter(b),u=b,x.forEach(t=>{u.insertAfter(t);u=t}));c.setTextNodeRange(k,g,l,h);return!0}
|
|
36
|
+
function Z(a,b){let c=m.$getSelection();if(!m.$isRangeSelection(c))return!1;let {anchor:e,focus:f}=c;var g=e.getNode();let h=f.getNode();a=a===m.MOVE_TO_START;if(!D(g)&&!m.$isTabNode(g)||!D(h)&&!m.$isTabNode(h))return!1;if(a)if(g=O(h,f.offset),null!==g){let {node:k,offset:l}=g;m.$isLineBreakNode(k)?k.selectNext(0,0):c.setTextNodeRange(k,l,k,l)}else h.getParentOrThrow().selectStart();else P(h).select();b.preventDefault();b.stopPropagation();return!0}exports.$createCodeHighlightNode=E;
|
|
37
|
+
exports.$createCodeNode=B;exports.$isCodeHighlightNode=D;exports.$isCodeNode=F;exports.CODE_LANGUAGE_FRIENDLY_NAME_MAP=G;exports.CODE_LANGUAGE_MAP=H;exports.CodeHighlightNode=K;exports.CodeNode=w;exports.DEFAULT_CODE_LANGUAGE="javascript";exports.PrismTokenizer=N;exports.getCodeLanguages=()=>Object.keys(window.Prism.languages).filter(a=>"function"!==typeof window.Prism.languages[a]).sort();exports.getDefaultCodeLanguage=()=>"javascript";exports.getEndOfCodeInLine=P;
|
|
38
|
+
exports.getFirstCodeNodeOfLine=C;exports.getLanguageFriendlyName=function(a){a=J(a);return G[a]||a};exports.getLastCodeNodeOfLine=M;exports.getStartOfCodeInLine=O;exports.normalizeCodeLang=J;
|
|
39
|
+
exports.registerCodeHighlighting=function(a,b){if(!a.hasNodes([w,K]))throw Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");null==b&&(b=N);return d.mergeRegister(a.registerMutationListener(w,c=>{a.update(()=>{for(let [g,h]of c)if("destroyed"!==h){var e=m.$getNodeByKey(g);if(null!==e)a:{var f=e;e=a.getElementByKey(f.getKey());if(null===e)break a;f=f.getChildren();let k=f.length;if(k===e.__cachedChildrenLength)break a;e.__cachedChildrenLength=k;let l="1",r=1;for(let n=
|
|
40
|
+
0;n<k;n++)m.$isLineBreakNode(f[n])&&(l+="\n"+ ++r);e.setAttribute("data-gutter",l)}}})}),a.registerNodeTransform(w,c=>R(c,a,b)),a.registerNodeTransform(m.TextNode,c=>Q(c,a,b)),a.registerNodeTransform(K,c=>Q(c,a,b)),a.registerCommand(m.KEY_TAB_COMMAND,c=>{let e=fa(c.shiftKey);if(null===e)return!1;c.preventDefault();a.dispatchCommand(e,void 0);return!0},m.COMMAND_PRIORITY_LOW),a.registerCommand(m.INSERT_TAB_COMMAND,()=>{let c=m.$getSelection();if(!V(c))return!1;m.$insertNodes([m.$createTabNode()]);
|
|
41
|
+
return!0},m.COMMAND_PRIORITY_LOW),a.registerCommand(m.INDENT_CONTENT_COMMAND,()=>X(m.INDENT_CONTENT_COMMAND),m.COMMAND_PRIORITY_LOW),a.registerCommand(m.OUTDENT_CONTENT_COMMAND,()=>X(m.OUTDENT_CONTENT_COMMAND),m.COMMAND_PRIORITY_LOW),a.registerCommand(m.KEY_ARROW_UP_COMMAND,c=>Y(m.KEY_ARROW_UP_COMMAND,c),m.COMMAND_PRIORITY_LOW),a.registerCommand(m.KEY_ARROW_DOWN_COMMAND,c=>Y(m.KEY_ARROW_DOWN_COMMAND,c),m.COMMAND_PRIORITY_LOW),a.registerCommand(m.MOVE_TO_END,c=>Z(m.MOVE_TO_END,c),m.COMMAND_PRIORITY_LOW),
|
|
42
|
+
a.registerCommand(m.MOVE_TO_START,c=>Z(m.MOVE_TO_START,c),m.COMMAND_PRIORITY_LOW))}
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
import"prismjs";import"prismjs/components/prism-clike.js";import"prismjs/components/prism-javascript.js";import"prismjs/components/prism-markup.js";import"prismjs/components/prism-markdown.js";import"prismjs/components/prism-c.js";import"prismjs/components/prism-css.js";import"prismjs/components/prism-objectivec.js";import"prismjs/components/prism-sql.js";import"prismjs/components/prism-python.js";import"prismjs/components/prism-rust.js";import"prismjs/components/prism-swift.js";import"prismjs/components/prism-typescript.js";import"prismjs/components/prism-java.js";import"prismjs/components/prism-cpp.js";import{isHTMLElement as e,addClassNamesToElement as t,removeClassNamesFromElement as n,mergeRegister as r}from"@lexical/utils";import{ElementNode as o,$applyNodeReplacement as i,$createParagraphNode as s,$isTextNode as l,$isTabNode as c,$createTabNode as u,$createLineBreakNode as g,TextNode as a,$isLineBreakNode as f,$createTextNode as p,$getNodeByKey as d,$getSelection as h,$isRangeSelection as m,INDENT_CONTENT_COMMAND as x,OUTDENT_CONTENT_COMMAND as N,INSERT_TAB_COMMAND as y,KEY_ARROW_UP_COMMAND as C,MOVE_TO_START as v,KEY_TAB_COMMAND as T,COMMAND_PRIORITY_LOW as _,$insertNodes as b,KEY_ARROW_DOWN_COMMAND as j,MOVE_TO_END as S}from"lexical";const w=e=>null!=e&&window.Prism.languages.hasOwnProperty(e)?e:void 0;function k(t,n){for(const r of t.childNodes){if(e(r)&&r.tagName===n)return!0;k(r,n)}return!1}const P="data-highlight-language";class L extends o{static getType(){return"code"}static clone(e){return new L(e.__language,e.__key)}constructor(e,t){super(t),this.__language=w(e)}createDOM(e){const n=document.createElement("code");t(n,e.theme.code),n.setAttribute("spellcheck","false");const r=this.getLanguage();return r&&n.setAttribute(P,r),n}updateDOM(e,t,n){const r=this.__language,o=e.__language;return r?r!==o&&t.setAttribute(P,r):o&&t.removeAttribute(P),!1}exportDOM(e){const n=document.createElement("pre");t(n,e._config.theme.code),n.setAttribute("spellcheck","false");const r=this.getLanguage();return r&&n.setAttribute(P,r),{element:n}}static importDOM(){return{code:e=>null!=e.textContent&&(/\r?\n/.test(e.textContent)||k(e,"BR"))?{conversion:A,priority:1}:null,div:e=>({conversion:D,priority:1}),pre:e=>({conversion:A,priority:0}),table:e=>F(e)?{conversion:M,priority:3}:null,td:e=>{const t=e,n=t.closest("table");return t.classList.contains("js-file-line")?{conversion:z,priority:3}:n&&F(n)?{conversion:B,priority:3}:null},tr:e=>{const t=e.closest("table");return t&&F(t)?{conversion:B,priority:3}:null}}}static importJSON(e){const t=E(e.language);return t.setFormat(e.format),t.setIndent(e.indent),t.setDirection(e.direction),t}exportJSON(){return{...super.exportJSON(),language:this.getLanguage(),type:"code",version:1}}insertNewAfter(e,t=!0){const n=this.getChildren(),r=n.length;if(r>=2&&"\n"===n[r-1].getTextContent()&&"\n"===n[r-2].getTextContent()&&e.isCollapsed()&&e.anchor.key===this.__key&&e.anchor.offset===r){n[r-1].remove(),n[r-2].remove();const e=s();return this.insertAfter(e,t),e}const{anchor:o,focus:i}=e,a=(o.isBefore(i)?o:i).getNode();if(l(a)){let e=Y(a);const t=[];for(;;)if(c(e))t.push(u()),e=e.getNextSibling();else{if(!V(e))break;{let n=0;const r=e.getTextContent(),o=e.getTextContentSize();for(;n<o&&" "===r[n];)n++;if(0!==n&&t.push(G(" ".repeat(n))),n!==o)break;e=e.getNextSibling()}}const n=a.splitText(o.offset)[0],r=0===o.offset?0:1,i=n.getIndexWithinParent()+r,s=a.getParentOrThrow(),l=[g(),...t];s.splice(i,0,l);const f=t[t.length-1];f?f.select():0===o.offset?n.selectPrevious():n.getNextSibling().selectNext(0,0)}if(O(a)){const{offset:t}=e.anchor;a.splice(t,0,[g()]),a.select(t+1,t+1)}return null}canIndent(){return!1}collapseAtStart(){const e=s();return this.getChildren().forEach((t=>e.append(t))),this.replace(e),!0}setLanguage(e){this.getWritable().__language=w(e)}getLanguage(){return this.getLatest().__language}}function E(e){return i(new L(e))}function O(e){return e instanceof L}function A(t){let n;return e(t)&&(n=t.getAttribute(P)),{node:E(n)}}function D(e){const t=e,n=H(t);return n||function(e){let t=e.parentElement;for(;null!==t;){if(H(t))return!0;t=t.parentElement}return!1}(t)?{after:t=>{const n=e.parentNode;return null!=n&&e!==n.lastChild&&t.push(g()),t},node:n?E():null}:{node:null}}function M(){return{node:E()}}function B(){return{node:null}}function z(e){const t=e;return{after:e=>(t.parentNode&&t.parentNode.nextSibling&&e.push(g()),e),node:null}}function H(e){return null!==e.style.fontFamily.match("monospace")}function F(e){return e.classList.contains("js-file-line-container")}const J="javascript",K={c:"C",clike:"C-like",cpp:"C++",css:"CSS",html:"HTML",java:"Java",js:"JavaScript",markdown:"Markdown",objc:"Objective-C",plain:"Plain Text",py:"Python",rust:"Rust",sql:"SQL",swift:"Swift",typescript:"TypeScript",xml:"XML"},R={cpp:"cpp",java:"java",javascript:"js",md:"markdown",plaintext:"plain",python:"py",text:"plain",ts:"typescript"};function I(e){return R[e]||e}function q(e){const t=I(e);return K[t]||t}const U=()=>J,W=()=>Object.keys(window.Prism.languages).filter((e=>"function"!=typeof window.Prism.languages[e])).sort();class Q extends a{constructor(e,t,n){super(e,n),this.__highlightType=t}static getType(){return"code-highlight"}static clone(e){return new Q(e.__text,e.__highlightType||void 0,e.__key)}getHighlightType(){return this.getLatest().__highlightType}canHaveFormat(){return!1}createDOM(e){const n=super.createDOM(e),r=X(e.theme,this.__highlightType);return t(n,r),n}updateDOM(e,r,o){const i=super.updateDOM(e,r,o),s=X(o.theme,e.__highlightType),l=X(o.theme,this.__highlightType);return s!==l&&(s&&n(r,s),l&&t(r,l)),i}static importJSON(e){const t=G(e.text,e.highlightType);return t.setFormat(e.format),t.setDetail(e.detail),t.setMode(e.mode),t.setStyle(e.style),t}exportJSON(){return{...super.exportJSON(),highlightType:this.getHighlightType(),type:"code-highlight",version:1}}setFormat(e){return this}isParentRequired(){return!0}createParentElementNode(){return E()}}function X(e,t){return t&&e&&e.codeHighlight&&e.codeHighlight[t]}function G(e,t){return i(new Q(e,t))}function V(e){return e instanceof Q}function Y(e){let t=e,n=e;for(;V(n)||c(n);)t=n,n=n.getPreviousSibling();return t}function Z(e){let t=e,n=e;for(;V(n)||c(n);)t=n,n=n.getNextSibling();return t}const $={defaultLanguage:J,tokenize(e,t){return window.Prism.tokenize(e,window.Prism.languages[t||""]||window.Prism.languages[this.defaultLanguage])}};function ee(e,t){let n=null,r=null,o=e,i=t,s=e.getTextContent();for(;;){if(0===i){if(o=o.getPreviousSibling(),null===o)break;if(!(V(o)||c(o)||f(o)))throw Error("Expected a valid Code Node: CodeHighlightNode, TabNode, LineBreakNode");if(f(o)){n={node:o,offset:1};break}i=Math.max(0,o.getTextContentSize()-1),s=o.getTextContent()}else i--;const e=s[i];V(o)&&" "!==e&&(r={node:o,offset:i})}if(null!==r)return r;let l=null;if(t<e.getTextContentSize())V(e)&&(l=e.getTextContent()[t]);else{const t=e.getNextSibling();V(t)&&(l=t.getTextContent()[0])}if(null!==l&&" "!==l)return n;{const r=function(e,t){let n=e,r=t,o=e.getTextContent(),i=e.getTextContentSize();for(;;){if(!V(n)||r===i){if(n=n.getNextSibling(),null===n||f(n))return null;V(n)&&(r=0,o=n.getTextContent(),i=n.getTextContentSize())}if(V(n)){if(" "!==o[r])return{node:n,offset:r};r++}}}(e,t);return null!==r?r:n}}function te(e){const t=Z(e);if(f(t))throw Error("Unexpected lineBreakNode in getEndOfCodeInLine");return t}function ne(e,t,n){const r=e.getParent();O(r)?ie(r,t,n):V(e)&&e.replace(p(e.__text))}function re(e,t){const n=t.getElementByKey(e.getKey());if(null===n)return;const r=e.getChildren(),o=r.length;if(o===n.__cachedChildrenLength)return;n.__cachedChildrenLength=o;let i="1",s=1;for(let e=0;e<o;e++)f(r[e])&&(i+="\n"+ ++s);n.setAttribute("data-gutter",i)}const oe=new Set;function ie(e,t,n){const r=e.getKey();oe.has(r)||(oe.add(r),void 0===e.getLanguage()&&e.setLanguage(n.defaultLanguage),t.update((()=>{!function(e,t){const n=d(e);if(!O(n)||!n.isAttached())return;const r=h();if(!m(r))return void t();const o=r.anchor,i=o.offset,s="element"===o.type&&f(n.getChildAtIndex(o.offset-1));let c=0;if(!s){const e=o.getNode();c=i+e.getPreviousSiblings().reduce(((e,t)=>e+t.getTextContentSize()),0)}if(!t())return;if(s)return void o.getNode().select(i,i);n.getChildren().some((e=>{const t=l(e);if(t||f(e)){const n=e.getTextContentSize();if(t&&n>=c)return e.select(c,c),!0;c-=n}return!1}))}(r,(()=>{const t=d(r);if(!O(t)||!t.isAttached())return!1;const o=t.getTextContent(),i=se(n.tokenize(o,t.getLanguage()||n.defaultLanguage)),s=function(e,t){let n=0;for(;n<e.length&&le(e[n],t[n]);)n++;const r=e.length,o=t.length,i=Math.min(r,o)-n;let s=0;for(;s<i;)if(s++,!le(e[r-s],t[o-s])){s--;break}const l=n,c=r-s,u=t.slice(n,o-s);return{from:l,nodesForReplacement:u,to:c}}(t.getChildren(),i),{from:l,to:c,nodesForReplacement:u}=s;return!(l===c&&!u.length)&&(e.splice(l,c-l,u),!0)}))}),{onUpdate:()=>{oe.delete(r)},skipTransforms:!0}))}function se(e,t){const n=[];for(const r of e)if("string"==typeof r){const e=r.split(/(\n|\t)/),o=e.length;for(let r=0;r<o;r++){const o=e[r];"\n"===o||"\r\n"===o?n.push(g()):"\t"===o?n.push(u()):o.length>0&&n.push(G(o,t))}}else{const{content:e}=r;"string"==typeof e?n.push(...se([e],r.type)):Array.isArray(e)&&n.push(...se(e,r.type))}return n}function le(e,t){return V(e)&&V(t)&&e.__text===t.__text&&e.__highlightType===t.__highlightType||c(e)&&c(t)||f(e)&&f(t)}function ce(e){if(!m(e))return!1;const t=e.anchor.getNode(),n=e.focus.getNode();if(t.is(n)&&O(t))return!0;const r=t.getParent();return O(r)&&r.is(n.getParent())}function ue(e){const t=e.getNodes(),n=[[]];if(1===t.length&&O(t[0]))return n;let r=n[0];for(let e=0;e<t.length;e++){const o=t[e];if(!(V(o)||c(o)||f(o)))throw Error("Expected selection to be inside CodeBlock and consisting of CodeHighlightNode, TabNode and LineBreakNode");f(o)?0!==e&&r.length>0&&(r=[],n.push(r)):r.push(o)}return n}function ge(e){const t=h();if(!m(t)||!ce(t))return!1;const n=ue(t),r=n.length;if(n.length>1){for(let t=0;t<r;t++){const r=n[t];if(r.length>0){let n=r[0];0===t&&(n=Y(n)),null!==n&&(e===x?n.insertBefore(u()):c(n)&&n.remove())}}return!0}const o=t.getNodes()[0];if(!(O(o)||V(o)||c(o)||f(o)))throw Error("Expected selection firstNode to be CodeHighlightNode or CodeTabNode");if(O(o))return e===x&&t.insertNodes([u()]),!0;const i=Y(o);if(null===i)throw Error("Expected getFirstCodeNodeOfLine to return a valid Code Node");return e===x?f(i)?i.insertAfter(u()):i.insertBefore(u()):c(i)&&i.remove(),!0}function ae(e,t){const n=h();if(!m(n))return!1;const{anchor:r,focus:o}=n,i=r.offset,s=o.offset,l=r.getNode(),u=o.getNode(),g=e===C;if(!ce(n)||!V(l)&&!c(l)||!V(u)&&!c(u))return!1;if(!t.altKey){if(n.isCollapsed()){const e=l.getParentOrThrow();if(g&&0===i&&null===l.getPreviousSibling()){if(null===e.getPreviousSibling())return e.selectPrevious(),t.preventDefault(),!0}else if(!g&&i===l.getTextContentSize()&&null===l.getNextSibling()){if(null===e.getNextSibling())return e.selectNext(),t.preventDefault(),!0}}return!1}let a,p;if(l.isBefore(u)?(a=Y(l),p=Z(u)):(a=Y(u),p=Z(l)),null==a||null==p)return!1;const d=a.getNodesBetween(p);for(let e=0;e<d.length;e++){const t=d[e];if(!V(t)&&!c(t)&&!f(t))return!1}t.preventDefault(),t.stopPropagation();const x=g?a.getPreviousSibling():p.getNextSibling();if(!f(x))return!0;const N=g?x.getPreviousSibling():x.getNextSibling();if(null==N)return!0;const y=V(N)||c(N)||f(N)?g?Y(N):Z(N):null;let v=null!=y?y:N;return x.remove(),d.forEach((e=>e.remove())),e===C?(d.forEach((e=>v.insertBefore(e))),v.insertBefore(x)):(v.insertAfter(x),v=x,d.forEach((e=>{v.insertAfter(e),v=e}))),n.setTextNodeRange(l,i,u,s),!0}function fe(e,t){const n=h();if(!m(n))return!1;const{anchor:r,focus:o}=n,i=r.getNode(),s=o.getNode(),l=e===v;if(!V(i)&&!c(i)||!V(s)&&!c(s))return!1;if(l){const e=ee(s,o.offset);if(null!==e){const{node:t,offset:r}=e;f(t)?t.selectNext(0,0):n.setTextNodeRange(t,r,t,r)}else s.getParentOrThrow().selectStart()}else{te(s).select()}return t.preventDefault(),t.stopPropagation(),!0}function pe(e,t){if(!e.hasNodes([L,Q]))throw new Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");return null==t&&(t=$),r(e.registerMutationListener(L,(t=>{e.update((()=>{for(const[n,r]of t)if("destroyed"!==r){const t=d(n);null!==t&&re(t,e)}}))})),e.registerNodeTransform(L,(n=>ie(n,e,t))),e.registerNodeTransform(a,(n=>ne(n,e,t))),e.registerNodeTransform(Q,(n=>ne(n,e,t))),e.registerCommand(T,(t=>{const n=function(e){const t=h();if(!m(t)||!ce(t))return null;const n=e?N:x,r=e?N:y;if(ue(t).length>1)return n;const o=t.getNodes()[0];if(!(O(o)||V(o)||c(o)||f(o)))throw Error("Expected selection firstNode to be CodeHighlightNode or TabNode");if(O(o))return n;const i=Y(o),s=Z(o),l=t.anchor,u=t.focus;let g,a;return u.isBefore(l)?(g=u,a=l):(g=l,a=u),null!==i&&null!==s&&g.key===i.getKey()&&0===g.offset&&a.key===s.getKey()&&a.offset===s.getTextContentSize()?n:r}(t.shiftKey);return null!==n&&(t.preventDefault(),e.dispatchCommand(n,void 0),!0)}),_),e.registerCommand(y,(()=>!!ce(h())&&(b([u()]),!0)),_),e.registerCommand(x,(e=>ge(x)),_),e.registerCommand(N,(e=>ge(N)),_),e.registerCommand(C,(e=>ae(C,e)),_),e.registerCommand(j,(e=>ae(j,e)),_),e.registerCommand(S,(e=>fe(S,e)),_),e.registerCommand(v,(e=>fe(v,e)),_))}export{G as $createCodeHighlightNode,E as $createCodeNode,V as $isCodeHighlightNode,O as $isCodeNode,K as CODE_LANGUAGE_FRIENDLY_NAME_MAP,R as CODE_LANGUAGE_MAP,Q as CodeHighlightNode,L as CodeNode,J as DEFAULT_CODE_LANGUAGE,$ as PrismTokenizer,W as getCodeLanguages,U as getDefaultCodeLanguage,te as getEndOfCodeInLine,Y as getFirstCodeNodeOfLine,q as getLanguageFriendlyName,Z as getLastCodeNodeOfLine,ee as getStartOfCodeInLine,I as normalizeCodeLang,pe as registerCodeHighlighting};
|
package/index.d.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
/** @module @lexical/code */
|
|
2
1
|
/**
|
|
3
2
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
4
3
|
*
|
|
5
4
|
* This source code is licensed under the MIT license found in the
|
|
6
5
|
* LICENSE file in the root directory of this source tree.
|
|
7
6
|
*
|
|
8
|
-
*
|
|
9
7
|
*/
|
|
10
8
|
export { getEndOfCodeInLine, getStartOfCodeInLine, PrismTokenizer, registerCodeHighlighting, } from './CodeHighlighter';
|
|
11
9
|
export { $createCodeHighlightNode, $isCodeHighlightNode, CODE_LANGUAGE_FRIENDLY_NAME_MAP, CODE_LANGUAGE_MAP, CodeHighlightNode, DEFAULT_CODE_LANGUAGE, getCodeLanguages, getDefaultCodeLanguage, getFirstCodeNodeOfLine, getLanguageFriendlyName, getLastCodeNodeOfLine, normalizeCodeLang, } from './CodeHighlightNode';
|
package/package.json
CHANGED
|
@@ -8,13 +8,12 @@
|
|
|
8
8
|
"code"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.14.
|
|
11
|
+
"version": "0.14.4",
|
|
12
12
|
"main": "LexicalCode.js",
|
|
13
|
-
"
|
|
14
|
-
"lexical": "0.14.2"
|
|
15
|
-
},
|
|
13
|
+
"types": "index.d.ts",
|
|
16
14
|
"dependencies": {
|
|
17
|
-
"@lexical/utils": "0.14.
|
|
15
|
+
"@lexical/utils": "0.14.4",
|
|
16
|
+
"lexical": "0.14.4",
|
|
18
17
|
"prismjs": "^1.27.0"
|
|
19
18
|
},
|
|
20
19
|
"repository": {
|
|
@@ -25,6 +24,23 @@
|
|
|
25
24
|
"devDependencies": {
|
|
26
25
|
"@types/prismjs": "^1.26.0"
|
|
27
26
|
},
|
|
28
|
-
"module": "LexicalCode.
|
|
29
|
-
"sideEffects": false
|
|
27
|
+
"module": "LexicalCode.mjs",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./index.d.ts",
|
|
33
|
+
"development": "./LexicalCode.dev.mjs",
|
|
34
|
+
"production": "./LexicalCode.prod.mjs",
|
|
35
|
+
"node": "./LexicalCode.node.mjs",
|
|
36
|
+
"default": "./LexicalCode.mjs"
|
|
37
|
+
},
|
|
38
|
+
"require": {
|
|
39
|
+
"types": "./index.d.ts",
|
|
40
|
+
"development": "./LexicalCode.dev.js",
|
|
41
|
+
"production": "./LexicalCode.prod.js",
|
|
42
|
+
"default": "./LexicalCode.js"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
30
46
|
}
|
package/LexicalCode.prod.esm.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
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
|
-
import*as e from"prismjs";import"prismjs/components/prism-clike";import"prismjs/components/prism-javascript";import"prismjs/components/prism-markup";import"prismjs/components/prism-markdown";import"prismjs/components/prism-c";import"prismjs/components/prism-css";import"prismjs/components/prism-objectivec";import"prismjs/components/prism-sql";import"prismjs/components/prism-python";import"prismjs/components/prism-rust";import"prismjs/components/prism-swift";import"prismjs/components/prism-typescript";import"prismjs/components/prism-java";import"prismjs/components/prism-cpp";import{addClassNamesToElement as t,isHTMLElement as n,removeClassNamesFromElement as r,mergeRegister as o}from"@lexical/utils";import{ElementNode as i,$applyNodeReplacement as s,$createParagraphNode as l,$isTextNode as c,$isTabNode as u,$createTabNode as a,$createLineBreakNode as g,TextNode as f,$isLineBreakNode as p,$getNodeByKey as h,KEY_TAB_COMMAND as d,COMMAND_PRIORITY_LOW as m,INSERT_TAB_COMMAND as x,$getSelection as N,$insertNodes as y,INDENT_CONTENT_COMMAND as C,OUTDENT_CONTENT_COMMAND as v,KEY_ARROW_UP_COMMAND as T,KEY_ARROW_DOWN_COMMAND as _,MOVE_TO_END as b,MOVE_TO_START as S,$createTextNode as j,$isRangeSelection as k}from"lexical";const w=t=>null!=t&&e.languages.hasOwnProperty(t)?t:void 0;function L(e,t){for(const r of e.childNodes){if(n(r)&&r.tagName===t)return!0;L(r,t)}return!1}const P="data-highlight-language";class E extends i{static getType(){return"code"}static clone(e){return new E(e.__language,e.__key)}constructor(e,t){super(t),this.__language=w(e)}createDOM(e){const n=document.createElement("code");t(n,e.theme.code),n.setAttribute("spellcheck","false");const r=this.getLanguage();return r&&n.setAttribute(P,r),n}updateDOM(e,t,n){const r=this.__language,o=e.__language;return r?r!==o&&t.setAttribute(P,r):o&&t.removeAttribute(P),!1}exportDOM(e){const n=document.createElement("pre");t(n,e._config.theme.code),n.setAttribute("spellcheck","false");const r=this.getLanguage();return r&&n.setAttribute(P,r),{element:n}}static importDOM(){return{code:e=>null!=e.textContent&&(/\r?\n/.test(e.textContent)||L(e,"BR"))?{conversion:D,priority:1}:null,div:e=>({conversion:M,priority:1}),pre:e=>({conversion:D,priority:0}),table:e=>J(e)?{conversion:B,priority:3}:null,td:e=>{const t=e,n=t.closest("table");return t.classList.contains("js-file-line")?{conversion:H,priority:3}:n&&J(n)?{conversion:z,priority:3}:null},tr:e=>{const t=e.closest("table");return t&&J(t)?{conversion:z,priority:3}:null}}}static importJSON(e){const t=O(e.language);return t.setFormat(e.format),t.setIndent(e.indent),t.setDirection(e.direction),t}exportJSON(){return{...super.exportJSON(),language:this.getLanguage(),type:"code",version:1}}insertNewAfter(e,t=!0){const n=this.getChildren(),r=n.length;if(r>=2&&"\n"===n[r-1].getTextContent()&&"\n"===n[r-2].getTextContent()&&e.isCollapsed()&&e.anchor.key===this.__key&&e.anchor.offset===r){n[r-1].remove(),n[r-2].remove();const e=l();return this.insertAfter(e,t),e}const{anchor:o,focus:i}=e,s=(o.isBefore(i)?o:i).getNode();if(c(s)){let e=Z(s);const t=[];for(;;)if(u(e))t.push(a()),e=e.getNextSibling();else{if(!Y(e))break;{let n=0;const r=e.getTextContent(),o=e.getTextContentSize();for(;n<o&&" "===r[n];)n++;if(0!==n&&t.push(V(" ".repeat(n))),n!==o)break;e=e.getNextSibling()}}const n=s.splitText(o.offset)[0],r=0===o.offset?0:1,i=n.getIndexWithinParent()+r,l=s.getParentOrThrow(),c=[g(),...t];l.splice(i,0,c);const f=t[t.length-1];f?f.select():0===o.offset?n.selectPrevious():n.getNextSibling().selectNext(0,0)}if(A(s)){const{offset:t}=e.anchor;s.splice(t,0,[g()]),s.select(t+1,t+1)}return null}canIndent(){return!1}collapseAtStart(){const e=l();return this.getChildren().forEach((t=>e.append(t))),this.replace(e),!0}setLanguage(e){this.getWritable().__language=w(e)}getLanguage(){return this.getLatest().__language}}function O(e){return s(new E(e))}function A(e){return e instanceof E}function D(e){let t;return n(e)&&(t=e.getAttribute(P)),{node:O(t)}}function M(e){const t=e,n=F(t);return n||function(e){let t=e.parentElement;for(;null!==t;){if(F(t))return!0;t=t.parentElement}return!1}(t)?{after:t=>{const n=e.parentNode;return null!=n&&e!==n.lastChild&&t.push(g()),t},node:n?O():null}:{node:null}}function B(){return{node:O()}}function z(){return{node:null}}function H(e){const t=e;return{after:e=>(t.parentNode&&t.parentNode.nextSibling&&e.push(g()),e),node:null}}function F(e){return null!==e.style.fontFamily.match("monospace")}function J(e){return e.classList.contains("js-file-line-container")}const K="javascript",R={c:"C",clike:"C-like",cpp:"C++",css:"CSS",html:"HTML",java:"Java",js:"JavaScript",markdown:"Markdown",objc:"Objective-C",plain:"Plain Text",py:"Python",rust:"Rust",sql:"SQL",swift:"Swift",typescript:"TypeScript",xml:"XML"},I={cpp:"cpp",java:"java",javascript:"js",md:"markdown",plaintext:"plain",python:"py",text:"plain",ts:"typescript"};function q(e){return I[e]||e}function U(e){const t=q(e);return R[t]||t}const W=()=>K,Q=()=>Object.keys(e.languages).filter((t=>"function"!=typeof e.languages[t])).sort();class X extends f{constructor(e,t,n){super(e,n),this.__highlightType=t}static getType(){return"code-highlight"}static clone(e){return new X(e.__text,e.__highlightType||void 0,e.__key)}getHighlightType(){return this.getLatest().__highlightType}canHaveFormat(){return!1}createDOM(e){const n=super.createDOM(e),r=G(e.theme,this.__highlightType);return t(n,r),n}updateDOM(e,n,o){const i=super.updateDOM(e,n,o),s=G(o.theme,e.__highlightType),l=G(o.theme,this.__highlightType);return s!==l&&(s&&r(n,s),l&&t(n,l)),i}static importJSON(e){const t=V(e.text,e.highlightType);return t.setFormat(e.format),t.setDetail(e.detail),t.setMode(e.mode),t.setStyle(e.style),t}exportJSON(){return{...super.exportJSON(),highlightType:this.getHighlightType(),type:"code-highlight",version:1}}setFormat(e){return this}isParentRequired(){return!0}createParentElementNode(){return O()}}function G(e,t){return t&&e&&e.codeHighlight&&e.codeHighlight[t]}function V(e,t){return s(new X(e,t))}function Y(e){return e instanceof X}function Z(e){let t=e,n=e;for(;Y(n)||u(n);)t=n,n=n.getPreviousSibling();return t}function $(e){let t=e,n=e;for(;Y(n)||u(n);)t=n,n=n.getNextSibling();return t}const ee={defaultLanguage:K,tokenize(t,n){return e.tokenize(t,e.languages[n||""]||e.languages[this.defaultLanguage])}};function te(e,t){let n=null,r=null,o=e,i=t,s=e.getTextContent();for(;;){if(0===i){if(o=o.getPreviousSibling(),null===o)break;if(!(Y(o)||u(o)||p(o)))throw Error("Expected a valid Code Node: CodeHighlightNode, TabNode, LineBreakNode");if(p(o)){n={node:o,offset:1};break}i=Math.max(0,o.getTextContentSize()-1),s=o.getTextContent()}else i--;const e=s[i];Y(o)&&" "!==e&&(r={node:o,offset:i})}if(null!==r)return r;let l=null;if(t<e.getTextContentSize())Y(e)&&(l=e.getTextContent()[t]);else{const t=e.getNextSibling();Y(t)&&(l=t.getTextContent()[0])}if(null!==l&&" "!==l)return n;{const r=function(e,t){let n=e,r=t,o=e.getTextContent(),i=e.getTextContentSize();for(;;){if(!Y(n)||r===i){if(n=n.getNextSibling(),null===n||p(n))return null;Y(n)&&(r=0,o=n.getTextContent(),i=n.getTextContentSize())}if(Y(n)){if(" "!==o[r])return{node:n,offset:r};r++}}}(e,t);return null!==r?r:n}}function ne(e){const t=$(e);if(p(t))throw Error("Unexpected lineBreakNode in getEndOfCodeInLine");return t}function re(e,t,n){const r=e.getParent();A(r)?se(r,t,n):Y(e)&&e.replace(j(e.__text))}function oe(e,t){const n=t.getElementByKey(e.getKey());if(null===n)return;const r=e.getChildren(),o=r.length;if(o===n.__cachedChildrenLength)return;n.__cachedChildrenLength=o;let i="1",s=1;for(let e=0;e<o;e++)p(r[e])&&(i+="\n"+ ++s);n.setAttribute("data-gutter",i)}const ie=new Set;function se(e,t,n){const r=e.getKey();ie.has(r)||(ie.add(r),void 0===e.getLanguage()&&e.setLanguage(n.defaultLanguage),t.update((()=>{!function(e,t){const n=h(e);if(!A(n)||!n.isAttached())return;const r=N();if(!k(r))return void t();const o=r.anchor,i=o.offset,s="element"===o.type&&p(n.getChildAtIndex(o.offset-1));let l=0;if(!s){const e=o.getNode();l=i+e.getPreviousSiblings().reduce(((e,t)=>e+t.getTextContentSize()),0)}if(!t())return;if(s)return void o.getNode().select(i,i);n.getChildren().some((e=>{const t=c(e);if(t||p(e)){const n=e.getTextContentSize();if(t&&n>=l)return e.select(l,l),!0;l-=n}return!1}))}(r,(()=>{const t=h(r);if(!A(t)||!t.isAttached())return!1;const o=t.getTextContent(),i=le(n.tokenize(o,t.getLanguage()||n.defaultLanguage)),s=function(e,t){let n=0;for(;n<e.length&&ce(e[n],t[n]);)n++;const r=e.length,o=t.length,i=Math.min(r,o)-n;let s=0;for(;s<i;)if(s++,!ce(e[r-s],t[o-s])){s--;break}const l=n,c=r-s,u=t.slice(n,o-s);return{from:l,nodesForReplacement:u,to:c}}(t.getChildren(),i),{from:l,to:c,nodesForReplacement:u}=s;return!(l===c&&!u.length)&&(e.splice(l,c-l,u),!0)}))}),{onUpdate:()=>{ie.delete(r)},skipTransforms:!0}))}function le(e,t){const n=[];for(const r of e)if("string"==typeof r){const e=r.split(/(\n|\t)/),o=e.length;for(let r=0;r<o;r++){const o=e[r];"\n"===o||"\r\n"===o?n.push(g()):"\t"===o?n.push(a()):o.length>0&&n.push(V(o,t))}}else{const{content:e}=r;"string"==typeof e?n.push(...le([e],r.type)):Array.isArray(e)&&n.push(...le(e,r.type))}return n}function ce(e,t){return Y(e)&&Y(t)&&e.__text===t.__text&&e.__highlightType===t.__highlightType||u(e)&&u(t)||p(e)&&p(t)}function ue(e){if(!k(e))return!1;const t=e.anchor.getNode(),n=e.focus.getNode();if(t.is(n)&&A(t))return!0;const r=t.getParent();return A(r)&&r.is(n.getParent())}function ae(e){const t=e.getNodes(),n=[[]];if(1===t.length&&A(t[0]))return n;let r=n[0];for(let e=0;e<t.length;e++){const o=t[e];if(!(Y(o)||u(o)||p(o)))throw Error("Expected selection to be inside CodeBlock and consisting of CodeHighlightNode, TabNode and LineBreakNode");p(o)?0!==e&&r.length>0&&(r=[],n.push(r)):r.push(o)}return n}function ge(e){const t=N();if(!k(t)||!ue(t))return!1;const n=ae(t),r=n.length;if(n.length>1){for(let t=0;t<r;t++){const r=n[t];if(r.length>0){let n=r[0];0===t&&(n=Z(n)),null!==n&&(e===C?n.insertBefore(a()):u(n)&&n.remove())}}return!0}const o=t.getNodes()[0];if(!(A(o)||Y(o)||u(o)||p(o)))throw Error("Expected selection firstNode to be CodeHighlightNode or CodeTabNode");if(A(o))return e===C&&t.insertNodes([a()]),!0;const i=Z(o);if(null===i)throw Error("Expected getFirstCodeNodeOfLine to return a valid Code Node");return e===C?p(i)?i.insertAfter(a()):i.insertBefore(a()):u(i)&&i.remove(),!0}function fe(e,t){const n=N();if(!k(n))return!1;const{anchor:r,focus:o}=n,i=r.offset,s=o.offset,l=r.getNode(),c=o.getNode(),a=e===T;if(!ue(n)||!Y(l)&&!u(l)||!Y(c)&&!u(c))return!1;if(!t.altKey){if(n.isCollapsed()){const e=l.getParentOrThrow();if(a&&0===i&&null===l.getPreviousSibling()){if(null===e.getPreviousSibling())return e.selectPrevious(),t.preventDefault(),!0}else if(!a&&i===l.getTextContentSize()&&null===l.getNextSibling()){if(null===e.getNextSibling())return e.selectNext(),t.preventDefault(),!0}}return!1}let g,f;if(l.isBefore(c)?(g=Z(l),f=$(c)):(g=Z(c),f=$(l)),null==g||null==f)return!1;const h=g.getNodesBetween(f);for(let e=0;e<h.length;e++){const t=h[e];if(!Y(t)&&!u(t)&&!p(t))return!1}t.preventDefault(),t.stopPropagation();const d=a?g.getPreviousSibling():f.getNextSibling();if(!p(d))return!0;const m=a?d.getPreviousSibling():d.getNextSibling();if(null==m)return!0;const x=Y(m)||u(m)||p(m)?a?Z(m):$(m):null;let y=null!=x?x:m;return d.remove(),h.forEach((e=>e.remove())),e===T?(h.forEach((e=>y.insertBefore(e))),y.insertBefore(d)):(y.insertAfter(d),y=d,h.forEach((e=>{y.insertAfter(e),y=e}))),n.setTextNodeRange(l,i,c,s),!0}function pe(e,t){const n=N();if(!k(n))return!1;const{anchor:r,focus:o}=n,i=r.getNode(),s=o.getNode(),l=e===S;if(!Y(i)&&!u(i)||!Y(s)&&!u(s))return!1;if(l){const e=te(s,o.offset);if(null!==e){const{node:t,offset:r}=e;p(t)?t.selectNext(0,0):n.setTextNodeRange(t,r,t,r)}else s.getParentOrThrow().selectStart()}else{ne(s).select()}return t.preventDefault(),t.stopPropagation(),!0}function he(e,t){if(!e.hasNodes([E,X]))throw new Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");return null==t&&(t=ee),o(e.registerMutationListener(E,(t=>{e.update((()=>{for(const[n,r]of t)if("destroyed"!==r){const t=h(n);null!==t&&oe(t,e)}}))})),e.registerNodeTransform(E,(n=>se(n,e,t))),e.registerNodeTransform(f,(n=>re(n,e,t))),e.registerNodeTransform(X,(n=>re(n,e,t))),e.registerCommand(d,(t=>{const n=function(e){const t=N();if(!k(t)||!ue(t))return null;const n=e?v:C,r=e?v:x;if(ae(t).length>1)return n;const o=t.getNodes()[0];if(!(A(o)||Y(o)||u(o)||p(o)))throw Error("Expected selection firstNode to be CodeHighlightNode or TabNode");if(A(o))return n;const i=Z(o),s=$(o),l=t.anchor,c=t.focus;let a,g;return c.isBefore(l)?(a=c,g=l):(a=l,g=c),null!==i&&null!==s&&a.key===i.getKey()&&0===a.offset&&g.key===s.getKey()&&g.offset===s.getTextContentSize()?n:r}(t.shiftKey);return null!==n&&(t.preventDefault(),e.dispatchCommand(n,void 0),!0)}),m),e.registerCommand(x,(()=>!!ue(N())&&(y([a()]),!0)),m),e.registerCommand(C,(e=>ge(C)),m),e.registerCommand(v,(e=>ge(v)),m),e.registerCommand(T,(e=>fe(T,e)),m),e.registerCommand(_,(e=>fe(_,e)),m),e.registerCommand(b,(e=>pe(b,e)),m),e.registerCommand(S,(e=>pe(S,e)),m))}export{V as $createCodeHighlightNode,O as $createCodeNode,Y as $isCodeHighlightNode,A as $isCodeNode,R as CODE_LANGUAGE_FRIENDLY_NAME_MAP,I as CODE_LANGUAGE_MAP,X as CodeHighlightNode,E as CodeNode,K as DEFAULT_CODE_LANGUAGE,ee as PrismTokenizer,Q as getCodeLanguages,W as getDefaultCodeLanguage,ne as getEndOfCodeInLine,Z as getFirstCodeNodeOfLine,U as getLanguageFriendlyName,$ as getLastCodeNodeOfLine,te as getStartOfCodeInLine,q as normalizeCodeLang,he as registerCodeHighlighting};
|