@lexical/selection 0.2.4 → 0.2.5
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 +15 -28
- package/LexicalSelection.prod.js +15 -16
- package/package.json +2 -2
package/LexicalSelection.dev.js
CHANGED
|
@@ -30,10 +30,12 @@ function $cloneWithProperties(node) {
|
|
|
30
30
|
clone.__indent = latest.__indent;
|
|
31
31
|
clone.__dir = latest.__dir;
|
|
32
32
|
} else if (lexical.$isTextNode(latest) && lexical.$isTextNode(clone)) {
|
|
33
|
+
const marks = latest.__marks;
|
|
33
34
|
clone.__format = latest.__format;
|
|
34
35
|
clone.__style = latest.__style;
|
|
35
36
|
clone.__mode = latest.__mode;
|
|
36
37
|
clone.__detail = latest.__detail;
|
|
38
|
+
clone.__marks = marks === null ? null : Array.from(marks);
|
|
37
39
|
} // $FlowFixMe
|
|
38
40
|
|
|
39
41
|
|
|
@@ -60,7 +62,7 @@ function $getParentAvoidingExcludedElements(node) {
|
|
|
60
62
|
return parent;
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
function $copyLeafNodeBranchToRoot(leaf, startingOffset, isLeftSide, range, nodeMap) {
|
|
65
|
+
function $copyLeafNodeBranchToRoot(leaf, startingOffset, endingOffset, isLeftSide, range, nodeMap) {
|
|
64
66
|
let node = leaf;
|
|
65
67
|
let offset = startingOffset;
|
|
66
68
|
|
|
@@ -82,9 +84,9 @@ function $copyLeafNodeBranchToRoot(leaf, startingOffset, isLeftSide, range, node
|
|
|
82
84
|
}
|
|
83
85
|
|
|
84
86
|
if (lexical.$isTextNode(clone) && !clone.isSegmented() && !clone.isToken()) {
|
|
85
|
-
clone.__text = clone.__text.slice(isLeftSide ? offset : 0, isLeftSide ?
|
|
87
|
+
clone.__text = clone.__text.slice(isLeftSide ? offset : 0, isLeftSide ? endingOffset : offset);
|
|
86
88
|
} else if (lexical.$isElementNode(clone)) {
|
|
87
|
-
clone.__children = clone.__children.slice(isLeftSide ? offset : 0, isLeftSide ? undefined : offset + 1);
|
|
89
|
+
clone.__children = clone.__children.slice(isLeftSide ? offset : 0, isLeftSide ? undefined : (offset || 0) + 1);
|
|
88
90
|
}
|
|
89
91
|
|
|
90
92
|
if (lexical.$isRootNode(parent)) {
|
|
@@ -140,25 +142,7 @@ function $cloneContentsImpl(selection) {
|
|
|
140
142
|
if (lexical.$isRangeSelection(selection)) {
|
|
141
143
|
const anchor = selection.anchor;
|
|
142
144
|
const focus = selection.focus;
|
|
143
|
-
const anchorOffset =
|
|
144
|
-
const focusOffset = focus.getCharacterOffset();
|
|
145
|
-
const anchorNode = anchor.getNode();
|
|
146
|
-
const focusNode = focus.getNode();
|
|
147
|
-
const anchorNodeParent = anchorNode.getParentOrThrow(); // Handle a single text node extraction
|
|
148
|
-
|
|
149
|
-
if (anchorNode === focusNode && lexical.$isTextNode(anchorNode) && (anchorNodeParent.canBeEmpty() || anchorNodeParent.getChildrenSize() > 1)) {
|
|
150
|
-
const clonedFirstNode = $cloneWithProperties(anchorNode);
|
|
151
|
-
const isBefore = focusOffset > anchorOffset;
|
|
152
|
-
const startOffset = isBefore ? anchorOffset : focusOffset;
|
|
153
|
-
const endOffset = isBefore ? focusOffset : anchorOffset;
|
|
154
|
-
clonedFirstNode.__text = clonedFirstNode.__text.slice(startOffset, endOffset);
|
|
155
|
-
const key = clonedFirstNode.getKey();
|
|
156
|
-
return {
|
|
157
|
-
nodeMap: [[key, clonedFirstNode]],
|
|
158
|
-
range: [key]
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
|
|
145
|
+
const [anchorOffset, focusOffset] = selection.getCharacterOffsets();
|
|
162
146
|
const nodes = selection.getNodes();
|
|
163
147
|
|
|
164
148
|
if (nodes.length === 0) {
|
|
@@ -199,9 +183,10 @@ function $cloneContentsImpl(selection) {
|
|
|
199
183
|
const lastNode = nodes[nodesLength - 1];
|
|
200
184
|
const isBefore = anchor.isBefore(focus);
|
|
201
185
|
const nodeMap = new Map();
|
|
202
|
-
const range = [];
|
|
186
|
+
const range = [];
|
|
187
|
+
const isOnlyText = lexical.$isTextNode(firstNode) && nodesLength === 1; // Do first node to root
|
|
203
188
|
|
|
204
|
-
$copyLeafNodeBranchToRoot(firstNode, isBefore ? anchorOffset : focusOffset, true, range, nodeMap); // Copy all nodes between
|
|
189
|
+
$copyLeafNodeBranchToRoot(firstNode, isBefore ? anchorOffset : focusOffset, isOnlyText ? isBefore ? focusOffset : anchorOffset : undefined, true, range, nodeMap); // Copy all nodes between
|
|
205
190
|
|
|
206
191
|
for (let i = 0; i < nodesLength; i++) {
|
|
207
192
|
const node = nodes[i];
|
|
@@ -214,12 +199,14 @@ function $cloneContentsImpl(selection) {
|
|
|
214
199
|
range.push(node.getKey());
|
|
215
200
|
}
|
|
216
201
|
|
|
217
|
-
|
|
202
|
+
if (key !== 'root') {
|
|
203
|
+
nodeMap.set(key, clone);
|
|
204
|
+
}
|
|
218
205
|
}
|
|
219
206
|
} // Do last node to root
|
|
220
207
|
|
|
221
208
|
|
|
222
|
-
$copyLeafNodeBranchToRoot(lastNode, isBefore ? focusOffset : anchorOffset, false, range, nodeMap);
|
|
209
|
+
$copyLeafNodeBranchToRoot(lastNode, isOnlyText ? undefined : isBefore ? focusOffset : anchorOffset, undefined, false, range, nodeMap);
|
|
223
210
|
return {
|
|
224
211
|
nodeMap: Array.from(nodeMap.entries()),
|
|
225
212
|
range
|
|
@@ -469,9 +456,9 @@ function $removeParentEmptyElements(startingNode) {
|
|
|
469
456
|
function $wrapLeafNodesInElements(selection, createElement, wrappingElement) {
|
|
470
457
|
const nodes = selection.getNodes();
|
|
471
458
|
const nodesLength = nodes.length;
|
|
459
|
+
const anchor = selection.anchor;
|
|
472
460
|
|
|
473
|
-
if (nodesLength === 0) {
|
|
474
|
-
const anchor = selection.anchor;
|
|
461
|
+
if (nodesLength === 0 || nodesLength === 1 && anchor.type === 'element' && anchor.getNode().getChildrenSize() === 0) {
|
|
475
462
|
const target = anchor.type === 'text' ? anchor.getNode().getParentOrThrow() : anchor.getNode();
|
|
476
463
|
const children = target.getChildren();
|
|
477
464
|
let element = createElement();
|
package/LexicalSelection.prod.js
CHANGED
|
@@ -4,19 +4,18 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
var
|
|
8
|
-
function w(a,c,b,
|
|
9
|
-
|
|
10
|
-
function x(a){if(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
function
|
|
14
|
-
exports.$
|
|
15
|
-
exports.$
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
exports.$
|
|
19
|
-
exports.$
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
l.$isElementNode(c)&&(d=c),null===c)if(b)d.append(b);else for(b=0;b<e.length;b++)d.append(e[b]);else if(b)c.insertBefore(b);else for(b=0;b<e.length;b++)c.insertBefore(e[b]);else if(b)d.insertAfter(b);else for(b=e.length-1;0<=b;b--)d.insertAfter(e[b]);b=l.$getPreviousSelection();l.$isRangeSelection(b)&&b.anchor.getNode().isAttached()&&b.focus.getNode().isAttached()?(a=b.clone(),a.dirty=!0,l.$setSelection(a)):a.dirty=!0}};exports.getStyleObjectFromCSS=y;
|
|
7
|
+
var k=require("lexical");const t=new Map;function v(a){a=a.getLatest();const c=a.constructor.clone(a);c.__parent=a.__parent;if(k.$isElementNode(a)&&k.$isElementNode(c))c.__children=Array.from(a.__children),c.__format=a.__format,c.__indent=a.__indent,c.__dir=a.__dir;else if(k.$isTextNode(a)&&k.$isTextNode(c)){const b=a.__marks;c.__format=a.__format;c.__style=a.__style;c.__mode=a.__mode;c.__detail=a.__detail;c.__marks=null===b?null:Array.from(b)}return c}
|
|
8
|
+
function w(a,c,b,e,l,f){for(var d=c;null!==a;){for(c=a.getParent();null!==c&&c.excludeFromCopy();)c=c.getParent();if(null===c)break;if(!k.$isElementNode(a)||!a.excludeFromCopy()){const h=a.getKey();let g=f.get(h);const m=void 0===g;m&&(g=v(a),f.set(h,g));!k.$isTextNode(g)||g.isSegmented()||g.isToken()?k.$isElementNode(g)&&(g.__children=g.__children.slice(e?d:0,e?void 0:(d||0)+1)):g.__text=g.__text.slice(e?d:0,e?b:d);if(k.$isRootNode(c)){m&&l.push(h);break}}d=f.get(c.getKey());d=k.$isElementNode(d)?
|
|
9
|
+
d.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=c}}
|
|
10
|
+
function x(a){if(k.$isRangeSelection(a)){var c=a.anchor,b=a.focus;const [g,m]=a.getCharacterOffsets();a=a.getNodes();if(0===a.length)return{nodeMap:[],range:[]};let n=a.length;var e=a[0],l=e.getParent();if(null!==l&&(!l.canBeEmpty()||k.$isRootNode(l))){var f=l.__children;if(f.length===n){var d=!0;for(var h=0;h<f.length;h++)if(f[h]!==a[h].__key){d=!1;break}d&&(n++,a.push(l))}}l=a[n-1];c=c.isBefore(b);b=new Map;f=[];d=k.$isTextNode(e)&&1===n;w(e,c?g:m,d?c?m:g:void 0,!0,f,b);for(e=0;e<n;e++){h=a[e];
|
|
11
|
+
const r=h.getKey();if(!(b.has(r)||k.$isElementNode(h)&&h.excludeFromCopy())){const p=v(h);k.$isRootNode(h.getParent())&&f.push(h.getKey());"root"!==r&&b.set(r,p)}}w(l,d?void 0:c?m:g,void 0,!1,f,b);return{nodeMap:Array.from(b.entries()),range:f}}if(k.$isGridSelection(a))return{nodeMap:a.getNodes().map(g=>{const m=g.getKey();g=v(g);return[m,g]}),range:[a.gridKey]};throw Error("Minified Lexical error #68; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");
|
|
12
|
+
}function y(a){return t.get(a)||null}function z(a,c){var b=y(a.getStyle());c=b?{...b,...c}:c;b="";for(e in c)e&&(b+=`${e}: ${c[e]};`);var e=b;a.setStyle(e);t.set(e,c)}function A(a,c,b,e){a.modify(c?"extend":"move",b,e)}function B(a){a=a.anchor.getNode();return"rtl"===(k.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}function C(a){for(;null!==a&&!k.$isRootNode(a);){const c=a.getLatest(),b=a.getParent();0===c.__children.length&&a.remove(!0);a=b}}exports.$cloneContents=function(a){return x(a)};
|
|
13
|
+
exports.$getSelectionStyleValueForProperty=function(a,c,b=""){let e=null;const l=a.getNodes();var f=a.anchor,d=a.focus,h=a.isBackward();a=h?d.offset:f.offset;f=h?d.getNode():f.getNode();for(d=0;d<l.length;d++){var g=l[d];if((0===d||0!==a||!g.is(f))&&k.$isTextNode(g)){h=c;var m=b;g=g.getStyle();g=y(g);h=null!==g?g[h]||m:m;if(null===e)e=h;else if(e!==h){e="";break}}}return null===e?b:e};exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};
|
|
14
|
+
exports.$isParentElementRTL=B;exports.$moveCaretSelection=A;exports.$moveCharacter=function(a,c,b){const e=B(a);A(a,c,b?!e:e,"character")};
|
|
15
|
+
exports.$patchStyleText=function(a,c){var b=a.getNodes();const e=b.length-1;let l=b[0],f=b[e];if(!a.isCollapsed()){var d=a.anchor,h=a.focus;a=l.getTextContent().length;var g=h.offset,m=d.offset;d=(h=d.isBefore(h))?m:g;h=h?g:m;if(d===l.getTextContentSize()){const n=l.getNextSibling();k.$isTextNode(n)&&(d=m=0,l=n)}if(l.is(f))k.$isTextNode(l)&&(d=m>g?g:m,h=m>g?m:g,d!==h&&(0===d&&h===a?(z(l,c),l.select(d,h)):(b=l.splitText(d,h),b=0===d?b[0]:b[1],z(b,c),b.select(0,h-d))));else for(k.$isTextNode(l)&&(0!==
|
|
16
|
+
d&&([,l]=l.splitText(d)),z(l,c)),k.$isTextNode(f)&&(a=f.getTextContent().length,h!==a&&([f]=f.splitText(h)),0!==h&&z(f,c)),a=1;a<e;a++)g=b[a],m=g.getKey(),k.$isTextNode(g)&&m!==l.getKey()&&m!==f.getKey()&&!g.isToken()&&z(g,c)}};
|
|
17
|
+
exports.$selectAll=function(a){const c=a.anchor;a=a.focus;var b=c.getNode().getTopLevelElementOrThrow().getParentOrThrow();let e=b.getFirstDescendant();b=b.getLastDescendant();let l="element",f="element",d=0;k.$isTextNode(e)?l="text":k.$isElementNode(e)||null===e||(e=e.getParentOrThrow());k.$isTextNode(b)?(f="text",d=b.getTextContentSize()):k.$isElementNode(b)||null===b||(b=b.getParentOrThrow(),d=b.getChildrenSize());e&&b&&(c.set(e.getKey(),0,l),a.set(b.getKey(),d,f))};
|
|
18
|
+
exports.$shouldOverrideDefaultCharacterSelection=function(a,c){a=k.$getDecoratorNode(a.focus,c);return k.$isDecoratorNode(a)&&!a.isIsolated()};
|
|
19
|
+
exports.$wrapLeafNodesInElements=function(a,c,b){const e=a.getNodes(),l=e.length;var f=a.anchor;if(0===l||1===l&&"element"===f.type&&0===f.getNode().getChildrenSize()){a="text"===f.type?f.getNode().getParentOrThrow():f.getNode();f=a.getChildren();let q=c();f.forEach(u=>q.append(u));b&&(q=b.append(q));a.replace(q)}else{var d=e[0],h=new Map;f=[];d=k.$isElementNode(d)?d:d.getParentOrThrow();for(d.isInline()&&(d=d.getParentOrThrow());null!==d;){var g=d.getPreviousSibling();if(null!==g){d=g;break}d=d.getParentOrThrow();
|
|
20
|
+
if(k.$isRootNode(d))break}g=new Set;for(var m=0;m<l;m++){var n=e[m];k.$isElementNode(n)&&0===n.getChildrenSize()&&g.add(n.getKey())}var r=new Set;for(m=0;m<l;m++){var p=e[m];n=p.getParent();null!==n&&n.isInline()&&(n=n.getParent());if(null!==n&&k.$isLeafNode(p)&&!r.has(p.getKey())){if(p=n.getKey(),void 0===h.get(p)){const q=c();f.push(q);h.set(p,q);n.getChildren().forEach(u=>{q.append(u);r.add(u.getKey())});C(n)}}else g.has(p.getKey())&&(f.push(c()),p.remove())}if(b)for(c=0;c<f.length;c++)b.append(f[c]);
|
|
21
|
+
if(k.$isRootNode(d))if(c=d.getFirstChild(),k.$isElementNode(c)&&(d=c),null===c)if(b)d.append(b);else for(b=0;b<f.length;b++)d.append(f[b]);else if(b)c.insertBefore(b);else for(b=0;b<f.length;b++)c.insertBefore(f[b]);else if(b)d.insertAfter(b);else for(b=f.length-1;0<=b;b--)d.insertAfter(f[b]);b=k.$getPreviousSelection();k.$isRangeSelection(b)&&b.anchor.getNode().isAttached()&&b.focus.getNode().isAttached()?(a=b.clone(),a.dirty=!0,k.$setSelection(a)):a.dirty=!0}};exports.getStyleObjectFromCSS=y;
|
package/package.json
CHANGED