@lexical/selection 0.2.0 → 0.2.3
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 +4 -2
- package/LexicalSelection.prod.js +2 -2
- package/README.md +125 -1
- package/package.json +2 -2
package/LexicalSelection.dev.js
CHANGED
|
@@ -459,7 +459,7 @@ function $removeParentEmptyElements(startingNode) {
|
|
|
459
459
|
const parentNode = node.getParent();
|
|
460
460
|
|
|
461
461
|
if (latest.__children.length === 0) {
|
|
462
|
-
node.remove();
|
|
462
|
+
node.remove(true);
|
|
463
463
|
}
|
|
464
464
|
|
|
465
465
|
node = parentNode;
|
|
@@ -605,7 +605,9 @@ function $wrapLeafNodesInElements(selection, createElement, wrappingElement) {
|
|
|
605
605
|
const prevSelection = lexical.$getPreviousSelection();
|
|
606
606
|
|
|
607
607
|
if (lexical.$isRangeSelection(prevSelection) && isPointAttached(prevSelection.anchor) && isPointAttached(prevSelection.focus)) {
|
|
608
|
-
|
|
608
|
+
const clonedSelection = prevSelection.clone();
|
|
609
|
+
clonedSelection.dirty = true;
|
|
610
|
+
lexical.$setSelection(clonedSelection);
|
|
609
611
|
} else {
|
|
610
612
|
selection.dirty = true;
|
|
611
613
|
}
|
package/LexicalSelection.prod.js
CHANGED
|
@@ -10,7 +10,7 @@ e.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=c}}
|
|
|
10
10
|
function x(a){if(l.$isRangeSelection(a)){var c=a.anchor,b=a.focus,g=c.getCharacterOffset();const n=b.getCharacterOffset();var h=c.getNode(),e=b.getNode(),d=h.getParentOrThrow();if(h===e&&l.$isTextNode(h)&&(d.canBeEmpty()||1<d.getChildrenSize()))return a=u(h),h=n>g,a.__text=a.__text.slice(h?g:n,h?n:g),g=a.getKey(),{nodeMap:[[g,a]],range:[g]};a=a.getNodes();if(0===a.length)return{nodeMap:[],range:[]};h=a.length;e=a[0];d=e.getParent();if(null!==d&&(!d.canBeEmpty()||l.$isRootNode(d))){var f=d.__children;
|
|
11
11
|
if(f.length===h){var k=!0;for(var m=0;m<f.length;m++)if(f[m]!==a[m].__key){k=!1;break}k&&(h++,a.push(d))}}d=a[h-1];c=c.isBefore(b);b=new Map;f=[];w(e,c?g:n,!0,f,b);for(e=0;e<h;e++)if(k=a[e],m=k.getKey(),!(b.has(m)||l.$isElementNode(k)&&k.excludeFromCopy())){const r=u(k);l.$isRootNode(k.getParent())&&f.push(k.getKey());b.set(m,r)}w(d,c?n:g,!1,f,b);return{nodeMap:Array.from(b.entries()),range:f}}if(l.$isGridSelection(a))return{nodeMap:a.getNodes().map(n=>{const r=n.getKey();n=u(n);return[r,n]}),range:[a.gridKey]};
|
|
12
12
|
throw Error("Minified Lexical error #68; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");}function y(a){return t.get(a)||null}function z(a,c){var b=y(a.getStyle());c=b?{...b,...c}:c;b="";for(g in c)g&&(b+=`${g}: ${c[g]};`);var g=b;a.setStyle(g);t.set(g,c)}function A(a,c,b,g){a.modify(c?"extend":"move",b,g)}function B(a){a=a.anchor.getNode();return"rtl"===(l.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}
|
|
13
|
-
function C(a){for(;null!==a&&!l.$isRootNode(a);){const c=a.getLatest(),b=a.getParent();0===c.__children.length&&a.remove();a=b}}exports.$cloneContents=function(a){return x(a)};
|
|
13
|
+
function C(a){for(;null!==a&&!l.$isRootNode(a);){const c=a.getLatest(),b=a.getParent();0===c.__children.length&&a.remove(!0);a=b}}exports.$cloneContents=function(a){return x(a)};
|
|
14
14
|
exports.$getSelectionStyleValueForProperty=function(a,c,b=""){let g=null;const h=a.getNodes();var e=a.anchor,d=a.focus,f=a.isBackward();a=f?d.offset:e.offset;e=f?d.getNode():e.getNode();for(d=0;d<h.length;d++){var k=h[d];if((0===d||0!==a||!k.is(e))&&l.$isTextNode(k)){f=c;var m=b;k=k.getStyle();k=y(k);f=null!==k?k[f]||m:m;if(null===g)g=f;else if(g!==f){g="";break}}}return null===g?b:g};exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};
|
|
15
15
|
exports.$isParentElementRTL=B;exports.$moveCaretSelection=A;exports.$moveCharacter=function(a,c,b){const g=B(a);A(a,c,b?!g:g,"character")};
|
|
16
16
|
exports.$patchStyleText=function(a,c){var b=a.getNodes();const g=b.length-1;let h=b[0],e=b[g];if(!a.isCollapsed()){var d=a.anchor,f=a.focus;a=h.getTextContent().length;var k=f.offset,m=d.offset;d=(f=d.isBefore(f))?m:k;f=f?k:m;if(d===h.getTextContentSize()){const n=h.getNextSibling();l.$isTextNode(n)&&(d=m=0,h=n)}if(h.is(e))l.$isTextNode(h)&&(d=m>k?k:m,f=m>k?m:k,d!==f&&(0===d&&f===a?(z(h,c),h.select(d,f)):(b=h.splitText(d,f),b=0===d?b[0]:b[1],z(b,c),b.select(0,f-d))));else for(l.$isTextNode(h)&&(0!==
|
|
@@ -19,4 +19,4 @@ exports.$selectAll=function(a){const c=a.anchor;a=a.focus;var b=c.getNode().getT
|
|
|
19
19
|
exports.$shouldOverrideDefaultCharacterSelection=function(a,c){a=l.$getDecoratorNode(a.focus,c);return l.$isDecoratorNode(a)&&!a.isIsolated()};
|
|
20
20
|
exports.$wrapLeafNodesInElements=function(a,c,b){const g=a.getNodes(),h=g.length;if(0===h){a=a.anchor;a="text"===a.type?a.getNode().getParentOrThrow():a.getNode();var e=a.getChildren();let p=c();e.forEach(v=>p.append(v));b&&(p=b.append(p));a.replace(p)}else{var d=g[0],f=new Map;e=[];d=l.$isElementNode(d)?d:d.getParentOrThrow();for(d.isInline()&&(d=d.getParentOrThrow());null!==d;){var k=d.getPreviousSibling();if(null!==k){d=k;break}d=d.getParentOrThrow();if(l.$isRootNode(d))break}k=new Set;for(var m=
|
|
21
21
|
0;m<h;m++){var n=g[m];l.$isElementNode(n)&&0===n.getChildrenSize()&&k.add(n.getKey())}var r=new Set;for(m=0;m<h;m++){var q=g[m];n=q.getParent();null!==n&&n.isInline()&&(n=n.getParent());if(null!==n&&l.$isLeafNode(q)&&!r.has(q.getKey())){if(q=n.getKey(),void 0===f.get(q)){const p=c();e.push(p);f.set(q,p);n.getChildren().forEach(v=>{p.append(v);r.add(v.getKey())});C(n)}}else k.has(q.getKey())&&(e.push(c()),q.remove())}if(b)for(c=0;c<e.length;c++)b.append(e[c]);if(l.$isRootNode(d))if(c=d.getFirstChild(),
|
|
22
|
-
l.$isElementNode(c)&&(d=c),null===c)if(b)d.append(b);else for(b=0;b<e.length;b++)d.append(e[b]);else if(b)c.insertBefore(b);else for(b=0;b<e.length;b++)c.insertBefore(e[b]);else if(b)d.insertAfter(b);else for(b=e.length-1;0<=b;b--)d.insertAfter(e[b]);b=l.$getPreviousSelection();l.$isRangeSelection(b)&&b.anchor.getNode().isAttached()&&b.focus.getNode().isAttached()?l.$setSelection(
|
|
22
|
+
l.$isElementNode(c)&&(d=c),null===c)if(b)d.append(b);else for(b=0;b<e.length;b++)d.append(e[b]);else if(b)c.insertBefore(b);else for(b=0;b<e.length;b++)c.insertBefore(e[b]);else if(b)d.insertAfter(b);else for(b=e.length-1;0<=b;b--)d.insertAfter(e[b]);b=l.$getPreviousSelection();l.$isRangeSelection(b)&&b.anchor.getNode().isAttached()&&b.focus.getNode().isAttached()?(a=b.clone(),a.dirty=!0,l.$setSelection(a)):a.dirty=!0}};exports.getStyleObjectFromCSS=y;
|
package/README.md
CHANGED
|
@@ -1,3 +1,127 @@
|
|
|
1
1
|
# `@lexical/selection`
|
|
2
2
|
|
|
3
|
-
This package contains
|
|
3
|
+
This package contains selection helpers for Lexical.
|
|
4
|
+
|
|
5
|
+
### Methods
|
|
6
|
+
|
|
7
|
+
#### `$cloneContents`
|
|
8
|
+
|
|
9
|
+
Clones the Lexical nodes in the selection, returning a map of Key -> LexicalNode and a list containing the keys
|
|
10
|
+
of all direct children of the RootNode. Useful for insertion/transfer operations, such as copy and paste.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
export function $cloneContents(
|
|
14
|
+
selection: RangeSelection | NodeSelection | GridSelection,
|
|
15
|
+
): {
|
|
16
|
+
nodeMap: Array<[NodeKey, LexicalNode]>;
|
|
17
|
+
range: Array<NodeKey>;
|
|
18
|
+
};
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
#### `getStyleObjectFromCSS`
|
|
22
|
+
|
|
23
|
+
Given a CSS string, returns an object from the style cache.
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
export function getStyleObjectFromCSS(css: string): {
|
|
27
|
+
[key: string]: string;
|
|
28
|
+
} | null;
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
#### `$patchStyleText`
|
|
32
|
+
|
|
33
|
+
Applies the provided styles to the TextNodes in the provided Selection. Key names in the patch argument should be
|
|
34
|
+
the valid CSS properties (i.e., kebab-case).
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
export function $patchStyleText(
|
|
38
|
+
selection: RangeSelection | GridSelection,
|
|
39
|
+
patch: {
|
|
40
|
+
[key: string]: string;
|
|
41
|
+
},
|
|
42
|
+
): void;
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### `$getSelectionStyleValueForProperty`
|
|
46
|
+
|
|
47
|
+
Given a selection and a valid CSS property name, returns the current value of that property for TextNodes in the Selection, if set. If not set, it returns the defaultValue. If all TextNodes do not have the same value, it returns an empty string.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
export function $getSelectionStyleValueForProperty(
|
|
51
|
+
selection: RangeSelection,
|
|
52
|
+
styleProperty: string,
|
|
53
|
+
defaultValue: string,
|
|
54
|
+
): string;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### `$moveCaretSelection`
|
|
58
|
+
|
|
59
|
+
Moves the selection according to the arguments.
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
export function $moveCaretSelection(
|
|
63
|
+
selection: RangeSelection,
|
|
64
|
+
isHoldingShift: boolean,
|
|
65
|
+
isBackward: boolean,
|
|
66
|
+
granularity: 'character' | 'word' | 'lineboundary',
|
|
67
|
+
): void;
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### `$isParentElementRTL`
|
|
71
|
+
|
|
72
|
+
Returns true if the parent of the Selection anchor node is in Right-To-Left mode, false if not.
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
export function $isParentElementRTL(selection: RangeSelection): boolean;
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
#### `$moveCharacter`
|
|
79
|
+
|
|
80
|
+
Wraps $moveCaretSelection, using character granularity and accounting for RTL mode.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
export function $moveCharacter(
|
|
84
|
+
selection: RangeSelection,
|
|
85
|
+
isHoldingShift: boolean,
|
|
86
|
+
isBackward: boolean,
|
|
87
|
+
): void;
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### `$selectAll`
|
|
91
|
+
|
|
92
|
+
Expands the current Selection to cover all of the content in the editor.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
export function $selectAll(selection: RangeSelection): void;
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### `$wrapLeafNodesInElements`
|
|
99
|
+
|
|
100
|
+
Attempts to wrap all leaf nodes in the Selection in ElementNodes returned from createElement. If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement. It attempts to append the resulting sub-tree to the nearest safe insertion target.
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
export function $wrapLeafNodesInElements(
|
|
104
|
+
selection: RangeSelection,
|
|
105
|
+
createElement: () => ElementNode,
|
|
106
|
+
wrappingElement?: ElementNode,
|
|
107
|
+
): void;
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### `$isAtNodeEnd`
|
|
111
|
+
|
|
112
|
+
Returns true if the provided point offset is in the last possible position.
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
export function $isAtNodeEnd(point: Point): boolean;
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### `$shouldOverrideDefaultCharacterSelection`
|
|
119
|
+
|
|
120
|
+
Returns true if default character selection should be overridden, false if not. Used with DecoratorNodes
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
export function $shouldOverrideDefaultCharacterSelection(
|
|
124
|
+
selection: RangeSelection,
|
|
125
|
+
isBackward: boolean,
|
|
126
|
+
): boolean;
|
|
127
|
+
```
|
package/package.json
CHANGED