@lexical/selection 0.44.1-nightly.20260519.0 → 0.45.1-dev.0
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.
- package/{LexicalSelection.dev.js → dist/LexicalSelection.dev.js} +68 -24
- package/{LexicalSelection.dev.mjs → dist/LexicalSelection.dev.mjs} +68 -24
- package/{LexicalSelection.js.flow → dist/LexicalSelection.js.flow} +1 -1
- package/dist/LexicalSelection.prod.js +9 -0
- package/dist/LexicalSelection.prod.mjs +9 -0
- package/{lexical-node.d.ts → dist/lexical-node.d.ts} +0 -7
- package/{range-selection.d.ts → dist/range-selection.d.ts} +1 -2
- package/package.json +30 -15
- package/src/index.ts +47 -0
- package/src/lexical-node.ts +434 -0
- package/src/range-selection.ts +667 -0
- package/src/utils.ts +238 -0
- package/LexicalSelection.prod.js +0 -9
- package/LexicalSelection.prod.mjs +0 -9
- /package/{LexicalSelection.js → dist/LexicalSelection.js} +0 -0
- /package/{LexicalSelection.mjs → dist/LexicalSelection.mjs} +0 -0
- /package/{LexicalSelection.node.mjs → dist/LexicalSelection.node.mjs} +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{utils.d.ts → dist/utils.d.ts} +0 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
import type {ElementNode, LexicalEditor, LexicalNode} from 'lexical';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
$getEditor,
|
|
12
|
+
$isRootNode,
|
|
13
|
+
$isTextNode,
|
|
14
|
+
getStyleObjectFromCSS,
|
|
15
|
+
} from 'lexical';
|
|
16
|
+
|
|
17
|
+
function getDOMTextNode(element: Node | null): Text | null {
|
|
18
|
+
let node = element;
|
|
19
|
+
|
|
20
|
+
while (node != null) {
|
|
21
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
22
|
+
return node as Text;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
node = node.firstChild;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function getDOMIndexWithinParent(node: ChildNode): [ParentNode, number] {
|
|
32
|
+
const parent = node.parentNode;
|
|
33
|
+
|
|
34
|
+
if (parent == null) {
|
|
35
|
+
throw new Error('Should never happen');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return [parent, Array.from(parent.childNodes).indexOf(node)];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Creates a selection range for the DOM.
|
|
43
|
+
* @param editor - The lexical editor.
|
|
44
|
+
* @param anchorNode - The anchor node of a selection.
|
|
45
|
+
* @param _anchorOffset - The amount of space offset from the anchor to the focus.
|
|
46
|
+
* @param focusNode - The current focus.
|
|
47
|
+
* @param _focusOffset - The amount of space offset from the focus to the anchor.
|
|
48
|
+
* @returns The range of selection for the DOM that was created.
|
|
49
|
+
*/
|
|
50
|
+
export function createDOMRange(
|
|
51
|
+
editor: LexicalEditor,
|
|
52
|
+
anchorNode: LexicalNode,
|
|
53
|
+
_anchorOffset: number,
|
|
54
|
+
focusNode: LexicalNode,
|
|
55
|
+
_focusOffset: number,
|
|
56
|
+
): Range | null {
|
|
57
|
+
const anchorKey = anchorNode.getKey();
|
|
58
|
+
const focusKey = focusNode.getKey();
|
|
59
|
+
const range = document.createRange();
|
|
60
|
+
let anchorDOM: Node | Text | null = editor.getElementByKey(anchorKey);
|
|
61
|
+
let focusDOM: Node | Text | null = editor.getElementByKey(focusKey);
|
|
62
|
+
let anchorOffset = _anchorOffset;
|
|
63
|
+
let focusOffset = _focusOffset;
|
|
64
|
+
|
|
65
|
+
if ($isTextNode(anchorNode)) {
|
|
66
|
+
anchorDOM = getDOMTextNode(anchorDOM);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if ($isTextNode(focusNode)) {
|
|
70
|
+
focusDOM = getDOMTextNode(focusDOM);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
anchorNode === undefined ||
|
|
75
|
+
focusNode === undefined ||
|
|
76
|
+
anchorDOM === null ||
|
|
77
|
+
focusDOM === null
|
|
78
|
+
) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (anchorDOM.nodeName === 'BR') {
|
|
83
|
+
[anchorDOM, anchorOffset] = getDOMIndexWithinParent(anchorDOM as ChildNode);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (focusDOM.nodeName === 'BR') {
|
|
87
|
+
[focusDOM, focusOffset] = getDOMIndexWithinParent(focusDOM as ChildNode);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const firstChild = anchorDOM.firstChild;
|
|
91
|
+
|
|
92
|
+
if (
|
|
93
|
+
anchorDOM === focusDOM &&
|
|
94
|
+
firstChild != null &&
|
|
95
|
+
firstChild.nodeName === 'BR' &&
|
|
96
|
+
anchorOffset === 0 &&
|
|
97
|
+
focusOffset === 0
|
|
98
|
+
) {
|
|
99
|
+
focusOffset = 1;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
range.setStart(anchorDOM, anchorOffset);
|
|
104
|
+
range.setEnd(focusDOM, focusOffset);
|
|
105
|
+
} catch (_e) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (
|
|
110
|
+
range.collapsed &&
|
|
111
|
+
(anchorOffset !== focusOffset || anchorKey !== focusKey)
|
|
112
|
+
) {
|
|
113
|
+
// Range is backwards, we need to reverse it
|
|
114
|
+
range.setStart(focusDOM, focusOffset);
|
|
115
|
+
range.setEnd(anchorDOM, anchorOffset);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return range;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Creates DOMRects, generally used to help the editor find a specific location on the screen.
|
|
123
|
+
* @param editor - The lexical editor
|
|
124
|
+
* @param range - A fragment of a document that can contain nodes and parts of text nodes.
|
|
125
|
+
* @returns The selectionRects as an array.
|
|
126
|
+
*/
|
|
127
|
+
export function createRectsFromDOMRange(
|
|
128
|
+
editor: LexicalEditor,
|
|
129
|
+
range: Range,
|
|
130
|
+
): Array<ClientRect> {
|
|
131
|
+
const rootElement = editor.getRootElement();
|
|
132
|
+
|
|
133
|
+
if (rootElement === null) {
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
const rootRect = rootElement.getBoundingClientRect();
|
|
137
|
+
const computedStyle = getComputedStyle(rootElement);
|
|
138
|
+
const rootPadding =
|
|
139
|
+
parseFloat(computedStyle.paddingLeft) +
|
|
140
|
+
parseFloat(computedStyle.paddingRight);
|
|
141
|
+
const selectionRects = Array.from(range.getClientRects());
|
|
142
|
+
let selectionRectsLength = selectionRects.length;
|
|
143
|
+
//sort rects from top left to bottom right.
|
|
144
|
+
selectionRects.sort((a, b) => {
|
|
145
|
+
const top = a.top - b.top;
|
|
146
|
+
// Some rects match position closely, but not perfectly,
|
|
147
|
+
// so we give a 3px tolerance.
|
|
148
|
+
if (Math.abs(top) <= 3) {
|
|
149
|
+
return a.left - b.left;
|
|
150
|
+
}
|
|
151
|
+
return top;
|
|
152
|
+
});
|
|
153
|
+
let prevRect;
|
|
154
|
+
for (let i = 0; i < selectionRectsLength; i++) {
|
|
155
|
+
const selectionRect = selectionRects[i];
|
|
156
|
+
// Exclude rects that overlap preceding Rects in the sorted list.
|
|
157
|
+
const isOverlappingRect =
|
|
158
|
+
prevRect &&
|
|
159
|
+
prevRect.top <= selectionRect.top &&
|
|
160
|
+
prevRect.top + prevRect.height > selectionRect.top &&
|
|
161
|
+
prevRect.left + prevRect.width > selectionRect.left;
|
|
162
|
+
// Exclude selections that span the entire element
|
|
163
|
+
const selectionSpansElement =
|
|
164
|
+
selectionRect.width + rootPadding === rootRect.width;
|
|
165
|
+
if (isOverlappingRect || selectionSpansElement) {
|
|
166
|
+
selectionRects.splice(i--, 1);
|
|
167
|
+
selectionRectsLength--;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
prevRect = selectionRect;
|
|
171
|
+
}
|
|
172
|
+
return selectionRects;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @deprecated Use {@link getStyleObjectFromCSS}, this is just an alias for backwards compatibility.
|
|
177
|
+
*/
|
|
178
|
+
export const getStyleObjectFromRawCSS = getStyleObjectFromCSS;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Serializes a style object into a CSS declaration string, the inverse of
|
|
182
|
+
* {@link getStyleObjectFromCSS}.
|
|
183
|
+
* @param styles - An object mapping CSS property names to their values.
|
|
184
|
+
* @returns A CSS string of the form `prop: value;` for each entry, concatenated together.
|
|
185
|
+
*/
|
|
186
|
+
export function getCSSFromStyleObject(styles: Record<string, string>): string {
|
|
187
|
+
let css = '';
|
|
188
|
+
|
|
189
|
+
for (const style in styles) {
|
|
190
|
+
if (style) {
|
|
191
|
+
css += `${style}: ${styles[style]};`;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return css;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Gets the computed DOM styles of the element.
|
|
200
|
+
* @param element - The node to check the styles for.
|
|
201
|
+
* @returns the computed styles of the element or null if there is no DOM element or no default view for the document.
|
|
202
|
+
*/
|
|
203
|
+
export function $getComputedStyleForElement(
|
|
204
|
+
element: ElementNode,
|
|
205
|
+
): CSSStyleDeclaration | null {
|
|
206
|
+
const editor = $getEditor();
|
|
207
|
+
const domElement = editor.getElementByKey(element.getKey());
|
|
208
|
+
if (domElement === null) {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
const view = domElement.ownerDocument.defaultView;
|
|
212
|
+
if (view === null) {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
return view.getComputedStyle(domElement);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Gets the computed DOM styles of the parent of the node.
|
|
220
|
+
* @param node - The node to check its parent's styles for.
|
|
221
|
+
* @returns the computed styles of the node or null if there is no DOM element or no default view for the document.
|
|
222
|
+
*/
|
|
223
|
+
export function $getComputedStyleForParent(
|
|
224
|
+
node: LexicalNode,
|
|
225
|
+
): CSSStyleDeclaration | null {
|
|
226
|
+
const parent = $isRootNode(node) ? node : node.getParentOrThrow();
|
|
227
|
+
return $getComputedStyleForElement(parent);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Determines whether a node's parent is RTL.
|
|
232
|
+
* @param node - The node to check whether it is RTL.
|
|
233
|
+
* @returns whether the node is RTL.
|
|
234
|
+
*/
|
|
235
|
+
export function $isParentRTL(node: LexicalNode): boolean {
|
|
236
|
+
const styles = $getComputedStyleForParent(node);
|
|
237
|
+
return styles !== null && styles.direction === 'rtl';
|
|
238
|
+
}
|
package/LexicalSelection.prod.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
"use strict";var e=require("lexical");function t(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}function n(e){let t=e;for(;null!=t;){if(t.nodeType===Node.TEXT_NODE)return t;t=t.firstChild}return null}function o(e){const t=e.parentNode;if(null==t)throw new Error("Should never happen");return[t,Array.from(t.childNodes).indexOf(e)]}function r(e){let t="";for(const n in e)n&&(t+=`${n}: ${e[n]};`);return t}function s(t){const n=e.$getEditor().getElementByKey(t.getKey());if(null===n)return null;const o=n.ownerDocument.defaultView;return null===o?null:o.getComputedStyle(n)}function l(t){return s(e.$isRootNode(t)?t:t.getParentOrThrow())}function i(t,n,o){let r=n.getNode(),s=o;if(e.$isElementNode(r)){const e=r.getDescendantByIndex(n.offset);null!==e&&(r=e)}for(;s>0&&null!==r;){if(e.$isElementNode(r)){const e=r.getLastDescendant();null!==e&&(r=e)}let o=r.getPreviousSibling(),l=0;if(null===o){let e=r.getParentOrThrow(),t=e.getPreviousSibling();for(;null===t;){if(e=e.getParent(),null===e){o=null;break}t=e.getPreviousSibling()}null!==e&&(l=e.isInline()?0:2,o=t)}let i=r.getTextContent();""===i&&e.$isElementNode(r)&&!r.isInline()&&(i="\n\n");const c=i.length;if(!e.$isTextNode(r)||s>=c){const t=r.getParent();r.remove(),null==t||0!==t.getChildrenSize()||e.$isRootNode(t)||t.remove(),s-=c+l,r=o}else{const o=r.getKey(),l=t.getEditorState().read(()=>{const t=e.$getNodeByKey(o);return e.$isTextNode(t)&&t.isSimpleText()?t.getTextContent():null}),d=c-s,f=i.slice(0,d);if(null!==l&&l!==i){const t=e.$getPreviousSelection();let n=r;if(r.isSimpleText())r.setTextContent(l);else{const t=e.$createTextNode(l);r.replace(t),n=t}if(e.$isRangeSelection(t)&&t.isCollapsed()){const e=t.anchor.offset;n.select(e,e)}}else if(r.isSimpleText()){const e=n.key===o;let t=n.offset;t<s&&(t=c);const l=e?t-s:0,i=e?t:d;if(e&&0===l){const[e]=r.splitText(l,i);e.remove()}else{const[,e]=r.splitText(l,i);e.remove()}}else{const t=e.$createTextNode(f);r.replace(t)}s=0}}}const c=()=>{};function d(n,o){(e.$isRangeSelection(n)?n.isCollapsed():e.$isTextNode(n)||e.$isElementNode(n))||t(280);const s=e.getStyleObjectFromCSS(e.$isRangeSelection(n)?n.style:e.$isTextNode(n)?n.getStyle():n.getTextStyle()),l=r(Object.entries(o).reduce((e,[t,o])=>("function"==typeof o?e[t]=o(s[t],n):null===o?delete e[t]:e[t]=o,e),{...s}));e.$isRangeSelection(n)||e.$isTextNode(n)?n.setStyle(l):n.setTextStyle(l)}function f(t){const n=e.$getSelection();if(!n)return;const o=new Map,r=e=>o.get(e.getKey())||[0,e.getTextContentSize()];if(e.$isRangeSelection(n))for(const t of e.$caretRangeFromSelection(n).getTextSlices())t&&o.set(t.caret.origin.getKey(),t.getSliceIndices());const s=n.getNodes();for(const n of s){if(!e.$isTextNode(n)||!n.canHaveFormat())continue;const[o,s]=r(n);if(s!==o)if(e.$isTokenOrSegmented(n)||0===o&&s===n.getTextContentSize())t(n);else{t(n.splitText(o,s)[0===o?0:1])}}e.$isRangeSelection(n)&&"text"===n.anchor.type&&"text"===n.focus.type&&n.anchor.key===n.focus.key&&a(n)}function a(e){if(e.isBackward()){const{anchor:t,focus:n}=e,{key:o,offset:r,type:s}=t;t.set(n.key,n.offset,n.type),n.set(o,r,s)}}function g(e,t){const n=e.getFormatType(),o=e.getIndent();n!==t.getFormatType()&&t.setFormat(n),o!==t.getIndent()&&t.setIndent(o)}function u(e){return e.getNode().isAttached()}function p(t){let n=t;for(;null!==n&&!e.$isRootOrShadowRoot(n);){const e=n.getLatest(),t=n.getParent();0===e.getChildrenSize()&&n.remove(!0),n=t}}function $(n,o,r,s,l=null){if(0===o.length)return;const i=o[0],c=new Map,d=[];let f=e.$isElementNode(i)?i:i.getParentOrThrow();f.isInline()&&(f=f.getParentOrThrow());let a=!1;for(;null!==f;){const t=f.getPreviousSibling();if(null!==t){f=t,a=!0;break}if(f=f.getParentOrThrow(),e.$isRootOrShadowRoot(f))break}const g=new Set;for(let t=0;t<r;t++){const n=o[t];e.$isElementNode(n)&&0===n.getChildrenSize()&&g.add(n.getKey())}const $=new Set;for(let n=0;n<r;n++){const r=o[n];let l=r.getParent();if(null!==l&&l.isInline()&&(l=l.getParent()),null!==l&&e.$isLeafNode(r)&&!$.has(r.getKey())){const t=l.getKey();if(void 0===c.get(t)){const n=s();n.setFormat(l.getFormatType()),n.setIndent(l.getIndent()),d.push(n),c.set(t,n);const o=l.getChildren();n.splice(n.getChildrenSize(),0,o);for(const t of o)if($.add(t.getKey()),e.$isElementNode(t))for(const e of t.getChildrenKeys())$.add(e);p(l)}}else if(g.has(r.getKey())){e.$isElementNode(r)||t(179);const n=s();n.setFormat(r.getFormatType()),n.setIndent(r.getIndent()),d.push(n),r.remove(!0)}}if(null!==l)for(let e=0;e<d.length;e++){const t=d[e];l.append(t)}let h=null;if(e.$isRootOrShadowRoot(f))if(a)if(null!==l)f.insertAfter(l);else for(let e=d.length-1;e>=0;e--){const t=d[e];f.insertAfter(t)}else{const t=f.getFirstChild();if(e.$isElementNode(t)&&(f=t),null===t)if(l)f.append(l);else for(let e=0;e<d.length;e++){const t=d[e];f.append(t),h=t}else if(null!==l)t.insertBefore(l);else for(let e=0;e<d.length;e++){const n=d[e];t.insertBefore(n),h=n}}else if(l)f.insertAfter(l);else for(let e=d.length-1;e>=0;e--){const t=d[e];f.insertAfter(t),h=t}const S=e.$getPreviousSelection();e.$isRangeSelection(S)&&u(S.anchor)&&u(S.focus)?e.$setSelection(S.clone()):null!==h?h.selectEnd():n.dirty=!0}function h(e){const t=S(e);return null!==t&&"vertical-rl"===t.writingMode}function S(t){const n=t.anchor.getNode();return e.$isElementNode(n)?s(n):l(n)}function m(e,t,n,o){e.modify(t?"extend":"move",n,o)}function N(e){const t=S(e);return null!==t&&"rtl"===t.direction}function y(t,n,o){const r=t.getStyle(),s=e.getStyleObjectFromCSS(r);return null!==s&&s[n]||o}const x=e.getStyleObjectFromCSS,T=i;exports.$cloneWithProperties=e.$cloneWithProperties,exports.$selectAll=e.$selectAll,exports.$addNodeStyle=c,exports.$copyBlockFormatIndent=g,exports.$ensureForwardRangeSelection=a,exports.$forEachSelectedTextNode=f,exports.$getComputedStyleForElement=s,exports.$getComputedStyleForParent=l,exports.$getSelectionStyleValueForProperty=function(t,n,o=""){let r=null;const s=t.getNodes(),l=t.anchor,i=t.focus,c=t.isBackward(),d=c?i.getNode():l.getNode(),f=c?l.getNode():i.getNode(),a=c?i.offset:l.offset,g=c?l.offset:i.offset;if(e.$isRangeSelection(t)&&t.isCollapsed()&&""!==t.style){const o=t.style,r=e.getStyleObjectFromCSS(o);if(null!==r&&n in r)return r[n]}for(let t=0;t<s.length;t++){const l=s[t];if((0!==t||!l.is(d)||!e.$isTextNode(l)||a!==l.getTextContentSize())&&((0===t||!l.is(f)||0!==g)&&e.$isTextNode(l))){const e=y(l,n,o);if(null===r)r=e;else if(r!==e){r="";break}}}return null===r?o:r},exports.$isAtNodeEnd=function(n){if("text"===n.type)return n.offset===n.getNode().getTextContentSize();const o=n.getNode();return e.$isElementNode(o)||t(177),n.offset===o.getChildrenSize()},exports.$isParentElementRTL=N,exports.$isParentRTL=function(e){const t=l(e);return null!==t&&"rtl"===t.direction},exports.$moveCaretSelection=m,exports.$moveCharacter=function(e,t,n){const o=N(e);let r;r=h(e)||o?!n:n,m(e,t,r,"character")},exports.$patchStyleText=function(t,n){if(e.$isRangeSelection(t)&&t.isCollapsed()){d(t,n);const o=t.anchor.getNode();e.$isElementNode(o)&&o.isEmpty()&&d(o,n)}f(e=>{d(e,n)});const o=t.getNodes();if(o.length>0){const t=new Set;for(const r of o){if(!e.$isElementNode(r)||!r.canBeEmpty()||0!==r.getChildrenSize())continue;const o=r.getKey();t.has(o)||(t.add(o),d(r,n))}}},exports.$setBlocksType=function(t,n,o=g){if(null===t)return;const r=t.getStartEndPoints(),s=new Map;if(r){const[t,n]=r,o=e.$findMatchingParent(t.getNode(),e.INTERNAL_$isBlock),l=e.$findMatchingParent(n.getNode(),e.INTERNAL_$isBlock);e.$isElementNode(o)&&s.set(o.getKey(),o),e.$isElementNode(l)&&s.set(l.getKey(),l)}for(const n of t.getNodes())if(e.$isElementNode(n)&&e.INTERNAL_$isBlock(n))s.set(n.getKey(),n);else if(null===r){const t=e.$findMatchingParent(n,e.INTERNAL_$isBlock);e.$isElementNode(t)&&s.set(t.getKey(),t)}for(const[,e]of s){const t=n();o(e,t),e.replace(t,!0)}},exports.$shouldOverrideDefaultCharacterSelection=function(t,n){let o=h(t)?!n:n;N(t)&&(o=!o);const r=e.$caretFromPoint(t.focus,o?"previous":"next");if(e.$isExtendableTextPointCaret(r))return!1;for(const t of e.$extendCaretToRange(r)){if(e.$isChildCaret(t))return!t.origin.isInline();if(!e.$isElementNode(t.origin)){if(e.$isDecoratorNode(t.origin))return!0;break}}return!1},exports.$sliceSelectedTextNodeContent=function(t,n,o="self"){const r=t.getStartEndPoints();if(n.isSelected(t)&&!e.$isTokenOrSegmented(n)&&null!==r){const[s,l]=r,i=t.isBackward(),c=s.getNode(),d=l.getNode(),f=n.is(c),a=n.is(d);if(f||a){const[r,s]=e.$getCharacterOffsets(t),l=c.is(d),f=n.is(i?d:c),a=n.is(i?c:d);let g,u=0;if(l)u=r>s?s:r,g=r>s?r:s;else if(f){u=i?s:r,g=void 0}else if(a){u=0,g=i?r:s}const p=n.__text.slice(u,g);p!==n.__text&&("clone"===o&&(n=e.$cloneWithPropertiesEphemeral(n)),n.__text=p)}}return n},exports.$trimTextContentFromAnchor=i,exports.$wrapNodes=function(t,n,o=null){const r=t.getStartEndPoints(),s=r?r[0]:null,l=t.getNodes(),i=l.length;if(null!==s&&(0===i||1===i&&"element"===s.type&&0===s.getNode().getChildrenSize())){const e="text"===s.type?s.getNode().getParentOrThrow():s.getNode(),t=e.getChildren();let r=n();return r.setFormat(e.getFormatType()),r.setIndent(e.getIndent()),t.forEach(e=>r.append(e)),o&&(r=o.append(r)),void e.replace(r)}let c=null,d=[];for(let r=0;r<i;r++){const s=l[r];e.$isRootOrShadowRoot(s)?($(t,d,d.length,n,o),d=[],c=s):null===c||null!==c&&e.$hasAncestor(s,c)?d.push(s):($(t,d,d.length,n,o),d=[s])}$(t,d,d.length,n,o)},exports.createDOMRange=function(t,r,s,l,i){const c=r.getKey(),d=l.getKey(),f=document.createRange();let a=t.getElementByKey(c),g=t.getElementByKey(d),u=s,p=i;if(e.$isTextNode(r)&&(a=n(a)),e.$isTextNode(l)&&(g=n(g)),void 0===r||void 0===l||null===a||null===g)return null;"BR"===a.nodeName&&([a,u]=o(a)),"BR"===g.nodeName&&([g,p]=o(g));const $=a.firstChild;a===g&&null!=$&&"BR"===$.nodeName&&0===u&&0===p&&(p=1);try{f.setStart(a,u),f.setEnd(g,p)}catch(e){return null}return!f.collapsed||u===p&&c===d||(f.setStart(g,p),f.setEnd(a,u)),f},exports.createRectsFromDOMRange=function(e,t){const n=e.getRootElement();if(null===n)return[];const o=n.getBoundingClientRect(),r=getComputedStyle(n),s=parseFloat(r.paddingLeft)+parseFloat(r.paddingRight),l=Array.from(t.getClientRects());let i,c=l.length;l.sort((e,t)=>{const n=e.top-t.top;return Math.abs(n)<=3?e.left-t.left:n});for(let e=0;e<c;e++){const t=l[e],n=i&&i.top<=t.top&&i.top+i.height>t.top&&i.left+i.width>t.left,r=t.width+s===o.width;n||r?(l.splice(e--,1),c--):i=t}return l},exports.getCSSFromStyleObject=r,exports.getStyleObjectFromCSS=x,exports.trimTextContentFromAnchor=T;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import{$isTextNode as e,$getEditor as t,$isRootNode as n,$getSelection as o,$isRangeSelection as l,$caretRangeFromSelection as r,$isTokenOrSegmented as i,$isElementNode as s,$getCharacterOffsets as c,$cloneWithPropertiesEphemeral as f,$getNodeByKey as u,$getPreviousSelection as g,$createTextNode as d,getStyleObjectFromCSS as a,$findMatchingParent as p,INTERNAL_$isBlock as h,$caretFromPoint as y,$isExtendableTextPointCaret as m,$extendCaretToRange as S,$isChildCaret as x,$isDecoratorNode as T,$isRootOrShadowRoot as C,$hasAncestor as N,$isLeafNode as w,$setSelection as v}from"lexical";export{$cloneWithProperties,$selectAll}from"lexical";function K(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}function P(e){let t=e;for(;null!=t;){if(t.nodeType===Node.TEXT_NODE)return t;t=t.firstChild}return null}function E(e){const t=e.parentNode;if(null==t)throw new Error("Should never happen");return[t,Array.from(t.childNodes).indexOf(e)]}function I(t,n,o,l,r){const i=n.getKey(),s=l.getKey(),c=document.createRange();let f=t.getElementByKey(i),u=t.getElementByKey(s),g=o,d=r;if(e(n)&&(f=P(f)),e(l)&&(u=P(u)),void 0===n||void 0===l||null===f||null===u)return null;"BR"===f.nodeName&&([f,g]=E(f)),"BR"===u.nodeName&&([u,d]=E(u));const a=f.firstChild;f===u&&null!=a&&"BR"===a.nodeName&&0===g&&0===d&&(d=1);try{c.setStart(f,g),c.setEnd(u,d)}catch(e){return null}return!c.collapsed||g===d&&i===s||(c.setStart(u,d),c.setEnd(f,g)),c}function B(e,t){const n=e.getRootElement();if(null===n)return[];const o=n.getBoundingClientRect(),l=getComputedStyle(n),r=parseFloat(l.paddingLeft)+parseFloat(l.paddingRight),i=Array.from(t.getClientRects());let s,c=i.length;i.sort((e,t)=>{const n=e.top-t.top;return Math.abs(n)<=3?e.left-t.left:n});for(let e=0;e<c;e++){const t=i[e],n=s&&s.top<=t.top&&s.top+s.height>t.top&&s.left+s.width>t.left,l=t.width+r===o.width;n||l?(i.splice(e--,1),c--):s=t}return i}function F(e){let t="";for(const n in e)n&&(t+=`${n}: ${e[n]};`);return t}function k(e){const n=t().getElementByKey(e.getKey());if(null===n)return null;const o=n.ownerDocument.defaultView;return null===o?null:o.getComputedStyle(n)}function b(e){return k(n(e)?e:e.getParentOrThrow())}function z(e){const t=b(e);return null!==t&&"rtl"===t.direction}function O(e,t,n="self"){const o=e.getStartEndPoints();if(t.isSelected(e)&&!i(t)&&null!==o){const[l,r]=o,i=e.isBackward(),s=l.getNode(),u=r.getNode(),g=t.is(s),d=t.is(u);if(g||d){const[o,l]=c(e),r=s.is(u),g=t.is(i?u:s),d=t.is(i?s:u);let a,p=0;if(r)p=o>l?l:o,a=o>l?o:l;else if(g){p=i?l:o,a=void 0}else if(d){p=0,a=i?o:l}const h=t.__text.slice(p,a);h!==t.__text&&("clone"===n&&(t=f(t)),t.__text=h)}}return t}function R(e){if("text"===e.type)return e.offset===e.getNode().getTextContentSize();const t=e.getNode();return s(t)||K(177),e.offset===t.getChildrenSize()}function A(t,o,r){let i=o.getNode(),c=r;if(s(i)){const e=i.getDescendantByIndex(o.offset);null!==e&&(i=e)}for(;c>0&&null!==i;){if(s(i)){const e=i.getLastDescendant();null!==e&&(i=e)}let r=i.getPreviousSibling(),f=0;if(null===r){let e=i.getParentOrThrow(),t=e.getPreviousSibling();for(;null===t;){if(e=e.getParent(),null===e){r=null;break}t=e.getPreviousSibling()}null!==e&&(f=e.isInline()?0:2,r=t)}let a=i.getTextContent();""===a&&s(i)&&!i.isInline()&&(a="\n\n");const p=a.length;if(!e(i)||c>=p){const e=i.getParent();i.remove(),null==e||0!==e.getChildrenSize()||n(e)||e.remove(),c-=p+f,i=r}else{const n=i.getKey(),r=t.getEditorState().read(()=>{const t=u(n);return e(t)&&t.isSimpleText()?t.getTextContent():null}),s=p-c,f=a.slice(0,s);if(null!==r&&r!==a){const e=g();let t=i;if(i.isSimpleText())i.setTextContent(r);else{const e=d(r);i.replace(e),t=e}if(l(e)&&e.isCollapsed()){const n=e.anchor.offset;t.select(n,n)}}else if(i.isSimpleText()){const e=o.key===n;let t=o.offset;t<c&&(t=p);const l=e?t-c:0,r=e?t:s;if(e&&0===l){const[e]=i.splitText(l,r);e.remove()}else{const[,e]=i.splitText(l,r);e.remove()}}else{const e=d(f);i.replace(e)}c=0}}}const _=()=>{};function L(t,n){(l(t)?t.isCollapsed():e(t)||s(t))||K(280);const o=a(l(t)?t.style:e(t)?t.getStyle():t.getTextStyle()),r=F(Object.entries(n).reduce((e,[n,l])=>("function"==typeof l?e[n]=l(o[n],t):null===l?delete e[n]:e[n]=l,e),{...o}));l(t)||e(t)?t.setStyle(r):t.setTextStyle(r)}function M(e,t){if(l(e)&&e.isCollapsed()){L(e,t);const n=e.anchor.getNode();s(n)&&n.isEmpty()&&L(n,t)}$(e=>{L(e,t)});const n=e.getNodes();if(n.length>0){const e=new Set;for(const o of n){if(!s(o)||!o.canBeEmpty()||0!==o.getChildrenSize())continue;const n=o.getKey();e.has(n)||(e.add(n),L(o,t))}}}function $(t){const n=o();if(!n)return;const s=new Map,c=e=>s.get(e.getKey())||[0,e.getTextContentSize()];if(l(n))for(const e of r(n).getTextSlices())e&&s.set(e.caret.origin.getKey(),e.getSliceIndices());const f=n.getNodes();for(const n of f){if(!e(n)||!n.canHaveFormat())continue;const[o,l]=c(n);if(l!==o)if(i(n)||0===o&&l===n.getTextContentSize())t(n);else{t(n.splitText(o,l)[0===o?0:1])}}l(n)&&"text"===n.anchor.type&&"text"===n.focus.type&&n.anchor.key===n.focus.key&&D(n)}function D(e){if(e.isBackward()){const{anchor:t,focus:n}=e,{key:o,offset:l,type:r}=t;t.set(n.key,n.offset,n.type),n.set(o,l,r)}}function j(e,t){const n=e.getFormatType(),o=e.getIndent();n!==t.getFormatType()&&t.setFormat(n),o!==t.getIndent()&&t.setIndent(o)}function U(e,t,n=j){if(null===e)return;const o=e.getStartEndPoints(),l=new Map;if(o){const[e,t]=o,n=p(e.getNode(),h),r=p(t.getNode(),h);s(n)&&l.set(n.getKey(),n),s(r)&&l.set(r.getKey(),r)}for(const t of e.getNodes())if(s(t)&&h(t))l.set(t.getKey(),t);else if(null===o){const e=p(t,h);s(e)&&l.set(e.getKey(),e)}for(const[,e]of l){const o=t();n(e,o),e.replace(o,!0)}}function H(e){return e.getNode().isAttached()}function V(e){let t=e;for(;null!==t&&!C(t);){const e=t.getLatest(),n=t.getParent();0===e.getChildrenSize()&&t.remove(!0),t=n}}function W(e,t,n=null){const o=e.getStartEndPoints(),l=o?o[0]:null,r=e.getNodes(),i=r.length;if(null!==l&&(0===i||1===i&&"element"===l.type&&0===l.getNode().getChildrenSize())){const e="text"===l.type?l.getNode().getParentOrThrow():l.getNode(),o=e.getChildren();let r=t();return r.setFormat(e.getFormatType()),r.setIndent(e.getIndent()),o.forEach(e=>r.append(e)),n&&(r=n.append(r)),void e.replace(r)}let s=null,c=[];for(let o=0;o<i;o++){const l=r[o];C(l)?(X(e,c,c.length,t,n),c=[],s=l):null===s||null!==s&&N(l,s)?c.push(l):(X(e,c,c.length,t,n),c=[l])}X(e,c,c.length,t,n)}function X(e,t,n,o,r=null){if(0===t.length)return;const i=t[0],c=new Map,f=[];let u=s(i)?i:i.getParentOrThrow();u.isInline()&&(u=u.getParentOrThrow());let d=!1;for(;null!==u;){const e=u.getPreviousSibling();if(null!==e){u=e,d=!0;break}if(u=u.getParentOrThrow(),C(u))break}const a=new Set;for(let e=0;e<n;e++){const n=t[e];s(n)&&0===n.getChildrenSize()&&a.add(n.getKey())}const p=new Set;for(let e=0;e<n;e++){const n=t[e];let l=n.getParent();if(null!==l&&l.isInline()&&(l=l.getParent()),null!==l&&w(n)&&!p.has(n.getKey())){const e=l.getKey();if(void 0===c.get(e)){const t=o();t.setFormat(l.getFormatType()),t.setIndent(l.getIndent()),f.push(t),c.set(e,t);const n=l.getChildren();t.splice(t.getChildrenSize(),0,n);for(const e of n)if(p.add(e.getKey()),s(e))for(const t of e.getChildrenKeys())p.add(t);V(l)}}else if(a.has(n.getKey())){s(n)||K(179);const e=o();e.setFormat(n.getFormatType()),e.setIndent(n.getIndent()),f.push(e),n.remove(!0)}}if(null!==r)for(let e=0;e<f.length;e++){const t=f[e];r.append(t)}let h=null;if(C(u))if(d)if(null!==r)u.insertAfter(r);else for(let e=f.length-1;e>=0;e--){const t=f[e];u.insertAfter(t)}else{const e=u.getFirstChild();if(s(e)&&(u=e),null===e)if(r)u.append(r);else for(let e=0;e<f.length;e++){const t=f[e];u.append(t),h=t}else if(null!==r)e.insertBefore(r);else for(let t=0;t<f.length;t++){const n=f[t];e.insertBefore(n),h=n}}else if(r)u.insertAfter(r);else for(let e=f.length-1;e>=0;e--){const t=f[e];u.insertAfter(t),h=t}const y=g();l(y)&&H(y.anchor)&&H(y.focus)?v(y.clone()):null!==h?h.selectEnd():e.dirty=!0}function q(e){const t=G(e);return null!==t&&"vertical-rl"===t.writingMode}function G(e){const t=e.anchor.getNode();return s(t)?k(t):b(t)}function J(e,t){let n=q(e)?!t:t;Y(e)&&(n=!n);const o=y(e.focus,n?"previous":"next");if(m(o))return!1;for(const e of S(o)){if(x(e))return!e.origin.isInline();if(!s(e.origin)){if(T(e.origin))return!0;break}}return!1}function Q(e,t,n,o){e.modify(t?"extend":"move",n,o)}function Y(e){const t=G(e);return null!==t&&"rtl"===t.direction}function Z(e,t,n){const o=Y(e);let l;l=q(e)||o?!n:n,Q(e,t,l,"character")}function ee(e,t,n){const o=e.getStyle(),l=a(o);return null!==l&&l[t]||n}function te(t,n,o=""){let r=null;const i=t.getNodes(),s=t.anchor,c=t.focus,f=t.isBackward(),u=f?c.getNode():s.getNode(),g=f?s.getNode():c.getNode(),d=f?c.offset:s.offset,p=f?s.offset:c.offset;if(l(t)&&t.isCollapsed()&&""!==t.style){const e=t.style,o=a(e);if(null!==o&&n in o)return o[n]}for(let t=0;t<i.length;t++){const l=i[t];if((0!==t||!l.is(u)||!e(l)||d!==l.getTextContentSize())&&((0===t||!l.is(g)||0!==p)&&e(l))){const e=ee(l,n,o);if(null===r)r=e;else if(r!==e){r="";break}}}return null===r?o:r}const ne=a,oe=A;export{_ as $addNodeStyle,j as $copyBlockFormatIndent,D as $ensureForwardRangeSelection,$ as $forEachSelectedTextNode,k as $getComputedStyleForElement,b as $getComputedStyleForParent,te as $getSelectionStyleValueForProperty,R as $isAtNodeEnd,Y as $isParentElementRTL,z as $isParentRTL,Q as $moveCaretSelection,Z as $moveCharacter,M as $patchStyleText,U as $setBlocksType,J as $shouldOverrideDefaultCharacterSelection,O as $sliceSelectedTextNodeContent,A as $trimTextContentFromAnchor,W as $wrapNodes,I as createDOMRange,B as createRectsFromDOMRange,F as getCSSFromStyleObject,ne as getStyleObjectFromCSS,oe as trimTextContentFromAnchor};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|