koel/resources/assets/js/composables/useContextMenu.ts

32 lines
675 B
TypeScript
Raw Normal View History

import { reactive, ref } from 'vue'
import ContextMenuBase from '@/components/ui/ContextMenuBase.vue'
2022-04-20 10:20:09 +00:00
export type ContextMenuContext = Record<string, any>
2022-04-15 17:00:08 +00:00
export const useContextMenu = () => {
2022-04-24 08:29:14 +00:00
const base = ref<InstanceType<typeof ContextMenuBase>>()
2022-04-15 17:00:08 +00:00
2022-04-20 10:20:09 +00:00
const context = reactive<ContextMenuContext>({})
2022-07-19 08:19:57 +00:00
const open = async (top: number, left: number, ctx: ContextMenuContext = {}) => {
2022-04-20 10:20:09 +00:00
Object.assign(context, ctx)
2022-07-19 08:19:57 +00:00
await base.value?.open(top, left, ctx)
2022-04-20 10:20:09 +00:00
}
2022-04-15 17:00:08 +00:00
const close = () => base.value?.close()
2022-06-10 10:47:46 +00:00
const trigger = (func: Closure) => {
close()
func()
}
2022-04-15 17:00:08 +00:00
return {
2022-04-24 08:29:14 +00:00
ContextMenuBase,
base,
context,
2022-04-15 17:00:08 +00:00
open,
2022-04-20 10:20:09 +00:00
close,
trigger
2022-04-15 17:00:08 +00:00
}
}