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