@lexical/text 0.1.15

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Dominic Gannaway
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,223 @@
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
+ 'use strict';
8
+
9
+ var lexical = require('lexical');
10
+
11
+ /**
12
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
13
+ *
14
+ * This source code is licensed under the MIT license found in the
15
+ * LICENSE file in the root directory of this source tree.
16
+ *
17
+ *
18
+ */
19
+ function $findTextIntersectionFromCharacters(root, targetCharacters) {
20
+ let node = root.getFirstChild();
21
+ let currentCharacters = 0;
22
+
23
+ mainLoop: while (node !== null) {
24
+ if (lexical.$isElementNode(node)) {
25
+ const child = node.getFirstChild();
26
+
27
+ if (child !== null) {
28
+ node = child;
29
+ continue;
30
+ }
31
+ } else if (lexical.$isTextNode(node)) {
32
+ const characters = node.getTextContentSize();
33
+
34
+ if (currentCharacters + characters > targetCharacters) {
35
+ return {
36
+ node,
37
+ offset: targetCharacters - currentCharacters
38
+ };
39
+ }
40
+
41
+ currentCharacters += characters;
42
+ }
43
+
44
+ const sibling = node.getNextSibling();
45
+
46
+ if (sibling !== null) {
47
+ node = sibling;
48
+ continue;
49
+ }
50
+
51
+ let parent = node.getParent();
52
+
53
+ while (parent !== null) {
54
+ const parentSibling = parent.getNextSibling();
55
+
56
+ if (parentSibling !== null) {
57
+ node = parentSibling;
58
+ continue mainLoop;
59
+ }
60
+
61
+ parent = parent.getParent();
62
+ }
63
+
64
+ break;
65
+ }
66
+
67
+ return null;
68
+ } // Return text content for child text nodes. Each non-text node is separated by input string.
69
+ // Caution, this function creates a string and should not be used within a tight loop.
70
+ // Use $getNodeWithOffsetsFromJoinedTextNodesFromElementNode below to convert
71
+ // indexes in the return string back into their corresponding node and offsets.
72
+
73
+ function $joinTextNodesInElementNode(elementNode, separator, stopAt) {
74
+ let textContent = '';
75
+ const children = elementNode.getChildren();
76
+ const length = children.length;
77
+
78
+ for (let i = 0; i < length; ++i) {
79
+ const child = children[i];
80
+
81
+ if (lexical.$isTextNode(child)) {
82
+ const childTextContent = child.getTextContent();
83
+
84
+ if (child.is(stopAt.node)) {
85
+ if (stopAt.offset > childTextContent.length) {
86
+ {
87
+ throw Error(`Node ${child.__key} and selection point do not match.`);
88
+ }
89
+ }
90
+
91
+ textContent += child.getTextContent().substr(0, stopAt.offset);
92
+ break;
93
+ } else {
94
+ textContent += childTextContent;
95
+ }
96
+ } else {
97
+ textContent += separator;
98
+ }
99
+ }
100
+
101
+ return textContent;
102
+ } // This function converts the offsetInJoinedText to
103
+ // a node and offset result or null if not found.
104
+ // This function is to be used in conjunction with joinTextNodesInElementNode above.
105
+ // The joinedTextContent should be return value from joinTextNodesInElementNode.
106
+ //
107
+ // The offsetInJoinedText is relative to the entire string which
108
+ // itself is relevant to the parent ElementNode.
109
+ //
110
+ // Example:
111
+ // Given a Paragraph with 2 TextNodes. The first is Hello, the second is World.
112
+ // The joinedTextContent would be "HelloWorld"
113
+ // The offsetInJoinedText might be for the letter "e" = 1 or "r" = 7.
114
+ // The return values would be {TextNode1, 1} or {TextNode2,2}, respectively.
115
+
116
+ function $findNodeWithOffsetFromJoinedText(offsetInJoinedText, joinedTextLength, separatorLength, elementNode) {
117
+ const children = elementNode.getChildren();
118
+ const childrenLength = children.length;
119
+ let runningLength = 0;
120
+ let isPriorNodeTextNode = false;
121
+
122
+ for (let i = 0; i < childrenLength; ++i) {
123
+ // We must examine the offsetInJoinedText that is located
124
+ // at the length of the string.
125
+ // For example, given "hello", the length is 5, yet
126
+ // the caller still wants the node + offset at the
127
+ // right edge of the "o".
128
+ if (runningLength > joinedTextLength) {
129
+ break;
130
+ }
131
+
132
+ const child = children[i];
133
+ const isChildNodeTestNode = lexical.$isTextNode(child);
134
+ const childContentLength = isChildNodeTestNode ? child.getTextContent().length : separatorLength;
135
+ const newRunningLength = runningLength + childContentLength;
136
+ const isJoinedOffsetWithinNode = isPriorNodeTextNode === false && runningLength === offsetInJoinedText || runningLength === 0 && runningLength === offsetInJoinedText || runningLength < offsetInJoinedText && offsetInJoinedText <= newRunningLength;
137
+
138
+ if (isJoinedOffsetWithinNode && lexical.$isTextNode(child)) {
139
+ // Check isTextNode again for flow.
140
+ return {
141
+ node: child,
142
+ offset: offsetInJoinedText - runningLength
143
+ };
144
+ }
145
+
146
+ runningLength = newRunningLength;
147
+ isPriorNodeTextNode = isChildNodeTestNode;
148
+ }
149
+
150
+ return null;
151
+ }
152
+ function $isRootTextContentEmpty(isEditorComposing, trim = true) {
153
+ if (isEditorComposing) {
154
+ return false;
155
+ }
156
+
157
+ let text = $rootTextContentCurry();
158
+
159
+ if (trim) {
160
+ text = text.trim();
161
+ }
162
+
163
+ return text === '';
164
+ }
165
+ function $isRootTextContentEmptyCurry(isEditorComposing, trim) {
166
+ return () => $isRootTextContentEmpty(isEditorComposing, trim);
167
+ }
168
+ function $rootTextContentCurry() {
169
+ const root = lexical.$getRoot();
170
+ return root.getTextContent();
171
+ }
172
+ function $canShowPlaceholder(isComposing) {
173
+ if (!$isRootTextContentEmpty(isComposing, false)) {
174
+ return false;
175
+ }
176
+
177
+ const root = lexical.$getRoot();
178
+ const children = root.getChildren();
179
+ const childrenLength = children.length;
180
+
181
+ if (childrenLength > 1) {
182
+ return false;
183
+ }
184
+
185
+ for (let i = 0; i < childrenLength; i++) {
186
+ const topBlock = children[i];
187
+
188
+ if (lexical.$isElementNode(topBlock)) {
189
+ if (topBlock.__type !== 'paragraph') {
190
+ return false;
191
+ }
192
+
193
+ if (topBlock.__indent !== 0) {
194
+ return false;
195
+ }
196
+
197
+ const topBlockChildren = topBlock.getChildren();
198
+ const topBlockChildrenLength = topBlockChildren.length;
199
+
200
+ for (let s = 0; s < topBlockChildrenLength; s++) {
201
+ const child = topBlockChildren[i];
202
+
203
+ if (!lexical.$isTextNode(child)) {
204
+ return false;
205
+ }
206
+ }
207
+ }
208
+ }
209
+
210
+ return true;
211
+ }
212
+ function $canShowPlaceholderCurry(isEditorComposing) {
213
+ return () => $canShowPlaceholder(isEditorComposing);
214
+ }
215
+
216
+ exports.$canShowPlaceholder = $canShowPlaceholder;
217
+ exports.$canShowPlaceholderCurry = $canShowPlaceholderCurry;
218
+ exports.$findNodeWithOffsetFromJoinedText = $findNodeWithOffsetFromJoinedText;
219
+ exports.$findTextIntersectionFromCharacters = $findTextIntersectionFromCharacters;
220
+ exports.$isRootTextContentEmpty = $isRootTextContentEmpty;
221
+ exports.$isRootTextContentEmptyCurry = $isRootTextContentEmptyCurry;
222
+ exports.$joinTextNodesInElementNode = $joinTextNodesInElementNode;
223
+ exports.$rootTextContentCurry = $rootTextContentCurry;
package/LexicalText.js ADDED
@@ -0,0 +1,9 @@
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
+ 'use strict'
8
+ const LexicalText = process.env.NODE_ENV === 'development' ? require('./LexicalText.dev.js') : require('./LexicalText.prod.js')
9
+ module.exports = LexicalText;
@@ -0,0 +1,11 @@
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
+ var f=require("lexical");function m(a,e=!0){if(a)return!1;a=p();e&&(a=a.trim());return""===a}function p(){return f.$getRoot().getTextContent()}function q(a){if(!m(a,!1))return!1;a=f.$getRoot().getChildren();const e=a.length;if(1<e)return!1;for(let c=0;c<e;c++){var b=a[c];if(f.$isElementNode(b)){if("paragraph"!==b.__type||0!==b.__indent)return!1;b=b.getChildren();const k=b.length;for(let d=0;d<k;d++)if(!f.$isTextNode(b[c]))return!1}}return!0}exports.$canShowPlaceholder=q;
8
+ exports.$canShowPlaceholderCurry=function(a){return()=>q(a)};exports.$findNodeWithOffsetFromJoinedText=function(a,e,b,c){c=c.getChildren();const k=c.length;let d=0,g=!1;for(let n=0;n<k&&!(d>e);++n){const l=c[n],r=f.$isTextNode(l);var h=r?l.getTextContent().length:b;h=d+h;if((!1===g&&d===a||0===d&&d===a||d<a&&a<=h)&&f.$isTextNode(l))return{node:l,offset:a-d};d=h;g=r}return null};
9
+ exports.$findTextIntersectionFromCharacters=function(a,e){var b=a.getFirstChild();a=0;a:for(;null!==b;){if(f.$isElementNode(b)){var c=b.getFirstChild();if(null!==c){b=c;continue}}else if(f.$isTextNode(b)){c=b.getTextContentSize();if(a+c>e)return{node:b,offset:e-a};a+=c}c=b.getNextSibling();if(null!==c)b=c;else{for(b=b.getParent();null!==b;){c=b.getNextSibling();if(null!==c){b=c;continue a}b=b.getParent()}break}}return null};exports.$isRootTextContentEmpty=m;
10
+ exports.$isRootTextContentEmptyCurry=function(a,e){return()=>m(a,e)};
11
+ exports.$joinTextNodesInElementNode=function(a,e,b){let c="";a=a.getChildren();const k=a.length;for(let d=0;d<k;++d){const g=a[d];if(f.$isTextNode(g)){const h=g.getTextContent();if(g.is(b.node)){if(b.offset>h.length)throw Error("Minified Lexical error #50; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");c+=g.getTextContent().substr(0,b.offset);break}else c+=h}else c+=e}return c};exports.$rootTextContentCurry=p;
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # `@lexical/text`
2
+
3
+ This package contains utilities and helpers for handling Lexical text.
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@lexical/text",
3
+ "author": {
4
+ "name": "Dominic Gannaway",
5
+ "email": "dg@domgan.com"
6
+ },
7
+ "description": "This package contains utilities and helpers for handling Lexical text.",
8
+ "keywords": [
9
+ "lexical",
10
+ "editor",
11
+ "rich-text",
12
+ "list",
13
+ "text"
14
+ ],
15
+ "license": "MIT",
16
+ "version": "0.1.15",
17
+ "main": "LexicalText.js",
18
+ "peerDependencies": {
19
+ "lexical": "0.1.15"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/facebook/lexical",
24
+ "directory": "packages/lexical-text"
25
+ }
26
+ }