@lexical/text 0.7.7 → 0.7.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/LexicalText.dev.js +4 -55
  2. package/package.json +2 -2
@@ -12,64 +12,49 @@ var lexical = require('lexical');
12
12
  function $findTextIntersectionFromCharacters(root, targetCharacters) {
13
13
  let node = root.getFirstChild();
14
14
  let currentCharacters = 0;
15
-
16
15
  mainLoop: while (node !== null) {
17
16
  if (lexical.$isElementNode(node)) {
18
17
  const child = node.getFirstChild();
19
-
20
18
  if (child !== null) {
21
19
  node = child;
22
20
  continue;
23
21
  }
24
22
  } else if (lexical.$isTextNode(node)) {
25
23
  const characters = node.getTextContentSize();
26
-
27
24
  if (currentCharacters + characters > targetCharacters) {
28
25
  return {
29
26
  node,
30
27
  offset: targetCharacters - currentCharacters
31
28
  };
32
29
  }
33
-
34
30
  currentCharacters += characters;
35
31
  }
36
-
37
32
  const sibling = node.getNextSibling();
38
-
39
33
  if (sibling !== null) {
40
34
  node = sibling;
41
35
  continue;
42
36
  }
43
-
44
37
  let parent = node.getParent();
45
-
46
38
  while (parent !== null) {
47
39
  const parentSibling = parent.getNextSibling();
48
-
49
40
  if (parentSibling !== null) {
50
41
  node = parentSibling;
51
42
  continue mainLoop;
52
43
  }
53
-
54
44
  parent = parent.getParent();
55
45
  }
56
-
57
46
  break;
58
47
  }
59
-
60
48
  return null;
61
49
  }
62
50
  function $isRootTextContentEmpty(isEditorComposing, trim = true) {
63
51
  if (isEditorComposing) {
64
52
  return false;
65
53
  }
66
-
67
54
  let text = $rootTextContent();
68
-
69
55
  if (trim) {
70
56
  text = text.trim();
71
57
  }
72
-
73
58
  return text === '';
74
59
  }
75
60
  function $isRootTextContentEmptyCurry(isEditorComposing, trim) {
@@ -83,40 +68,31 @@ function $canShowPlaceholder(isComposing) {
83
68
  if (!$isRootTextContentEmpty(isComposing, false)) {
84
69
  return false;
85
70
  }
86
-
87
71
  const root = lexical.$getRoot();
88
72
  const children = root.getChildren();
89
73
  const childrenLength = children.length;
90
-
91
74
  if (childrenLength > 1) {
92
75
  return false;
93
76
  }
94
-
95
77
  for (let i = 0; i < childrenLength; i++) {
96
78
  const topBlock = children[i];
97
-
98
79
  if (lexical.$isElementNode(topBlock)) {
99
80
  if (!lexical.$isParagraphNode(topBlock)) {
100
81
  return false;
101
82
  }
102
-
103
83
  if (topBlock.__indent !== 0) {
104
84
  return false;
105
85
  }
106
-
107
86
  const topBlockChildren = topBlock.getChildren();
108
87
  const topBlockChildrenLength = topBlockChildren.length;
109
-
110
88
  for (let s = 0; s < topBlockChildrenLength; s++) {
111
89
  const child = topBlockChildren[i];
112
-
113
90
  if (!lexical.$isTextNode(child)) {
114
91
  return false;
115
92
  }
116
93
  }
117
94
  }
118
95
  }
119
-
120
96
  return true;
121
97
  }
122
98
  function $canShowPlaceholderCurry(isEditorComposing) {
@@ -126,80 +102,67 @@ function registerLexicalTextEntity(editor, getMatch, targetNode, createNode) {
126
102
  const isTargetNode = node => {
127
103
  return node instanceof targetNode;
128
104
  };
129
-
130
105
  const replaceWithSimpleText = node => {
131
106
  const textNode = lexical.$createTextNode(node.getTextContent());
132
107
  textNode.setFormat(node.getFormat());
133
108
  node.replace(textNode);
134
109
  };
135
-
136
110
  const getMode = node => {
137
111
  return node.getLatest().__mode;
138
112
  };
139
-
140
113
  const textNodeTransform = node => {
141
114
  if (!node.isSimpleText()) {
142
115
  return;
143
116
  }
144
-
145
117
  const prevSibling = node.getPreviousSibling();
146
118
  let text = node.getTextContent();
147
119
  let currentNode = node;
148
120
  let match;
149
-
150
121
  if (lexical.$isTextNode(prevSibling)) {
151
122
  const previousText = prevSibling.getTextContent();
152
123
  const combinedText = previousText + text;
153
124
  const prevMatch = getMatch(combinedText);
154
-
155
125
  if (isTargetNode(prevSibling)) {
156
126
  if (prevMatch === null || getMode(prevSibling) !== 0) {
157
127
  replaceWithSimpleText(prevSibling);
158
128
  return;
159
129
  } else {
160
130
  const diff = prevMatch.end - previousText.length;
161
-
162
131
  if (diff > 0) {
163
132
  const concatText = text.slice(0, diff);
164
133
  const newTextContent = previousText + concatText;
165
134
  prevSibling.select();
166
135
  prevSibling.setTextContent(newTextContent);
167
-
168
136
  if (diff === text.length) {
169
137
  node.remove();
170
138
  } else {
171
139
  const remainingText = text.slice(diff);
172
140
  node.setTextContent(remainingText);
173
141
  }
174
-
175
142
  return;
176
143
  }
177
144
  }
178
145
  } else if (prevMatch === null || prevMatch.start < previousText.length) {
179
146
  return;
180
147
  }
181
- } // eslint-disable-next-line no-constant-condition
182
-
148
+ }
183
149
 
150
+ // eslint-disable-next-line no-constant-condition
184
151
  while (true) {
185
152
  match = getMatch(text);
186
153
  let nextText = match === null ? '' : text.slice(match.end);
187
154
  text = nextText;
188
-
189
155
  if (nextText === '') {
190
156
  const nextSibling = currentNode.getNextSibling();
191
-
192
157
  if (lexical.$isTextNode(nextSibling)) {
193
158
  nextText = currentNode.getTextContent() + nextSibling.getTextContent();
194
159
  const nextMatch = getMatch(nextText);
195
-
196
160
  if (nextMatch === null) {
197
161
  if (isTargetNode(nextSibling)) {
198
162
  replaceWithSimpleText(nextSibling);
199
163
  } else {
200
164
  nextSibling.markDirty();
201
165
  }
202
-
203
166
  return;
204
167
  } else if (nextMatch.start !== 0) {
205
168
  return;
@@ -207,70 +170,56 @@ function registerLexicalTextEntity(editor, getMatch, targetNode, createNode) {
207
170
  }
208
171
  } else {
209
172
  const nextMatch = getMatch(nextText);
210
-
211
173
  if (nextMatch !== null && nextMatch.start === 0) {
212
174
  return;
213
175
  }
214
176
  }
215
-
216
177
  if (match === null) {
217
178
  return;
218
179
  }
219
-
220
180
  if (match.start === 0 && lexical.$isTextNode(prevSibling) && prevSibling.isTextEntity()) {
221
181
  continue;
222
182
  }
223
-
224
183
  let nodeToReplace;
225
-
226
184
  if (match.start === 0) {
227
185
  [nodeToReplace, currentNode] = currentNode.splitText(match.end);
228
186
  } else {
229
187
  [, nodeToReplace, currentNode] = currentNode.splitText(match.start, match.end);
230
188
  }
231
-
232
189
  const replacementNode = createNode(nodeToReplace);
233
190
  nodeToReplace.replace(replacementNode);
234
-
235
191
  if (currentNode == null) {
236
192
  return;
237
193
  }
238
194
  }
239
195
  };
240
-
241
196
  const reverseNodeTransform = node => {
242
197
  const text = node.getTextContent();
243
198
  const match = getMatch(text);
244
-
245
199
  if (match === null || match.start !== 0) {
246
200
  replaceWithSimpleText(node);
247
201
  return;
248
202
  }
249
-
250
203
  if (text.length > match.end) {
251
204
  // This will split out the rest of the text as simple text
252
205
  node.splitText(match.end);
253
206
  return;
254
207
  }
255
-
256
208
  const prevSibling = node.getPreviousSibling();
257
-
258
209
  if (lexical.$isTextNode(prevSibling) && prevSibling.isTextEntity()) {
259
210
  replaceWithSimpleText(prevSibling);
260
211
  replaceWithSimpleText(node);
261
212
  }
262
-
263
213
  const nextSibling = node.getNextSibling();
264
-
265
214
  if (lexical.$isTextNode(nextSibling) && nextSibling.isTextEntity()) {
266
- replaceWithSimpleText(nextSibling); // This may have already been converted in the previous block
215
+ replaceWithSimpleText(nextSibling);
267
216
 
217
+ // This may have already been converted in the previous block
268
218
  if (isTargetNode(node)) {
269
219
  replaceWithSimpleText(node);
270
220
  }
271
221
  }
272
222
  };
273
-
274
223
  const removePlainTextTransform = editor.registerNodeTransform(lexical.TextNode, textNodeTransform);
275
224
  const removeReverseNodeTransform = editor.registerNodeTransform(targetNode, reverseNodeTransform);
276
225
  return [removePlainTextTransform, removeReverseNodeTransform];
package/package.json CHANGED
@@ -9,10 +9,10 @@
9
9
  "text"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.7.7",
12
+ "version": "0.7.9",
13
13
  "main": "LexicalText.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.7.7"
15
+ "lexical": "0.7.9"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",