@react-fabric/lexical 1.0.0-beta.57 → 1.0.0-beta.59
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/css/_lexical.css +4 -0
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +33 -1
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +11 -11
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +525 -194
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +26 -23
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +3 -3
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +95 -38
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +11 -10
- package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +387 -124
- package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +22 -18
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +54 -29
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +10 -10
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +107 -8
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +4 -0
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +23 -20
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +420 -137
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +50 -45
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +61 -65
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +2 -0
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +47 -46
- package/dist/cjs/packages/lexical/src/hooks/useFormatWatcher.js +3 -1
- package/dist/cjs/packages/lexical/src/plugins/ToolbarPlugin.js +20 -10
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +34 -2
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +524 -196
- package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +4 -4
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +96 -40
- package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +387 -127
- package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +5 -4
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +56 -31
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +106 -10
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +3 -1
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +417 -139
- package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +62 -67
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +2 -1
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +3 -3
- package/dist/esm/packages/lexical/src/hooks/useFormatWatcher.js +3 -1
- package/dist/esm/packages/lexical/src/plugins/ToolbarPlugin.js +20 -10
- package/package.json +12 -12
- package/src/hooks/useFormatWatcher.ts +3 -1
- package/src/plugins/ToolbarPlugin.tsx +5 -10
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import { addClassNamesToElement, removeClassNamesFromElement, mergeRegister, isHTMLElement, $getAdjacentCaret } from '../utils/LexicalUtils.js';
|
|
4
|
-
import { ElementNode, $createParagraphNode, $isTextNode, $isTabNode, $createTabNode, $createLineBreakNode, $
|
|
4
|
+
import { ElementNode, $createParagraphNode, $isTextNode, $isTabNode, $createTabNode, $createLineBreakNode, $create, TextNode, $applyNodeReplacement, $isLineBreakNode, $getNodeByKey, KEY_TAB_COMMAND, COMMAND_PRIORITY_LOW, INSERT_TAB_COMMAND, $getSelection, $insertNodes, INDENT_CONTENT_COMMAND, OUTDENT_CONTENT_COMMAND, KEY_ARROW_UP_COMMAND, $isRangeSelection, KEY_ARROW_DOWN_COMMAND, MOVE_TO_START, MOVE_TO_END, $getSiblingCaret, $createTextNode, $createPoint, $setSelectionFromCaretRange, $getCaretRangeInDirection, $getCaretRange, $getTextPointCaret, $normalizeCaret } from 'lexical';
|
|
5
5
|
import '../../prismjs/prism.js';
|
|
6
6
|
import '../../prismjs/components/prism-clike.js';
|
|
7
7
|
import '../../prismjs/components/prism-javascript.js';
|
|
@@ -50,24 +50,8 @@ function formatDevErrorMessage(message) {
|
|
|
50
50
|
*
|
|
51
51
|
*/
|
|
52
52
|
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
57
|
-
*
|
|
58
|
-
* This source code is licensed under the MIT license found in the
|
|
59
|
-
* LICENSE file in the root directory of this source tree.
|
|
60
|
-
*
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
const isLanguageSupportedByPrism = language => {
|
|
64
|
-
try {
|
|
65
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
66
|
-
return language ? Prism.languages.hasOwnProperty(language) : false;
|
|
67
|
-
} catch (_unused) {
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
};
|
|
53
|
+
const DEFAULT_CODE_LANGUAGE = 'javascript';
|
|
54
|
+
const getDefaultCodeLanguage = () => DEFAULT_CODE_LANGUAGE;
|
|
71
55
|
function hasChildDOMNodeTag(node, tagName) {
|
|
72
56
|
for (const child of node.childNodes) {
|
|
73
57
|
if (isHTMLElement(child) && child.tagName === tagName) {
|
|
@@ -79,6 +63,7 @@ function hasChildDOMNodeTag(node, tagName) {
|
|
|
79
63
|
}
|
|
80
64
|
const LANGUAGE_DATA_ATTRIBUTE = 'data-language';
|
|
81
65
|
const HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE = 'data-highlight-language';
|
|
66
|
+
const THEME_DATA_ATTRIBUTE = 'data-theme';
|
|
82
67
|
|
|
83
68
|
/** @noInheritDoc */
|
|
84
69
|
class CodeNode extends ElementNode {
|
|
@@ -86,6 +71,8 @@ class CodeNode extends ElementNode {
|
|
|
86
71
|
|
|
87
72
|
/** @internal */
|
|
88
73
|
|
|
74
|
+
/** @internal */
|
|
75
|
+
|
|
89
76
|
static getType() {
|
|
90
77
|
return 'code';
|
|
91
78
|
}
|
|
@@ -95,7 +82,14 @@ class CodeNode extends ElementNode {
|
|
|
95
82
|
constructor(language, key) {
|
|
96
83
|
super(key);
|
|
97
84
|
this.__language = language || undefined;
|
|
98
|
-
this.__isSyntaxHighlightSupported =
|
|
85
|
+
this.__isSyntaxHighlightSupported = false;
|
|
86
|
+
this.__theme = undefined;
|
|
87
|
+
}
|
|
88
|
+
afterCloneFrom(prevNode) {
|
|
89
|
+
super.afterCloneFrom(prevNode);
|
|
90
|
+
this.__language = prevNode.__language;
|
|
91
|
+
this.__theme = prevNode.__theme;
|
|
92
|
+
this.__isSyntaxHighlightSupported = prevNode.__isSyntaxHighlightSupported;
|
|
99
93
|
}
|
|
100
94
|
|
|
101
95
|
// View
|
|
@@ -110,6 +104,14 @@ class CodeNode extends ElementNode {
|
|
|
110
104
|
element.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
|
|
111
105
|
}
|
|
112
106
|
}
|
|
107
|
+
const theme = this.getTheme();
|
|
108
|
+
if (theme) {
|
|
109
|
+
element.setAttribute(THEME_DATA_ATTRIBUTE, theme);
|
|
110
|
+
}
|
|
111
|
+
const style = this.getStyle();
|
|
112
|
+
if (style) {
|
|
113
|
+
element.setAttribute('style', style);
|
|
114
|
+
}
|
|
113
115
|
return element;
|
|
114
116
|
}
|
|
115
117
|
updateDOM(prevNode, dom, config) {
|
|
@@ -118,15 +120,40 @@ class CodeNode extends ElementNode {
|
|
|
118
120
|
if (language) {
|
|
119
121
|
if (language !== prevLanguage) {
|
|
120
122
|
dom.setAttribute(LANGUAGE_DATA_ATTRIBUTE, language);
|
|
121
|
-
if (this.__isSyntaxHighlightSupported) {
|
|
122
|
-
dom.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
|
|
123
|
-
}
|
|
124
123
|
}
|
|
125
124
|
} else if (prevLanguage) {
|
|
126
125
|
dom.removeAttribute(LANGUAGE_DATA_ATTRIBUTE);
|
|
127
|
-
|
|
126
|
+
}
|
|
127
|
+
const isSyntaxHighlightSupported = this.__isSyntaxHighlightSupported;
|
|
128
|
+
const prevIsSyntaxHighlightSupported = prevNode.__isSyntaxHighlightSupported;
|
|
129
|
+
if (prevIsSyntaxHighlightSupported && prevLanguage) {
|
|
130
|
+
if (isSyntaxHighlightSupported && language) {
|
|
131
|
+
if (language !== prevLanguage) {
|
|
132
|
+
dom.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
128
135
|
dom.removeAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE);
|
|
129
136
|
}
|
|
137
|
+
} else if (isSyntaxHighlightSupported && language) {
|
|
138
|
+
dom.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
|
|
139
|
+
}
|
|
140
|
+
const theme = this.__theme;
|
|
141
|
+
const prevTheme = prevNode.__theme;
|
|
142
|
+
if (theme) {
|
|
143
|
+
if (theme !== prevTheme) {
|
|
144
|
+
dom.setAttribute(THEME_DATA_ATTRIBUTE, theme);
|
|
145
|
+
}
|
|
146
|
+
} else if (prevTheme) {
|
|
147
|
+
dom.removeAttribute(THEME_DATA_ATTRIBUTE);
|
|
148
|
+
}
|
|
149
|
+
const style = this.__style;
|
|
150
|
+
const prevStyle = prevNode.__style;
|
|
151
|
+
if (style) {
|
|
152
|
+
if (style !== prevStyle) {
|
|
153
|
+
dom.setAttribute('style', style);
|
|
154
|
+
}
|
|
155
|
+
} else if (prevStyle) {
|
|
156
|
+
dom.removeAttribute('style');
|
|
130
157
|
}
|
|
131
158
|
return false;
|
|
132
159
|
}
|
|
@@ -141,6 +168,14 @@ class CodeNode extends ElementNode {
|
|
|
141
168
|
element.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
|
|
142
169
|
}
|
|
143
170
|
}
|
|
171
|
+
const theme = this.getTheme();
|
|
172
|
+
if (theme) {
|
|
173
|
+
element.setAttribute(THEME_DATA_ATTRIBUTE, theme);
|
|
174
|
+
}
|
|
175
|
+
const style = this.getStyle();
|
|
176
|
+
if (style) {
|
|
177
|
+
element.setAttribute('style', style);
|
|
178
|
+
}
|
|
144
179
|
return {
|
|
145
180
|
element
|
|
146
181
|
};
|
|
@@ -208,12 +243,13 @@ class CodeNode extends ElementNode {
|
|
|
208
243
|
return $createCodeNode().updateFromJSON(serializedNode);
|
|
209
244
|
}
|
|
210
245
|
updateFromJSON(serializedNode) {
|
|
211
|
-
return super.updateFromJSON(serializedNode).setLanguage(serializedNode.language);
|
|
246
|
+
return super.updateFromJSON(serializedNode).setLanguage(serializedNode.language).setTheme(serializedNode.theme);
|
|
212
247
|
}
|
|
213
248
|
exportJSON() {
|
|
214
249
|
return {
|
|
215
250
|
...super.exportJSON(),
|
|
216
|
-
language: this.getLanguage()
|
|
251
|
+
language: this.getLanguage(),
|
|
252
|
+
theme: this.getTheme()
|
|
217
253
|
};
|
|
218
254
|
}
|
|
219
255
|
|
|
@@ -301,18 +337,30 @@ class CodeNode extends ElementNode {
|
|
|
301
337
|
setLanguage(language) {
|
|
302
338
|
const writable = this.getWritable();
|
|
303
339
|
writable.__language = language || undefined;
|
|
304
|
-
writable.__isSyntaxHighlightSupported = isLanguageSupportedByPrism(language);
|
|
305
340
|
return writable;
|
|
306
341
|
}
|
|
307
342
|
getLanguage() {
|
|
308
343
|
return this.getLatest().__language;
|
|
309
344
|
}
|
|
345
|
+
setIsSyntaxHighlightSupported(isSupported) {
|
|
346
|
+
const writable = this.getWritable();
|
|
347
|
+
writable.__isSyntaxHighlightSupported = isSupported;
|
|
348
|
+
return writable;
|
|
349
|
+
}
|
|
310
350
|
getIsSyntaxHighlightSupported() {
|
|
311
351
|
return this.getLatest().__isSyntaxHighlightSupported;
|
|
312
352
|
}
|
|
353
|
+
setTheme(theme) {
|
|
354
|
+
const writable = this.getWritable();
|
|
355
|
+
writable.__theme = theme || undefined;
|
|
356
|
+
return writable;
|
|
357
|
+
}
|
|
358
|
+
getTheme() {
|
|
359
|
+
return this.getLatest().__theme;
|
|
360
|
+
}
|
|
313
361
|
}
|
|
314
|
-
function $createCodeNode(language) {
|
|
315
|
-
return $
|
|
362
|
+
function $createCodeNode(language, theme) {
|
|
363
|
+
return $create(CodeNode).setLanguage(language).setTheme(theme);
|
|
316
364
|
}
|
|
317
365
|
function $isCodeNode(node) {
|
|
318
366
|
return node instanceof CodeNode;
|
|
@@ -374,49 +422,6 @@ function isGitHubCodeTable(table) {
|
|
|
374
422
|
*
|
|
375
423
|
*/
|
|
376
424
|
|
|
377
|
-
const DEFAULT_CODE_LANGUAGE = 'javascript';
|
|
378
|
-
const CODE_LANGUAGE_FRIENDLY_NAME_MAP = {
|
|
379
|
-
c: 'C',
|
|
380
|
-
clike: 'C-like',
|
|
381
|
-
cpp: 'C++',
|
|
382
|
-
css: 'CSS',
|
|
383
|
-
html: 'HTML',
|
|
384
|
-
java: 'Java',
|
|
385
|
-
js: 'JavaScript',
|
|
386
|
-
markdown: 'Markdown',
|
|
387
|
-
objc: 'Objective-C',
|
|
388
|
-
plain: 'Plain Text',
|
|
389
|
-
powershell: 'PowerShell',
|
|
390
|
-
py: 'Python',
|
|
391
|
-
rust: 'Rust',
|
|
392
|
-
sql: 'SQL',
|
|
393
|
-
swift: 'Swift',
|
|
394
|
-
typescript: 'TypeScript',
|
|
395
|
-
xml: 'XML'
|
|
396
|
-
};
|
|
397
|
-
const CODE_LANGUAGE_MAP = {
|
|
398
|
-
cpp: 'cpp',
|
|
399
|
-
java: 'java',
|
|
400
|
-
javascript: 'js',
|
|
401
|
-
md: 'markdown',
|
|
402
|
-
plaintext: 'plain',
|
|
403
|
-
python: 'py',
|
|
404
|
-
text: 'plain',
|
|
405
|
-
ts: 'typescript'
|
|
406
|
-
};
|
|
407
|
-
function normalizeCodeLang(lang) {
|
|
408
|
-
return CODE_LANGUAGE_MAP[lang] || lang;
|
|
409
|
-
}
|
|
410
|
-
function getLanguageFriendlyName(lang) {
|
|
411
|
-
const _lang = normalizeCodeLang(lang);
|
|
412
|
-
return CODE_LANGUAGE_FRIENDLY_NAME_MAP[_lang] || _lang;
|
|
413
|
-
}
|
|
414
|
-
const getDefaultCodeLanguage = () => DEFAULT_CODE_LANGUAGE;
|
|
415
|
-
const getCodeLanguages = () => Object.keys(Prism.languages).filter(
|
|
416
|
-
// Prism has several language helpers mixed into languages object
|
|
417
|
-
// so filtering them out here to get langs list
|
|
418
|
-
language => typeof Prism.languages[language] !== 'function').sort();
|
|
419
|
-
|
|
420
425
|
/** @noInheritDoc */
|
|
421
426
|
class CodeHighlightNode extends TextNode {
|
|
422
427
|
/** @internal */
|
|
@@ -496,6 +501,7 @@ function $createCodeHighlightNode(text = '', highlightType) {
|
|
|
496
501
|
function $isCodeHighlightNode(node) {
|
|
497
502
|
return node instanceof CodeHighlightNode;
|
|
498
503
|
}
|
|
504
|
+
|
|
499
505
|
function $getLastMatchingCodeNode(anchor, direction) {
|
|
500
506
|
let matchingNode = anchor;
|
|
501
507
|
for (let caret = $getSiblingCaret(anchor, direction); caret && ($isCodeHighlightNode(caret.origin) || $isTabNode(caret.origin)); caret = $getAdjacentCaret(caret)) {
|
|
@@ -509,13 +515,6 @@ function $getFirstCodeNodeOfLine(anchor) {
|
|
|
509
515
|
function $getLastCodeNodeOfLine(anchor) {
|
|
510
516
|
return $getLastMatchingCodeNode(anchor, 'next');
|
|
511
517
|
}
|
|
512
|
-
|
|
513
|
-
const PrismTokenizer = {
|
|
514
|
-
defaultLanguage: DEFAULT_CODE_LANGUAGE,
|
|
515
|
-
tokenize(code, language) {
|
|
516
|
-
return Prism.tokenize(code, Prism.languages[language || ''] || Prism.languages[this.defaultLanguage]);
|
|
517
|
-
}
|
|
518
|
-
};
|
|
519
518
|
function $getStartOfCodeInLine(anchor, offset) {
|
|
520
519
|
let last = null;
|
|
521
520
|
let lastNonBlank = null;
|
|
@@ -616,6 +615,290 @@ function $getEndOfCodeInLine(anchor) {
|
|
|
616
615
|
}
|
|
617
616
|
return lastNode;
|
|
618
617
|
}
|
|
618
|
+
|
|
619
|
+
(function (Prism) {
|
|
620
|
+
|
|
621
|
+
Prism.languages.diff = {
|
|
622
|
+
'coord': [
|
|
623
|
+
// Match all kinds of coord lines (prefixed by "+++", "---" or "***").
|
|
624
|
+
/^(?:\*{3}|-{3}|\+{3}).*$/m,
|
|
625
|
+
// Match "@@ ... @@" coord lines in unified diff.
|
|
626
|
+
/^@@.*@@$/m,
|
|
627
|
+
// Match coord lines in normal diff (starts with a number).
|
|
628
|
+
/^\d.*$/m
|
|
629
|
+
]
|
|
630
|
+
|
|
631
|
+
// deleted, inserted, unchanged, diff
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* A map from the name of a block to its line prefix.
|
|
636
|
+
*
|
|
637
|
+
* @type {Object<string, string>}
|
|
638
|
+
*/
|
|
639
|
+
var PREFIXES = {
|
|
640
|
+
'deleted-sign': '-',
|
|
641
|
+
'deleted-arrow': '<',
|
|
642
|
+
'inserted-sign': '+',
|
|
643
|
+
'inserted-arrow': '>',
|
|
644
|
+
'unchanged': ' ',
|
|
645
|
+
'diff': '!',
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
// add a token for each prefix
|
|
649
|
+
Object.keys(PREFIXES).forEach(function (name) {
|
|
650
|
+
var prefix = PREFIXES[name];
|
|
651
|
+
|
|
652
|
+
var alias = [];
|
|
653
|
+
if (!/^\w+$/.test(name)) { // "deleted-sign" -> "deleted"
|
|
654
|
+
alias.push(/\w+/.exec(name)[0]);
|
|
655
|
+
}
|
|
656
|
+
if (name === 'diff') {
|
|
657
|
+
alias.push('bold');
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
Prism.languages.diff[name] = {
|
|
661
|
+
pattern: RegExp('^(?:[' + prefix + '].*(?:\r\n?|\n|(?![\\s\\S])))+', 'm'),
|
|
662
|
+
alias: alias,
|
|
663
|
+
inside: {
|
|
664
|
+
'line': {
|
|
665
|
+
pattern: /(.)(?=[\s\S]).*(?:\r\n?|\n)?/,
|
|
666
|
+
lookbehind: true
|
|
667
|
+
},
|
|
668
|
+
'prefix': {
|
|
669
|
+
pattern: /[\s\S]/,
|
|
670
|
+
alias: /\w+/.exec(name)[0]
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
// make prefixes available to Diff plugin
|
|
678
|
+
Object.defineProperty(Prism.languages.diff, 'PREFIXES', {
|
|
679
|
+
value: PREFIXES
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
}(Prism));
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
686
|
+
*
|
|
687
|
+
* This source code is licensed under the MIT license found in the
|
|
688
|
+
* LICENSE file in the root directory of this source tree.
|
|
689
|
+
*
|
|
690
|
+
*/
|
|
691
|
+
|
|
692
|
+
const Prism$1 = globalThis.Prism || window.Prism;
|
|
693
|
+
const CODE_LANGUAGE_FRIENDLY_NAME_MAP = {
|
|
694
|
+
c: 'C',
|
|
695
|
+
clike: 'C-like',
|
|
696
|
+
cpp: 'C++',
|
|
697
|
+
css: 'CSS',
|
|
698
|
+
html: 'HTML',
|
|
699
|
+
java: 'Java',
|
|
700
|
+
js: 'JavaScript',
|
|
701
|
+
markdown: 'Markdown',
|
|
702
|
+
objc: 'Objective-C',
|
|
703
|
+
plain: 'Plain Text',
|
|
704
|
+
powershell: 'PowerShell',
|
|
705
|
+
py: 'Python',
|
|
706
|
+
rust: 'Rust',
|
|
707
|
+
sql: 'SQL',
|
|
708
|
+
swift: 'Swift',
|
|
709
|
+
typescript: 'TypeScript',
|
|
710
|
+
xml: 'XML'
|
|
711
|
+
};
|
|
712
|
+
const CODE_LANGUAGE_MAP = {
|
|
713
|
+
cpp: 'cpp',
|
|
714
|
+
java: 'java',
|
|
715
|
+
javascript: 'js',
|
|
716
|
+
md: 'markdown',
|
|
717
|
+
plaintext: 'plain',
|
|
718
|
+
python: 'py',
|
|
719
|
+
text: 'plain',
|
|
720
|
+
ts: 'typescript'
|
|
721
|
+
};
|
|
722
|
+
function normalizeCodeLang(lang) {
|
|
723
|
+
return CODE_LANGUAGE_MAP[lang] || lang;
|
|
724
|
+
}
|
|
725
|
+
function getLanguageFriendlyName(lang) {
|
|
726
|
+
const _lang = normalizeCodeLang(lang);
|
|
727
|
+
return CODE_LANGUAGE_FRIENDLY_NAME_MAP[_lang] || _lang;
|
|
728
|
+
}
|
|
729
|
+
const getCodeLanguages = () => Object.keys(Prism$1.languages).filter(
|
|
730
|
+
// Prism has several language helpers mixed into languages object
|
|
731
|
+
// so filtering them out here to get langs list
|
|
732
|
+
language => typeof Prism$1.languages[language] !== 'function').sort();
|
|
733
|
+
function getCodeLanguageOptions() {
|
|
734
|
+
const options = [];
|
|
735
|
+
for (const [lang, friendlyName] of Object.entries(CODE_LANGUAGE_FRIENDLY_NAME_MAP)) {
|
|
736
|
+
options.push([lang, friendlyName]);
|
|
737
|
+
}
|
|
738
|
+
return options;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
// Prism has no theme support
|
|
742
|
+
function getCodeThemeOptions() {
|
|
743
|
+
const options = [];
|
|
744
|
+
return options;
|
|
745
|
+
}
|
|
746
|
+
function getDiffedLanguage(language) {
|
|
747
|
+
const DIFF_LANGUAGE_REGEX = /^diff-([\w-]+)/i;
|
|
748
|
+
const diffLanguageMatch = DIFF_LANGUAGE_REGEX.exec(language);
|
|
749
|
+
return diffLanguageMatch ? diffLanguageMatch[1] : null;
|
|
750
|
+
}
|
|
751
|
+
function isCodeLanguageLoaded(language) {
|
|
752
|
+
const diffedLanguage = getDiffedLanguage(language);
|
|
753
|
+
const langId = diffedLanguage ? diffedLanguage : language;
|
|
754
|
+
try {
|
|
755
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
756
|
+
return langId ? Prism$1.languages.hasOwnProperty(langId) : false;
|
|
757
|
+
} catch (_unused) {
|
|
758
|
+
return false;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
async function loadCodeLanguage(language, editor, codeNodeKey) {
|
|
762
|
+
// NOT IMPLEMENTED
|
|
763
|
+
}
|
|
764
|
+
function getTextContent(token) {
|
|
765
|
+
if (typeof token === 'string') {
|
|
766
|
+
return token;
|
|
767
|
+
} else if (Array.isArray(token)) {
|
|
768
|
+
return token.map(getTextContent).join('');
|
|
769
|
+
} else {
|
|
770
|
+
return getTextContent(token.content);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
// The following code is extracted/adapted from prismjs v2
|
|
775
|
+
// It will probably be possible to use it directly from prism v2
|
|
776
|
+
// in the future when prismjs v2 is published and Lexical upgrades
|
|
777
|
+
// the prismsjs dependency
|
|
778
|
+
function tokenizeDiffHighlight(tokens, language) {
|
|
779
|
+
const diffLanguage = language;
|
|
780
|
+
const diffGrammar = Prism$1.languages[diffLanguage];
|
|
781
|
+
const env = {
|
|
782
|
+
tokens
|
|
783
|
+
};
|
|
784
|
+
const PREFIXES = Prism$1.languages.diff.PREFIXES;
|
|
785
|
+
for (const token of env.tokens) {
|
|
786
|
+
if (typeof token === 'string' || !(token.type in PREFIXES) || !Array.isArray(token.content)) {
|
|
787
|
+
continue;
|
|
788
|
+
}
|
|
789
|
+
const type = token.type;
|
|
790
|
+
let insertedPrefixes = 0;
|
|
791
|
+
const getPrefixToken = () => {
|
|
792
|
+
insertedPrefixes++;
|
|
793
|
+
return new Prism$1.Token('prefix', PREFIXES[type], type.replace(/^(\w+).*/, '$1'));
|
|
794
|
+
};
|
|
795
|
+
const withoutPrefixes = token.content.filter(t => typeof t === 'string' || t.type !== 'prefix');
|
|
796
|
+
const prefixCount = token.content.length - withoutPrefixes.length;
|
|
797
|
+
const diffTokens = Prism$1.tokenize(getTextContent(withoutPrefixes), diffGrammar);
|
|
798
|
+
|
|
799
|
+
// re-insert prefixes
|
|
800
|
+
// always add a prefix at the start
|
|
801
|
+
diffTokens.unshift(getPrefixToken());
|
|
802
|
+
const LINE_BREAK = /\r\n|\n/g;
|
|
803
|
+
const insertAfterLineBreakString = text => {
|
|
804
|
+
const result = [];
|
|
805
|
+
LINE_BREAK.lastIndex = 0;
|
|
806
|
+
let last = 0;
|
|
807
|
+
let m;
|
|
808
|
+
while (insertedPrefixes < prefixCount && (m = LINE_BREAK.exec(text))) {
|
|
809
|
+
const end = m.index + m[0].length;
|
|
810
|
+
result.push(text.slice(last, end));
|
|
811
|
+
last = end;
|
|
812
|
+
result.push(getPrefixToken());
|
|
813
|
+
}
|
|
814
|
+
if (result.length === 0) {
|
|
815
|
+
return undefined;
|
|
816
|
+
}
|
|
817
|
+
if (last < text.length) {
|
|
818
|
+
result.push(text.slice(last));
|
|
819
|
+
}
|
|
820
|
+
return result;
|
|
821
|
+
};
|
|
822
|
+
const insertAfterLineBreak = toks => {
|
|
823
|
+
for (let i = 0; i < toks.length && insertedPrefixes < prefixCount; i++) {
|
|
824
|
+
const tok = toks[i];
|
|
825
|
+
if (typeof tok === 'string') {
|
|
826
|
+
const inserted = insertAfterLineBreakString(tok);
|
|
827
|
+
if (inserted) {
|
|
828
|
+
toks.splice(i, 1, ...inserted);
|
|
829
|
+
i += inserted.length - 1;
|
|
830
|
+
}
|
|
831
|
+
} else if (typeof tok.content === 'string') {
|
|
832
|
+
const inserted = insertAfterLineBreakString(tok.content);
|
|
833
|
+
if (inserted) {
|
|
834
|
+
tok.content = inserted;
|
|
835
|
+
}
|
|
836
|
+
} else if (Array.isArray(tok.content)) {
|
|
837
|
+
insertAfterLineBreak(tok.content);
|
|
838
|
+
} else {
|
|
839
|
+
insertAfterLineBreak([tok.content]);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
insertAfterLineBreak(diffTokens);
|
|
844
|
+
if (insertedPrefixes < prefixCount) {
|
|
845
|
+
// we are missing the last prefix
|
|
846
|
+
diffTokens.push(getPrefixToken());
|
|
847
|
+
}
|
|
848
|
+
token.content = diffTokens;
|
|
849
|
+
}
|
|
850
|
+
return env.tokens;
|
|
851
|
+
}
|
|
852
|
+
function $getHighlightNodes(codeNode, language) {
|
|
853
|
+
const DIFF_LANGUAGE_REGEX = /^diff-([\w-]+)/i;
|
|
854
|
+
const diffLanguageMatch = DIFF_LANGUAGE_REGEX.exec(language);
|
|
855
|
+
const code = codeNode.getTextContent();
|
|
856
|
+
let tokens = Prism$1.tokenize(code, Prism$1.languages[diffLanguageMatch ? 'diff' : language]);
|
|
857
|
+
if (diffLanguageMatch) {
|
|
858
|
+
tokens = tokenizeDiffHighlight(tokens, diffLanguageMatch[1]);
|
|
859
|
+
}
|
|
860
|
+
return $mapTokensToLexicalStructure(tokens);
|
|
861
|
+
}
|
|
862
|
+
function $mapTokensToLexicalStructure(tokens, type) {
|
|
863
|
+
const nodes = [];
|
|
864
|
+
for (const token of tokens) {
|
|
865
|
+
if (typeof token === 'string') {
|
|
866
|
+
const partials = token.split(/(\n|\t)/);
|
|
867
|
+
const partialsLength = partials.length;
|
|
868
|
+
for (let i = 0; i < partialsLength; i++) {
|
|
869
|
+
const part = partials[i];
|
|
870
|
+
if (part === '\n' || part === '\r\n') {
|
|
871
|
+
nodes.push($createLineBreakNode());
|
|
872
|
+
} else if (part === '\t') {
|
|
873
|
+
nodes.push($createTabNode());
|
|
874
|
+
} else if (part.length > 0) {
|
|
875
|
+
nodes.push($createCodeHighlightNode(part, type));
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
} else {
|
|
879
|
+
const {
|
|
880
|
+
content,
|
|
881
|
+
alias
|
|
882
|
+
} = token;
|
|
883
|
+
if (typeof content === 'string') {
|
|
884
|
+
nodes.push(...$mapTokensToLexicalStructure([content], token.type === 'prefix' && typeof alias === 'string' ? alias : token.type));
|
|
885
|
+
} else if (Array.isArray(content)) {
|
|
886
|
+
nodes.push(...$mapTokensToLexicalStructure(content, token.type === 'unchanged' ? undefined : token.type));
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
return nodes;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
const PrismTokenizer = {
|
|
894
|
+
$tokenize(codeNode, language) {
|
|
895
|
+
return $getHighlightNodes(codeNode, language || this.defaultLanguage);
|
|
896
|
+
},
|
|
897
|
+
defaultLanguage: DEFAULT_CODE_LANGUAGE,
|
|
898
|
+
tokenize(code, language) {
|
|
899
|
+
return Prism$1.tokenize(code, Prism$1.languages[language || ''] || Prism$1.languages[this.defaultLanguage]);
|
|
900
|
+
}
|
|
901
|
+
};
|
|
619
902
|
function $textNodeTransform(node, editor, tokenizer) {
|
|
620
903
|
// Since CodeNode has flat children structure we only need to check
|
|
621
904
|
// if node's parent is a code node and run highlighting if so
|
|
@@ -663,18 +946,30 @@ function updateCodeGutter(node, editor) {
|
|
|
663
946
|
const nodesCurrentlyHighlighting = new Set();
|
|
664
947
|
function codeNodeTransform(node, editor, tokenizer) {
|
|
665
948
|
const nodeKey = node.getKey();
|
|
666
|
-
if (nodesCurrentlyHighlighting.has(nodeKey)) {
|
|
667
|
-
return;
|
|
668
|
-
}
|
|
669
|
-
nodesCurrentlyHighlighting.add(nodeKey);
|
|
670
949
|
|
|
671
950
|
// When new code block inserted it might not have language selected
|
|
672
951
|
if (node.getLanguage() === undefined) {
|
|
673
952
|
node.setLanguage(tokenizer.defaultLanguage);
|
|
674
953
|
}
|
|
954
|
+
const language = node.getLanguage() || tokenizer.defaultLanguage;
|
|
955
|
+
if (isCodeLanguageLoaded(language)) {
|
|
956
|
+
if (!node.getIsSyntaxHighlightSupported()) {
|
|
957
|
+
node.setIsSyntaxHighlightSupported(true);
|
|
958
|
+
}
|
|
959
|
+
} else {
|
|
960
|
+
if (node.getIsSyntaxHighlightSupported()) {
|
|
961
|
+
node.setIsSyntaxHighlightSupported(false);
|
|
962
|
+
}
|
|
963
|
+
loadCodeLanguage();
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
if (nodesCurrentlyHighlighting.has(nodeKey)) {
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
nodesCurrentlyHighlighting.add(nodeKey);
|
|
675
970
|
|
|
676
971
|
// Using nested update call to pass `skipTransforms` since we don't want
|
|
677
|
-
// each individual
|
|
972
|
+
// each individual CodeHighlightNode to be transformed again as it's already
|
|
678
973
|
// in its final state
|
|
679
974
|
editor.update(() => {
|
|
680
975
|
$updateAndRetainSelection(nodeKey, () => {
|
|
@@ -682,9 +977,11 @@ function codeNodeTransform(node, editor, tokenizer) {
|
|
|
682
977
|
if (!$isCodeNode(currentNode) || !currentNode.isAttached()) {
|
|
683
978
|
return false;
|
|
684
979
|
}
|
|
685
|
-
const
|
|
686
|
-
const
|
|
687
|
-
const
|
|
980
|
+
//const DIFF_LANGUAGE_REGEX = /^diff-([\w-]+)/i;
|
|
981
|
+
const currentLanguage = currentNode.getLanguage() || tokenizer.defaultLanguage;
|
|
982
|
+
//const diffLanguageMatch = DIFF_LANGUAGE_REGEX.exec(currentLanguage);
|
|
983
|
+
|
|
984
|
+
const highlightNodes = tokenizer.$tokenize(currentNode, currentLanguage);
|
|
688
985
|
const diffRange = getDiffRange(currentNode.getChildren(), highlightNodes);
|
|
689
986
|
const {
|
|
690
987
|
from,
|
|
@@ -704,35 +1001,6 @@ function codeNodeTransform(node, editor, tokenizer) {
|
|
|
704
1001
|
skipTransforms: true
|
|
705
1002
|
});
|
|
706
1003
|
}
|
|
707
|
-
function $getHighlightNodes(tokens, type) {
|
|
708
|
-
const nodes = [];
|
|
709
|
-
for (const token of tokens) {
|
|
710
|
-
if (typeof token === 'string') {
|
|
711
|
-
const partials = token.split(/(\n|\t)/);
|
|
712
|
-
const partialsLength = partials.length;
|
|
713
|
-
for (let i = 0; i < partialsLength; i++) {
|
|
714
|
-
const part = partials[i];
|
|
715
|
-
if (part === '\n' || part === '\r\n') {
|
|
716
|
-
nodes.push($createLineBreakNode());
|
|
717
|
-
} else if (part === '\t') {
|
|
718
|
-
nodes.push($createTabNode());
|
|
719
|
-
} else if (part.length > 0) {
|
|
720
|
-
nodes.push($createCodeHighlightNode(part, type));
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
} else {
|
|
724
|
-
const {
|
|
725
|
-
content
|
|
726
|
-
} = token;
|
|
727
|
-
if (typeof content === 'string') {
|
|
728
|
-
nodes.push(...$getHighlightNodes([content], token.type));
|
|
729
|
-
} else if (Array.isArray(content)) {
|
|
730
|
-
nodes.push(...$getHighlightNodes(content, token.type));
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
return nodes;
|
|
735
|
-
}
|
|
736
1004
|
|
|
737
1005
|
// Wrapping update function into selection retainer, that tries to keep cursor at the same
|
|
738
1006
|
// position as before.
|
|
@@ -819,43 +1087,66 @@ function getDiffRange(prevNodes, nextNodes) {
|
|
|
819
1087
|
};
|
|
820
1088
|
}
|
|
821
1089
|
function isEqual(nodeA, nodeB) {
|
|
822
|
-
// Only checking for code
|
|
1090
|
+
// Only checking for code highlight nodes, tabs and linebreaks. If it's regular text node
|
|
823
1091
|
// returning false so that it's transformed into code highlight node
|
|
824
1092
|
return $isCodeHighlightNode(nodeA) && $isCodeHighlightNode(nodeB) && nodeA.__text === nodeB.__text && nodeA.__highlightType === nodeB.__highlightType || $isTabNode(nodeA) && $isTabNode(nodeB) || $isLineBreakNode(nodeA) && $isLineBreakNode(nodeB);
|
|
825
1093
|
}
|
|
1094
|
+
|
|
1095
|
+
/**
|
|
1096
|
+
* Returns a boolean.
|
|
1097
|
+
* Check that the selection span is within a single CodeNode.
|
|
1098
|
+
* This is used to guard against executing handlers that can only be
|
|
1099
|
+
* applied in a single CodeNode context
|
|
1100
|
+
*/
|
|
826
1101
|
function $isSelectionInCode(selection) {
|
|
827
1102
|
if (!$isRangeSelection(selection)) {
|
|
828
1103
|
return false;
|
|
829
1104
|
}
|
|
830
1105
|
const anchorNode = selection.anchor.getNode();
|
|
1106
|
+
const maybeAnchorCodeNode = $isCodeNode(anchorNode) ? anchorNode : anchorNode.getParent();
|
|
831
1107
|
const focusNode = selection.focus.getNode();
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
}
|
|
835
|
-
const anchorParent = anchorNode.getParent();
|
|
836
|
-
return $isCodeNode(anchorParent) && anchorParent.is(focusNode.getParent());
|
|
1108
|
+
const maybeFocusCodeNode = $isCodeNode(focusNode) ? focusNode : focusNode.getParent();
|
|
1109
|
+
return $isCodeNode(maybeAnchorCodeNode) && maybeAnchorCodeNode.is(maybeFocusCodeNode);
|
|
837
1110
|
}
|
|
1111
|
+
|
|
1112
|
+
/**
|
|
1113
|
+
* Returns an Array of code lines
|
|
1114
|
+
* Take the sequence of LineBreakNode | TabNode | CodeHighlightNode forming
|
|
1115
|
+
* the selection and split it by LineBreakNode.
|
|
1116
|
+
* If the selection ends at the start of the last line, it is considered empty.
|
|
1117
|
+
* Empty lines are discarded.
|
|
1118
|
+
*/
|
|
838
1119
|
function $getCodeLines(selection) {
|
|
839
1120
|
const nodes = selection.getNodes();
|
|
840
|
-
const lines = [
|
|
1121
|
+
const lines = [];
|
|
841
1122
|
if (nodes.length === 1 && $isCodeNode(nodes[0])) {
|
|
842
1123
|
return lines;
|
|
843
1124
|
}
|
|
844
|
-
let lastLine =
|
|
1125
|
+
let lastLine = [];
|
|
845
1126
|
for (let i = 0; i < nodes.length; i++) {
|
|
846
1127
|
const node = nodes[i];
|
|
847
1128
|
if (!($isCodeHighlightNode(node) || $isTabNode(node) || $isLineBreakNode(node))) {
|
|
848
1129
|
formatDevErrorMessage(`Expected selection to be inside CodeBlock and consisting of CodeHighlightNode, TabNode and LineBreakNode`);
|
|
849
1130
|
}
|
|
850
1131
|
if ($isLineBreakNode(node)) {
|
|
851
|
-
if (
|
|
852
|
-
lastLine = [];
|
|
1132
|
+
if (lastLine.length > 0) {
|
|
853
1133
|
lines.push(lastLine);
|
|
1134
|
+
lastLine = [];
|
|
854
1135
|
}
|
|
855
1136
|
} else {
|
|
856
1137
|
lastLine.push(node);
|
|
857
1138
|
}
|
|
858
1139
|
}
|
|
1140
|
+
if (lastLine.length > 0) {
|
|
1141
|
+
const selectionEnd = selection.isBackward() ? selection.anchor : selection.focus;
|
|
1142
|
+
|
|
1143
|
+
// Discard the last line if the selection ends exactly at the
|
|
1144
|
+
// start of the line (no real selection)
|
|
1145
|
+
const lastPoint = $createPoint(lastLine[0].getKey(), 0, 'text');
|
|
1146
|
+
if (!selectionEnd.is(lastPoint)) {
|
|
1147
|
+
lines.push(lastLine);
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
859
1150
|
return lines;
|
|
860
1151
|
}
|
|
861
1152
|
function $handleTab(shiftKey) {
|
|
@@ -865,38 +1156,60 @@ function $handleTab(shiftKey) {
|
|
|
865
1156
|
}
|
|
866
1157
|
const indentOrOutdent = !shiftKey ? INDENT_CONTENT_COMMAND : OUTDENT_CONTENT_COMMAND;
|
|
867
1158
|
const tabOrOutdent = !shiftKey ? INSERT_TAB_COMMAND : OUTDENT_CONTENT_COMMAND;
|
|
868
|
-
|
|
869
|
-
const
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
const selectionNodes = selection.getNodes();
|
|
875
|
-
const firstNode = selectionNodes[0];
|
|
876
|
-
if (!($isCodeNode(firstNode) || $isCodeHighlightNode(firstNode) || $isTabNode(firstNode) || $isLineBreakNode(firstNode))) {
|
|
877
|
-
formatDevErrorMessage(`Expected selection firstNode to be CodeHighlightNode or TabNode`);
|
|
1159
|
+
const anchor = selection.anchor;
|
|
1160
|
+
const focus = selection.focus;
|
|
1161
|
+
|
|
1162
|
+
// 1. early decision when there is no real selection
|
|
1163
|
+
if (anchor.is(focus)) {
|
|
1164
|
+
return tabOrOutdent;
|
|
878
1165
|
}
|
|
879
|
-
|
|
1166
|
+
|
|
1167
|
+
// 2. If only empty lines or multiple non-empty lines are selected: indent/outdent
|
|
1168
|
+
const codeLines = $getCodeLines(selection);
|
|
1169
|
+
if (codeLines.length !== 1) {
|
|
880
1170
|
return indentOrOutdent;
|
|
881
1171
|
}
|
|
882
|
-
const
|
|
883
|
-
const
|
|
884
|
-
|
|
885
|
-
|
|
1172
|
+
const codeLine = codeLines[0];
|
|
1173
|
+
const codeLineLength = codeLine.length;
|
|
1174
|
+
if (!(codeLineLength !== 0)) {
|
|
1175
|
+
formatDevErrorMessage(`$getCodeLines only extracts non-empty lines`);
|
|
1176
|
+
} // Take into account the direction of the selection
|
|
886
1177
|
let selectionFirst;
|
|
887
1178
|
let selectionLast;
|
|
888
|
-
if (
|
|
1179
|
+
if (selection.isBackward()) {
|
|
889
1180
|
selectionFirst = focus;
|
|
890
1181
|
selectionLast = anchor;
|
|
891
1182
|
} else {
|
|
892
1183
|
selectionFirst = anchor;
|
|
893
1184
|
selectionLast = focus;
|
|
894
1185
|
}
|
|
895
|
-
|
|
1186
|
+
|
|
1187
|
+
// find boundary elements of the line
|
|
1188
|
+
// since codeLine only contains TabNode | CodeHighlightNode
|
|
1189
|
+
// the result of these functions should is of Type TabNode | CodeHighlightNode
|
|
1190
|
+
const firstOfLine = $getFirstCodeNodeOfLine(codeLine[0]);
|
|
1191
|
+
const lastOfLine = $getLastCodeNodeOfLine(codeLine[0]);
|
|
1192
|
+
const anchorOfLine = $createPoint(firstOfLine.getKey(), 0, 'text');
|
|
1193
|
+
const focusOfLine = $createPoint(lastOfLine.getKey(), lastOfLine.getTextContentSize(), 'text');
|
|
1194
|
+
|
|
1195
|
+
// 3. multiline because selection started strictly before the line
|
|
1196
|
+
if (selectionFirst.isBefore(anchorOfLine)) {
|
|
1197
|
+
return indentOrOutdent;
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
// 4. multiline because the selection stops strictly after the line
|
|
1201
|
+
if (focusOfLine.isBefore(selectionLast)) {
|
|
896
1202
|
return indentOrOutdent;
|
|
897
1203
|
}
|
|
898
|
-
|
|
899
|
-
|
|
1204
|
+
|
|
1205
|
+
// The selection if within the line.
|
|
1206
|
+
// 4. If it does not touch both borders, it needs a tab
|
|
1207
|
+
if (anchorOfLine.isBefore(selectionFirst) || selectionLast.isBefore(focusOfLine)) {
|
|
1208
|
+
return tabOrOutdent;
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
// 5. Selection is matching a full line on non-empty code
|
|
1212
|
+
return indentOrOutdent;
|
|
900
1213
|
}
|
|
901
1214
|
function $handleMultilineIndent(type) {
|
|
902
1215
|
const selection = $getSelection();
|
|
@@ -905,49 +1218,55 @@ function $handleMultilineIndent(type) {
|
|
|
905
1218
|
}
|
|
906
1219
|
const codeLines = $getCodeLines(selection);
|
|
907
1220
|
const codeLinesLength = codeLines.length;
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
if (line.length > 0) {
|
|
913
|
-
let firstOfLine = line[0];
|
|
914
|
-
// First and last lines might not be complete
|
|
915
|
-
if (i === 0) {
|
|
916
|
-
firstOfLine = $getFirstCodeNodeOfLine(firstOfLine);
|
|
917
|
-
}
|
|
918
|
-
if (firstOfLine !== null) {
|
|
919
|
-
if (type === INDENT_CONTENT_COMMAND) {
|
|
920
|
-
firstOfLine.insertBefore($createTabNode());
|
|
921
|
-
} else if ($isTabNode(firstOfLine)) {
|
|
922
|
-
firstOfLine.remove();
|
|
923
|
-
}
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
return true;
|
|
928
|
-
}
|
|
929
|
-
// Just one line
|
|
930
|
-
const selectionNodes = selection.getNodes();
|
|
931
|
-
const firstNode = selectionNodes[0];
|
|
932
|
-
if (!($isCodeNode(firstNode) || $isCodeHighlightNode(firstNode) || $isTabNode(firstNode) || $isLineBreakNode(firstNode))) {
|
|
933
|
-
formatDevErrorMessage(`Expected selection firstNode to be CodeHighlightNode or CodeTabNode`);
|
|
934
|
-
}
|
|
935
|
-
if ($isCodeNode(firstNode)) {
|
|
936
|
-
// CodeNode is empty
|
|
1221
|
+
|
|
1222
|
+
// Special Indent case
|
|
1223
|
+
// Selection is collapsed at the beginning of a line
|
|
1224
|
+
if (codeLinesLength === 0 && selection.isCollapsed()) {
|
|
937
1225
|
if (type === INDENT_CONTENT_COMMAND) {
|
|
938
1226
|
selection.insertNodes([$createTabNode()]);
|
|
939
1227
|
}
|
|
940
1228
|
return true;
|
|
941
1229
|
}
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
1230
|
+
|
|
1231
|
+
// Special Indent case
|
|
1232
|
+
// Selection is matching only one LineBreak
|
|
1233
|
+
if (codeLinesLength === 0 && type === INDENT_CONTENT_COMMAND && selection.getTextContent() === '\n') {
|
|
1234
|
+
const tabNode = $createTabNode();
|
|
1235
|
+
const lineBreakNode = $createLineBreakNode();
|
|
1236
|
+
const direction = selection.isBackward() ? 'previous' : 'next';
|
|
1237
|
+
selection.insertNodes([tabNode, lineBreakNode]);
|
|
1238
|
+
$setSelectionFromCaretRange($getCaretRangeInDirection($getCaretRange($getTextPointCaret(tabNode, 'next', 0), $normalizeCaret($getSiblingCaret(lineBreakNode, 'next'))), direction));
|
|
1239
|
+
return true;
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
// Indent Non Empty Lines
|
|
1243
|
+
for (let i = 0; i < codeLinesLength; i++) {
|
|
1244
|
+
const line = codeLines[i];
|
|
1245
|
+
// a line here is never empty
|
|
1246
|
+
if (line.length > 0) {
|
|
1247
|
+
let firstOfLine = line[0];
|
|
1248
|
+
|
|
1249
|
+
// make sure to consider the first node on the first line
|
|
1250
|
+
// because the line might not be fully selected
|
|
1251
|
+
if (i === 0) {
|
|
1252
|
+
firstOfLine = $getFirstCodeNodeOfLine(firstOfLine);
|
|
1253
|
+
}
|
|
1254
|
+
if (type === INDENT_CONTENT_COMMAND) {
|
|
1255
|
+
const tabNode = $createTabNode();
|
|
1256
|
+
firstOfLine.insertBefore(tabNode);
|
|
1257
|
+
// First real code line may need selection adjustment
|
|
1258
|
+
// when firstOfLine is at the selection boundary
|
|
1259
|
+
if (i === 0) {
|
|
1260
|
+
const anchorKey = selection.isBackward() ? 'focus' : 'anchor';
|
|
1261
|
+
const anchorLine = $createPoint(firstOfLine.getKey(), 0, 'text');
|
|
1262
|
+
if (selection[anchorKey].is(anchorLine)) {
|
|
1263
|
+
selection[anchorKey].set(tabNode.getKey(), 0, 'text');
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
} else if ($isTabNode(firstOfLine)) {
|
|
1267
|
+
firstOfLine.remove();
|
|
1268
|
+
}
|
|
948
1269
|
}
|
|
949
|
-
} else if ($isTabNode(firstOfLine)) {
|
|
950
|
-
firstOfLine.remove();
|
|
951
1270
|
}
|
|
952
1271
|
return true;
|
|
953
1272
|
}
|
|
@@ -976,7 +1295,7 @@ function $handleShiftLines(type, event) {
|
|
|
976
1295
|
}
|
|
977
1296
|
if (!event.altKey) {
|
|
978
1297
|
// Handle moving selection out of the code block, given there are no
|
|
979
|
-
//
|
|
1298
|
+
// siblings that can natively take the selection.
|
|
980
1299
|
if (selection.isCollapsed()) {
|
|
981
1300
|
const codeNode = anchorNode.getParentOrThrow();
|
|
982
1301
|
if (arrowIsUp && anchorOffset === 0 && anchorNode.getPreviousSibling() === null) {
|
|
@@ -1096,20 +1415,28 @@ function registerCodeHighlighting(editor, tokenizer) {
|
|
|
1096
1415
|
if (tokenizer == null) {
|
|
1097
1416
|
tokenizer = PrismTokenizer;
|
|
1098
1417
|
}
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1418
|
+
const registrations = [];
|
|
1419
|
+
|
|
1420
|
+
// Only register the mutation listener if not in headless mode
|
|
1421
|
+
if (editor._headless !== true) {
|
|
1422
|
+
registrations.push(editor.registerMutationListener(CodeNode, mutations => {
|
|
1423
|
+
editor.update(() => {
|
|
1424
|
+
for (const [key, type] of mutations) {
|
|
1425
|
+
if (type !== 'destroyed') {
|
|
1426
|
+
const node = $getNodeByKey(key);
|
|
1427
|
+
if (node !== null) {
|
|
1428
|
+
updateCodeGutter(node, editor);
|
|
1429
|
+
}
|
|
1106
1430
|
}
|
|
1107
1431
|
}
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
}
|
|
1432
|
+
});
|
|
1433
|
+
}, {
|
|
1434
|
+
skipInitialization: false
|
|
1435
|
+
}));
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
// Add the rest of the registrations
|
|
1439
|
+
registrations.push(editor.registerNodeTransform(CodeNode, node => codeNodeTransform(node, editor, tokenizer)), editor.registerNodeTransform(TextNode, node => $textNodeTransform(node, editor, tokenizer)), editor.registerNodeTransform(CodeHighlightNode, node => $textNodeTransform(node, editor, tokenizer)), editor.registerCommand(KEY_TAB_COMMAND, event => {
|
|
1113
1440
|
const command = $handleTab(event.shiftKey);
|
|
1114
1441
|
if (command === null) {
|
|
1115
1442
|
return false;
|
|
@@ -1161,6 +1488,7 @@ function registerCodeHighlighting(editor, tokenizer) {
|
|
|
1161
1488
|
}
|
|
1162
1489
|
return $handleShiftLines(KEY_ARROW_DOWN_COMMAND, event);
|
|
1163
1490
|
}, COMMAND_PRIORITY_LOW), editor.registerCommand(MOVE_TO_START, event => $handleMoveTo(MOVE_TO_START, event), COMMAND_PRIORITY_LOW), editor.registerCommand(MOVE_TO_END, event => $handleMoveTo(MOVE_TO_END, event), COMMAND_PRIORITY_LOW));
|
|
1491
|
+
return mergeRegister(...registrations);
|
|
1164
1492
|
}
|
|
1165
1493
|
|
|
1166
1494
|
/**
|
|
@@ -1181,4 +1509,4 @@ const getEndOfCodeInLine = $getEndOfCodeInLine;
|
|
|
1181
1509
|
/** @deprecated renamed to {@link $getStartOfCodeInLine} by @lexical/eslint-plugin rules-of-lexical */
|
|
1182
1510
|
const getStartOfCodeInLine = $getStartOfCodeInLine;
|
|
1183
1511
|
|
|
1184
|
-
export { $createCodeHighlightNode, $createCodeNode, $getEndOfCodeInLine, $getFirstCodeNodeOfLine, $getLastCodeNodeOfLine, $getStartOfCodeInLine, $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 };
|
|
1512
|
+
export { $createCodeHighlightNode, $createCodeNode, $getEndOfCodeInLine, $getFirstCodeNodeOfLine, $getLastCodeNodeOfLine, $getStartOfCodeInLine, $isCodeHighlightNode, $isCodeNode, CODE_LANGUAGE_FRIENDLY_NAME_MAP, CODE_LANGUAGE_MAP, CodeHighlightNode, CodeNode, DEFAULT_CODE_LANGUAGE, PrismTokenizer, getCodeLanguageOptions, getCodeLanguages, getCodeThemeOptions, getDefaultCodeLanguage, getEndOfCodeInLine, getFirstCodeNodeOfLine, getLanguageFriendlyName, getLastCodeNodeOfLine, getStartOfCodeInLine, normalizeCodeLang, normalizeCodeLang as normalizeCodeLanguage, registerCodeHighlighting };
|