@lexical/selection 0.12.6 → 0.13.1
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 +31 -33
- package/LexicalSelection.js.flow +3 -3
- package/LexicalSelection.prod.js +22 -23
- package/lexical-node.d.ts +3 -3
- package/package.json +2 -2
- package/range-selection.d.ts +5 -5
package/LexicalSelection.dev.js
CHANGED
|
@@ -240,14 +240,16 @@ function $cloneWithProperties(node) {
|
|
|
240
240
|
* @returns The updated TextNode.
|
|
241
241
|
*/
|
|
242
242
|
function $sliceSelectedTextNodeContent(selection, textNode) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
const
|
|
243
|
+
const anchorAndFocus = selection.getStartEndPoints();
|
|
244
|
+
if (textNode.isSelected(selection) && !textNode.isSegmented() && !textNode.isToken() && anchorAndFocus !== null) {
|
|
245
|
+
const [anchor, focus] = anchorAndFocus;
|
|
246
|
+
const isBackward = selection.isBackward();
|
|
247
|
+
const anchorNode = anchor.getNode();
|
|
248
|
+
const focusNode = focus.getNode();
|
|
246
249
|
const isAnchor = textNode.is(anchorNode);
|
|
247
250
|
const isFocus = textNode.is(focusNode);
|
|
248
251
|
if (isAnchor || isFocus) {
|
|
249
|
-
const
|
|
250
|
-
const [anchorOffset, focusOffset] = selection.getCharacterOffsets();
|
|
252
|
+
const [anchorOffset, focusOffset] = lexical.$getCharacterOffsets(selection);
|
|
251
253
|
const isSame = anchorNode.is(focusNode);
|
|
252
254
|
const isFirst = textNode.is(isBackward ? focusNode : anchorNode);
|
|
253
255
|
const isLast = textNode.is(isBackward ? anchorNode : focusNode);
|
|
@@ -412,7 +414,9 @@ function $addNodeStyle(node) {
|
|
|
412
414
|
function $patchStyle(target, patch) {
|
|
413
415
|
const prevStyles = getStyleObjectFromCSS('getStyle' in target ? target.getStyle() : target.style);
|
|
414
416
|
const newStyles = Object.entries(patch).reduce((styles, [key, value]) => {
|
|
415
|
-
if (value
|
|
417
|
+
if (value instanceof Function) {
|
|
418
|
+
styles[key] = value(prevStyles[key]);
|
|
419
|
+
} else if (value === null) {
|
|
416
420
|
delete styles[key];
|
|
417
421
|
} else {
|
|
418
422
|
styles[key] = value;
|
|
@@ -431,26 +435,16 @@ function $patchStyle(target, patch) {
|
|
|
431
435
|
* Will update partially selected TextNodes by splitting the TextNode and applying
|
|
432
436
|
* the styles to the appropriate one.
|
|
433
437
|
* @param selection - The selected node(s) to update.
|
|
434
|
-
* @param patch - The patch to apply, which can include multiple styles. { CSSProperty: value }
|
|
438
|
+
* @param patch - The patch to apply, which can include multiple styles. { CSSProperty: value }. Can also accept a function that returns the new property value.
|
|
435
439
|
*/
|
|
436
440
|
function $patchStyleText(selection, patch) {
|
|
437
441
|
const selectedNodes = selection.getNodes();
|
|
438
442
|
const selectedNodesLength = selectedNodes.length;
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
const cellSelectionAnchor = cellSelection.anchor;
|
|
442
|
-
const cellSelectionFocus = cellSelection.focus;
|
|
443
|
-
for (let i = 0; i < selectedNodesLength; i++) {
|
|
444
|
-
const node = selectedNodes[i];
|
|
445
|
-
if (lexical.DEPRECATED_$isGridCellNode(node)) {
|
|
446
|
-
cellSelectionAnchor.set(node.getKey(), 0, 'element');
|
|
447
|
-
cellSelectionFocus.set(node.getKey(), node.getChildrenSize(), 'element');
|
|
448
|
-
$patchStyleText(lexical.$normalizeSelection__EXPERIMENTAL(cellSelection), patch);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
lexical.$setSelection(selection);
|
|
443
|
+
const anchorAndFocus = selection.getStartEndPoints();
|
|
444
|
+
if (anchorAndFocus === null) {
|
|
452
445
|
return;
|
|
453
446
|
}
|
|
447
|
+
const [anchor, focus] = anchorAndFocus;
|
|
454
448
|
const lastIndex = selectedNodesLength - 1;
|
|
455
449
|
let firstNode = selectedNodes[0];
|
|
456
450
|
let lastNode = selectedNodes[lastIndex];
|
|
@@ -458,8 +452,6 @@ function $patchStyleText(selection, patch) {
|
|
|
458
452
|
$patchStyle(selection, patch);
|
|
459
453
|
return;
|
|
460
454
|
}
|
|
461
|
-
const anchor = selection.anchor;
|
|
462
|
-
const focus = selection.focus;
|
|
463
455
|
const firstNodeText = firstNode.getTextContent();
|
|
464
456
|
const firstNodeTextLength = firstNodeText.length;
|
|
465
457
|
const focusOffset = focus.offset;
|
|
@@ -485,7 +477,7 @@ function $patchStyleText(selection, patch) {
|
|
|
485
477
|
|
|
486
478
|
// This is the case where we only selected a single node
|
|
487
479
|
if (selectedNodes.length === 1) {
|
|
488
|
-
if (lexical.$isTextNode(firstNode)) {
|
|
480
|
+
if (lexical.$isTextNode(firstNode) && firstNode.canHaveFormat()) {
|
|
489
481
|
startOffset = startType === 'element' ? 0 : anchorOffset > focusOffset ? focusOffset : anchorOffset;
|
|
490
482
|
endOffset = endType === 'element' ? firstNodeTextLength : anchorOffset > focusOffset ? anchorOffset : focusOffset;
|
|
491
483
|
|
|
@@ -508,7 +500,7 @@ function $patchStyleText(selection, patch) {
|
|
|
508
500
|
}
|
|
509
501
|
} // multiple nodes selected.
|
|
510
502
|
} else {
|
|
511
|
-
if (lexical.$isTextNode(firstNode) && startOffset < firstNode.getTextContentSize()) {
|
|
503
|
+
if (lexical.$isTextNode(firstNode) && startOffset < firstNode.getTextContentSize() && firstNode.canHaveFormat()) {
|
|
512
504
|
if (startOffset !== 0) {
|
|
513
505
|
// the entire first node isn't selected, so split it
|
|
514
506
|
firstNode = firstNode.splitText(startOffset)[1];
|
|
@@ -517,7 +509,7 @@ function $patchStyleText(selection, patch) {
|
|
|
517
509
|
}
|
|
518
510
|
$patchStyle(firstNode, patch);
|
|
519
511
|
}
|
|
520
|
-
if (lexical.$isTextNode(lastNode)) {
|
|
512
|
+
if (lexical.$isTextNode(lastNode) && lastNode.canHaveFormat()) {
|
|
521
513
|
const lastNodeText = lastNode.getTextContent();
|
|
522
514
|
const lastNodeTextLength = lastNodeText.length;
|
|
523
515
|
|
|
@@ -533,7 +525,7 @@ function $patchStyleText(selection, patch) {
|
|
|
533
525
|
if (endOffset !== lastNodeTextLength) {
|
|
534
526
|
[lastNode] = lastNode.splitText(endOffset);
|
|
535
527
|
}
|
|
536
|
-
if (endOffset !== 0) {
|
|
528
|
+
if (endOffset !== 0 || endType === 'element') {
|
|
537
529
|
$patchStyle(lastNode, patch);
|
|
538
530
|
}
|
|
539
531
|
}
|
|
@@ -542,7 +534,7 @@ function $patchStyleText(selection, patch) {
|
|
|
542
534
|
for (let i = 1; i < lastIndex; i++) {
|
|
543
535
|
const selectedNode = selectedNodes[i];
|
|
544
536
|
const selectedNodeKey = selectedNode.getKey();
|
|
545
|
-
if (lexical.$isTextNode(selectedNode) && selectedNodeKey !== firstNode.getKey() && selectedNodeKey !== lastNode.getKey() && !selectedNode.isToken()) {
|
|
537
|
+
if (lexical.$isTextNode(selectedNode) && selectedNode.canHaveFormat() && selectedNodeKey !== firstNode.getKey() && selectedNodeKey !== lastNode.getKey() && !selectedNode.isToken()) {
|
|
546
538
|
$patchStyle(selectedNode, patch);
|
|
547
539
|
}
|
|
548
540
|
}
|
|
@@ -563,7 +555,12 @@ function $patchStyleText(selection, patch) {
|
|
|
563
555
|
* @param createElement - The function that creates the node. eg. $createParagraphNode.
|
|
564
556
|
*/
|
|
565
557
|
function $setBlocksType(selection, createElement) {
|
|
566
|
-
if (selection
|
|
558
|
+
if (selection === null) {
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
const anchorAndFocus = selection.getStartEndPoints();
|
|
562
|
+
const anchor = anchorAndFocus ? anchorAndFocus[0] : null;
|
|
563
|
+
if (anchor !== null && anchor.key === 'root') {
|
|
567
564
|
const element = createElement();
|
|
568
565
|
const root = lexical.$getRoot();
|
|
569
566
|
const firstChild = root.getFirstChild();
|
|
@@ -575,7 +572,7 @@ function $setBlocksType(selection, createElement) {
|
|
|
575
572
|
return;
|
|
576
573
|
}
|
|
577
574
|
const nodes = selection.getNodes();
|
|
578
|
-
const firstSelectedBlock = $getAncestor(
|
|
575
|
+
const firstSelectedBlock = anchor !== null ? $getAncestor(anchor.getNode(), INTERNAL_$isBlock) : false;
|
|
579
576
|
if (firstSelectedBlock && nodes.indexOf(firstSelectedBlock) === -1) {
|
|
580
577
|
nodes.push(firstSelectedBlock);
|
|
581
578
|
}
|
|
@@ -616,10 +613,11 @@ function $removeParentEmptyElements(startingNode) {
|
|
|
616
613
|
* @param wrappingElement - An element to append the wrapped selection and its children to.
|
|
617
614
|
*/
|
|
618
615
|
function $wrapNodes(selection, createElement, wrappingElement = null) {
|
|
616
|
+
const anchorAndFocus = selection.getStartEndPoints();
|
|
617
|
+
const anchor = anchorAndFocus ? anchorAndFocus[0] : null;
|
|
619
618
|
const nodes = selection.getNodes();
|
|
620
619
|
const nodesLength = nodes.length;
|
|
621
|
-
|
|
622
|
-
if (nodesLength === 0 || nodesLength === 1 && anchor.type === 'element' && anchor.getNode().getChildrenSize() === 0) {
|
|
620
|
+
if (anchor !== null && (nodesLength === 0 || nodesLength === 1 && anchor.type === 'element' && anchor.getNode().getChildrenSize() === 0)) {
|
|
623
621
|
const target = anchor.type === 'text' ? anchor.getNode().getParentOrThrow() : anchor.getNode();
|
|
624
622
|
const children = target.getChildren();
|
|
625
623
|
let element = createElement();
|
|
@@ -954,8 +952,8 @@ function $getSelectionStyleValueForProperty(selection, styleProperty, defaultVal
|
|
|
954
952
|
* Please do not use it as it may change in the future.
|
|
955
953
|
*/
|
|
956
954
|
function INTERNAL_$isBlock(node) {
|
|
957
|
-
if (lexical.$isDecoratorNode(node)
|
|
958
|
-
return
|
|
955
|
+
if (lexical.$isDecoratorNode(node)) {
|
|
956
|
+
return false;
|
|
959
957
|
}
|
|
960
958
|
if (!lexical.$isElementNode(node) || lexical.$isRootOrShadowRoot(node)) {
|
|
961
959
|
return false;
|
package/LexicalSelection.js.flow
CHANGED
|
@@ -11,9 +11,9 @@ import type {
|
|
|
11
11
|
LexicalEditor,
|
|
12
12
|
LexicalNode,
|
|
13
13
|
NodeKey,
|
|
14
|
+
BaseSelection,
|
|
14
15
|
NodeSelection,
|
|
15
16
|
Point,
|
|
16
|
-
INTERNAL_PointSelection,
|
|
17
17
|
RangeSelection,
|
|
18
18
|
} from 'lexical';
|
|
19
19
|
declare export function $cloneWithProperties<T: LexicalNode>(node: T): T;
|
|
@@ -21,7 +21,7 @@ declare export function getStyleObjectFromCSS(css: string): {
|
|
|
21
21
|
[string]: string,
|
|
22
22
|
};
|
|
23
23
|
declare export function $patchStyleText(
|
|
24
|
-
selection:
|
|
24
|
+
selection: BaseSelection,
|
|
25
25
|
patch: {
|
|
26
26
|
[string]: string | null,
|
|
27
27
|
},
|
|
@@ -45,7 +45,7 @@ declare export function $moveCharacter(
|
|
|
45
45
|
): void;
|
|
46
46
|
declare export function $selectAll(selection: RangeSelection): void;
|
|
47
47
|
declare export function $wrapNodes(
|
|
48
|
-
selection:
|
|
48
|
+
selection: BaseSelection,
|
|
49
49
|
createElement: () => ElementNode,
|
|
50
50
|
wrappingElement?: ElementNode,
|
|
51
51
|
): void;
|
package/LexicalSelection.prod.js
CHANGED
|
@@ -4,28 +4,27 @@
|
|
|
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 k=require("lexical");let
|
|
8
|
-
function A(a){let b="";for(let
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
c)for(b=0;b<g.length;b++)c.append(g[b]);b=null;if(k.$isRootOrShadowRoot(f))if(l)if(null!==c)f.insertAfter(c);else for(c=g.length-1;0<=c;c--)f.insertAfter(g[c]);else if(l=f.getFirstChild(),k.$isElementNode(l)&&(f=l),null===l)if(c)f.append(c);else for(c=0;c<g.length;c++)l=g[c],f.append(l),b=l;else if(null!==c)l.insertBefore(c);else for(f=0;f<g.length;f++)c=g[f],l.insertBefore(c),b=c;else if(c)f.insertAfter(c);else for(c=g.length-1;0<=c;c--)l=g[c],f.insertAfter(l),b=l;g=k.$getPreviousSelection();k.$isRangeSelection(g)&&
|
|
15
|
-
g.anchor.getNode().isAttached()&&g.focus.getNode().isAttached()?k.$setSelection(g.clone()):null!==b?b.selectEnd():a.dirty=!0}}function F(a,b,d,e){a.modify(b?"extend":"move",d,e)}function G(a){a=a.anchor.getNode();return"rtl"===(k.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}
|
|
16
|
-
function H(a){if(k.$isDecoratorNode(a)&&!a.isInline())return!0;if(!k.$isElementNode(a)||k.$isRootOrShadowRoot(a))return!1;var b=a.getFirstChild();b=null===b||k.$isLineBreakNode(b)||k.$isTextNode(b)||b.isInline();return!a.isInline()&&!1!==a.canBeEmpty()&&b}exports.$addNodeStyle=function(a){a=a.getStyle();let b=y(a);u.set(a,b)};
|
|
7
|
+
'use strict';var k=require("lexical");let v=new Map;function w(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function x(a){let b=a.parentNode;if(null==b)throw Error("Should never happen");return[b,Array.from(b.childNodes).indexOf(a)]}function y(a){let b={};a=a.split(";");for(let c of a)if(""!==c){let [e,d]=c.split(/:([^]+)/);e&&d&&(b[e.trim()]=d.trim())}return b}function z(a){let b=v.get(a);void 0===b&&(b=y(a),v.set(a,b));return b}
|
|
8
|
+
function A(a){let b="";for(let c in a)c&&(b+=`${c}: ${a[c]};`);return b}function B(a,b){let c=z("getStyle"in a?a.getStyle():a.style);b=Object.entries(b).reduce((d,[g,h])=>{h instanceof Function?d[g]=h(c[g]):null===h?delete d[g]:d[g]=h;return d},{...c});let e=A(b);a.setStyle(e);v.set(e,b)}function C(a){for(;null!==a&&!k.$isRootOrShadowRoot(a);){let b=a.getLatest(),c=a.getParent();0===b.getChildrenSize()&&a.remove(!0);a=c}}
|
|
9
|
+
function D(a,b,c,e,d=null){if(0!==b.length){var g=b[0],h=new Map,f=[];g=k.$isElementNode(g)?g:g.getParentOrThrow();g.isInline()&&(g=g.getParentOrThrow());for(var l=!1;null!==g;){var m=g.getPreviousSibling();if(null!==m){g=m;l=!0;break}g=g.getParentOrThrow();if(k.$isRootOrShadowRoot(g))break}m=new Set;for(var p=0;p<c;p++){var q=b[p];k.$isElementNode(q)&&0===q.getChildrenSize()&&m.add(q.getKey())}var n=new Set;for(p=0;p<c;p++){q=b[p];var r=q.getParent();null!==r&&r.isInline()&&(r=r.getParent());if(null!==
|
|
10
|
+
r&&k.$isLeafNode(q)&&!n.has(q.getKey())){if(q=r.getKey(),void 0===h.get(q)){let t=e();t.setFormat(r.getFormatType());t.setIndent(r.getIndent());f.push(t);h.set(q,t);r.getChildren().forEach(u=>{t.append(u);n.add(u.getKey());k.$isElementNode(u)&&u.getChildrenKeys().forEach(H=>n.add(H))});C(r)}}else if(m.has(q.getKey())){if(!k.$isElementNode(q))throw Error("Expected node in emptyElements to be an ElementNode");r=e();r.setFormat(q.getFormatType());r.setIndent(q.getIndent());f.push(r);q.remove(!0)}}if(null!==
|
|
11
|
+
d)for(b=0;b<f.length;b++)d.append(f[b]);b=null;if(k.$isRootOrShadowRoot(g))if(l)if(null!==d)g.insertAfter(d);else for(d=f.length-1;0<=d;d--)g.insertAfter(f[d]);else if(l=g.getFirstChild(),k.$isElementNode(l)&&(g=l),null===l)if(d)g.append(d);else for(d=0;d<f.length;d++)l=f[d],g.append(l),b=l;else if(null!==d)l.insertBefore(d);else for(g=0;g<f.length;g++)d=f[g],l.insertBefore(d),b=d;else if(d)g.insertAfter(d);else for(d=f.length-1;0<=d;d--)l=f[d],g.insertAfter(l),b=l;f=k.$getPreviousSelection();k.$isRangeSelection(f)&&
|
|
12
|
+
f.anchor.getNode().isAttached()&&f.focus.getNode().isAttached()?k.$setSelection(f.clone()):null!==b?b.selectEnd():a.dirty=!0}}function E(a,b,c,e){a.modify(b?"extend":"move",c,e)}function F(a){a=a.anchor.getNode();return"rtl"===(k.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}
|
|
13
|
+
function G(a){if(k.$isDecoratorNode(a)||!k.$isElementNode(a)||k.$isRootOrShadowRoot(a))return!1;var b=a.getFirstChild();b=null===b||k.$isLineBreakNode(b)||k.$isTextNode(b)||b.isInline();return!a.isInline()&&!1!==a.canBeEmpty()&&b}exports.$addNodeStyle=function(a){a=a.getStyle();let b=y(a);v.set(a,b)};
|
|
17
14
|
exports.$cloneWithProperties=function(a){let b=a.constructor.clone(a);b.__parent=a.__parent;b.__next=a.__next;b.__prev=a.__prev;if(k.$isElementNode(a)&&k.$isElementNode(b))return b.__first=a.__first,b.__last=a.__last,b.__size=a.__size,b.__format=a.__format,b.__indent=a.__indent,b.__dir=a.__dir,b;k.$isTextNode(a)&&k.$isTextNode(b)&&(b.__format=a.__format,b.__style=a.__style,b.__mode=a.__mode,b.__detail=a.__detail);return b};
|
|
18
|
-
exports.$getSelectionStyleValueForProperty=function(a,b,
|
|
19
|
-
exports.$isAtNodeEnd=function(a){if("text"===a.type)return a.offset===a.getNode().getTextContentSize();let b=a.getNode();if(!k.$isElementNode(b))throw Error("isAtNodeEnd: node must be a TextNode or ElementNode");return a.offset===b.getChildrenSize()};exports.$isParentElementRTL=
|
|
20
|
-
exports.$
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
exports.$
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
exports
|
|
27
|
-
|
|
15
|
+
exports.$getSelectionStyleValueForProperty=function(a,b,c=""){let e=null,d=a.getNodes();var g=a.anchor,h=a.focus,f=a.isBackward();let l=f?h.offset:g.offset;g=f?h.getNode():g.getNode();if(a.isCollapsed()&&""!==a.style&&(a=z(a.style),null!==a&&b in a))return a[b];for(a=0;a<d.length;a++){var m=d[a];if((0===a||0!==l||!m.is(g))&&k.$isTextNode(m))if(h=b,f=c,m=m.getStyle(),m=z(m),h=null!==m?m[h]||f:f,null===e)e=h;else if(e!==h){e="";break}}return null===e?c:e};
|
|
16
|
+
exports.$isAtNodeEnd=function(a){if("text"===a.type)return a.offset===a.getNode().getTextContentSize();let b=a.getNode();if(!k.$isElementNode(b))throw Error("isAtNodeEnd: node must be a TextNode or ElementNode");return a.offset===b.getChildrenSize()};exports.$isParentElementRTL=F;exports.$moveCaretSelection=E;exports.$moveCharacter=function(a,b,c){let e=F(a);E(a,b,c?!e:e,"character")};
|
|
17
|
+
exports.$patchStyleText=function(a,b){var c=a.getNodes(),e=c.length,d=a.getStartEndPoints();if(null!==d){var [g,h]=d;--e;d=c[0];var f=c[e];if(a.isCollapsed()&&k.$isRangeSelection(a))B(a,b);else{var l=d.getTextContent().length,m=h.offset,p=g.offset,q=g.isBefore(h),n=q?p:m;a=q?m:p;var r=q?g.type:h.type,t=q?h.type:g.type;q=q?h.key:g.key;if(k.$isTextNode(d)&&n===l){let u=d.getNextSibling();k.$isTextNode(u)&&(n=p=0,d=u)}if(1===c.length)k.$isTextNode(d)&&d.canHaveFormat()&&(n="element"===r?0:p>m?m:p,a=
|
|
18
|
+
"element"===t?l:p>m?p:m,n!==a&&(0===n&&a===l?(B(d,b),d.select(n,a)):(c=d.splitText(n,a),c=0===n?c[0]:c[1],B(c,b),c.select(0,a-n))));else for(k.$isTextNode(d)&&n<d.getTextContentSize()&&d.canHaveFormat()&&(0!==n&&(d=d.splitText(n)[1],n=0,g.set(d.getKey(),n,"text")),B(d,b)),k.$isTextNode(f)&&f.canHaveFormat()&&(n=f.getTextContent().length,f.__key!==q&&0!==a&&(a=n),a!==n&&([f]=f.splitText(a)),0===a&&"element"!==t||B(f,b)),a=1;a<e;a++)n=c[a],t=n.getKey(),k.$isTextNode(n)&&n.canHaveFormat()&&t!==d.getKey()&&
|
|
19
|
+
t!==f.getKey()&&!n.isToken()&&B(n,b)}}};exports.$selectAll=function(a){let b=a.anchor;a=a.focus;var c=b.getNode().getTopLevelElementOrThrow().getParentOrThrow();let e=c.getFirstDescendant();c=c.getLastDescendant();let d="element",g="element",h=0;k.$isTextNode(e)?d="text":k.$isElementNode(e)||null===e||(e=e.getParentOrThrow());k.$isTextNode(c)?(g="text",h=c.getTextContentSize()):k.$isElementNode(c)||null===c||(c=c.getParentOrThrow());e&&c&&(b.set(e.getKey(),0,d),a.set(c.getKey(),h,g))};
|
|
20
|
+
exports.$setBlocksType=function(a,b){if(null!==a){var c=a.getStartEndPoints();c=c?c[0]:null;if(null!==c&&"root"===c.key)b=b(),a=k.$getRoot(),(c=a.getFirstChild())?c.replace(b,!0):a.append(b);else{a=a.getNodes();if(null!==c){for(c=c.getNode();null!==c&&null!==c.getParent()&&!G(c);)c=c.getParentOrThrow();c=G(c)?c:null}else c=!1;c&&-1===a.indexOf(c)&&a.push(c);for(c=0;c<a.length;c++){let e=a[c];if(!G(e))continue;if(!k.$isElementNode(e))throw Error("Expected block node to be an ElementNode");let d=b();
|
|
21
|
+
d.setFormat(e.getFormatType());d.setIndent(e.getIndent());e.replace(d,!0)}}}};exports.$shouldOverrideDefaultCharacterSelection=function(a,b){a=k.$getAdjacentNode(a.focus,b);return k.$isDecoratorNode(a)&&!a.isIsolated()||k.$isElementNode(a)&&!a.isInline()&&!a.canBeEmpty()};
|
|
22
|
+
exports.$sliceSelectedTextNodeContent=function(a,b){var c=a.getStartEndPoints();if(b.isSelected(a)&&!b.isSegmented()&&!b.isToken()&&null!==c){let [f,l]=c;c=a.isBackward();var e=f.getNode(),d=l.getNode(),g=b.is(e),h=b.is(d);if(g||h){let [m,p]=k.$getCharacterOffsets(a);a=e.is(d);g=b.is(c?d:e);d=b.is(c?e:d);e=0;h=void 0;a?(e=m>p?p:m,h=m>p?m:p):g?(e=c?p:m,h=void 0):d&&(c=c?m:p,e=0,h=c);b.__text=b.__text.slice(e,h)}}return b};
|
|
23
|
+
exports.$wrapNodes=function(a,b,c=null){var e=a.getStartEndPoints(),d=e?e[0]:null;e=a.getNodes();let g=e.length;if(null!==d&&(0===g||1===g&&"element"===d.type&&0===d.getNode().getChildrenSize())){a="text"===d.type?d.getNode().getParentOrThrow():d.getNode();e=a.getChildren();let f=b();f.setFormat(a.getFormatType());f.setIndent(a.getIndent());e.forEach(l=>f.append(l));c&&(f=c.append(f));a.replace(f)}else{d=null;var h=[];for(let f=0;f<g;f++){let l=e[f];k.$isRootOrShadowRoot(l)?(D(a,h,h.length,b,c),h=
|
|
24
|
+
[],d=l):null===d||null!==d&&k.$hasAncestor(l,d)?h.push(l):(D(a,h,h.length,b,c),h=[l])}D(a,h,h.length,b,c)}};
|
|
25
|
+
exports.createDOMRange=function(a,b,c,e,d){let g=b.getKey(),h=e.getKey(),f=document.createRange(),l=a.getElementByKey(g);a=a.getElementByKey(h);k.$isTextNode(b)&&(l=w(l));k.$isTextNode(e)&&(a=w(a));if(void 0===b||void 0===e||null===l||null===a)return null;"BR"===l.nodeName&&([l,c]=x(l));"BR"===a.nodeName&&([a,d]=x(a));b=l.firstChild;l===a&&null!=b&&"BR"===b.nodeName&&0===c&&0===d&&(d=1);try{f.setStart(l,c),f.setEnd(a,d)}catch(m){return null}!f.collapsed||c===d&&g===h||(f.setStart(a,d),f.setEnd(l,
|
|
26
|
+
c));return f};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 e=b.length;b.sort((g,h)=>{let f=g.top-h.top;return 3>=Math.abs(f)?g.left-h.left:f});let d;for(let g=0;g<e;g++){let h=b[g],f=h.width+c===a.width;d&&d.top<=h.top&&d.top+d.height>h.top&&d.left+d.width>h.left||f?(b.splice(g--,1),e--):d=h}return b};
|
|
28
27
|
exports.getStyleObjectFromCSS=z;
|
|
29
|
-
exports.trimTextContentFromAnchor=function(a,b,
|
|
30
|
-
(
|
|
31
|
-
|
|
28
|
+
exports.trimTextContentFromAnchor=function(a,b,c){let e=b.getNode();if(k.$isElementNode(e)){var d=e.getDescendantByIndex(b.offset);null!==d&&(e=d)}for(;0<c&&null!==e;){k.$isElementNode(e)&&(d=e.getLastDescendant(),null!==d&&(e=d));var g=e.getPreviousSibling(),h=0;if(null===g){d=e.getParentOrThrow();for(var f=d.getPreviousSibling();null===f;){d=d.getParent();if(null===d){g=null;break}f=d.getPreviousSibling()}null!==d&&(h=d.isInline()?0:2,g=f)}f=e.getTextContent();""===f&&k.$isElementNode(e)&&!e.isInline()&&
|
|
29
|
+
(f="\n\n");d=f.length;if(!k.$isTextNode(e)||c>=d)f=e.getParent(),e.remove(),null==f||0!==f.getChildrenSize()||k.$isRootNode(f)||f.remove(),c-=d+h,e=g;else{let l=e.getKey();h=a.getEditorState().read(()=>{const p=k.$getNodeByKey(l);return k.$isTextNode(p)&&p.isSimpleText()?p.getTextContent():null});g=d-c;let m=f.slice(0,g);null!==h&&h!==f?(c=k.$getPreviousSelection(),d=e,e.isSimpleText()?e.setTextContent(h):(d=k.$createTextNode(h),e.replace(d)),k.$isRangeSelection(c)&&c.isCollapsed()&&(c=c.anchor.offset,
|
|
30
|
+
d.select(c,c))):e.isSimpleText()?(h=b.key===l,f=b.offset,f<c&&(f=d),c=h?f-c:0,d=h?f:g,h&&0===c?([c]=e.splitText(c,d),c.remove()):([,c]=e.splitText(c,d),c.remove())):(c=k.$createTextNode(m),e.replace(c));c=0}}}
|
package/lexical-node.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
|
-
import { BaseSelection,
|
|
8
|
+
import { BaseSelection, LexicalEditor, LexicalNode, Point, TextNode } from 'lexical';
|
|
9
9
|
/**
|
|
10
10
|
* Returns a copy of a node, but generates a new key for the copy.
|
|
11
11
|
* @param node - The node to be cloned.
|
|
@@ -45,6 +45,6 @@ export declare function $addNodeStyle(node: TextNode): void;
|
|
|
45
45
|
* Will update partially selected TextNodes by splitting the TextNode and applying
|
|
46
46
|
* the styles to the appropriate one.
|
|
47
47
|
* @param selection - The selected node(s) to update.
|
|
48
|
-
* @param patch - The patch to apply, which can include multiple styles. { CSSProperty: value }
|
|
48
|
+
* @param patch - The patch to apply, which can include multiple styles. { CSSProperty: value }. Can also accept a function that returns the new property value.
|
|
49
49
|
*/
|
|
50
|
-
export declare function $patchStyleText(selection:
|
|
50
|
+
export declare function $patchStyleText(selection: BaseSelection, patch: Record<string, string | null | ((currentStyleValue: string | null) => string)>): void;
|
package/package.json
CHANGED
package/range-selection.d.ts
CHANGED
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type { BaseSelection, ElementNode, LexicalNode, RangeSelection } from 'lexical';
|
|
9
9
|
/**
|
|
10
10
|
* Converts all nodes in the selection that are of one block type to another.
|
|
11
11
|
* @param selection - The selected blocks to be converted.
|
|
12
12
|
* @param createElement - The function that creates the node. eg. $createParagraphNode.
|
|
13
13
|
*/
|
|
14
|
-
export declare function $setBlocksType(selection:
|
|
14
|
+
export declare function $setBlocksType(selection: BaseSelection | null, createElement: () => ElementNode): void;
|
|
15
15
|
/**
|
|
16
16
|
* @deprecated
|
|
17
17
|
* Wraps all nodes in the selection into another node of the type returned by createElement.
|
|
@@ -19,7 +19,7 @@ export declare function $setBlocksType(selection: INTERNAL_PointSelection, creat
|
|
|
19
19
|
* @param createElement - A function that creates the wrapping ElementNode. eg. $createParagraphNode.
|
|
20
20
|
* @param wrappingElement - An element to append the wrapped selection and its children to.
|
|
21
21
|
*/
|
|
22
|
-
export declare function $wrapNodes(selection:
|
|
22
|
+
export declare function $wrapNodes(selection: BaseSelection, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
|
|
23
23
|
/**
|
|
24
24
|
* Wraps each node into a new ElementNode.
|
|
25
25
|
* @param selection - The selection of nodes to wrap.
|
|
@@ -29,7 +29,7 @@ export declare function $wrapNodes(selection: INTERNAL_PointSelection, createEle
|
|
|
29
29
|
* @param wrappingElement - An element to wrap all the nodes into.
|
|
30
30
|
* @returns
|
|
31
31
|
*/
|
|
32
|
-
export declare function $wrapNodesImpl(selection:
|
|
32
|
+
export declare function $wrapNodesImpl(selection: BaseSelection, nodes: LexicalNode[], nodesLength: number, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
|
|
33
33
|
/**
|
|
34
34
|
* Determines if the default character selection should be overridden. Used with DecoratorNodes
|
|
35
35
|
* @param selection - The selection whose default character selection may need to be overridden.
|
|
@@ -76,5 +76,5 @@ export declare function $getSelectionStyleValueForProperty(selection: RangeSelec
|
|
|
76
76
|
* This function is for internal use of the library.
|
|
77
77
|
* Please do not use it as it may change in the future.
|
|
78
78
|
*/
|
|
79
|
-
export declare function INTERNAL_$isBlock(node: LexicalNode): node is ElementNode
|
|
79
|
+
export declare function INTERNAL_$isBlock(node: LexicalNode): node is ElementNode;
|
|
80
80
|
export declare function $getAncestor<NodeType extends LexicalNode = LexicalNode>(node: LexicalNode, predicate: (ancestor: LexicalNode) => ancestor is NodeType): NodeType | null;
|