@lexical/text
Advanced tools
Comparing version
@@ -63,3 +63,3 @@ /** @module @lexical/text */ | ||
/** | ||
* Returns a touple that can be rested (...) into mergeRegister to clean up | ||
* Returns a tuple that can be rested (...) into mergeRegister to clean up | ||
* node transforms listeners that transforms text into another node, eg. a HashtagNode. | ||
@@ -66,0 +66,0 @@ * @example |
@@ -12,3 +12,2 @@ /** | ||
/** @module @lexical/text */ | ||
/** | ||
@@ -24,7 +23,5 @@ * Finds a TextNode with a size larger than targetCharacters and returns | ||
let currentCharacters = 0; | ||
mainLoop: while (node !== null) { | ||
if (lexical.$isElementNode(node)) { | ||
const child = node.getFirstChild(); | ||
if (child !== null) { | ||
@@ -36,3 +33,2 @@ node = child; | ||
const characters = node.getTextContentSize(); | ||
if (currentCharacters + characters > targetCharacters) { | ||
@@ -44,8 +40,5 @@ return { | ||
} | ||
currentCharacters += characters; | ||
} | ||
const sibling = node.getNextSibling(); | ||
if (sibling !== null) { | ||
@@ -55,8 +48,5 @@ node = sibling; | ||
} | ||
let parent = node.getParent(); | ||
while (parent !== null) { | ||
const parentSibling = parent.getNextSibling(); | ||
if (parentSibling !== null) { | ||
@@ -66,11 +56,9 @@ node = parentSibling; | ||
} | ||
parent = parent.getParent(); | ||
} | ||
break; | ||
} | ||
return null; | ||
} | ||
/** | ||
@@ -82,3 +70,2 @@ * Determines if the root has any text content and can trim any whitespace if it does. | ||
*/ | ||
function $isRootTextContentEmpty(isEditorComposing, trim = true) { | ||
@@ -88,11 +75,9 @@ if (isEditorComposing) { | ||
} | ||
let text = $rootTextContent(); | ||
if (trim) { | ||
text = text.trim(); | ||
} | ||
return text === ''; | ||
} | ||
/** | ||
@@ -104,6 +89,6 @@ * Returns a function that executes {@link $isRootTextContentEmpty} | ||
*/ | ||
function $isRootTextContentEmptyCurry(isEditorComposing, trim) { | ||
return () => $isRootTextContentEmpty(isEditorComposing, trim); | ||
} | ||
/** | ||
@@ -113,3 +98,2 @@ * Returns the root's text content. | ||
*/ | ||
function $rootTextContent() { | ||
@@ -119,2 +103,3 @@ const root = lexical.$getRoot(); | ||
} | ||
/** | ||
@@ -126,3 +111,2 @@ * Determines if the input should show the placeholder. If anything is in | ||
*/ | ||
function $canShowPlaceholder(isComposing) { | ||
@@ -132,18 +116,13 @@ if (!$isRootTextContentEmpty(isComposing, false)) { | ||
} | ||
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.$isDecoratorNode(topBlock)) { | ||
return false; | ||
} | ||
if (lexical.$isElementNode(topBlock)) { | ||
@@ -153,13 +132,9 @@ 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)) { | ||
@@ -171,5 +146,5 @@ return false; | ||
} | ||
return true; | ||
} | ||
/** | ||
@@ -180,9 +155,7 @@ * Returns a function that executes {@link $canShowPlaceholder} | ||
*/ | ||
function $canShowPlaceholderCurry(isEditorComposing) { | ||
return () => $canShowPlaceholder(isEditorComposing); | ||
} | ||
/** | ||
* Returns a touple that can be rested (...) into mergeRegister to clean up | ||
* Returns a tuple that can be rested (...) into mergeRegister to clean up | ||
* node transforms listeners that transforms text into another node, eg. a HashtagNode. | ||
@@ -209,3 +182,2 @@ * @example | ||
}; | ||
const replaceWithSimpleText = node => { | ||
@@ -216,7 +188,5 @@ const textNode = lexical.$createTextNode(node.getTextContent()); | ||
}; | ||
const getMode = node => { | ||
return node.getLatest().__mode; | ||
}; | ||
const textNodeTransform = node => { | ||
@@ -226,3 +196,2 @@ if (!node.isSimpleText()) { | ||
} | ||
const prevSibling = node.getPreviousSibling(); | ||
@@ -232,3 +201,2 @@ let text = node.getTextContent(); | ||
let match; | ||
if (lexical.$isTextNode(prevSibling)) { | ||
@@ -238,3 +206,2 @@ const previousText = prevSibling.getTextContent(); | ||
const prevMatch = getMatch(combinedText); | ||
if (isTargetNode(prevSibling)) { | ||
@@ -246,3 +213,2 @@ if (prevMatch === null || getMode(prevSibling) !== 0) { | ||
const diff = prevMatch.end - previousText.length; | ||
if (diff > 0) { | ||
@@ -253,3 +219,2 @@ const concatText = text.slice(0, diff); | ||
prevSibling.setTextContent(newTextContent); | ||
if (diff === text.length) { | ||
@@ -261,3 +226,2 @@ node.remove(); | ||
} | ||
return; | ||
@@ -269,5 +233,5 @@ } | ||
} | ||
} // eslint-disable-next-line no-constant-condition | ||
} | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
@@ -277,10 +241,7 @@ 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) { | ||
@@ -292,3 +253,2 @@ if (isTargetNode(nextSibling)) { | ||
} | ||
return; | ||
@@ -301,3 +261,2 @@ } else if (nextMatch.start !== 0) { | ||
const nextMatch = getMatch(nextText); | ||
if (nextMatch !== null && nextMatch.start === 0) { | ||
@@ -307,13 +266,9 @@ return; | ||
} | ||
if (match === null) { | ||
return; | ||
} | ||
if (match.start === 0 && lexical.$isTextNode(prevSibling) && prevSibling.isTextEntity()) { | ||
continue; | ||
} | ||
let nodeToReplace; | ||
if (match.start === 0) { | ||
@@ -324,7 +279,5 @@ [nodeToReplace, currentNode] = currentNode.splitText(match.end); | ||
} | ||
const replacementNode = createNode(nodeToReplace); | ||
replacementNode.setFormat(nodeToReplace.getFormat()); | ||
nodeToReplace.replace(replacementNode); | ||
if (currentNode == null) { | ||
@@ -335,7 +288,5 @@ return; | ||
}; | ||
const reverseNodeTransform = node => { | ||
const text = node.getTextContent(); | ||
const match = getMatch(text); | ||
if (match === null || match.start !== 0) { | ||
@@ -345,3 +296,2 @@ replaceWithSimpleText(node); | ||
} | ||
if (text.length > match.end) { | ||
@@ -352,5 +302,3 @@ // This will split out the rest of the text as simple text | ||
} | ||
const prevSibling = node.getPreviousSibling(); | ||
if (lexical.$isTextNode(prevSibling) && prevSibling.isTextEntity()) { | ||
@@ -360,8 +308,7 @@ replaceWithSimpleText(prevSibling); | ||
} | ||
const nextSibling = node.getNextSibling(); | ||
if (lexical.$isTextNode(nextSibling) && nextSibling.isTextEntity()) { | ||
replaceWithSimpleText(nextSibling); // This may have already been converted in the previous block | ||
replaceWithSimpleText(nextSibling); | ||
// This may have already been converted in the previous block | ||
if (isTargetNode(node)) { | ||
@@ -372,3 +319,2 @@ replaceWithSimpleText(node); | ||
}; | ||
const removePlainTextTransform = editor.registerNodeTransform(lexical.TextNode, textNodeTransform); | ||
@@ -375,0 +321,0 @@ const removeReverseNodeTransform = editor.registerNodeTransform(targetNode, reverseNodeTransform); |
@@ -12,6 +12,6 @@ { | ||
"license": "MIT", | ||
"version": "0.12.2", | ||
"version": "0.12.3", | ||
"main": "LexicalText.js", | ||
"peerDependencies": { | ||
"lexical": "0.12.2" | ||
"lexical": "0.12.3" | ||
}, | ||
@@ -18,0 +18,0 @@ "repository": { |
394
0.51%20332
-0.24%