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:
@@ -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' }}>
|
||||||
|
|||||||
@@ -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' }}>
|
||||||
|
|||||||
Reference in New Issue
Block a user