-
-
Notifications
You must be signed in to change notification settings - Fork 344
/
contextMenu.ts
229 lines (198 loc) · 8.28 KB
/
contextMenu.ts
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import SidebarMenu from './configs/sidebarMenu.config';
import SidebarDriveMenu from './configs/sidebarDriveMenu.config';
import BodyMenu from './configs/bodyMenu.config';
import MultipleSelectedMenu from './configs/multipleSelectedMenu.config';
import contextMenuItem from '../../Typings/contextMenuItem';
import fileThumbnail from '../Thumbnail/thumbnail';
import { getSelected } from '../Files/File Operation/select';
import FileMenu from './configs/fileMenu.config';
let contextMenu = document.querySelector('.contextmenu') as HTMLElement;
let contextMenuSubmenus = document.getElementById('contextmenu-submenus');
interface menuRoles {
[key: string]: () => void;
}
const menuRoles: menuRoles = {};
document.addEventListener('DOMContentLoaded', () => {
contextMenu = document.querySelector('.contextmenu') as HTMLElement;
contextMenuSubmenus = document.getElementById('contextmenu-submenus');
});
const MenuToElements = async (menu: contextMenuItem[][]): Promise<void> => {
for (let index = 0; index < menu.length; index++) {
const section = menu[index];
for (let i = 0; i < section.length; i++) {
const item = section[i];
if (item.visible || item.visible === undefined) {
const menu = document.createElement('span');
menu.classList.add('contextmenu-item');
if (item.icon) {
if (item.shortcut)
menu.innerHTML = `<img src = "${await fileThumbnail(
item.icon,
'contextmenu',
false
)}">${item?.menu.trim()}<span class="contextmenu-item-shortcut">${item.shortcut}</span>`;
else menu.innerHTML = `<img src = "${await fileThumbnail(item.icon, 'contextmenu', false)}" >${item?.menu?.trim()}`;
} else {
if (item.shortcut) menu.innerHTML = `${item?.menu?.trim()}<span class="contextmenu-item-shortcut">${item.shortcut}</span>`;
else menu.innerHTML = item?.menu?.trim();
}
if (typeof item?.role === 'function') {
const roleIdentifier = Math.random().toString(36).substr(2, 10) + item?.menu?.replace(/\W/g, '')?.trim();
menu.setAttribute('role', roleIdentifier);
menuRoles[roleIdentifier] = item?.role;
}
contextMenu.appendChild(menu);
const submenuId = Math.random().toString(36).substr(2, 10);
// Create submenu element for context menu
if (item.submenu) {
const submenu = document.createElement('div');
submenu.classList.add('contextmenu-submenu');
menu.dataset.submenu = submenuId;
submenu.id = submenuId;
contextMenuSubmenus.appendChild(submenu);
for (let j = 0; j < item.submenu.length; j++) {
const submenuItem = item.submenu[j];
const submenuItemElement = document.createElement('span');
submenuItemElement.classList.add('contextmenu-item');
if (submenuItem.icon) {
if (submenuItem.shortcut)
submenuItemElement.innerHTML = `<img src = "${await fileThumbnail(submenuItem.icon, 'contextmenu', false)}">${
submenuItem.name ?? submenuItem
}<span class="contextmenu-item-shortcut">${submenuItem.shortcut}</span>`;
else
submenuItemElement.innerHTML = `<img src = "${await fileThumbnail(
submenuItem.icon,
'contextmenu',
false
)}" >${submenuItem?.name?.trim()}`;
} else {
if (submenuItem.shortcut)
submenuItemElement.innerHTML = `${submenuItem.name ?? submenuItem}<span class="contextmenu-item-shortcut">${
submenuItem.shortcut
}</span>`;
else submenuItemElement.innerHTML = submenuItem.name;
}
if (typeof submenuItem?.role === 'function') {
const roleIdentifier = Math.random().toString(36).substr(2, 10) + submenuItem?.name?.replace(/\W/g, '')?.trim();
submenuItemElement.setAttribute('role', roleIdentifier);
menuRoles[roleIdentifier] = submenuItem?.role;
}
submenu.appendChild(submenuItemElement);
}
}
}
}
if (index !== menu.length - 1 && section.filter((menu) => menu.visible !== false).length > 0) contextMenu.innerHTML += `<hr />`;
}
return;
};
/**
* Context menu initializer
* @returns {void}
*/
const ContextMenu = (): void => {
document.addEventListener('contextmenu', async (e) => {
e.preventDefault();
document.querySelectorAll('.hover-preview').forEach((element) => {
element.parentNode.removeChild(element);
});
contextMenu.innerHTML = '';
contextMenu.style.height = 'initial';
contextMenu.style.overflowY = 'initial';
contextMenuSubmenus.innerHTML = '';
let coorX = e.pageX;
let coorY = e.pageY;
let target = e.target as HTMLElement;
while (target.dataset && !target.dataset.path) {
target = target.parentNode as HTMLElement;
}
if (!target?.dataset?.path) return;
const filePath = unescape(target.dataset.path);
// Create the context menu
if (getSelected().length > 1) {
await MenuToElements(await MultipleSelectedMenu(target, filePath));
} else if (target.classList.contains('sidebar-item')) {
await MenuToElements(await SidebarMenu(target, filePath));
} else if (target.classList.contains('drive-item')) {
await MenuToElements(await SidebarDriveMenu(target, filePath));
} else if (target === document.getElementById('workspace')) {
await MenuToElements(await BodyMenu(target, filePath));
} else {
await MenuToElements(await FileMenu(target, filePath));
}
if (coorY + contextMenu.offsetHeight > window.innerHeight && coorY - contextMenu.offsetHeight > -50) {
coorY -= contextMenu.offsetHeight;
}
if (coorX + contextMenu.offsetWidth > window.innerWidth) coorX = window.innerWidth - contextMenu.offsetWidth;
if (contextMenu.offsetHeight + coorY > window.innerHeight) {
contextMenu.style.height = `${
window.innerHeight - coorY - parseInt(window.getComputedStyle(contextMenu).getPropertyValue('padding-top')) * 2
}px`;
contextMenu.style.overflowY = 'auto';
}
contextMenu.style.left = coorX + 'px';
contextMenu.style.top = coorY + 'px';
contextMenu.scrollTop = 0;
document.addEventListener('click', exitContextMenu);
});
const exitContextMenu = () => {
contextMenu.style.left = '-100vw';
contextMenu.style.top = '-100vh';
contextMenuSubmenus.innerHTML = '';
document.removeEventListener('click', exitContextMenu);
};
// Submenu handler
document.addEventListener('mousemove', (e) => {
// Expand contextmenu
if (
(e.pageX >= contextMenu.offsetLeft + contextMenu.offsetWidth - 15 || e.pageX <= contextMenu.offsetLeft + 15) &&
e.pageX < contextMenu.offsetLeft + contextMenu.offsetWidth + 100
) {
return;
}
if (!((e.target as HTMLElement).parentNode as HTMLElement).className.startsWith('contextmenu-submenu')) {
document.querySelectorAll('.contextmenu-submenu').forEach((submenu) => ((submenu as HTMLElement).style.display = 'none'));
}
if (
(e.target as HTMLElement).dataset.submenu ||
((e.target as HTMLElement).parentNode as HTMLElement).className.startsWith('contextmenu-submenu')
) {
const submenuElement = document.getElementById((e.target as HTMLElement).dataset.submenu);
if (!submenuElement) return;
const menuCoordinate = (e.target as HTMLElement).getBoundingClientRect();
submenuElement.style.display = 'block';
submenuElement.style.height = 'initial';
submenuElement.style.overflowY = 'initial';
let submenuCoorX = contextMenu.offsetLeft + contextMenu.offsetWidth;
if (submenuCoorX + submenuElement.offsetWidth * 0.5 >= window.innerWidth) {
submenuCoorX = contextMenu.offsetLeft - submenuElement.offsetWidth;
}
if (submenuElement.offsetHeight + menuCoordinate.top > window.innerHeight) {
submenuElement.style.height = `${
window.innerHeight -
submenuElement.offsetHeight -
parseInt(window.getComputedStyle(submenuElement).getPropertyValue('padding-top')) * 2
}px`;
submenuElement.style.overflowY = 'auto';
}
submenuElement.style.left = submenuCoorX + 'px';
submenuElement.style.top = menuCoordinate.top + 'px';
submenuElement.scrollTop = 0;
}
});
contextMenu.addEventListener('click', (e) => {
exitContextMenu();
const menuClicked = e.target as HTMLElement;
const menuRole = menuClicked?.getAttribute('role');
if (!menuRole) return;
menuRoles[menuRole]();
});
contextMenuSubmenus.addEventListener('click', (e) => {
exitContextMenu();
const menuClicked = e.target as HTMLElement;
const menuRole = menuClicked?.getAttribute('role');
if (!menuRole) return;
menuRoles[menuRole]();
});
};
export default ContextMenu;