|
| 1 | +<template> |
| 2 | + <Teleport to="body"> |
| 3 | + <div |
| 4 | + class="fixed inset-0 z-[10001]" |
| 5 | + @click="$emit('close')" |
| 6 | + @contextmenu.prevent="$emit('close')" |
| 7 | + > |
| 8 | + <div |
| 9 | + ref="menuRef" |
| 10 | + class="absolute min-w-[180px] overflow-hidden rounded-card border border-line bg-surface py-1 shadow-tmgr-lg" |
| 11 | + :style="positionStyle" |
| 12 | + @click.stop |
| 13 | + @contextmenu.prevent |
| 14 | + > |
| 15 | + <button |
| 16 | + type="button" |
| 17 | + class="flex w-full items-center gap-2.5 px-3 py-2 text-left text-sm text-ink transition-colors hover:bg-surface-hover" |
| 18 | + @click="onEdit" |
| 19 | + > |
| 20 | + <DRIcon name="pencil" :size="13" stroke="currentColor" /> |
| 21 | + Edit |
| 22 | + </button> |
| 23 | + <button |
| 24 | + type="button" |
| 25 | + class="flex w-full items-center gap-2.5 px-3 py-2 text-left text-sm text-ink transition-colors hover:bg-surface-hover" |
| 26 | + @click="onArchive" |
| 27 | + > |
| 28 | + <DRIcon name="archive" :size="13" stroke="currentColor" /> |
| 29 | + Archive |
| 30 | + </button> |
| 31 | + <div class="my-1 h-px bg-line" /> |
| 32 | + <button |
| 33 | + type="button" |
| 34 | + class="flex w-full items-center gap-2.5 px-3 py-2 text-left text-sm text-status-fix-fg transition-colors hover:bg-status-fix-bg/40" |
| 35 | + @click="onDelete" |
| 36 | + > |
| 37 | + <DRIcon name="trash" :size="13" stroke="currentColor" /> |
| 38 | + Delete |
| 39 | + </button> |
| 40 | + </div> |
| 41 | + </div> |
| 42 | + </Teleport> |
| 43 | +</template> |
| 44 | + |
| 45 | +<script setup lang="ts"> |
| 46 | + import { computed, onMounted, onBeforeUnmount, ref, nextTick } from 'vue'; |
| 47 | + import DRIcon from './DRIcon.vue'; |
| 48 | + import type { RoutineEntry } from '@/types/dailyRoutine'; |
| 49 | +
|
| 50 | + const props = defineProps<{ |
| 51 | + entry: RoutineEntry; |
| 52 | + x: number; |
| 53 | + y: number; |
| 54 | + }>(); |
| 55 | +
|
| 56 | + const emit = defineEmits<{ |
| 57 | + (e: 'close'): void; |
| 58 | + (e: 'edit', entry: RoutineEntry): void; |
| 59 | + (e: 'archive', entry: RoutineEntry): void; |
| 60 | + (e: 'delete', entry: RoutineEntry): void; |
| 61 | + }>(); |
| 62 | +
|
| 63 | + const menuRef = ref<HTMLElement | null>(null); |
| 64 | + const adjustedX = ref(props.x); |
| 65 | + const adjustedY = ref(props.y); |
| 66 | +
|
| 67 | + const positionStyle = computed(() => ({ |
| 68 | + left: `${adjustedX.value}px`, |
| 69 | + top: `${adjustedY.value}px`, |
| 70 | + })); |
| 71 | +
|
| 72 | + function clampPosition() { |
| 73 | + const el = menuRef.value; |
| 74 | + if (!el) return; |
| 75 | + const rect = el.getBoundingClientRect(); |
| 76 | + const margin = 8; |
| 77 | + let x = props.x; |
| 78 | + let y = props.y; |
| 79 | + if (x + rect.width + margin > window.innerWidth) { |
| 80 | + x = window.innerWidth - rect.width - margin; |
| 81 | + } |
| 82 | + if (y + rect.height + margin > window.innerHeight) { |
| 83 | + y = window.innerHeight - rect.height - margin; |
| 84 | + } |
| 85 | + adjustedX.value = Math.max(margin, x); |
| 86 | + adjustedY.value = Math.max(margin, y); |
| 87 | + } |
| 88 | +
|
| 89 | + function onKey(e: KeyboardEvent) { |
| 90 | + if (e.key === 'Escape') emit('close'); |
| 91 | + } |
| 92 | +
|
| 93 | + function onEdit() { |
| 94 | + emit('edit', props.entry); |
| 95 | + emit('close'); |
| 96 | + } |
| 97 | +
|
| 98 | + function onArchive() { |
| 99 | + emit('archive', props.entry); |
| 100 | + emit('close'); |
| 101 | + } |
| 102 | +
|
| 103 | + function onDelete() { |
| 104 | + // eslint-disable-next-line no-alert |
| 105 | + if (!confirm(`Delete "${props.entry.title}"?`)) return; |
| 106 | + emit('delete', props.entry); |
| 107 | + emit('close'); |
| 108 | + } |
| 109 | +
|
| 110 | + onMounted(async () => { |
| 111 | + document.addEventListener('keydown', onKey); |
| 112 | + await nextTick(); |
| 113 | + clampPosition(); |
| 114 | + }); |
| 115 | + onBeforeUnmount(() => { |
| 116 | + document.removeEventListener('keydown', onKey); |
| 117 | + }); |
| 118 | +</script> |
0 commit comments