@react-fabric/lexical 1.0.0-beta.6 → 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 (134) hide show
  1. package/{src/editor/Lexical.module.css → css/_lexical.css} +8 -4
  2. package/css/styles.css +24 -0
  3. package/dist/cjs/{packages/lexical/src/components → components}/StickyComponent.js +3 -4
  4. package/dist/cjs/{packages/lexical/src/editor → editor}/LexicalEditor.js +35 -10
  5. package/dist/cjs/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +154 -145
  6. package/dist/cjs/i18n/ar.json.js +24 -0
  7. package/dist/cjs/i18n/en.json.js +24 -0
  8. package/dist/cjs/i18n/index.js +33 -0
  9. package/dist/cjs/{packages/lexical/src/index.js → index.js} +1 -0
  10. package/dist/cjs/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +5 -5
  11. package/dist/cjs/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +55 -34
  12. package/dist/esm/{packages/lexical/src/components → components}/StickyComponent.js +3 -4
  13. package/dist/esm/{packages/lexical/src/editor → editor}/LexicalEditor.js +34 -9
  14. package/dist/esm/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +102 -93
  15. package/dist/esm/i18n/ar.json.js +21 -0
  16. package/dist/esm/i18n/en.json.js +21 -0
  17. package/dist/esm/i18n/index.js +33 -0
  18. package/dist/esm/{packages/lexical/src/index.js → index.js} +1 -0
  19. package/dist/esm/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +2 -2
  20. package/dist/esm/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +56 -35
  21. package/dist/types/editor/LexicalEditor.d.ts +25 -0
  22. package/dist/types/hooks/useFormatWatcher.d.ts +3 -2
  23. package/dist/types/i18n/index.d.ts +1 -0
  24. package/dist/types/index.d.ts +1 -0
  25. package/dist/types/nodes/StickyNode.d.ts +0 -1
  26. package/package.json +14 -13
  27. package/src/components/StickyComponent.tsx +4 -5
  28. package/src/editor/LexicalEditor.tsx +57 -26
  29. package/src/hooks/useFormatWatcher.ts +38 -31
  30. package/src/i18n/ar.json +16 -0
  31. package/src/i18n/en.json +16 -0
  32. package/src/i18n/index.ts +28 -0
  33. package/src/index.ts +2 -0
  34. package/src/plugins/ToolbarPlugin.tsx +56 -35
  35. package/dist/cjs/_virtual/prism.js +0 -5
  36. package/dist/cjs/editor/Lexical.module.css +0 -1
  37. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -432
  38. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -21
  39. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -27
  40. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +0 -1139
  41. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +0 -25
  42. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +0 -50
  43. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +0 -245
  44. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +0 -19
  45. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +0 -18
  46. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +0 -471
  47. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +0 -17
  48. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +0 -24
  49. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +0 -1275
  50. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +0 -29
  51. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +0 -29
  52. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -789
  53. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +0 -29
  54. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -26
  55. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -976
  56. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +0 -35
  57. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -35
  58. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +0 -2792
  59. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +0 -23
  60. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +0 -55
  61. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -888
  62. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +0 -35
  63. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -66
  64. package/dist/cjs/node_modules/@mdi/js/mdi.js +0 -8
  65. package/dist/cjs/node_modules/prismjs/components/prism-c.js +0 -82
  66. package/dist/cjs/node_modules/prismjs/components/prism-clike.js +0 -33
  67. package/dist/cjs/node_modules/prismjs/components/prism-cpp.js +0 -101
  68. package/dist/cjs/node_modules/prismjs/components/prism-css.js +0 -66
  69. package/dist/cjs/node_modules/prismjs/components/prism-java.js +0 -126
  70. package/dist/cjs/node_modules/prismjs/components/prism-javascript.js +0 -174
  71. package/dist/cjs/node_modules/prismjs/components/prism-markdown.js +0 -417
  72. package/dist/cjs/node_modules/prismjs/components/prism-markup.js +0 -188
  73. package/dist/cjs/node_modules/prismjs/components/prism-objectivec.js +0 -14
  74. package/dist/cjs/node_modules/prismjs/components/prism-powershell.js +0 -60
  75. package/dist/cjs/node_modules/prismjs/components/prism-python.js +0 -67
  76. package/dist/cjs/node_modules/prismjs/components/prism-rust.js +0 -130
  77. package/dist/cjs/node_modules/prismjs/components/prism-sql.js +0 -34
  78. package/dist/cjs/node_modules/prismjs/components/prism-swift.js +0 -150
  79. package/dist/cjs/node_modules/prismjs/components/prism-typescript.js +0 -62
  80. package/dist/cjs/node_modules/prismjs/prism.js +0 -1951
  81. package/dist/esm/_virtual/prism.js +0 -5
  82. package/dist/esm/editor/Lexical.module.css +0 -1
  83. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -423
  84. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -19
  85. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -18
  86. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +0 -1121
  87. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +0 -21
  88. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +0 -32
  89. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +0 -244
  90. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +0 -18
  91. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +0 -17
  92. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +0 -463
  93. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +0 -17
  94. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +0 -16
  95. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +0 -1262
  96. package/dist/esm/node_modules/@lexical/list/LexicalList.js +0 -23
  97. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +0 -16
  98. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -781
  99. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +0 -23
  100. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -18
  101. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -957
  102. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +0 -26
  103. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -16
  104. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +0 -2754
  105. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +0 -20
  106. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +0 -17
  107. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -839
  108. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +0 -26
  109. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -17
  110. package/dist/esm/node_modules/@mdi/js/mdi.js +0 -7
  111. package/dist/esm/node_modules/prismjs/components/prism-c.js +0 -82
  112. package/dist/esm/node_modules/prismjs/components/prism-clike.js +0 -33
  113. package/dist/esm/node_modules/prismjs/components/prism-cpp.js +0 -101
  114. package/dist/esm/node_modules/prismjs/components/prism-css.js +0 -66
  115. package/dist/esm/node_modules/prismjs/components/prism-java.js +0 -126
  116. package/dist/esm/node_modules/prismjs/components/prism-javascript.js +0 -174
  117. package/dist/esm/node_modules/prismjs/components/prism-markdown.js +0 -417
  118. package/dist/esm/node_modules/prismjs/components/prism-markup.js +0 -188
  119. package/dist/esm/node_modules/prismjs/components/prism-objectivec.js +0 -14
  120. package/dist/esm/node_modules/prismjs/components/prism-powershell.js +0 -60
  121. package/dist/esm/node_modules/prismjs/components/prism-python.js +0 -67
  122. package/dist/esm/node_modules/prismjs/components/prism-rust.js +0 -130
  123. package/dist/esm/node_modules/prismjs/components/prism-sql.js +0 -34
  124. package/dist/esm/node_modules/prismjs/components/prism-swift.js +0 -150
  125. package/dist/esm/node_modules/prismjs/components/prism-typescript.js +0 -62
  126. package/dist/esm/node_modules/prismjs/prism.js +0 -1951
  127. /package/dist/cjs/{packages/lexical/src/editor → editor}/theme.js +0 -0
  128. /package/dist/cjs/{packages/lexical/src/nodes → nodes}/StickyNode.js +0 -0
  129. /package/dist/cjs/{packages/lexical/src/plugins → plugins}/StickyPlugin.js +0 -0
  130. /package/dist/cjs/{packages/lexical/src/utils → utils}/index.js +0 -0
  131. /package/dist/esm/{packages/lexical/src/editor → editor}/theme.js +0 -0
  132. /package/dist/esm/{packages/lexical/src/nodes → nodes}/StickyNode.js +0 -0
  133. /package/dist/esm/{packages/lexical/src/plugins → plugins}/StickyPlugin.js +0 -0
  134. /package/dist/esm/{packages/lexical/src/utils → utils}/index.js +0 -0
@@ -1,839 +0,0 @@
1
- 'use client';
2
-
3
- import { $getRoot, $isElementNode, $cloneWithProperties, $setSelection, $getSelection, $getPreviousSelection, $isRangeSelection, $isRootOrShadowRoot, $isTextNode, $splitNode, $createParagraphNode, TextNode } from 'lexical';
4
- export { $splitNode, isBlockDomNode, isHTMLAnchorElement, isHTMLElement, isInlineDomNode } from 'lexical';
5
- import { createRectsFromDOMRange } from '../selection/LexicalSelection.js';
6
-
7
- /**
8
- * Copyright (c) Meta Platforms, Inc. and affiliates.
9
- *
10
- * This source code is licensed under the MIT license found in the
11
- * LICENSE file in the root directory of this source tree.
12
- *
13
- */
14
-
15
-
16
- /**
17
- * Copyright (c) Meta Platforms, Inc. and affiliates.
18
- *
19
- * This source code is licensed under the MIT license found in the
20
- * LICENSE file in the root directory of this source tree.
21
- *
22
- */
23
-
24
- const CAN_USE_DOM$1 = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
25
-
26
- /**
27
- * Copyright (c) Meta Platforms, Inc. and affiliates.
28
- *
29
- * This source code is licensed under the MIT license found in the
30
- * LICENSE file in the root directory of this source tree.
31
- *
32
- */
33
-
34
- const documentMode = CAN_USE_DOM$1 && 'documentMode' in document ? document.documentMode : null;
35
- const IS_APPLE$1 = CAN_USE_DOM$1 && /Mac|iPod|iPhone|iPad/.test(navigator.platform);
36
- const IS_FIREFOX$1 = CAN_USE_DOM$1 && /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
37
- const CAN_USE_BEFORE_INPUT$1 = CAN_USE_DOM$1 && 'InputEvent' in window && !documentMode ? 'getTargetRanges' in new window.InputEvent('input') : false;
38
- const IS_SAFARI$1 = CAN_USE_DOM$1 && /Version\/[\d.]+.*Safari/.test(navigator.userAgent);
39
- const IS_IOS$1 = CAN_USE_DOM$1 && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
40
- const IS_ANDROID$1 = CAN_USE_DOM$1 && /Android/.test(navigator.userAgent);
41
-
42
- // Keep these in case we need to use them in the future.
43
- // export const IS_WINDOWS: boolean = CAN_USE_DOM && /Win/.test(navigator.platform);
44
- const IS_CHROME$1 = CAN_USE_DOM$1 && /^(?=.*Chrome).*/i.test(navigator.userAgent);
45
- // export const canUseTextInputEvent: boolean = CAN_USE_DOM && 'TextEvent' in window && !documentMode;
46
-
47
- const IS_ANDROID_CHROME$1 = CAN_USE_DOM$1 && IS_ANDROID$1 && IS_CHROME$1;
48
- const IS_APPLE_WEBKIT$1 = CAN_USE_DOM$1 && /AppleWebKit\/[\d.]+/.test(navigator.userAgent) && !IS_CHROME$1;
49
-
50
- /**
51
- * Copyright (c) Meta Platforms, Inc. and affiliates.
52
- *
53
- * This source code is licensed under the MIT license found in the
54
- * LICENSE file in the root directory of this source tree.
55
- *
56
- */
57
-
58
- function normalizeClassNames(...classNames) {
59
- const rval = [];
60
- for (const className of classNames) {
61
- if (className && typeof className === 'string') {
62
- for (const [s] of className.matchAll(/\S+/g)) {
63
- rval.push(s);
64
- }
65
- }
66
- }
67
- return rval;
68
- }
69
-
70
- /**
71
- * Copyright (c) Meta Platforms, Inc. and affiliates.
72
- *
73
- * This source code is licensed under the MIT license found in the
74
- * LICENSE file in the root directory of this source tree.
75
- *
76
- */
77
-
78
- /**
79
- * Returns a function that will execute all functions passed when called. It is generally used
80
- * to register multiple lexical listeners and then tear them down with a single function call, such
81
- * as React's useEffect hook.
82
- * @example
83
- * ```ts
84
- * useEffect(() => {
85
- * return mergeRegister(
86
- * editor.registerCommand(...registerCommand1 logic),
87
- * editor.registerCommand(...registerCommand2 logic),
88
- * editor.registerCommand(...registerCommand3 logic)
89
- * )
90
- * }, [editor])
91
- * ```
92
- * In this case, useEffect is returning the function returned by mergeRegister as a cleanup
93
- * function to be executed after either the useEffect runs again (due to one of its dependencies
94
- * updating) or the component it resides in unmounts.
95
- * Note the functions don't neccesarily need to be in an array as all arguments
96
- * are considered to be the func argument and spread from there.
97
- * The order of cleanup is the reverse of the argument order. Generally it is
98
- * expected that the first "acquire" will be "released" last (LIFO order),
99
- * because a later step may have some dependency on an earlier one.
100
- * @param func - An array of cleanup functions meant to be executed by the returned function.
101
- * @returns the function which executes all the passed cleanup functions.
102
- */
103
- function mergeRegister(...func) {
104
- return () => {
105
- for (let i = func.length - 1; i >= 0; i--) {
106
- func[i]();
107
- }
108
- // Clean up the references and make future calls a no-op
109
- func.length = 0;
110
- };
111
- }
112
-
113
- /**
114
- * Copyright (c) Meta Platforms, Inc. and affiliates.
115
- *
116
- * This source code is licensed under the MIT license found in the
117
- * LICENSE file in the root directory of this source tree.
118
- *
119
- */
120
-
121
- function px(value) {
122
- return `${value}px`;
123
- }
124
-
125
- /**
126
- * Copyright (c) Meta Platforms, Inc. and affiliates.
127
- *
128
- * This source code is licensed under the MIT license found in the
129
- * LICENSE file in the root directory of this source tree.
130
- *
131
- */
132
-
133
- const mutationObserverConfig = {
134
- attributes: true,
135
- characterData: true,
136
- childList: true,
137
- subtree: true
138
- };
139
- function positionNodeOnRange(editor, range, onReposition) {
140
- let rootDOMNode = null;
141
- let parentDOMNode = null;
142
- let observer = null;
143
- let lastNodes = [];
144
- const wrapperNode = document.createElement('div');
145
- function position() {
146
- if (!(rootDOMNode !== null)) {
147
- throw Error(`Unexpected null rootDOMNode`);
148
- }
149
- if (!(parentDOMNode !== null)) {
150
- throw Error(`Unexpected null parentDOMNode`);
151
- }
152
- const {
153
- left: rootLeft,
154
- top: rootTop
155
- } = rootDOMNode.getBoundingClientRect();
156
- const parentDOMNode_ = parentDOMNode;
157
- const rects = createRectsFromDOMRange(editor, range);
158
- if (!wrapperNode.isConnected) {
159
- parentDOMNode_.append(wrapperNode);
160
- }
161
- let hasRepositioned = false;
162
- for (let i = 0; i < rects.length; i++) {
163
- const rect = rects[i];
164
- // Try to reuse the previously created Node when possible, no need to
165
- // remove/create on the most common case reposition case
166
- const rectNode = lastNodes[i] || document.createElement('div');
167
- const rectNodeStyle = rectNode.style;
168
- if (rectNodeStyle.position !== 'absolute') {
169
- rectNodeStyle.position = 'absolute';
170
- hasRepositioned = true;
171
- }
172
- const left = px(rect.left - rootLeft);
173
- if (rectNodeStyle.left !== left) {
174
- rectNodeStyle.left = left;
175
- hasRepositioned = true;
176
- }
177
- const top = px(rect.top - rootTop);
178
- if (rectNodeStyle.top !== top) {
179
- rectNode.style.top = top;
180
- hasRepositioned = true;
181
- }
182
- const width = px(rect.width);
183
- if (rectNodeStyle.width !== width) {
184
- rectNode.style.width = width;
185
- hasRepositioned = true;
186
- }
187
- const height = px(rect.height);
188
- if (rectNodeStyle.height !== height) {
189
- rectNode.style.height = height;
190
- hasRepositioned = true;
191
- }
192
- if (rectNode.parentNode !== wrapperNode) {
193
- wrapperNode.append(rectNode);
194
- hasRepositioned = true;
195
- }
196
- lastNodes[i] = rectNode;
197
- }
198
- while (lastNodes.length > rects.length) {
199
- lastNodes.pop();
200
- }
201
- if (hasRepositioned) {
202
- onReposition(lastNodes);
203
- }
204
- }
205
- function stop() {
206
- parentDOMNode = null;
207
- rootDOMNode = null;
208
- if (observer !== null) {
209
- observer.disconnect();
210
- }
211
- observer = null;
212
- wrapperNode.remove();
213
- for (const node of lastNodes) {
214
- node.remove();
215
- }
216
- lastNodes = [];
217
- }
218
- function restart() {
219
- const currentRootDOMNode = editor.getRootElement();
220
- if (currentRootDOMNode === null) {
221
- return stop();
222
- }
223
- const currentParentDOMNode = currentRootDOMNode.parentElement;
224
- if (!(currentParentDOMNode instanceof HTMLElement)) {
225
- return stop();
226
- }
227
- stop();
228
- rootDOMNode = currentRootDOMNode;
229
- parentDOMNode = currentParentDOMNode;
230
- observer = new MutationObserver(mutations => {
231
- const nextRootDOMNode = editor.getRootElement();
232
- const nextParentDOMNode = nextRootDOMNode && nextRootDOMNode.parentElement;
233
- if (nextRootDOMNode !== rootDOMNode || nextParentDOMNode !== parentDOMNode) {
234
- return restart();
235
- }
236
- for (const mutation of mutations) {
237
- if (!wrapperNode.contains(mutation.target)) {
238
- // TODO throttle
239
- return position();
240
- }
241
- }
242
- });
243
- observer.observe(currentParentDOMNode, mutationObserverConfig);
244
- position();
245
- }
246
- const removeRootListener = editor.registerRootListener(restart);
247
- return () => {
248
- removeRootListener();
249
- stop();
250
- };
251
- }
252
-
253
- /**
254
- * Copyright (c) Meta Platforms, Inc. and affiliates.
255
- *
256
- * This source code is licensed under the MIT license found in the
257
- * LICENSE file in the root directory of this source tree.
258
- *
259
- */
260
-
261
- function markSelection(editor, onReposition) {
262
- let previousAnchorNode = null;
263
- let previousAnchorOffset = null;
264
- let previousFocusNode = null;
265
- let previousFocusOffset = null;
266
- let removeRangeListener = () => {};
267
- function compute(editorState) {
268
- editorState.read(() => {
269
- const selection = $getSelection();
270
- if (!$isRangeSelection(selection)) {
271
- // TODO
272
- previousAnchorNode = null;
273
- previousAnchorOffset = null;
274
- previousFocusNode = null;
275
- previousFocusOffset = null;
276
- removeRangeListener();
277
- removeRangeListener = () => {};
278
- return;
279
- }
280
- const {
281
- anchor,
282
- focus
283
- } = selection;
284
- const currentAnchorNode = anchor.getNode();
285
- const currentAnchorNodeKey = currentAnchorNode.getKey();
286
- const currentAnchorOffset = anchor.offset;
287
- const currentFocusNode = focus.getNode();
288
- const currentFocusNodeKey = currentFocusNode.getKey();
289
- const currentFocusOffset = focus.offset;
290
- const currentAnchorNodeDOM = editor.getElementByKey(currentAnchorNodeKey);
291
- const currentFocusNodeDOM = editor.getElementByKey(currentFocusNodeKey);
292
- const differentAnchorDOM = previousAnchorNode === null || currentAnchorNodeDOM === null || currentAnchorOffset !== previousAnchorOffset || currentAnchorNodeKey !== previousAnchorNode.getKey() || currentAnchorNode !== previousAnchorNode && (!(previousAnchorNode instanceof TextNode) || currentAnchorNode.updateDOM(previousAnchorNode, currentAnchorNodeDOM, editor._config));
293
- const differentFocusDOM = previousFocusNode === null || currentFocusNodeDOM === null || currentFocusOffset !== previousFocusOffset || currentFocusNodeKey !== previousFocusNode.getKey() || currentFocusNode !== previousFocusNode && (!(previousFocusNode instanceof TextNode) || currentFocusNode.updateDOM(previousFocusNode, currentFocusNodeDOM, editor._config));
294
- if (differentAnchorDOM || differentFocusDOM) {
295
- const anchorHTMLElement = editor.getElementByKey(anchor.getNode().getKey());
296
- const focusHTMLElement = editor.getElementByKey(focus.getNode().getKey());
297
- // TODO handle selection beyond the common TextNode
298
- if (anchorHTMLElement !== null && focusHTMLElement !== null && anchorHTMLElement.tagName === 'SPAN' && focusHTMLElement.tagName === 'SPAN') {
299
- const range = document.createRange();
300
- let firstHTMLElement;
301
- let firstOffset;
302
- let lastHTMLElement;
303
- let lastOffset;
304
- if (focus.isBefore(anchor)) {
305
- firstHTMLElement = focusHTMLElement;
306
- firstOffset = focus.offset;
307
- lastHTMLElement = anchorHTMLElement;
308
- lastOffset = anchor.offset;
309
- } else {
310
- firstHTMLElement = anchorHTMLElement;
311
- firstOffset = anchor.offset;
312
- lastHTMLElement = focusHTMLElement;
313
- lastOffset = focus.offset;
314
- }
315
- const firstTextNode = firstHTMLElement.firstChild;
316
- if (!(firstTextNode !== null)) {
317
- throw Error(`Expected text node to be first child of span`);
318
- }
319
- const lastTextNode = lastHTMLElement.firstChild;
320
- if (!(lastTextNode !== null)) {
321
- throw Error(`Expected text node to be first child of span`);
322
- }
323
- range.setStart(firstTextNode, firstOffset);
324
- range.setEnd(lastTextNode, lastOffset);
325
- removeRangeListener();
326
- removeRangeListener = positionNodeOnRange(editor, range, domNodes => {
327
- for (const domNode of domNodes) {
328
- const domNodeStyle = domNode.style;
329
- if (domNodeStyle.background !== 'Highlight') {
330
- domNodeStyle.background = 'Highlight';
331
- }
332
- if (domNodeStyle.color !== 'HighlightText') {
333
- domNodeStyle.color = 'HighlightText';
334
- }
335
- if (domNodeStyle.zIndex !== '-1') {
336
- domNodeStyle.zIndex = '-1';
337
- }
338
- if (domNodeStyle.pointerEvents !== 'none') {
339
- domNodeStyle.pointerEvents = 'none';
340
- }
341
- if (domNodeStyle.marginTop !== px(-1.5)) {
342
- domNodeStyle.marginTop = px(-1.5);
343
- }
344
- if (domNodeStyle.paddingTop !== px(4)) {
345
- domNodeStyle.paddingTop = px(4);
346
- }
347
- if (domNodeStyle.paddingBottom !== px(0)) {
348
- domNodeStyle.paddingBottom = px(0);
349
- }
350
- }
351
- if (onReposition !== undefined) {
352
- onReposition(domNodes);
353
- }
354
- });
355
- }
356
- }
357
- previousAnchorNode = currentAnchorNode;
358
- previousAnchorOffset = currentAnchorOffset;
359
- previousFocusNode = currentFocusNode;
360
- previousFocusOffset = currentFocusOffset;
361
- });
362
- }
363
- compute(editor.getEditorState());
364
- return mergeRegister(editor.registerUpdateListener(({
365
- editorState
366
- }) => compute(editorState)), removeRangeListener, () => {
367
- removeRangeListener();
368
- });
369
- }
370
-
371
- /**
372
- * Copyright (c) Meta Platforms, Inc. and affiliates.
373
- *
374
- * This source code is licensed under the MIT license found in the
375
- * LICENSE file in the root directory of this source tree.
376
- *
377
- */
378
-
379
- // Hotfix to export these with inlined types #5918
380
- const CAN_USE_BEFORE_INPUT = CAN_USE_BEFORE_INPUT$1;
381
- const CAN_USE_DOM = CAN_USE_DOM$1;
382
- const IS_ANDROID = IS_ANDROID$1;
383
- const IS_ANDROID_CHROME = IS_ANDROID_CHROME$1;
384
- const IS_APPLE = IS_APPLE$1;
385
- const IS_APPLE_WEBKIT = IS_APPLE_WEBKIT$1;
386
- const IS_CHROME = IS_CHROME$1;
387
- const IS_FIREFOX = IS_FIREFOX$1;
388
- const IS_IOS = IS_IOS$1;
389
- const IS_SAFARI = IS_SAFARI$1;
390
- /**
391
- * Takes an HTML element and adds the classNames passed within an array,
392
- * ignoring any non-string types. A space can be used to add multiple classes
393
- * eg. addClassNamesToElement(element, ['element-inner active', true, null])
394
- * will add both 'element-inner' and 'active' as classes to that element.
395
- * @param element - The element in which the classes are added
396
- * @param classNames - An array defining the class names to add to the element
397
- */
398
- function addClassNamesToElement(element, ...classNames) {
399
- const classesToAdd = normalizeClassNames(...classNames);
400
- if (classesToAdd.length > 0) {
401
- element.classList.add(...classesToAdd);
402
- }
403
- }
404
-
405
- /**
406
- * Takes an HTML element and removes the classNames passed within an array,
407
- * ignoring any non-string types. A space can be used to remove multiple classes
408
- * eg. removeClassNamesFromElement(element, ['active small', true, null])
409
- * will remove both the 'active' and 'small' classes from that element.
410
- * @param element - The element in which the classes are removed
411
- * @param classNames - An array defining the class names to remove from the element
412
- */
413
- function removeClassNamesFromElement(element, ...classNames) {
414
- const classesToRemove = normalizeClassNames(...classNames);
415
- if (classesToRemove.length > 0) {
416
- element.classList.remove(...classesToRemove);
417
- }
418
- }
419
-
420
- /**
421
- * Returns true if the file type matches the types passed within the acceptableMimeTypes array, false otherwise.
422
- * The types passed must be strings and are CASE-SENSITIVE.
423
- * eg. if file is of type 'text' and acceptableMimeTypes = ['TEXT', 'IMAGE'] the function will return false.
424
- * @param file - The file you want to type check.
425
- * @param acceptableMimeTypes - An array of strings of types which the file is checked against.
426
- * @returns true if the file is an acceptable mime type, false otherwise.
427
- */
428
- function isMimeType(file, acceptableMimeTypes) {
429
- for (const acceptableType of acceptableMimeTypes) {
430
- if (file.type.startsWith(acceptableType)) {
431
- return true;
432
- }
433
- }
434
- return false;
435
- }
436
-
437
- /**
438
- * Lexical File Reader with:
439
- * 1. MIME type support
440
- * 2. batched results (HistoryPlugin compatibility)
441
- * 3. Order aware (respects the order when multiple Files are passed)
442
- *
443
- * const filesResult = await mediaFileReader(files, ['image/']);
444
- * filesResult.forEach(file => editor.dispatchCommand('INSERT_IMAGE', \\{
445
- * src: file.result,
446
- * \\}));
447
- */
448
- function mediaFileReader(files, acceptableMimeTypes) {
449
- const filesIterator = files[Symbol.iterator]();
450
- return new Promise((resolve, reject) => {
451
- const processed = [];
452
- const handleNextFile = () => {
453
- const {
454
- done,
455
- value: file
456
- } = filesIterator.next();
457
- if (done) {
458
- return resolve(processed);
459
- }
460
- const fileReader = new FileReader();
461
- fileReader.addEventListener('error', reject);
462
- fileReader.addEventListener('load', () => {
463
- const result = fileReader.result;
464
- if (typeof result === 'string') {
465
- processed.push({
466
- file,
467
- result
468
- });
469
- }
470
- handleNextFile();
471
- });
472
- if (isMimeType(file, acceptableMimeTypes)) {
473
- fileReader.readAsDataURL(file);
474
- } else {
475
- handleNextFile();
476
- }
477
- };
478
- handleNextFile();
479
- });
480
- }
481
-
482
- /**
483
- * "Depth-First Search" starts at the root/top node of a tree and goes as far as it can down a branch end
484
- * before backtracking and finding a new path. Consider solving a maze by hugging either wall, moving down a
485
- * branch until you hit a dead-end (leaf) and backtracking to find the nearest branching path and repeat.
486
- * It will then return all the nodes found in the search in an array of objects.
487
- * @param startingNode - The node to start the search, if ommitted, it will start at the root node.
488
- * @param endingNode - The node to end the search, if ommitted, it will find all descendants of the startingNode.
489
- * @returns An array of objects of all the nodes found by the search, including their depth into the tree.
490
- * \\{depth: number, node: LexicalNode\\} It will always return at least 1 node (the ending node) so long as it exists
491
- */
492
- function $dfs(startingNode, endingNode) {
493
- const nodes = [];
494
- const start = (startingNode || $getRoot()).getLatest();
495
- const end = endingNode || ($isElementNode(start) ? start.getLastDescendant() || start : start);
496
- let node = start;
497
- let depth = $getDepth(node);
498
- while (node !== null && !node.is(end)) {
499
- nodes.push({
500
- depth,
501
- node
502
- });
503
- if ($isElementNode(node) && node.getChildrenSize() > 0) {
504
- node = node.getFirstChild();
505
- depth++;
506
- } else {
507
- // Find immediate sibling or nearest parent sibling
508
- let sibling = null;
509
- while (sibling === null && node !== null) {
510
- sibling = node.getNextSibling();
511
- if (sibling === null) {
512
- node = node.getParent();
513
- depth--;
514
- } else {
515
- node = sibling;
516
- }
517
- }
518
- }
519
- }
520
- if (node !== null && node.is(end)) {
521
- nodes.push({
522
- depth,
523
- node
524
- });
525
- }
526
- return nodes;
527
- }
528
- function $getDepth(node) {
529
- let innerNode = node;
530
- let depth = 0;
531
- while ((innerNode = innerNode.getParent()) !== null) {
532
- depth++;
533
- }
534
- return depth;
535
- }
536
-
537
- /**
538
- * Performs a right-to-left preorder tree traversal.
539
- * From the starting node it goes to the rightmost child, than backtracks to paret and finds new rightmost path.
540
- * It will return the next node in traversal sequence after the startingNode.
541
- * The traversal is similar to $dfs functions above, but the nodes are visited right-to-left, not left-to-right.
542
- * @param startingNode - The node to start the search.
543
- * @returns The next node in pre-order right to left traversal sequence or `null`, if the node does not exist
544
- */
545
- function $getNextRightPreorderNode(startingNode) {
546
- let node = startingNode;
547
- if ($isElementNode(node) && node.getChildrenSize() > 0) {
548
- node = node.getLastChild();
549
- } else {
550
- let sibling = null;
551
- while (sibling === null && node !== null) {
552
- sibling = node.getPreviousSibling();
553
- if (sibling === null) {
554
- node = node.getParent();
555
- } else {
556
- node = sibling;
557
- }
558
- }
559
- }
560
- return node;
561
- }
562
-
563
- /**
564
- * Takes a node and traverses up its ancestors (toward the root node)
565
- * in order to find a specific type of node.
566
- * @param node - the node to begin searching.
567
- * @param klass - an instance of the type of node to look for.
568
- * @returns the node of type klass that was passed, or null if none exist.
569
- */
570
- function $getNearestNodeOfType(node, klass) {
571
- let parent = node;
572
- while (parent != null) {
573
- if (parent instanceof klass) {
574
- return parent;
575
- }
576
- parent = parent.getParent();
577
- }
578
- return null;
579
- }
580
-
581
- /**
582
- * Returns the element node of the nearest ancestor, otherwise throws an error.
583
- * @param startNode - The starting node of the search
584
- * @returns The ancestor node found
585
- */
586
- function $getNearestBlockElementAncestorOrThrow(startNode) {
587
- const blockNode = $findMatchingParent(startNode, node => $isElementNode(node) && !node.isInline());
588
- if (!$isElementNode(blockNode)) {
589
- {
590
- throw Error(`Expected node ${startNode.__key} to have closest block element node.`);
591
- }
592
- }
593
- return blockNode;
594
- }
595
- /**
596
- * Starts with a node and moves up the tree (toward the root node) to find a matching node based on
597
- * the search parameters of the findFn. (Consider JavaScripts' .find() function where a testing function must be
598
- * passed as an argument. eg. if( (node) => node.__type === 'div') ) return true; otherwise return false
599
- * @param startingNode - The node where the search starts.
600
- * @param findFn - A testing function that returns true if the current node satisfies the testing parameters.
601
- * @returns A parent node that matches the findFn parameters, or null if one wasn't found.
602
- */
603
- const $findMatchingParent = (startingNode, findFn) => {
604
- let curr = startingNode;
605
- while (curr !== $getRoot() && curr != null) {
606
- if (findFn(curr)) {
607
- return curr;
608
- }
609
- curr = curr.getParent();
610
- }
611
- return null;
612
- };
613
-
614
- /**
615
- * Attempts to resolve nested element nodes of the same type into a single node of that type.
616
- * It is generally used for marks/commenting
617
- * @param editor - The lexical editor
618
- * @param targetNode - The target for the nested element to be extracted from.
619
- * @param cloneNode - See {@link $createMarkNode}
620
- * @param handleOverlap - Handles any overlap between the node to extract and the targetNode
621
- * @returns The lexical editor
622
- */
623
- function registerNestedElementResolver(editor, targetNode, cloneNode, handleOverlap) {
624
- const $isTargetNode = node => {
625
- return node instanceof targetNode;
626
- };
627
- const $findMatch = node => {
628
- // First validate we don't have any children that are of the target,
629
- // as we need to handle them first.
630
- const children = node.getChildren();
631
- for (let i = 0; i < children.length; i++) {
632
- const child = children[i];
633
- if ($isTargetNode(child)) {
634
- return null;
635
- }
636
- }
637
- let parentNode = node;
638
- let childNode = node;
639
- while (parentNode !== null) {
640
- childNode = parentNode;
641
- parentNode = parentNode.getParent();
642
- if ($isTargetNode(parentNode)) {
643
- return {
644
- child: childNode,
645
- parent: parentNode
646
- };
647
- }
648
- }
649
- return null;
650
- };
651
- const $elementNodeTransform = node => {
652
- const match = $findMatch(node);
653
- if (match !== null) {
654
- const {
655
- child,
656
- parent
657
- } = match;
658
-
659
- // Simple path, we can move child out and siblings into a new parent.
660
-
661
- if (child.is(node)) {
662
- handleOverlap(parent, node);
663
- const nextSiblings = child.getNextSiblings();
664
- const nextSiblingsLength = nextSiblings.length;
665
- parent.insertAfter(child);
666
- if (nextSiblingsLength !== 0) {
667
- const newParent = cloneNode(parent);
668
- child.insertAfter(newParent);
669
- for (let i = 0; i < nextSiblingsLength; i++) {
670
- newParent.append(nextSiblings[i]);
671
- }
672
- }
673
- if (!parent.canBeEmpty() && parent.getChildrenSize() === 0) {
674
- parent.remove();
675
- }
676
- }
677
- }
678
- };
679
- return editor.registerNodeTransform(targetNode, $elementNodeTransform);
680
- }
681
-
682
- /**
683
- * Clones the editor and marks it as dirty to be reconciled. If there was a selection,
684
- * it would be set back to its previous state, or null otherwise.
685
- * @param editor - The lexical editor
686
- * @param editorState - The editor's state
687
- */
688
- function $restoreEditorState(editor, editorState) {
689
- const FULL_RECONCILE = 2;
690
- const nodeMap = new Map();
691
- const activeEditorState = editor._pendingEditorState;
692
- for (const [key, node] of editorState._nodeMap) {
693
- nodeMap.set(key, $cloneWithProperties(node));
694
- }
695
- if (activeEditorState) {
696
- activeEditorState._nodeMap = nodeMap;
697
- }
698
- editor._dirtyType = FULL_RECONCILE;
699
- const selection = editorState._selection;
700
- $setSelection(selection === null ? null : selection.clone());
701
- }
702
-
703
- /**
704
- * If the selected insertion area is the root/shadow root node (see {@link lexical!$isRootOrShadowRoot}),
705
- * the node will be appended there, otherwise, it will be inserted before the insertion area.
706
- * If there is no selection where the node is to be inserted, it will be appended after any current nodes
707
- * within the tree, as a child of the root node. A paragraph node will then be added after the inserted node and selected.
708
- * @param node - The node to be inserted
709
- * @returns The node after its insertion
710
- */
711
- function $insertNodeToNearestRoot(node) {
712
- const selection = $getSelection() || $getPreviousSelection();
713
- if ($isRangeSelection(selection)) {
714
- const {
715
- focus
716
- } = selection;
717
- const focusNode = focus.getNode();
718
- const focusOffset = focus.offset;
719
- if ($isRootOrShadowRoot(focusNode)) {
720
- const focusChild = focusNode.getChildAtIndex(focusOffset);
721
- if (focusChild == null) {
722
- focusNode.append(node);
723
- } else {
724
- focusChild.insertBefore(node);
725
- }
726
- node.selectNext();
727
- } else {
728
- let splitNode;
729
- let splitOffset;
730
- if ($isTextNode(focusNode)) {
731
- splitNode = focusNode.getParentOrThrow();
732
- splitOffset = focusNode.getIndexWithinParent();
733
- if (focusOffset > 0) {
734
- splitOffset += 1;
735
- focusNode.splitText(focusOffset);
736
- }
737
- } else {
738
- splitNode = focusNode;
739
- splitOffset = focusOffset;
740
- }
741
- const [, rightTree] = $splitNode(splitNode, splitOffset);
742
- rightTree.insertBefore(node);
743
- rightTree.selectStart();
744
- }
745
- } else {
746
- if (selection != null) {
747
- const nodes = selection.getNodes();
748
- nodes[nodes.length - 1].getTopLevelElementOrThrow().insertAfter(node);
749
- } else {
750
- const root = $getRoot();
751
- root.append(node);
752
- }
753
- const paragraphNode = $createParagraphNode();
754
- node.insertAfter(paragraphNode);
755
- paragraphNode.select();
756
- }
757
- return node.getLatest();
758
- }
759
-
760
- /**
761
- * Wraps the node into another node created from a createElementNode function, eg. $createParagraphNode
762
- * @param node - Node to be wrapped.
763
- * @param createElementNode - Creates a new lexical element to wrap the to-be-wrapped node and returns it.
764
- * @returns A new lexical element with the previous node appended within (as a child, including its children).
765
- */
766
- function $wrapNodeInElement(node, createElementNode) {
767
- const elementNode = createElementNode();
768
- node.replace(elementNode);
769
- elementNode.append(node);
770
- return elementNode;
771
- }
772
-
773
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
774
-
775
- /**
776
- * @param object = The instance of the type
777
- * @param objectClass = The class of the type
778
- * @returns Whether the object is has the same Klass of the objectClass, ignoring the difference across window (e.g. different iframs)
779
- */
780
- function objectKlassEquals(object, objectClass) {
781
- return object !== null ? Object.getPrototypeOf(object).constructor.name === objectClass.name : false;
782
- }
783
-
784
- /**
785
- * Filter the nodes
786
- * @param nodes Array of nodes that needs to be filtered
787
- * @param filterFn A filter function that returns node if the current node satisfies the condition otherwise null
788
- * @returns Array of filtered nodes
789
- */
790
-
791
- function $filter(nodes, filterFn) {
792
- const result = [];
793
- for (let i = 0; i < nodes.length; i++) {
794
- const node = filterFn(nodes[i]);
795
- if (node !== null) {
796
- result.push(node);
797
- }
798
- }
799
- return result;
800
- }
801
- /**
802
- * Appends the node before the first child of the parent node
803
- * @param parent A parent node
804
- * @param node Node that needs to be appended
805
- */
806
- function $insertFirst(parent, node) {
807
- const firstChild = parent.getFirstChild();
808
- if (firstChild !== null) {
809
- firstChild.insertBefore(node);
810
- } else {
811
- parent.append(node);
812
- }
813
- }
814
-
815
- /**
816
- * Calculates the zoom level of an element as a result of using
817
- * css zoom property.
818
- * @param element
819
- */
820
- function calculateZoomLevel(element) {
821
- if (IS_FIREFOX) {
822
- return 1;
823
- }
824
- let zoom = 1;
825
- while (element) {
826
- zoom *= Number(window.getComputedStyle(element).getPropertyValue('zoom'));
827
- element = element.parentElement;
828
- }
829
- return zoom;
830
- }
831
-
832
- /**
833
- * Checks if the editor is a nested editor created by LexicalNestedComposer
834
- */
835
- function $isEditorIsNestedEditor(editor) {
836
- return editor._parentEditor !== null;
837
- }
838
-
839
- export { $dfs, $filter, $findMatchingParent, $getNearestBlockElementAncestorOrThrow, $getNearestNodeOfType, $getNextRightPreorderNode, $insertFirst, $insertNodeToNearestRoot, $isEditorIsNestedEditor, $restoreEditorState, $wrapNodeInElement, CAN_USE_BEFORE_INPUT, CAN_USE_DOM, IS_ANDROID, IS_ANDROID_CHROME, IS_APPLE, IS_APPLE_WEBKIT, IS_CHROME, IS_FIREFOX, IS_IOS, IS_SAFARI, addClassNamesToElement, calculateZoomLevel, isMimeType, markSelection, mediaFileReader, mergeRegister, objectKlassEquals, positionNodeOnRange, registerNestedElementResolver, removeClassNamesFromElement };