@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,1028 +0,0 @@
1
- 'use client';
2
-
3
- import { $isTextNode, $getEditor, $isRootNode, $isTokenOrSegmented, $getCharacterOffsets, $isElementNode, $getNodeByKey, $getPreviousSelection, $createTextNode, $isRangeSelection, $getSelection, $caretRangeFromSelection, $createRangeSelection, INTERNAL_$isBlock, $setSelection, $isRootOrShadowRoot, $hasAncestor, $isLeafNode, $caretFromPoint, $isExtendableTextPointCaret, $extendCaretToRange, $isChildCaret, $isDecoratorNode } from 'lexical';
4
- export { $cloneWithProperties, $selectAll } from 'lexical';
5
-
6
- /**
7
- * Copyright (c) Meta Platforms, Inc. and affiliates.
8
- *
9
- * This source code is licensed under the MIT license found in the
10
- * LICENSE file in the root directory of this source tree.
11
- *
12
- */
13
-
14
-
15
- /**
16
- * Copyright (c) Meta Platforms, Inc. and affiliates.
17
- *
18
- * This source code is licensed under the MIT license found in the
19
- * LICENSE file in the root directory of this source tree.
20
- *
21
- */
22
-
23
- // Do not require this module directly! Use normal `invariant` calls.
24
-
25
- function formatDevErrorMessage(message) {
26
- throw new Error(message);
27
- }
28
-
29
- /**
30
- * Copyright (c) Meta Platforms, Inc. and affiliates.
31
- *
32
- * This source code is licensed under the MIT license found in the
33
- * LICENSE file in the root directory of this source tree.
34
- *
35
- */
36
- const CSS_TO_STYLES = new Map();
37
-
38
- /**
39
- * Copyright (c) Meta Platforms, Inc. and affiliates.
40
- *
41
- * This source code is licensed under the MIT license found in the
42
- * LICENSE file in the root directory of this source tree.
43
- *
44
- */
45
-
46
- function getDOMTextNode(element) {
47
- let node = element;
48
- while (node != null) {
49
- if (node.nodeType === Node.TEXT_NODE) {
50
- return node;
51
- }
52
- node = node.firstChild;
53
- }
54
- return null;
55
- }
56
- function getDOMIndexWithinParent(node) {
57
- const parent = node.parentNode;
58
- if (parent == null) {
59
- throw new Error('Should never happen');
60
- }
61
- return [parent, Array.from(parent.childNodes).indexOf(node)];
62
- }
63
-
64
- /**
65
- * Creates a selection range for the DOM.
66
- * @param editor - The lexical editor.
67
- * @param anchorNode - The anchor node of a selection.
68
- * @param _anchorOffset - The amount of space offset from the anchor to the focus.
69
- * @param focusNode - The current focus.
70
- * @param _focusOffset - The amount of space offset from the focus to the anchor.
71
- * @returns The range of selection for the DOM that was created.
72
- */
73
- function createDOMRange(editor, anchorNode, _anchorOffset, focusNode, _focusOffset) {
74
- const anchorKey = anchorNode.getKey();
75
- const focusKey = focusNode.getKey();
76
- const range = document.createRange();
77
- let anchorDOM = editor.getElementByKey(anchorKey);
78
- let focusDOM = editor.getElementByKey(focusKey);
79
- let anchorOffset = _anchorOffset;
80
- let focusOffset = _focusOffset;
81
- if ($isTextNode(anchorNode)) {
82
- anchorDOM = getDOMTextNode(anchorDOM);
83
- }
84
- if ($isTextNode(focusNode)) {
85
- focusDOM = getDOMTextNode(focusDOM);
86
- }
87
- if (anchorNode === undefined || focusNode === undefined || anchorDOM === null || focusDOM === null) {
88
- return null;
89
- }
90
- if (anchorDOM.nodeName === 'BR') {
91
- [anchorDOM, anchorOffset] = getDOMIndexWithinParent(anchorDOM);
92
- }
93
- if (focusDOM.nodeName === 'BR') {
94
- [focusDOM, focusOffset] = getDOMIndexWithinParent(focusDOM);
95
- }
96
- const firstChild = anchorDOM.firstChild;
97
- if (anchorDOM === focusDOM && firstChild != null && firstChild.nodeName === 'BR' && anchorOffset === 0 && focusOffset === 0) {
98
- focusOffset = 1;
99
- }
100
- try {
101
- range.setStart(anchorDOM, anchorOffset);
102
- range.setEnd(focusDOM, focusOffset);
103
- } catch (e) {
104
- return null;
105
- }
106
- if (range.collapsed && (anchorOffset !== focusOffset || anchorKey !== focusKey)) {
107
- // Range is backwards, we need to reverse it
108
- range.setStart(focusDOM, focusOffset);
109
- range.setEnd(anchorDOM, anchorOffset);
110
- }
111
- return range;
112
- }
113
-
114
- /**
115
- * Creates DOMRects, generally used to help the editor find a specific location on the screen.
116
- * @param editor - The lexical editor
117
- * @param range - A fragment of a document that can contain nodes and parts of text nodes.
118
- * @returns The selectionRects as an array.
119
- */
120
- function createRectsFromDOMRange(editor, range) {
121
- const rootElement = editor.getRootElement();
122
- if (rootElement === null) {
123
- return [];
124
- }
125
- const rootRect = rootElement.getBoundingClientRect();
126
- const computedStyle = getComputedStyle(rootElement);
127
- const rootPadding = parseFloat(computedStyle.paddingLeft) + parseFloat(computedStyle.paddingRight);
128
- const selectionRects = Array.from(range.getClientRects());
129
- let selectionRectsLength = selectionRects.length;
130
- //sort rects from top left to bottom right.
131
- selectionRects.sort((a, b) => {
132
- const top = a.top - b.top;
133
- // Some rects match position closely, but not perfectly,
134
- // so we give a 3px tolerance.
135
- if (Math.abs(top) <= 3) {
136
- return a.left - b.left;
137
- }
138
- return top;
139
- });
140
- let prevRect;
141
- for (let i = 0; i < selectionRectsLength; i++) {
142
- const selectionRect = selectionRects[i];
143
- // Exclude rects that overlap preceding Rects in the sorted list.
144
- const isOverlappingRect = prevRect && prevRect.top <= selectionRect.top && prevRect.top + prevRect.height > selectionRect.top && prevRect.left + prevRect.width > selectionRect.left;
145
- // Exclude selections that span the entire element
146
- const selectionSpansElement = selectionRect.width + rootPadding === rootRect.width;
147
- if (isOverlappingRect || selectionSpansElement) {
148
- selectionRects.splice(i--, 1);
149
- selectionRectsLength--;
150
- continue;
151
- }
152
- prevRect = selectionRect;
153
- }
154
- return selectionRects;
155
- }
156
-
157
- /**
158
- * Creates an object containing all the styles and their values provided in the CSS string.
159
- * @param css - The CSS string of styles and their values.
160
- * @returns The styleObject containing all the styles and their values.
161
- */
162
- function getStyleObjectFromRawCSS(css) {
163
- const styleObject = {};
164
- if (!css) {
165
- return styleObject;
166
- }
167
- const styles = css.split(';');
168
- for (const style of styles) {
169
- if (style !== '') {
170
- const [key, value] = style.split(/:([^]+)/); // split on first colon
171
- if (key && value) {
172
- styleObject[key.trim()] = value.trim();
173
- }
174
- }
175
- }
176
- return styleObject;
177
- }
178
-
179
- /**
180
- * Given a CSS string, returns an object from the style cache.
181
- * @param css - The CSS property as a string.
182
- * @returns The value of the given CSS property.
183
- */
184
- function getStyleObjectFromCSS(css) {
185
- let value = CSS_TO_STYLES.get(css);
186
- if (value === undefined) {
187
- value = getStyleObjectFromRawCSS(css);
188
- CSS_TO_STYLES.set(css, value);
189
- }
190
- {
191
- // Freeze the value in DEV to prevent accidental mutations
192
- Object.freeze(value);
193
- }
194
- return value;
195
- }
196
-
197
- /**
198
- * Gets the CSS styles from the style object.
199
- * @param styles - The style object containing the styles to get.
200
- * @returns A string containing the CSS styles and their values.
201
- */
202
- function getCSSFromStyleObject(styles) {
203
- let css = '';
204
- for (const style in styles) {
205
- if (style) {
206
- css += `${style}: ${styles[style]};`;
207
- }
208
- }
209
- return css;
210
- }
211
-
212
- /**
213
- * Gets the computed DOM styles of the element.
214
- * @param node - The node to check the styles for.
215
- * @returns the computed styles of the element or null if there is no DOM element or no default view for the document.
216
- */
217
- function $getComputedStyleForElement(element) {
218
- const editor = $getEditor();
219
- const domElement = editor.getElementByKey(element.getKey());
220
- if (domElement === null) {
221
- return null;
222
- }
223
- const view = domElement.ownerDocument.defaultView;
224
- if (view === null) {
225
- return null;
226
- }
227
- return view.getComputedStyle(domElement);
228
- }
229
-
230
- /**
231
- * Gets the computed DOM styles of the parent of the node.
232
- * @param node - The node to check its parent's styles for.
233
- * @returns the computed styles of the node or null if there is no DOM element or no default view for the document.
234
- */
235
- function $getComputedStyleForParent(node) {
236
- const parent = $isRootNode(node) ? node : node.getParentOrThrow();
237
- return $getComputedStyleForElement(parent);
238
- }
239
-
240
- /**
241
- * Determines whether a node's parent is RTL.
242
- * @param node - The node to check whether it is RTL.
243
- * @returns whether the node is RTL.
244
- */
245
- function $isParentRTL(node) {
246
- const styles = $getComputedStyleForParent(node);
247
- return styles !== null && styles.direction === 'rtl';
248
- }
249
-
250
- /**
251
- * Generally used to append text content to HTML and JSON. Grabs the text content and "slices"
252
- * it to be generated into the new TextNode.
253
- * @param selection - The selection containing the node whose TextNode is to be edited.
254
- * @param textNode - The TextNode to be edited.
255
- * @returns The updated TextNode.
256
- */
257
- function $sliceSelectedTextNodeContent(selection, textNode) {
258
- const anchorAndFocus = selection.getStartEndPoints();
259
- if (textNode.isSelected(selection) && !$isTokenOrSegmented(textNode) && anchorAndFocus !== null) {
260
- const [anchor, focus] = anchorAndFocus;
261
- const isBackward = selection.isBackward();
262
- const anchorNode = anchor.getNode();
263
- const focusNode = focus.getNode();
264
- const isAnchor = textNode.is(anchorNode);
265
- const isFocus = textNode.is(focusNode);
266
- if (isAnchor || isFocus) {
267
- const [anchorOffset, focusOffset] = $getCharacterOffsets(selection);
268
- const isSame = anchorNode.is(focusNode);
269
- const isFirst = textNode.is(isBackward ? focusNode : anchorNode);
270
- const isLast = textNode.is(isBackward ? anchorNode : focusNode);
271
- let startOffset = 0;
272
- let endOffset = undefined;
273
- if (isSame) {
274
- startOffset = anchorOffset > focusOffset ? focusOffset : anchorOffset;
275
- endOffset = anchorOffset > focusOffset ? anchorOffset : focusOffset;
276
- } else if (isFirst) {
277
- const offset = isBackward ? focusOffset : anchorOffset;
278
- startOffset = offset;
279
- endOffset = undefined;
280
- } else if (isLast) {
281
- const offset = isBackward ? anchorOffset : focusOffset;
282
- startOffset = 0;
283
- endOffset = offset;
284
- }
285
-
286
- // NOTE: This mutates __text directly because the primary use case is to
287
- // modify a $cloneWithProperties node that should never be added
288
- // to the EditorState so we must not call getWritable via setTextContent
289
- textNode.__text = textNode.__text.slice(startOffset, endOffset);
290
- }
291
- }
292
- return textNode;
293
- }
294
-
295
- /**
296
- * Determines if the current selection is at the end of the node.
297
- * @param point - The point of the selection to test.
298
- * @returns true if the provided point offset is in the last possible position, false otherwise.
299
- */
300
- function $isAtNodeEnd(point) {
301
- if (point.type === 'text') {
302
- return point.offset === point.getNode().getTextContentSize();
303
- }
304
- const node = point.getNode();
305
- if (!$isElementNode(node)) {
306
- formatDevErrorMessage(`isAtNodeEnd: node must be a TextNode or ElementNode`);
307
- }
308
- return point.offset === node.getChildrenSize();
309
- }
310
-
311
- /**
312
- * Trims text from a node in order to shorten it, eg. to enforce a text's max length. If it deletes text
313
- * that is an ancestor of the anchor then it will leave 2 indents, otherwise, if no text content exists, it deletes
314
- * the TextNode. It will move the focus to either the end of any left over text or beginning of a new TextNode.
315
- * @param editor - The lexical editor.
316
- * @param anchor - The anchor of the current selection, where the selection should be pointing.
317
- * @param delCount - The amount of characters to delete. Useful as a dynamic variable eg. textContentSize - maxLength;
318
- */
319
- function $trimTextContentFromAnchor(editor, anchor, delCount) {
320
- // Work from the current selection anchor point
321
- let currentNode = anchor.getNode();
322
- let remaining = delCount;
323
- if ($isElementNode(currentNode)) {
324
- const descendantNode = currentNode.getDescendantByIndex(anchor.offset);
325
- if (descendantNode !== null) {
326
- currentNode = descendantNode;
327
- }
328
- }
329
- while (remaining > 0 && currentNode !== null) {
330
- if ($isElementNode(currentNode)) {
331
- const lastDescendant = currentNode.getLastDescendant();
332
- if (lastDescendant !== null) {
333
- currentNode = lastDescendant;
334
- }
335
- }
336
- let nextNode = currentNode.getPreviousSibling();
337
- let additionalElementWhitespace = 0;
338
- if (nextNode === null) {
339
- let parent = currentNode.getParentOrThrow();
340
- let parentSibling = parent.getPreviousSibling();
341
- while (parentSibling === null) {
342
- parent = parent.getParent();
343
- if (parent === null) {
344
- nextNode = null;
345
- break;
346
- }
347
- parentSibling = parent.getPreviousSibling();
348
- }
349
- if (parent !== null) {
350
- additionalElementWhitespace = parent.isInline() ? 0 : 2;
351
- nextNode = parentSibling;
352
- }
353
- }
354
- let text = currentNode.getTextContent();
355
- // If the text is empty, we need to consider adding in two line breaks to match
356
- // the content if we were to get it from its parent.
357
- if (text === '' && $isElementNode(currentNode) && !currentNode.isInline()) {
358
- // TODO: should this be handled in core?
359
- text = '\n\n';
360
- }
361
- const currentNodeSize = text.length;
362
- if (!$isTextNode(currentNode) || remaining >= currentNodeSize) {
363
- const parent = currentNode.getParent();
364
- currentNode.remove();
365
- if (parent != null && parent.getChildrenSize() === 0 && !$isRootNode(parent)) {
366
- parent.remove();
367
- }
368
- remaining -= currentNodeSize + additionalElementWhitespace;
369
- currentNode = nextNode;
370
- } else {
371
- const key = currentNode.getKey();
372
- // See if we can just revert it to what was in the last editor state
373
- const prevTextContent = editor.getEditorState().read(() => {
374
- const prevNode = $getNodeByKey(key);
375
- if ($isTextNode(prevNode) && prevNode.isSimpleText()) {
376
- return prevNode.getTextContent();
377
- }
378
- return null;
379
- });
380
- const offset = currentNodeSize - remaining;
381
- const slicedText = text.slice(0, offset);
382
- if (prevTextContent !== null && prevTextContent !== text) {
383
- const prevSelection = $getPreviousSelection();
384
- let target = currentNode;
385
- if (!currentNode.isSimpleText()) {
386
- const textNode = $createTextNode(prevTextContent);
387
- currentNode.replace(textNode);
388
- target = textNode;
389
- } else {
390
- currentNode.setTextContent(prevTextContent);
391
- }
392
- if ($isRangeSelection(prevSelection) && prevSelection.isCollapsed()) {
393
- const prevOffset = prevSelection.anchor.offset;
394
- target.select(prevOffset, prevOffset);
395
- }
396
- } else if (currentNode.isSimpleText()) {
397
- // Split text
398
- const isSelected = anchor.key === key;
399
- let anchorOffset = anchor.offset;
400
- // Move offset to end if it's less than the remaining number, otherwise
401
- // we'll have a negative splitStart.
402
- if (anchorOffset < remaining) {
403
- anchorOffset = currentNodeSize;
404
- }
405
- const splitStart = isSelected ? anchorOffset - remaining : 0;
406
- const splitEnd = isSelected ? anchorOffset : offset;
407
- if (isSelected && splitStart === 0) {
408
- const [excessNode] = currentNode.splitText(splitStart, splitEnd);
409
- excessNode.remove();
410
- } else {
411
- const [, excessNode] = currentNode.splitText(splitStart, splitEnd);
412
- excessNode.remove();
413
- }
414
- } else {
415
- const textNode = $createTextNode(slicedText);
416
- currentNode.replace(textNode);
417
- }
418
- remaining = 0;
419
- }
420
- }
421
- }
422
-
423
- /**
424
- * Gets the TextNode's style object and adds the styles to the CSS.
425
- * @param node - The TextNode to add styles to.
426
- */
427
- function $addNodeStyle(node) {
428
- const CSSText = node.getStyle();
429
- const styles = getStyleObjectFromRawCSS(CSSText);
430
- CSS_TO_STYLES.set(CSSText, styles);
431
- }
432
-
433
- /**
434
- * Applies the provided styles to the given TextNode, ElementNode, or
435
- * collapsed RangeSelection.
436
- *
437
- * @param target - The TextNode, ElementNode, or collapsed RangeSelection to apply the styles to
438
- * @param patch - The patch to apply, which can include multiple styles. \\{CSSProperty: value\\} . Can also accept a function that returns the new property value.
439
- */
440
- function $patchStyle(target, patch) {
441
- if (!($isRangeSelection(target) ? target.isCollapsed() : $isTextNode(target) || $isElementNode(target))) {
442
- formatDevErrorMessage(`$patchStyle must only be called with a TextNode, ElementNode, or collapsed RangeSelection`);
443
- }
444
- const prevStyles = getStyleObjectFromCSS($isRangeSelection(target) ? target.style : $isTextNode(target) ? target.getStyle() : target.getTextStyle());
445
- const newStyles = Object.entries(patch).reduce((styles, [key, value]) => {
446
- if (typeof value === 'function') {
447
- styles[key] = value(prevStyles[key], target);
448
- } else if (value === null) {
449
- delete styles[key];
450
- } else {
451
- styles[key] = value;
452
- }
453
- return styles;
454
- }, {
455
- ...prevStyles
456
- });
457
- const newCSSText = getCSSFromStyleObject(newStyles);
458
- if ($isRangeSelection(target) || $isTextNode(target)) {
459
- target.setStyle(newCSSText);
460
- } else {
461
- target.setTextStyle(newCSSText);
462
- }
463
- CSS_TO_STYLES.set(newCSSText, newStyles);
464
- }
465
-
466
- /**
467
- * Applies the provided styles to the TextNodes in the provided Selection.
468
- * Will update partially selected TextNodes by splitting the TextNode and applying
469
- * the styles to the appropriate one.
470
- * @param selection - The selected node(s) to update.
471
- * @param patch - The patch to apply, which can include multiple styles. \\{CSSProperty: value\\} . Can also accept a function that returns the new property value.
472
- */
473
- function $patchStyleText(selection, patch) {
474
- if ($isRangeSelection(selection) && selection.isCollapsed()) {
475
- $patchStyle(selection, patch);
476
- const emptyNode = selection.anchor.getNode();
477
- if ($isElementNode(emptyNode) && emptyNode.isEmpty()) {
478
- $patchStyle(emptyNode, patch);
479
- }
480
- }
481
- $forEachSelectedTextNode(textNode => {
482
- $patchStyle(textNode, patch);
483
- });
484
- }
485
- function $forEachSelectedTextNode(fn) {
486
- const selection = $getSelection();
487
- if (!selection) {
488
- return;
489
- }
490
- const slicedTextNodes = new Map();
491
- const getSliceIndices = node => slicedTextNodes.get(node.getKey()) || [0, node.getTextContentSize()];
492
- if ($isRangeSelection(selection)) {
493
- for (const slice of $caretRangeFromSelection(selection).getTextSlices()) {
494
- if (slice) {
495
- slicedTextNodes.set(slice.caret.origin.getKey(), slice.getSliceIndices());
496
- }
497
- }
498
- }
499
- const selectedNodes = selection.getNodes();
500
- for (const selectedNode of selectedNodes) {
501
- if (!($isTextNode(selectedNode) && selectedNode.canHaveFormat())) {
502
- continue;
503
- }
504
- const [startOffset, endOffset] = getSliceIndices(selectedNode);
505
- // No actual text is selected, so do nothing.
506
- if (endOffset === startOffset) {
507
- continue;
508
- }
509
-
510
- // The entire node is selected or a token/segment, so just format it
511
- if ($isTokenOrSegmented(selectedNode) || startOffset === 0 && endOffset === selectedNode.getTextContentSize()) {
512
- fn(selectedNode);
513
- } else {
514
- // The node is partially selected, so split it into two or three nodes
515
- // and style the selected one.
516
- const splitNodes = selectedNode.splitText(startOffset, endOffset);
517
- const replacement = splitNodes[startOffset === 0 ? 0 : 1];
518
- fn(replacement);
519
- }
520
- }
521
- // Prior to NodeCaret #7046 this would have been a side-effect
522
- // so we do this for test compatibility.
523
- // TODO: we may want to consider simplifying by removing this
524
- if ($isRangeSelection(selection) && selection.anchor.type === 'text' && selection.focus.type === 'text' && selection.anchor.key === selection.focus.key) {
525
- $ensureForwardRangeSelection(selection);
526
- }
527
- }
528
-
529
- /**
530
- * Ensure that the given RangeSelection is not backwards. If it
531
- * is backwards, then the anchor and focus points will be swapped
532
- * in-place. Ensuring that the selection is a writable RangeSelection
533
- * is the responsibility of the caller (e.g. in a read-only context
534
- * you will want to clone $getSelection() before using this).
535
- *
536
- * @param selection a writable RangeSelection
537
- */
538
- function $ensureForwardRangeSelection(selection) {
539
- if (selection.isBackward()) {
540
- const {
541
- anchor,
542
- focus
543
- } = selection;
544
- // stash for the in-place swap
545
- const {
546
- key,
547
- offset,
548
- type
549
- } = anchor;
550
- anchor.set(focus.key, focus.offset, focus.type);
551
- focus.set(key, offset, type);
552
- }
553
- }
554
-
555
- function $copyBlockFormatIndent(srcNode, destNode) {
556
- const format = srcNode.getFormatType();
557
- const indent = srcNode.getIndent();
558
- if (format !== destNode.getFormatType()) {
559
- destNode.setFormat(format);
560
- }
561
- if (indent !== destNode.getIndent()) {
562
- destNode.setIndent(indent);
563
- }
564
- }
565
-
566
- /**
567
- * Converts all nodes in the selection that are of one block type to another.
568
- * @param selection - The selected blocks to be converted.
569
- * @param $createElement - The function that creates the node. eg. $createParagraphNode.
570
- * @param $afterCreateElement - The function that updates the new node based on the previous one ($copyBlockFormatIndent by default)
571
- */
572
- function $setBlocksType(selection, $createElement, $afterCreateElement = $copyBlockFormatIndent) {
573
- if (selection === null) {
574
- return;
575
- }
576
- // Selections tend to not include their containing blocks so we effectively
577
- // expand it here
578
- const anchorAndFocus = selection.getStartEndPoints();
579
- const blockMap = new Map();
580
- let newSelection = null;
581
- if (anchorAndFocus) {
582
- const [anchor, focus] = anchorAndFocus;
583
- newSelection = $createRangeSelection();
584
- newSelection.anchor.set(anchor.key, anchor.offset, anchor.type);
585
- newSelection.focus.set(focus.key, focus.offset, focus.type);
586
- const anchorBlock = $getAncestor(anchor.getNode(), INTERNAL_$isBlock);
587
- const focusBlock = $getAncestor(focus.getNode(), INTERNAL_$isBlock);
588
- if ($isElementNode(anchorBlock)) {
589
- blockMap.set(anchorBlock.getKey(), anchorBlock);
590
- }
591
- if ($isElementNode(focusBlock)) {
592
- blockMap.set(focusBlock.getKey(), focusBlock);
593
- }
594
- }
595
- for (const node of selection.getNodes()) {
596
- if ($isElementNode(node) && INTERNAL_$isBlock(node)) {
597
- blockMap.set(node.getKey(), node);
598
- } else if (anchorAndFocus === null) {
599
- const ancestorBlock = $getAncestor(node, INTERNAL_$isBlock);
600
- if ($isElementNode(ancestorBlock)) {
601
- blockMap.set(ancestorBlock.getKey(), ancestorBlock);
602
- }
603
- }
604
- }
605
- for (const [key, prevNode] of blockMap) {
606
- const element = $createElement();
607
- $afterCreateElement(prevNode, element);
608
- prevNode.replace(element, true);
609
- if (newSelection) {
610
- if (key === newSelection.anchor.key) {
611
- newSelection.anchor.set(element.getKey(), newSelection.anchor.offset, newSelection.anchor.type);
612
- }
613
- if (key === newSelection.focus.key) {
614
- newSelection.focus.set(element.getKey(), newSelection.focus.offset, newSelection.focus.type);
615
- }
616
- }
617
- }
618
- if (newSelection && selection.is($getSelection())) {
619
- $setSelection(newSelection);
620
- }
621
- }
622
- function isPointAttached(point) {
623
- return point.getNode().isAttached();
624
- }
625
- function $removeParentEmptyElements(startingNode) {
626
- let node = startingNode;
627
- while (node !== null && !$isRootOrShadowRoot(node)) {
628
- const latest = node.getLatest();
629
- const parentNode = node.getParent();
630
- if (latest.getChildrenSize() === 0) {
631
- node.remove(true);
632
- }
633
- node = parentNode;
634
- }
635
- }
636
-
637
- /**
638
- * @deprecated In favor of $setBlockTypes
639
- * Wraps all nodes in the selection into another node of the type returned by createElement.
640
- * @param selection - The selection of nodes to be wrapped.
641
- * @param createElement - A function that creates the wrapping ElementNode. eg. $createParagraphNode.
642
- * @param wrappingElement - An element to append the wrapped selection and its children to.
643
- */
644
- function $wrapNodes(selection, createElement, wrappingElement = null) {
645
- const anchorAndFocus = selection.getStartEndPoints();
646
- const anchor = anchorAndFocus ? anchorAndFocus[0] : null;
647
- const nodes = selection.getNodes();
648
- const nodesLength = nodes.length;
649
- if (anchor !== null && (nodesLength === 0 || nodesLength === 1 && anchor.type === 'element' && anchor.getNode().getChildrenSize() === 0)) {
650
- const target = anchor.type === 'text' ? anchor.getNode().getParentOrThrow() : anchor.getNode();
651
- const children = target.getChildren();
652
- let element = createElement();
653
- element.setFormat(target.getFormatType());
654
- element.setIndent(target.getIndent());
655
- children.forEach(child => element.append(child));
656
- if (wrappingElement) {
657
- element = wrappingElement.append(element);
658
- }
659
- target.replace(element);
660
- return;
661
- }
662
- let topLevelNode = null;
663
- let descendants = [];
664
- for (let i = 0; i < nodesLength; i++) {
665
- const node = nodes[i];
666
- // Determine whether wrapping has to be broken down into multiple chunks. This can happen if the
667
- // user selected multiple Root-like nodes that have to be treated separately as if they are
668
- // their own branch. I.e. you don't want to wrap a whole table, but rather the contents of each
669
- // of each of the cell nodes.
670
- if ($isRootOrShadowRoot(node)) {
671
- $wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
672
- descendants = [];
673
- topLevelNode = node;
674
- } else if (topLevelNode === null || topLevelNode !== null && $hasAncestor(node, topLevelNode)) {
675
- descendants.push(node);
676
- } else {
677
- $wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
678
- descendants = [node];
679
- }
680
- }
681
- $wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
682
- }
683
-
684
- /**
685
- * Wraps each node into a new ElementNode.
686
- * @param selection - The selection of nodes to wrap.
687
- * @param nodes - An array of nodes, generally the descendants of the selection.
688
- * @param nodesLength - The length of nodes.
689
- * @param createElement - A function that creates the wrapping ElementNode. eg. $createParagraphNode.
690
- * @param wrappingElement - An element to wrap all the nodes into.
691
- * @returns
692
- */
693
- function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingElement = null) {
694
- if (nodes.length === 0) {
695
- return;
696
- }
697
- const firstNode = nodes[0];
698
- const elementMapping = new Map();
699
- const elements = [];
700
- // The below logic is to find the right target for us to
701
- // either insertAfter/insertBefore/append the corresponding
702
- // elements to. This is made more complicated due to nested
703
- // structures.
704
- let target = $isElementNode(firstNode) ? firstNode : firstNode.getParentOrThrow();
705
- if (target.isInline()) {
706
- target = target.getParentOrThrow();
707
- }
708
- let targetIsPrevSibling = false;
709
- while (target !== null) {
710
- const prevSibling = target.getPreviousSibling();
711
- if (prevSibling !== null) {
712
- target = prevSibling;
713
- targetIsPrevSibling = true;
714
- break;
715
- }
716
- target = target.getParentOrThrow();
717
- if ($isRootOrShadowRoot(target)) {
718
- break;
719
- }
720
- }
721
- const emptyElements = new Set();
722
-
723
- // Find any top level empty elements
724
- for (let i = 0; i < nodesLength; i++) {
725
- const node = nodes[i];
726
- if ($isElementNode(node) && node.getChildrenSize() === 0) {
727
- emptyElements.add(node.getKey());
728
- }
729
- }
730
- const movedNodes = new Set();
731
-
732
- // Move out all leaf nodes into our elements array.
733
- // If we find a top level empty element, also move make
734
- // an element for that.
735
- for (let i = 0; i < nodesLength; i++) {
736
- const node = nodes[i];
737
- let parent = node.getParent();
738
- if (parent !== null && parent.isInline()) {
739
- parent = parent.getParent();
740
- }
741
- if (parent !== null && $isLeafNode(node) && !movedNodes.has(node.getKey())) {
742
- const parentKey = parent.getKey();
743
- if (elementMapping.get(parentKey) === undefined) {
744
- const targetElement = createElement();
745
- targetElement.setFormat(parent.getFormatType());
746
- targetElement.setIndent(parent.getIndent());
747
- elements.push(targetElement);
748
- elementMapping.set(parentKey, targetElement);
749
- // Move node and its siblings to the new
750
- // element.
751
- parent.getChildren().forEach(child => {
752
- targetElement.append(child);
753
- movedNodes.add(child.getKey());
754
- if ($isElementNode(child)) {
755
- // Skip nested leaf nodes if the parent has already been moved
756
- child.getChildrenKeys().forEach(key => movedNodes.add(key));
757
- }
758
- });
759
- $removeParentEmptyElements(parent);
760
- }
761
- } else if (emptyElements.has(node.getKey())) {
762
- if (!$isElementNode(node)) {
763
- formatDevErrorMessage(`Expected node in emptyElements to be an ElementNode`);
764
- }
765
- const targetElement = createElement();
766
- targetElement.setFormat(node.getFormatType());
767
- targetElement.setIndent(node.getIndent());
768
- elements.push(targetElement);
769
- node.remove(true);
770
- }
771
- }
772
- if (wrappingElement !== null) {
773
- for (let i = 0; i < elements.length; i++) {
774
- const element = elements[i];
775
- wrappingElement.append(element);
776
- }
777
- }
778
- let lastElement = null;
779
-
780
- // If our target is Root-like, let's see if we can re-adjust
781
- // so that the target is the first child instead.
782
- if ($isRootOrShadowRoot(target)) {
783
- if (targetIsPrevSibling) {
784
- if (wrappingElement !== null) {
785
- target.insertAfter(wrappingElement);
786
- } else {
787
- for (let i = elements.length - 1; i >= 0; i--) {
788
- const element = elements[i];
789
- target.insertAfter(element);
790
- }
791
- }
792
- } else {
793
- const firstChild = target.getFirstChild();
794
- if ($isElementNode(firstChild)) {
795
- target = firstChild;
796
- }
797
- if (firstChild === null) {
798
- if (wrappingElement) {
799
- target.append(wrappingElement);
800
- } else {
801
- for (let i = 0; i < elements.length; i++) {
802
- const element = elements[i];
803
- target.append(element);
804
- lastElement = element;
805
- }
806
- }
807
- } else {
808
- if (wrappingElement !== null) {
809
- firstChild.insertBefore(wrappingElement);
810
- } else {
811
- for (let i = 0; i < elements.length; i++) {
812
- const element = elements[i];
813
- firstChild.insertBefore(element);
814
- lastElement = element;
815
- }
816
- }
817
- }
818
- }
819
- } else {
820
- if (wrappingElement) {
821
- target.insertAfter(wrappingElement);
822
- } else {
823
- for (let i = elements.length - 1; i >= 0; i--) {
824
- const element = elements[i];
825
- target.insertAfter(element);
826
- lastElement = element;
827
- }
828
- }
829
- }
830
- const prevSelection = $getPreviousSelection();
831
- if ($isRangeSelection(prevSelection) && isPointAttached(prevSelection.anchor) && isPointAttached(prevSelection.focus)) {
832
- $setSelection(prevSelection.clone());
833
- } else if (lastElement !== null) {
834
- lastElement.selectEnd();
835
- } else {
836
- selection.dirty = true;
837
- }
838
- }
839
-
840
- /**
841
- * Tests if the selection's parent element has vertical writing mode.
842
- * @param selection - The selection whose parent to test.
843
- * @returns true if the selection's parent has vertical writing mode (writing-mode: vertical-rl), false otherwise.
844
- */
845
- function $isEditorVerticalOrientation(selection) {
846
- const computedStyle = $getComputedStyle(selection);
847
- return computedStyle !== null && computedStyle.writingMode === 'vertical-rl';
848
- }
849
-
850
- /**
851
- * Gets the computed DOM styles of the parent of the selection's anchor node.
852
- * @param selection - The selection to check the styles for.
853
- * @returns the computed styles of the node or null if there is no DOM element or no default view for the document.
854
- */
855
- function $getComputedStyle(selection) {
856
- const anchorNode = selection.anchor.getNode();
857
- if ($isElementNode(anchorNode)) {
858
- return $getComputedStyleForElement(anchorNode);
859
- }
860
- return $getComputedStyleForParent(anchorNode);
861
- }
862
-
863
- /**
864
- * Determines if the default character selection should be overridden. Used with DecoratorNodes
865
- * @param selection - The selection whose default character selection may need to be overridden.
866
- * @param isBackward - Is the selection backwards (the focus comes before the anchor)?
867
- * @returns true if it should be overridden, false if not.
868
- */
869
- function $shouldOverrideDefaultCharacterSelection(selection, isBackward) {
870
- const isVertical = $isEditorVerticalOrientation(selection);
871
-
872
- // In vertical writing mode, we adjust the direction for correct caret movement
873
- let adjustedIsBackward = isVertical ? !isBackward : isBackward;
874
-
875
- // In right-to-left writing mode, we invert the direction for correct caret movement
876
- if ($isParentElementRTL(selection)) {
877
- adjustedIsBackward = !adjustedIsBackward;
878
- }
879
- const focusCaret = $caretFromPoint(selection.focus, adjustedIsBackward ? 'previous' : 'next');
880
- if ($isExtendableTextPointCaret(focusCaret)) {
881
- return false;
882
- }
883
- for (const nextCaret of $extendCaretToRange(focusCaret)) {
884
- if ($isChildCaret(nextCaret)) {
885
- return !nextCaret.origin.isInline();
886
- } else if ($isElementNode(nextCaret.origin)) {
887
- continue;
888
- } else if ($isDecoratorNode(nextCaret.origin)) {
889
- return true;
890
- }
891
- break;
892
- }
893
- return false;
894
- }
895
-
896
- /**
897
- * Moves the selection according to the arguments.
898
- * @param selection - The selected text or nodes.
899
- * @param isHoldingShift - Is the shift key being held down during the operation.
900
- * @param isBackward - Is the selection selected backwards (the focus comes before the anchor)?
901
- * @param granularity - The distance to adjust the current selection.
902
- */
903
- function $moveCaretSelection(selection, isHoldingShift, isBackward, granularity) {
904
- selection.modify(isHoldingShift ? 'extend' : 'move', isBackward, granularity);
905
- }
906
-
907
- /**
908
- * Tests a parent element for right to left direction.
909
- * @param selection - The selection whose parent is to be tested.
910
- * @returns true if the selections' parent element has a direction of 'rtl' (right to left), false otherwise.
911
- */
912
- function $isParentElementRTL(selection) {
913
- const computedStyle = $getComputedStyle(selection);
914
- return computedStyle !== null && computedStyle.direction === 'rtl';
915
- }
916
-
917
- /**
918
- * Moves selection by character according to arguments.
919
- * @param selection - The selection of the characters to move.
920
- * @param isHoldingShift - Is the shift key being held down during the operation.
921
- * @param isBackward - Is the selection backward (the focus comes before the anchor)?
922
- */
923
- function $moveCharacter(selection, isHoldingShift, isBackward) {
924
- const isRTL = $isParentElementRTL(selection);
925
- const isVertical = $isEditorVerticalOrientation(selection);
926
-
927
- // In vertical-rl writing mode, arrow key directions need to be flipped
928
- // to match the visual flow of text (top to bottom, right to left)
929
- let adjustedIsBackward;
930
- if (isVertical) {
931
- // In vertical-rl mode, we need to completely invert the direction
932
- // Left arrow (backward) should move down (forward)
933
- // Right arrow (forward) should move up (backward)
934
- adjustedIsBackward = !isBackward;
935
- } else if (isRTL) {
936
- // In horizontal RTL mode, use the standard RTL behavior
937
- adjustedIsBackward = !isBackward;
938
- } else {
939
- // Standard LTR horizontal text
940
- adjustedIsBackward = isBackward;
941
- }
942
-
943
- // Apply the direction adjustment to move the caret
944
- $moveCaretSelection(selection, isHoldingShift, adjustedIsBackward, 'character');
945
- }
946
-
947
- /**
948
- * Returns the current value of a CSS property for Nodes, if set. If not set, it returns the defaultValue.
949
- * @param node - The node whose style value to get.
950
- * @param styleProperty - The CSS style property.
951
- * @param defaultValue - The default value for the property.
952
- * @returns The value of the property for node.
953
- */
954
- function $getNodeStyleValueForProperty(node, styleProperty, defaultValue) {
955
- const css = node.getStyle();
956
- const styleObject = getStyleObjectFromCSS(css);
957
- if (styleObject !== null) {
958
- return styleObject[styleProperty] || defaultValue;
959
- }
960
- return defaultValue;
961
- }
962
-
963
- /**
964
- * Returns the current value of a CSS property for TextNodes in the Selection, if set. If not set, it returns the defaultValue.
965
- * If all TextNodes do not have the same value, it returns an empty string.
966
- * @param selection - The selection of TextNodes whose value to find.
967
- * @param styleProperty - The CSS style property.
968
- * @param defaultValue - The default value for the property, defaults to an empty string.
969
- * @returns The value of the property for the selected TextNodes.
970
- */
971
- function $getSelectionStyleValueForProperty(selection, styleProperty, defaultValue = '') {
972
- let styleValue = null;
973
- const nodes = selection.getNodes();
974
- const anchor = selection.anchor;
975
- const focus = selection.focus;
976
- const isBackward = selection.isBackward();
977
- const endOffset = isBackward ? focus.offset : anchor.offset;
978
- const endNode = isBackward ? focus.getNode() : anchor.getNode();
979
- if ($isRangeSelection(selection) && selection.isCollapsed() && selection.style !== '') {
980
- const css = selection.style;
981
- const styleObject = getStyleObjectFromCSS(css);
982
- if (styleObject !== null && styleProperty in styleObject) {
983
- return styleObject[styleProperty];
984
- }
985
- }
986
- for (let i = 0; i < nodes.length; i++) {
987
- const node = nodes[i];
988
-
989
- // if no actual characters in the end node are selected, we don't
990
- // include it in the selection for purposes of determining style
991
- // value
992
- if (i !== 0 && endOffset === 0 && node.is(endNode)) {
993
- continue;
994
- }
995
- if ($isTextNode(node)) {
996
- const nodeStyleValue = $getNodeStyleValueForProperty(node, styleProperty, defaultValue);
997
- if (styleValue === null) {
998
- styleValue = nodeStyleValue;
999
- } else if (styleValue !== nodeStyleValue) {
1000
- // multiple text nodes are in the selection and they don't all
1001
- // have the same style.
1002
- styleValue = '';
1003
- break;
1004
- }
1005
- }
1006
- }
1007
- return styleValue === null ? defaultValue : styleValue;
1008
- }
1009
- function $getAncestor(node, predicate) {
1010
- let parent = node;
1011
- while (parent !== null && parent.getParent() !== null && !predicate(parent)) {
1012
- parent = parent.getParentOrThrow();
1013
- }
1014
- return predicate(parent) ? parent : null;
1015
- }
1016
-
1017
- /**
1018
- * Copyright (c) Meta Platforms, Inc. and affiliates.
1019
- *
1020
- * This source code is licensed under the MIT license found in the
1021
- * LICENSE file in the root directory of this source tree.
1022
- *
1023
- */
1024
-
1025
- /** @deprecated renamed to {@link $trimTextContentFromAnchor} by @lexical/eslint-plugin rules-of-lexical */
1026
- const trimTextContentFromAnchor = $trimTextContentFromAnchor;
1027
-
1028
- export { $addNodeStyle, $copyBlockFormatIndent, $ensureForwardRangeSelection, $forEachSelectedTextNode, $getComputedStyleForElement, $getComputedStyleForParent, $getSelectionStyleValueForProperty, $isAtNodeEnd, $isParentElementRTL, $isParentRTL, $moveCaretSelection, $moveCharacter, $patchStyleText, $setBlocksType, $shouldOverrideDefaultCharacterSelection, $sliceSelectedTextNodeContent, $trimTextContentFromAnchor, $wrapNodes, createDOMRange, createRectsFromDOMRange, getCSSFromStyleObject, getStyleObjectFromCSS, trimTextContentFromAnchor };