|
| 1 | +import type { UnionToIntersection } from "ts-essentials"; |
| 2 | + |
| 3 | +import type { ExposedFunctions } from "../../vscode-host/src/deth/in-iframe/lib"; |
| 4 | + |
| 5 | +const log = console.log.bind(console, "\x1b[32m [ecv:top]"); |
| 6 | + |
| 7 | +// on load, we copy pathname and search params to the iframe |
| 8 | +const iframe = document.getElementById("frame")! as HTMLIFrameElement; |
| 9 | +const url = new URL(iframe.getAttribute("src")!); |
| 10 | +const { hostname, pathname, search } = window.location; |
| 11 | + |
| 12 | +url.pathname = pathname; |
| 13 | +url.search = search; |
| 14 | + |
| 15 | +// prefix of the hostname is passed to `explorer` search param |
| 16 | +// @see vscode-host/src/deth/commands/ethViewerCommands.ts |
| 17 | +if (hostname.endsWith(".deth.net") && !url.searchParams.get("explorer")) { |
| 18 | + url.searchParams.set("explorer", hostname.slice(0, -9)); |
| 19 | +} |
| 20 | + |
| 21 | +log("setting iframe src:", url.href); |
| 22 | +iframe.setAttribute("src", url.href); |
| 23 | +// @todo in the future, we should also listen for postMessage events when |
| 24 | +// the ECV app inside of the iframe updates the URL, but it's not happening |
| 25 | +// right now |
| 26 | + |
| 27 | +void (function exposeFunctions() { |
| 28 | + let channel: MessageChannel; |
| 29 | + |
| 30 | + const exposedFunctions: ExposedFunctions.Async = { |
| 31 | + /** |
| 32 | + * see VSCode's BrowserKeyboardMapperFactoryBase._getBrowserKeyMapping |
| 33 | + */ |
| 34 | + async getLayoutMap() { |
| 35 | + const keyboard = (navigator as any).keyboard as NavigatorKeyboard | null; |
| 36 | + |
| 37 | + if (!keyboard) { |
| 38 | + throw new Error( |
| 39 | + '"navigator.keyboard" is not available — it should not be called from the editor' |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + const keyboardLayoutMap = await keyboard.getLayoutMap(); |
| 44 | + // KeyboardLayoutMap can't be cloned, so it can't be sent with postMessage |
| 45 | + return new Map(keyboardLayoutMap); |
| 46 | + }, |
| 47 | + async setTitle(title: string) { |
| 48 | + document.title = title; |
| 49 | + }, |
| 50 | + }; |
| 51 | + |
| 52 | + type ExposedFunctionCall = { |
| 53 | + [P in keyof ExposedFunctions]: { |
| 54 | + type: P; |
| 55 | + args: Parameters<ExposedFunctions[P]>; |
| 56 | + }; |
| 57 | + }[keyof ExposedFunctions]; |
| 58 | + |
| 59 | + iframe.addEventListener("load", function onLoad() { |
| 60 | + iframe.removeEventListener("load", onLoad); |
| 61 | + |
| 62 | + channel = new MessageChannel(); |
| 63 | + const iframeWindow = iframe.contentWindow!; |
| 64 | + |
| 65 | + log("iframe loaded, passing channel port"); |
| 66 | + // The code responsible for interaction with this port lies in |
| 67 | + // vscode-host/src/deth/in-iframe.ts |
| 68 | + iframeWindow.postMessage({ type: "port-open" }, "*", [channel.port2]); |
| 69 | + |
| 70 | + channel.port1.start(); |
| 71 | + channel.port1.onmessage = (event) => { |
| 72 | + if (event.data && "type" in event.data) { |
| 73 | + const data = event.data as ExposedFunctionCall; |
| 74 | + const fun = exposedFunctions[data.type]; |
| 75 | + |
| 76 | + // This isn't very pretty, but it's a way to preserve a semblance of |
| 77 | + // and avoid a huge switch case with this command-dispatch pattern |
| 78 | + type Fun = UnionToIntersection<typeof fun>; |
| 79 | + type Args = Parameters<Fun>; |
| 80 | + |
| 81 | + void (fun as Fun)(...(data.args as Args)).then((res) => { |
| 82 | + log(`returned from ${data.type}:`, res, event.data); |
| 83 | + |
| 84 | + channel.port1.postMessage({ type: "result", fun: data.type, res }); |
| 85 | + }); |
| 86 | + } else { |
| 87 | + log("received unexpected message from iframe:", event.data); |
| 88 | + } |
| 89 | + }; |
| 90 | + }); |
| 91 | +})(); |
| 92 | + |
| 93 | +interface NavigatorKeyboard { |
| 94 | + getLayoutMap(): Promise<KeyboardLayoutMap>; |
| 95 | +} |
| 96 | +/** https://developer.mozilla.org/en-US/docs/Web/API/KeyboardLayoutMap */ |
| 97 | +interface KeyboardLayoutMap |
| 98 | + extends Iterable< |
| 99 | + [label: string, key: string] /* example: ['BracketRight', ']'] */ |
| 100 | + > {} |
0 commit comments