2020-10-17 13:12:00 +02:00
|
|
|
<template>
|
2023-01-15 01:14:17 +01:00
|
|
|
<Transition
|
|
|
|
appear
|
2023-04-01 06:42:40 +02:00
|
|
|
:enter-active-class="defaultStore.state.animation ? $style.transition_fade_enterActive : ''"
|
|
|
|
:leave-active-class="defaultStore.state.animation ? $style.transition_fade_leaveActive : ''"
|
|
|
|
:enter-from-class="defaultStore.state.animation ? $style.transition_fade_enterFrom : ''"
|
|
|
|
:leave-to-class="defaultStore.state.animation ? $style.transition_fade_leaveTo : ''"
|
2023-01-15 01:14:17 +01:00
|
|
|
>
|
|
|
|
<div ref="rootEl" :class="$style.root" :style="{ zIndex }" @contextmenu.prevent.stop="() => {}">
|
2022-07-17 16:18:05 +02:00
|
|
|
<MkMenu :items="items" :align="'left'" @close="$emit('closed')"/>
|
2020-10-25 15:22:27 +01:00
|
|
|
</div>
|
2022-12-30 05:37:14 +01:00
|
|
|
</Transition>
|
2020-10-17 13:12:00 +02:00
|
|
|
</template>
|
|
|
|
|
2022-01-30 06:11:52 +01:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onMounted, onBeforeUnmount } from 'vue';
|
2022-09-06 11:21:49 +02:00
|
|
|
import MkMenu from './MkMenu.vue';
|
2022-01-30 06:11:52 +01:00
|
|
|
import { MenuItem } from './types/menu.vue';
|
2022-07-17 16:18:05 +02:00
|
|
|
import contains from '@/scripts/contains';
|
2021-12-18 04:12:47 +01:00
|
|
|
import * as os from '@/os';
|
2023-04-01 06:42:40 +02:00
|
|
|
import { defaultStore } from '@/store';
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2022-01-30 06:11:52 +01:00
|
|
|
const props = defineProps<{
|
|
|
|
items: MenuItem[];
|
|
|
|
ev: MouseEvent;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
2022-05-26 15:53:09 +02:00
|
|
|
(ev: 'closed'): void;
|
2022-01-30 06:11:52 +01:00
|
|
|
}>();
|
|
|
|
|
2023-01-03 02:12:37 +01:00
|
|
|
let rootEl = $shallowRef<HTMLDivElement>();
|
2022-01-30 06:11:52 +01:00
|
|
|
|
|
|
|
let zIndex = $ref<number>(os.claimZIndex('high'));
|
|
|
|
|
2023-02-25 05:44:37 +01:00
|
|
|
const SCROLLBAR_THICKNESS = 16;
|
|
|
|
|
2022-01-30 06:11:52 +01:00
|
|
|
onMounted(() => {
|
|
|
|
let left = props.ev.pageX + 1; // 間違って右ダブルクリックした場合に意図せずアイテムがクリックされるのを防ぐため + 1
|
|
|
|
let top = props.ev.pageY + 1; // 間違って右ダブルクリックした場合に意図せずアイテムがクリックされるのを防ぐため + 1
|
|
|
|
|
|
|
|
const width = rootEl.offsetWidth;
|
|
|
|
const height = rootEl.offsetHeight;
|
|
|
|
|
2023-02-25 05:44:37 +01:00
|
|
|
if (left + width - window.pageXOffset >= (window.innerWidth - SCROLLBAR_THICKNESS)) {
|
|
|
|
left = (window.innerWidth - SCROLLBAR_THICKNESS) - width + window.pageXOffset;
|
2022-01-30 06:11:52 +01:00
|
|
|
}
|
|
|
|
|
2023-02-25 05:44:37 +01:00
|
|
|
if (top + height - window.pageYOffset >= (window.innerHeight - SCROLLBAR_THICKNESS)) {
|
|
|
|
top = (window.innerHeight - SCROLLBAR_THICKNESS) - height + window.pageYOffset;
|
2022-01-30 06:11:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (top < 0) {
|
|
|
|
top = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left < 0) {
|
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rootEl.style.top = `${top}px`;
|
|
|
|
rootEl.style.left = `${left}px`;
|
|
|
|
|
|
|
|
for (const el of Array.from(document.querySelectorAll('body *'))) {
|
|
|
|
el.addEventListener('mousedown', onMousedown);
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
|
|
|
});
|
2022-01-30 06:11:52 +01:00
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
for (const el of Array.from(document.querySelectorAll('body *'))) {
|
|
|
|
el.removeEventListener('mousedown', onMousedown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-26 15:53:09 +02:00
|
|
|
function onMousedown(evt: Event) {
|
|
|
|
if (!contains(rootEl, evt.target) && (rootEl !== evt.target)) emit('closed');
|
2022-01-30 06:11:52 +01:00
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
</script>
|
|
|
|
|
2023-01-15 01:14:17 +01:00
|
|
|
<style lang="scss" module>
|
|
|
|
.transition_fade_enterActive,
|
|
|
|
.transition_fade_leaveActive {
|
2023-01-08 01:58:35 +01:00
|
|
|
transition: opacity 0.3s cubic-bezier(0.16, 1, 0.3, 1), transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
|
2020-10-25 15:22:27 +01:00
|
|
|
transform-origin: left top;
|
|
|
|
}
|
2023-01-15 01:14:17 +01:00
|
|
|
.transition_fade_enterFrom,
|
|
|
|
.transition_fade_leaveTo {
|
2020-10-25 15:22:27 +01:00
|
|
|
opacity: 0;
|
|
|
|
transform: scale(0.9);
|
|
|
|
}
|
2023-01-15 01:14:17 +01:00
|
|
|
|
|
|
|
.root {
|
|
|
|
position: absolute;
|
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
</style>
|