wujingjing
2024-08-29 19b778d2d04bed31ce2e1f167c6ff2fda9906421
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
<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;width:${width}px`"
            :key="Math.random()"
            v-show="state.isShow"
        >
            <ul class="el-dropdown-menu">
                <li
                    v-for="(v, k) in dropdownList"
                    v-show="v.show ?? true"
                    class="el-dropdown-menu__item"
                    aria-disabled="false"
                    tabindex="-1"
                    :key="k"
                    @click="v.click"
                >
                    <SvgIcon :name="v.icon" />
                    <span>{{ v.txt }}</span>
                </li>
            </ul>
            <div class="el-popper__arrow" :style="{ left: `${state.arrowLeft}px` }"></div>
        </div>
    </transition>
</template>
 
<script setup lang="ts">
import { computed, reactive, onMounted, onUnmounted, watch, PropType, toRefs } from 'vue';
import { DropdownListType } from './types';
 
// 定义父组件传过来的值
const props = defineProps({
    mousePosition: {
        type: Object,
        default: () => {
            return {
                x: 0,
                y: 0,
            };
        },
    },
    dropdownList: {
        type: Array<DropdownListType>,
        default: () => {
            return [];
        },
    },
    /** @description Dropdown 下拉菜单的宽度 */
    width: {
        type: String,
        default: () => {
            return '117';
        },
    },
});
 
const { mousePosition, dropdownList } = toRefs(props);
const numWidth = computed(() => {
    return parseInt(props.width);
});
 
// 定义变量内容
const state = reactive({
    isShow: false,
 
    arrowLeft: 10,
});
 
// 父级传过来的坐标 x,y 值
const dropdowns = computed(() => {
    if (mousePosition.value.x + numWidth.value > document.documentElement.clientWidth) {
        return {
            x: document.documentElement.clientWidth - numWidth.value - 5,
            y: mousePosition.value.y,
        };
    } else {
        return mousePosition.value;
    }
});
 
// 打开右键菜单
const openContextmenu = () => {
    state.isShow = true;
};
// 关闭右键菜单
const closeContextmenu = () => {
    state.isShow = false;
};
// 监听页面监听进行右键菜单的关闭
onMounted(() => {
    document.body.addEventListener('click', closeContextmenu);
});
// 页面卸载时,移除右键菜单监听事件
onUnmounted(() => {
    document.body.removeEventListener('click', closeContextmenu);
});
// 监听下拉菜单位置
watch(
    () => mousePosition.value,
    ({ x }) => {
        if (x + numWidth.value > document.documentElement.clientWidth) state.arrowLeft = numWidth.value - (document.documentElement.clientWidth - x);
        else state.arrowLeft = 10;
    },
    {
        deep: true,
    }
);
 
// 暴露变量
defineExpose({
    openContextmenu,
});
</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>