File size: 3,552 Bytes
81d8726 | 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 133 134 135 136 137 138 139 140 141 | import { ref } from 'vue'
import ContextMenu from '@/components/ContextMenu.vue'
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
export function useContextMenu() {
const menuRef = ref(null)
const isVisible = ref(false)
let contextMenuApp = null
let contextMenuInstance = null
let isClosing = false
const showMenu = async (event, items) => {
event.preventDefault()
// 如果正在关闭中,等待关闭完成
if (isClosing) {
await new Promise(resolve => {
const checkClosed = () => {
if (!isClosing) {
resolve()
} else {
setTimeout(checkClosed, 10)
}
}
checkClosed()
})
}
// 如果已有菜单实例,先同步关闭
if (contextMenuInstance) {
await closeMenuSync()
}
contextMenuApp = createApp(ContextMenu)
contextMenuApp.use(ElementPlus)
const container = document.createElement('div')
// 检测全屏状态,决定挂载位置
const fullscreenElement = document.fullscreenElement
if (fullscreenElement) {
// 在全屏模式下,挂载到全屏元素中
fullscreenElement.appendChild(container)
} else {
// 非全屏模式,挂载到body
document.body.appendChild(container)
}
contextMenuInstance = contextMenuApp.mount(container)
menuRef.value = contextMenuInstance
contextMenuInstance.showMenu(event, items || [
{
label: 'A menu item',
onClick: () => {
alert('You click a menu item')
}
},
{
label: 'A submenu',
children: [
{
label: 'Sub item 1',
onClick: () => {
console.log('Sub item 1 clicked')
}
},
]
},
])
contextMenuInstance._container = container
isVisible.value = true
}
const closeMenu = () => {
if (contextMenuInstance && !isClosing) {
isClosing = true
isVisible.value = false
contextMenuInstance.closeMenu()
setTimeout(() => {
if (contextMenuInstance && contextMenuInstance._container) {
try {
contextMenuApp?.unmount()
// 检查容器的父元素,从正确的父元素中移除
const container = contextMenuInstance._container
if (container && container.parentNode) {
container.parentNode.removeChild(container)
}
} catch (error) {
console.warn('清理菜单时出错:', error)
}
contextMenuInstance = null
contextMenuApp = null
menuRef.value = null
}
isClosing = false
}, 100)
}
}
const closeMenuSync = () => {
return new Promise((resolve) => {
if (!contextMenuInstance) {
resolve()
return
}
isClosing = true
isVisible.value = false
contextMenuInstance.closeMenu()
try {
contextMenuApp?.unmount()
// 检查容器的父元素,从正确的父元素中移除
const container = contextMenuInstance._container
if (container && container.parentNode) {
container.parentNode.removeChild(container)
}
} catch (error) {
console.warn('同步清理菜单时出错:', error)
}
contextMenuInstance = null
contextMenuApp = null
menuRef.value = null
isClosing = false
resolve()
})
}
return {
menuRef,
isVisible,
showMenu,
closeMenu
}
}
|