@mptw/skylens-ui 1.4.79 → 1.4.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/SLContextMenu.vue +109 -0
- package/package.json +1 -1
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
<template lang="pug">
|
|
2
|
+
.sl-context-menu-avatar(ref='el')
|
|
3
|
+
ClientOnly
|
|
4
|
+
Teleport(to='body')
|
|
5
|
+
.sl-context-menu-wrapper.hidden(ref='menuWrapperEl', @mousedown='onMouseDown')
|
|
6
|
+
.sl-context-menu(ref='menuEl')
|
|
7
|
+
a.sl-context-menu-item(
|
|
8
|
+
v-for='action in actions',
|
|
9
|
+
href='javascript:;',
|
|
10
|
+
:key='action.key',
|
|
11
|
+
@click='onClickAction(action.key)',
|
|
12
|
+
) {{ action.label }}
|
|
13
|
+
</template>
|
|
14
|
+
|
|
15
|
+
<script lang="ts">
|
|
16
|
+
import { fromEvent, Subscription } from 'rxjs';
|
|
17
|
+
|
|
18
|
+
export default defineComponent({
|
|
19
|
+
name: 'SLContextMenu',
|
|
20
|
+
props: {
|
|
21
|
+
actions: {
|
|
22
|
+
type: Array as PropType<{
|
|
23
|
+
key: string,
|
|
24
|
+
label: string,
|
|
25
|
+
}[]>,
|
|
26
|
+
default: () => [{
|
|
27
|
+
key: 'saveAs',
|
|
28
|
+
label: '另存圖檔 (.png)',
|
|
29
|
+
}, {
|
|
30
|
+
key: 'copy',
|
|
31
|
+
label: '複製圖檔',
|
|
32
|
+
}],
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
emits: ['action'],
|
|
36
|
+
setup(props, { emit }) {
|
|
37
|
+
const { actions } = toRefs(props);
|
|
38
|
+
|
|
39
|
+
const el = shallowRef<HTMLDivElement | null>(null);
|
|
40
|
+
const menuWrapperEl = shallowRef<HTMLDivElement | null>(null);
|
|
41
|
+
const menuEl = shallowRef<HTMLDivElement | null>(null);
|
|
42
|
+
|
|
43
|
+
let contextMenuSubscriber: Subscription | null = null;
|
|
44
|
+
|
|
45
|
+
function onClickAction(key: string) {
|
|
46
|
+
emit('action', key);
|
|
47
|
+
hide();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function show() {
|
|
51
|
+
menuWrapperEl.value?.classList.remove('hidden');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function hide() {
|
|
55
|
+
menuWrapperEl.value?.classList.add('hidden');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function onRightClick(e: PointerEvent) {
|
|
59
|
+
console.log(menuEl.value, actions.value);
|
|
60
|
+
if (!menuEl.value || !actions.value.length) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
e.preventDefault();
|
|
65
|
+
menuEl.value.setAttribute('style', `top: ${e.clientY}px; left: ${e.clientX}px;`);
|
|
66
|
+
show();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function onMouseDown() {
|
|
70
|
+
hide();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
onMounted(() => {
|
|
74
|
+
if (el.value && el.value.parentElement) {
|
|
75
|
+
contextMenuSubscriber = fromEvent(el.value.parentElement, 'contextmenu').subscribe((e) => {
|
|
76
|
+
onRightClick(e as PointerEvent);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
onBeforeUnmount(() => {
|
|
82
|
+
if (contextMenuSubscriber) {
|
|
83
|
+
contextMenuSubscriber.unsubscribe();
|
|
84
|
+
}
|
|
85
|
+
contextMenuSubscriber = null;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
el,
|
|
90
|
+
menuWrapperEl,
|
|
91
|
+
menuEl,
|
|
92
|
+
onClickAction,
|
|
93
|
+
onMouseDown,
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
</script>
|
|
98
|
+
|
|
99
|
+
<style lang="stylus">
|
|
100
|
+
.sl-context-menu-wrapper
|
|
101
|
+
@apply s('z-[9999]') fixed top-0 left-0 w-full h-full
|
|
102
|
+
|
|
103
|
+
.sl-context-menu
|
|
104
|
+
@apply absolute border border-primary-600 rounded overflow-hidden shadow-popup-sm
|
|
105
|
+
|
|
106
|
+
&-item
|
|
107
|
+
@apply block w-auto s('min-w-[160px]') p-3 bg-white text-base text-gray-2
|
|
108
|
+
@apply s('hover:bg-primary-100') s('hover:text-primary')
|
|
109
|
+
</style>
|