@react-fabric/lexical 1.0.0-beta.60 → 1.0.0-beta.61

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 (127) hide show
  1. package/dist/cjs/{packages/lexical/src/components → components}/StickyComponent.js +2 -3
  2. package/dist/cjs/{packages/lexical/src/editor → editor}/LexicalEditor.js +5 -5
  3. package/dist/cjs/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +101 -95
  4. package/dist/cjs/i18n/ar.json.js +24 -0
  5. package/dist/cjs/i18n/en.json.js +24 -0
  6. package/dist/cjs/i18n/index.js +33 -0
  7. package/dist/cjs/{packages/lexical/src/index.js → index.js} +1 -0
  8. package/dist/cjs/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +5 -5
  9. package/dist/cjs/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +35 -22
  10. package/dist/esm/{packages/lexical/src/components → components}/StickyComponent.js +2 -3
  11. package/dist/esm/{packages/lexical/src/editor → editor}/LexicalEditor.js +4 -4
  12. package/dist/esm/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +43 -37
  13. package/dist/esm/i18n/ar.json.js +21 -0
  14. package/dist/esm/i18n/en.json.js +21 -0
  15. package/dist/esm/i18n/index.js +33 -0
  16. package/dist/esm/{packages/lexical/src/index.js → index.js} +1 -0
  17. package/dist/esm/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +2 -2
  18. package/dist/esm/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +36 -23
  19. package/dist/types/hooks/useFormatWatcher.d.ts +1 -0
  20. package/dist/types/i18n/index.d.ts +1 -0
  21. package/dist/types/index.d.ts +1 -0
  22. package/package.json +5 -5
  23. package/src/components/StickyComponent.tsx +2 -3
  24. package/src/hooks/useFormatWatcher.ts +3 -0
  25. package/src/i18n/ar.json +16 -0
  26. package/src/i18n/en.json +16 -0
  27. package/src/i18n/index.ts +28 -0
  28. package/src/index.ts +2 -0
  29. package/src/plugins/ToolbarPlugin.tsx +34 -23
  30. package/dist/cjs/_virtual/prism.js +0 -5
  31. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -474
  32. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -21
  33. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -27
  34. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +0 -1537
  35. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +0 -25
  36. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +0 -57
  37. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +0 -252
  38. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +0 -19
  39. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +0 -18
  40. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +0 -572
  41. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +0 -17
  42. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +0 -25
  43. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +0 -1614
  44. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +0 -29
  45. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +0 -36
  46. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -838
  47. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +0 -29
  48. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -26
  49. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -1057
  50. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +0 -39
  51. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -45
  52. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +0 -4207
  53. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +0 -23
  54. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +0 -68
  55. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -1166
  56. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +0 -47
  57. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -81
  58. package/dist/cjs/node_modules/@mdi/js/mdi.js +0 -8
  59. package/dist/cjs/node_modules/prismjs/components/prism-c.js +0 -82
  60. package/dist/cjs/node_modules/prismjs/components/prism-clike.js +0 -33
  61. package/dist/cjs/node_modules/prismjs/components/prism-cpp.js +0 -101
  62. package/dist/cjs/node_modules/prismjs/components/prism-css.js +0 -66
  63. package/dist/cjs/node_modules/prismjs/components/prism-java.js +0 -126
  64. package/dist/cjs/node_modules/prismjs/components/prism-javascript.js +0 -174
  65. package/dist/cjs/node_modules/prismjs/components/prism-markdown.js +0 -417
  66. package/dist/cjs/node_modules/prismjs/components/prism-markup.js +0 -188
  67. package/dist/cjs/node_modules/prismjs/components/prism-objectivec.js +0 -14
  68. package/dist/cjs/node_modules/prismjs/components/prism-powershell.js +0 -60
  69. package/dist/cjs/node_modules/prismjs/components/prism-python.js +0 -67
  70. package/dist/cjs/node_modules/prismjs/components/prism-rust.js +0 -130
  71. package/dist/cjs/node_modules/prismjs/components/prism-sql.js +0 -34
  72. package/dist/cjs/node_modules/prismjs/components/prism-swift.js +0 -150
  73. package/dist/cjs/node_modules/prismjs/components/prism-typescript.js +0 -62
  74. package/dist/cjs/node_modules/prismjs/prism.js +0 -1951
  75. package/dist/esm/_virtual/prism.js +0 -5
  76. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -465
  77. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -19
  78. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -18
  79. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +0 -1512
  80. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +0 -21
  81. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +0 -32
  82. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +0 -251
  83. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +0 -18
  84. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +0 -17
  85. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +0 -563
  86. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +0 -17
  87. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +0 -16
  88. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +0 -1595
  89. package/dist/esm/node_modules/@lexical/list/LexicalList.js +0 -23
  90. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +0 -17
  91. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -830
  92. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +0 -23
  93. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -18
  94. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -1028
  95. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +0 -28
  96. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -16
  97. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +0 -4156
  98. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +0 -20
  99. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +0 -17
  100. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -1102
  101. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +0 -32
  102. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -17
  103. package/dist/esm/node_modules/@mdi/js/mdi.js +0 -7
  104. package/dist/esm/node_modules/prismjs/components/prism-c.js +0 -82
  105. package/dist/esm/node_modules/prismjs/components/prism-clike.js +0 -33
  106. package/dist/esm/node_modules/prismjs/components/prism-cpp.js +0 -101
  107. package/dist/esm/node_modules/prismjs/components/prism-css.js +0 -66
  108. package/dist/esm/node_modules/prismjs/components/prism-java.js +0 -126
  109. package/dist/esm/node_modules/prismjs/components/prism-javascript.js +0 -174
  110. package/dist/esm/node_modules/prismjs/components/prism-markdown.js +0 -417
  111. package/dist/esm/node_modules/prismjs/components/prism-markup.js +0 -188
  112. package/dist/esm/node_modules/prismjs/components/prism-objectivec.js +0 -14
  113. package/dist/esm/node_modules/prismjs/components/prism-powershell.js +0 -60
  114. package/dist/esm/node_modules/prismjs/components/prism-python.js +0 -67
  115. package/dist/esm/node_modules/prismjs/components/prism-rust.js +0 -130
  116. package/dist/esm/node_modules/prismjs/components/prism-sql.js +0 -34
  117. package/dist/esm/node_modules/prismjs/components/prism-swift.js +0 -150
  118. package/dist/esm/node_modules/prismjs/components/prism-typescript.js +0 -62
  119. package/dist/esm/node_modules/prismjs/prism.js +0 -1951
  120. /package/dist/cjs/{packages/lexical/src/editor → editor}/theme.js +0 -0
  121. /package/dist/cjs/{packages/lexical/src/nodes → nodes}/StickyNode.js +0 -0
  122. /package/dist/cjs/{packages/lexical/src/plugins → plugins}/StickyPlugin.js +0 -0
  123. /package/dist/cjs/{packages/lexical/src/utils → utils}/index.js +0 -0
  124. /package/dist/esm/{packages/lexical/src/editor → editor}/theme.js +0 -0
  125. /package/dist/esm/{packages/lexical/src/nodes → nodes}/StickyNode.js +0 -0
  126. /package/dist/esm/{packages/lexical/src/plugins → plugins}/StickyPlugin.js +0 -0
  127. /package/dist/esm/{packages/lexical/src/utils → utils}/index.js +0 -0
@@ -1,838 +0,0 @@
1
- 'use strict';
2
-
3
- var LexicalClipboard = require('../clipboard/LexicalClipboard.js');
4
- var LexicalSelection = require('../selection/LexicalSelection.js');
5
- var LexicalUtils = require('../utils/LexicalUtils.js');
6
- var lexical = require('lexical');
7
-
8
- /**
9
- * Copyright (c) Meta Platforms, Inc. and affiliates.
10
- *
11
- * This source code is licensed under the MIT license found in the
12
- * LICENSE file in the root directory of this source tree.
13
- *
14
- */
15
-
16
-
17
- /**
18
- * Copyright (c) Meta Platforms, Inc. and affiliates.
19
- *
20
- * This source code is licensed under the MIT license found in the
21
- * LICENSE file in the root directory of this source tree.
22
- *
23
- */
24
-
25
- function caretFromPoint(x, y) {
26
- if (typeof document.caretRangeFromPoint !== 'undefined') {
27
- const range = document.caretRangeFromPoint(x, y);
28
- if (range === null) {
29
- return null;
30
- }
31
- return {
32
- node: range.startContainer,
33
- offset: range.startOffset
34
- };
35
- // @ts-ignore
36
- } else if (document.caretPositionFromPoint !== 'undefined') {
37
- // @ts-ignore FF - no types
38
- const range = document.caretPositionFromPoint(x, y);
39
- if (range === null) {
40
- return null;
41
- }
42
- return {
43
- node: range.offsetNode,
44
- offset: range.offset
45
- };
46
- } else {
47
- // Gracefully handle IE
48
- return null;
49
- }
50
- }
51
-
52
- /**
53
- * Copyright (c) Meta Platforms, Inc. and affiliates.
54
- *
55
- * This source code is licensed under the MIT license found in the
56
- * LICENSE file in the root directory of this source tree.
57
- *
58
- */
59
-
60
- const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
61
-
62
- /**
63
- * Copyright (c) Meta Platforms, Inc. and affiliates.
64
- *
65
- * This source code is licensed under the MIT license found in the
66
- * LICENSE file in the root directory of this source tree.
67
- *
68
- */
69
-
70
- const documentMode = CAN_USE_DOM && 'documentMode' in document ? document.documentMode : null;
71
- const CAN_USE_BEFORE_INPUT = CAN_USE_DOM && 'InputEvent' in window && !documentMode ? 'getTargetRanges' in new window.InputEvent('input') : false;
72
- const IS_SAFARI = CAN_USE_DOM && /Version\/[\d.]+.*Safari/.test(navigator.userAgent);
73
- const IS_IOS = CAN_USE_DOM && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
74
-
75
- // Keep these in case we need to use them in the future.
76
- // export const IS_WINDOWS: boolean = CAN_USE_DOM && /Win/.test(navigator.platform);
77
- const IS_CHROME = CAN_USE_DOM && /^(?=.*Chrome).*/i.test(navigator.userAgent);
78
- const IS_APPLE_WEBKIT = CAN_USE_DOM && /AppleWebKit\/[\d.]+/.test(navigator.userAgent) && !IS_CHROME;
79
-
80
- /**
81
- * Copyright (c) Meta Platforms, Inc. and affiliates.
82
- *
83
- * This source code is licensed under the MIT license found in the
84
- * LICENSE file in the root directory of this source tree.
85
- *
86
- */
87
-
88
- const DRAG_DROP_PASTE = lexical.createCommand('DRAG_DROP_PASTE_FILE');
89
- /** @noInheritDoc */
90
- class QuoteNode extends lexical.ElementNode {
91
- static getType() {
92
- return 'quote';
93
- }
94
- static clone(node) {
95
- return new QuoteNode(node.__key);
96
- }
97
-
98
- // View
99
-
100
- createDOM(config) {
101
- const element = document.createElement('blockquote');
102
- LexicalUtils.addClassNamesToElement(element, config.theme.quote);
103
- return element;
104
- }
105
- updateDOM(prevNode, dom) {
106
- return false;
107
- }
108
- static importDOM() {
109
- return {
110
- blockquote: node => ({
111
- conversion: $convertBlockquoteElement,
112
- priority: 0
113
- })
114
- };
115
- }
116
- exportDOM(editor) {
117
- const {
118
- element
119
- } = super.exportDOM(editor);
120
- if (LexicalUtils.isHTMLElement(element)) {
121
- if (this.isEmpty()) {
122
- element.append(document.createElement('br'));
123
- }
124
- const formatType = this.getFormatType();
125
- if (formatType) {
126
- element.style.textAlign = formatType;
127
- }
128
- const direction = this.getDirection();
129
- if (direction) {
130
- element.dir = direction;
131
- }
132
- }
133
- return {
134
- element
135
- };
136
- }
137
- static importJSON(serializedNode) {
138
- return $createQuoteNode().updateFromJSON(serializedNode);
139
- }
140
-
141
- // Mutation
142
-
143
- insertNewAfter(_, restoreSelection) {
144
- const newBlock = lexical.$createParagraphNode();
145
- const direction = this.getDirection();
146
- newBlock.setDirection(direction);
147
- this.insertAfter(newBlock, restoreSelection);
148
- return newBlock;
149
- }
150
- collapseAtStart() {
151
- const paragraph = lexical.$createParagraphNode();
152
- const children = this.getChildren();
153
- children.forEach(child => paragraph.append(child));
154
- this.replace(paragraph);
155
- return true;
156
- }
157
- canMergeWhenEmpty() {
158
- return true;
159
- }
160
- }
161
- function $createQuoteNode() {
162
- return lexical.$applyNodeReplacement(new QuoteNode());
163
- }
164
- function $isQuoteNode(node) {
165
- return node instanceof QuoteNode;
166
- }
167
- /** @noInheritDoc */
168
- class HeadingNode extends lexical.ElementNode {
169
- /** @internal */
170
-
171
- static getType() {
172
- return 'heading';
173
- }
174
- static clone(node) {
175
- return new HeadingNode(node.__tag, node.__key);
176
- }
177
- constructor(tag, key) {
178
- super(key);
179
- this.__tag = tag;
180
- }
181
- getTag() {
182
- return this.__tag;
183
- }
184
- setTag(tag) {
185
- const self = this.getWritable();
186
- this.__tag = tag;
187
- return self;
188
- }
189
-
190
- // View
191
-
192
- createDOM(config) {
193
- const tag = this.__tag;
194
- const element = document.createElement(tag);
195
- const theme = config.theme;
196
- const classNames = theme.heading;
197
- if (classNames !== undefined) {
198
- const className = classNames[tag];
199
- LexicalUtils.addClassNamesToElement(element, className);
200
- }
201
- return element;
202
- }
203
- updateDOM(prevNode, dom, config) {
204
- return prevNode.__tag !== this.__tag;
205
- }
206
- static importDOM() {
207
- return {
208
- h1: node => ({
209
- conversion: $convertHeadingElement,
210
- priority: 0
211
- }),
212
- h2: node => ({
213
- conversion: $convertHeadingElement,
214
- priority: 0
215
- }),
216
- h3: node => ({
217
- conversion: $convertHeadingElement,
218
- priority: 0
219
- }),
220
- h4: node => ({
221
- conversion: $convertHeadingElement,
222
- priority: 0
223
- }),
224
- h5: node => ({
225
- conversion: $convertHeadingElement,
226
- priority: 0
227
- }),
228
- h6: node => ({
229
- conversion: $convertHeadingElement,
230
- priority: 0
231
- }),
232
- p: node => {
233
- // domNode is a <p> since we matched it by nodeName
234
- const paragraph = node;
235
- const firstChild = paragraph.firstChild;
236
- if (firstChild !== null && isGoogleDocsTitle(firstChild)) {
237
- return {
238
- conversion: () => ({
239
- node: null
240
- }),
241
- priority: 3
242
- };
243
- }
244
- return null;
245
- },
246
- span: node => {
247
- if (isGoogleDocsTitle(node)) {
248
- return {
249
- conversion: domNode => {
250
- return {
251
- node: $createHeadingNode('h1')
252
- };
253
- },
254
- priority: 3
255
- };
256
- }
257
- return null;
258
- }
259
- };
260
- }
261
- exportDOM(editor) {
262
- const {
263
- element
264
- } = super.exportDOM(editor);
265
- if (LexicalUtils.isHTMLElement(element)) {
266
- if (this.isEmpty()) {
267
- element.append(document.createElement('br'));
268
- }
269
- const formatType = this.getFormatType();
270
- if (formatType) {
271
- element.style.textAlign = formatType;
272
- }
273
- const direction = this.getDirection();
274
- if (direction) {
275
- element.dir = direction;
276
- }
277
- }
278
- return {
279
- element
280
- };
281
- }
282
- static importJSON(serializedNode) {
283
- return $createHeadingNode(serializedNode.tag).updateFromJSON(serializedNode);
284
- }
285
- updateFromJSON(serializedNode) {
286
- return super.updateFromJSON(serializedNode).setTag(serializedNode.tag);
287
- }
288
- exportJSON() {
289
- return {
290
- ...super.exportJSON(),
291
- tag: this.getTag()
292
- };
293
- }
294
-
295
- // Mutation
296
- insertNewAfter(selection, restoreSelection = true) {
297
- const anchorOffet = selection ? selection.anchor.offset : 0;
298
- const lastDesc = this.getLastDescendant();
299
- const isAtEnd = !lastDesc || selection && selection.anchor.key === lastDesc.getKey() && anchorOffet === lastDesc.getTextContentSize();
300
- const newElement = isAtEnd || !selection ? lexical.$createParagraphNode() : $createHeadingNode(this.getTag());
301
- const direction = this.getDirection();
302
- newElement.setDirection(direction);
303
- this.insertAfter(newElement, restoreSelection);
304
- if (anchorOffet === 0 && !this.isEmpty() && selection) {
305
- const paragraph = lexical.$createParagraphNode();
306
- paragraph.select();
307
- this.replace(paragraph, true);
308
- }
309
- return newElement;
310
- }
311
- collapseAtStart() {
312
- const newElement = !this.isEmpty() ? $createHeadingNode(this.getTag()) : lexical.$createParagraphNode();
313
- const children = this.getChildren();
314
- children.forEach(child => newElement.append(child));
315
- this.replace(newElement);
316
- return true;
317
- }
318
- extractWithChild() {
319
- return true;
320
- }
321
- }
322
- function isGoogleDocsTitle(domNode) {
323
- if (domNode.nodeName.toLowerCase() === 'span') {
324
- return domNode.style.fontSize === '26pt';
325
- }
326
- return false;
327
- }
328
- function $convertHeadingElement(element) {
329
- const nodeName = element.nodeName.toLowerCase();
330
- let node = null;
331
- if (nodeName === 'h1' || nodeName === 'h2' || nodeName === 'h3' || nodeName === 'h4' || nodeName === 'h5' || nodeName === 'h6') {
332
- node = $createHeadingNode(nodeName);
333
- if (element.style !== null) {
334
- lexical.setNodeIndentFromDOM(element, node);
335
- node.setFormat(element.style.textAlign);
336
- }
337
- }
338
- return {
339
- node
340
- };
341
- }
342
- function $convertBlockquoteElement(element) {
343
- const node = $createQuoteNode();
344
- if (element.style !== null) {
345
- node.setFormat(element.style.textAlign);
346
- lexical.setNodeIndentFromDOM(element, node);
347
- }
348
- return {
349
- node
350
- };
351
- }
352
- function $createHeadingNode(headingTag = 'h1') {
353
- return lexical.$applyNodeReplacement(new HeadingNode(headingTag));
354
- }
355
- function $isHeadingNode(node) {
356
- return node instanceof HeadingNode;
357
- }
358
- function onPasteForRichText(event, editor) {
359
- event.preventDefault();
360
- editor.update(() => {
361
- const selection = lexical.$getSelection();
362
- const clipboardData = LexicalUtils.objectKlassEquals(event, InputEvent) || LexicalUtils.objectKlassEquals(event, KeyboardEvent) ? null : event.clipboardData;
363
- if (clipboardData != null && selection !== null) {
364
- LexicalClipboard.$insertDataTransferForRichText(clipboardData, selection, editor);
365
- }
366
- }, {
367
- tag: lexical.PASTE_TAG
368
- });
369
- }
370
- async function onCutForRichText(event, editor) {
371
- await LexicalClipboard.copyToClipboard(editor, LexicalUtils.objectKlassEquals(event, ClipboardEvent) ? event : null);
372
- editor.update(() => {
373
- const selection = lexical.$getSelection();
374
- if (lexical.$isRangeSelection(selection)) {
375
- selection.removeText();
376
- } else if (lexical.$isNodeSelection(selection)) {
377
- selection.getNodes().forEach(node => node.remove());
378
- }
379
- });
380
- }
381
-
382
- // Clipboard may contain files that we aren't allowed to read. While the event is arguably useless,
383
- // in certain occasions, we want to know whether it was a file transfer, as opposed to text. We
384
- // control this with the first boolean flag.
385
- function eventFiles(event) {
386
- let dataTransfer = null;
387
- if (LexicalUtils.objectKlassEquals(event, DragEvent)) {
388
- dataTransfer = event.dataTransfer;
389
- } else if (LexicalUtils.objectKlassEquals(event, ClipboardEvent)) {
390
- dataTransfer = event.clipboardData;
391
- }
392
- if (dataTransfer === null) {
393
- return [false, [], false];
394
- }
395
- const types = dataTransfer.types;
396
- const hasFiles = types.includes('Files');
397
- const hasContent = types.includes('text/html') || types.includes('text/plain');
398
- return [hasFiles, Array.from(dataTransfer.files), hasContent];
399
- }
400
- function $handleIndentAndOutdent(indentOrOutdent) {
401
- const selection = lexical.$getSelection();
402
- if (!lexical.$isRangeSelection(selection)) {
403
- return false;
404
- }
405
- const alreadyHandled = new Set();
406
- const nodes = selection.getNodes();
407
- for (let i = 0; i < nodes.length; i++) {
408
- const node = nodes[i];
409
- const key = node.getKey();
410
- if (alreadyHandled.has(key)) {
411
- continue;
412
- }
413
- const parentBlock = LexicalUtils.$findMatchingParent(node, parentNode => lexical.$isElementNode(parentNode) && !parentNode.isInline());
414
- if (parentBlock === null) {
415
- continue;
416
- }
417
- const parentKey = parentBlock.getKey();
418
- if (parentBlock.canIndent() && !alreadyHandled.has(parentKey)) {
419
- alreadyHandled.add(parentKey);
420
- indentOrOutdent(parentBlock);
421
- }
422
- }
423
- return alreadyHandled.size > 0;
424
- }
425
- function $isTargetWithinDecorator(target) {
426
- const node = lexical.$getNearestNodeFromDOMNode(target);
427
- return lexical.$isDecoratorNode(node);
428
- }
429
- function $isSelectionAtEndOfRoot(selection) {
430
- const focus = selection.focus;
431
- return focus.key === 'root' && focus.offset === lexical.$getRoot().getChildrenSize();
432
- }
433
- function $isSelectionCollapsedAtFrontOfIndentedBlock(selection) {
434
- if (!selection.isCollapsed()) {
435
- return false;
436
- }
437
- const {
438
- anchor
439
- } = selection;
440
- if (anchor.offset !== 0) {
441
- return false;
442
- }
443
- const anchorNode = anchor.getNode();
444
- if (lexical.$isRootNode(anchorNode)) {
445
- return false;
446
- }
447
- const element = LexicalUtils.$getNearestBlockElementAncestorOrThrow(anchorNode);
448
- return element.getIndent() > 0 && (element.is(anchorNode) || anchorNode.is(element.getFirstDescendant()));
449
- }
450
-
451
- /**
452
- * Resets the capitalization of the selection to default.
453
- * Called when the user presses space, tab, or enter key.
454
- * @param selection The selection to reset the capitalization of.
455
- */
456
- function $resetCapitalization(selection) {
457
- for (const format of ['lowercase', 'uppercase', 'capitalize']) {
458
- if (selection.hasFormat(format)) {
459
- selection.toggleFormat(format);
460
- }
461
- }
462
- }
463
- function registerRichText(editor) {
464
- const removeListener = LexicalUtils.mergeRegister(editor.registerCommand(lexical.CLICK_COMMAND, payload => {
465
- const selection = lexical.$getSelection();
466
- if (lexical.$isNodeSelection(selection)) {
467
- selection.clear();
468
- return true;
469
- }
470
- return false;
471
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.DELETE_CHARACTER_COMMAND, isBackward => {
472
- const selection = lexical.$getSelection();
473
- if (lexical.$isRangeSelection(selection)) {
474
- selection.deleteCharacter(isBackward);
475
- return true;
476
- } else if (lexical.$isNodeSelection(selection)) {
477
- selection.deleteNodes();
478
- return true;
479
- }
480
- return false;
481
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.DELETE_WORD_COMMAND, isBackward => {
482
- const selection = lexical.$getSelection();
483
- if (!lexical.$isRangeSelection(selection)) {
484
- return false;
485
- }
486
- selection.deleteWord(isBackward);
487
- return true;
488
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.DELETE_LINE_COMMAND, isBackward => {
489
- const selection = lexical.$getSelection();
490
- if (!lexical.$isRangeSelection(selection)) {
491
- return false;
492
- }
493
- selection.deleteLine(isBackward);
494
- return true;
495
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.CONTROLLED_TEXT_INSERTION_COMMAND, eventOrText => {
496
- const selection = lexical.$getSelection();
497
- if (typeof eventOrText === 'string') {
498
- if (selection !== null) {
499
- selection.insertText(eventOrText);
500
- }
501
- } else {
502
- if (selection === null) {
503
- return false;
504
- }
505
- const dataTransfer = eventOrText.dataTransfer;
506
- if (dataTransfer != null) {
507
- LexicalClipboard.$insertDataTransferForRichText(dataTransfer, selection, editor);
508
- } else if (lexical.$isRangeSelection(selection)) {
509
- const data = eventOrText.data;
510
- if (data) {
511
- selection.insertText(data);
512
- }
513
- return true;
514
- }
515
- }
516
- return true;
517
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.REMOVE_TEXT_COMMAND, () => {
518
- const selection = lexical.$getSelection();
519
- if (!lexical.$isRangeSelection(selection)) {
520
- return false;
521
- }
522
- selection.removeText();
523
- return true;
524
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.FORMAT_TEXT_COMMAND, format => {
525
- const selection = lexical.$getSelection();
526
- if (!lexical.$isRangeSelection(selection)) {
527
- return false;
528
- }
529
- selection.formatText(format);
530
- return true;
531
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.FORMAT_ELEMENT_COMMAND, format => {
532
- const selection = lexical.$getSelection();
533
- if (!lexical.$isRangeSelection(selection) && !lexical.$isNodeSelection(selection)) {
534
- return false;
535
- }
536
- const nodes = selection.getNodes();
537
- for (const node of nodes) {
538
- const element = LexicalUtils.$findMatchingParent(node, parentNode => lexical.$isElementNode(parentNode) && !parentNode.isInline());
539
- if (element !== null) {
540
- element.setFormat(format);
541
- }
542
- }
543
- return true;
544
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.INSERT_LINE_BREAK_COMMAND, selectStart => {
545
- const selection = lexical.$getSelection();
546
- if (!lexical.$isRangeSelection(selection)) {
547
- return false;
548
- }
549
- selection.insertLineBreak(selectStart);
550
- return true;
551
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.INSERT_PARAGRAPH_COMMAND, () => {
552
- const selection = lexical.$getSelection();
553
- if (!lexical.$isRangeSelection(selection)) {
554
- return false;
555
- }
556
- selection.insertParagraph();
557
- return true;
558
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.INSERT_TAB_COMMAND, () => {
559
- lexical.$insertNodes([lexical.$createTabNode()]);
560
- return true;
561
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.INDENT_CONTENT_COMMAND, () => {
562
- return $handleIndentAndOutdent(block => {
563
- const indent = block.getIndent();
564
- block.setIndent(indent + 1);
565
- });
566
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.OUTDENT_CONTENT_COMMAND, () => {
567
- return $handleIndentAndOutdent(block => {
568
- const indent = block.getIndent();
569
- if (indent > 0) {
570
- block.setIndent(Math.max(0, indent - 1));
571
- }
572
- });
573
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND, event => {
574
- const selection = lexical.$getSelection();
575
- if (lexical.$isNodeSelection(selection)) {
576
- // If selection is on a node, let's try and move selection
577
- // back to being a range selection.
578
- const nodes = selection.getNodes();
579
- if (nodes.length > 0) {
580
- nodes[0].selectPrevious();
581
- return true;
582
- }
583
- } else if (lexical.$isRangeSelection(selection)) {
584
- const possibleNode = lexical.$getAdjacentNode(selection.focus, true);
585
- if (!event.shiftKey && lexical.$isDecoratorNode(possibleNode) && !possibleNode.isIsolated() && !possibleNode.isInline()) {
586
- possibleNode.selectPrevious();
587
- event.preventDefault();
588
- return true;
589
- }
590
- }
591
- return false;
592
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, event => {
593
- const selection = lexical.$getSelection();
594
- if (lexical.$isNodeSelection(selection)) {
595
- // If selection is on a node, let's try and move selection
596
- // back to being a range selection.
597
- const nodes = selection.getNodes();
598
- if (nodes.length > 0) {
599
- nodes[0].selectNext(0, 0);
600
- return true;
601
- }
602
- } else if (lexical.$isRangeSelection(selection)) {
603
- if ($isSelectionAtEndOfRoot(selection)) {
604
- event.preventDefault();
605
- return true;
606
- }
607
- const possibleNode = lexical.$getAdjacentNode(selection.focus, false);
608
- if (!event.shiftKey && lexical.$isDecoratorNode(possibleNode) && !possibleNode.isIsolated() && !possibleNode.isInline()) {
609
- possibleNode.selectNext();
610
- event.preventDefault();
611
- return true;
612
- }
613
- }
614
- return false;
615
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_ARROW_LEFT_COMMAND, event => {
616
- const selection = lexical.$getSelection();
617
- if (lexical.$isNodeSelection(selection)) {
618
- // If selection is on a node, let's try and move selection
619
- // back to being a range selection.
620
- const nodes = selection.getNodes();
621
- if (nodes.length > 0) {
622
- event.preventDefault();
623
- if (LexicalSelection.$isParentRTL(nodes[0])) {
624
- nodes[0].selectNext(0, 0);
625
- } else {
626
- nodes[0].selectPrevious();
627
- }
628
- return true;
629
- }
630
- }
631
- if (!lexical.$isRangeSelection(selection)) {
632
- return false;
633
- }
634
- if (LexicalSelection.$shouldOverrideDefaultCharacterSelection(selection, true)) {
635
- const isHoldingShift = event.shiftKey;
636
- event.preventDefault();
637
- LexicalSelection.$moveCharacter(selection, isHoldingShift, true);
638
- return true;
639
- }
640
- return false;
641
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_ARROW_RIGHT_COMMAND, event => {
642
- const selection = lexical.$getSelection();
643
- if (lexical.$isNodeSelection(selection)) {
644
- // If selection is on a node, let's try and move selection
645
- // back to being a range selection.
646
- const nodes = selection.getNodes();
647
- if (nodes.length > 0) {
648
- event.preventDefault();
649
- if (LexicalSelection.$isParentRTL(nodes[0])) {
650
- nodes[0].selectPrevious();
651
- } else {
652
- nodes[0].selectNext(0, 0);
653
- }
654
- return true;
655
- }
656
- }
657
- if (!lexical.$isRangeSelection(selection)) {
658
- return false;
659
- }
660
- const isHoldingShift = event.shiftKey;
661
- if (LexicalSelection.$shouldOverrideDefaultCharacterSelection(selection, false)) {
662
- event.preventDefault();
663
- LexicalSelection.$moveCharacter(selection, isHoldingShift, false);
664
- return true;
665
- }
666
- return false;
667
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_BACKSPACE_COMMAND, event => {
668
- if ($isTargetWithinDecorator(event.target)) {
669
- return false;
670
- }
671
- const selection = lexical.$getSelection();
672
- if (lexical.$isRangeSelection(selection)) {
673
- if ($isSelectionCollapsedAtFrontOfIndentedBlock(selection)) {
674
- event.preventDefault();
675
- return editor.dispatchCommand(lexical.OUTDENT_CONTENT_COMMAND, undefined);
676
- }
677
- // Exception handling for iOS native behavior instead of Lexical's behavior when using Korean on iOS devices.
678
- // more details - https://github.com/facebook/lexical/issues/5841
679
- if (IS_IOS && navigator.language === 'ko-KR') {
680
- return false;
681
- }
682
- } else if (!lexical.$isNodeSelection(selection)) {
683
- return false;
684
- }
685
- event.preventDefault();
686
- return editor.dispatchCommand(lexical.DELETE_CHARACTER_COMMAND, true);
687
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_DELETE_COMMAND, event => {
688
- if ($isTargetWithinDecorator(event.target)) {
689
- return false;
690
- }
691
- const selection = lexical.$getSelection();
692
- if (!(lexical.$isRangeSelection(selection) || lexical.$isNodeSelection(selection))) {
693
- return false;
694
- }
695
- event.preventDefault();
696
- return editor.dispatchCommand(lexical.DELETE_CHARACTER_COMMAND, false);
697
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_ENTER_COMMAND, event => {
698
- const selection = lexical.$getSelection();
699
- if (!lexical.$isRangeSelection(selection)) {
700
- return false;
701
- }
702
- $resetCapitalization(selection);
703
- if (event !== null) {
704
- // If we have beforeinput, then we can avoid blocking
705
- // the default behavior. This ensures that the iOS can
706
- // intercept that we're actually inserting a paragraph,
707
- // and autocomplete, autocapitalize etc work as intended.
708
- // This can also cause a strange performance issue in
709
- // Safari, where there is a noticeable pause due to
710
- // preventing the key down of enter.
711
- if ((IS_IOS || IS_SAFARI || IS_APPLE_WEBKIT) && CAN_USE_BEFORE_INPUT) {
712
- return false;
713
- }
714
- event.preventDefault();
715
- if (event.shiftKey) {
716
- return editor.dispatchCommand(lexical.INSERT_LINE_BREAK_COMMAND, false);
717
- }
718
- }
719
- return editor.dispatchCommand(lexical.INSERT_PARAGRAPH_COMMAND, undefined);
720
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_ESCAPE_COMMAND, () => {
721
- const selection = lexical.$getSelection();
722
- if (!lexical.$isRangeSelection(selection)) {
723
- return false;
724
- }
725
- editor.blur();
726
- return true;
727
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.DROP_COMMAND, event => {
728
- const [, files] = eventFiles(event);
729
- if (files.length > 0) {
730
- const x = event.clientX;
731
- const y = event.clientY;
732
- const eventRange = caretFromPoint(x, y);
733
- if (eventRange !== null) {
734
- const {
735
- offset: domOffset,
736
- node: domNode
737
- } = eventRange;
738
- const node = lexical.$getNearestNodeFromDOMNode(domNode);
739
- if (node !== null) {
740
- const selection = lexical.$createRangeSelection();
741
- if (lexical.$isTextNode(node)) {
742
- selection.anchor.set(node.getKey(), domOffset, 'text');
743
- selection.focus.set(node.getKey(), domOffset, 'text');
744
- } else {
745
- const parentKey = node.getParentOrThrow().getKey();
746
- const offset = node.getIndexWithinParent() + 1;
747
- selection.anchor.set(parentKey, offset, 'element');
748
- selection.focus.set(parentKey, offset, 'element');
749
- }
750
- const normalizedSelection = lexical.$normalizeSelection__EXPERIMENTAL(selection);
751
- lexical.$setSelection(normalizedSelection);
752
- }
753
- editor.dispatchCommand(DRAG_DROP_PASTE, files);
754
- }
755
- event.preventDefault();
756
- return true;
757
- }
758
- const selection = lexical.$getSelection();
759
- if (lexical.$isRangeSelection(selection)) {
760
- return true;
761
- }
762
- return false;
763
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.DRAGSTART_COMMAND, event => {
764
- const [isFileTransfer] = eventFiles(event);
765
- const selection = lexical.$getSelection();
766
- if (isFileTransfer && !lexical.$isRangeSelection(selection)) {
767
- return false;
768
- }
769
- return true;
770
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.DRAGOVER_COMMAND, event => {
771
- const [isFileTransfer] = eventFiles(event);
772
- const selection = lexical.$getSelection();
773
- if (isFileTransfer && !lexical.$isRangeSelection(selection)) {
774
- return false;
775
- }
776
- const x = event.clientX;
777
- const y = event.clientY;
778
- const eventRange = caretFromPoint(x, y);
779
- if (eventRange !== null) {
780
- const node = lexical.$getNearestNodeFromDOMNode(eventRange.node);
781
- if (lexical.$isDecoratorNode(node)) {
782
- // Show browser caret as the user is dragging the media across the screen. Won't work
783
- // for DecoratorNode nor it's relevant.
784
- event.preventDefault();
785
- }
786
- }
787
- return true;
788
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.SELECT_ALL_COMMAND, () => {
789
- lexical.$selectAll();
790
- return true;
791
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.COPY_COMMAND, event => {
792
- LexicalClipboard.copyToClipboard(editor, LexicalUtils.objectKlassEquals(event, ClipboardEvent) ? event : null);
793
- return true;
794
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.CUT_COMMAND, event => {
795
- onCutForRichText(event, editor);
796
- return true;
797
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.PASTE_COMMAND, event => {
798
- const [, files, hasTextContent] = eventFiles(event);
799
- if (files.length > 0 && !hasTextContent) {
800
- editor.dispatchCommand(DRAG_DROP_PASTE, files);
801
- return true;
802
- }
803
-
804
- // if inputs then paste within the input ignore creating a new node on paste event
805
- if (lexical.isDOMNode(event.target) && lexical.isSelectionCapturedInDecoratorInput(event.target)) {
806
- return false;
807
- }
808
- const selection = lexical.$getSelection();
809
- if (selection !== null) {
810
- onPasteForRichText(event, editor);
811
- return true;
812
- }
813
- return false;
814
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_SPACE_COMMAND, _ => {
815
- const selection = lexical.$getSelection();
816
- if (lexical.$isRangeSelection(selection)) {
817
- $resetCapitalization(selection);
818
- }
819
- return false;
820
- }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_TAB_COMMAND, _ => {
821
- const selection = lexical.$getSelection();
822
- if (lexical.$isRangeSelection(selection)) {
823
- $resetCapitalization(selection);
824
- }
825
- return false;
826
- }, lexical.COMMAND_PRIORITY_EDITOR));
827
- return removeListener;
828
- }
829
-
830
- exports.$createHeadingNode = $createHeadingNode;
831
- exports.$createQuoteNode = $createQuoteNode;
832
- exports.$isHeadingNode = $isHeadingNode;
833
- exports.$isQuoteNode = $isQuoteNode;
834
- exports.DRAG_DROP_PASTE = DRAG_DROP_PASTE;
835
- exports.HeadingNode = HeadingNode;
836
- exports.QuoteNode = QuoteNode;
837
- exports.eventFiles = eventFiles;
838
- exports.registerRichText = registerRichText;