@lexical/text 0.8.0 → 0.9.0

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