2021-03-31 16:21:34 +08:00
|
|
|
import React, { useEffect, useRef } from 'react'
|
2021-08-11 20:37:58 +08:00
|
|
|
import { BubbleMenuPlugin, BubbleMenuPluginProps } from '@tiptap/extension-bubble-menu'
|
2021-03-31 16:21:34 +08:00
|
|
|
|
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-08-11 20:37:58 +08:00
|
|
|
const {
|
|
|
|
key,
|
|
|
|
editor,
|
|
|
|
tippyOptions,
|
|
|
|
shouldShow,
|
|
|
|
} = props
|
2021-03-31 16:21:34 +08:00
|
|
|
|
|
|
|
editor.registerPlugin(BubbleMenuPlugin({
|
2021-08-11 20:37:58 +08:00
|
|
|
key,
|
2021-03-31 16:21:34 +08:00
|
|
|
editor,
|
|
|
|
element: element.current as HTMLElement,
|
2021-04-16 18:42:56 +08:00
|
|
|
tippyOptions,
|
2021-08-11 20:37:58 +08:00
|
|
|
shouldShow,
|
2021-03-31 16:21:34 +08:00
|
|
|
}))
|
|
|
|
|
|
|
|
return () => {
|
2021-08-11 20:37:58 +08:00
|
|
|
editor.unregisterPlugin(key)
|
2021-03-31 16:21:34 +08:00
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return (
|
2021-04-01 23:55:32 +08:00
|
|
|
<div ref={element} className={props.className} style={{ visibility: 'hidden' }}>
|
2021-03-31 16:21:34 +08:00
|
|
|
{props.children}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|