-
I'm writing a custom link plugin, but I'm running into an issue with the
My plugin looks like this: import { useEffect } from 'react'
import { $getSelection, $isRangeSelection, createCommand, COMMAND_PRIORITY_LOW } from 'lexical'
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import { $toggleLink, LinkNode } from '@lexical/link'
export const INSERT_LINK_URL_COMMAND = createCommand('INSERT_LINK_URL_COMMAND')
const LinkPlugin = () => {
const [editor] = useLexicalComposerContext()
useEffect(() => {
if (!editor.hasNodes([LinkNode])) {
throw new Error('LinkPlugin: LinkNode not registered on editor')
}
return editor.registerCommand(
INSERT_LINK_URL_COMMAND,
(payload) => {
const { linkTitle, linkUrl, linkType } = payload
if (linkUrl && linkUrl !== '') {
editor.update(() => {
const selection = $getSelection()
if ($isRangeSelection(selection)) {
const { anchor, focus } = selection
//add text for the link
selection.insertText(linkTitle)
//select inserted text
anchor.offset -= linkTitle.length
focus.offset = anchor.offset + linkTitle.length
//transform selection into a link
$toggleLink(linkUrl)
}
})
}
//clean up
return true
},
COMMAND_PRIORITY_LOW
)
}, [editor])
return null
}
export default LinkPlugin And I've created a test button component to use in my toolbar: import { INSERT_LINK_URL_COMMAND } from './LinkPlugin.jsx'
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
const LinkButton = () => {
const [editor] = useLexicalComposerContext()
const testLink = () => {
const link = { linkTitle: 'reddit', linkUrl: 'https://www.reddit.com', linkType: 'normal' }
editor.dispatchCommand(INSERT_LINK_URL_COMMAND, link)
}
return <button type='button' onClick={testLink} />
} If I comment out Initially I referenced this discussion to build my functionality with. Which is basically a copy and paste. And, was successful for the folks in that discussion. But, I'm clearly using Any ideas? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Answering my own question here. The issue ended up being a version mismatch between Upgrading everything to |
Beta Was this translation helpful? Give feedback.
Answering my own question here. The issue ended up being a version mismatch between
lexical
and@lexical/link
.lexical
was at 0.16.0, and@lexical/link
was at 0.16.1. Which caused@lexical/link
to create it's ownnode_modules/lexical
folder, and a separate instance ofLexicalEditor
. Which was the root issue here.Upgrading everything to
0.16.1
fixed my issues.