fix: Add editor dependency when registering BubbleMenuPlugin and FloatingMenuPlugin (#2018)

* Add `editor` dependency when registering `BubbleMenuPlugin`

When we are initializing editor via the `useEditor` hook with dependencies the `BubbleMenu` component is only registered the first time the editor is initialized.

Adding editor to the dependency array registering/unregistering the `BubbleMenuPlugin` fixes this. (I tested exactly this code in our project.)

I also added a check that ensures that the menu element referenced via the `useRef` is defined when registering the plugin - otherwise, there is no point in registering the plugin.

* Add `editor` dependency when registering `FloatingMenuPlugin`
This commit is contained in:
Tomas Valenta
2021-10-12 11:09:00 +02:00
committed by GitHub
parent 0e0b0b6a8c
commit e9465ec0f6
2 changed files with 12 additions and 2 deletions

View File

@@ -11,6 +11,8 @@ export const BubbleMenu: React.FC<BubbleMenuProps> = props => {
const element = useRef<HTMLDivElement>(null) const element = useRef<HTMLDivElement>(null)
useEffect(() => { useEffect(() => {
if (!element.current) return
const { const {
pluginKey = 'bubbleMenu', pluginKey = 'bubbleMenu',
editor, editor,
@@ -29,7 +31,10 @@ export const BubbleMenu: React.FC<BubbleMenuProps> = props => {
return () => { return () => {
editor.unregisterPlugin(pluginKey) editor.unregisterPlugin(pluginKey)
} }
}, []) }, [
props.editor,
element.current,
])
return ( return (
<div ref={element} className={props.className} style={{ visibility: 'hidden' }}> <div ref={element} className={props.className} style={{ visibility: 'hidden' }}>

View File

@@ -11,6 +11,8 @@ export const FloatingMenu: React.FC<FloatingMenuProps> = props => {
const element = useRef<HTMLDivElement>(null) const element = useRef<HTMLDivElement>(null)
useEffect(() => { useEffect(() => {
if (!element.current) return
const { const {
pluginKey = 'floatingMenu', pluginKey = 'floatingMenu',
editor, editor,
@@ -29,7 +31,10 @@ export const FloatingMenu: React.FC<FloatingMenuProps> = props => {
return () => { return () => {
editor.unregisterPlugin(pluginKey) editor.unregisterPlugin(pluginKey)
} }
}, []) }, [
props.editor,
element.current,
])
return ( return (
<div ref={element} className={props.className} style={{ visibility: 'hidden' }}> <div ref={element} className={props.className} style={{ visibility: 'hidden' }}>