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

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,781 +0,0 @@
1
- 'use client';
2
-
3
- import { $insertDataTransferForRichText, copyToClipboard } from '../clipboard/LexicalClipboard.js';
4
- import { $shouldOverrideDefaultCharacterSelection, $moveCharacter } from '../selection/LexicalSelection.js';
5
- import { addClassNamesToElement, isHTMLElement, objectKlassEquals, mergeRegister, $findMatchingParent, $getNearestBlockElementAncestorOrThrow } from '../utils/LexicalUtils.js';
6
- import { createCommand, ElementNode, $createParagraphNode, $applyNodeReplacement, CLICK_COMMAND, $getSelection, $isNodeSelection, DELETE_CHARACTER_COMMAND, $isRangeSelection, COMMAND_PRIORITY_EDITOR, DELETE_WORD_COMMAND, DELETE_LINE_COMMAND, CONTROLLED_TEXT_INSERTION_COMMAND, REMOVE_TEXT_COMMAND, FORMAT_TEXT_COMMAND, FORMAT_ELEMENT_COMMAND, $isElementNode, INSERT_LINE_BREAK_COMMAND, INSERT_PARAGRAPH_COMMAND, INSERT_TAB_COMMAND, $insertNodes, $createTabNode, INDENT_CONTENT_COMMAND, OUTDENT_CONTENT_COMMAND, KEY_ARROW_UP_COMMAND, $getAdjacentNode, $isDecoratorNode, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_LEFT_COMMAND, KEY_ARROW_RIGHT_COMMAND, KEY_BACKSPACE_COMMAND, $isRootNode, KEY_DELETE_COMMAND, KEY_ENTER_COMMAND, KEY_ESCAPE_COMMAND, DROP_COMMAND, $getNearestNodeFromDOMNode, $createRangeSelection, $isTextNode, $normalizeSelection__EXPERIMENTAL, $setSelection, DRAGSTART_COMMAND, DRAGOVER_COMMAND, SELECT_ALL_COMMAND, $selectAll, COPY_COMMAND, CUT_COMMAND, PASTE_COMMAND, isSelectionCapturedInDecoratorInput, $getRoot } from '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 = createCommand('DRAG_DROP_PASTE_FILE');
89
- /** @noInheritDoc */
90
- class QuoteNode extends ElementNode {
91
- static getType() {
92
- return 'quote';
93
- }
94
- static clone(node) {
95
- return new QuoteNode(node.__key);
96
- }
97
- constructor(key) {
98
- super(key);
99
- }
100
-
101
- // View
102
-
103
- createDOM(config) {
104
- const element = document.createElement('blockquote');
105
- addClassNamesToElement(element, config.theme.quote);
106
- return element;
107
- }
108
- updateDOM(prevNode, dom) {
109
- return false;
110
- }
111
- static importDOM() {
112
- return {
113
- blockquote: node => ({
114
- conversion: $convertBlockquoteElement,
115
- priority: 0
116
- })
117
- };
118
- }
119
- exportDOM(editor) {
120
- const {
121
- element
122
- } = super.exportDOM(editor);
123
- if (element && isHTMLElement(element)) {
124
- if (this.isEmpty()) {
125
- element.append(document.createElement('br'));
126
- }
127
- const formatType = this.getFormatType();
128
- element.style.textAlign = formatType;
129
- const direction = this.getDirection();
130
- if (direction) {
131
- element.dir = direction;
132
- }
133
- }
134
- return {
135
- element
136
- };
137
- }
138
- static importJSON(serializedNode) {
139
- const node = $createQuoteNode();
140
- node.setFormat(serializedNode.format);
141
- node.setIndent(serializedNode.indent);
142
- node.setDirection(serializedNode.direction);
143
- return node;
144
- }
145
- exportJSON() {
146
- return {
147
- ...super.exportJSON(),
148
- type: 'quote'
149
- };
150
- }
151
-
152
- // Mutation
153
-
154
- insertNewAfter(_, restoreSelection) {
155
- const newBlock = $createParagraphNode();
156
- const direction = this.getDirection();
157
- newBlock.setDirection(direction);
158
- this.insertAfter(newBlock, restoreSelection);
159
- return newBlock;
160
- }
161
- collapseAtStart() {
162
- const paragraph = $createParagraphNode();
163
- const children = this.getChildren();
164
- children.forEach(child => paragraph.append(child));
165
- this.replace(paragraph);
166
- return true;
167
- }
168
- canMergeWhenEmpty() {
169
- return true;
170
- }
171
- }
172
- function $createQuoteNode() {
173
- return $applyNodeReplacement(new QuoteNode());
174
- }
175
- function $isQuoteNode(node) {
176
- return node instanceof QuoteNode;
177
- }
178
- /** @noInheritDoc */
179
- class HeadingNode extends ElementNode {
180
- /** @internal */
181
-
182
- static getType() {
183
- return 'heading';
184
- }
185
- static clone(node) {
186
- return new HeadingNode(node.__tag, node.__key);
187
- }
188
- constructor(tag, key) {
189
- super(key);
190
- this.__tag = tag;
191
- }
192
- getTag() {
193
- return this.__tag;
194
- }
195
-
196
- // View
197
-
198
- createDOM(config) {
199
- const tag = this.__tag;
200
- const element = document.createElement(tag);
201
- const theme = config.theme;
202
- const classNames = theme.heading;
203
- if (classNames !== undefined) {
204
- const className = classNames[tag];
205
- addClassNamesToElement(element, className);
206
- }
207
- return element;
208
- }
209
- updateDOM(prevNode, dom) {
210
- return false;
211
- }
212
- static importDOM() {
213
- return {
214
- h1: node => ({
215
- conversion: $convertHeadingElement,
216
- priority: 0
217
- }),
218
- h2: node => ({
219
- conversion: $convertHeadingElement,
220
- priority: 0
221
- }),
222
- h3: node => ({
223
- conversion: $convertHeadingElement,
224
- priority: 0
225
- }),
226
- h4: node => ({
227
- conversion: $convertHeadingElement,
228
- priority: 0
229
- }),
230
- h5: node => ({
231
- conversion: $convertHeadingElement,
232
- priority: 0
233
- }),
234
- h6: node => ({
235
- conversion: $convertHeadingElement,
236
- priority: 0
237
- }),
238
- p: node => {
239
- // domNode is a <p> since we matched it by nodeName
240
- const paragraph = node;
241
- const firstChild = paragraph.firstChild;
242
- if (firstChild !== null && isGoogleDocsTitle(firstChild)) {
243
- return {
244
- conversion: () => ({
245
- node: null
246
- }),
247
- priority: 3
248
- };
249
- }
250
- return null;
251
- },
252
- span: node => {
253
- if (isGoogleDocsTitle(node)) {
254
- return {
255
- conversion: domNode => {
256
- return {
257
- node: $createHeadingNode('h1')
258
- };
259
- },
260
- priority: 3
261
- };
262
- }
263
- return null;
264
- }
265
- };
266
- }
267
- exportDOM(editor) {
268
- const {
269
- element
270
- } = super.exportDOM(editor);
271
- if (element && isHTMLElement(element)) {
272
- if (this.isEmpty()) {
273
- element.append(document.createElement('br'));
274
- }
275
- const formatType = this.getFormatType();
276
- element.style.textAlign = formatType;
277
- const direction = this.getDirection();
278
- if (direction) {
279
- element.dir = direction;
280
- }
281
- }
282
- return {
283
- element
284
- };
285
- }
286
- static importJSON(serializedNode) {
287
- const node = $createHeadingNode(serializedNode.tag);
288
- node.setFormat(serializedNode.format);
289
- node.setIndent(serializedNode.indent);
290
- node.setDirection(serializedNode.direction);
291
- return node;
292
- }
293
- exportJSON() {
294
- return {
295
- ...super.exportJSON(),
296
- tag: this.getTag(),
297
- type: 'heading',
298
- version: 1
299
- };
300
- }
301
-
302
- // Mutation
303
- insertNewAfter(selection, restoreSelection = true) {
304
- const anchorOffet = selection ? selection.anchor.offset : 0;
305
- const lastDesc = this.getLastDescendant();
306
- const isAtEnd = !lastDesc || selection && selection.anchor.key === lastDesc.getKey() && anchorOffet === lastDesc.getTextContentSize();
307
- const newElement = isAtEnd || !selection ? $createParagraphNode() : $createHeadingNode(this.getTag());
308
- const direction = this.getDirection();
309
- newElement.setDirection(direction);
310
- this.insertAfter(newElement, restoreSelection);
311
- if (anchorOffet === 0 && !this.isEmpty() && selection) {
312
- const paragraph = $createParagraphNode();
313
- paragraph.select();
314
- this.replace(paragraph, true);
315
- }
316
- return newElement;
317
- }
318
- collapseAtStart() {
319
- const newElement = !this.isEmpty() ? $createHeadingNode(this.getTag()) : $createParagraphNode();
320
- const children = this.getChildren();
321
- children.forEach(child => newElement.append(child));
322
- this.replace(newElement);
323
- return true;
324
- }
325
- extractWithChild() {
326
- return true;
327
- }
328
- }
329
- function isGoogleDocsTitle(domNode) {
330
- if (domNode.nodeName.toLowerCase() === 'span') {
331
- return domNode.style.fontSize === '26pt';
332
- }
333
- return false;
334
- }
335
- function $convertHeadingElement(element) {
336
- const nodeName = element.nodeName.toLowerCase();
337
- let node = null;
338
- if (nodeName === 'h1' || nodeName === 'h2' || nodeName === 'h3' || nodeName === 'h4' || nodeName === 'h5' || nodeName === 'h6') {
339
- node = $createHeadingNode(nodeName);
340
- if (element.style !== null) {
341
- node.setFormat(element.style.textAlign);
342
- }
343
- }
344
- return {
345
- node
346
- };
347
- }
348
- function $convertBlockquoteElement(element) {
349
- const node = $createQuoteNode();
350
- if (element.style !== null) {
351
- node.setFormat(element.style.textAlign);
352
- }
353
- return {
354
- node
355
- };
356
- }
357
- function $createHeadingNode(headingTag) {
358
- return $applyNodeReplacement(new HeadingNode(headingTag));
359
- }
360
- function $isHeadingNode(node) {
361
- return node instanceof HeadingNode;
362
- }
363
- function onPasteForRichText(event, editor) {
364
- event.preventDefault();
365
- editor.update(() => {
366
- const selection = $getSelection();
367
- const clipboardData = objectKlassEquals(event, InputEvent) || objectKlassEquals(event, KeyboardEvent) ? null : event.clipboardData;
368
- if (clipboardData != null && selection !== null) {
369
- $insertDataTransferForRichText(clipboardData, selection, editor);
370
- }
371
- }, {
372
- tag: 'paste'
373
- });
374
- }
375
- async function onCutForRichText(event, editor) {
376
- await copyToClipboard(editor, objectKlassEquals(event, ClipboardEvent) ? event : null);
377
- editor.update(() => {
378
- const selection = $getSelection();
379
- if ($isRangeSelection(selection)) {
380
- selection.removeText();
381
- } else if ($isNodeSelection(selection)) {
382
- selection.getNodes().forEach(node => node.remove());
383
- }
384
- });
385
- }
386
-
387
- // Clipboard may contain files that we aren't allowed to read. While the event is arguably useless,
388
- // in certain occasions, we want to know whether it was a file transfer, as opposed to text. We
389
- // control this with the first boolean flag.
390
- function eventFiles(event) {
391
- let dataTransfer = null;
392
- if (objectKlassEquals(event, DragEvent)) {
393
- dataTransfer = event.dataTransfer;
394
- } else if (objectKlassEquals(event, ClipboardEvent)) {
395
- dataTransfer = event.clipboardData;
396
- }
397
- if (dataTransfer === null) {
398
- return [false, [], false];
399
- }
400
- const types = dataTransfer.types;
401
- const hasFiles = types.includes('Files');
402
- const hasContent = types.includes('text/html') || types.includes('text/plain');
403
- return [hasFiles, Array.from(dataTransfer.files), hasContent];
404
- }
405
- function $handleIndentAndOutdent(indentOrOutdent) {
406
- const selection = $getSelection();
407
- if (!$isRangeSelection(selection)) {
408
- return false;
409
- }
410
- const alreadyHandled = new Set();
411
- const nodes = selection.getNodes();
412
- for (let i = 0; i < nodes.length; i++) {
413
- const node = nodes[i];
414
- const key = node.getKey();
415
- if (alreadyHandled.has(key)) {
416
- continue;
417
- }
418
- const parentBlock = $findMatchingParent(node, parentNode => $isElementNode(parentNode) && !parentNode.isInline());
419
- if (parentBlock === null) {
420
- continue;
421
- }
422
- const parentKey = parentBlock.getKey();
423
- if (parentBlock.canIndent() && !alreadyHandled.has(parentKey)) {
424
- alreadyHandled.add(parentKey);
425
- indentOrOutdent(parentBlock);
426
- }
427
- }
428
- return alreadyHandled.size > 0;
429
- }
430
- function $isTargetWithinDecorator(target) {
431
- const node = $getNearestNodeFromDOMNode(target);
432
- return $isDecoratorNode(node);
433
- }
434
- function $isSelectionAtEndOfRoot(selection) {
435
- const focus = selection.focus;
436
- return focus.key === 'root' && focus.offset === $getRoot().getChildrenSize();
437
- }
438
- function registerRichText(editor) {
439
- const removeListener = mergeRegister(editor.registerCommand(CLICK_COMMAND, payload => {
440
- const selection = $getSelection();
441
- if ($isNodeSelection(selection)) {
442
- selection.clear();
443
- return true;
444
- }
445
- return false;
446
- }, 0), editor.registerCommand(DELETE_CHARACTER_COMMAND, isBackward => {
447
- const selection = $getSelection();
448
- if (!$isRangeSelection(selection)) {
449
- return false;
450
- }
451
- selection.deleteCharacter(isBackward);
452
- return true;
453
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(DELETE_WORD_COMMAND, isBackward => {
454
- const selection = $getSelection();
455
- if (!$isRangeSelection(selection)) {
456
- return false;
457
- }
458
- selection.deleteWord(isBackward);
459
- return true;
460
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(DELETE_LINE_COMMAND, isBackward => {
461
- const selection = $getSelection();
462
- if (!$isRangeSelection(selection)) {
463
- return false;
464
- }
465
- selection.deleteLine(isBackward);
466
- return true;
467
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(CONTROLLED_TEXT_INSERTION_COMMAND, eventOrText => {
468
- const selection = $getSelection();
469
- if (typeof eventOrText === 'string') {
470
- if (selection !== null) {
471
- selection.insertText(eventOrText);
472
- }
473
- } else {
474
- if (selection === null) {
475
- return false;
476
- }
477
- const dataTransfer = eventOrText.dataTransfer;
478
- if (dataTransfer != null) {
479
- $insertDataTransferForRichText(dataTransfer, selection, editor);
480
- } else if ($isRangeSelection(selection)) {
481
- const data = eventOrText.data;
482
- if (data) {
483
- selection.insertText(data);
484
- }
485
- return true;
486
- }
487
- }
488
- return true;
489
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(REMOVE_TEXT_COMMAND, () => {
490
- const selection = $getSelection();
491
- if (!$isRangeSelection(selection)) {
492
- return false;
493
- }
494
- selection.removeText();
495
- return true;
496
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(FORMAT_TEXT_COMMAND, format => {
497
- const selection = $getSelection();
498
- if (!$isRangeSelection(selection)) {
499
- return false;
500
- }
501
- selection.formatText(format);
502
- return true;
503
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(FORMAT_ELEMENT_COMMAND, format => {
504
- const selection = $getSelection();
505
- if (!$isRangeSelection(selection) && !$isNodeSelection(selection)) {
506
- return false;
507
- }
508
- const nodes = selection.getNodes();
509
- for (const node of nodes) {
510
- const element = $findMatchingParent(node, parentNode => $isElementNode(parentNode) && !parentNode.isInline());
511
- if (element !== null) {
512
- element.setFormat(format);
513
- }
514
- }
515
- return true;
516
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(INSERT_LINE_BREAK_COMMAND, selectStart => {
517
- const selection = $getSelection();
518
- if (!$isRangeSelection(selection)) {
519
- return false;
520
- }
521
- selection.insertLineBreak(selectStart);
522
- return true;
523
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(INSERT_PARAGRAPH_COMMAND, () => {
524
- const selection = $getSelection();
525
- if (!$isRangeSelection(selection)) {
526
- return false;
527
- }
528
- selection.insertParagraph();
529
- return true;
530
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(INSERT_TAB_COMMAND, () => {
531
- $insertNodes([$createTabNode()]);
532
- return true;
533
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(INDENT_CONTENT_COMMAND, () => {
534
- return $handleIndentAndOutdent(block => {
535
- const indent = block.getIndent();
536
- block.setIndent(indent + 1);
537
- });
538
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(OUTDENT_CONTENT_COMMAND, () => {
539
- return $handleIndentAndOutdent(block => {
540
- const indent = block.getIndent();
541
- if (indent > 0) {
542
- block.setIndent(indent - 1);
543
- }
544
- });
545
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(KEY_ARROW_UP_COMMAND, event => {
546
- const selection = $getSelection();
547
- if ($isNodeSelection(selection) && !$isTargetWithinDecorator(event.target)) {
548
- // If selection is on a node, let's try and move selection
549
- // back to being a range selection.
550
- const nodes = selection.getNodes();
551
- if (nodes.length > 0) {
552
- nodes[0].selectPrevious();
553
- return true;
554
- }
555
- } else if ($isRangeSelection(selection)) {
556
- const possibleNode = $getAdjacentNode(selection.focus, true);
557
- if (!event.shiftKey && $isDecoratorNode(possibleNode) && !possibleNode.isIsolated() && !possibleNode.isInline()) {
558
- possibleNode.selectPrevious();
559
- event.preventDefault();
560
- return true;
561
- }
562
- }
563
- return false;
564
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(KEY_ARROW_DOWN_COMMAND, event => {
565
- const selection = $getSelection();
566
- if ($isNodeSelection(selection)) {
567
- // If selection is on a node, let's try and move selection
568
- // back to being a range selection.
569
- const nodes = selection.getNodes();
570
- if (nodes.length > 0) {
571
- nodes[0].selectNext(0, 0);
572
- return true;
573
- }
574
- } else if ($isRangeSelection(selection)) {
575
- if ($isSelectionAtEndOfRoot(selection)) {
576
- event.preventDefault();
577
- return true;
578
- }
579
- const possibleNode = $getAdjacentNode(selection.focus, false);
580
- if (!event.shiftKey && $isDecoratorNode(possibleNode) && !possibleNode.isIsolated() && !possibleNode.isInline()) {
581
- possibleNode.selectNext();
582
- event.preventDefault();
583
- return true;
584
- }
585
- }
586
- return false;
587
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(KEY_ARROW_LEFT_COMMAND, event => {
588
- const selection = $getSelection();
589
- if ($isNodeSelection(selection)) {
590
- // If selection is on a node, let's try and move selection
591
- // back to being a range selection.
592
- const nodes = selection.getNodes();
593
- if (nodes.length > 0) {
594
- event.preventDefault();
595
- nodes[0].selectPrevious();
596
- return true;
597
- }
598
- }
599
- if (!$isRangeSelection(selection)) {
600
- return false;
601
- }
602
- if ($shouldOverrideDefaultCharacterSelection(selection, true)) {
603
- const isHoldingShift = event.shiftKey;
604
- event.preventDefault();
605
- $moveCharacter(selection, isHoldingShift, true);
606
- return true;
607
- }
608
- return false;
609
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(KEY_ARROW_RIGHT_COMMAND, event => {
610
- const selection = $getSelection();
611
- if ($isNodeSelection(selection) && !$isTargetWithinDecorator(event.target)) {
612
- // If selection is on a node, let's try and move selection
613
- // back to being a range selection.
614
- const nodes = selection.getNodes();
615
- if (nodes.length > 0) {
616
- event.preventDefault();
617
- nodes[0].selectNext(0, 0);
618
- return true;
619
- }
620
- }
621
- if (!$isRangeSelection(selection)) {
622
- return false;
623
- }
624
- const isHoldingShift = event.shiftKey;
625
- if ($shouldOverrideDefaultCharacterSelection(selection, false)) {
626
- event.preventDefault();
627
- $moveCharacter(selection, isHoldingShift, false);
628
- return true;
629
- }
630
- return false;
631
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(KEY_BACKSPACE_COMMAND, event => {
632
- if ($isTargetWithinDecorator(event.target)) {
633
- return false;
634
- }
635
- const selection = $getSelection();
636
- if (!$isRangeSelection(selection)) {
637
- return false;
638
- }
639
- event.preventDefault();
640
- const {
641
- anchor
642
- } = selection;
643
- const anchorNode = anchor.getNode();
644
- if (selection.isCollapsed() && anchor.offset === 0 && !$isRootNode(anchorNode)) {
645
- const element = $getNearestBlockElementAncestorOrThrow(anchorNode);
646
- if (element.getIndent() > 0) {
647
- return editor.dispatchCommand(OUTDENT_CONTENT_COMMAND, undefined);
648
- }
649
- }
650
- return editor.dispatchCommand(DELETE_CHARACTER_COMMAND, true);
651
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(KEY_DELETE_COMMAND, event => {
652
- if ($isTargetWithinDecorator(event.target)) {
653
- return false;
654
- }
655
- const selection = $getSelection();
656
- if (!$isRangeSelection(selection)) {
657
- return false;
658
- }
659
- event.preventDefault();
660
- return editor.dispatchCommand(DELETE_CHARACTER_COMMAND, false);
661
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(KEY_ENTER_COMMAND, event => {
662
- const selection = $getSelection();
663
- if (!$isRangeSelection(selection)) {
664
- return false;
665
- }
666
- if (event !== null) {
667
- // If we have beforeinput, then we can avoid blocking
668
- // the default behavior. This ensures that the iOS can
669
- // intercept that we're actually inserting a paragraph,
670
- // and autocomplete, autocapitalize etc work as intended.
671
- // This can also cause a strange performance issue in
672
- // Safari, where there is a noticeable pause due to
673
- // preventing the key down of enter.
674
- if ((IS_IOS || IS_SAFARI || IS_APPLE_WEBKIT) && CAN_USE_BEFORE_INPUT) {
675
- return false;
676
- }
677
- event.preventDefault();
678
- if (event.shiftKey) {
679
- return editor.dispatchCommand(INSERT_LINE_BREAK_COMMAND, false);
680
- }
681
- }
682
- return editor.dispatchCommand(INSERT_PARAGRAPH_COMMAND, undefined);
683
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(KEY_ESCAPE_COMMAND, () => {
684
- const selection = $getSelection();
685
- if (!$isRangeSelection(selection)) {
686
- return false;
687
- }
688
- editor.blur();
689
- return true;
690
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(DROP_COMMAND, event => {
691
- const [, files] = eventFiles(event);
692
- if (files.length > 0) {
693
- const x = event.clientX;
694
- const y = event.clientY;
695
- const eventRange = caretFromPoint(x, y);
696
- if (eventRange !== null) {
697
- const {
698
- offset: domOffset,
699
- node: domNode
700
- } = eventRange;
701
- const node = $getNearestNodeFromDOMNode(domNode);
702
- if (node !== null) {
703
- const selection = $createRangeSelection();
704
- if ($isTextNode(node)) {
705
- selection.anchor.set(node.getKey(), domOffset, 'text');
706
- selection.focus.set(node.getKey(), domOffset, 'text');
707
- } else {
708
- const parentKey = node.getParentOrThrow().getKey();
709
- const offset = node.getIndexWithinParent() + 1;
710
- selection.anchor.set(parentKey, offset, 'element');
711
- selection.focus.set(parentKey, offset, 'element');
712
- }
713
- const normalizedSelection = $normalizeSelection__EXPERIMENTAL(selection);
714
- $setSelection(normalizedSelection);
715
- }
716
- editor.dispatchCommand(DRAG_DROP_PASTE, files);
717
- }
718
- event.preventDefault();
719
- return true;
720
- }
721
- const selection = $getSelection();
722
- if ($isRangeSelection(selection)) {
723
- return true;
724
- }
725
- return false;
726
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(DRAGSTART_COMMAND, event => {
727
- const [isFileTransfer] = eventFiles(event);
728
- const selection = $getSelection();
729
- if (isFileTransfer && !$isRangeSelection(selection)) {
730
- return false;
731
- }
732
- return true;
733
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(DRAGOVER_COMMAND, event => {
734
- const [isFileTransfer] = eventFiles(event);
735
- const selection = $getSelection();
736
- if (isFileTransfer && !$isRangeSelection(selection)) {
737
- return false;
738
- }
739
- const x = event.clientX;
740
- const y = event.clientY;
741
- const eventRange = caretFromPoint(x, y);
742
- if (eventRange !== null) {
743
- const node = $getNearestNodeFromDOMNode(eventRange.node);
744
- if ($isDecoratorNode(node)) {
745
- // Show browser caret as the user is dragging the media across the screen. Won't work
746
- // for DecoratorNode nor it's relevant.
747
- event.preventDefault();
748
- }
749
- }
750
- return true;
751
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(SELECT_ALL_COMMAND, () => {
752
- $selectAll();
753
- return true;
754
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(COPY_COMMAND, event => {
755
- copyToClipboard(editor, objectKlassEquals(event, ClipboardEvent) ? event : null);
756
- return true;
757
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(CUT_COMMAND, event => {
758
- onCutForRichText(event, editor);
759
- return true;
760
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(PASTE_COMMAND, event => {
761
- const [, files, hasTextContent] = eventFiles(event);
762
- if (files.length > 0 && !hasTextContent) {
763
- editor.dispatchCommand(DRAG_DROP_PASTE, files);
764
- return true;
765
- }
766
-
767
- // if inputs then paste within the input ignore creating a new node on paste event
768
- if (isSelectionCapturedInDecoratorInput(event.target)) {
769
- return false;
770
- }
771
- const selection = $getSelection();
772
- if (selection !== null) {
773
- onPasteForRichText(event, editor);
774
- return true;
775
- }
776
- return false;
777
- }, COMMAND_PRIORITY_EDITOR));
778
- return removeListener;
779
- }
780
-
781
- export { $createHeadingNode, $createQuoteNode, $isHeadingNode, $isQuoteNode, DRAG_DROP_PASTE, HeadingNode, QuoteNode, eventFiles, registerRichText };