| 1 | import type { ContextMenuItem } from '@slidev/types' |
| 2 | import type { ComputedRef } from 'vue' |
| 3 | import { shallowRef } from 'vue' |
| 4 | import { useNav } from '../composables/useNav' |
| 5 | import { configs, mode } from '../env' |
| 6 | import setupContextMenu from '../setup/context-menu' |
| 7 | |
| 8 | export const currentContextMenu = shallowRef<null | { |
| 9 | x: number |
| 10 | y: number |
| 11 | items: ComputedRef<ContextMenuItem[]> |
| 12 | }>(null) |
| 13 | |
| 14 | export function openContextMenu(x: number, y: number) { |
| 15 | currentContextMenu.value = { |
| 16 | x, |
| 17 | y, |
| 18 | items: setupContextMenu(), |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | export function closeContextMenu() { |
| 23 | currentContextMenu.value = null |
| 24 | } |
| 25 | |
| 26 | export function onContextMenu(ev: MouseEvent) { |
| 27 | if (configs.contextMenu !== true && configs.contextMenu != null && configs.contextMenu !== mode) |
| 28 | return |
| 29 | if (ev.shiftKey || ev.defaultPrevented) |
| 30 | return |
| 31 | |
| 32 | const { isEmbedded } = useNav() |
| 33 | if (isEmbedded.value) |
| 34 | return |
| 35 | |
| 36 | openContextMenu(ev.pageX, ev.pageY) |
| 37 | ev.preventDefault() |
| 38 | ev.stopPropagation() |
| 39 | } |
| 40 |