Skip to content

Commit e087d2e

Browse files
committed
fix(desktop): ignore synthetic annotation keyboard actions
1 parent aef0ddf commit e087d2e

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎apps/desktop/src/preview/AnnotationKeyboard.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ describe("resolveAnnotationSubmission", () => {
3737
});
3838

3939
const dispatchKey = (
40-
input: { inOverlay?: boolean; commentFocused?: boolean; canSend?: boolean } = {},
40+
input: {
41+
inOverlay?: boolean;
42+
commentFocused?: boolean;
43+
canSend?: boolean;
44+
isTrusted?: boolean;
45+
} = {},
4146
overrides: Partial<Parameters<typeof resolveAnnotationSubmission>[0]> = {},
4247
type = "keydown",
4348
) => {
4449
const window = new EventTarget();
4550
const event = Object.assign(new Event(type, { cancelable: true }), keyboardEvent(overrides));
51+
Object.defineProperty(event, "isTrusted", { value: input.isTrusted ?? true });
4652
const actions: Array<ReturnType<typeof handleAnnotationKeyEvent>> = [];
4753
let pageEvents = 0;
4854
window.addEventListener(type, () => {
@@ -102,6 +108,18 @@ describe("annotation keyboard isolation", () => {
102108
});
103109
});
104110

111+
it("ignores page-generated submission and cancellation events on the overlay host", () => {
112+
expect(dispatchKey({ isTrusted: false })).toEqual({
113+
action: null,
114+
pageEvents: 0,
115+
defaultPrevented: false,
116+
});
117+
expect(dispatchKey({ isTrusted: false }, { ctrlKey: true }).action).toBeNull();
118+
expect(dispatchKey({ isTrusted: false }, { metaKey: true }).action).toBeNull();
119+
expect(dispatchKey({ isTrusted: false }, { key: "Escape" }).action).toBeNull();
120+
expect(dispatchKey({ isTrusted: false, inOverlay: false }, { key: "r" }).action).toBeNull();
121+
});
122+
105123
it("leaves page keyboard input and tool selection outside the overlay available", () => {
106124
expect(dispatchKey({ inOverlay: false }, { key: " " }).pageEvents).toBe(1);
107125
expect(dispatchKey({ inOverlay: false }, { key: "r" }).action).toBe("marquee");

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ export function handleAnnotationKeyEvent(
1212
event: AnnotationKeyboardEvent &
1313
Pick<
1414
KeyboardEvent,
15-
"type" | "target" | "preventDefault" | "stopImmediatePropagation" | "stopPropagation"
15+
| "type"
16+
| "target"
17+
| "isTrusted"
18+
| "preventDefault"
19+
| "stopImmediatePropagation"
20+
| "stopPropagation"
1621
>,
1722
input: {
1823
overlayTarget: EventTarget;
@@ -22,7 +27,8 @@ export function handleAnnotationKeyEvent(
2227
): "cancel" | "select" | "marquee" | "draw" | "erase" | PreviewAnnotationSubmission | null {
2328
const inOverlay = event.target === input.overlayTarget;
2429
if (inOverlay) event.stopImmediatePropagation();
25-
if (event.type !== "keydown") return null;
30+
// Page scripts can target the visible shadow host without accessing the editor.
31+
if (event.type !== "keydown" || !event.isTrusted) return null;
2632
if (event.key === "Escape") {
2733
event.preventDefault();
2834
event.stopPropagation();

0 commit comments

Comments
 (0)