2022-06-08 20:10:25 +08:00
|
|
|
import { BubbleMenuPlugin, BubbleMenuPluginProps } from '@tiptap/extension-bubble-menu'
|
2022-11-04 23:39:41 +08:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2021-03-31 16:21:34 +08:00
|
|
|
|
2022-11-04 23:39:41 +08:00
|
|
|
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
2021-08-24 00:44:40 +08:00
|
|
|
|
|
|
|
export type BubbleMenuProps = Omit<Optional<BubbleMenuPluginProps, 'pluginKey'>, 'element'> & {
|
2022-11-04 23:39:41 +08:00
|
|
|
className?: string;
|
|
|
|
children: React.ReactNode;
|
2022-11-05 05:04:47 +08:00
|
|
|
updateDelay?: number;
|
2022-11-04 23:39:41 +08:00
|
|
|
};
|
2021-03-31 16:21:34 +08:00
|
|
|
|
2022-04-11 16:45:50 +08:00
|
|
|
export const BubbleMenu = (props: BubbleMenuProps) => {
|
2021-12-22 19:13:36 +08:00
|
|
|
const [element, setElement] = useState<HTMLDivElement | null>(null)
|
2021-03-31 16:21:34 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-12-22 19:13:36 +08:00
|
|
|
if (!element) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (props.editor.isDestroyed) {
|
2021-10-12 17:38:01 +08:00
|
|
|
return
|
|
|
|
}
|
2021-10-12 17:09:00 +08:00
|
|
|
|
2021-08-11 20:37:58 +08:00
|
|
|
const {
|
2022-11-05 05:04:47 +08:00
|
|
|
pluginKey = 'bubbleMenu', editor, tippyOptions = {}, updateDelay, shouldShow = null,
|
2021-08-11 20:37:58 +08:00
|
|
|
} = props
|
2021-03-31 16:21:34 +08:00
|
|
|
|
2021-12-22 19:13:36 +08:00
|
|
|
const plugin = BubbleMenuPlugin({
|
2022-11-05 05:04:47 +08:00
|
|
|
updateDelay,
|
2021-03-31 16:21:34 +08:00
|
|
|
editor,
|
2021-12-22 19:13:36 +08:00
|
|
|
element,
|
2022-11-04 23:39:41 +08:00
|
|
|
pluginKey,
|
2021-08-11 20:37:58 +08:00
|
|
|
shouldShow,
|
2022-11-04 23:39:41 +08:00
|
|
|
tippyOptions,
|
2021-12-22 19:13:36 +08:00
|
|
|
})
|
2021-03-31 16:21:34 +08:00
|
|
|
|
2021-12-22 19:13:36 +08:00
|
|
|
editor.registerPlugin(plugin)
|
|
|
|
return () => editor.unregisterPlugin(pluginKey)
|
2022-11-04 23:39:41 +08:00
|
|
|
}, [props.editor, element])
|
2021-03-31 16:21:34 +08:00
|
|
|
|
|
|
|
return (
|
2021-12-22 19:13:36 +08:00
|
|
|
<div ref={setElement} className={props.className} style={{ visibility: 'hidden' }}>
|
2021-03-31 16:21:34 +08:00
|
|
|
{props.children}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|