UNPKG

@laserware/hoverboard

Version:

Better context menus for Electron.

1 lines 2.76 kB
{"version":3,"sources":["../src/sandbox/preload.ts","../src/sandbox/globals.ts"],"sourcesContent":["import { contextBridge, ipcRenderer } from \"electron\";\n\nimport {\n type HoverboardGlobals,\n IpcChannel,\n type ShowContextMenuRequest,\n type ShowContextMenuResponse,\n hoverboardApiKey,\n} from \"./globals.js\";\n\nfunction preloadHoverboard(): void {\n const globals: HoverboardGlobals = {\n showContextMenu(\n request: ShowContextMenuRequest,\n ): Promise<ShowContextMenuResponse> {\n return ipcRenderer.invoke(IpcChannel.ForShowContextMenu, request);\n },\n hideContextMenu(id: string): Promise<void> {\n return ipcRenderer.invoke(IpcChannel.ForHideContextMenu, id);\n },\n };\n\n if (process.contextIsolated) {\n contextBridge.exposeInMainWorld(hoverboardApiKey, globals);\n } else {\n window[hoverboardApiKey] = globals;\n }\n}\n\npreloadHoverboard();\n","import type {\n KeyboardEvent,\n MenuItemConstructorOptions,\n Point,\n} from \"electron\";\n\nexport const hoverboardApiKey = \"__laserware_hoverboard__\";\n\ndeclare global {\n interface Window {\n [hoverboardApiKey]: HoverboardGlobals;\n }\n}\n\n// biome-ignore lint/suspicious/noConstEnum: Swapped out with value in build process.\nexport const enum IpcChannel {\n ForShowContextMenu = \"hoverboard/contextMenu/show\",\n ForHideContextMenu = \"hoverboard/contextMenu/hide\",\n}\n\nexport interface ShowContextMenuRequest {\n menuId: string;\n position: Point;\n linkURL?: string;\n template: MenuItemConstructorOptions[];\n}\n\nexport interface ShowContextMenuResponse {\n menuId: string;\n menuItemId: string | null;\n event: KeyboardEvent;\n}\n\nexport interface HoverboardGlobals {\n showContextMenu(\n request: ShowContextMenuRequest,\n ): Promise<ShowContextMenuResponse>;\n hideContextMenu(menuId: string): Promise<void>;\n}\n\nexport function getHoverboardGlobals(): HoverboardGlobals {\n const windowGlobals = window[hoverboardApiKey];\n\n if (windowGlobals === undefined) {\n throw new Error(\"Globals not found, need to use preload\");\n }\n\n return {\n showContextMenu(\n request: ShowContextMenuRequest,\n ): Promise<ShowContextMenuResponse> {\n return windowGlobals.showContextMenu(request);\n },\n hideContextMenu(menuId: string): Promise<void> {\n return windowGlobals.hideContextMenu(menuId);\n },\n };\n}\n"],"mappings":";;;AAAA,sBAA2C;;;ACMpC,IAAM,mBAAmB;;;ADIhC,SAAS,oBAA0B;AACjC,QAAM,UAA6B;AAAA,IACjC,gBACE,SACkC;AAClC,aAAO,4BAAY,+DAAsC,OAAO;AAAA,IAClE;AAAA,IACA,gBAAgB,IAA2B;AACzC,aAAO,4BAAY,+DAAsC,EAAE;AAAA,IAC7D;AAAA,EACF;AAEA,MAAI,QAAQ,iBAAiB;AAC3B,kCAAc,kBAAkB,kBAAkB,OAAO;AAAA,EAC3D,OAAO;AACL,WAAO,gBAAgB,IAAI;AAAA,EAC7B;AACF;AAEA,kBAAkB;","names":[]}