@lexical/selection 0.9.0 → 0.9.2

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.
@@ -48,6 +48,16 @@ function getDOMIndexWithinParent(node) {
48
48
 
49
49
  return [parent, Array.from(parent.childNodes).indexOf(node)];
50
50
  }
51
+ /**
52
+ * Creates a selection range for the DOM.
53
+ * @param editor - The lexical editor.
54
+ * @param anchorNode - The anchor node of a selection.
55
+ * @param _anchorOffset - The amount of space offset from the anchor to the focus.
56
+ * @param focusNode - The current focus.
57
+ * @param _focusOffset - The amount of space offset from the focus to the anchor.
58
+ * @returns The range of selection for the DOM that was created.
59
+ */
60
+
51
61
 
52
62
  function createDOMRange(editor, anchorNode, _anchorOffset, focusNode, _focusOffset) {
53
63
  const anchorKey = anchorNode.getKey();
@@ -99,6 +109,13 @@ function createDOMRange(editor, anchorNode, _anchorOffset, focusNode, _focusOffs
99
109
 
100
110
  return range;
101
111
  }
112
+ /**
113
+ * Creates DOMRects, generally used to help the editor find a specific location on the screen.
114
+ * @param editor - The lexical editor
115
+ * @param range - A fragment of a document that can contain nodes and parts of text nodes.
116
+ * @returns The selectionRects as an array.
117
+ */
118
+
102
119
  function createRectsFromDOMRange(editor, range) {
103
120
  const rootElement = editor.getRootElement();
104
121
 
@@ -134,6 +151,12 @@ function createRectsFromDOMRange(editor, range) {
134
151
 
135
152
  return selectionRects;
136
153
  }
154
+ /**
155
+ * Creates an object containing all the styles and their values provided in the CSS string.
156
+ * @param css - The CSS string of styles and their values.
157
+ * @returns The styleObject containing all the styles and their values.
158
+ */
159
+
137
160
  function getStyleObjectFromRawCSS(css) {
138
161
  const styleObject = {};
139
162
  const styles = css.split(';');
@@ -148,6 +171,12 @@ function getStyleObjectFromRawCSS(css) {
148
171
 
149
172
  return styleObject;
150
173
  }
174
+ /**
175
+ * Given a CSS string, returns an object from the style cache.
176
+ * @param css - The CSS property as a string.
177
+ * @returns The value of the given CSS property.
178
+ */
179
+
151
180
  function getStyleObjectFromCSS(css) {
152
181
  let value = CSS_TO_STYLES.get(css);
153
182
 
@@ -158,6 +187,12 @@ function getStyleObjectFromCSS(css) {
158
187
 
159
188
  return value;
160
189
  }
190
+ /**
191
+ * Gets the CSS styles from the style object.
192
+ * @param styles - The style object containing the styles to get.
193
+ * @returns A string containing the CSS styles and their values.
194
+ */
195
+
161
196
  function getCSSFromStyleObject(styles) {
162
197
  let css = '';
163
198
 
@@ -195,6 +230,12 @@ function $updateTextNodeProperties(target, source) {
195
230
  target.__detail = source.__detail;
196
231
  return target;
197
232
  }
233
+ /**
234
+ * Returns a copy of a node, but generates a new key for the copy.
235
+ * @param node - The node to be cloned.
236
+ * @returns The clone of the node.
237
+ */
238
+
198
239
 
199
240
  function $cloneWithProperties(node) {
200
241
  const latest = node.getLatest();
@@ -215,6 +256,14 @@ function $cloneWithProperties(node) {
215
256
 
216
257
  return clone;
217
258
  }
259
+ /**
260
+ * Generally used to append text content to HTML and JSON. Grabs the text content and "slices"
261
+ * it to be generated into the new TextNode.
262
+ * @param selection - The selection containing the node whose TextNode is to be edited.
263
+ * @param textNode - The TextNode to be edited.
264
+ * @returns The updated TextNode.
265
+ */
266
+
218
267
  function $sliceSelectedTextNodeContent(selection, textNode) {
219
268
  if (textNode.isSelected() && !textNode.isSegmented() && !textNode.isToken() && (lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection))) {
220
269
  const anchorNode = selection.anchor.getNode();
@@ -251,6 +300,12 @@ function $sliceSelectedTextNodeContent(selection, textNode) {
251
300
 
252
301
  return textNode;
253
302
  }
303
+ /**
304
+ * Determines if the current selection is at the end of the node.
305
+ * @param point - The point of the selection to test.
306
+ * @returns true if the provided point offset is in the last possible position, false otherwise.
307
+ */
308
+
254
309
  function $isAtNodeEnd(point) {
255
310
  if (point.type === 'text') {
256
311
  return point.offset === point.getNode().getTextContentSize();
@@ -258,6 +313,15 @@ function $isAtNodeEnd(point) {
258
313
 
259
314
  return point.offset === point.getNode().getChildrenSize();
260
315
  }
316
+ /**
317
+ * Trims text from a node in order to shorten it, eg. to enforce a text's max length. If it deletes text
318
+ * that is an ancestor of the anchor then it will leave 2 indents, otherwise, if no text content exists, it deletes
319
+ * the TextNode. It will move the focus to either the end of any left over text or beginning of a new TextNode.
320
+ * @param editor - The lexical editor.
321
+ * @param anchor - The anchor of the current selection, where the selection should be pointing.
322
+ * @param delCount - The amount of characters to delete. Useful as a dynamic variable eg. textContentSize - maxLength;
323
+ */
324
+
261
325
  function trimTextContentFromAnchor(editor, anchor, delCount) {
262
326
  // Work from the current selection anchor point
263
327
  let currentNode = anchor.getNode();
@@ -381,6 +445,11 @@ function trimTextContentFromAnchor(editor, anchor, delCount) {
381
445
  }
382
446
  }
383
447
  }
448
+ /**
449
+ * Gets the TextNode's style object and adds the styles to the CSS.
450
+ * @param node - The TextNode to add styles to.
451
+ */
452
+
384
453
  function $addNodeStyle(node) {
385
454
  const CSSText = node.getStyle();
386
455
  const styles = getStyleObjectFromRawCSS(CSSText);
@@ -403,6 +472,14 @@ function $patchStyle(target, patch) {
403
472
  target.setStyle(newCSSText);
404
473
  CSS_TO_STYLES.set(newCSSText, newStyles);
405
474
  }
475
+ /**
476
+ * Applies the provided styles to the TextNodes in the provided Selection.
477
+ * Will update partially selected TextNodes by splitting the TextNode and applying
478
+ * the styles to the appropriate one.
479
+ * @param selection - The selected node(s) to update.
480
+ * @param patch - The patch to apply, which can include multiple styles. { CSSProperty: value }
481
+ */
482
+
406
483
 
407
484
  function $patchStyleText(selection, patch) {
408
485
  const selectedNodes = selection.getNodes();
@@ -517,11 +594,9 @@ function $patchStyleText(selection, patch) {
517
594
  *
518
595
  */
519
596
  /**
520
- * Converts all nodes in the selection that are of one block type to another specified by parameter
521
- *
522
- * @param selection
523
- * @param createElement
524
- * @returns
597
+ * Converts all nodes in the selection that are of one block type to another.
598
+ * @param selection - The selected blocks to be converted.
599
+ * @param createElement - The function that creates the node. eg. $createParagraphNode.
525
600
  */
526
601
 
527
602
  function $setBlocksType(selection, createElement) {
@@ -596,7 +671,13 @@ function $removeParentEmptyElements(startingNode) {
596
671
  node = parentNode;
597
672
  }
598
673
  }
599
- /** @deprecated */
674
+ /**
675
+ * @deprecated
676
+ * Wraps all nodes in the selection into another node of the type returned by createElement.
677
+ * @param selection - The selection of nodes to be wrapped.
678
+ * @param createElement - A function that creates the wrapping ElementNode. eg. $createParagraphNode.
679
+ * @param wrappingElement - An element to append the wrapped selection and its children to.
680
+ */
600
681
 
601
682
 
602
683
  function $wrapNodes(selection, createElement, wrappingElement = null) {
@@ -643,6 +724,16 @@ function $wrapNodes(selection, createElement, wrappingElement = null) {
643
724
 
644
725
  $wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
645
726
  }
727
+ /**
728
+ * Wraps each node into a new ElementNode.
729
+ * @param selection - The selection of nodes to wrap.
730
+ * @param nodes - An array of nodes, generally the descendants of the selection.
731
+ * @param nodesLength - The length of nodes.
732
+ * @param createElement - A function that creates the wrapping ElementNode. eg. $createParagraphNode.
733
+ * @param wrappingElement - An element to wrap all the nodes into.
734
+ * @returns
735
+ */
736
+
646
737
  function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingElement = null) {
647
738
  if (nodes.length === 0) {
648
739
  return;
@@ -803,22 +894,55 @@ function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingEl
803
894
  selection.dirty = true;
804
895
  }
805
896
  }
897
+ /**
898
+ * Determines if the default character selection should be overridden. Used with DecoratorNodes
899
+ * @param selection - The selection whose default character selection may need to be overridden.
900
+ * @param isBackward - Is the selection backwards (the focus comes before the anchor)?
901
+ * @returns true if it should be overridden, false if not.
902
+ */
903
+
806
904
  function $shouldOverrideDefaultCharacterSelection(selection, isBackward) {
807
905
  const possibleNode = lexical.$getAdjacentNode(selection.focus, isBackward);
808
906
  return lexical.$isDecoratorNode(possibleNode) && !possibleNode.isIsolated() || lexical.$isElementNode(possibleNode) && !possibleNode.isInline() && !possibleNode.canBeEmpty();
809
907
  }
908
+ /**
909
+ * Moves the selection according to the arguments.
910
+ * @param selection - The selected text or nodes.
911
+ * @param isHoldingShift - Is the shift key being held down during the operation.
912
+ * @param isBackward - Is the selection selected backwards (the focus comes before the anchor)?
913
+ * @param granularity - The distance to adjust the current selection.
914
+ */
915
+
810
916
  function $moveCaretSelection(selection, isHoldingShift, isBackward, granularity) {
811
917
  selection.modify(isHoldingShift ? 'extend' : 'move', isBackward, granularity);
812
918
  }
919
+ /**
920
+ * Tests a parent element for right to left direction.
921
+ * @param selection - The selection whose parent is to be tested.
922
+ * @returns true if the selections' parent element has a direction of 'rtl' (right to left), false otherwise.
923
+ */
924
+
813
925
  function $isParentElementRTL(selection) {
814
926
  const anchorNode = selection.anchor.getNode();
815
927
  const parent = lexical.$isRootNode(anchorNode) ? anchorNode : anchorNode.getParentOrThrow();
816
928
  return parent.getDirection() === 'rtl';
817
929
  }
930
+ /**
931
+ * Moves selection by character according to arguments.
932
+ * @param selection - The selection of the characters to move.
933
+ * @param isHoldingShift - Is the shift key being held down during the operation.
934
+ * @param isBackward - Is the selection backward (the focus comes before the anchor)?
935
+ */
936
+
818
937
  function $moveCharacter(selection, isHoldingShift, isBackward) {
819
938
  const isRTL = $isParentElementRTL(selection);
820
939
  $moveCaretSelection(selection, isHoldingShift, isBackward ? !isRTL : isRTL, 'character');
821
940
  }
941
+ /**
942
+ * Expands the current Selection to cover all of the content in the editor.
943
+ * @param selection - The current selection.
944
+ */
945
+
822
946
  function $selectAll(selection) {
823
947
  const anchor = selection.anchor;
824
948
  const focus = selection.focus;
@@ -849,6 +973,13 @@ function $selectAll(selection) {
849
973
  focus.set(lastNode.getKey(), lastOffset, lastType);
850
974
  }
851
975
  }
976
+ /**
977
+ * Returns the current value of a CSS property for Nodes, if set. If not set, it returns the defaultValue.
978
+ * @param node - The node whose style value to get.
979
+ * @param styleProperty - The CSS style property.
980
+ * @param defaultValue - The default value for the property.
981
+ * @returns The value of the property for node.
982
+ */
852
983
 
853
984
  function $getNodeStyleValueForProperty(node, styleProperty, defaultValue) {
854
985
  const css = node.getStyle();
@@ -860,6 +991,15 @@ function $getNodeStyleValueForProperty(node, styleProperty, defaultValue) {
860
991
 
861
992
  return defaultValue;
862
993
  }
994
+ /**
995
+ * Returns the current value of a CSS property for TextNodes in the Selection, if set. If not set, it returns the defaultValue.
996
+ * If all TextNodes do not have the same value, it returns an empty string.
997
+ * @param selection - The selection of TextNodes whose value to find.
998
+ * @param styleProperty - The CSS style property.
999
+ * @param defaultValue - The default value for the property, defaults to an empty string.
1000
+ * @returns The value of the property for the selected TextNodes.
1001
+ */
1002
+
863
1003
 
864
1004
  function $getSelectionStyleValueForProperty(selection, styleProperty, defaultValue = '') {
865
1005
  let styleValue = null;
package/lexical-node.d.ts CHANGED
@@ -6,9 +6,45 @@
6
6
  *
7
7
  */
8
8
  import type { GridSelection, LexicalEditor, LexicalNode, NodeSelection, Point, RangeSelection, TextNode } from 'lexical';
9
+ /**
10
+ * Returns a copy of a node, but generates a new key for the copy.
11
+ * @param node - The node to be cloned.
12
+ * @returns The clone of the node.
13
+ */
9
14
  export declare function $cloneWithProperties<T extends LexicalNode>(node: T): T;
15
+ /**
16
+ * Generally used to append text content to HTML and JSON. Grabs the text content and "slices"
17
+ * it to be generated into the new TextNode.
18
+ * @param selection - The selection containing the node whose TextNode is to be edited.
19
+ * @param textNode - The TextNode to be edited.
20
+ * @returns The updated TextNode.
21
+ */
10
22
  export declare function $sliceSelectedTextNodeContent(selection: RangeSelection | GridSelection | NodeSelection, textNode: TextNode): LexicalNode;
23
+ /**
24
+ * Determines if the current selection is at the end of the node.
25
+ * @param point - The point of the selection to test.
26
+ * @returns true if the provided point offset is in the last possible position, false otherwise.
27
+ */
11
28
  export declare function $isAtNodeEnd(point: Point): boolean;
29
+ /**
30
+ * Trims text from a node in order to shorten it, eg. to enforce a text's max length. If it deletes text
31
+ * that is an ancestor of the anchor then it will leave 2 indents, otherwise, if no text content exists, it deletes
32
+ * the TextNode. It will move the focus to either the end of any left over text or beginning of a new TextNode.
33
+ * @param editor - The lexical editor.
34
+ * @param anchor - The anchor of the current selection, where the selection should be pointing.
35
+ * @param delCount - The amount of characters to delete. Useful as a dynamic variable eg. textContentSize - maxLength;
36
+ */
12
37
  export declare function trimTextContentFromAnchor(editor: LexicalEditor, anchor: Point, delCount: number): void;
38
+ /**
39
+ * Gets the TextNode's style object and adds the styles to the CSS.
40
+ * @param node - The TextNode to add styles to.
41
+ */
13
42
  export declare function $addNodeStyle(node: TextNode): void;
43
+ /**
44
+ * Applies the provided styles to the TextNodes in the provided Selection.
45
+ * Will update partially selected TextNodes by splitting the TextNode and applying
46
+ * the styles to the appropriate one.
47
+ * @param selection - The selected node(s) to update.
48
+ * @param patch - The patch to apply, which can include multiple styles. { CSSProperty: value }
49
+ */
14
50
  export declare function $patchStyleText(selection: RangeSelection, patch: Record<string, string | null>): void;
package/package.json CHANGED
@@ -9,10 +9,10 @@
9
9
  "selection"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.9.0",
12
+ "version": "0.9.2",
13
13
  "main": "LexicalSelection.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.9.0"
15
+ "lexical": "0.9.2"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",
@@ -7,19 +7,68 @@
7
7
  */
8
8
  import type { ElementNode, GridSelection, LexicalNode, RangeSelection } from 'lexical';
9
9
  /**
10
- * Converts all nodes in the selection that are of one block type to another specified by parameter
11
- *
12
- * @param selection
13
- * @param createElement
14
- * @returns
10
+ * Converts all nodes in the selection that are of one block type to another.
11
+ * @param selection - The selected blocks to be converted.
12
+ * @param createElement - The function that creates the node. eg. $createParagraphNode.
15
13
  */
16
14
  export declare function $setBlocksType(selection: RangeSelection | GridSelection, createElement: () => ElementNode): void;
17
- /** @deprecated */
15
+ /**
16
+ * @deprecated
17
+ * Wraps all nodes in the selection into another node of the type returned by createElement.
18
+ * @param selection - The selection of nodes to be wrapped.
19
+ * @param createElement - A function that creates the wrapping ElementNode. eg. $createParagraphNode.
20
+ * @param wrappingElement - An element to append the wrapped selection and its children to.
21
+ */
18
22
  export declare function $wrapNodes(selection: RangeSelection | GridSelection, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
23
+ /**
24
+ * Wraps each node into a new ElementNode.
25
+ * @param selection - The selection of nodes to wrap.
26
+ * @param nodes - An array of nodes, generally the descendants of the selection.
27
+ * @param nodesLength - The length of nodes.
28
+ * @param createElement - A function that creates the wrapping ElementNode. eg. $createParagraphNode.
29
+ * @param wrappingElement - An element to wrap all the nodes into.
30
+ * @returns
31
+ */
19
32
  export declare function $wrapNodesImpl(selection: RangeSelection | GridSelection, nodes: LexicalNode[], nodesLength: number, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
33
+ /**
34
+ * Determines if the default character selection should be overridden. Used with DecoratorNodes
35
+ * @param selection - The selection whose default character selection may need to be overridden.
36
+ * @param isBackward - Is the selection backwards (the focus comes before the anchor)?
37
+ * @returns true if it should be overridden, false if not.
38
+ */
20
39
  export declare function $shouldOverrideDefaultCharacterSelection(selection: RangeSelection, isBackward: boolean): boolean;
40
+ /**
41
+ * Moves the selection according to the arguments.
42
+ * @param selection - The selected text or nodes.
43
+ * @param isHoldingShift - Is the shift key being held down during the operation.
44
+ * @param isBackward - Is the selection selected backwards (the focus comes before the anchor)?
45
+ * @param granularity - The distance to adjust the current selection.
46
+ */
21
47
  export declare function $moveCaretSelection(selection: RangeSelection, isHoldingShift: boolean, isBackward: boolean, granularity: 'character' | 'word' | 'lineboundary'): void;
48
+ /**
49
+ * Tests a parent element for right to left direction.
50
+ * @param selection - The selection whose parent is to be tested.
51
+ * @returns true if the selections' parent element has a direction of 'rtl' (right to left), false otherwise.
52
+ */
22
53
  export declare function $isParentElementRTL(selection: RangeSelection): boolean;
54
+ /**
55
+ * Moves selection by character according to arguments.
56
+ * @param selection - The selection of the characters to move.
57
+ * @param isHoldingShift - Is the shift key being held down during the operation.
58
+ * @param isBackward - Is the selection backward (the focus comes before the anchor)?
59
+ */
23
60
  export declare function $moveCharacter(selection: RangeSelection, isHoldingShift: boolean, isBackward: boolean): void;
61
+ /**
62
+ * Expands the current Selection to cover all of the content in the editor.
63
+ * @param selection - The current selection.
64
+ */
24
65
  export declare function $selectAll(selection: RangeSelection): void;
66
+ /**
67
+ * Returns the current value of a CSS property for TextNodes in the Selection, if set. If not set, it returns the defaultValue.
68
+ * If all TextNodes do not have the same value, it returns an empty string.
69
+ * @param selection - The selection of TextNodes whose value to find.
70
+ * @param styleProperty - The CSS style property.
71
+ * @param defaultValue - The default value for the property, defaults to an empty string.
72
+ * @returns The value of the property for the selected TextNodes.
73
+ */
25
74
  export declare function $getSelectionStyleValueForProperty(selection: RangeSelection, styleProperty: string, defaultValue?: string): string;
package/utils.d.ts CHANGED
@@ -6,8 +6,38 @@
6
6
  *
7
7
  */
8
8
  import type { LexicalEditor, LexicalNode } from 'lexical';
9
+ /**
10
+ * Creates a selection range for the DOM.
11
+ * @param editor - The lexical editor.
12
+ * @param anchorNode - The anchor node of a selection.
13
+ * @param _anchorOffset - The amount of space offset from the anchor to the focus.
14
+ * @param focusNode - The current focus.
15
+ * @param _focusOffset - The amount of space offset from the focus to the anchor.
16
+ * @returns The range of selection for the DOM that was created.
17
+ */
9
18
  export declare function createDOMRange(editor: LexicalEditor, anchorNode: LexicalNode, _anchorOffset: number, focusNode: LexicalNode, _focusOffset: number): Range | null;
19
+ /**
20
+ * Creates DOMRects, generally used to help the editor find a specific location on the screen.
21
+ * @param editor - The lexical editor
22
+ * @param range - A fragment of a document that can contain nodes and parts of text nodes.
23
+ * @returns The selectionRects as an array.
24
+ */
10
25
  export declare function createRectsFromDOMRange(editor: LexicalEditor, range: Range): Array<ClientRect>;
26
+ /**
27
+ * Creates an object containing all the styles and their values provided in the CSS string.
28
+ * @param css - The CSS string of styles and their values.
29
+ * @returns The styleObject containing all the styles and their values.
30
+ */
11
31
  export declare function getStyleObjectFromRawCSS(css: string): Record<string, string>;
32
+ /**
33
+ * Given a CSS string, returns an object from the style cache.
34
+ * @param css - The CSS property as a string.
35
+ * @returns The value of the given CSS property.
36
+ */
12
37
  export declare function getStyleObjectFromCSS(css: string): Record<string, string>;
38
+ /**
39
+ * Gets the CSS styles from the style object.
40
+ * @param styles - The style object containing the styles to get.
41
+ * @returns A string containing the CSS styles and their values.
42
+ */
13
43
  export declare function getCSSFromStyleObject(styles: Record<string, string>): string;