@lexical/selection 0.4.0 → 0.5.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 +88 -27
- package/LexicalSelection.js.flow +2 -2
- package/LexicalSelection.prod.js +25 -23
- package/README.md +3 -3
- package/index.d.ts +14 -3
- package/package.json +2 -2
package/LexicalSelection.dev.js
CHANGED
|
@@ -201,7 +201,7 @@ function $cloneContentsImpl(selection) {
|
|
|
201
201
|
nodeMap: Array.from(nodeMap.entries()),
|
|
202
202
|
range
|
|
203
203
|
};
|
|
204
|
-
} else if (lexical
|
|
204
|
+
} else if (lexical.DEPRECATED_$isGridSelection(selection)) {
|
|
205
205
|
const nodeMap = selection.getNodes().map(node => {
|
|
206
206
|
const nodeKey = node.getKey();
|
|
207
207
|
const clone = $cloneWithProperties(node);
|
|
@@ -219,7 +219,14 @@ function $cloneContentsImpl(selection) {
|
|
|
219
219
|
}
|
|
220
220
|
|
|
221
221
|
function getStyleObjectFromCSS(css) {
|
|
222
|
-
|
|
222
|
+
let value = cssToStyles.get(css);
|
|
223
|
+
|
|
224
|
+
if (value === undefined) {
|
|
225
|
+
value = getStyleObjectFromRawCSS(css);
|
|
226
|
+
cssToStyles.set(css, value);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return value;
|
|
223
230
|
}
|
|
224
231
|
|
|
225
232
|
function getStyleObjectFromRawCSS(css) {
|
|
@@ -451,7 +458,7 @@ function $selectAll(selection) {
|
|
|
451
458
|
function $removeParentEmptyElements(startingNode) {
|
|
452
459
|
let node = startingNode;
|
|
453
460
|
|
|
454
|
-
while (node !== null && !lexical.$
|
|
461
|
+
while (node !== null && !lexical.$isRootOrShadowRoot(node)) {
|
|
455
462
|
const latest = node.getLatest();
|
|
456
463
|
const parentNode = node.getParent();
|
|
457
464
|
|
|
@@ -462,8 +469,19 @@ function $removeParentEmptyElements(startingNode) {
|
|
|
462
469
|
node = parentNode;
|
|
463
470
|
}
|
|
464
471
|
}
|
|
472
|
+
/**
|
|
473
|
+
* Attempts to wrap all nodes in the Selection in ElementNodes returned from createElement.
|
|
474
|
+
* If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement.
|
|
475
|
+
* It attempts to append the resulting sub-tree to the nearest safe insertion target.
|
|
476
|
+
*
|
|
477
|
+
* @param selection
|
|
478
|
+
* @param createElement
|
|
479
|
+
* @param wrappingElement
|
|
480
|
+
* @returns
|
|
481
|
+
*/
|
|
465
482
|
|
|
466
|
-
|
|
483
|
+
|
|
484
|
+
function $wrapNodes(selection, createElement, wrappingElement = null) {
|
|
467
485
|
const nodes = selection.getNodes();
|
|
468
486
|
const nodesLength = nodes.length;
|
|
469
487
|
const anchor = selection.anchor;
|
|
@@ -484,6 +502,34 @@ function $wrapLeafNodesInElements(selection, createElement, wrappingElement) {
|
|
|
484
502
|
return;
|
|
485
503
|
}
|
|
486
504
|
|
|
505
|
+
let topLevelNode = null;
|
|
506
|
+
let descendants = [];
|
|
507
|
+
|
|
508
|
+
for (let i = 0; i < nodesLength; i++) {
|
|
509
|
+
const node = nodes[i]; // Determine whether wrapping has to be broken down into multiple chunks. This can happen if the
|
|
510
|
+
// user selected multiple Root-like nodes that have to be treated separately as if they are
|
|
511
|
+
// their own branch. I.e. you don't want to wrap a whole table, but rather the contents of each
|
|
512
|
+
// of each of the cell nodes.
|
|
513
|
+
|
|
514
|
+
if (lexical.$isRootOrShadowRoot(node)) {
|
|
515
|
+
$wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
|
|
516
|
+
descendants = [];
|
|
517
|
+
topLevelNode = node;
|
|
518
|
+
} else if (topLevelNode === null || topLevelNode !== null && lexical.$hasAncestor(node, topLevelNode)) {
|
|
519
|
+
descendants.push(node);
|
|
520
|
+
} else {
|
|
521
|
+
$wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
|
|
522
|
+
descendants = [node];
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
$wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
|
|
527
|
+
}
|
|
528
|
+
function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingElement = null) {
|
|
529
|
+
if (nodes.length === 0) {
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
|
|
487
533
|
const firstNode = nodes[0];
|
|
488
534
|
const elementMapping = new Map();
|
|
489
535
|
const elements = []; // The below logic is to find the right target for us to
|
|
@@ -497,17 +543,20 @@ function $wrapLeafNodesInElements(selection, createElement, wrappingElement) {
|
|
|
497
543
|
target = target.getParentOrThrow();
|
|
498
544
|
}
|
|
499
545
|
|
|
546
|
+
let targetIsPrevSibling = false;
|
|
547
|
+
|
|
500
548
|
while (target !== null) {
|
|
501
549
|
const prevSibling = target.getPreviousSibling();
|
|
502
550
|
|
|
503
551
|
if (prevSibling !== null) {
|
|
504
552
|
target = prevSibling;
|
|
553
|
+
targetIsPrevSibling = true;
|
|
505
554
|
break;
|
|
506
555
|
}
|
|
507
556
|
|
|
508
557
|
target = target.getParentOrThrow();
|
|
509
558
|
|
|
510
|
-
if (lexical.$
|
|
559
|
+
if (lexical.$isRootOrShadowRoot(target)) {
|
|
511
560
|
break;
|
|
512
561
|
}
|
|
513
562
|
}
|
|
@@ -556,42 +605,53 @@ function $wrapLeafNodesInElements(selection, createElement, wrappingElement) {
|
|
|
556
605
|
targetElement.setFormat(node.getFormatType());
|
|
557
606
|
targetElement.setIndent(node.getIndent());
|
|
558
607
|
elements.push(targetElement);
|
|
559
|
-
node.remove();
|
|
608
|
+
node.remove(true);
|
|
560
609
|
}
|
|
561
610
|
}
|
|
562
611
|
|
|
563
|
-
if (wrappingElement) {
|
|
612
|
+
if (wrappingElement !== null) {
|
|
564
613
|
for (let i = 0; i < elements.length; i++) {
|
|
565
614
|
const element = elements[i];
|
|
566
615
|
wrappingElement.append(element);
|
|
567
616
|
}
|
|
568
|
-
} // If our target is
|
|
617
|
+
} // If our target is Root-like, let's see if we can re-adjust
|
|
569
618
|
// so that the target is the first child instead.
|
|
570
619
|
|
|
571
620
|
|
|
572
|
-
if (lexical.$
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
target = firstChild;
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
if (firstChild === null) {
|
|
580
|
-
if (wrappingElement) {
|
|
581
|
-
target.append(wrappingElement);
|
|
621
|
+
if (lexical.$isRootOrShadowRoot(target)) {
|
|
622
|
+
if (targetIsPrevSibling) {
|
|
623
|
+
if (wrappingElement !== null) {
|
|
624
|
+
target.insertAfter(wrappingElement);
|
|
582
625
|
} else {
|
|
583
|
-
for (let i =
|
|
626
|
+
for (let i = elements.length - 1; i >= 0; i--) {
|
|
584
627
|
const element = elements[i];
|
|
585
|
-
target.
|
|
628
|
+
target.insertAfter(element);
|
|
586
629
|
}
|
|
587
630
|
}
|
|
588
631
|
} else {
|
|
589
|
-
|
|
590
|
-
|
|
632
|
+
const firstChild = target.getFirstChild();
|
|
633
|
+
|
|
634
|
+
if (lexical.$isElementNode(firstChild)) {
|
|
635
|
+
target = firstChild;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
if (firstChild === null) {
|
|
639
|
+
if (wrappingElement) {
|
|
640
|
+
target.append(wrappingElement);
|
|
641
|
+
} else {
|
|
642
|
+
for (let i = 0; i < elements.length; i++) {
|
|
643
|
+
const element = elements[i];
|
|
644
|
+
target.append(element);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
591
647
|
} else {
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
648
|
+
if (wrappingElement !== null) {
|
|
649
|
+
firstChild.insertBefore(wrappingElement);
|
|
650
|
+
} else {
|
|
651
|
+
for (let i = 0; i < elements.length; i++) {
|
|
652
|
+
const element = elements[i];
|
|
653
|
+
firstChild.insertBefore(element);
|
|
654
|
+
}
|
|
595
655
|
}
|
|
596
656
|
}
|
|
597
657
|
}
|
|
@@ -864,7 +924,7 @@ function trimTextContentFromAnchor(editor, anchor, delCount) {
|
|
|
864
924
|
}
|
|
865
925
|
}
|
|
866
926
|
function $sliceSelectedTextNodeContent(selection, textNode) {
|
|
867
|
-
if (textNode.isSelected() && !textNode.isSegmented() && !textNode.isToken() && (lexical.$isRangeSelection(selection) || lexical
|
|
927
|
+
if (textNode.isSelected() && !textNode.isSegmented() && !textNode.isToken() && (lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection))) {
|
|
868
928
|
const anchorNode = selection.anchor.getNode();
|
|
869
929
|
const focusNode = selection.focus.getNode();
|
|
870
930
|
const isAnchor = textNode.is(anchorNode);
|
|
@@ -912,7 +972,8 @@ exports.$patchStyleText = $patchStyleText;
|
|
|
912
972
|
exports.$selectAll = $selectAll;
|
|
913
973
|
exports.$shouldOverrideDefaultCharacterSelection = $shouldOverrideDefaultCharacterSelection;
|
|
914
974
|
exports.$sliceSelectedTextNodeContent = $sliceSelectedTextNodeContent;
|
|
915
|
-
exports.$
|
|
975
|
+
exports.$wrapNodes = $wrapNodes;
|
|
976
|
+
exports.$wrapNodesImpl = $wrapNodesImpl;
|
|
916
977
|
exports.createDOMRange = createDOMRange;
|
|
917
978
|
exports.createRectsFromDOMRange = createRectsFromDOMRange;
|
|
918
979
|
exports.getStyleObjectFromCSS = getStyleObjectFromCSS;
|
package/LexicalSelection.js.flow
CHANGED
|
@@ -25,7 +25,7 @@ declare export function $cloneContents<T: LexicalNode>(
|
|
|
25
25
|
declare export function $cloneWithProperties<T: LexicalNode>(node: T): T;
|
|
26
26
|
declare export function getStyleObjectFromCSS(css: string): {
|
|
27
27
|
[string]: string,
|
|
28
|
-
}
|
|
28
|
+
};
|
|
29
29
|
declare export function $patchStyleText(
|
|
30
30
|
selection: RangeSelection | GridSelection,
|
|
31
31
|
patch: {
|
|
@@ -50,7 +50,7 @@ declare export function $moveCharacter(
|
|
|
50
50
|
isBackward: boolean,
|
|
51
51
|
): void;
|
|
52
52
|
declare export function $selectAll(selection: RangeSelection): void;
|
|
53
|
-
declare export function $
|
|
53
|
+
declare export function $wrapNodes(
|
|
54
54
|
selection: RangeSelection,
|
|
55
55
|
createElement: () => ElementNode,
|
|
56
56
|
wrappingElement?: ElementNode,
|
package/LexicalSelection.prod.js
CHANGED
|
@@ -4,26 +4,28 @@
|
|
|
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
|
-
'use strict';var l=require("lexical");let
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
-
function x(a){if(l.$isRangeSelection(a)){var
|
|
11
|
-
|
|
12
|
-
}function y(a){
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
exports.$
|
|
19
|
-
exports.$
|
|
20
|
-
exports.$
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
exports
|
|
26
|
-
|
|
27
|
-
exports.
|
|
28
|
-
|
|
29
|
-
(g=
|
|
7
|
+
'use strict';var l=require("lexical");let r=new Map;function u(a){a=a.getLatest();let b=a.constructor.clone(a);b.__parent=a.__parent;l.$isElementNode(a)&&l.$isElementNode(b)?(b.__children=Array.from(a.__children),b.__format=a.__format,b.__indent=a.__indent,b.__dir=a.__dir):l.$isTextNode(a)&&l.$isTextNode(b)&&(b.__format=a.__format,b.__style=a.__style,b.__mode=a.__mode,b.__detail=a.__detail);return b}
|
|
8
|
+
function v(a,b,c,g,d,f){for(var h=b;null!==a;){for(b=a.getParent();null!==b&&b.excludeFromCopy("clone");)b=b.getParent();if(null===b)break;if(!l.$isElementNode(a)||!a.excludeFromCopy("clone")){let e=a.getKey(),k=f.get(e),m=void 0===k;m&&(k=u(a),f.set(e,k));!l.$isTextNode(k)||k.isSegmented()||k.isToken()?l.$isElementNode(k)&&(k.__children=k.__children.slice(g?h:0,g?void 0:(h||0)+1)):k.__text=k.__text.slice(g?h:0,g?c:h);if(l.$isRootNode(b)){m&&d.push(e);break}}h=f.get(b.getKey());h=l.$isElementNode(h)?
|
|
9
|
+
h.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=b}}
|
|
10
|
+
function x(a){if(l.$isRangeSelection(a)){var b=a.anchor,c=a.focus;let [k,m]=a.getCharacterOffsets();a=a.getNodes();if(0===a.length)return{nodeMap:[],range:[]};let p=a.length;var g=a[0],d=g.getParent();if(null!==d&&(!d.canBeEmpty()||l.$isRootNode(d))){var f=d.__children;if(f.length===p){var h=!0;for(var e=0;e<f.length;e++)if(f[e]!==a[e].__key){h=!1;break}h&&(p++,a.push(d))}}d=a[p-1];b=b.isBefore(c);c=new Map;f=[];h=l.$isTextNode(g)&&1===p;v(g,b?k:m,h?b?m:k:void 0,!0,f,c);for(g=0;g<p;g++){e=a[g];let n=
|
|
11
|
+
e.getKey();if(!(c.has(n)||l.$isElementNode(e)&&e.excludeFromCopy("clone"))){let w=u(e);l.$isRootNode(e.getParent())&&f.push(e.getKey());"root"!==n&&c.set(n,w)}}v(d,h?void 0:b?m:k,void 0,!1,f,c);return{nodeMap:Array.from(c.entries()),range:f}}if(l.DEPRECATED_$isGridSelection(a))return{nodeMap:a.getNodes().map(k=>{const m=k.getKey();k=u(k);return[m,k]}),range:[a.gridKey]};throw Error("Minified Lexical error #1; visit https://lexical.dev/docs/error?code=1 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");
|
|
12
|
+
}function y(a){let b=r.get(a);void 0===b&&(b=z(a),r.set(a,b));return b}function z(a){let b={};a=a.split(";");for(let c of a)if(""!==c){let [g,d]=c.split(/:([^]+)/);b[g.trim()]=d.trim()}return b}function A(a,b){var c=y(a.getStyle());b=c?{...c,...b}:b;c="";for(g in b)g&&(c+=`${g}: ${b[g]};`);var g=c;a.setStyle(g);r.set(g,b)}function B(a,b,c,g){a.modify(b?"extend":"move",c,g)}function C(a){a=a.anchor.getNode();return"rtl"===(l.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}
|
|
13
|
+
function E(a){for(;null!==a&&!l.$isRootOrShadowRoot(a);){let b=a.getLatest(),c=a.getParent();0===b.__children.length&&a.remove(!0);a=c}}
|
|
14
|
+
function F(a,b,c,g,d=null){if(0!==b.length){var f=b[0],h=new Map,e=[];f=l.$isElementNode(f)?f:f.getParentOrThrow();f.isInline()&&(f=f.getParentOrThrow());for(var k=!1;null!==f;){var m=f.getPreviousSibling();if(null!==m){f=m;k=!0;break}f=f.getParentOrThrow();if(l.$isRootOrShadowRoot(f))break}m=new Set;for(var p=0;p<c;p++){var n=b[p];l.$isElementNode(n)&&0===n.getChildrenSize()&&m.add(n.getKey())}var w=new Set;for(p=0;p<c;p++){n=b[p];var q=n.getParent();null!==q&&q.isInline()&&(q=q.getParent());if(null!==
|
|
15
|
+
q&&l.$isLeafNode(n)&&!w.has(n.getKey())){if(n=q.getKey(),void 0===h.get(n)){let t=g();t.setFormat(q.getFormatType());t.setIndent(q.getIndent());e.push(t);h.set(n,t);q.getChildren().forEach(D=>{t.append(D);w.add(D.getKey())});E(q)}}else m.has(n.getKey())&&(q=g(),q.setFormat(n.getFormatType()),q.setIndent(n.getIndent()),e.push(q),n.remove(!0))}if(null!==d)for(b=0;b<e.length;b++)d.append(e[b]);if(l.$isRootOrShadowRoot(f))if(k)if(null!==d)f.insertAfter(d);else for(d=e.length-1;0<=d;d--)f.insertAfter(e[d]);
|
|
16
|
+
else if(b=f.getFirstChild(),l.$isElementNode(b)&&(f=b),null===b)if(d)f.append(d);else for(d=0;d<e.length;d++)f.append(e[d]);else if(null!==d)b.insertBefore(d);else for(f=0;f<e.length;f++)b.insertBefore(e[f]);else if(d)f.insertAfter(d);else for(d=e.length-1;0<=d;d--)f.insertAfter(e[d]);e=l.$getPreviousSelection();l.$isRangeSelection(e)&&e.anchor.getNode().isAttached()&&e.focus.getNode().isAttached()?l.$setSelection(e.clone()):a.dirty=!0}}
|
|
17
|
+
function G(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function H(a){let b=a.parentNode;if(null==b)throw Error("Should never happen");return[b,Array.from(b.childNodes).indexOf(a)]}exports.$addNodeStyle=function(a){a=a.getStyle();let b=z(a);r.set(a,b)};exports.$cloneContents=function(a){return x(a)};exports.$cloneWithProperties=u;
|
|
18
|
+
exports.$getSelectionStyleValueForProperty=function(a,b,c=""){let g=null,d=a.getNodes();var f=a.anchor,h=a.focus,e=a.isBackward();a=e?h.offset:f.offset;f=e?h.getNode():f.getNode();for(h=0;h<d.length;h++){var k=d[h];if((0===h||0!==a||!k.is(f))&&l.$isTextNode(k)){e=b;var m=c;k=k.getStyle();k=y(k);e=null!==k?k[e]||m:m;if(null===g)g=e;else if(g!==e){g="";break}}}return null===g?c:g};exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};
|
|
19
|
+
exports.$isParentElementRTL=C;exports.$moveCaretSelection=B;exports.$moveCharacter=function(a,b,c){let g=C(a);B(a,b,c?!g:g,"character")};
|
|
20
|
+
exports.$patchStyleText=function(a,b){var c=a.getNodes();let g=c.length-1,d=c[0],f=c[g];if(!a.isCollapsed()){var h=a.anchor,e=a.focus;a=d.getTextContent().length;var k=e.offset,m=h.offset;h=(e=h.isBefore(e))?m:k;e=e?k:m;if(h===d.getTextContentSize()){let p=d.getNextSibling();l.$isTextNode(p)&&(h=m=0,d=p)}if(d.is(f))l.$isTextNode(d)&&(h=m>k?k:m,e=m>k?m:k,h!==e&&(0===h&&e===a?(A(d,b),d.select(h,e)):(c=d.splitText(h,e),c=0===h?c[0]:c[1],A(c,b),c.select(0,e-h))));else for(l.$isTextNode(d)&&(0!==h&&(d=
|
|
21
|
+
d.splitText(h)[1]),A(d,b)),l.$isTextNode(f)&&(a=f.getTextContent().length,e!==a&&([f]=f.splitText(e)),0!==e&&A(f,b)),a=1;a<g;a++)k=c[a],m=k.getKey(),l.$isTextNode(k)&&m!==d.getKey()&&m!==f.getKey()&&!k.isToken()&&A(k,b)}};
|
|
22
|
+
exports.$selectAll=function(a){let b=a.anchor;a=a.focus;var c=b.getNode().getTopLevelElementOrThrow().getParentOrThrow();let g=c.getFirstDescendant();c=c.getLastDescendant();let d="element",f="element",h=0;l.$isTextNode(g)?d="text":l.$isElementNode(g)||null===g||(g=g.getParentOrThrow());l.$isTextNode(c)?(f="text",h=c.getTextContentSize()):l.$isElementNode(c)||null===c||(c=c.getParentOrThrow());g&&c&&(b.set(g.getKey(),0,d),a.set(c.getKey(),h,f))};
|
|
23
|
+
exports.$shouldOverrideDefaultCharacterSelection=function(a,b){a=l.$getDecoratorNode(a.focus,b);return l.$isDecoratorNode(a)&&!a.isIsolated()};
|
|
24
|
+
exports.$sliceSelectedTextNodeContent=function(a,b){if(b.isSelected()&&!b.isSegmented()&&!b.isToken()&&(l.$isRangeSelection(a)||l.DEPRECATED_$isGridSelection(a))){var c=a.anchor.getNode(),g=a.focus.getNode(),d=b.is(c),f=b.is(g);if(d||f){d=a.isBackward();let [h,e]=a.getCharacterOffsets();a=c.is(g);f=b.is(d?g:c);g=b.is(d?c:g);c=0;let k=void 0;a?(c=h>e?e:h,k=h>e?h:e):f?(c=d?e:h,k=void 0):g&&(d=d?h:e,c=0,k=d);b.__text=b.__text.slice(c,k)}}return b};
|
|
25
|
+
exports.$wrapNodes=function(a,b,c=null){var g=a.getNodes();let d=g.length;var f=a.anchor;if(0===d||1===d&&"element"===f.type&&0===f.getNode().getChildrenSize()){a="text"===f.type?f.getNode().getParentOrThrow():f.getNode();g=a.getChildren();let e=b();e.setFormat(a.getFormatType());e.setIndent(a.getIndent());g.forEach(k=>e.append(k));c&&(e=c.append(e));a.replace(e)}else{f=null;var h=[];for(let e=0;e<d;e++){let k=g[e];l.$isRootOrShadowRoot(k)?(F(a,h,h.length,b,c),h=[],f=k):null===f||null!==f&&l.$hasAncestor(k,
|
|
26
|
+
f)?h.push(k):(F(a,h,h.length,b,c),h=[k])}F(a,h,h.length,b,c)}};exports.$wrapNodesImpl=F;
|
|
27
|
+
exports.createDOMRange=function(a,b,c,g,d){let f=b.getKey(),h=g.getKey(),e=document.createRange(),k=a.getElementByKey(f);a=a.getElementByKey(h);l.$isTextNode(b)&&(k=G(k));l.$isTextNode(g)&&(a=G(a));if(void 0===b||void 0===g||null===k||null===a)return null;"BR"===k.nodeName&&([k,c]=H(k));"BR"===a.nodeName&&([a,d]=H(a));b=k.firstChild;k===a&&null!=b&&"BR"===b.nodeName&&0===c&&0===d&&(d=1);try{e.setStart(k,c),e.setEnd(a,d)}catch(m){return null}!e.collapsed||c===d&&f===h||(e.setStart(a,d),e.setEnd(k,
|
|
28
|
+
c));return e};exports.createRectsFromDOMRange=function(a,b){var c=a.getRootElement();if(null===c)return[];a=c.getBoundingClientRect();c=getComputedStyle(c);c=parseFloat(c.paddingLeft)+parseFloat(c.paddingRight);b=Array.from(b.getClientRects());let g=b.length,d;for(let f=0;f<g;f++){let h=b[f],e=h.width+c===a.width;d&&d.top===h.top&&d.left===h.left&&d.width===h.width&&d.height===h.height||e?(b.splice(f--,1),g--):d=h}return b};exports.getStyleObjectFromCSS=y;
|
|
29
|
+
exports.trimTextContentFromAnchor=function(a,b,c){let g=b.getNode();if(l.$isElementNode(g)){var d=g.getDescendantByIndex(b.offset);null!==d&&(g=d)}for(;0<c&&null!==g;){var f=g.getPreviousSibling(),h=0;if(null===f){d=g.getParentOrThrow();for(var e=d.getPreviousSibling();null===e;){d=d.getParent();if(null===d){f=null;break}e=d.getPreviousSibling()}null!==d&&(h=d.isInline()?0:2,f=l.$isElementNode(e)?e.getLastDescendant():e)}let k=g.getTextContent();""===k&&l.$isElementNode(g)&&!g.isInline()&&(k="\n\n");
|
|
30
|
+
d=k.length;e=d-c;let m=k.slice(0,e);if(!l.$isTextNode(g)||c>=d)e=g.getParent(),g.remove(),null!=e&&0===e.getChildrenSize()&&e.remove(),c-=d+h,g=f;else{let p=g.getKey();f=a.getEditorState().read(()=>{const n=l.$getNodeByKey(p);return l.$isTextNode(n)&&n.isSimpleText()?n.getTextContent():null});null!==f&&f!==k?(c=l.$getPreviousSelection(),d=g,g.isSimpleText()?g.setTextContent(f):(d=l.$createTextNode(f),g.replace(d)),l.$isRangeSelection(c)&&c.isCollapsed()&&(c=c.anchor.offset,d.select(c,c))):g.isSimpleText()?
|
|
31
|
+
(f=b.key===p,h=b.offset,h<c&&(h=d),c=f?h-c:0,d=f?h:e,f&&0===c?([c]=g.splitText(c,d),c.remove()):([,c]=g.splitText(c,d),c.remove())):(c=l.$createTextNode(m),g.replace(c));c=0}}}
|
package/README.md
CHANGED
|
@@ -95,12 +95,12 @@ Expands the current Selection to cover all of the content in the editor.
|
|
|
95
95
|
export function $selectAll(selection: RangeSelection): void;
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
-
#### `$
|
|
98
|
+
#### `$wrapNodes`
|
|
99
99
|
|
|
100
|
-
Attempts to wrap all
|
|
100
|
+
Attempts to wrap all nodes in the Selection in ElementNodes returned from createElement. If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement. It attempts to append the resulting sub-tree to the nearest safe insertion target.
|
|
101
101
|
|
|
102
102
|
```ts
|
|
103
|
-
export function $
|
|
103
|
+
export function $wrapNodes(
|
|
104
104
|
selection: RangeSelection,
|
|
105
105
|
createElement: () => ElementNode,
|
|
106
106
|
wrappingElement?: ElementNode,
|
package/index.d.ts
CHANGED
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*
|
|
8
8
|
*/
|
|
9
|
-
import
|
|
9
|
+
import { ElementNode, GridSelection, LexicalEditor, LexicalNode, NodeKey, NodeSelection, Point, RangeSelection, TextNode } from 'lexical';
|
|
10
10
|
export declare function $cloneWithProperties<T extends LexicalNode>(node: T): T;
|
|
11
11
|
export declare function $cloneContents(selection: RangeSelection | NodeSelection | GridSelection): {
|
|
12
12
|
nodeMap: Array<[NodeKey, LexicalNode]>;
|
|
13
13
|
range: Array<NodeKey>;
|
|
14
14
|
};
|
|
15
|
-
export declare function getStyleObjectFromCSS(css: string): Record<string, string
|
|
15
|
+
export declare function getStyleObjectFromCSS(css: string): Record<string, string>;
|
|
16
16
|
export declare function $addNodeStyle(node: TextNode): void;
|
|
17
17
|
export declare function $patchStyleText(selection: RangeSelection | GridSelection, patch: Record<string, string>): void;
|
|
18
18
|
export declare function $getSelectionStyleValueForProperty(selection: RangeSelection, styleProperty: string, defaultValue?: string): string;
|
|
@@ -20,7 +20,18 @@ export declare function $moveCaretSelection(selection: RangeSelection, isHolding
|
|
|
20
20
|
export declare function $isParentElementRTL(selection: RangeSelection): boolean;
|
|
21
21
|
export declare function $moveCharacter(selection: RangeSelection, isHoldingShift: boolean, isBackward: boolean): void;
|
|
22
22
|
export declare function $selectAll(selection: RangeSelection): void;
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Attempts to wrap all nodes in the Selection in ElementNodes returned from createElement.
|
|
25
|
+
* If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement.
|
|
26
|
+
* It attempts to append the resulting sub-tree to the nearest safe insertion target.
|
|
27
|
+
*
|
|
28
|
+
* @param selection
|
|
29
|
+
* @param createElement
|
|
30
|
+
* @param wrappingElement
|
|
31
|
+
* @returns
|
|
32
|
+
*/
|
|
33
|
+
export declare function $wrapNodes(selection: RangeSelection, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
|
|
34
|
+
export declare function $wrapNodesImpl(selection: RangeSelection, nodes: LexicalNode[], nodesLength: number, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
|
|
24
35
|
export declare function $isAtNodeEnd(point: Point): boolean;
|
|
25
36
|
export declare function $shouldOverrideDefaultCharacterSelection(selection: RangeSelection, isBackward: boolean): boolean;
|
|
26
37
|
export declare function createDOMRange(editor: LexicalEditor, anchorNode: LexicalNode, _anchorOffset: number, focusNode: LexicalNode, _focusOffset: number): Range | null;
|
package/package.json
CHANGED