@lexical/text 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -57,89 +57,6 @@ function $findTextIntersectionFromCharacters(root, targetCharacters) {
57
57
  break;
58
58
  }
59
59
 
60
- return null;
61
- } // Return text content for child text nodes. Each non-text node is separated by input string.
62
- // Caution, this function creates a string and should not be used within a tight loop.
63
- // Use $getNodeWithOffsetsFromJoinedTextNodesFromElementNode below to convert
64
- // indexes in the return string back into their corresponding node and offsets.
65
-
66
- function $joinTextNodesInElementNode(elementNode, separator, stopAt) {
67
- let textContent = '';
68
- const children = elementNode.getChildren();
69
- const length = children.length;
70
-
71
- for (let i = 0; i < length; ++i) {
72
- const child = children[i];
73
-
74
- if (lexical.$isTextNode(child)) {
75
- const childTextContent = child.getTextContent();
76
-
77
- if (child.is(stopAt.node)) {
78
- if (stopAt.offset > childTextContent.length) {
79
- {
80
- throw Error(`Node ${child.__key} and selection point do not match.`);
81
- }
82
- }
83
-
84
- textContent += child.getTextContent().substr(0, stopAt.offset);
85
- break;
86
- } else {
87
- textContent += childTextContent;
88
- }
89
- } else {
90
- textContent += separator;
91
- }
92
- }
93
-
94
- return textContent;
95
- } // This function converts the offsetInJoinedText to
96
- // a node and offset result or null if not found.
97
- // This function is to be used in conjunction with joinTextNodesInElementNode above.
98
- // The joinedTextContent should be return value from joinTextNodesInElementNode.
99
- //
100
- // The offsetInJoinedText is relative to the entire string which
101
- // itself is relevant to the parent ElementNode.
102
- //
103
- // Example:
104
- // Given a Paragraph with 2 TextNodes. The first is Hello, the second is World.
105
- // The joinedTextContent would be "HelloWorld"
106
- // The offsetInJoinedText might be for the letter "e" = 1 or "r" = 7.
107
- // The return values would be {TextNode1, 1} or {TextNode2,2}, respectively.
108
-
109
- function $findNodeWithOffsetFromJoinedText(offsetInJoinedText, joinedTextLength, separatorLength, elementNode) {
110
- const children = elementNode.getChildren();
111
- const childrenLength = children.length;
112
- let runningLength = 0;
113
- let isPriorNodeTextNode = false;
114
-
115
- for (let i = 0; i < childrenLength; ++i) {
116
- // We must examine the offsetInJoinedText that is located
117
- // at the length of the string.
118
- // For example, given "hello", the length is 5, yet
119
- // the caller still wants the node + offset at the
120
- // right edge of the "o".
121
- if (runningLength > joinedTextLength) {
122
- break;
123
- }
124
-
125
- const child = children[i];
126
- const isChildNodeTestNode = lexical.$isTextNode(child);
127
- const childContentLength = isChildNodeTestNode ? child.getTextContent().length : separatorLength;
128
- const newRunningLength = runningLength + childContentLength;
129
- const isJoinedOffsetWithinNode = isPriorNodeTextNode === false && runningLength === offsetInJoinedText || runningLength === 0 && runningLength === offsetInJoinedText || runningLength < offsetInJoinedText && offsetInJoinedText <= newRunningLength;
130
-
131
- if (isJoinedOffsetWithinNode && lexical.$isTextNode(child)) {
132
- // Check isTextNode again for flow.
133
- return {
134
- node: child,
135
- offset: offsetInJoinedText - runningLength
136
- };
137
- }
138
-
139
- runningLength = newRunningLength;
140
- isPriorNodeTextNode = isChildNodeTestNode;
141
- }
142
-
143
60
  return null;
144
61
  }
145
62
  function $isRootTextContentEmpty(isEditorComposing, trim = true) {
@@ -162,8 +79,7 @@ function $rootTextContent() {
162
79
  const root = lexical.$getRoot();
163
80
  return root.getTextContent();
164
81
  }
165
- function $canShowPlaceholder(isComposing, // TODO 0.5 make mandatory
166
- isEditable = true) {
82
+ function $canShowPlaceholder(isComposing, isEditable) {
167
83
  if (!isEditable || !$isRootTextContentEmpty(isComposing, false)) {
168
84
  return false;
169
85
  }
@@ -203,8 +119,7 @@ isEditable = true) {
203
119
 
204
120
  return true;
205
121
  }
206
- function $canShowPlaceholderCurry(isEditorComposing, // TODO 0.5 make mandatory
207
- isEditable = true) {
122
+ function $canShowPlaceholderCurry(isEditorComposing, isEditable) {
208
123
  return () => $canShowPlaceholder(isEditorComposing, isEditable);
209
124
  }
210
125
  function registerLexicalTextEntity(editor, getMatch, targetNode, createNode) {
@@ -363,10 +278,8 @@ function registerLexicalTextEntity(editor, getMatch, targetNode, createNode) {
363
278
 
364
279
  exports.$canShowPlaceholder = $canShowPlaceholder;
365
280
  exports.$canShowPlaceholderCurry = $canShowPlaceholderCurry;
366
- exports.$findNodeWithOffsetFromJoinedText = $findNodeWithOffsetFromJoinedText;
367
281
  exports.$findTextIntersectionFromCharacters = $findTextIntersectionFromCharacters;
368
282
  exports.$isRootTextContentEmpty = $isRootTextContentEmpty;
369
283
  exports.$isRootTextContentEmptyCurry = $isRootTextContentEmptyCurry;
370
- exports.$joinTextNodesInElementNode = $joinTextNodesInElementNode;
371
284
  exports.$rootTextContent = $rootTextContent;
372
285
  exports.registerLexicalTextEntity = registerLexicalTextEntity;
@@ -18,17 +18,6 @@ declare export function $findTextIntersectionFromCharacters(
18
18
  node: TextNode,
19
19
  offset: number,
20
20
  };
21
- declare export function $joinTextNodesInElementNode(
22
- elementNode: ElementNode,
23
- separator: string,
24
- stopAt: TextNodeWithOffset,
25
- ): string;
26
- declare export function $findNodeWithOffsetFromJoinedText(
27
- offsetInJoinedText: number,
28
- joinedTextLength: number,
29
- separatorLength: number,
30
- elementNode: ElementNode,
31
- ): ?TextNodeWithOffset;
32
21
  declare export function $isRootTextContentEmpty(
33
22
  isEditorComposing: boolean,
34
23
  trim?: boolean,
@@ -4,12 +4,9 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- 'use strict';var k=require("lexical");function r(b,f=!0){if(b)return!1;b=t();f&&(b=b.trim());return""===b}function t(){return k.$getRoot().getTextContent()}function u(b,f=!0){if(!f||!r(b,!1))return!1;b=k.$getRoot().getChildren();f=b.length;if(1<f)return!1;for(let e=0;e<f;e++){var c=b[e];if(k.$isElementNode(c)){if("paragraph"!==c.__type||0!==c.__indent)return!1;c=c.getChildren();let p=c.length;for(let g=0;g<p;g++)if(!k.$isTextNode(c[e]))return!1}}return!0}exports.$canShowPlaceholder=u;
8
- exports.$canShowPlaceholderCurry=function(b,f=!0){return()=>u(b,f)};exports.$findNodeWithOffsetFromJoinedText=function(b,f,c,e){e=e.getChildren();let p=e.length,g=0,a=!1;for(let l=0;l<p&&!(g>f);++l){let m=e[l],n=k.$isTextNode(m);var d=n?m.getTextContent().length:c;d=g+d;if((!1===a&&g===b||0===g&&g===b||g<b&&b<=d)&&k.$isTextNode(m))return{node:m,offset:b-g};g=d;a=n}return null};
9
- exports.$findTextIntersectionFromCharacters=function(b,f){var c=b.getFirstChild();b=0;a:for(;null!==c;){if(k.$isElementNode(c)){var e=c.getFirstChild();if(null!==e){c=e;continue}}else if(k.$isTextNode(c)){e=c.getTextContentSize();if(b+e>f)return{node:c,offset:f-b};b+=e}e=c.getNextSibling();if(null!==e)c=e;else{for(c=c.getParent();null!==c;){e=c.getNextSibling();if(null!==e){c=e;continue a}c=c.getParent()}break}}return null};exports.$isRootTextContentEmpty=r;
10
- exports.$isRootTextContentEmptyCurry=function(b,f){return()=>r(b,f)};
11
- exports.$joinTextNodesInElementNode=function(b,f,c){let e="";b=b.getChildren();let p=b.length;for(let g=0;g<p;++g){let a=b[g];if(k.$isTextNode(a)){let d=a.getTextContent();if(a.is(c.node)){if(c.offset>d.length)throw Error("Minified Lexical error #2; visit https://lexical.dev/docs/error?code=2 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");e+=a.getTextContent().substr(0,c.offset);break}else e+=d}else e+=f}return e};
12
- exports.$rootTextContent=t;
13
- exports.registerLexicalTextEntity=function(b,f,c,e){let p=a=>{const d=k.$createTextNode(a.getTextContent());d.setFormat(a.getFormat());a.replace(d)},g=b.registerNodeTransform(k.TextNode,a=>{if(a.isSimpleText()){var d=a.getPreviousSibling(),l=a.getTextContent(),m=a;if(k.$isTextNode(d)){var n=d.getTextContent(),h=f(n+l);if(d instanceof c){if(null===h||0!==d.getLatest().__mode){p(d);return}h=h.end-n.length;if(0<h){m=l.slice(0,h);m=n+m;d.select();d.setTextContent(m);h===l.length?a.remove():(d=l.slice(h),
14
- a.setTextContent(d));return}}else if(null===h||h.start<n.length)return}for(;;){a=f(l);l=h=null===a?"":l.slice(a.end);if(""===h){if(n=m.getNextSibling(),k.$isTextNode(n))if(h=m.getTextContent()+n.getTextContent(),h=f(h),null===h){n instanceof c?p(n):n.markDirty();break}else if(0!==h.start)break}else if(n=f(h),null!==n&&0===n.start)break;if(null===a)break;if(0===a.start&&k.$isTextNode(d)&&d.isTextEntity())continue;let q;0===a.start?[q,m]=m.splitText(a.end):[,q,m]=m.splitText(a.start,a.end);a=e(q);q.replace(a);
15
- if(null==m)break}}});b=b.registerNodeTransform(c,a=>{var d=a.getTextContent();const l=f(d);null===l||0!==l.start?p(a):d.length>l.end?a.splitText(l.end):(d=a.getPreviousSibling(),k.$isTextNode(d)&&d.isTextEntity()&&(p(d),p(a)),d=a.getNextSibling(),k.$isTextNode(d)&&d.isTextEntity()&&(p(d),a instanceof c&&p(a)))});return[g,b]}
7
+ 'use strict';var h=require("lexical");function r(c,e=!0){if(c)return!1;c=t();e&&(c=c.trim());return""===c}function t(){return h.$getRoot().getTextContent()}function u(c,e){if(!e||!r(c,!1))return!1;c=h.$getRoot().getChildren();e=c.length;if(1<e)return!1;for(let f=0;f<e;f++){var b=c[f];if(h.$isElementNode(b)){if("paragraph"!==b.__type||0!==b.__indent)return!1;b=b.getChildren();let n=b.length;for(let p=0;p<n;p++)if(!h.$isTextNode(b[f]))return!1}}return!0}exports.$canShowPlaceholder=u;
8
+ exports.$canShowPlaceholderCurry=function(c,e){return()=>u(c,e)};exports.$findTextIntersectionFromCharacters=function(c,e){var b=c.getFirstChild();c=0;a:for(;null!==b;){if(h.$isElementNode(b)){var f=b.getFirstChild();if(null!==f){b=f;continue}}else if(h.$isTextNode(b)){f=b.getTextContentSize();if(c+f>e)return{node:b,offset:e-c};c+=f}f=b.getNextSibling();if(null!==f)b=f;else{for(b=b.getParent();null!==b;){f=b.getNextSibling();if(null!==f){b=f;continue a}b=b.getParent()}break}}return null};
9
+ exports.$isRootTextContentEmpty=r;exports.$isRootTextContentEmptyCurry=function(c,e){return()=>r(c,e)};exports.$rootTextContent=t;
10
+ exports.registerLexicalTextEntity=function(c,e,b,f){let n=a=>{const d=h.$createTextNode(a.getTextContent());d.setFormat(a.getFormat());a.replace(d)},p=c.registerNodeTransform(h.TextNode,a=>{if(a.isSimpleText()){var d=a.getPreviousSibling(),l=a.getTextContent(),m=a;if(h.$isTextNode(d)){var k=d.getTextContent(),g=e(k+l);if(d instanceof b){if(null===g||0!==d.getLatest().__mode){n(d);return}g=g.end-k.length;if(0<g){m=l.slice(0,g);m=k+m;d.select();d.setTextContent(m);g===l.length?a.remove():(d=l.slice(g),
11
+ a.setTextContent(d));return}}else if(null===g||g.start<k.length)return}for(;;){a=e(l);l=g=null===a?"":l.slice(a.end);if(""===g){if(k=m.getNextSibling(),h.$isTextNode(k))if(g=m.getTextContent()+k.getTextContent(),g=e(g),null===g){k instanceof b?n(k):k.markDirty();break}else if(0!==g.start)break}else if(k=e(g),null!==k&&0===k.start)break;if(null===a)break;if(0===a.start&&h.$isTextNode(d)&&d.isTextEntity())continue;let q;0===a.start?[q,m]=m.splitText(a.end):[,q,m]=m.splitText(a.start,a.end);a=f(q);q.replace(a);
12
+ if(null==m)break}}});c=c.registerNodeTransform(b,a=>{var d=a.getTextContent();const l=e(d);null===l||0!==l.start?n(a):d.length>l.end?a.splitText(l.end):(d=a.getPreviousSibling(),h.$isTextNode(d)&&d.isTextEntity()&&(n(d),n(a)),d=a.getNextSibling(),h.$isTextNode(d)&&d.isTextEntity()&&(n(d),a instanceof b&&n(a)))});return[p,c]}
package/index.d.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  *
8
8
  */
9
- import type { ElementNode, Klass, LexicalEditor, RootNode } from 'lexical';
9
+ import type { Klass, LexicalEditor, RootNode } from 'lexical';
10
10
  import { TextNode } from 'lexical';
11
11
  export declare type TextNodeWithOffset = {
12
12
  node: TextNode;
@@ -16,13 +16,11 @@ export declare function $findTextIntersectionFromCharacters(root: RootNode, targ
16
16
  node: TextNode;
17
17
  offset: number;
18
18
  };
19
- export declare function $joinTextNodesInElementNode(elementNode: ElementNode, separator: string, stopAt: TextNodeWithOffset): string;
20
- export declare function $findNodeWithOffsetFromJoinedText(offsetInJoinedText: number, joinedTextLength: number, separatorLength: number, elementNode: ElementNode): TextNodeWithOffset | null;
21
19
  export declare function $isRootTextContentEmpty(isEditorComposing: boolean, trim?: boolean): boolean;
22
20
  export declare function $isRootTextContentEmptyCurry(isEditorComposing: boolean, trim?: boolean): () => boolean;
23
21
  export declare function $rootTextContent(): string;
24
- export declare function $canShowPlaceholder(isComposing: boolean, isEditable?: boolean): boolean;
25
- export declare function $canShowPlaceholderCurry(isEditorComposing: boolean, isEditable?: boolean): () => boolean;
22
+ export declare function $canShowPlaceholder(isComposing: boolean, isEditable: boolean): boolean;
23
+ export declare function $canShowPlaceholderCurry(isEditorComposing: boolean, isEditable: boolean): () => boolean;
26
24
  export declare type EntityMatch = {
27
25
  end: number;
28
26
  start: number;
package/package.json CHANGED
@@ -9,10 +9,10 @@
9
9
  "text"
10
10
  ],
11
11
  "license": "MIT",
12
- "version": "0.4.0",
12
+ "version": "0.5.0",
13
13
  "main": "LexicalText.js",
14
14
  "peerDependencies": {
15
- "lexical": "0.4.0"
15
+ "lexical": "0.5.0"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",