@lexical/selection 0.14.2 → 0.14.4
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 +7 -0
- package/{LexicalSelection.dev.esm.js → LexicalSelection.dev.mjs} +8 -1
- package/{LexicalSelection.esm.js → LexicalSelection.mjs} +2 -2
- package/LexicalSelection.node.mjs +24 -0
- package/LexicalSelection.prod.js +2 -2
- package/LexicalSelection.prod.mjs +7 -0
- package/index.d.ts +0 -1
- package/package.json +24 -6
- package/LexicalSelection.prod.esm.js +0 -7
package/LexicalSelection.dev.js
CHANGED
|
@@ -210,6 +210,10 @@ function $updateTextNodeProperties(target, source) {
|
|
|
210
210
|
target.__detail = source.__detail;
|
|
211
211
|
return target;
|
|
212
212
|
}
|
|
213
|
+
function $updateParagraphNodeProperties(target, source) {
|
|
214
|
+
target.__textFormat = source.__textFormat;
|
|
215
|
+
return target;
|
|
216
|
+
}
|
|
213
217
|
|
|
214
218
|
/**
|
|
215
219
|
* Returns a copy of a node, but generates a new key for the copy.
|
|
@@ -229,6 +233,9 @@ function $cloneWithProperties(node) {
|
|
|
229
233
|
if (lexical.$isTextNode(node) && lexical.$isTextNode(clone)) {
|
|
230
234
|
return $updateTextNodeProperties(clone, node);
|
|
231
235
|
}
|
|
236
|
+
if (lexical.$isParagraphNode(node) && lexical.$isParagraphNode(clone)) {
|
|
237
|
+
return $updateParagraphNodeProperties(clone, node);
|
|
238
|
+
}
|
|
232
239
|
return clone;
|
|
233
240
|
}
|
|
234
241
|
|
|
@@ -4,7 +4,7 @@
|
|
|
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
|
-
import { $isTextNode, $isElementNode, $getCharacterOffsets, $isRootNode, $getNodeByKey, $getPreviousSelection, $createTextNode, $isRangeSelection, $getRoot, $isRootOrShadowRoot, $hasAncestor, $isLeafNode, $setSelection, $getAdjacentNode, $isDecoratorNode, $isLineBreakNode } from 'lexical';
|
|
7
|
+
import { $isTextNode, $isElementNode, $isParagraphNode, $getCharacterOffsets, $isRootNode, $getNodeByKey, $getPreviousSelection, $createTextNode, $isRangeSelection, $getRoot, $isRootOrShadowRoot, $hasAncestor, $isLeafNode, $setSelection, $getAdjacentNode, $isDecoratorNode, $isLineBreakNode } from 'lexical';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
@@ -208,6 +208,10 @@ function $updateTextNodeProperties(target, source) {
|
|
|
208
208
|
target.__detail = source.__detail;
|
|
209
209
|
return target;
|
|
210
210
|
}
|
|
211
|
+
function $updateParagraphNodeProperties(target, source) {
|
|
212
|
+
target.__textFormat = source.__textFormat;
|
|
213
|
+
return target;
|
|
214
|
+
}
|
|
211
215
|
|
|
212
216
|
/**
|
|
213
217
|
* Returns a copy of a node, but generates a new key for the copy.
|
|
@@ -227,6 +231,9 @@ function $cloneWithProperties(node) {
|
|
|
227
231
|
if ($isTextNode(node) && $isTextNode(clone)) {
|
|
228
232
|
return $updateTextNodeProperties(clone, node);
|
|
229
233
|
}
|
|
234
|
+
if ($isParagraphNode(node) && $isParagraphNode(clone)) {
|
|
235
|
+
return $updateParagraphNodeProperties(clone, node);
|
|
236
|
+
}
|
|
230
237
|
return clone;
|
|
231
238
|
}
|
|
232
239
|
|
|
@@ -4,8 +4,8 @@
|
|
|
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
|
-
import * as modDev from './LexicalSelection.dev.
|
|
8
|
-
import * as modProd from './LexicalSelection.prod.
|
|
7
|
+
import * as modDev from './LexicalSelection.dev.mjs';
|
|
8
|
+
import * as modProd from './LexicalSelection.prod.mjs';
|
|
9
9
|
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
10
10
|
export const $addNodeStyle = mod.$addNodeStyle;
|
|
11
11
|
export const $cloneWithProperties = mod.$cloneWithProperties;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
const mod = await (process.env.NODE_ENV === 'development' ? import('./LexicalSelection.dev.mjs') : import('./LexicalSelection.prod.mjs'));
|
|
8
|
+
export const $addNodeStyle = mod.$addNodeStyle;
|
|
9
|
+
export const $cloneWithProperties = mod.$cloneWithProperties;
|
|
10
|
+
export const $getSelectionStyleValueForProperty = mod.$getSelectionStyleValueForProperty;
|
|
11
|
+
export const $isAtNodeEnd = mod.$isAtNodeEnd;
|
|
12
|
+
export const $isParentElementRTL = mod.$isParentElementRTL;
|
|
13
|
+
export const $moveCaretSelection = mod.$moveCaretSelection;
|
|
14
|
+
export const $moveCharacter = mod.$moveCharacter;
|
|
15
|
+
export const $patchStyleText = mod.$patchStyleText;
|
|
16
|
+
export const $selectAll = mod.$selectAll;
|
|
17
|
+
export const $setBlocksType = mod.$setBlocksType;
|
|
18
|
+
export const $shouldOverrideDefaultCharacterSelection = mod.$shouldOverrideDefaultCharacterSelection;
|
|
19
|
+
export const $sliceSelectedTextNodeContent = mod.$sliceSelectedTextNodeContent;
|
|
20
|
+
export const $wrapNodes = mod.$wrapNodes;
|
|
21
|
+
export const createDOMRange = mod.createDOMRange;
|
|
22
|
+
export const createRectsFromDOMRange = mod.createRectsFromDOMRange;
|
|
23
|
+
export const getStyleObjectFromCSS = mod.getStyleObjectFromCSS;
|
|
24
|
+
export const trimTextContentFromAnchor = mod.trimTextContentFromAnchor;
|
package/LexicalSelection.prod.js
CHANGED
|
@@ -11,8 +11,8 @@ r&&k.$isLeafNode(q)&&!n.has(q.getKey())){if(q=r.getKey(),void 0===h.get(q)){let
|
|
|
11
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
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
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)};
|
|
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)
|
|
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};
|
|
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;if(k.$isTextNode(a)&&k.$isTextNode(b))return b.__format=a.__format,b.__style=a.__style,b.__mode=a.__mode,b.__detail=a.__detail,b;k.$isParagraphNode(a)&&k.$isParagraphNode(b)&&(b.__textFormat=a.__textFormat);
|
|
15
|
+
return b};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
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
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
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()&&
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import{$isTextNode as e,$isElementNode as t,$isParagraphNode as n,$getCharacterOffsets as o,$isRootNode as l,$getNodeByKey as r,$getPreviousSelection as s,$createTextNode as i,$isRangeSelection as c,$getRoot as f,$isRootOrShadowRoot as u,$hasAncestor as g,$isLeafNode as a,$setSelection as d,$getAdjacentNode as p,$isDecoratorNode as h,$isLineBreakNode as _}from"lexical";const m=new Map;function y(e){let t=e;for(;null!=t;){if(t.nodeType===Node.TEXT_NODE)return t;t=t.firstChild}return null}function T(e){const t=e.parentNode;if(null==t)throw new Error("Should never happen");return[t,Array.from(t.childNodes).indexOf(e)]}function x(t,n,o,l,r){const s=n.getKey(),i=l.getKey(),c=document.createRange();let f=t.getElementByKey(s),u=t.getElementByKey(i),g=o,a=r;if(e(n)&&(f=y(f)),e(l)&&(u=y(u)),void 0===n||void 0===l||null===f||null===u)return null;"BR"===f.nodeName&&([f,g]=T(f)),"BR"===u.nodeName&&([u,a]=T(u));const d=f.firstChild;f===u&&null!=d&&"BR"===d.nodeName&&0===g&&0===a&&(a=1);try{c.setStart(f,g),c.setEnd(u,a)}catch(e){return null}return!c.collapsed||g===a&&s===i||(c.setStart(u,a),c.setEnd(f,g)),c}function S(e,t){const n=e.getRootElement();if(null===n)return[];const o=n.getBoundingClientRect(),l=getComputedStyle(n),r=parseFloat(l.paddingLeft)+parseFloat(l.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,l=t.width+r===o.width;n||l?(s.splice(e--,1),c--):i=t}return s}function N(e){const t={},n=e.split(";");for(const e of n)if(""!==e){const[n,o]=e.split(/:([^]+)/);n&&o&&(t[n.trim()]=o.trim())}return t}function E(e){let t=m.get(e);return void 0===t&&(t=N(e),m.set(e,t)),t}function v(o){const l=o.constructor.clone(o);return l.__parent=o.__parent,l.__next=o.__next,l.__prev=o.__prev,t(o)&&t(l)?(s=o,(r=l).__first=s.__first,r.__last=s.__last,r.__size=s.__size,r.__format=s.__format,r.__indent=s.__indent,r.__dir=s.__dir,r):e(o)&&e(l)?function(e,t){return e.__format=t.__format,e.__style=t.__style,e.__mode=t.__mode,e.__detail=t.__detail,e}(l,o):n(o)&&n(l)?function(e,t){return e.__textFormat=t.__textFormat,e}(l,o):l;var r,s}function C(e,t){const n=e.getStartEndPoints();if(t.isSelected(e)&&!t.isSegmented()&&!t.isToken()&&null!==n){const[l,r]=n,s=e.isBackward(),i=l.getNode(),c=r.getNode(),f=t.is(i),u=t.is(c);if(f||u){const[n,l]=o(e),r=i.is(c),f=t.is(s?c:i),u=t.is(s?i:c);let g,a=0;if(r)a=n>l?l:n,g=n>l?n:l;else if(f){a=s?l:n,g=void 0}else if(u){a=0,g=s?n:l}return t.__text=t.__text.slice(a,g),t}}return t}function w(e){if("text"===e.type)return e.offset===e.getNode().getTextContentSize();const n=e.getNode();if(!t(n))throw Error("isAtNodeEnd: node must be a TextNode or ElementNode");return e.offset===n.getChildrenSize()}function P(n,o,f){let u=o.getNode(),g=f;if(t(u)){const e=u.getDescendantByIndex(o.offset);null!==e&&(u=e)}for(;g>0&&null!==u;){if(t(u)){const e=u.getLastDescendant();null!==e&&(u=e)}let f=u.getPreviousSibling(),a=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&&(a=e.isInline()?0:2,f=t)}let d=u.getTextContent();""===d&&t(u)&&!u.isInline()&&(d="\n\n");const p=d.length;if(!e(u)||g>=p){const e=u.getParent();u.remove(),null==e||0!==e.getChildrenSize()||l(e)||e.remove(),g-=p+a,u=f}else{const t=u.getKey(),l=n.getEditorState().read((()=>{const n=r(t);return e(n)&&n.isSimpleText()?n.getTextContent():null})),f=p-g,a=d.slice(0,f);if(null!==l&&l!==d){const e=s();let t=u;if(u.isSimpleText())u.setTextContent(l);else{const e=i(l);u.replace(e),t=e}if(c(e)&&e.isCollapsed()){const n=e.anchor.offset;t.select(n,n)}}else if(u.isSimpleText()){const e=o.key===t;let n=o.offset;n<g&&(n=p);const l=e?n-g:0,r=e?n:f;if(e&&0===l){const[e]=u.splitText(l,r);e.remove()}else{const[,e]=u.splitText(l,r);e.remove()}}else{const e=i(a);u.replace(e)}g=0}}}function F(e){const t=e.getStyle(),n=N(t);m.set(t,n)}function I(e,t){const n=E("getStyle"in e?e.getStyle():e.style),o=Object.entries(t).reduce(((e,[t,o])=>(o instanceof Function?e[t]=o(n[t]):null===o?delete e[t]:e[t]=o,e)),{...n}||{}),l=function(e){let t="";for(const n in e)n&&(t+=`${n}: ${e[n]};`);return t}(o);e.setStyle(l),m.set(l,o)}function K(t,n){const o=t.getNodes(),l=o.length,r=t.getStartEndPoints();if(null===r)return;const[s,i]=r,f=l-1;let u=o[0],g=o[f];if(t.isCollapsed()&&c(t))return void I(t,n);const a=u.getTextContent().length,d=i.offset;let p=s.offset;const h=s.isBefore(i);let _=h?p:d,m=h?d:p;const y=h?s.type:i.type,T=h?i.type:s.type,x=h?i.key:s.key;if(e(u)&&_===a){const t=u.getNextSibling();e(t)&&(p=0,_=0,u=t)}if(1===o.length){if(e(u)&&u.canHaveFormat()){if(_="element"===y?0:p>d?d:p,m="element"===T?a:p>d?p:d,_===m)return;if(0===_&&m===a)I(u,n),u.select(_,m);else{const e=u.splitText(_,m),t=0===_?e[0]:e[1];I(t,n),t.select(0,m-_)}}}else{if(e(u)&&_<u.getTextContentSize()&&u.canHaveFormat()&&(0!==_&&(u=u.splitText(_)[1],_=0,s.set(u.getKey(),_,"text")),I(u,n)),e(g)&&g.canHaveFormat()){const e=g.getTextContent().length;g.__key!==x&&0!==m&&(m=e),m!==e&&([g]=g.splitText(m)),0===m&&"element"!==T||I(g,n)}for(let t=1;t<f;t++){const l=o[t],r=l.getKey();e(l)&&l.canHaveFormat()&&r!==u.getKey()&&r!==g.getKey()&&!l.isToken()&&I(l,n)}}}function b(e,n){if(null===e)return;const o=e.getStartEndPoints(),l=o?o[0]:null;if(null!==l&&"root"===l.key){const e=n(),t=f(),o=t.getFirstChild();return void(o?o.replace(e,!0):t.append(e))}const r=e.getNodes(),s=null!==l&&function(e,t){let n=e;for(;null!==n&&null!==n.getParent()&&!t(n);)n=n.getParentOrThrow();return t(n)?n:null}(l.getNode(),j);s&&-1===r.indexOf(s)&&r.push(s);for(let e=0;e<r.length;e++){const o=r[e];if(!j(o))continue;if(!t(o))throw Error("Expected block node to be an ElementNode");const l=n();l.setFormat(o.getFormatType()),l.setIndent(o.getIndent()),o.replace(l,!0)}}function O(e){return e.getNode().isAttached()}function k(e){let t=e;for(;null!==t&&!u(t);){const e=t.getLatest(),n=t.getParent();0===e.getChildrenSize()&&t.remove(!0),t=n}}function B(e,t,n=null){const o=e.getStartEndPoints(),l=o?o[0]:null,r=e.getNodes(),s=r.length;if(null!==l&&(0===s||1===s&&"element"===l.type&&0===l.getNode().getChildrenSize())){const e="text"===l.type?l.getNode().getParentOrThrow():l.getNode(),o=e.getChildren();let r=t();return r.setFormat(e.getFormatType()),r.setIndent(e.getIndent()),o.forEach((e=>r.append(e))),n&&(r=n.append(r)),void e.replace(r)}let i=null,c=[];for(let o=0;o<s;o++){const l=r[o];u(l)?(z(e,c,c.length,t,n),c=[],i=l):null===i||null!==i&&g(l,i)?c.push(l):(z(e,c,c.length,t,n),c=[l])}z(e,c,c.length,t,n)}function z(e,n,o,l,r=null){if(0===n.length)return;const i=n[0],f=new Map,g=[];let p=t(i)?i:i.getParentOrThrow();p.isInline()&&(p=p.getParentOrThrow());let h=!1;for(;null!==p;){const e=p.getPreviousSibling();if(null!==e){p=e,h=!0;break}if(p=p.getParentOrThrow(),u(p))break}const _=new Set;for(let e=0;e<o;e++){const o=n[e];t(o)&&0===o.getChildrenSize()&&_.add(o.getKey())}const m=new Set;for(let e=0;e<o;e++){const o=n[e];let r=o.getParent();if(null!==r&&r.isInline()&&(r=r.getParent()),null!==r&&a(o)&&!m.has(o.getKey())){const e=r.getKey();if(void 0===f.get(e)){const n=l();n.setFormat(r.getFormatType()),n.setIndent(r.getIndent()),g.push(n),f.set(e,n),r.getChildren().forEach((e=>{n.append(e),m.add(e.getKey()),t(e)&&e.getChildrenKeys().forEach((e=>m.add(e)))})),k(r)}}else if(_.has(o.getKey())){if(!t(o))throw Error("Expected node in emptyElements to be an ElementNode");const e=l();e.setFormat(o.getFormatType()),e.setIndent(o.getIndent()),g.push(e),o.remove(!0)}}if(null!==r)for(let e=0;e<g.length;e++){const t=g[e];r.append(t)}let y=null;if(u(p))if(h)if(null!==r)p.insertAfter(r);else for(let e=g.length-1;e>=0;e--){const t=g[e];p.insertAfter(t)}else{const e=p.getFirstChild();if(t(e)&&(p=e),null===e)if(r)p.append(r);else for(let e=0;e<g.length;e++){const t=g[e];p.append(t),y=t}else if(null!==r)e.insertBefore(r);else for(let t=0;t<g.length;t++){const n=g[t];e.insertBefore(n),y=n}}else if(r)p.insertAfter(r);else for(let e=g.length-1;e>=0;e--){const t=g[e];p.insertAfter(t),y=t}const T=s();c(T)&&O(T.anchor)&&O(T.focus)?d(T.clone()):null!==y?y.selectEnd():e.dirty=!0}function A(e,n){const o=p(e.focus,n);return h(o)&&!o.isIsolated()||t(o)&&!o.isInline()&&!o.canBeEmpty()}function R(e,t,n,o){e.modify(t?"extend":"move",n,o)}function D(e){const t=e.anchor.getNode();return"rtl"===(l(t)?t:t.getParentOrThrow()).getDirection()}function L(e,t,n){const o=D(e);R(e,t,n?!o:o,"character")}function H(n){const o=n.anchor,l=n.focus,r=o.getNode().getTopLevelElementOrThrow().getParentOrThrow();let s=r.getFirstDescendant(),i=r.getLastDescendant(),c="element",f="element",u=0;e(s)?c="text":t(s)||null===s||(s=s.getParentOrThrow()),e(i)?(f="text",u=i.getTextContentSize()):t(i)||null===i||(i=i.getParentOrThrow()),s&&i&&(o.set(s.getKey(),0,c),l.set(i.getKey(),u,f))}function M(e,t,n){const o=E(e.getStyle());return null!==o&&o[t]||n}function $(t,n,o=""){let l=null;const r=t.getNodes(),s=t.anchor,i=t.focus,c=t.isBackward(),f=c?i.offset:s.offset,u=c?i.getNode():s.getNode();if(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!==f||!s.is(u))&&e(s)){const e=M(s,n,o);if(null===l)l=e;else if(l!==e){l="";break}}}return null===l?o:l}function j(n){if(h(n))return!1;if(!t(n)||u(n))return!1;const o=n.getFirstChild(),l=null===o||_(o)||e(o)||o.isInline();return!n.isInline()&&!1!==n.canBeEmpty()&&l}export{F as $addNodeStyle,v as $cloneWithProperties,$ as $getSelectionStyleValueForProperty,w as $isAtNodeEnd,D as $isParentElementRTL,R as $moveCaretSelection,L as $moveCharacter,K as $patchStyleText,H as $selectAll,b as $setBlocksType,A as $shouldOverrideDefaultCharacterSelection,C as $sliceSelectedTextNodeContent,B as $wrapNodes,x as createDOMRange,S as createRectsFromDOMRange,E as getStyleObjectFromCSS,P as trimTextContentFromAnchor};
|
package/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -9,16 +9,34 @@
|
|
|
9
9
|
"selection"
|
|
10
10
|
],
|
|
11
11
|
"license": "MIT",
|
|
12
|
-
"version": "0.14.
|
|
12
|
+
"version": "0.14.4",
|
|
13
13
|
"main": "LexicalSelection.js",
|
|
14
|
-
"
|
|
15
|
-
"lexical": "0.14.2"
|
|
16
|
-
},
|
|
14
|
+
"types": "index.d.ts",
|
|
17
15
|
"repository": {
|
|
18
16
|
"type": "git",
|
|
19
17
|
"url": "https://github.com/facebook/lexical",
|
|
20
18
|
"directory": "packages/lexical-selection"
|
|
21
19
|
},
|
|
22
|
-
"module": "LexicalSelection.
|
|
23
|
-
"sideEffects": false
|
|
20
|
+
"module": "LexicalSelection.mjs",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": {
|
|
25
|
+
"types": "./index.d.ts",
|
|
26
|
+
"development": "./LexicalSelection.dev.mjs",
|
|
27
|
+
"production": "./LexicalSelection.prod.mjs",
|
|
28
|
+
"node": "./LexicalSelection.node.mjs",
|
|
29
|
+
"default": "./LexicalSelection.mjs"
|
|
30
|
+
},
|
|
31
|
+
"require": {
|
|
32
|
+
"types": "./index.d.ts",
|
|
33
|
+
"development": "./LexicalSelection.dev.js",
|
|
34
|
+
"production": "./LexicalSelection.prod.js",
|
|
35
|
+
"default": "./LexicalSelection.js"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"lexical": "0.14.4"
|
|
41
|
+
}
|
|
24
42
|
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
import{$isTextNode as e,$isElementNode as t,$getCharacterOffsets as n,$isRootNode as o,$getNodeByKey as l,$getPreviousSelection as r,$createTextNode as s,$isRangeSelection as i,$getRoot as c,$isRootOrShadowRoot as f,$hasAncestor as u,$isLeafNode as g,$setSelection as a,$getAdjacentNode as d,$isDecoratorNode as p,$isLineBreakNode as h}from"lexical";const m=new Map;function _(e){let t=e;for(;null!=t;){if(t.nodeType===Node.TEXT_NODE)return t;t=t.firstChild}return null}function y(e){const t=e.parentNode;if(null==t)throw new Error("Should never happen");return[t,Array.from(t.childNodes).indexOf(e)]}function T(t,n,o,l,r){const s=n.getKey(),i=l.getKey(),c=document.createRange();let f=t.getElementByKey(s),u=t.getElementByKey(i),g=o,a=r;if(e(n)&&(f=_(f)),e(l)&&(u=_(u)),void 0===n||void 0===l||null===f||null===u)return null;"BR"===f.nodeName&&([f,g]=y(f)),"BR"===u.nodeName&&([u,a]=y(u));const d=f.firstChild;f===u&&null!=d&&"BR"===d.nodeName&&0===g&&0===a&&(a=1);try{c.setStart(f,g),c.setEnd(u,a)}catch(e){return null}return!c.collapsed||g===a&&s===i||(c.setStart(u,a),c.setEnd(f,g)),c}function x(e,t){const n=e.getRootElement();if(null===n)return[];const o=n.getBoundingClientRect(),l=getComputedStyle(n),r=parseFloat(l.paddingLeft)+parseFloat(l.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,l=t.width+r===o.width;n||l?(s.splice(e--,1),c--):i=t}return s}function S(e){const t={},n=e.split(";");for(const e of n)if(""!==e){const[n,o]=e.split(/:([^]+)/);n&&o&&(t[n.trim()]=o.trim())}return t}function N(e){let t=m.get(e);return void 0===t&&(t=S(e),m.set(e,t)),t}function E(n){const o=n.constructor.clone(n);return o.__parent=n.__parent,o.__next=n.__next,o.__prev=n.__prev,t(n)&&t(o)?(r=n,(l=o).__first=r.__first,l.__last=r.__last,l.__size=r.__size,l.__format=r.__format,l.__indent=r.__indent,l.__dir=r.__dir,l):e(n)&&e(o)?function(e,t){return e.__format=t.__format,e.__style=t.__style,e.__mode=t.__mode,e.__detail=t.__detail,e}(o,n):o;var l,r}function v(e,t){const o=e.getStartEndPoints();if(t.isSelected(e)&&!t.isSegmented()&&!t.isToken()&&null!==o){const[l,r]=o,s=e.isBackward(),i=l.getNode(),c=r.getNode(),f=t.is(i),u=t.is(c);if(f||u){const[o,l]=n(e),r=i.is(c),f=t.is(s?c:i),u=t.is(s?i:c);let g,a=0;if(r)a=o>l?l:o,g=o>l?o:l;else if(f){a=s?l:o,g=void 0}else if(u){a=0,g=s?o:l}return t.__text=t.__text.slice(a,g),t}}return t}function C(e){if("text"===e.type)return e.offset===e.getNode().getTextContentSize();const n=e.getNode();if(!t(n))throw Error("isAtNodeEnd: node must be a TextNode or ElementNode");return e.offset===n.getChildrenSize()}function w(n,c,f){let u=c.getNode(),g=f;if(t(u)){const e=u.getDescendantByIndex(c.offset);null!==e&&(u=e)}for(;g>0&&null!==u;){if(t(u)){const e=u.getLastDescendant();null!==e&&(u=e)}let f=u.getPreviousSibling(),a=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&&(a=e.isInline()?0:2,f=t)}let d=u.getTextContent();""===d&&t(u)&&!u.isInline()&&(d="\n\n");const p=d.length;if(!e(u)||g>=p){const e=u.getParent();u.remove(),null==e||0!==e.getChildrenSize()||o(e)||e.remove(),g-=p+a,u=f}else{const t=u.getKey(),o=n.getEditorState().read((()=>{const n=l(t);return e(n)&&n.isSimpleText()?n.getTextContent():null})),f=p-g,a=d.slice(0,f);if(null!==o&&o!==d){const e=r();let t=u;if(u.isSimpleText())u.setTextContent(o);else{const e=s(o);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===t;let n=c.offset;n<g&&(n=p);const o=e?n-g:0,l=e?n:f;if(e&&0===o){const[e]=u.splitText(o,l);e.remove()}else{const[,e]=u.splitText(o,l);e.remove()}}else{const e=s(a);u.replace(e)}g=0}}}function P(e){const t=e.getStyle(),n=S(t);m.set(t,n)}function F(e,t){const n=N("getStyle"in e?e.getStyle():e.style),o=Object.entries(t).reduce(((e,[t,o])=>(o instanceof Function?e[t]=o(n[t]):null===o?delete e[t]:e[t]=o,e)),{...n}||{}),l=function(e){let t="";for(const n in e)n&&(t+=`${n}: ${e[n]};`);return t}(o);e.setStyle(l),m.set(l,o)}function I(t,n){const o=t.getNodes(),l=o.length,r=t.getStartEndPoints();if(null===r)return;const[s,c]=r,f=l-1;let u=o[0],g=o[f];if(t.isCollapsed()&&i(t))return void F(t,n);const a=u.getTextContent().length,d=c.offset;let p=s.offset;const h=s.isBefore(c);let m=h?p:d,_=h?d:p;const y=h?s.type:c.type,T=h?c.type:s.type,x=h?c.key:s.key;if(e(u)&&m===a){const t=u.getNextSibling();e(t)&&(p=0,m=0,u=t)}if(1===o.length){if(e(u)&&u.canHaveFormat()){if(m="element"===y?0:p>d?d:p,_="element"===T?a:p>d?p:d,m===_)return;if(0===m&&_===a)F(u,n),u.select(m,_);else{const e=u.splitText(m,_),t=0===m?e[0]:e[1];F(t,n),t.select(0,_-m)}}}else{if(e(u)&&m<u.getTextContentSize()&&u.canHaveFormat()&&(0!==m&&(u=u.splitText(m)[1],m=0,s.set(u.getKey(),m,"text")),F(u,n)),e(g)&&g.canHaveFormat()){const e=g.getTextContent().length;g.__key!==x&&0!==_&&(_=e),_!==e&&([g]=g.splitText(_)),0===_&&"element"!==T||F(g,n)}for(let t=1;t<f;t++){const l=o[t],r=l.getKey();e(l)&&l.canHaveFormat()&&r!==u.getKey()&&r!==g.getKey()&&!l.isToken()&&F(l,n)}}}function K(e,n){if(null===e)return;const o=e.getStartEndPoints(),l=o?o[0]:null;if(null!==l&&"root"===l.key){const e=n(),t=c(),o=t.getFirstChild();return void(o?o.replace(e,!0):t.append(e))}const r=e.getNodes(),s=null!==l&&function(e,t){let n=e;for(;null!==n&&null!==n.getParent()&&!t(n);)n=n.getParentOrThrow();return t(n)?n:null}(l.getNode(),$);s&&-1===r.indexOf(s)&&r.push(s);for(let e=0;e<r.length;e++){const o=r[e];if(!$(o))continue;if(!t(o))throw Error("Expected block node to be an ElementNode");const l=n();l.setFormat(o.getFormatType()),l.setIndent(o.getIndent()),o.replace(l,!0)}}function b(e){return e.getNode().isAttached()}function O(e){let t=e;for(;null!==t&&!f(t);){const e=t.getLatest(),n=t.getParent();0===e.getChildrenSize()&&t.remove(!0),t=n}}function k(e,t,n=null){const o=e.getStartEndPoints(),l=o?o[0]:null,r=e.getNodes(),s=r.length;if(null!==l&&(0===s||1===s&&"element"===l.type&&0===l.getNode().getChildrenSize())){const e="text"===l.type?l.getNode().getParentOrThrow():l.getNode(),o=e.getChildren();let r=t();return r.setFormat(e.getFormatType()),r.setIndent(e.getIndent()),o.forEach((e=>r.append(e))),n&&(r=n.append(r)),void e.replace(r)}let i=null,c=[];for(let o=0;o<s;o++){const l=r[o];f(l)?(B(e,c,c.length,t,n),c=[],i=l):null===i||null!==i&&u(l,i)?c.push(l):(B(e,c,c.length,t,n),c=[l])}B(e,c,c.length,t,n)}function B(e,n,o,l,s=null){if(0===n.length)return;const c=n[0],u=new Map,d=[];let p=t(c)?c:c.getParentOrThrow();p.isInline()&&(p=p.getParentOrThrow());let h=!1;for(;null!==p;){const e=p.getPreviousSibling();if(null!==e){p=e,h=!0;break}if(p=p.getParentOrThrow(),f(p))break}const m=new Set;for(let e=0;e<o;e++){const o=n[e];t(o)&&0===o.getChildrenSize()&&m.add(o.getKey())}const _=new Set;for(let e=0;e<o;e++){const o=n[e];let r=o.getParent();if(null!==r&&r.isInline()&&(r=r.getParent()),null!==r&&g(o)&&!_.has(o.getKey())){const e=r.getKey();if(void 0===u.get(e)){const n=l();n.setFormat(r.getFormatType()),n.setIndent(r.getIndent()),d.push(n),u.set(e,n),r.getChildren().forEach((e=>{n.append(e),_.add(e.getKey()),t(e)&&e.getChildrenKeys().forEach((e=>_.add(e)))})),O(r)}}else if(m.has(o.getKey())){if(!t(o))throw Error("Expected node in emptyElements to be an ElementNode");const e=l();e.setFormat(o.getFormatType()),e.setIndent(o.getIndent()),d.push(e),o.remove(!0)}}if(null!==s)for(let e=0;e<d.length;e++){const t=d[e];s.append(t)}let y=null;if(f(p))if(h)if(null!==s)p.insertAfter(s);else for(let e=d.length-1;e>=0;e--){const t=d[e];p.insertAfter(t)}else{const e=p.getFirstChild();if(t(e)&&(p=e),null===e)if(s)p.append(s);else for(let e=0;e<d.length;e++){const t=d[e];p.append(t),y=t}else if(null!==s)e.insertBefore(s);else for(let t=0;t<d.length;t++){const n=d[t];e.insertBefore(n),y=n}}else if(s)p.insertAfter(s);else for(let e=d.length-1;e>=0;e--){const t=d[e];p.insertAfter(t),y=t}const T=r();i(T)&&b(T.anchor)&&b(T.focus)?a(T.clone()):null!==y?y.selectEnd():e.dirty=!0}function z(e,n){const o=d(e.focus,n);return p(o)&&!o.isIsolated()||t(o)&&!o.isInline()&&!o.canBeEmpty()}function A(e,t,n,o){e.modify(t?"extend":"move",n,o)}function R(e){const t=e.anchor.getNode();return"rtl"===(o(t)?t:t.getParentOrThrow()).getDirection()}function D(e,t,n){const o=R(e);A(e,t,n?!o:o,"character")}function L(n){const o=n.anchor,l=n.focus,r=o.getNode().getTopLevelElementOrThrow().getParentOrThrow();let s=r.getFirstDescendant(),i=r.getLastDescendant(),c="element",f="element",u=0;e(s)?c="text":t(s)||null===s||(s=s.getParentOrThrow()),e(i)?(f="text",u=i.getTextContentSize()):t(i)||null===i||(i=i.getParentOrThrow()),s&&i&&(o.set(s.getKey(),0,c),l.set(i.getKey(),u,f))}function H(e,t,n){const o=N(e.getStyle());return null!==o&&o[t]||n}function M(t,n,o=""){let l=null;const r=t.getNodes(),s=t.anchor,i=t.focus,c=t.isBackward(),f=c?i.offset:s.offset,u=c?i.getNode():s.getNode();if(t.isCollapsed()&&""!==t.style){const e=N(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!==f||!s.is(u))&&e(s)){const e=H(s,n,o);if(null===l)l=e;else if(l!==e){l="";break}}}return null===l?o:l}function $(n){if(p(n))return!1;if(!t(n)||f(n))return!1;const o=n.getFirstChild(),l=null===o||h(o)||e(o)||o.isInline();return!n.isInline()&&!1!==n.canBeEmpty()&&l}export{P as $addNodeStyle,E as $cloneWithProperties,M as $getSelectionStyleValueForProperty,C as $isAtNodeEnd,R as $isParentElementRTL,A as $moveCaretSelection,D as $moveCharacter,I as $patchStyleText,L as $selectAll,K as $setBlocksType,z as $shouldOverrideDefaultCharacterSelection,v as $sliceSelectedTextNodeContent,k as $wrapNodes,T as createDOMRange,x as createRectsFromDOMRange,N as getStyleObjectFromCSS,w as trimTextContentFromAnchor};
|