tiptap/packages/vue-2/src/BubbleMenu.ts

65 lines
1.7 KiB
TypeScript
Raw Normal View History

import { BubbleMenuPlugin, BubbleMenuPluginProps } from '@tiptap/extension-bubble-menu'
import Vue, { Component, PropType } from 'vue'
2021-03-30 20:07:18 +08:00
2021-05-04 17:03:11 +08:00
export interface BubbleMenuInterface extends Vue {
2021-08-13 18:33:06 +08:00
pluginKey: BubbleMenuPluginProps['pluginKey'],
2021-05-04 17:03:11 +08:00
editor: BubbleMenuPluginProps['editor'],
tippyOptions: BubbleMenuPluginProps['tippyOptions'],
shouldShow: BubbleMenuPluginProps['shouldShow'],
2021-05-03 21:11:26 +08:00
}
export const BubbleMenu: Component = {
2021-03-30 20:07:18 +08:00
name: 'BubbleMenu',
props: {
pluginKey: {
2021-08-13 18:33:06 +08:00
type: [String, Object as PropType<Exclude<BubbleMenuPluginProps['pluginKey'], string>>],
default: 'bubbleMenu',
},
2021-03-30 20:07:18 +08:00
editor: {
type: Object as PropType<BubbleMenuPluginProps['editor']>,
required: true,
},
2021-04-16 18:42:56 +08:00
tippyOptions: {
type: Object as PropType<BubbleMenuPluginProps['tippyOptions']>,
default: () => ({}),
},
shouldShow: {
type: Function as PropType<Exclude<BubbleMenuPluginProps['shouldShow'], null>>,
default: null,
},
2021-03-30 20:07:18 +08:00
},
watch: {
editor: {
immediate: true,
2021-05-04 17:03:11 +08:00
handler(this: BubbleMenuInterface, editor: BubbleMenuPluginProps['editor']) {
2021-03-30 20:07:18 +08:00
if (!editor) {
return
}
this.$nextTick(() => {
editor.registerPlugin(BubbleMenuPlugin({
2021-08-13 18:33:06 +08:00
pluginKey: this.pluginKey,
2021-03-30 20:07:18 +08:00
editor,
element: this.$el as HTMLElement,
2021-04-16 18:42:56 +08:00
tippyOptions: this.tippyOptions,
shouldShow: this.shouldShow,
2021-03-30 20:07:18 +08:00
}))
})
},
},
},
2021-05-04 17:03:11 +08:00
render(this: BubbleMenuInterface, createElement) {
2021-04-01 23:55:32 +08:00
return createElement('div', { style: { visibility: 'hidden' } }, this.$slots.default)
2021-03-30 20:07:18 +08:00
},
2021-05-04 17:03:11 +08:00
beforeDestroy(this: BubbleMenuInterface) {
this.editor.unregisterPlugin(this.pluginKey)
2021-03-30 20:07:18 +08:00
},
2021-05-03 21:11:26 +08:00
}