2021-03-31 16:21:34 +08:00
|
|
|
import React, { useEffect, useRef } from 'react'
|
|
|
|
import { BubbleMenuPlugin, BubbleMenuPluginKey, BubbleMenuPluginProps } from '@tiptap/extension-bubble-menu'
|
|
|
|
|
2021-03-31 17:13:09 +08:00
|
|
|
export type BubbleMenuProps = Omit<BubbleMenuPluginProps, 'element'> & {
|
|
|
|
className?: string,
|
|
|
|
}
|
2021-03-31 16:21:34 +08:00
|
|
|
|
2021-03-31 17:13:09 +08:00
|
|
|
export const BubbleMenu: React.FC<BubbleMenuProps> = props => {
|
2021-03-31 16:21:34 +08:00
|
|
|
const element = useRef<HTMLDivElement>(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-03-31 17:13:09 +08:00
|
|
|
const { editor, keepInBounds = true } = props
|
2021-03-31 16:21:34 +08:00
|
|
|
|
|
|
|
editor.registerPlugin(BubbleMenuPlugin({
|
|
|
|
editor,
|
|
|
|
element: element.current as HTMLElement,
|
|
|
|
keepInBounds,
|
|
|
|
}))
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
editor.unregisterPlugin(BubbleMenuPluginKey)
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return (
|
2021-03-31 17:13:09 +08:00
|
|
|
<div ref={element} className={props.className}>
|
2021-03-31 16:21:34 +08:00
|
|
|
{props.children}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|