UNPKG

@laserware/hoverboard

Version:

Better context menus for Electron.

1 lines 2.87 kB
{"version":3,"sources":["../src/sandbox/globals.ts","../src/sandbox/preload.ts"],"names":[],"mappings":";;;;;AAMO,IAAM,gBAAmB,GAAA,0BAAA;;;ACIhC,SAAS,iBAA0B,GAAA;AACjC,EAAA,MAAM,OAA6B,GAAA;AAAA,IACjC,gBACE,OACkC,EAAA;AAClC,MAAO,OAAA,WAAA,CAAY,+DAAsC,OAAO,CAAA;AAAA,KAClE;AAAA,IACA,gBAAgB,EAA2B,EAAA;AACzC,MAAO,OAAA,WAAA,CAAY,+DAAsC,EAAE,CAAA;AAAA;AAC7D,GACF;AAEA,EAAA,IAAI,QAAQ,eAAiB,EAAA;AAC3B,IAAc,aAAA,CAAA,iBAAA,CAAkB,kBAAkB,OAAO,CAAA;AAAA,GACpD,MAAA;AACL,IAAA,MAAA,CAAO,gBAAgB,CAAI,GAAA,OAAA;AAAA;AAE/B;AAEA,iBAAkB,EAAA","file":"preload.mjs","sourcesContent":["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","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"]}