@lexical/selection 0.7.9 → 0.8.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 +17 -9
- package/LexicalSelection.prod.js +18 -18
- package/lexical-node.d.ts +1 -1
- package/package.json +2 -2
package/LexicalSelection.dev.js
CHANGED
|
@@ -323,8 +323,8 @@ function $addNodeStyle(node) {
|
|
|
323
323
|
const styles = getStyleObjectFromRawCSS(CSSText);
|
|
324
324
|
CSS_TO_STYLES.set(CSSText, styles);
|
|
325
325
|
}
|
|
326
|
-
function $
|
|
327
|
-
const prevStyles = getStyleObjectFromCSS(
|
|
326
|
+
function $patchStyle(target, patch) {
|
|
327
|
+
const prevStyles = getStyleObjectFromCSS('getStyle' in target ? target.getStyle() : target.style);
|
|
328
328
|
const newStyles = Object.entries(patch).reduce((styles, [key, value]) => {
|
|
329
329
|
if (value === null) {
|
|
330
330
|
delete styles[key];
|
|
@@ -336,7 +336,7 @@ function $patchNodeStyle(node, patch) {
|
|
|
336
336
|
...prevStyles
|
|
337
337
|
} || {});
|
|
338
338
|
const newCSSText = getCSSFromStyleObject(newStyles);
|
|
339
|
-
|
|
339
|
+
target.setStyle(newCSSText);
|
|
340
340
|
CSS_TO_STYLES.set(newCSSText, newStyles);
|
|
341
341
|
}
|
|
342
342
|
function $patchStyleText(selection, patch) {
|
|
@@ -346,6 +346,7 @@ function $patchStyleText(selection, patch) {
|
|
|
346
346
|
let firstNode = selectedNodes[0];
|
|
347
347
|
let lastNode = selectedNodes[lastIndex];
|
|
348
348
|
if (selection.isCollapsed()) {
|
|
349
|
+
$patchStyle(selection, patch);
|
|
349
350
|
return;
|
|
350
351
|
}
|
|
351
352
|
const anchor = selection.anchor;
|
|
@@ -386,14 +387,14 @@ function $patchStyleText(selection, patch) {
|
|
|
386
387
|
|
|
387
388
|
// The entire node is selected, so just format it
|
|
388
389
|
if (startOffset === 0 && endOffset === firstNodeTextLength) {
|
|
389
|
-
$
|
|
390
|
+
$patchStyle(firstNode, patch);
|
|
390
391
|
firstNode.select(startOffset, endOffset);
|
|
391
392
|
} else {
|
|
392
393
|
// The node is partially selected, so split it into two nodes
|
|
393
394
|
// and style the selected one.
|
|
394
395
|
const splitNodes = firstNode.splitText(startOffset, endOffset);
|
|
395
396
|
const replacement = startOffset === 0 ? splitNodes[0] : splitNodes[1];
|
|
396
|
-
$
|
|
397
|
+
$patchStyle(replacement, patch);
|
|
397
398
|
replacement.select(0, endOffset - startOffset);
|
|
398
399
|
}
|
|
399
400
|
} // multiple nodes selected.
|
|
@@ -404,7 +405,7 @@ function $patchStyleText(selection, patch) {
|
|
|
404
405
|
firstNode = firstNode.splitText(startOffset)[1];
|
|
405
406
|
startOffset = 0;
|
|
406
407
|
}
|
|
407
|
-
$
|
|
408
|
+
$patchStyle(firstNode, patch);
|
|
408
409
|
}
|
|
409
410
|
if (lexical.$isTextNode(lastNode)) {
|
|
410
411
|
const lastNodeText = lastNode.getTextContent();
|
|
@@ -423,7 +424,7 @@ function $patchStyleText(selection, patch) {
|
|
|
423
424
|
[lastNode] = lastNode.splitText(endOffset);
|
|
424
425
|
}
|
|
425
426
|
if (endOffset !== 0) {
|
|
426
|
-
$
|
|
427
|
+
$patchStyle(lastNode, patch);
|
|
427
428
|
}
|
|
428
429
|
}
|
|
429
430
|
|
|
@@ -432,7 +433,7 @@ function $patchStyleText(selection, patch) {
|
|
|
432
433
|
const selectedNode = selectedNodes[i];
|
|
433
434
|
const selectedNodeKey = selectedNode.getKey();
|
|
434
435
|
if (lexical.$isTextNode(selectedNode) && selectedNodeKey !== firstNode.getKey() && selectedNodeKey !== lastNode.getKey() && !selectedNode.isToken()) {
|
|
435
|
-
$
|
|
436
|
+
$patchStyle(selectedNode, patch);
|
|
436
437
|
}
|
|
437
438
|
}
|
|
438
439
|
}
|
|
@@ -733,6 +734,13 @@ function $getSelectionStyleValueForProperty(selection, styleProperty, defaultVal
|
|
|
733
734
|
const isBackward = selection.isBackward();
|
|
734
735
|
const endOffset = isBackward ? focus.offset : anchor.offset;
|
|
735
736
|
const endNode = isBackward ? focus.getNode() : anchor.getNode();
|
|
737
|
+
if (selection.style !== '') {
|
|
738
|
+
const css = selection.style;
|
|
739
|
+
const styleObject = getStyleObjectFromCSS(css);
|
|
740
|
+
if (styleObject !== null && styleProperty in styleObject) {
|
|
741
|
+
return styleObject[styleProperty];
|
|
742
|
+
}
|
|
743
|
+
}
|
|
736
744
|
for (let i = 0; i < nodes.length; i++) {
|
|
737
745
|
const node = nodes[i];
|
|
738
746
|
|
|
@@ -748,7 +756,7 @@ function $getSelectionStyleValueForProperty(selection, styleProperty, defaultVal
|
|
|
748
756
|
styleValue = nodeStyleValue;
|
|
749
757
|
} else if (styleValue !== nodeStyleValue) {
|
|
750
758
|
// multiple text nodes are in the selection and they don't all
|
|
751
|
-
// have the same
|
|
759
|
+
// have the same style.
|
|
752
760
|
styleValue = '';
|
|
753
761
|
break;
|
|
754
762
|
}
|
package/LexicalSelection.prod.js
CHANGED
|
@@ -4,25 +4,25 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
'use strict';var l=require("lexical");let u=new Map;function v(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function
|
|
8
|
-
function A(a){let b="";for(let c in a)c&&(b+=`${c}: ${a[c]};`);return b}function B(a,b){var c=z(a.getStyle());b=Object.entries(b).reduce((f,[d,e])=>{null===e?delete f[d]:f[d]=e;return f},{...c});c=A(b);a.setStyle(c);u.set(c,b)}function C(a){for(;null!==a&&!l.$isRootOrShadowRoot(a);){let b=a.getLatest(),c=a.getParent();0===b.getChildrenSize()&&a.remove(!0);a=c}}
|
|
9
|
-
function D(a,b,c,f,d=null){if(0!==b.length){var e=b[0],
|
|
10
|
-
r&&l.$isLeafNode(
|
|
11
|
-
d)e.insertAfter(d);else for(d=g.length-1;0<=d;d--)e.insertAfter(g[d]);else if(
|
|
7
|
+
'use strict';var l=require("lexical");let u=new Map;function v(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function w(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 [f,d]=c.split(/:([^]+)/);b[f.trim()]=d.trim()}return b}function z(a){let b=u.get(a);void 0===b&&(b=y(a),u.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){var c=z("getStyle"in a?a.getStyle():a.style);b=Object.entries(b).reduce((f,[d,e])=>{null===e?delete f[d]:f[d]=e;return f},{...c});c=A(b);a.setStyle(c);u.set(c,b)}function C(a){for(;null!==a&&!l.$isRootOrShadowRoot(a);){let b=a.getLatest(),c=a.getParent();0===b.getChildrenSize()&&a.remove(!0);a=c}}
|
|
9
|
+
function D(a,b,c,f,d=null){if(0!==b.length){var e=b[0],h=new Map,g=[];e=l.$isElementNode(e)?e:e.getParentOrThrow();e.isInline()&&(e=e.getParentOrThrow());for(var k=!1;null!==e;){var m=e.getPreviousSibling();if(null!==m){e=m;k=!0;break}e=e.getParentOrThrow();if(l.$isRootOrShadowRoot(e))break}m=new Set;for(var q=0;q<c;q++){var n=b[q];l.$isElementNode(n)&&0===n.getChildrenSize()&&m.add(n.getKey())}var p=new Set;for(q=0;q<c;q++){n=b[q];var r=n.getParent();null!==r&&r.isInline()&&(r=r.getParent());if(null!==
|
|
10
|
+
r&&l.$isLeafNode(n)&&!p.has(n.getKey())){if(n=r.getKey(),void 0===h.get(n)){let t=f();t.setFormat(r.getFormatType());t.setIndent(r.getIndent());g.push(t);h.set(n,t);r.getChildren().forEach(x=>{t.append(x);p.add(x.getKey());l.$isElementNode(x)&&x.getChildrenKeys().forEach(G=>p.add(G))});C(r)}}else m.has(n.getKey())&&(r=f(),r.setFormat(n.getFormatType()),r.setIndent(n.getIndent()),g.push(r),n.remove(!0))}if(null!==d)for(b=0;b<g.length;b++)d.append(g[b]);b=null;if(l.$isRootOrShadowRoot(e))if(k)if(null!==
|
|
11
|
+
d)e.insertAfter(d);else for(d=g.length-1;0<=d;d--)e.insertAfter(g[d]);else if(k=e.getFirstChild(),l.$isElementNode(k)&&(e=k),null===k)if(d)e.append(d);else for(d=0;d<g.length;d++)k=g[d],e.append(k),b=k;else if(null!==d)k.insertBefore(d);else for(e=0;e<g.length;e++)d=g[e],k.insertBefore(d),b=d;else if(d)e.insertAfter(d);else for(d=g.length-1;0<=d;d--)k=g[d],e.insertAfter(k),b=k;g=l.$getPreviousSelection();l.$isRangeSelection(g)&&g.anchor.getNode().isAttached()&&g.focus.getNode().isAttached()?l.$setSelection(g.clone()):
|
|
12
12
|
null!==b?b.selectEnd():a.dirty=!0}}function E(a,b,c,f){a.modify(b?"extend":"move",c,f)}function F(a){a=a.anchor.getNode();return"rtl"===(l.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}exports.$addNodeStyle=function(a){a=a.getStyle();let b=y(a);u.set(a,b)};
|
|
13
13
|
exports.$cloneWithProperties=function(a){a=a.getLatest();let b=a.constructor.clone(a);b.__parent=a.__parent;b.__next=a.__next;b.__prev=a.__prev;if(l.$isElementNode(a)&&l.$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;l.$isTextNode(a)&&l.$isTextNode(b)&&(b.__format=a.__format,b.__style=a.__style,b.__mode=a.__mode,b.__detail=a.__detail);return b};
|
|
14
|
-
exports.$getSelectionStyleValueForProperty=function(a,b,c=""){let f=null,d=a.getNodes();var e=a.anchor,
|
|
15
|
-
exports.$isParentElementRTL=F;exports.$moveCaretSelection=E;exports.$moveCharacter=function(a,b,c){let f=F(a);E(a,b,c?!f:f,"character")};
|
|
16
|
-
exports.$patchStyleText=function(a,b){var c=a.getNodes();let f=c.length-1,d=c[0],e=c[f];if(
|
|
17
|
-
|
|
18
|
-
exports.$selectAll=function(a){let b=a.anchor;a=a.focus;var c=b.getNode().getTopLevelElementOrThrow().getParentOrThrow();let f=c.getFirstDescendant();c=c.getLastDescendant();let d="element",e="element",
|
|
14
|
+
exports.$getSelectionStyleValueForProperty=function(a,b,c=""){let f=null,d=a.getNodes();var e=a.anchor,h=a.focus,g=a.isBackward();let k=g?h.offset:e.offset;e=g?h.getNode():e.getNode();if(""!==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!==k||!m.is(e))&&l.$isTextNode(m))if(h=b,g=c,m=m.getStyle(),m=z(m),h=null!==m?m[h]||g:g,null===f)f=h;else if(f!==h){f="";break}}return null===f?c:f};
|
|
15
|
+
exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};exports.$isParentElementRTL=F;exports.$moveCaretSelection=E;exports.$moveCharacter=function(a,b,c){let f=F(a);E(a,b,c?!f:f,"character")};
|
|
16
|
+
exports.$patchStyleText=function(a,b){var c=a.getNodes();let f=c.length-1,d=c[0],e=c[f];if(a.isCollapsed())B(a,b);else{var h=a.anchor,g=a.focus,k=d.getTextContent().length,m=g.offset,q=h.offset,n=h.isBefore(g),p=n?q:m;a=n?m:q;var r=n?h.type:g.type,t=n?g.type:h.type;h=n?g.key:h.key;l.$isTextNode(d)&&p===k&&(g=d.getNextSibling(),l.$isTextNode(g)&&(p=q=0,d=g));if(d.is(e))l.$isTextNode(d)&&(p="element"===r?0:q>m?m:q,a="element"===t?k:q>m?q:m,p!==a&&(0===p&&a===k?(B(d,b),d.select(p,a)):(c=d.splitText(p,
|
|
17
|
+
a),c=0===p?c[0]:c[1],B(c,b),c.select(0,a-p))));else for(l.$isTextNode(d)&&p<d.getTextContentSize()&&(0!==p&&(d=d.splitText(p)[1]),B(d,b)),l.$isTextNode(e)&&(p=e.getTextContent().length,e.__key!==h&&0!==a&&(a=p),a!==p&&([e]=e.splitText(a)),0!==a&&B(e,b)),a=1;a<f;a++)p=c[a],k=p.getKey(),l.$isTextNode(p)&&k!==d.getKey()&&k!==e.getKey()&&!p.isToken()&&B(p,b)}};
|
|
18
|
+
exports.$selectAll=function(a){let b=a.anchor;a=a.focus;var c=b.getNode().getTopLevelElementOrThrow().getParentOrThrow();let f=c.getFirstDescendant();c=c.getLastDescendant();let d="element",e="element",h=0;l.$isTextNode(f)?d="text":l.$isElementNode(f)||null===f||(f=f.getParentOrThrow());l.$isTextNode(c)?(e="text",h=c.getTextContentSize()):l.$isElementNode(c)||null===c||(c=c.getParentOrThrow());f&&c&&(b.set(f.getKey(),0,d),a.set(c.getKey(),h,e))};
|
|
19
19
|
exports.$setBlocksType_experimental=function(a,b){if("root"===a.anchor.key){b=b();var c=l.$getRoot();(a=c.getFirstChild())?a.replace(b,!0):c.append(b)}else for(c=a.getNodes(),"text"===a.anchor.type&&(a=a.anchor.getNode().getParent(),a=a.isInline()?a.getParent():a,-1===c.indexOf(a)&&c.push(a)),a=0;a<c.length;a++){let d=c[a];var f=d;!l.$isElementNode(f)||l.$isRootOrShadowRoot(f)||f.isInline()||(f=b(),f.setFormat(d.getFormatType()),f.setIndent(d.getIndent()),d.replace(f,!0))}};
|
|
20
20
|
exports.$shouldOverrideDefaultCharacterSelection=function(a,b){a=l.$getAdjacentNode(a.focus,b);return l.$isDecoratorNode(a)&&!a.isIsolated()||l.$isElementNode(a)&&!a.isInline()&&!a.canBeEmpty()};
|
|
21
|
-
exports.$sliceSelectedTextNodeContent=function(a,b){if(b.isSelected()&&!b.isSegmented()&&!b.isToken()&&(l.$isRangeSelection(a)||l.DEPRECATED_$isGridSelection(a))){var c=a.anchor.getNode(),f=a.focus.getNode(),d=b.is(c),e=b.is(f);if(d||e){d=a.isBackward();let [
|
|
22
|
-
exports.$wrapNodes=function(a,b,c=null){var f=a.getNodes();let d=f.length;var e=a.anchor;if(0===d||1===d&&"element"===e.type&&0===e.getNode().getChildrenSize()){a="text"===e.type?e.getNode().getParentOrThrow():e.getNode();f=a.getChildren();let g=b();g.setFormat(a.getFormatType());g.setIndent(a.getIndent());f.forEach(
|
|
23
|
-
e)?
|
|
24
|
-
exports.createDOMRange=function(a,b,c,f,d){let e=b.getKey(),
|
|
25
|
-
c));return g};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 f=b.length,d;for(let e=0;e<f;e++){let
|
|
26
|
-
exports.trimTextContentFromAnchor=function(a,b,c){let f=b.getNode();if(l.$isElementNode(f)){var d=f.getDescendantByIndex(b.offset);null!==d&&(f=d)}for(;0<c&&null!==f;){var e=f.getPreviousSibling(),
|
|
27
|
-
d=
|
|
28
|
-
(e=b.key===q,
|
|
21
|
+
exports.$sliceSelectedTextNodeContent=function(a,b){if(b.isSelected()&&!b.isSegmented()&&!b.isToken()&&(l.$isRangeSelection(a)||l.DEPRECATED_$isGridSelection(a))){var c=a.anchor.getNode(),f=a.focus.getNode(),d=b.is(c),e=b.is(f);if(d||e){d=a.isBackward();let [h,g]=a.getCharacterOffsets();a=c.is(f);e=b.is(d?f:c);f=b.is(d?c:f);c=0;let k=void 0;a?(c=h>g?g:h,k=h>g?h:g):e?(c=d?g:h,k=void 0):f&&(d=d?h:g,c=0,k=d);b.__text=b.__text.slice(c,k)}}return b};
|
|
22
|
+
exports.$wrapNodes=function(a,b,c=null){var f=a.getNodes();let d=f.length;var e=a.anchor;if(0===d||1===d&&"element"===e.type&&0===e.getNode().getChildrenSize()){a="text"===e.type?e.getNode().getParentOrThrow():e.getNode();f=a.getChildren();let g=b();g.setFormat(a.getFormatType());g.setIndent(a.getIndent());f.forEach(k=>g.append(k));c&&(g=c.append(g));a.replace(g)}else{e=null;var h=[];for(let g=0;g<d;g++){let k=f[g];l.$isRootOrShadowRoot(k)?(D(a,h,h.length,b,c),h=[],e=k):null===e||null!==e&&l.$hasAncestor(k,
|
|
23
|
+
e)?h.push(k):(D(a,h,h.length,b,c),h=[k])}D(a,h,h.length,b,c)}};
|
|
24
|
+
exports.createDOMRange=function(a,b,c,f,d){let e=b.getKey(),h=f.getKey(),g=document.createRange(),k=a.getElementByKey(e);a=a.getElementByKey(h);l.$isTextNode(b)&&(k=v(k));l.$isTextNode(f)&&(a=v(a));if(void 0===b||void 0===f||null===k||null===a)return null;"BR"===k.nodeName&&([k,c]=w(k));"BR"===a.nodeName&&([a,d]=w(a));b=k.firstChild;k===a&&null!=b&&"BR"===b.nodeName&&0===c&&0===d&&(d=1);try{g.setStart(k,c),g.setEnd(a,d)}catch(m){return null}!g.collapsed||c===d&&e===h||(g.setStart(a,d),g.setEnd(k,
|
|
25
|
+
c));return g};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 f=b.length,d;for(let e=0;e<f;e++){let h=b[e],g=h.width+c===a.width;d&&d.top===h.top&&d.left===h.left&&d.width===h.width&&d.height===h.height||g?(b.splice(e--,1),f--):d=h}return b};exports.getStyleObjectFromCSS=z;
|
|
26
|
+
exports.trimTextContentFromAnchor=function(a,b,c){let f=b.getNode();if(l.$isElementNode(f)){var d=f.getDescendantByIndex(b.offset);null!==d&&(f=d)}for(;0<c&&null!==f;){var e=f.getPreviousSibling(),h=0;if(null===e){d=f.getParentOrThrow();for(var g=d.getPreviousSibling();null===g;){d=d.getParent();if(null===d){e=null;break}g=d.getPreviousSibling()}null!==d&&(h=d.isInline()?0:2,e=l.$isElementNode(g)?g.getLastDescendant():g)}let k=f.getTextContent();""===k&&l.$isElementNode(f)&&!f.isInline()&&(k="\n\n");
|
|
27
|
+
d=k.length;g=d-c;let m=k.slice(0,g);if(!l.$isTextNode(f)||c>=d)g=f.getParent(),f.remove(),null!=g&&0===g.getChildrenSize()&&g.remove(),c-=d+h,f=e;else{let q=f.getKey();e=a.getEditorState().read(()=>{const n=l.$getNodeByKey(q);return l.$isTextNode(n)&&n.isSimpleText()?n.getTextContent():null});null!==e&&e!==k?(c=l.$getPreviousSelection(),d=f,f.isSimpleText()?f.setTextContent(e):(d=l.$createTextNode(e),f.replace(d)),l.$isRangeSelection(c)&&c.isCollapsed()&&(c=c.anchor.offset,d.select(c,c))):f.isSimpleText()?
|
|
28
|
+
(e=b.key===q,h=b.offset,h<c&&(h=d),c=e?h-c:0,d=e?h:g,e&&0===c?([c]=f.splitText(c,d),c.remove()):([,c]=f.splitText(c,d),c.remove())):(c=l.$createTextNode(m),f.replace(c));c=0}}}
|
package/lexical-node.d.ts
CHANGED
|
@@ -11,4 +11,4 @@ export declare function $sliceSelectedTextNodeContent(selection: RangeSelection
|
|
|
11
11
|
export declare function $isAtNodeEnd(point: Point): boolean;
|
|
12
12
|
export declare function trimTextContentFromAnchor(editor: LexicalEditor, anchor: Point, delCount: number): void;
|
|
13
13
|
export declare function $addNodeStyle(node: TextNode): void;
|
|
14
|
-
export declare function $patchStyleText(selection: RangeSelection
|
|
14
|
+
export declare function $patchStyleText(selection: RangeSelection, patch: Record<string, string | null>): void;
|
package/package.json
CHANGED