createContextMenu.ts
2.11 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
import { NodeMouseEvent } from '@vue-flow/core';
import ContextMenuVue from './index.vue';
import { isClient } from '/@/utils/is';
import { createVNode, render, getCurrentInstance, onUnmounted, toRaw, unref } from 'vue';
const menuManager: {
domList: Element[];
resolve: Fn;
} = {
domList: [],
resolve: () => {},
};
const createContextMenu = function (
options: NodeMouseEvent,
params?: InstanceType<typeof ContextMenuVue>['$props']
) {
const { event } = options || {};
if (!(params?.items && params?.items.length)) return;
event && event?.preventDefault();
if (!isClient) {
return;
}
return new Promise((resolve) => {
const body = document.body;
const container = document.createElement('div');
const propsData: Partial<InstanceType<typeof ContextMenuVue>['$props']> = {
...params,
};
if (options.event) {
const { clientX, clientY } = options.event as MouseEvent;
propsData.axis = { x: clientX, y: clientY };
}
if (options.node) {
propsData.nodeData = toRaw(unref(options.node.data));
}
const vm = createVNode(ContextMenuVue, propsData);
render(vm, container);
const handleClick = function () {
menuManager.resolve('');
};
menuManager.domList.push(container);
const remove = function () {
menuManager.domList.forEach((dom: Element) => {
try {
dom && body.removeChild(dom);
} catch (error) {}
});
body.removeEventListener('click', handleClick);
body.removeEventListener('scroll', handleClick);
};
menuManager.resolve = function (arg) {
remove();
resolve(arg);
};
remove();
body.appendChild(container);
body.addEventListener('click', handleClick);
body.addEventListener('scroll', handleClick);
});
};
const destroyContextMenu = function () {
if (menuManager) {
menuManager.resolve('');
menuManager.domList = [];
}
};
export function useContextMenu(authRemove = true) {
if (getCurrentInstance() && authRemove) {
onUnmounted(() => {
destroyContextMenu();
});
}
return [createContextMenu, destroyContextMenu];
}