Skip to content

Commit 6a0ebfb

Browse files
authored
fix(console): capture keydown events to handle clipboard actions inside vs-code (#3927)
Fixes clipboard actions (copy, paste, select-all) for Console inside VS Code. There is an open issue in vscode repo microsoft/vscode#129178 talking about same problem. solves #3904 *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
1 parent 5f56442 commit 6a0ebfb

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

apps/wing-console/console/app/demo/index.w

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ let api = new cloud.Api();
88
api.get("/test-get", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
99
return cloud.ApiResponse {
1010
status: 200,
11-
body: Json.stringify({
12-
query: Json (Json req).get("query"),
13-
})
11+
body: Json.stringify(req.query)
1412
};
1513
});
1614
api.post("/test-post", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {

apps/wing-console/console/ui/src/Console.tsx

+43-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
22
import { httpLink, wsLink, splitLink, createWSClient } from "@trpc/client";
33
import { Mode } from "@wingconsole/design-system";
44
import { Trace } from "@wingconsole/server";
5+
import { useEffect, useMemo } from "react";
56

67
import { App } from "./App.js";
78
import { AppContext } from "./AppContext.js";
@@ -58,7 +59,48 @@ export const Console = ({
5859

5960
let windowTitle = title ?? "Wing Console";
6061

61-
const appMode = layout === LayoutType.Default ? "local" : "remote";
62+
const appMode = useMemo(() => {
63+
return layout === LayoutType.Default || layout === LayoutType.Vscode
64+
? "local"
65+
: "remote";
66+
}, [layout]);
67+
68+
// This is a workaround for handling copy/paste when running in VSCode
69+
// There is an open issue in VSCode repo: https://github.com/microsoft/vscode/issues/129178
70+
useEffect(() => {
71+
if (layout !== LayoutType.Vscode) {
72+
return;
73+
}
74+
75+
const vscodeCommands = (event: KeyboardEvent) => {
76+
if (!(event.ctrlKey || event.metaKey)) {
77+
return;
78+
}
79+
switch (event.code) {
80+
case "KeyC": {
81+
document.execCommand("copy");
82+
break;
83+
}
84+
case "KeyX": {
85+
document.execCommand("cut");
86+
break;
87+
}
88+
case "KeyV": {
89+
document.execCommand("paste");
90+
break;
91+
}
92+
case "KeyA": {
93+
document.execCommand("selectAll");
94+
}
95+
}
96+
};
97+
98+
window.addEventListener("keydown", vscodeCommands);
99+
return () => {
100+
window.removeEventListener("keydown", vscodeCommands);
101+
};
102+
}, [layout]);
103+
62104
return (
63105
<AppContext.Provider value={{ appMode, title: windowTitle }}>
64106
<trpc.Provider client={trpcClient} queryClient={queryClient}>

0 commit comments

Comments
 (0)