@lexical/selection 0.21.1-nightly.20241220.0 → 0.22.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 +23 -14
- package/LexicalSelection.dev.mjs +24 -16
- package/LexicalSelection.mjs +1 -0
- package/LexicalSelection.node.mjs +1 -0
- package/LexicalSelection.prod.js +23 -24
- package/LexicalSelection.prod.mjs +1 -1
- package/index.d.ts +2 -2
- package/lexical-node.d.ts +1 -0
- package/package.json +2 -2
package/LexicalSelection.dev.js
CHANGED
|
@@ -407,20 +407,28 @@ function $patchStyle(target, patch) {
|
|
|
407
407
|
* @param patch - The patch to apply, which can include multiple styles. \\{CSSProperty: value\\} . Can also accept a function that returns the new property value.
|
|
408
408
|
*/
|
|
409
409
|
function $patchStyleText(selection, patch) {
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
410
|
+
if (selection.isCollapsed() && lexical.$isRangeSelection(selection)) {
|
|
411
|
+
$patchStyle(selection, patch);
|
|
412
|
+
} else {
|
|
413
|
+
$forEachSelectedTextNode(textNode => {
|
|
414
|
+
$patchStyle(textNode, patch);
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
function $forEachSelectedTextNode(fn) {
|
|
419
|
+
const selection = lexical.$getSelection();
|
|
420
|
+
if (!lexical.$isRangeSelection(selection)) {
|
|
414
421
|
return;
|
|
415
422
|
}
|
|
416
|
-
const
|
|
423
|
+
const selectedNodes = selection.getNodes();
|
|
424
|
+
const selectedNodesLength = selectedNodes.length;
|
|
425
|
+
const {
|
|
426
|
+
anchor,
|
|
427
|
+
focus
|
|
428
|
+
} = selection;
|
|
417
429
|
const lastIndex = selectedNodesLength - 1;
|
|
418
430
|
let firstNode = selectedNodes[0];
|
|
419
431
|
let lastNode = selectedNodes[lastIndex];
|
|
420
|
-
if (selection.isCollapsed() && lexical.$isRangeSelection(selection)) {
|
|
421
|
-
$patchStyle(selection, patch);
|
|
422
|
-
return;
|
|
423
|
-
}
|
|
424
432
|
const firstNodeText = firstNode.getTextContent();
|
|
425
433
|
const firstNodeTextLength = firstNodeText.length;
|
|
426
434
|
const focusOffset = focus.offset;
|
|
@@ -457,14 +465,14 @@ function $patchStyleText(selection, patch) {
|
|
|
457
465
|
|
|
458
466
|
// The entire node is selected or a token/segment, so just format it
|
|
459
467
|
if (lexical.$isTokenOrSegmented(firstNode) || startOffset === 0 && endOffset === firstNodeTextLength) {
|
|
460
|
-
|
|
468
|
+
fn(firstNode);
|
|
461
469
|
firstNode.select(startOffset, endOffset);
|
|
462
470
|
} else {
|
|
463
471
|
// The node is partially selected, so split it into two nodes
|
|
464
472
|
// and style the selected one.
|
|
465
473
|
const splitNodes = firstNode.splitText(startOffset, endOffset);
|
|
466
474
|
const replacement = startOffset === 0 ? splitNodes[0] : splitNodes[1];
|
|
467
|
-
|
|
475
|
+
fn(replacement);
|
|
468
476
|
replacement.select(0, endOffset - startOffset);
|
|
469
477
|
}
|
|
470
478
|
} // multiple nodes selected.
|
|
@@ -480,7 +488,7 @@ function $patchStyleText(selection, patch) {
|
|
|
480
488
|
focus.set(firstNode.getKey(), startOffset, 'text');
|
|
481
489
|
}
|
|
482
490
|
}
|
|
483
|
-
|
|
491
|
+
fn(firstNode);
|
|
484
492
|
}
|
|
485
493
|
if (lexical.$isTextNode(lastNode) && lastNode.canHaveFormat()) {
|
|
486
494
|
const lastNodeText = lastNode.getTextContent();
|
|
@@ -499,7 +507,7 @@ function $patchStyleText(selection, patch) {
|
|
|
499
507
|
[lastNode] = lastNode.splitText(endOffset);
|
|
500
508
|
}
|
|
501
509
|
if (endOffset !== 0 || endType === 'element') {
|
|
502
|
-
|
|
510
|
+
fn(lastNode);
|
|
503
511
|
}
|
|
504
512
|
}
|
|
505
513
|
|
|
@@ -508,7 +516,7 @@ function $patchStyleText(selection, patch) {
|
|
|
508
516
|
const selectedNode = selectedNodes[i];
|
|
509
517
|
const selectedNodeKey = selectedNode.getKey();
|
|
510
518
|
if (lexical.$isTextNode(selectedNode) && selectedNode.canHaveFormat() && selectedNodeKey !== firstNode.getKey() && selectedNodeKey !== lastNode.getKey() && !selectedNode.isToken()) {
|
|
511
|
-
|
|
519
|
+
fn(selectedNode);
|
|
512
520
|
}
|
|
513
521
|
}
|
|
514
522
|
}
|
|
@@ -926,6 +934,7 @@ const trimTextContentFromAnchor = $trimTextContentFromAnchor;
|
|
|
926
934
|
exports.$cloneWithProperties = lexical.$cloneWithProperties;
|
|
927
935
|
exports.$selectAll = lexical.$selectAll;
|
|
928
936
|
exports.$addNodeStyle = $addNodeStyle;
|
|
937
|
+
exports.$forEachSelectedTextNode = $forEachSelectedTextNode;
|
|
929
938
|
exports.$getSelectionStyleValueForProperty = $getSelectionStyleValueForProperty;
|
|
930
939
|
exports.$isAtNodeEnd = $isAtNodeEnd;
|
|
931
940
|
exports.$isParentElementRTL = $isParentElementRTL;
|
package/LexicalSelection.dev.mjs
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { $isTextNode, $getCharacterOffsets, $isElementNode, $isRootNode, $getNodeByKey, $getPreviousSelection, $createTextNode, $isRangeSelection, $isTokenOrSegmented, $getRoot, $isRootOrShadowRoot, $hasAncestor, $isLeafNode, $setSelection, $getAdjacentNode, $isDecoratorNode, $isLineBreakNode } from 'lexical';
|
|
9
|
+
import { $isTextNode, $getCharacterOffsets, $isElementNode, $isRootNode, $getNodeByKey, $getPreviousSelection, $createTextNode, $isRangeSelection, $getSelection, $isTokenOrSegmented, $getRoot, $isRootOrShadowRoot, $hasAncestor, $isLeafNode, $setSelection, $getAdjacentNode, $isDecoratorNode, $isLineBreakNode } from 'lexical';
|
|
10
10
|
export { $cloneWithProperties, $selectAll } from 'lexical';
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -406,20 +406,28 @@ function $patchStyle(target, patch) {
|
|
|
406
406
|
* @param patch - The patch to apply, which can include multiple styles. \\{CSSProperty: value\\} . Can also accept a function that returns the new property value.
|
|
407
407
|
*/
|
|
408
408
|
function $patchStyleText(selection, patch) {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
409
|
+
if (selection.isCollapsed() && $isRangeSelection(selection)) {
|
|
410
|
+
$patchStyle(selection, patch);
|
|
411
|
+
} else {
|
|
412
|
+
$forEachSelectedTextNode(textNode => {
|
|
413
|
+
$patchStyle(textNode, patch);
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
function $forEachSelectedTextNode(fn) {
|
|
418
|
+
const selection = $getSelection();
|
|
419
|
+
if (!$isRangeSelection(selection)) {
|
|
413
420
|
return;
|
|
414
421
|
}
|
|
415
|
-
const
|
|
422
|
+
const selectedNodes = selection.getNodes();
|
|
423
|
+
const selectedNodesLength = selectedNodes.length;
|
|
424
|
+
const {
|
|
425
|
+
anchor,
|
|
426
|
+
focus
|
|
427
|
+
} = selection;
|
|
416
428
|
const lastIndex = selectedNodesLength - 1;
|
|
417
429
|
let firstNode = selectedNodes[0];
|
|
418
430
|
let lastNode = selectedNodes[lastIndex];
|
|
419
|
-
if (selection.isCollapsed() && $isRangeSelection(selection)) {
|
|
420
|
-
$patchStyle(selection, patch);
|
|
421
|
-
return;
|
|
422
|
-
}
|
|
423
431
|
const firstNodeText = firstNode.getTextContent();
|
|
424
432
|
const firstNodeTextLength = firstNodeText.length;
|
|
425
433
|
const focusOffset = focus.offset;
|
|
@@ -456,14 +464,14 @@ function $patchStyleText(selection, patch) {
|
|
|
456
464
|
|
|
457
465
|
// The entire node is selected or a token/segment, so just format it
|
|
458
466
|
if ($isTokenOrSegmented(firstNode) || startOffset === 0 && endOffset === firstNodeTextLength) {
|
|
459
|
-
|
|
467
|
+
fn(firstNode);
|
|
460
468
|
firstNode.select(startOffset, endOffset);
|
|
461
469
|
} else {
|
|
462
470
|
// The node is partially selected, so split it into two nodes
|
|
463
471
|
// and style the selected one.
|
|
464
472
|
const splitNodes = firstNode.splitText(startOffset, endOffset);
|
|
465
473
|
const replacement = startOffset === 0 ? splitNodes[0] : splitNodes[1];
|
|
466
|
-
|
|
474
|
+
fn(replacement);
|
|
467
475
|
replacement.select(0, endOffset - startOffset);
|
|
468
476
|
}
|
|
469
477
|
} // multiple nodes selected.
|
|
@@ -479,7 +487,7 @@ function $patchStyleText(selection, patch) {
|
|
|
479
487
|
focus.set(firstNode.getKey(), startOffset, 'text');
|
|
480
488
|
}
|
|
481
489
|
}
|
|
482
|
-
|
|
490
|
+
fn(firstNode);
|
|
483
491
|
}
|
|
484
492
|
if ($isTextNode(lastNode) && lastNode.canHaveFormat()) {
|
|
485
493
|
const lastNodeText = lastNode.getTextContent();
|
|
@@ -498,7 +506,7 @@ function $patchStyleText(selection, patch) {
|
|
|
498
506
|
[lastNode] = lastNode.splitText(endOffset);
|
|
499
507
|
}
|
|
500
508
|
if (endOffset !== 0 || endType === 'element') {
|
|
501
|
-
|
|
509
|
+
fn(lastNode);
|
|
502
510
|
}
|
|
503
511
|
}
|
|
504
512
|
|
|
@@ -507,7 +515,7 @@ function $patchStyleText(selection, patch) {
|
|
|
507
515
|
const selectedNode = selectedNodes[i];
|
|
508
516
|
const selectedNodeKey = selectedNode.getKey();
|
|
509
517
|
if ($isTextNode(selectedNode) && selectedNode.canHaveFormat() && selectedNodeKey !== firstNode.getKey() && selectedNodeKey !== lastNode.getKey() && !selectedNode.isToken()) {
|
|
510
|
-
|
|
518
|
+
fn(selectedNode);
|
|
511
519
|
}
|
|
512
520
|
}
|
|
513
521
|
}
|
|
@@ -922,4 +930,4 @@ function $getAncestor(node, predicate) {
|
|
|
922
930
|
/** @deprecated renamed to {@link $trimTextContentFromAnchor} by @lexical/eslint-plugin rules-of-lexical */
|
|
923
931
|
const trimTextContentFromAnchor = $trimTextContentFromAnchor;
|
|
924
932
|
|
|
925
|
-
export { $addNodeStyle, $getSelectionStyleValueForProperty, $isAtNodeEnd, $isParentElementRTL, $moveCaretSelection, $moveCharacter, $patchStyleText, $setBlocksType, $shouldOverrideDefaultCharacterSelection, $sliceSelectedTextNodeContent, $trimTextContentFromAnchor, $wrapNodes, createDOMRange, createRectsFromDOMRange, getCSSFromStyleObject, getStyleObjectFromCSS, trimTextContentFromAnchor };
|
|
933
|
+
export { $addNodeStyle, $forEachSelectedTextNode, $getSelectionStyleValueForProperty, $isAtNodeEnd, $isParentElementRTL, $moveCaretSelection, $moveCharacter, $patchStyleText, $setBlocksType, $shouldOverrideDefaultCharacterSelection, $sliceSelectedTextNodeContent, $trimTextContentFromAnchor, $wrapNodes, createDOMRange, createRectsFromDOMRange, getCSSFromStyleObject, getStyleObjectFromCSS, trimTextContentFromAnchor };
|
package/LexicalSelection.mjs
CHANGED
|
@@ -11,6 +11,7 @@ import * as modProd from './LexicalSelection.prod.mjs';
|
|
|
11
11
|
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
12
12
|
export const $addNodeStyle = mod.$addNodeStyle;
|
|
13
13
|
export const $cloneWithProperties = mod.$cloneWithProperties;
|
|
14
|
+
export const $forEachSelectedTextNode = mod.$forEachSelectedTextNode;
|
|
14
15
|
export const $getSelectionStyleValueForProperty = mod.$getSelectionStyleValueForProperty;
|
|
15
16
|
export const $isAtNodeEnd = mod.$isAtNodeEnd;
|
|
16
17
|
export const $isParentElementRTL = mod.$isParentElementRTL;
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
const mod = await (process.env.NODE_ENV === 'development' ? import('./LexicalSelection.dev.mjs') : import('./LexicalSelection.prod.mjs'));
|
|
10
10
|
export const $addNodeStyle = mod.$addNodeStyle;
|
|
11
11
|
export const $cloneWithProperties = mod.$cloneWithProperties;
|
|
12
|
+
export const $forEachSelectedTextNode = mod.$forEachSelectedTextNode;
|
|
12
13
|
export const $getSelectionStyleValueForProperty = mod.$getSelectionStyleValueForProperty;
|
|
13
14
|
export const $isAtNodeEnd = mod.$isAtNodeEnd;
|
|
14
15
|
export const $isParentElementRTL = mod.$isParentElementRTL;
|
package/LexicalSelection.prod.js
CHANGED
|
@@ -6,28 +6,27 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
'use strict';var k=require("lexical"),
|
|
10
|
-
function y(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function
|
|
11
|
-
function D(a){let
|
|
12
|
-
function E(a,
|
|
13
|
-
if(!k.$isTextNode(e)||
|
|
14
|
-
e.isSimpleText()?(h=
|
|
15
|
-
function G(a){
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
exports.$
|
|
23
|
-
exports.$
|
|
24
|
-
"
|
|
25
|
-
a
|
|
26
|
-
exports.$
|
|
27
|
-
|
|
28
|
-
exports.$
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
exports.
|
|
32
|
-
c));return f};exports.createRectsFromDOMRange=function(a,d){var c=a.getRootElement();if(null===c)return[];a=c.getBoundingClientRect();c=getComputedStyle(c);c=parseFloat(c.paddingLeft)+parseFloat(c.paddingRight);d=Array.from(d.getClientRects());let e=d.length;d.sort((g,h)=>{let f=g.top-h.top;return 3>=Math.abs(f)?g.left-h.left:f});let b;for(let g=0;g<e;g++){let h=d[g],f=h.width+c===a.width;b&&b.top<=h.top&&b.top+b.height>h.top&&b.left+b.width>h.left||f?(d.splice(g--,1),e--):b=h}return d};
|
|
9
|
+
'use strict';var k=require("lexical"),v;function w(a){let c=new URLSearchParams;c.append("code",a);for(let b=1;b<arguments.length;b++)c.append("v",arguments[b]);throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?${c} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}v=w&&w.__esModule&&Object.prototype.hasOwnProperty.call(w,"default")?w["default"]:w;let x=new Map;
|
|
10
|
+
function y(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function A(a){let c=a.parentNode;if(null==c)throw Error("Should never happen");return[c,Array.from(c.childNodes).indexOf(a)]}function B(a){let c={};if(!a)return c;a=a.split(";");for(let b of a)if(""!==b){let [e,d]=b.split(/:([^]+)/);e&&d&&(c[e.trim()]=d.trim())}return c}function C(a){let c=x.get(a);void 0===c&&(c=B(a),x.set(a,c));return c}
|
|
11
|
+
function D(a){let c="";for(let b in a)b&&(c+=`${b}: ${a[b]};`);return c}
|
|
12
|
+
function E(a,c,b){let e=c.getNode();if(k.$isElementNode(e)){var d=e.getDescendantByIndex(c.offset);null!==d&&(e=d)}for(;0<b&&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()&&(f="\n\n");d=f.length;
|
|
13
|
+
if(!k.$isTextNode(e)||b>=d)f=e.getParent(),e.remove(),null==f||0!==f.getChildrenSize()||k.$isRootNode(f)||f.remove(),b-=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-b;let n=f.slice(0,g);null!==h&&h!==f?(b=k.$getPreviousSelection(),d=e,e.isSimpleText()?e.setTextContent(h):(d=k.$createTextNode(h),e.replace(d)),k.$isRangeSelection(b)&&b.isCollapsed()&&(b=b.anchor.offset,d.select(b,b))):
|
|
14
|
+
e.isSimpleText()?(h=c.key===l,f=c.offset,f<b&&(f=d),b=h?f-b:0,d=h?f:g,h&&0===b?([b]=e.splitText(b,d),b.remove()):([,b]=e.splitText(b,d),b.remove())):(b=k.$createTextNode(n),e.replace(b));b=0}}}function F(a,c){let b=C("getStyle"in a?a.getStyle():a.style);c=Object.entries(c).reduce((d,[g,h])=>{"function"===typeof h?d[g]=h(b[g],a):null===h?delete d[g]:d[g]=h;return d},{...b});let e=D(c);a.setStyle(e);x.set(e,c)}
|
|
15
|
+
function G(a){var c=k.$getSelection();if(k.$isRangeSelection(c)){var b=c.getNodes(),e=b.length,{anchor:d,focus:g}=c;c=e-1;e=b[0];var h=b[c],f=e.getTextContent().length,l=g.offset,n=d.offset,p=d.isBefore(g),m=p?n:l,q=p?l:n,r=p?d.type:g.type,t=p?g.type:d.type,u=p?g.key:d.key;if(k.$isTextNode(e)&&m===f){let z=e.getNextSibling();k.$isTextNode(z)&&(m=n=0,e=z)}if(1===b.length)k.$isTextNode(e)&&e.canHaveFormat()&&(m="element"===r?0:n>l?l:n,q="element"===t?f:n>l?n:l,m!==q&&(k.$isTokenOrSegmented(e)||0===
|
|
16
|
+
m&&q===f?(a(e),e.select(m,q)):(b=e.splitText(m,q),b=0===m?b[0]:b[1],a(b),b.select(0,q-m))));else for(k.$isTextNode(e)&&m<e.getTextContentSize()&&e.canHaveFormat()&&(0===m||k.$isTokenOrSegmented(e)||(e=e.splitText(m)[1],m=0,p?d.set(e.getKey(),m,"text"):g.set(e.getKey(),m,"text")),a(e)),k.$isTextNode(h)&&h.canHaveFormat()&&(m=h.getTextContent().length,h.__key!==u&&0!==q&&(q=m),q===m||k.$isTokenOrSegmented(h)||([h]=h.splitText(q)),0===q&&"element"!==t||a(h)),q=1;q<c;q++)m=b[q],t=m.getKey(),k.$isTextNode(m)&&
|
|
17
|
+
m.canHaveFormat()&&t!==e.getKey()&&t!==h.getKey()&&!m.isToken()&&a(m)}}function H(a){for(;null!==a&&!k.$isRootOrShadowRoot(a);){let c=a.getLatest(),b=a.getParent();0===c.getChildrenSize()&&a.remove(!0);a=b}}
|
|
18
|
+
function I(a,c,b,e,d=null){if(0!==c.length){var g=c[0],h=new Map,f=[];g=k.$isElementNode(g)?g:g.getParentOrThrow();g.isInline()&&(g=g.getParentOrThrow());for(var l=!1;null!==g;){var n=g.getPreviousSibling();if(null!==n){g=n;l=!0;break}g=g.getParentOrThrow();if(k.$isRootOrShadowRoot(g))break}n=new Set;for(var p=0;p<b;p++){var m=c[p];k.$isElementNode(m)&&0===m.getChildrenSize()&&n.add(m.getKey())}var q=new Set;for(p=0;p<b;p++){m=c[p];var r=m.getParent();null!==r&&r.isInline()&&(r=r.getParent());if(null!==
|
|
19
|
+
r&&k.$isLeafNode(m)&&!q.has(m.getKey())){if(m=r.getKey(),void 0===h.get(m)){let t=e();t.setFormat(r.getFormatType());t.setIndent(r.getIndent());f.push(t);h.set(m,t);r.getChildren().forEach(u=>{t.append(u);q.add(u.getKey());k.$isElementNode(u)&&u.getChildrenKeys().forEach(z=>q.add(z))});H(r)}}else n.has(m.getKey())&&(k.$isElementNode(m)||v(179),r=e(),r.setFormat(m.getFormatType()),r.setIndent(m.getIndent()),f.push(r),m.remove(!0))}if(null!==d)for(c=0;c<f.length;c++)d.append(f[c]);c=null;if(k.$isRootOrShadowRoot(g))if(l)if(null!==
|
|
20
|
+
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),c=l;else if(null!==d)l.insertBefore(d);else for(g=0;g<f.length;g++)d=f[g],l.insertBefore(d),c=d;else if(d)g.insertAfter(d);else for(d=f.length-1;0<=d;d--)l=f[d],g.insertAfter(l),c=l;f=k.$getPreviousSelection();k.$isRangeSelection(f)&&f.anchor.getNode().isAttached()&&f.focus.getNode().isAttached()?k.$setSelection(f.clone()):
|
|
21
|
+
null!==c?c.selectEnd():a.dirty=!0}}function J(a,c,b,e){a.modify(c?"extend":"move",b,e)}function K(a){a=a.anchor.getNode();return"rtl"===(k.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}function L(a){if(k.$isDecoratorNode(a)||!k.$isElementNode(a)||k.$isRootOrShadowRoot(a))return!1;var c=a.getFirstChild();c=null===c||k.$isLineBreakNode(c)||k.$isTextNode(c)||c.isInline();return!a.isInline()&&!1!==a.canBeEmpty()&&c}exports.$cloneWithProperties=k.$cloneWithProperties;exports.$selectAll=k.$selectAll;
|
|
22
|
+
exports.$addNodeStyle=function(a){a=a.getStyle();let c=B(a);x.set(a,c)};exports.$forEachSelectedTextNode=G;
|
|
23
|
+
exports.$getSelectionStyleValueForProperty=function(a,c,b=""){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(k.$isRangeSelection(a)&&a.isCollapsed()&&""!==a.style&&(a=C(a.style),null!==a&&c in a))return a[c];for(a=0;a<d.length;a++){var n=d[a];if((0===a||0!==l||!n.is(g))&&k.$isTextNode(n))if(h=c,f=b,n=n.getStyle(),n=C(n),h=null!==n?n[h]||f:f,null===e)e=h;else if(e!==h){e="";break}}return null===e?b:e};
|
|
24
|
+
exports.$isAtNodeEnd=function(a){if("text"===a.type)return a.offset===a.getNode().getTextContentSize();let c=a.getNode();k.$isElementNode(c)||v(177);return a.offset===c.getChildrenSize()};exports.$isParentElementRTL=K;exports.$moveCaretSelection=J;exports.$moveCharacter=function(a,c,b){let e=K(a);J(a,c,b?!e:e,"character")};exports.$patchStyleText=function(a,c){a.isCollapsed()&&k.$isRangeSelection(a)?F(a,c):G(b=>{F(b,c)})};
|
|
25
|
+
exports.$setBlocksType=function(a,c){if(null!==a){var b=a.getStartEndPoints();b=b?b[0]:null;if(null!==b&&"root"===b.key)c=c(),a=k.$getRoot(),(b=a.getFirstChild())?b.replace(c,!0):a.append(c);else{a=a.getNodes();if(null!==b){for(b=b.getNode();null!==b&&null!==b.getParent()&&!L(b);)b=b.getParentOrThrow();b=L(b)?b:null}else b=!1;b&&-1===a.indexOf(b)&&a.push(b);for(b=0;b<a.length;b++){let e=a[b];if(!L(e))continue;k.$isElementNode(e)||v(178);let d=c();d.setFormat(e.getFormatType());d.setIndent(e.getIndent());
|
|
26
|
+
e.replace(d,!0)}}}};exports.$shouldOverrideDefaultCharacterSelection=function(a,c){a=k.$getAdjacentNode(a.focus,c);return k.$isDecoratorNode(a)&&!a.isIsolated()||k.$isElementNode(a)&&!a.isInline()&&!a.canBeEmpty()};
|
|
27
|
+
exports.$sliceSelectedTextNodeContent=function(a,c){var b=a.getStartEndPoints();if(c.isSelected(a)&&!c.isSegmented()&&!c.isToken()&&null!==b){let [f,l]=b;b=a.isBackward();var e=f.getNode(),d=l.getNode(),g=c.is(e),h=c.is(d);if(g||h){let [n,p]=k.$getCharacterOffsets(a);a=e.is(d);g=c.is(b?d:e);d=c.is(b?e:d);e=0;h=void 0;a?(e=n>p?p:n,h=n>p?n:p):g?(e=b?p:n,h=void 0):d&&(b=b?n:p,e=0,h=b);c.__text=c.__text.slice(e,h)}}return c};exports.$trimTextContentFromAnchor=E;
|
|
28
|
+
exports.$wrapNodes=function(a,c,b=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=c();f.setFormat(a.getFormatType());f.setIndent(a.getIndent());e.forEach(l=>f.append(l));b&&(f=b.append(f));a.replace(f)}else{d=null;var h=[];for(let f=0;f<g;f++){let l=e[f];k.$isRootOrShadowRoot(l)?(I(a,h,h.length,c,b),h=
|
|
29
|
+
[],d=l):null===d||null!==d&&k.$hasAncestor(l,d)?h.push(l):(I(a,h,h.length,c,b),h=[l])}I(a,h,h.length,c,b)}};
|
|
30
|
+
exports.createDOMRange=function(a,c,b,e,d){let g=c.getKey(),h=e.getKey(),f=document.createRange(),l=a.getElementByKey(g);a=a.getElementByKey(h);k.$isTextNode(c)&&(l=y(l));k.$isTextNode(e)&&(a=y(a));if(void 0===c||void 0===e||null===l||null===a)return null;"BR"===l.nodeName&&([l,b]=A(l));"BR"===a.nodeName&&([a,d]=A(a));c=l.firstChild;l===a&&null!=c&&"BR"===c.nodeName&&0===b&&0===d&&(d=1);try{f.setStart(l,b),f.setEnd(a,d)}catch(n){return null}!f.collapsed||b===d&&g===h||(f.setStart(a,d),f.setEnd(l,
|
|
31
|
+
b));return f};exports.createRectsFromDOMRange=function(a,c){var b=a.getRootElement();if(null===b)return[];a=b.getBoundingClientRect();b=getComputedStyle(b);b=parseFloat(b.paddingLeft)+parseFloat(b.paddingRight);c=Array.from(c.getClientRects());let e=c.length;c.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=c[g],f=h.width+b===a.width;d&&d.top<=h.top&&d.top+d.height>h.top&&d.left+d.width>h.left||f?(c.splice(g--,1),e--):d=h}return c};
|
|
33
32
|
exports.getCSSFromStyleObject=D;exports.getStyleObjectFromCSS=C;exports.trimTextContentFromAnchor=E
|
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import{$isTextNode as e,$getCharacterOffsets as t,$isElementNode as n,$isRootNode as l,$getNodeByKey as o,$getPreviousSelection as r,$createTextNode as
|
|
9
|
+
import{$isTextNode as e,$getCharacterOffsets as t,$isElementNode as n,$isRootNode as l,$getNodeByKey as o,$getPreviousSelection as r,$createTextNode as s,$isRangeSelection as i,$getSelection as c,$isTokenOrSegmented as f,$getRoot as u,$isRootOrShadowRoot as a,$hasAncestor as g,$isLeafNode as d,$setSelection as p,$getAdjacentNode as h,$isDecoratorNode as y,$isLineBreakNode as m}from"lexical";export{$cloneWithProperties,$selectAll}from"lexical";function S(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var x=S((function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));const T=new Map;function v(e){let t=e;for(;null!=t;){if(t.nodeType===Node.TEXT_NODE)return t;t=t.firstChild}return null}function C(e){const t=e.parentNode;if(null==t)throw new Error("Should never happen");return[t,Array.from(t.childNodes).indexOf(e)]}function N(t,n,l,o,r){const s=n.getKey(),i=o.getKey(),c=document.createRange();let f=t.getElementByKey(s),u=t.getElementByKey(i),a=l,g=r;if(e(n)&&(f=v(f)),e(o)&&(u=v(u)),void 0===n||void 0===o||null===f||null===u)return null;"BR"===f.nodeName&&([f,a]=C(f)),"BR"===u.nodeName&&([u,g]=C(u));const d=f.firstChild;f===u&&null!=d&&"BR"===d.nodeName&&0===a&&0===g&&(g=1);try{c.setStart(f,a),c.setEnd(u,g)}catch(e){return null}return!c.collapsed||a===g&&s===i||(c.setStart(u,g),c.setEnd(f,a)),c}function P(e,t){const n=e.getRootElement();if(null===n)return[];const l=n.getBoundingClientRect(),o=getComputedStyle(n),r=parseFloat(o.paddingLeft)+parseFloat(o.paddingRight),s=Array.from(t.getClientRects());let i,c=s.length;s.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=s[e],n=i&&i.top<=t.top&&i.top+i.height>t.top&&i.left+i.width>t.left,o=t.width+r===l.width;n||o?(s.splice(e--,1),c--):i=t}return s}function w(e){const t={};if(!e)return t;const n=e.split(";");for(const e of n)if(""!==e){const[n,l]=e.split(/:([^]+)/);n&&l&&(t[n.trim()]=l.trim())}return t}function E(e){let t=T.get(e);return void 0===t&&(t=w(e),T.set(e,t)),t}function F(e){let t="";for(const n in e)n&&(t+=`${n}: ${e[n]};`);return t}function I(e,n){const l=e.getStartEndPoints();if(n.isSelected(e)&&!n.isSegmented()&&!n.isToken()&&null!==l){const[o,r]=l,s=e.isBackward(),i=o.getNode(),c=r.getNode(),f=n.is(i),u=n.is(c);if(f||u){const[l,o]=t(e),r=i.is(c),f=n.is(s?c:i),u=n.is(s?i:c);let a,g=0;if(r)g=l>o?o:l,a=l>o?l:o;else if(f){g=s?o:l,a=void 0}else if(u){g=0,a=s?l:o}return n.__text=n.__text.slice(g,a),n}}return n}function K(e){if("text"===e.type)return e.offset===e.getNode().getTextContentSize();const t=e.getNode();return n(t)||x(177),e.offset===t.getChildrenSize()}function B(t,c,f){let u=c.getNode(),a=f;if(n(u)){const e=u.getDescendantByIndex(c.offset);null!==e&&(u=e)}for(;a>0&&null!==u;){if(n(u)){const e=u.getLastDescendant();null!==e&&(u=e)}let f=u.getPreviousSibling(),g=0;if(null===f){let e=u.getParentOrThrow(),t=e.getPreviousSibling();for(;null===t;){if(e=e.getParent(),null===e){f=null;break}t=e.getPreviousSibling()}null!==e&&(g=e.isInline()?0:2,f=t)}let d=u.getTextContent();""===d&&n(u)&&!u.isInline()&&(d="\n\n");const p=d.length;if(!e(u)||a>=p){const e=u.getParent();u.remove(),null==e||0!==e.getChildrenSize()||l(e)||e.remove(),a-=p+g,u=f}else{const n=u.getKey(),l=t.getEditorState().read((()=>{const t=o(n);return e(t)&&t.isSimpleText()?t.getTextContent():null})),f=p-a,g=d.slice(0,f);if(null!==l&&l!==d){const e=r();let t=u;if(u.isSimpleText())u.setTextContent(l);else{const e=s(l);u.replace(e),t=e}if(i(e)&&e.isCollapsed()){const n=e.anchor.offset;t.select(n,n)}}else if(u.isSimpleText()){const e=c.key===n;let t=c.offset;t<a&&(t=p);const l=e?t-a:0,o=e?t:f;if(e&&0===l){const[e]=u.splitText(l,o);e.remove()}else{const[,e]=u.splitText(l,o);e.remove()}}else{const e=s(g);u.replace(e)}a=0}}}function k(e){const t=e.getStyle(),n=w(t);T.set(t,n)}function O(e,t){const n=E("getStyle"in e?e.getStyle():e.style),l=Object.entries(t).reduce(((t,[l,o])=>("function"==typeof o?t[l]=o(n[l],e):null===o?delete t[l]:t[l]=o,t)),{...n}||{}),o=F(l);e.setStyle(o),T.set(o,l)}function b(e,t){e.isCollapsed()&&i(e)?O(e,t):R((e=>{O(e,t)}))}function R(t){const n=c();if(!i(n))return;const l=n.getNodes(),o=l.length,{anchor:r,focus:s}=n,u=o-1;let a=l[0],g=l[u];const d=a.getTextContent().length,p=s.offset;let h=r.offset;const y=r.isBefore(s);let m=y?h:p,S=y?p:h;const x=y?r.type:s.type,T=y?s.type:r.type,v=y?s.key:r.key;if(e(a)&&m===d){const t=a.getNextSibling();e(t)&&(h=0,m=0,a=t)}if(1===l.length){if(e(a)&&a.canHaveFormat()){if(m="element"===x?0:h>p?p:h,S="element"===T?d:h>p?h:p,m===S)return;if(f(a)||0===m&&S===d)t(a),a.select(m,S);else{const e=a.splitText(m,S),n=0===m?e[0]:e[1];t(n),n.select(0,S-m)}}}else{if(e(a)&&m<a.getTextContentSize()&&a.canHaveFormat()&&(0===m||f(a)||(a=a.splitText(m)[1],m=0,y?r.set(a.getKey(),m,"text"):s.set(a.getKey(),m,"text")),t(a)),e(g)&&g.canHaveFormat()){const e=g.getTextContent().length;g.__key!==v&&0!==S&&(S=e),S===e||f(g)||([g]=g.splitText(S)),0===S&&"element"!==T||t(g)}for(let n=1;n<u;n++){const o=l[n],r=o.getKey();e(o)&&o.canHaveFormat()&&r!==a.getKey()&&r!==g.getKey()&&!o.isToken()&&t(o)}}}function _(e,t){if(null===e)return;const l=e.getStartEndPoints(),o=l?l[0]:null;if(null!==o&&"root"===o.key){const e=t(),n=u(),l=n.getFirstChild();return void(l?l.replace(e,!0):n.append(e))}const r=e.getNodes(),s=null!==o&&function(e,t){let n=e;for(;null!==n&&null!==n.getParent()&&!t(n);)n=n.getParentOrThrow();return t(n)?n:null}(o.getNode(),X);s&&-1===r.indexOf(s)&&r.push(s);for(let e=0;e<r.length;e++){const l=r[e];if(!X(l))continue;n(l)||x(178);const o=t();o.setFormat(l.getFormatType()),o.setIndent(l.getIndent()),l.replace(o,!0)}}function A(e){return e.getNode().isAttached()}function z(e){let t=e;for(;null!==t&&!a(t);){const e=t.getLatest(),n=t.getParent();0===e.getChildrenSize()&&t.remove(!0),t=n}}function $(e,t,n=null){const l=e.getStartEndPoints(),o=l?l[0]:null,r=e.getNodes(),s=r.length;if(null!==o&&(0===s||1===s&&"element"===o.type&&0===o.getNode().getChildrenSize())){const e="text"===o.type?o.getNode().getParentOrThrow():o.getNode(),l=e.getChildren();let r=t();return r.setFormat(e.getFormatType()),r.setIndent(e.getIndent()),l.forEach((e=>r.append(e))),n&&(r=n.append(r)),void e.replace(r)}let i=null,c=[];for(let l=0;l<s;l++){const o=r[l];a(o)?(L(e,c,c.length,t,n),c=[],i=o):null===i||null!==i&&g(o,i)?c.push(o):(L(e,c,c.length,t,n),c=[o])}L(e,c,c.length,t,n)}function L(e,t,l,o,s=null){if(0===t.length)return;const c=t[0],f=new Map,u=[];let g=n(c)?c:c.getParentOrThrow();g.isInline()&&(g=g.getParentOrThrow());let h=!1;for(;null!==g;){const e=g.getPreviousSibling();if(null!==e){g=e,h=!0;break}if(g=g.getParentOrThrow(),a(g))break}const y=new Set;for(let e=0;e<l;e++){const l=t[e];n(l)&&0===l.getChildrenSize()&&y.add(l.getKey())}const m=new Set;for(let e=0;e<l;e++){const l=t[e];let r=l.getParent();if(null!==r&&r.isInline()&&(r=r.getParent()),null!==r&&d(l)&&!m.has(l.getKey())){const e=r.getKey();if(void 0===f.get(e)){const t=o();t.setFormat(r.getFormatType()),t.setIndent(r.getIndent()),u.push(t),f.set(e,t),r.getChildren().forEach((e=>{t.append(e),m.add(e.getKey()),n(e)&&e.getChildrenKeys().forEach((e=>m.add(e)))})),z(r)}}else if(y.has(l.getKey())){n(l)||x(179);const e=o();e.setFormat(l.getFormatType()),e.setIndent(l.getIndent()),u.push(e),l.remove(!0)}}if(null!==s)for(let e=0;e<u.length;e++){const t=u[e];s.append(t)}let S=null;if(a(g))if(h)if(null!==s)g.insertAfter(s);else for(let e=u.length-1;e>=0;e--){const t=u[e];g.insertAfter(t)}else{const e=g.getFirstChild();if(n(e)&&(g=e),null===e)if(s)g.append(s);else for(let e=0;e<u.length;e++){const t=u[e];g.append(t),S=t}else if(null!==s)e.insertBefore(s);else for(let t=0;t<u.length;t++){const n=u[t];e.insertBefore(n),S=n}}else if(s)g.insertAfter(s);else for(let e=u.length-1;e>=0;e--){const t=u[e];g.insertAfter(t),S=t}const T=r();i(T)&&A(T.anchor)&&A(T.focus)?p(T.clone()):null!==S?S.selectEnd():e.dirty=!0}function M(e,t){const l=h(e.focus,t);return y(l)&&!l.isIsolated()||n(l)&&!l.isInline()&&!l.canBeEmpty()}function D(e,t,n,l){e.modify(t?"extend":"move",n,l)}function H(e){const t=e.anchor.getNode();return"rtl"===(l(t)?t:t.getParentOrThrow()).getDirection()}function j(e,t,n){const l=H(e);D(e,t,n?!l:l,"character")}function U(e,t,n){const l=E(e.getStyle());return null!==l&&l[t]||n}function W(t,n,l=""){let o=null;const r=t.getNodes(),s=t.anchor,c=t.focus,f=t.isBackward(),u=f?c.offset:s.offset,a=f?c.getNode():s.getNode();if(i(t)&&t.isCollapsed()&&""!==t.style){const e=E(t.style);if(null!==e&&n in e)return e[n]}for(let t=0;t<r.length;t++){const s=r[t];if((0===t||0!==u||!s.is(a))&&e(s)){const e=U(s,n,l);if(null===o)o=e;else if(o!==e){o="";break}}}return null===o?l:o}function X(t){if(y(t))return!1;if(!n(t)||a(t))return!1;const l=t.getFirstChild(),o=null===l||m(l)||e(l)||l.isInline();return!t.isInline()&&!1!==t.canBeEmpty()&&o}const q=B;export{k as $addNodeStyle,R as $forEachSelectedTextNode,W as $getSelectionStyleValueForProperty,K as $isAtNodeEnd,H as $isParentElementRTL,D as $moveCaretSelection,j as $moveCharacter,b as $patchStyleText,_ as $setBlocksType,M as $shouldOverrideDefaultCharacterSelection,I as $sliceSelectedTextNodeContent,B as $trimTextContentFromAnchor,$ as $wrapNodes,N as createDOMRange,P as createRectsFromDOMRange,F as getCSSFromStyleObject,E as getStyleObjectFromCSS,q as trimTextContentFromAnchor};
|
package/index.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 { $addNodeStyle, $isAtNodeEnd, $patchStyleText, $sliceSelectedTextNodeContent, $trimTextContentFromAnchor } from './lexical-node';
|
|
8
|
+
import { $addNodeStyle, $forEachSelectedTextNode, $isAtNodeEnd, $patchStyleText, $sliceSelectedTextNodeContent, $trimTextContentFromAnchor } from './lexical-node';
|
|
9
9
|
import { $getSelectionStyleValueForProperty, $isParentElementRTL, $moveCaretSelection, $moveCharacter, $setBlocksType, $shouldOverrideDefaultCharacterSelection, $wrapNodes } from './range-selection';
|
|
10
10
|
import { createDOMRange, createRectsFromDOMRange, getCSSFromStyleObject, getStyleObjectFromCSS } from './utils';
|
|
11
11
|
export {
|
|
12
12
|
/** @deprecated moved to the lexical package */ $cloneWithProperties,
|
|
13
13
|
/** @deprecated moved to the lexical package */ $selectAll, } from 'lexical';
|
|
14
|
-
export { $addNodeStyle, $isAtNodeEnd, $patchStyleText, $sliceSelectedTextNodeContent, $trimTextContentFromAnchor, };
|
|
14
|
+
export { $addNodeStyle, $forEachSelectedTextNode, $isAtNodeEnd, $patchStyleText, $sliceSelectedTextNodeContent, $trimTextContentFromAnchor, };
|
|
15
15
|
/** @deprecated renamed to {@link $trimTextContentFromAnchor} by @lexical/eslint-plugin rules-of-lexical */
|
|
16
16
|
export declare const trimTextContentFromAnchor: typeof $trimTextContentFromAnchor;
|
|
17
17
|
export { $getSelectionStyleValueForProperty, $isParentElementRTL, $moveCaretSelection, $moveCharacter, $setBlocksType, $shouldOverrideDefaultCharacterSelection, $wrapNodes, };
|
package/lexical-node.d.ts
CHANGED
|
@@ -42,3 +42,4 @@ export declare function $addNodeStyle(node: TextNode): void;
|
|
|
42
42
|
* @param patch - The patch to apply, which can include multiple styles. \\{CSSProperty: value\\} . Can also accept a function that returns the new property value.
|
|
43
43
|
*/
|
|
44
44
|
export declare function $patchStyleText(selection: BaseSelection, patch: Record<string, string | null | ((currentStyleValue: string | null, target: TextNode | RangeSelection) => string)>): void;
|
|
45
|
+
export declare function $forEachSelectedTextNode(fn: (textNode: TextNode) => void): void;
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"selection"
|
|
10
10
|
],
|
|
11
11
|
"license": "MIT",
|
|
12
|
-
"version": "0.
|
|
12
|
+
"version": "0.22.0",
|
|
13
13
|
"main": "LexicalSelection.js",
|
|
14
14
|
"types": "index.d.ts",
|
|
15
15
|
"repository": {
|
|
@@ -37,6 +37,6 @@
|
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"lexical": "0.
|
|
40
|
+
"lexical": "0.22.0"
|
|
41
41
|
}
|
|
42
42
|
}
|