Your Name
2022-06-27 0a3027eccd3b0bb6542e5fc831f7e02740dcfc95
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
<template>
    <transition name="el-zoom-in-center">
        <div
            aria-hidden="true"
            class="el-dropdown__popper el-popper is-light is-pure custom-contextmenu"
            role="tooltip"
            data-popper-placement="bottom"
            :style="`top: ${dropdowns.y + 5}px;left: ${dropdowns.x}px;`"
            :key="Math.random()"
            v-show="isShow"
        >
            <ul class="el-dropdown-menu">
                <li
                    v-for="(v, k) in dropdownList"
                    class="el-dropdown-menu__item"
                    aria-disabled="false"
                    tabindex="-1"
                    :key="k"
                    @click="onCurrentClick(v.contextMenuClickId)"
                >
                    <SvgIcon :name="v.icon" />
                    <span>{{ v.txt }}{{ item.type === 'line' ? '线' : '节点' }}</span>
                </li>
            </ul>
            <div class="el-popper__arrow" style="left: 10px"></div>
        </div>
    </transition>
</template>
 
<script lang="ts">
import { computed, defineComponent, reactive, toRefs, onMounted, onUnmounted } from 'vue';
 
export default defineComponent({
    name: 'pagesWorkflowContextmenu',
    props: {
        dropdown: {
            type: Object,
        },
    },
    setup(props, { emit }) {
        const state = reactive({
            isShow: false,
            dropdownList: [
                { contextMenuClickId: 0, txt: '删除', icon: 'ele-Delete' },
                { contextMenuClickId: 1, txt: '编辑', icon: 'ele-Edit' },
            ],
            item: {
                type: 'node',
            },
            conn: {},
        });
        // 父级传过来的坐标 x,y 值
        const dropdowns = computed(() => {
            return <any>props.dropdown;
        });
        // 当前项菜单点击
        const onCurrentClick = (contextMenuClickId: number) => {
            emit('current', Object.assign({}, { contextMenuClickId }, state.item), state.conn);
        };
        // 打开右键菜单:判断是否固定,固定则不显示关闭按钮
        const openContextmenu = (item: any, conn = {}) => {
            state.item = item;
            state.conn = conn;
            closeContextmenu();
            setTimeout(() => {
                state.isShow = true;
            }, 10);
        };
        // 关闭右键菜单
        const closeContextmenu = () => {
            state.isShow = false;
        };
        // 监听页面监听进行右键菜单的关闭
        onMounted(() => {
            document.body.addEventListener('click', closeContextmenu);
            document.body.addEventListener('contextmenu', closeContextmenu);
        });
        // 页面卸载时,移除右键菜单监听事件
        onUnmounted(() => {
            document.body.removeEventListener('click', closeContextmenu);
            document.body.removeEventListener('contextmenu', closeContextmenu);
        });
        return {
            dropdowns,
            openContextmenu,
            closeContextmenu,
            onCurrentClick,
            ...toRefs(state),
        };
    },
});
</script>
 
<style scoped lang="scss">
.custom-contextmenu {
    transform-origin: center top;
    z-index: 2190;
    position: fixed;
    .el-dropdown-menu__item {
        font-size: 12px !important;
        white-space: nowrap;
        i {
            font-size: 12px !important;
        }
    }
}
</style>