mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-22 16:48:23 +01:00
9e1460b9e1
This PR swaps usage of the `uuid` library for the built-in `crypto.randomUUID` function, which is available in both NodeJS and the browser. The built-in function is around 12x faster than the `uuid` library. The strings produced by the built-in function are fully compatible with the UUIDv4 standard, so it's an easy switch.
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
// Copyright 2024, Command Line Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import { getApi } from "./global";
|
|
|
|
class ContextMenuModelType {
|
|
handlers: Map<string, () => void> = new Map(); // id -> handler
|
|
|
|
constructor() {
|
|
getApi().onContextMenuClick(this.handleContextMenuClick.bind(this));
|
|
}
|
|
|
|
handleContextMenuClick(id: string): void {
|
|
const handler = this.handlers.get(id);
|
|
if (handler) {
|
|
handler();
|
|
}
|
|
}
|
|
|
|
_convertAndRegisterMenu(menu: ContextMenuItem[]): ElectronContextMenuItem[] {
|
|
const electronMenuItems: ElectronContextMenuItem[] = [];
|
|
for (const item of menu) {
|
|
const electronItem: ElectronContextMenuItem = {
|
|
role: item.role,
|
|
type: item.type,
|
|
label: item.label,
|
|
id: crypto.randomUUID(),
|
|
};
|
|
if (item.click) {
|
|
this.handlers.set(electronItem.id, item.click);
|
|
}
|
|
if (item.submenu) {
|
|
electronItem.submenu = this._convertAndRegisterMenu(item.submenu);
|
|
}
|
|
electronMenuItems.push(electronItem);
|
|
}
|
|
return electronMenuItems;
|
|
}
|
|
|
|
showContextMenu(menu: ContextMenuItem[], ev: React.MouseEvent<any>): void {
|
|
this.handlers.clear();
|
|
const electronMenuItems = this._convertAndRegisterMenu(menu);
|
|
getApi().showContextMenu(electronMenuItems, { x: ev.clientX, y: ev.clientY });
|
|
}
|
|
}
|
|
|
|
const ContextMenuModel = new ContextMenuModelType();
|
|
|
|
export { ContextMenuModel, ContextMenuModelType };
|