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

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