Insert link node in the middle of a text node #3013
-
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
|
Hm, yea so to make this work you'd need to split the text node at the point of insertion and create three new nodes like: TextNode (This is some) There should be a util for this somewhere like splitText, but you'll probably need to get the split point from the Selection offset at the point where the LinkNode is inserted. |
Beta Was this translation helpful? Give feedback.
-
|
I also encountered this problem, this is how I solved it. import { $insertNodes } from 'lexical'
const insertLink = (text: string, link: string) => {
editor.update(() => {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
const linkNode = $createLinkNode(link, { target: '_blank' });
linkNode.append($createTextNode(text));
$insertNodes([linkNode, $createTextNode('')]) // insert linkNode by this
}
});
}; |
Beta Was this translation helpful? Give feedback.
-
|
@mariusplanable were you able to resolve your issue afterwards? If yes, please share your solution. |
Beta Was this translation helpful? Give feedback.



Hm, yea so to make this work you'd need to split the text node at the point of insertion and create three new nodes like:
TextNode (This is some)
LinkNode
- TextNode(MY LINK TEXT)
TextNode (random text)
There should be a util for this somewhere like splitText, but you'll probably need to get the split point from the Selection offset at the point where the LinkNode is inserted.