Search code examples
lexicaljs

LexicalJS Stop textNode combination with the same format


I am trying to stop the combining of textNodes within a paragraphNode when the textNodes have the same format. The very basic code is below, where I am trying to split a textNode, with spaces between words, into separate textNodes. However, When I append the new textNodes to the paragraphNode then the textNodes with the same formatting all get combined into a single textNode.

Is this even possible out of the box with textNodes, or should I be extending the textNode in combination with some other elementNodes?

                const currentTextNode = selection.extract()[0];
                const currentParagraph = currentTextNode.getParent();
                const nodeText = currentTextNode.__text;
                if(nodeText.includes(" ")){
                    const splitText = nodeText.split(" ");
                    currentTextNode.remove(true);
                    splitText.forEach((string) => {
                        const newTextNode = $createTextNode(string + ' ');
                        newTextNode.setFormat('bold');
                        currentParagraph.append(newTextNode);
                    });
                } else {
                    currentTextNode.setFormat(0);
                }

Solution

  • You should be able to do this with the toggleUnmergeable method on TextNode.