Skip to content

Commit 4bf4ae7

Browse files
fix(desktop): annotation tool keys work after clicking a toolbar button (#26211)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 77d17b1 commit 4bf4ae7

5 files changed

Lines changed: 93 additions & 5 deletions

File tree

‎apps/desktop/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"acorn": "8.16.0",
3838
"cross-env": "^10.1.0",
3939
"electron-builder": "26.16.1",
40+
"jsdom": "^30.0.1",
4041
"tailwindcss": "^4.0.0",
4142
"vite-plus": "catalog:"
4243
},
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { expect, it, vi } from "vite-plus/test";
2+
3+
import { START_PICK_CHANNEL } from "./GuestProtocol.ts";
4+
import "./PickPreload.ts";
5+
6+
const ipcListeners = vi.hoisted(() => new Map<string, Array<(...args: unknown[]) => void>>());
7+
8+
vi.mock("electron", () => ({
9+
ipcRenderer: {
10+
on: (channel: string, listener: (...args: unknown[]) => void) => {
11+
ipcListeners.set(channel, [...(ipcListeners.get(channel) ?? []), listener]);
12+
},
13+
off: () => {},
14+
send: () => {},
15+
invoke: async () => undefined,
16+
},
17+
}));
18+
19+
vi.mock("react-grab/primitives", () => ({ getElementContext: async () => null }));
20+
21+
const startAnnotation = (): ShadowRoot => {
22+
vi.stubGlobal(
23+
"ResizeObserver",
24+
class {
25+
observe() {}
26+
disconnect() {}
27+
},
28+
);
29+
using attachShadow = vi.spyOn(Element.prototype, "attachShadow");
30+
for (const listener of ipcListeners.get(START_PICK_CHANNEL) ?? []) listener({}, undefined, false);
31+
const shadowRoot = attachShadow.mock.results[0]?.value;
32+
if (!(shadowRoot instanceof ShadowRoot)) throw new Error("annotation overlay did not start");
33+
return shadowRoot;
34+
};
35+
36+
it("switches tools from the keyboard after a toolbar button is clicked", () => {
37+
const shadowRoot = startAnnotation();
38+
const region = Array.from(shadowRoot.querySelectorAll("button")).find(
39+
(button) => button.textContent === "Region",
40+
);
41+
if (!region) throw new Error("Region toolbar button not found");
42+
43+
region.focus();
44+
region.click();
45+
expect(document.documentElement.getAttribute("data-t3code-annotation-tool")).toBe("marquee");
46+
47+
region.dispatchEvent(new KeyboardEvent("keydown", { key: "v", bubbles: true, composed: true }));
48+
49+
expect(
50+
document.documentElement.getAttribute("data-t3code-annotation-tool"),
51+
"V should switch to Select while focus is on a toolbar button",
52+
).toBe("select");
53+
});
54+
55+
it("keeps the tool while typing a tool key in the comment", () => {
56+
const shadowRoot = startAnnotation();
57+
const comment = shadowRoot.querySelector("textarea");
58+
if (!comment) throw new Error("comment textarea not found");
59+
60+
comment.focus();
61+
comment.dispatchEvent(new KeyboardEvent("keydown", { key: "r", bubbles: true, composed: true }));
62+
63+
expect(document.documentElement.getAttribute("data-t3code-annotation-tool")).toBe("select");
64+
});

‎apps/desktop/src/preview/PickPreload.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1673,13 +1673,13 @@ function startAnnotation(
16731673
return;
16741674
}
16751675
}
1676-
if (isAnnotationNode(event.target as Element) && event.key !== "Escape") return;
16771676
if (event.key === "Escape") {
16781677
event.preventDefault();
16791678
event.stopPropagation();
16801679
teardown(true);
16811680
return;
16821681
}
1682+
if (shadowRoot.activeElement?.matches("input, textarea, select")) return;
16831683
if (event.key === "v") tool = "select";
16841684
else if (event.key === "r") tool = "marquee";
16851685
else if (event.key === "d") tool = "draw";

‎apps/desktop/vite.config.ts‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isDesktopRuntimeExternalDependency } from "../../scripts/lib/desktop-ex
55
import { loadRepoEnv } from "../../scripts/lib/public-config.ts";
66

77
const repoEnv = loadRepoEnv();
8+
const domTests = ["src/preview/PickPreload.test.ts"];
89

910
// Install only bundle externals; the server bundle already carries its dependency tree.
1011
const isMainProcessExternal = (id: string) =>
@@ -151,13 +152,32 @@ export default defineConfig({
151152
},
152153
],
153154
test: {
154-
include: [...defaultInclude, "../../native/kde-snap-shot/src/*.test.ts"],
155155
// The Windows lane runs workspace suites concurrently; filesystem-heavy
156156
// desktop integration tests can exceed Vitest's 5 second default there.
157157
testTimeout: 15_000,
158-
setupFiles: [
159-
"../../packages/shared/src/testing/longTempDir.ts",
160-
"../../packages/shared/src/testing/providerEnv.ts",
158+
projects: [
159+
{
160+
extends: true,
161+
test: {
162+
name: "unit",
163+
include: [...defaultInclude, "../../native/kde-snap-shot/src/*.test.ts"],
164+
exclude: ["**/node_modules/**", ...domTests],
165+
setupFiles: [
166+
"../../packages/shared/src/testing/longTempDir.ts",
167+
"../../packages/shared/src/testing/providerEnv.ts",
168+
],
169+
},
170+
},
171+
{
172+
extends: true,
173+
test: {
174+
name: "dom",
175+
include: domTests,
176+
// Guest preloads run in the page; these tests drive them against a DOM.
177+
environment: "jsdom",
178+
setupFiles: ["../../packages/shared/src/testing/providerEnv.ts"],
179+
},
180+
},
161181
],
162182
},
163183
});

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)