-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext-menu-provider.tsx
More file actions
151 lines (134 loc) · 4.27 KB
/
Copy pathcontext-menu-provider.tsx
File metadata and controls
151 lines (134 loc) · 4.27 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { useCallback, useEffect, useState } from "react";
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuSeparator,
ContextMenuTrigger,
} from "@/components/common/context-menu";
interface ContextMenuProviderProps {
children: React.ReactNode;
isTriggerDisabled: boolean;
}
const isMac = typeof window !== "undefined" && /Mac|iPhone|iPad|iPod/.test(navigator.userAgent);
const onEscapeContextMenu = () => {
const escEvent = new KeyboardEvent("keydown", { key: "Escape" });
document.dispatchEvent(escEvent);
};
export default function ContextMenuProvider({
children,
isTriggerDisabled,
}: ContextMenuProviderProps) {
const [isContextMenuOpen, setIsContextMenuOpen] = useState(false);
const onDuplicate = () => {
console.log("Duplicate 실행");
// 여기에 Duplicate 로직 추가
};
const onLink = () => {
console.log("Link 실행");
// 여기에 Link 로직 추가
};
const onUnlink = () => {
console.log("Unlink 실행");
// 여기에 Unlink 로직 추가
};
const onSummary = () => {
console.log("Summary 실행");
// 여기에 Summary 로직 추가
};
const onDelete = () => {
console.log("Delete 실행");
// 여기에 Delete 로직 추가
};
const onCloseContextMenu = useCallback(() => {
setIsContextMenuOpen(false);
onEscapeContextMenu();
}, []);
const onKeyDown = useCallback(
(e: KeyboardEvent) => {
const key = e.key.toLowerCase();
const isModifierPressed = isMac ? e.metaKey : e.ctrlKey;
switch (key) {
case "d":
if (!isModifierPressed) return;
e.preventDefault();
onDuplicate();
onCloseContextMenu();
break;
case "l":
e.preventDefault();
onLink();
onCloseContextMenu();
break;
case "u":
e.preventDefault();
onUnlink();
onCloseContextMenu();
break;
case "s":
e.preventDefault();
onSummary();
onCloseContextMenu();
break;
case "x":
e.preventDefault();
onDelete();
onCloseContextMenu();
break;
}
},
[onCloseContextMenu]
);
useEffect(() => {
if (!open) return;
window.addEventListener("keydown", onKeyDown);
return () => {
window.removeEventListener("keydown", onKeyDown);
};
}, [isContextMenuOpen, onKeyDown]);
return (
<ContextMenu onOpenChange={setIsContextMenuOpen}>
<ContextMenuTrigger asChild disabled={isTriggerDisabled}>
{children}
</ContextMenuTrigger>
<ContextMenuContent className="min-w-60 rounded-2xl bg-[#333] p-5 text-white">
<ContextMenuItem
className="flex items-center justify-between rounded-xl px-4 py-1 focus:bg-[#7E4ED7] focus:text-white"
onSelect={onDuplicate}
>
<p className="text-xl font-bold">Duplicate</p>
<p className="font-light">{isMac ? "⌘D" : "Ctrl+D"}</p>
</ContextMenuItem>
<ContextMenuSeparator className="my-5" />
<ContextMenuItem
className="focus:bg-primary flex items-center justify-between rounded-xl px-4 py-1 focus:text-white"
onSelect={onLink}
>
<p className="text-xl font-bold">Link</p>
<p className="font-light">L</p>
</ContextMenuItem>
<ContextMenuItem
className="focus:bg-primary flex items-center justify-between rounded-xl px-4 py-1 focus:text-white"
onSelect={onUnlink}
>
<p className="text-xl font-bold">Unlink</p>
<p className="font-light">U</p>
</ContextMenuItem>
<ContextMenuItem
className="focus:bg-primary flex items-center justify-between rounded-xl px-4 py-1 focus:text-white"
onSelect={onSummary}
>
<p className="text-xl font-bold">Summary</p>
<p className="font-light">S</p>
</ContextMenuItem>
<ContextMenuItem
className="focus:bg-primary flex items-center justify-between rounded-xl px-4 py-1 focus:text-white"
onSelect={onDelete}
>
<p className="text-xl font-bold">Delete</p>
<p className="font-light">X</p>
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
);
}