@react-fabric/lexical 1.0.0-beta.9 → 1.0.1

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.
Files changed (148) hide show
  1. package/{src/editor/Lexical.module.css → css/_lexical.css} +79 -13
  2. package/css/styles.css +24 -0
  3. package/dist/cjs/{packages/lexical/src/editor → editor}/LexicalEditor.js +98 -45
  4. package/dist/cjs/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +147 -140
  5. package/dist/cjs/i18n/ar/lexical.json.js +30 -0
  6. package/dist/cjs/i18n/en/lexical.json.js +30 -0
  7. package/{src/plugins/StickyPlugin.ts → dist/cjs/i18n/index.js} +10 -15
  8. package/dist/cjs/index.js +36 -0
  9. package/dist/cjs/plugins/CommentPlugin.js +481 -0
  10. package/dist/cjs/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +17 -20
  11. package/dist/cjs/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +58 -37
  12. package/dist/esm/{packages/lexical/src/editor → editor}/LexicalEditor.js +101 -48
  13. package/dist/esm/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +95 -88
  14. package/dist/esm/i18n/ar/lexical.json.js +25 -0
  15. package/dist/esm/i18n/en/lexical.json.js +25 -0
  16. package/dist/esm/i18n/index.js +33 -0
  17. package/dist/esm/index.js +34 -0
  18. package/dist/esm/plugins/CommentPlugin.js +481 -0
  19. package/dist/esm/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +15 -18
  20. package/dist/esm/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +59 -38
  21. package/dist/types/editor/LexicalEditor.d.ts +33 -4
  22. package/dist/types/hooks/useFormatWatcher.d.ts +7 -4
  23. package/dist/types/i18n/index.d.ts +1 -0
  24. package/dist/types/index.d.ts +27 -0
  25. package/dist/types/plugins/CommentPlugin.d.ts +5 -0
  26. package/dist/types/plugins/DraggableBlockPlugin.d.ts +3 -2
  27. package/dist/types/plugins/ToolbarPlugin.d.ts +1 -1
  28. package/dist/types/utils/index.d.ts +3 -6
  29. package/package.json +18 -18
  30. package/src/editor/LexicalEditor.tsx +95 -55
  31. package/src/hooks/useFormatWatcher.ts +50 -74
  32. package/src/i18n/ar/lexical.json +18 -0
  33. package/src/i18n/en/lexical.json +18 -0
  34. package/src/i18n/index.ts +28 -0
  35. package/src/index.ts +24 -17
  36. package/src/plugins/CommentPlugin.tsx +523 -0
  37. package/src/plugins/DraggableBlockPlugin.tsx +23 -68
  38. package/src/plugins/ToolbarPlugin.tsx +83 -131
  39. package/src/utils/index.ts +12 -108
  40. package/dist/cjs/_virtual/prism.js +0 -5
  41. package/dist/cjs/editor/Lexical.module.css +0 -1
  42. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -432
  43. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -21
  44. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -27
  45. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +0 -1139
  46. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +0 -25
  47. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +0 -50
  48. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +0 -245
  49. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +0 -19
  50. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +0 -18
  51. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +0 -471
  52. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +0 -17
  53. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +0 -24
  54. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +0 -1275
  55. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +0 -29
  56. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +0 -29
  57. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -789
  58. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +0 -29
  59. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -26
  60. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -976
  61. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +0 -35
  62. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -35
  63. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +0 -2792
  64. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +0 -23
  65. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +0 -55
  66. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -888
  67. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +0 -35
  68. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -66
  69. package/dist/cjs/node_modules/@mdi/js/mdi.js +0 -8
  70. package/dist/cjs/node_modules/prismjs/components/prism-c.js +0 -82
  71. package/dist/cjs/node_modules/prismjs/components/prism-clike.js +0 -33
  72. package/dist/cjs/node_modules/prismjs/components/prism-cpp.js +0 -101
  73. package/dist/cjs/node_modules/prismjs/components/prism-css.js +0 -66
  74. package/dist/cjs/node_modules/prismjs/components/prism-java.js +0 -126
  75. package/dist/cjs/node_modules/prismjs/components/prism-javascript.js +0 -174
  76. package/dist/cjs/node_modules/prismjs/components/prism-markdown.js +0 -417
  77. package/dist/cjs/node_modules/prismjs/components/prism-markup.js +0 -188
  78. package/dist/cjs/node_modules/prismjs/components/prism-objectivec.js +0 -14
  79. package/dist/cjs/node_modules/prismjs/components/prism-powershell.js +0 -60
  80. package/dist/cjs/node_modules/prismjs/components/prism-python.js +0 -67
  81. package/dist/cjs/node_modules/prismjs/components/prism-rust.js +0 -130
  82. package/dist/cjs/node_modules/prismjs/components/prism-sql.js +0 -34
  83. package/dist/cjs/node_modules/prismjs/components/prism-swift.js +0 -150
  84. package/dist/cjs/node_modules/prismjs/components/prism-typescript.js +0 -62
  85. package/dist/cjs/node_modules/prismjs/prism.js +0 -1951
  86. package/dist/cjs/packages/lexical/src/components/StickyComponent.js +0 -221
  87. package/dist/cjs/packages/lexical/src/index.js +0 -7
  88. package/dist/cjs/packages/lexical/src/nodes/StickyNode.js +0 -218
  89. package/dist/cjs/packages/lexical/src/plugins/StickyPlugin.js +0 -20
  90. package/dist/esm/_virtual/prism.js +0 -5
  91. package/dist/esm/editor/Lexical.module.css +0 -1
  92. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -423
  93. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -19
  94. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -18
  95. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +0 -1121
  96. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +0 -21
  97. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +0 -32
  98. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +0 -244
  99. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +0 -18
  100. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +0 -17
  101. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +0 -463
  102. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +0 -17
  103. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +0 -16
  104. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +0 -1262
  105. package/dist/esm/node_modules/@lexical/list/LexicalList.js +0 -23
  106. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +0 -16
  107. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -781
  108. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +0 -23
  109. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -18
  110. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -957
  111. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +0 -26
  112. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -16
  113. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +0 -2754
  114. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +0 -20
  115. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +0 -17
  116. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -839
  117. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +0 -26
  118. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -17
  119. package/dist/esm/node_modules/@mdi/js/mdi.js +0 -7
  120. package/dist/esm/node_modules/prismjs/components/prism-c.js +0 -82
  121. package/dist/esm/node_modules/prismjs/components/prism-clike.js +0 -33
  122. package/dist/esm/node_modules/prismjs/components/prism-cpp.js +0 -101
  123. package/dist/esm/node_modules/prismjs/components/prism-css.js +0 -66
  124. package/dist/esm/node_modules/prismjs/components/prism-java.js +0 -126
  125. package/dist/esm/node_modules/prismjs/components/prism-javascript.js +0 -174
  126. package/dist/esm/node_modules/prismjs/components/prism-markdown.js +0 -417
  127. package/dist/esm/node_modules/prismjs/components/prism-markup.js +0 -188
  128. package/dist/esm/node_modules/prismjs/components/prism-objectivec.js +0 -14
  129. package/dist/esm/node_modules/prismjs/components/prism-powershell.js +0 -60
  130. package/dist/esm/node_modules/prismjs/components/prism-python.js +0 -67
  131. package/dist/esm/node_modules/prismjs/components/prism-rust.js +0 -130
  132. package/dist/esm/node_modules/prismjs/components/prism-sql.js +0 -34
  133. package/dist/esm/node_modules/prismjs/components/prism-swift.js +0 -150
  134. package/dist/esm/node_modules/prismjs/components/prism-typescript.js +0 -62
  135. package/dist/esm/node_modules/prismjs/prism.js +0 -1951
  136. package/dist/esm/packages/lexical/src/components/StickyComponent.js +0 -221
  137. package/dist/esm/packages/lexical/src/index.js +0 -3
  138. package/dist/esm/packages/lexical/src/nodes/StickyNode.js +0 -216
  139. package/dist/esm/packages/lexical/src/plugins/StickyPlugin.js +0 -20
  140. package/dist/types/components/StickyComponent.d.ts +0 -9
  141. package/dist/types/nodes/StickyNode.d.ts +0 -35
  142. package/dist/types/plugins/StickyPlugin.d.ts +0 -1
  143. package/src/components/StickyComponent.tsx +0 -271
  144. package/src/nodes/StickyNode.tsx +0 -249
  145. /package/dist/cjs/{packages/lexical/src/editor → editor}/theme.js +0 -0
  146. /package/dist/cjs/{packages/lexical/src/utils → utils}/index.js +0 -0
  147. /package/dist/esm/{packages/lexical/src/editor → editor}/theme.js +0 -0
  148. /package/dist/esm/{packages/lexical/src/utils → utils}/index.js +0 -0
@@ -1,1139 +0,0 @@
1
- 'use strict';
2
-
3
- require('../../prismjs/prism.js');
4
- require('../../prismjs/components/prism-clike.js');
5
- require('../../prismjs/components/prism-javascript.js');
6
- require('../../prismjs/components/prism-markup.js');
7
- require('../../prismjs/components/prism-markdown.js');
8
- require('../../prismjs/components/prism-c.js');
9
- require('../../prismjs/components/prism-css.js');
10
- require('../../prismjs/components/prism-objectivec.js');
11
- require('../../prismjs/components/prism-sql.js');
12
- require('../../prismjs/components/prism-powershell.js');
13
- require('../../prismjs/components/prism-python.js');
14
- require('../../prismjs/components/prism-rust.js');
15
- require('../../prismjs/components/prism-swift.js');
16
- require('../../prismjs/components/prism-typescript.js');
17
- require('../../prismjs/components/prism-java.js');
18
- require('../../prismjs/components/prism-cpp.js');
19
- var LexicalUtils = require('../utils/LexicalUtils.js');
20
- var lexical = require('lexical');
21
-
22
- /**
23
- * Copyright (c) Meta Platforms, Inc. and affiliates.
24
- *
25
- * This source code is licensed under the MIT license found in the
26
- * LICENSE file in the root directory of this source tree.
27
- *
28
- */
29
-
30
-
31
- /**
32
- * Copyright (c) Meta Platforms, Inc. and affiliates.
33
- *
34
- * This source code is licensed under the MIT license found in the
35
- * LICENSE file in the root directory of this source tree.
36
- *
37
- */
38
-
39
- const isLanguageSupportedByPrism = language => {
40
- try {
41
- // eslint-disable-next-line no-prototype-builtins
42
- return language ? window.Prism.languages.hasOwnProperty(language) : false;
43
- } catch (_unused) {
44
- return false;
45
- }
46
- };
47
- function hasChildDOMNodeTag(node, tagName) {
48
- for (const child of node.childNodes) {
49
- if (LexicalUtils.isHTMLElement(child) && child.tagName === tagName) {
50
- return true;
51
- }
52
- hasChildDOMNodeTag(child, tagName);
53
- }
54
- return false;
55
- }
56
- const LANGUAGE_DATA_ATTRIBUTE = 'data-language';
57
- const HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE = 'data-highlight-language';
58
-
59
- /** @noInheritDoc */
60
- class CodeNode extends lexical.ElementNode {
61
- /** @internal */
62
-
63
- /** @internal */
64
-
65
- static getType() {
66
- return 'code';
67
- }
68
- static clone(node) {
69
- return new CodeNode(node.__language, node.__key);
70
- }
71
- constructor(language, key) {
72
- super(key);
73
- this.__language = language;
74
- this.__isSyntaxHighlightSupported = isLanguageSupportedByPrism(language);
75
- }
76
-
77
- // View
78
- createDOM(config) {
79
- const element = document.createElement('code');
80
- LexicalUtils.addClassNamesToElement(element, config.theme.code);
81
- element.setAttribute('spellcheck', 'false');
82
- const language = this.getLanguage();
83
- if (language) {
84
- element.setAttribute(LANGUAGE_DATA_ATTRIBUTE, language);
85
- if (this.getIsSyntaxHighlightSupported()) {
86
- element.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
87
- }
88
- }
89
- return element;
90
- }
91
- updateDOM(prevNode, dom, config) {
92
- const language = this.__language;
93
- const prevLanguage = prevNode.__language;
94
- if (language) {
95
- if (language !== prevLanguage) {
96
- dom.setAttribute(LANGUAGE_DATA_ATTRIBUTE, language);
97
- if (this.__isSyntaxHighlightSupported) {
98
- dom.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
99
- }
100
- }
101
- } else if (prevLanguage) {
102
- dom.removeAttribute(LANGUAGE_DATA_ATTRIBUTE);
103
- if (prevNode.__isSyntaxHighlightSupported) {
104
- dom.removeAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE);
105
- }
106
- }
107
- return false;
108
- }
109
- exportDOM(editor) {
110
- const element = document.createElement('pre');
111
- LexicalUtils.addClassNamesToElement(element, editor._config.theme.code);
112
- element.setAttribute('spellcheck', 'false');
113
- const language = this.getLanguage();
114
- if (language) {
115
- element.setAttribute(LANGUAGE_DATA_ATTRIBUTE, language);
116
- if (this.getIsSyntaxHighlightSupported()) {
117
- element.setAttribute(HIGHLIGHT_LANGUAGE_DATA_ATTRIBUTE, language);
118
- }
119
- }
120
- return {
121
- element
122
- };
123
- }
124
- static importDOM() {
125
- return {
126
- // Typically <pre> is used for code blocks, and <code> for inline code styles
127
- // but if it's a multi line <code> we'll create a block. Pass through to
128
- // inline format handled by TextNode otherwise.
129
- code: node => {
130
- const isMultiLine = node.textContent != null && (/\r?\n/.test(node.textContent) || hasChildDOMNodeTag(node, 'BR'));
131
- return isMultiLine ? {
132
- conversion: $convertPreElement,
133
- priority: 1
134
- } : null;
135
- },
136
- div: () => ({
137
- conversion: $convertDivElement,
138
- priority: 1
139
- }),
140
- pre: () => ({
141
- conversion: $convertPreElement,
142
- priority: 0
143
- }),
144
- table: node => {
145
- const table = node;
146
- // domNode is a <table> since we matched it by nodeName
147
- if (isGitHubCodeTable(table)) {
148
- return {
149
- conversion: $convertTableElement,
150
- priority: 3
151
- };
152
- }
153
- return null;
154
- },
155
- td: node => {
156
- // element is a <td> since we matched it by nodeName
157
- const td = node;
158
- const table = td.closest('table');
159
- if (isGitHubCodeCell(td) || table && isGitHubCodeTable(table)) {
160
- // Return a no-op if it's a table cell in a code table, but not a code line.
161
- // Otherwise it'll fall back to the T
162
- return {
163
- conversion: convertCodeNoop,
164
- priority: 3
165
- };
166
- }
167
- return null;
168
- },
169
- tr: node => {
170
- // element is a <tr> since we matched it by nodeName
171
- const tr = node;
172
- const table = tr.closest('table');
173
- if (table && isGitHubCodeTable(table)) {
174
- return {
175
- conversion: convertCodeNoop,
176
- priority: 3
177
- };
178
- }
179
- return null;
180
- }
181
- };
182
- }
183
- static importJSON(serializedNode) {
184
- const node = $createCodeNode(serializedNode.language);
185
- node.setFormat(serializedNode.format);
186
- node.setIndent(serializedNode.indent);
187
- node.setDirection(serializedNode.direction);
188
- return node;
189
- }
190
- exportJSON() {
191
- return {
192
- ...super.exportJSON(),
193
- language: this.getLanguage(),
194
- type: 'code',
195
- version: 1
196
- };
197
- }
198
-
199
- // Mutation
200
- insertNewAfter(selection, restoreSelection = true) {
201
- const children = this.getChildren();
202
- const childrenLength = children.length;
203
- if (childrenLength >= 2 && children[childrenLength - 1].getTextContent() === '\n' && children[childrenLength - 2].getTextContent() === '\n' && selection.isCollapsed() && selection.anchor.key === this.__key && selection.anchor.offset === childrenLength) {
204
- children[childrenLength - 1].remove();
205
- children[childrenLength - 2].remove();
206
- const newElement = lexical.$createParagraphNode();
207
- this.insertAfter(newElement, restoreSelection);
208
- return newElement;
209
- }
210
-
211
- // If the selection is within the codeblock, find all leading tabs and
212
- // spaces of the current line. Create a new line that has all those
213
- // tabs and spaces, such that leading indentation is preserved.
214
- const {
215
- anchor,
216
- focus
217
- } = selection;
218
- const firstPoint = anchor.isBefore(focus) ? anchor : focus;
219
- const firstSelectionNode = firstPoint.getNode();
220
- if (lexical.$isTextNode(firstSelectionNode)) {
221
- let node = getFirstCodeNodeOfLine(firstSelectionNode);
222
- const insertNodes = [];
223
- // eslint-disable-next-line no-constant-condition
224
- while (true) {
225
- if (lexical.$isTabNode(node)) {
226
- insertNodes.push(lexical.$createTabNode());
227
- node = node.getNextSibling();
228
- } else if ($isCodeHighlightNode(node)) {
229
- let spaces = 0;
230
- const text = node.getTextContent();
231
- const textSize = node.getTextContentSize();
232
- while (spaces < textSize && text[spaces] === ' ') {
233
- spaces++;
234
- }
235
- if (spaces !== 0) {
236
- insertNodes.push($createCodeHighlightNode(' '.repeat(spaces)));
237
- }
238
- if (spaces !== textSize) {
239
- break;
240
- }
241
- node = node.getNextSibling();
242
- } else {
243
- break;
244
- }
245
- }
246
- const split = firstSelectionNode.splitText(anchor.offset)[0];
247
- const x = anchor.offset === 0 ? 0 : 1;
248
- const index = split.getIndexWithinParent() + x;
249
- const codeNode = firstSelectionNode.getParentOrThrow();
250
- const nodesToInsert = [lexical.$createLineBreakNode(), ...insertNodes];
251
- codeNode.splice(index, 0, nodesToInsert);
252
- const last = insertNodes[insertNodes.length - 1];
253
- if (last) {
254
- last.select();
255
- } else if (anchor.offset === 0) {
256
- split.selectPrevious();
257
- } else {
258
- split.getNextSibling().selectNext(0, 0);
259
- }
260
- }
261
- if ($isCodeNode(firstSelectionNode)) {
262
- const {
263
- offset
264
- } = selection.anchor;
265
- firstSelectionNode.splice(offset, 0, [lexical.$createLineBreakNode()]);
266
- firstSelectionNode.select(offset + 1, offset + 1);
267
- }
268
- return null;
269
- }
270
- canIndent() {
271
- return false;
272
- }
273
- collapseAtStart() {
274
- const paragraph = lexical.$createParagraphNode();
275
- const children = this.getChildren();
276
- children.forEach(child => paragraph.append(child));
277
- this.replace(paragraph);
278
- return true;
279
- }
280
- setLanguage(language) {
281
- const writable = this.getWritable();
282
- writable.__language = language;
283
- writable.__isSyntaxHighlightSupported = isLanguageSupportedByPrism(language);
284
- }
285
- getLanguage() {
286
- return this.getLatest().__language;
287
- }
288
- getIsSyntaxHighlightSupported() {
289
- return this.getLatest().__isSyntaxHighlightSupported;
290
- }
291
- }
292
- function $createCodeNode(language) {
293
- return lexical.$applyNodeReplacement(new CodeNode(language));
294
- }
295
- function $isCodeNode(node) {
296
- return node instanceof CodeNode;
297
- }
298
- function $convertPreElement(domNode) {
299
- const language = domNode.getAttribute(LANGUAGE_DATA_ATTRIBUTE);
300
- return {
301
- node: $createCodeNode(language)
302
- };
303
- }
304
- function $convertDivElement(domNode) {
305
- // domNode is a <div> since we matched it by nodeName
306
- const div = domNode;
307
- const isCode = isCodeElement(div);
308
- if (!isCode && !isCodeChildElement(div)) {
309
- return {
310
- node: null
311
- };
312
- }
313
- return {
314
- node: isCode ? $createCodeNode() : null
315
- };
316
- }
317
- function $convertTableElement() {
318
- return {
319
- node: $createCodeNode()
320
- };
321
- }
322
- function convertCodeNoop() {
323
- return {
324
- node: null
325
- };
326
- }
327
- function isCodeElement(div) {
328
- return div.style.fontFamily.match('monospace') !== null;
329
- }
330
- function isCodeChildElement(node) {
331
- let parent = node.parentElement;
332
- while (parent !== null) {
333
- if (isCodeElement(parent)) {
334
- return true;
335
- }
336
- parent = parent.parentElement;
337
- }
338
- return false;
339
- }
340
- function isGitHubCodeCell(cell) {
341
- return cell.classList.contains('js-file-line');
342
- }
343
- function isGitHubCodeTable(table) {
344
- return table.classList.contains('js-file-line-container');
345
- }
346
-
347
- /**
348
- * Copyright (c) Meta Platforms, Inc. and affiliates.
349
- *
350
- * This source code is licensed under the MIT license found in the
351
- * LICENSE file in the root directory of this source tree.
352
- *
353
- */
354
-
355
- const DEFAULT_CODE_LANGUAGE = 'javascript';
356
- const CODE_LANGUAGE_FRIENDLY_NAME_MAP = {
357
- c: 'C',
358
- clike: 'C-like',
359
- cpp: 'C++',
360
- css: 'CSS',
361
- html: 'HTML',
362
- java: 'Java',
363
- js: 'JavaScript',
364
- markdown: 'Markdown',
365
- objc: 'Objective-C',
366
- plain: 'Plain Text',
367
- powershell: 'PowerShell',
368
- py: 'Python',
369
- rust: 'Rust',
370
- sql: 'SQL',
371
- swift: 'Swift',
372
- typescript: 'TypeScript',
373
- xml: 'XML'
374
- };
375
- const CODE_LANGUAGE_MAP = {
376
- cpp: 'cpp',
377
- java: 'java',
378
- javascript: 'js',
379
- md: 'markdown',
380
- plaintext: 'plain',
381
- python: 'py',
382
- text: 'plain',
383
- ts: 'typescript'
384
- };
385
- function normalizeCodeLang(lang) {
386
- return CODE_LANGUAGE_MAP[lang] || lang;
387
- }
388
- function getLanguageFriendlyName(lang) {
389
- const _lang = normalizeCodeLang(lang);
390
- return CODE_LANGUAGE_FRIENDLY_NAME_MAP[_lang] || _lang;
391
- }
392
- const getDefaultCodeLanguage = () => DEFAULT_CODE_LANGUAGE;
393
- const getCodeLanguages = () => Object.keys(window.Prism.languages).filter(
394
- // Prism has several language helpers mixed into languages object
395
- // so filtering them out here to get langs list
396
- language => typeof window.Prism.languages[language] !== 'function').sort();
397
-
398
- /** @noInheritDoc */
399
- class CodeHighlightNode extends lexical.TextNode {
400
- /** @internal */
401
-
402
- constructor(text, highlightType, key) {
403
- super(text, key);
404
- this.__highlightType = highlightType;
405
- }
406
- static getType() {
407
- return 'code-highlight';
408
- }
409
- static clone(node) {
410
- return new CodeHighlightNode(node.__text, node.__highlightType || undefined, node.__key);
411
- }
412
- getHighlightType() {
413
- const self = this.getLatest();
414
- return self.__highlightType;
415
- }
416
- canHaveFormat() {
417
- return false;
418
- }
419
- createDOM(config) {
420
- const element = super.createDOM(config);
421
- const className = getHighlightThemeClass(config.theme, this.__highlightType);
422
- LexicalUtils.addClassNamesToElement(element, className);
423
- return element;
424
- }
425
- updateDOM(prevNode, dom, config) {
426
- const update = super.updateDOM(prevNode, dom, config);
427
- const prevClassName = getHighlightThemeClass(config.theme, prevNode.__highlightType);
428
- const nextClassName = getHighlightThemeClass(config.theme, this.__highlightType);
429
- if (prevClassName !== nextClassName) {
430
- if (prevClassName) {
431
- LexicalUtils.removeClassNamesFromElement(dom, prevClassName);
432
- }
433
- if (nextClassName) {
434
- LexicalUtils.addClassNamesToElement(dom, nextClassName);
435
- }
436
- }
437
- return update;
438
- }
439
- static importJSON(serializedNode) {
440
- const node = $createCodeHighlightNode(serializedNode.text, serializedNode.highlightType);
441
- node.setFormat(serializedNode.format);
442
- node.setDetail(serializedNode.detail);
443
- node.setMode(serializedNode.mode);
444
- node.setStyle(serializedNode.style);
445
- return node;
446
- }
447
- exportJSON() {
448
- return {
449
- ...super.exportJSON(),
450
- highlightType: this.getHighlightType(),
451
- type: 'code-highlight',
452
- version: 1
453
- };
454
- }
455
-
456
- // Prevent formatting (bold, underline, etc)
457
- setFormat(format) {
458
- return this;
459
- }
460
- isParentRequired() {
461
- return true;
462
- }
463
- createParentElementNode() {
464
- return $createCodeNode();
465
- }
466
- }
467
- function getHighlightThemeClass(theme, highlightType) {
468
- return highlightType && theme && theme.codeHighlight && theme.codeHighlight[highlightType];
469
- }
470
- function $createCodeHighlightNode(text, highlightType) {
471
- return lexical.$applyNodeReplacement(new CodeHighlightNode(text, highlightType));
472
- }
473
- function $isCodeHighlightNode(node) {
474
- return node instanceof CodeHighlightNode;
475
- }
476
- function getFirstCodeNodeOfLine(anchor) {
477
- let previousNode = anchor;
478
- let node = anchor;
479
- while ($isCodeHighlightNode(node) || lexical.$isTabNode(node)) {
480
- previousNode = node;
481
- node = node.getPreviousSibling();
482
- }
483
- return previousNode;
484
- }
485
- function getLastCodeNodeOfLine(anchor) {
486
- let nextNode = anchor;
487
- let node = anchor;
488
- while ($isCodeHighlightNode(node) || lexical.$isTabNode(node)) {
489
- nextNode = node;
490
- node = node.getNextSibling();
491
- }
492
- return nextNode;
493
- }
494
-
495
- /**
496
- * Copyright (c) Meta Platforms, Inc. and affiliates.
497
- *
498
- * This source code is licensed under the MIT license found in the
499
- * LICENSE file in the root directory of this source tree.
500
- *
501
- */
502
-
503
- const PrismTokenizer = {
504
- defaultLanguage: DEFAULT_CODE_LANGUAGE,
505
- tokenize(code, language) {
506
- return window.Prism.tokenize(code, window.Prism.languages[language || ''] || window.Prism.languages[this.defaultLanguage]);
507
- }
508
- };
509
- function getStartOfCodeInLine(anchor, offset) {
510
- let last = null;
511
- let lastNonBlank = null;
512
- let node = anchor;
513
- let nodeOffset = offset;
514
- let nodeTextContent = anchor.getTextContent();
515
- // eslint-disable-next-line no-constant-condition
516
- while (true) {
517
- if (nodeOffset === 0) {
518
- node = node.getPreviousSibling();
519
- if (node === null) {
520
- break;
521
- }
522
- if (!($isCodeHighlightNode(node) || lexical.$isTabNode(node) || lexical.$isLineBreakNode(node))) {
523
- throw Error(`Expected a valid Code Node: CodeHighlightNode, TabNode, LineBreakNode`);
524
- }
525
- if (lexical.$isLineBreakNode(node)) {
526
- last = {
527
- node,
528
- offset: 1
529
- };
530
- break;
531
- }
532
- nodeOffset = Math.max(0, node.getTextContentSize() - 1);
533
- nodeTextContent = node.getTextContent();
534
- } else {
535
- nodeOffset--;
536
- }
537
- const character = nodeTextContent[nodeOffset];
538
- if ($isCodeHighlightNode(node) && character !== ' ') {
539
- lastNonBlank = {
540
- node,
541
- offset: nodeOffset
542
- };
543
- }
544
- }
545
- // lastNonBlank !== null: anchor in the middle of code; move to line beginning
546
- if (lastNonBlank !== null) {
547
- return lastNonBlank;
548
- }
549
- // Spaces, tabs or nothing ahead of anchor
550
- let codeCharacterAtAnchorOffset = null;
551
- if (offset < anchor.getTextContentSize()) {
552
- if ($isCodeHighlightNode(anchor)) {
553
- codeCharacterAtAnchorOffset = anchor.getTextContent()[offset];
554
- }
555
- } else {
556
- const nextSibling = anchor.getNextSibling();
557
- if ($isCodeHighlightNode(nextSibling)) {
558
- codeCharacterAtAnchorOffset = nextSibling.getTextContent()[0];
559
- }
560
- }
561
- if (codeCharacterAtAnchorOffset !== null && codeCharacterAtAnchorOffset !== ' ') {
562
- // Borderline whitespace and code, move to line beginning
563
- return last;
564
- } else {
565
- const nextNonBlank = findNextNonBlankInLine(anchor, offset);
566
- if (nextNonBlank !== null) {
567
- return nextNonBlank;
568
- } else {
569
- return last;
570
- }
571
- }
572
- }
573
- function findNextNonBlankInLine(anchor, offset) {
574
- let node = anchor;
575
- let nodeOffset = offset;
576
- let nodeTextContent = anchor.getTextContent();
577
- let nodeTextContentSize = anchor.getTextContentSize();
578
- // eslint-disable-next-line no-constant-condition
579
- while (true) {
580
- if (!$isCodeHighlightNode(node) || nodeOffset === nodeTextContentSize) {
581
- node = node.getNextSibling();
582
- if (node === null || lexical.$isLineBreakNode(node)) {
583
- return null;
584
- }
585
- if ($isCodeHighlightNode(node)) {
586
- nodeOffset = 0;
587
- nodeTextContent = node.getTextContent();
588
- nodeTextContentSize = node.getTextContentSize();
589
- }
590
- }
591
- if ($isCodeHighlightNode(node)) {
592
- if (nodeTextContent[nodeOffset] !== ' ') {
593
- return {
594
- node,
595
- offset: nodeOffset
596
- };
597
- }
598
- nodeOffset++;
599
- }
600
- }
601
- }
602
- function getEndOfCodeInLine(anchor) {
603
- const lastNode = getLastCodeNodeOfLine(anchor);
604
- if (!!lexical.$isLineBreakNode(lastNode)) {
605
- throw Error(`Unexpected lineBreakNode in getEndOfCodeInLine`);
606
- }
607
- return lastNode;
608
- }
609
- function $textNodeTransform(node, editor, tokenizer) {
610
- // Since CodeNode has flat children structure we only need to check
611
- // if node's parent is a code node and run highlighting if so
612
- const parentNode = node.getParent();
613
- if ($isCodeNode(parentNode)) {
614
- codeNodeTransform(parentNode, editor, tokenizer);
615
- } else if ($isCodeHighlightNode(node)) {
616
- // When code block converted into paragraph or other element
617
- // code highlight nodes converted back to normal text
618
- node.replace(lexical.$createTextNode(node.__text));
619
- }
620
- }
621
- function updateCodeGutter(node, editor) {
622
- const codeElement = editor.getElementByKey(node.getKey());
623
- if (codeElement === null) {
624
- return;
625
- }
626
- const children = node.getChildren();
627
- const childrenLength = children.length;
628
- // @ts-ignore: internal field
629
- if (childrenLength === codeElement.__cachedChildrenLength) {
630
- // Avoid updating the attribute if the children length hasn't changed.
631
- return;
632
- }
633
- // @ts-ignore:: internal field
634
- codeElement.__cachedChildrenLength = childrenLength;
635
- let gutter = '1';
636
- let count = 1;
637
- for (let i = 0; i < childrenLength; i++) {
638
- if (lexical.$isLineBreakNode(children[i])) {
639
- gutter += '\n' + ++count;
640
- }
641
- }
642
- codeElement.setAttribute('data-gutter', gutter);
643
- }
644
-
645
- // Using `skipTransforms` to prevent extra transforms since reformatting the code
646
- // will not affect code block content itself.
647
- //
648
- // Using extra cache (`nodesCurrentlyHighlighting`) since both CodeNode and CodeHighlightNode
649
- // transforms might be called at the same time (e.g. new CodeHighlight node inserted) and
650
- // in both cases we'll rerun whole reformatting over CodeNode, which is redundant.
651
- // Especially when pasting code into CodeBlock.
652
-
653
- const nodesCurrentlyHighlighting = new Set();
654
- function codeNodeTransform(node, editor, tokenizer) {
655
- const nodeKey = node.getKey();
656
- if (nodesCurrentlyHighlighting.has(nodeKey)) {
657
- return;
658
- }
659
- nodesCurrentlyHighlighting.add(nodeKey);
660
-
661
- // When new code block inserted it might not have language selected
662
- if (node.getLanguage() === undefined) {
663
- node.setLanguage(tokenizer.defaultLanguage);
664
- }
665
-
666
- // Using nested update call to pass `skipTransforms` since we don't want
667
- // each individual codehighlight node to be transformed again as it's already
668
- // in its final state
669
- editor.update(() => {
670
- $updateAndRetainSelection(nodeKey, () => {
671
- const currentNode = lexical.$getNodeByKey(nodeKey);
672
- if (!$isCodeNode(currentNode) || !currentNode.isAttached()) {
673
- return false;
674
- }
675
- const code = currentNode.getTextContent();
676
- const tokens = tokenizer.tokenize(code, currentNode.getLanguage() || tokenizer.defaultLanguage);
677
- const highlightNodes = $getHighlightNodes(tokens);
678
- const diffRange = getDiffRange(currentNode.getChildren(), highlightNodes);
679
- const {
680
- from,
681
- to,
682
- nodesForReplacement
683
- } = diffRange;
684
- if (from !== to || nodesForReplacement.length) {
685
- node.splice(from, to - from, nodesForReplacement);
686
- return true;
687
- }
688
- return false;
689
- });
690
- }, {
691
- onUpdate: () => {
692
- nodesCurrentlyHighlighting.delete(nodeKey);
693
- },
694
- skipTransforms: true
695
- });
696
- }
697
- function $getHighlightNodes(tokens, type) {
698
- const nodes = [];
699
- for (const token of tokens) {
700
- if (typeof token === 'string') {
701
- const partials = token.split(/(\n|\t)/);
702
- const partialsLength = partials.length;
703
- for (let i = 0; i < partialsLength; i++) {
704
- const part = partials[i];
705
- if (part === '\n' || part === '\r\n') {
706
- nodes.push(lexical.$createLineBreakNode());
707
- } else if (part === '\t') {
708
- nodes.push(lexical.$createTabNode());
709
- } else if (part.length > 0) {
710
- nodes.push($createCodeHighlightNode(part, type));
711
- }
712
- }
713
- } else {
714
- const {
715
- content
716
- } = token;
717
- if (typeof content === 'string') {
718
- nodes.push(...$getHighlightNodes([content], token.type));
719
- } else if (Array.isArray(content)) {
720
- nodes.push(...$getHighlightNodes(content, token.type));
721
- }
722
- }
723
- }
724
- return nodes;
725
- }
726
-
727
- // Wrapping update function into selection retainer, that tries to keep cursor at the same
728
- // position as before.
729
- function $updateAndRetainSelection(nodeKey, updateFn) {
730
- const node = lexical.$getNodeByKey(nodeKey);
731
- if (!$isCodeNode(node) || !node.isAttached()) {
732
- return;
733
- }
734
- const selection = lexical.$getSelection();
735
- // If it's not range selection (or null selection) there's no need to change it,
736
- // but we can still run highlighting logic
737
- if (!lexical.$isRangeSelection(selection)) {
738
- updateFn();
739
- return;
740
- }
741
- const anchor = selection.anchor;
742
- const anchorOffset = anchor.offset;
743
- const isNewLineAnchor = anchor.type === 'element' && lexical.$isLineBreakNode(node.getChildAtIndex(anchor.offset - 1));
744
- let textOffset = 0;
745
-
746
- // Calculating previous text offset (all text node prior to anchor + anchor own text offset)
747
- if (!isNewLineAnchor) {
748
- const anchorNode = anchor.getNode();
749
- textOffset = anchorOffset + anchorNode.getPreviousSiblings().reduce((offset, _node) => {
750
- return offset + _node.getTextContentSize();
751
- }, 0);
752
- }
753
- const hasChanges = updateFn();
754
- if (!hasChanges) {
755
- return;
756
- }
757
-
758
- // Non-text anchors only happen for line breaks, otherwise
759
- // selection will be within text node (code highlight node)
760
- if (isNewLineAnchor) {
761
- anchor.getNode().select(anchorOffset, anchorOffset);
762
- return;
763
- }
764
-
765
- // If it was non-element anchor then we walk through child nodes
766
- // and looking for a position of original text offset
767
- node.getChildren().some(_node => {
768
- const isText = lexical.$isTextNode(_node);
769
- if (isText || lexical.$isLineBreakNode(_node)) {
770
- const textContentSize = _node.getTextContentSize();
771
- if (isText && textContentSize >= textOffset) {
772
- _node.select(textOffset, textOffset);
773
- return true;
774
- }
775
- textOffset -= textContentSize;
776
- }
777
- return false;
778
- });
779
- }
780
-
781
- // Finds minimal diff range between two nodes lists. It returns from/to range boundaries of prevNodes
782
- // that needs to be replaced with `nodes` (subset of nextNodes) to make prevNodes equal to nextNodes.
783
- function getDiffRange(prevNodes, nextNodes) {
784
- let leadingMatch = 0;
785
- while (leadingMatch < prevNodes.length) {
786
- if (!isEqual(prevNodes[leadingMatch], nextNodes[leadingMatch])) {
787
- break;
788
- }
789
- leadingMatch++;
790
- }
791
- const prevNodesLength = prevNodes.length;
792
- const nextNodesLength = nextNodes.length;
793
- const maxTrailingMatch = Math.min(prevNodesLength, nextNodesLength) - leadingMatch;
794
- let trailingMatch = 0;
795
- while (trailingMatch < maxTrailingMatch) {
796
- trailingMatch++;
797
- if (!isEqual(prevNodes[prevNodesLength - trailingMatch], nextNodes[nextNodesLength - trailingMatch])) {
798
- trailingMatch--;
799
- break;
800
- }
801
- }
802
- const from = leadingMatch;
803
- const to = prevNodesLength - trailingMatch;
804
- const nodesForReplacement = nextNodes.slice(leadingMatch, nextNodesLength - trailingMatch);
805
- return {
806
- from,
807
- nodesForReplacement,
808
- to
809
- };
810
- }
811
- function isEqual(nodeA, nodeB) {
812
- // Only checking for code higlight nodes, tabs and linebreaks. If it's regular text node
813
- // returning false so that it's transformed into code highlight node
814
- return $isCodeHighlightNode(nodeA) && $isCodeHighlightNode(nodeB) && nodeA.__text === nodeB.__text && nodeA.__highlightType === nodeB.__highlightType || lexical.$isTabNode(nodeA) && lexical.$isTabNode(nodeB) || lexical.$isLineBreakNode(nodeA) && lexical.$isLineBreakNode(nodeB);
815
- }
816
- function $isSelectionInCode(selection) {
817
- if (!lexical.$isRangeSelection(selection)) {
818
- return false;
819
- }
820
- const anchorNode = selection.anchor.getNode();
821
- const focusNode = selection.focus.getNode();
822
- if (anchorNode.is(focusNode) && $isCodeNode(anchorNode)) {
823
- return true;
824
- }
825
- const anchorParent = anchorNode.getParent();
826
- return $isCodeNode(anchorParent) && anchorParent.is(focusNode.getParent());
827
- }
828
- function $getCodeLines(selection) {
829
- const nodes = selection.getNodes();
830
- const lines = [[]];
831
- if (nodes.length === 1 && $isCodeNode(nodes[0])) {
832
- return lines;
833
- }
834
- let lastLine = lines[0];
835
- for (let i = 0; i < nodes.length; i++) {
836
- const node = nodes[i];
837
- if (!($isCodeHighlightNode(node) || lexical.$isTabNode(node) || lexical.$isLineBreakNode(node))) {
838
- throw Error(`Expected selection to be inside CodeBlock and consisting of CodeHighlightNode, TabNode and LineBreakNode`);
839
- }
840
- if (lexical.$isLineBreakNode(node)) {
841
- if (i !== 0 && lastLine.length > 0) {
842
- lastLine = [];
843
- lines.push(lastLine);
844
- }
845
- } else {
846
- lastLine.push(node);
847
- }
848
- }
849
- return lines;
850
- }
851
- function $handleTab(shiftKey) {
852
- const selection = lexical.$getSelection();
853
- if (!lexical.$isRangeSelection(selection) || !$isSelectionInCode(selection)) {
854
- return null;
855
- }
856
- const indentOrOutdent = !shiftKey ? lexical.INDENT_CONTENT_COMMAND : lexical.OUTDENT_CONTENT_COMMAND;
857
- const tabOrOutdent = !shiftKey ? lexical.INSERT_TAB_COMMAND : lexical.OUTDENT_CONTENT_COMMAND;
858
- // 1. If multiple lines selected: indent/outdent
859
- const codeLines = $getCodeLines(selection);
860
- if (codeLines.length > 1) {
861
- return indentOrOutdent;
862
- }
863
- // 2. If entire line selected: indent/outdent
864
- const selectionNodes = selection.getNodes();
865
- const firstNode = selectionNodes[0];
866
- if (!($isCodeNode(firstNode) || $isCodeHighlightNode(firstNode) || lexical.$isTabNode(firstNode) || lexical.$isLineBreakNode(firstNode))) {
867
- throw Error(`Expected selection firstNode to be CodeHighlightNode or TabNode`);
868
- }
869
- if ($isCodeNode(firstNode)) {
870
- return indentOrOutdent;
871
- }
872
- const firstOfLine = getFirstCodeNodeOfLine(firstNode);
873
- const lastOfLine = getLastCodeNodeOfLine(firstNode);
874
- const anchor = selection.anchor;
875
- const focus = selection.focus;
876
- let selectionFirst;
877
- let selectionLast;
878
- if (focus.isBefore(anchor)) {
879
- selectionFirst = focus;
880
- selectionLast = anchor;
881
- } else {
882
- selectionFirst = anchor;
883
- selectionLast = focus;
884
- }
885
- if (firstOfLine !== null && lastOfLine !== null && selectionFirst.key === firstOfLine.getKey() && selectionFirst.offset === 0 && selectionLast.key === lastOfLine.getKey() && selectionLast.offset === lastOfLine.getTextContentSize()) {
886
- return indentOrOutdent;
887
- }
888
- // 3. Else: tab/outdent
889
- return tabOrOutdent;
890
- }
891
- function $handleMultilineIndent(type) {
892
- const selection = lexical.$getSelection();
893
- if (!lexical.$isRangeSelection(selection) || !$isSelectionInCode(selection)) {
894
- return false;
895
- }
896
- const codeLines = $getCodeLines(selection);
897
- const codeLinesLength = codeLines.length;
898
- // Multiple lines selection
899
- if (codeLines.length > 1) {
900
- for (let i = 0; i < codeLinesLength; i++) {
901
- const line = codeLines[i];
902
- if (line.length > 0) {
903
- let firstOfLine = line[0];
904
- // First and last lines might not be complete
905
- if (i === 0) {
906
- firstOfLine = getFirstCodeNodeOfLine(firstOfLine);
907
- }
908
- if (firstOfLine !== null) {
909
- if (type === lexical.INDENT_CONTENT_COMMAND) {
910
- firstOfLine.insertBefore(lexical.$createTabNode());
911
- } else if (lexical.$isTabNode(firstOfLine)) {
912
- firstOfLine.remove();
913
- }
914
- }
915
- }
916
- }
917
- return true;
918
- }
919
- // Just one line
920
- const selectionNodes = selection.getNodes();
921
- const firstNode = selectionNodes[0];
922
- if (!($isCodeNode(firstNode) || $isCodeHighlightNode(firstNode) || lexical.$isTabNode(firstNode) || lexical.$isLineBreakNode(firstNode))) {
923
- throw Error(`Expected selection firstNode to be CodeHighlightNode or CodeTabNode`);
924
- }
925
- if ($isCodeNode(firstNode)) {
926
- // CodeNode is empty
927
- if (type === lexical.INDENT_CONTENT_COMMAND) {
928
- selection.insertNodes([lexical.$createTabNode()]);
929
- }
930
- return true;
931
- }
932
- const firstOfLine = getFirstCodeNodeOfLine(firstNode);
933
- if (!(firstOfLine !== null)) {
934
- throw Error(`Expected getFirstCodeNodeOfLine to return a valid Code Node`);
935
- }
936
- if (type === lexical.INDENT_CONTENT_COMMAND) {
937
- if (lexical.$isLineBreakNode(firstOfLine)) {
938
- firstOfLine.insertAfter(lexical.$createTabNode());
939
- } else {
940
- firstOfLine.insertBefore(lexical.$createTabNode());
941
- }
942
- } else if (lexical.$isTabNode(firstOfLine)) {
943
- firstOfLine.remove();
944
- }
945
- return true;
946
- }
947
- function $handleShiftLines(type, event) {
948
- // We only care about the alt+arrow keys
949
- const selection = lexical.$getSelection();
950
- if (!lexical.$isRangeSelection(selection)) {
951
- return false;
952
- }
953
-
954
- // I'm not quite sure why, but it seems like calling anchor.getNode() collapses the selection here
955
- // So first, get the anchor and the focus, then get their nodes
956
- const {
957
- anchor,
958
- focus
959
- } = selection;
960
- const anchorOffset = anchor.offset;
961
- const focusOffset = focus.offset;
962
- const anchorNode = anchor.getNode();
963
- const focusNode = focus.getNode();
964
- const arrowIsUp = type === lexical.KEY_ARROW_UP_COMMAND;
965
-
966
- // Ensure the selection is within the codeblock
967
- if (!$isSelectionInCode(selection) || !($isCodeHighlightNode(anchorNode) || lexical.$isTabNode(anchorNode)) || !($isCodeHighlightNode(focusNode) || lexical.$isTabNode(focusNode))) {
968
- return false;
969
- }
970
- if (!event.altKey) {
971
- // Handle moving selection out of the code block, given there are no
972
- // sibling thats can natively take the selection.
973
- if (selection.isCollapsed()) {
974
- const codeNode = anchorNode.getParentOrThrow();
975
- if (arrowIsUp && anchorOffset === 0 && anchorNode.getPreviousSibling() === null) {
976
- const codeNodeSibling = codeNode.getPreviousSibling();
977
- if (codeNodeSibling === null) {
978
- codeNode.selectPrevious();
979
- event.preventDefault();
980
- return true;
981
- }
982
- } else if (!arrowIsUp && anchorOffset === anchorNode.getTextContentSize() && anchorNode.getNextSibling() === null) {
983
- const codeNodeSibling = codeNode.getNextSibling();
984
- if (codeNodeSibling === null) {
985
- codeNode.selectNext();
986
- event.preventDefault();
987
- return true;
988
- }
989
- }
990
- }
991
- return false;
992
- }
993
- let start;
994
- let end;
995
- if (anchorNode.isBefore(focusNode)) {
996
- start = getFirstCodeNodeOfLine(anchorNode);
997
- end = getLastCodeNodeOfLine(focusNode);
998
- } else {
999
- start = getFirstCodeNodeOfLine(focusNode);
1000
- end = getLastCodeNodeOfLine(anchorNode);
1001
- }
1002
- if (start == null || end == null) {
1003
- return false;
1004
- }
1005
- const range = start.getNodesBetween(end);
1006
- for (let i = 0; i < range.length; i++) {
1007
- const node = range[i];
1008
- if (!$isCodeHighlightNode(node) && !lexical.$isTabNode(node) && !lexical.$isLineBreakNode(node)) {
1009
- return false;
1010
- }
1011
- }
1012
-
1013
- // After this point, we know the selection is within the codeblock. We may not be able to
1014
- // actually move the lines around, but we want to return true either way to prevent
1015
- // the event's default behavior
1016
- event.preventDefault();
1017
- event.stopPropagation(); // required to stop cursor movement under Firefox
1018
-
1019
- const linebreak = arrowIsUp ? start.getPreviousSibling() : end.getNextSibling();
1020
- if (!lexical.$isLineBreakNode(linebreak)) {
1021
- return true;
1022
- }
1023
- const sibling = arrowIsUp ? linebreak.getPreviousSibling() : linebreak.getNextSibling();
1024
- if (sibling == null) {
1025
- return true;
1026
- }
1027
- const maybeInsertionPoint = $isCodeHighlightNode(sibling) || lexical.$isTabNode(sibling) || lexical.$isLineBreakNode(sibling) ? arrowIsUp ? getFirstCodeNodeOfLine(sibling) : getLastCodeNodeOfLine(sibling) : null;
1028
- let insertionPoint = maybeInsertionPoint != null ? maybeInsertionPoint : sibling;
1029
- linebreak.remove();
1030
- range.forEach(node => node.remove());
1031
- if (type === lexical.KEY_ARROW_UP_COMMAND) {
1032
- range.forEach(node => insertionPoint.insertBefore(node));
1033
- insertionPoint.insertBefore(linebreak);
1034
- } else {
1035
- insertionPoint.insertAfter(linebreak);
1036
- insertionPoint = linebreak;
1037
- range.forEach(node => {
1038
- insertionPoint.insertAfter(node);
1039
- insertionPoint = node;
1040
- });
1041
- }
1042
- selection.setTextNodeRange(anchorNode, anchorOffset, focusNode, focusOffset);
1043
- return true;
1044
- }
1045
- function $handleMoveTo(type, event) {
1046
- const selection = lexical.$getSelection();
1047
- if (!lexical.$isRangeSelection(selection)) {
1048
- return false;
1049
- }
1050
- const {
1051
- anchor,
1052
- focus
1053
- } = selection;
1054
- const anchorNode = anchor.getNode();
1055
- const focusNode = focus.getNode();
1056
- const isMoveToStart = type === lexical.MOVE_TO_START;
1057
- if (!($isCodeHighlightNode(anchorNode) || lexical.$isTabNode(anchorNode)) || !($isCodeHighlightNode(focusNode) || lexical.$isTabNode(focusNode))) {
1058
- return false;
1059
- }
1060
- if (isMoveToStart) {
1061
- const start = getStartOfCodeInLine(focusNode, focus.offset);
1062
- if (start !== null) {
1063
- const {
1064
- node,
1065
- offset
1066
- } = start;
1067
- if (lexical.$isLineBreakNode(node)) {
1068
- node.selectNext(0, 0);
1069
- } else {
1070
- selection.setTextNodeRange(node, offset, node, offset);
1071
- }
1072
- } else {
1073
- focusNode.getParentOrThrow().selectStart();
1074
- }
1075
- } else {
1076
- const node = getEndOfCodeInLine(focusNode);
1077
- node.select();
1078
- }
1079
- event.preventDefault();
1080
- event.stopPropagation();
1081
- return true;
1082
- }
1083
- function registerCodeHighlighting(editor, tokenizer) {
1084
- if (!editor.hasNodes([CodeNode, CodeHighlightNode])) {
1085
- throw new Error('CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor');
1086
- }
1087
- if (tokenizer == null) {
1088
- tokenizer = PrismTokenizer;
1089
- }
1090
- return LexicalUtils.mergeRegister(editor.registerMutationListener(CodeNode, mutations => {
1091
- editor.update(() => {
1092
- for (const [key, type] of mutations) {
1093
- if (type !== 'destroyed') {
1094
- const node = lexical.$getNodeByKey(key);
1095
- if (node !== null) {
1096
- updateCodeGutter(node, editor);
1097
- }
1098
- }
1099
- }
1100
- });
1101
- }, {
1102
- skipInitialization: false
1103
- }), editor.registerNodeTransform(CodeNode, node => codeNodeTransform(node, editor, tokenizer)), editor.registerNodeTransform(lexical.TextNode, node => $textNodeTransform(node, editor, tokenizer)), editor.registerNodeTransform(CodeHighlightNode, node => $textNodeTransform(node, editor, tokenizer)), editor.registerCommand(lexical.KEY_TAB_COMMAND, event => {
1104
- const command = $handleTab(event.shiftKey);
1105
- if (command === null) {
1106
- return false;
1107
- }
1108
- event.preventDefault();
1109
- editor.dispatchCommand(command, undefined);
1110
- return true;
1111
- }, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.INSERT_TAB_COMMAND, () => {
1112
- const selection = lexical.$getSelection();
1113
- if (!$isSelectionInCode(selection)) {
1114
- return false;
1115
- }
1116
- lexical.$insertNodes([lexical.$createTabNode()]);
1117
- return true;
1118
- }, 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));
1119
- }
1120
-
1121
- exports.$createCodeHighlightNode = $createCodeHighlightNode;
1122
- exports.$createCodeNode = $createCodeNode;
1123
- exports.$isCodeHighlightNode = $isCodeHighlightNode;
1124
- exports.$isCodeNode = $isCodeNode;
1125
- exports.CODE_LANGUAGE_FRIENDLY_NAME_MAP = CODE_LANGUAGE_FRIENDLY_NAME_MAP;
1126
- exports.CODE_LANGUAGE_MAP = CODE_LANGUAGE_MAP;
1127
- exports.CodeHighlightNode = CodeHighlightNode;
1128
- exports.CodeNode = CodeNode;
1129
- exports.DEFAULT_CODE_LANGUAGE = DEFAULT_CODE_LANGUAGE;
1130
- exports.PrismTokenizer = PrismTokenizer;
1131
- exports.getCodeLanguages = getCodeLanguages;
1132
- exports.getDefaultCodeLanguage = getDefaultCodeLanguage;
1133
- exports.getEndOfCodeInLine = getEndOfCodeInLine;
1134
- exports.getFirstCodeNodeOfLine = getFirstCodeNodeOfLine;
1135
- exports.getLanguageFriendlyName = getLanguageFriendlyName;
1136
- exports.getLastCodeNodeOfLine = getLastCodeNodeOfLine;
1137
- exports.getStartOfCodeInLine = getStartOfCodeInLine;
1138
- exports.normalizeCodeLang = normalizeCodeLang;
1139
- exports.registerCodeHighlighting = registerCodeHighlighting;