@lexical/text
Advanced tools
Comparing version 0.8.1 to 0.9.0
@@ -15,5 +15,7 @@ /** | ||
let currentCharacters = 0; | ||
mainLoop: while (node !== null) { | ||
if (lexical.$isElementNode(node)) { | ||
const child = node.getFirstChild(); | ||
if (child !== null) { | ||
@@ -25,2 +27,3 @@ node = child; | ||
const characters = node.getTextContentSize(); | ||
if (currentCharacters + characters > targetCharacters) { | ||
@@ -32,5 +35,8 @@ return { | ||
} | ||
currentCharacters += characters; | ||
} | ||
const sibling = node.getNextSibling(); | ||
if (sibling !== null) { | ||
@@ -40,5 +46,8 @@ node = sibling; | ||
} | ||
let parent = node.getParent(); | ||
while (parent !== null) { | ||
const parentSibling = parent.getNextSibling(); | ||
if (parentSibling !== null) { | ||
@@ -48,6 +57,9 @@ node = parentSibling; | ||
} | ||
parent = parent.getParent(); | ||
} | ||
break; | ||
} | ||
return null; | ||
@@ -59,6 +71,9 @@ } | ||
} | ||
let text = $rootTextContent(); | ||
if (trim) { | ||
text = text.trim(); | ||
} | ||
return text === ''; | ||
@@ -77,10 +92,14 @@ } | ||
} | ||
const root = lexical.$getRoot(); | ||
const children = root.getChildren(); | ||
const childrenLength = children.length; | ||
if (childrenLength > 1) { | ||
return false; | ||
} | ||
for (let i = 0; i < childrenLength; i++) { | ||
const topBlock = children[i]; | ||
if (lexical.$isElementNode(topBlock)) { | ||
@@ -90,9 +109,13 @@ if (!lexical.$isParagraphNode(topBlock)) { | ||
} | ||
if (topBlock.__indent !== 0) { | ||
return false; | ||
} | ||
const topBlockChildren = topBlock.getChildren(); | ||
const topBlockChildrenLength = topBlockChildren.length; | ||
for (let s = 0; s < topBlockChildrenLength; s++) { | ||
const child = topBlockChildren[i]; | ||
if (!lexical.$isTextNode(child)) { | ||
@@ -104,2 +127,3 @@ return false; | ||
} | ||
return true; | ||
@@ -114,2 +138,3 @@ } | ||
}; | ||
const replaceWithSimpleText = node => { | ||
@@ -120,5 +145,7 @@ const textNode = lexical.$createTextNode(node.getTextContent()); | ||
}; | ||
const getMode = node => { | ||
return node.getLatest().__mode; | ||
}; | ||
const textNodeTransform = node => { | ||
@@ -128,2 +155,3 @@ if (!node.isSimpleText()) { | ||
} | ||
const prevSibling = node.getPreviousSibling(); | ||
@@ -133,2 +161,3 @@ let text = node.getTextContent(); | ||
let match; | ||
if (lexical.$isTextNode(prevSibling)) { | ||
@@ -138,2 +167,3 @@ const previousText = prevSibling.getTextContent(); | ||
const prevMatch = getMatch(combinedText); | ||
if (isTargetNode(prevSibling)) { | ||
@@ -145,2 +175,3 @@ if (prevMatch === null || getMode(prevSibling) !== 0) { | ||
const diff = prevMatch.end - previousText.length; | ||
if (diff > 0) { | ||
@@ -151,2 +182,3 @@ const concatText = text.slice(0, diff); | ||
prevSibling.setTextContent(newTextContent); | ||
if (diff === text.length) { | ||
@@ -158,2 +190,3 @@ node.remove(); | ||
} | ||
return; | ||
@@ -165,5 +198,5 @@ } | ||
} | ||
} | ||
} // eslint-disable-next-line no-constant-condition | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
@@ -173,7 +206,10 @@ match = getMatch(text); | ||
text = nextText; | ||
if (nextText === '') { | ||
const nextSibling = currentNode.getNextSibling(); | ||
if (lexical.$isTextNode(nextSibling)) { | ||
nextText = currentNode.getTextContent() + nextSibling.getTextContent(); | ||
const nextMatch = getMatch(nextText); | ||
if (nextMatch === null) { | ||
@@ -185,2 +221,3 @@ if (isTargetNode(nextSibling)) { | ||
} | ||
return; | ||
@@ -193,2 +230,3 @@ } else if (nextMatch.start !== 0) { | ||
const nextMatch = getMatch(nextText); | ||
if (nextMatch !== null && nextMatch.start === 0) { | ||
@@ -198,9 +236,13 @@ return; | ||
} | ||
if (match === null) { | ||
return; | ||
} | ||
if (match.start === 0 && lexical.$isTextNode(prevSibling) && prevSibling.isTextEntity()) { | ||
continue; | ||
} | ||
let nodeToReplace; | ||
if (match.start === 0) { | ||
@@ -211,4 +253,6 @@ [nodeToReplace, currentNode] = currentNode.splitText(match.end); | ||
} | ||
const replacementNode = createNode(nodeToReplace); | ||
nodeToReplace.replace(replacementNode); | ||
if (currentNode == null) { | ||
@@ -219,5 +263,7 @@ return; | ||
}; | ||
const reverseNodeTransform = node => { | ||
const text = node.getTextContent(); | ||
const match = getMatch(text); | ||
if (match === null || match.start !== 0) { | ||
@@ -227,2 +273,3 @@ replaceWithSimpleText(node); | ||
} | ||
if (text.length > match.end) { | ||
@@ -233,3 +280,5 @@ // This will split out the rest of the text as simple text | ||
} | ||
const prevSibling = node.getPreviousSibling(); | ||
if (lexical.$isTextNode(prevSibling) && prevSibling.isTextEntity()) { | ||
@@ -239,7 +288,8 @@ replaceWithSimpleText(prevSibling); | ||
} | ||
const nextSibling = node.getNextSibling(); | ||
if (lexical.$isTextNode(nextSibling) && nextSibling.isTextEntity()) { | ||
replaceWithSimpleText(nextSibling); | ||
replaceWithSimpleText(nextSibling); // This may have already been converted in the previous block | ||
// This may have already been converted in the previous block | ||
if (isTargetNode(node)) { | ||
@@ -250,2 +300,3 @@ replaceWithSimpleText(node); | ||
}; | ||
const removePlainTextTransform = editor.registerNodeTransform(lexical.TextNode, textNodeTransform); | ||
@@ -252,0 +303,0 @@ const removeReverseNodeTransform = editor.registerNodeTransform(targetNode, reverseNodeTransform); |
@@ -12,6 +12,6 @@ { | ||
"license": "MIT", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"main": "LexicalText.js", | ||
"peerDependencies": { | ||
"lexical": "0.8.1" | ||
"lexical": "0.9.0" | ||
}, | ||
@@ -18,0 +18,0 @@ "repository": { |
14787
280