forked from ueberdosis/tiptap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloatingMenu.ts
More file actions
132 lines (108 loc) · 3.38 KB
/
Copy pathFloatingMenu.ts
File metadata and controls
132 lines (108 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import type { FloatingMenuPluginProps } from '@tiptap/extension-floating-menu'
import { FloatingMenuPlugin } from '@tiptap/extension-floating-menu'
import { PluginKey } from '@tiptap/pm/state'
import type { Component, CreateElement, PropType } from 'vue'
import type Vue from 'vue'
export interface FloatingMenuInterface extends Vue {
$attrs: Record<string, any>
$listeners: Record<string, (...args: any[]) => unknown>
pluginKey: FloatingMenuPluginProps['pluginKey']
generatedPluginKey?: FloatingMenuPluginProps['pluginKey']
editor: FloatingMenuPluginProps['editor']
updateDelay: FloatingMenuPluginProps['updateDelay']
resizeDelay: FloatingMenuPluginProps['resizeDelay']
options: FloatingMenuPluginProps['options']
appendTo: FloatingMenuPluginProps['appendTo']
shouldShow: FloatingMenuPluginProps['shouldShow']
getPluginKey: () => FloatingMenuPluginProps['pluginKey']
isDestroyed: boolean
}
export const FloatingMenu: Component = {
name: 'FloatingMenu',
inheritAttrs: false,
props: {
pluginKey: {
type: [String, Object as PropType<Exclude<FloatingMenuPluginProps['pluginKey'], string>>],
default: undefined,
},
editor: {
type: Object as PropType<FloatingMenuPluginProps['editor']>,
required: true,
},
updateDelay: {
type: Number as PropType<FloatingMenuPluginProps['updateDelay']>,
},
resizeDelay: {
type: Number as PropType<FloatingMenuPluginProps['resizeDelay']>,
},
options: {
type: Object as PropType<FloatingMenuPluginProps['options']>,
default: () => ({}),
},
appendTo: {
type: [Object, Function] as PropType<FloatingMenuPluginProps['appendTo']>,
default: undefined,
},
shouldShow: {
type: Function as PropType<Exclude<FloatingMenuPluginProps['shouldShow'], null>>,
default: null,
},
},
mounted(this: FloatingMenuInterface) {
const editor = this.editor
const el = this.$el as HTMLElement
if (!editor || !el) {
return
}
el.style.visibility = 'hidden'
el.style.position = 'absolute'
// Remove element from DOM; plugin will re-parent it when shown
el.remove()
this.$nextTick(() => {
if (this.isDestroyed) {
return
}
editor.registerPlugin(
FloatingMenuPlugin({
pluginKey: this.getPluginKey(),
editor,
element: el,
updateDelay: this.updateDelay,
resizeDelay: this.resizeDelay,
options: this.options,
appendTo: this.appendTo,
shouldShow: this.shouldShow,
}),
)
})
},
render(this: FloatingMenuInterface, createElement: CreateElement) {
const vnodeData = (this.$vnode?.data ?? {}) as any
return createElement(
'div',
{
attrs: this.$attrs,
on: this.$listeners,
class: [vnodeData.staticClass, vnodeData.class],
style: [vnodeData.staticStyle, vnodeData.style],
},
this.$slots.default,
)
},
beforeDestroy(this: FloatingMenuInterface) {
this.isDestroyed = true
const editor = this.editor
if (!editor) {
return
}
editor.unregisterPlugin(this.getPluginKey())
},
methods: {
getPluginKey(this: FloatingMenuInterface) {
if (!this.generatedPluginKey) {
this.generatedPluginKey = this.pluginKey ?? new PluginKey('floatingMenu')
}
return this.generatedPluginKey
},
},
}