@lexical/text 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,6 +9,14 @@
9
9
  var lexical = require('lexical');
10
10
 
11
11
  /** @module @lexical/text */
12
+
13
+ /**
14
+ * Finds a TextNode with a size larger than targetCharacters and returns
15
+ * the node along with the remaining length of the text.
16
+ * @param root - The RootNode.
17
+ * @param targetCharacters - The number of characters whose TextNode must be larger than.
18
+ * @returns The TextNode and the intersections offset, or null if no TextNode is found.
19
+ */
12
20
  function $findTextIntersectionFromCharacters(root, targetCharacters) {
13
21
  let node = root.getFirstChild();
14
22
  let currentCharacters = 0;
@@ -59,6 +67,13 @@ function $findTextIntersectionFromCharacters(root, targetCharacters) {
59
67
 
60
68
  return null;
61
69
  }
70
+ /**
71
+ * Determines if the root has any text content and can trim any whitespace if it does.
72
+ * @param isEditorComposing - Is the editor in composition mode due to an active Input Method Editor?
73
+ * @param trim - Should the root text have its whitespaced trimmed? Defaults to true.
74
+ * @returns true if text content is empty, false if there is text or isEditorComposing is true.
75
+ */
76
+
62
77
  function $isRootTextContentEmpty(isEditorComposing, trim = true) {
63
78
  if (isEditorComposing) {
64
79
  return false;
@@ -72,13 +87,32 @@ function $isRootTextContentEmpty(isEditorComposing, trim = true) {
72
87
 
73
88
  return text === '';
74
89
  }
90
+ /**
91
+ * Returns a function that executes {@link $isRootTextContentEmpty}
92
+ * @param isEditorComposing - Is the editor in composition mode due to an active Input Method Editor?
93
+ * @param trim - Should the root text have its whitespaced trimmed? Defaults to true.
94
+ * @returns A function that executes $isRootTextContentEmpty based on arguments.
95
+ */
96
+
75
97
  function $isRootTextContentEmptyCurry(isEditorComposing, trim) {
76
98
  return () => $isRootTextContentEmpty(isEditorComposing, trim);
77
99
  }
100
+ /**
101
+ * Returns the root's text content.
102
+ * @returns The root's text content.
103
+ */
104
+
78
105
  function $rootTextContent() {
79
106
  const root = lexical.$getRoot();
80
107
  return root.getTextContent();
81
108
  }
109
+ /**
110
+ * Determines if the input should show the placeholder. If anything is in
111
+ * in the root the placeholder should not be shown.
112
+ * @param isComposing - Is the editor in composition mode due to an active Input Method Editor?
113
+ * @returns true if the input should show the placeholder, false otherwise.
114
+ */
115
+
82
116
  function $canShowPlaceholder(isComposing) {
83
117
  if (!$isRootTextContentEmpty(isComposing, false)) {
84
118
  return false;
@@ -123,9 +157,35 @@ function $canShowPlaceholder(isComposing) {
123
157
 
124
158
  return true;
125
159
  }
160
+ /**
161
+ * Returns a function that executes {@link $canShowPlaceholder}
162
+ * @param isEditorComposing - Is the editor in composition mode due to an active Input Method Editor?
163
+ * @returns A function that executes $canShowPlaceholder with arguments.
164
+ */
165
+
126
166
  function $canShowPlaceholderCurry(isEditorComposing) {
127
167
  return () => $canShowPlaceholder(isEditorComposing);
128
168
  }
169
+
170
+ /**
171
+ * Returns a touple that can be rested (...) into mergeRegister to clean up
172
+ * node transforms listeners that transforms text into another node, eg. a HashtagNode.
173
+ * @example
174
+ * ```ts
175
+ * useEffect(() => {
176
+ return mergeRegister(
177
+ ...registerLexicalTextEntity(editor, getMatch, targetNode, createNode),
178
+ );
179
+ }, [createNode, editor, getMatch, targetNode]);
180
+ * ```
181
+ * Where targetNode is the type of node containing the text you want to transform (like a text input),
182
+ * then getMatch uses a regex to find a matching text and creates the proper node to include the matching text.
183
+ * @param editor - The lexical editor.
184
+ * @param getMatch - Finds a matching string that satisfies a regex expression.
185
+ * @param targetNode - The node type that contains text to match with. eg. HashtagNode
186
+ * @param createNode - A function that creates a new node to contain the matched text. eg createHashtagNode
187
+ * @returns An array containing the plain text and reverse node transform listeners.
188
+ */
129
189
  function registerLexicalTextEntity(editor, getMatch, targetNode, createNode) {
130
190
  const isTargetNode = node => {
131
191
  return node instanceof targetNode;
package/index.d.ts CHANGED
@@ -12,17 +12,70 @@ export declare type TextNodeWithOffset = {
12
12
  node: TextNode;
13
13
  offset: number;
14
14
  };
15
+ /**
16
+ * Finds a TextNode with a size larger than targetCharacters and returns
17
+ * the node along with the remaining length of the text.
18
+ * @param root - The RootNode.
19
+ * @param targetCharacters - The number of characters whose TextNode must be larger than.
20
+ * @returns The TextNode and the intersections offset, or null if no TextNode is found.
21
+ */
15
22
  export declare function $findTextIntersectionFromCharacters(root: RootNode, targetCharacters: number): null | {
16
23
  node: TextNode;
17
24
  offset: number;
18
25
  };
26
+ /**
27
+ * Determines if the root has any text content and can trim any whitespace if it does.
28
+ * @param isEditorComposing - Is the editor in composition mode due to an active Input Method Editor?
29
+ * @param trim - Should the root text have its whitespaced trimmed? Defaults to true.
30
+ * @returns true if text content is empty, false if there is text or isEditorComposing is true.
31
+ */
19
32
  export declare function $isRootTextContentEmpty(isEditorComposing: boolean, trim?: boolean): boolean;
33
+ /**
34
+ * Returns a function that executes {@link $isRootTextContentEmpty}
35
+ * @param isEditorComposing - Is the editor in composition mode due to an active Input Method Editor?
36
+ * @param trim - Should the root text have its whitespaced trimmed? Defaults to true.
37
+ * @returns A function that executes $isRootTextContentEmpty based on arguments.
38
+ */
20
39
  export declare function $isRootTextContentEmptyCurry(isEditorComposing: boolean, trim?: boolean): () => boolean;
40
+ /**
41
+ * Returns the root's text content.
42
+ * @returns The root's text content.
43
+ */
21
44
  export declare function $rootTextContent(): string;
45
+ /**
46
+ * Determines if the input should show the placeholder. If anything is in
47
+ * in the root the placeholder should not be shown.
48
+ * @param isComposing - Is the editor in composition mode due to an active Input Method Editor?
49
+ * @returns true if the input should show the placeholder, false otherwise.
50
+ */
22
51
  export declare function $canShowPlaceholder(isComposing: boolean): boolean;
52
+ /**
53
+ * Returns a function that executes {@link $canShowPlaceholder}
54
+ * @param isEditorComposing - Is the editor in composition mode due to an active Input Method Editor?
55
+ * @returns A function that executes $canShowPlaceholder with arguments.
56
+ */
23
57
  export declare function $canShowPlaceholderCurry(isEditorComposing: boolean): () => boolean;
24
58
  export declare type EntityMatch = {
25
59
  end: number;
26
60
  start: number;
27
61
  };
62
+ /**
63
+ * Returns a touple that can be rested (...) into mergeRegister to clean up
64
+ * node transforms listeners that transforms text into another node, eg. a HashtagNode.
65
+ * @example
66
+ * ```ts
67
+ * useEffect(() => {
68
+ return mergeRegister(
69
+ ...registerLexicalTextEntity(editor, getMatch, targetNode, createNode),
70
+ );
71
+ }, [createNode, editor, getMatch, targetNode]);
72
+ * ```
73
+ * Where targetNode is the type of node containing the text you want to transform (like a text input),
74
+ * then getMatch uses a regex to find a matching text and creates the proper node to include the matching text.
75
+ * @param editor - The lexical editor.
76
+ * @param getMatch - Finds a matching string that satisfies a regex expression.
77
+ * @param targetNode - The node type that contains text to match with. eg. HashtagNode
78
+ * @param createNode - A function that creates a new node to contain the matched text. eg createHashtagNode
79
+ * @returns An array containing the plain text and reverse node transform listeners.
80
+ */
28
81
  export declare function registerLexicalTextEntity<T extends TextNode>(editor: LexicalEditor, getMatch: (text: string) => null | EntityMatch, targetNode: Klass<T>, createNode: (textNode: TextNode) => T): Array<() => void>;
package/package.json CHANGED
@@ -9,10 +9,10 @@
9
9
  "text"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.9.1",
12
+ "version": "0.9.2",
13
13
  "main": "LexicalText.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.9.1"
15
+ "lexical": "0.9.2"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",