Skip to content
TypeScript

fix(desktop): PDFs in the preview can be annotated (#24022)

shivam6ba807810b2f
diff --git a/apps/desktop/src/preview/PickPreload.ts b/apps/desktop/src/preview/PickPreload.ts
index e49d27d40ba..4059468da22 100644
--- a/apps/desktop/src/preview/PickPreload.ts
+++ b/apps/desktop/src/preview/PickPreload.ts
@@ -35,6 +35,7 @@ import {
START_PICK_CHANNEL,
} from "./GuestProtocol.ts";
const OVERLAY_ATTRIBUTE = "data-t3code-annotation-ui";
+const INPUT_LAYER_ATTRIBUTE = "data-t3code-annotation-input";
const Z_INDEX_OVERLAY = 2147483646;
const PRIMARY = "var(--t3-primary)";
const PRIMARY_FILL = "color-mix(in srgb, var(--t3-primary) 10%, transparent)";
@@ -268,7 +269,7 @@ function isAnnotationNode(element: Element): boolean {
function pickFromPoint(clientX: number, clientY: number): Element | null {
for (const candidate of document.elementsFromPoint(clientX, clientY)) {
if (!(candidate instanceof Element)) continue;
- if (isAnnotationNode(candidate)) continue;
+ if (isAnnotationNode(candidate) || candidate.hasAttribute(INPUT_LAYER_ATTRIBUTE)) continue;
if (candidate === document.documentElement || candidate === document.body) continue;
return candidate;
}
@@ -636,7 +637,18 @@ function startAnnotation(sendEnabled: boolean): void {
const strokes: PreviewAnnotationStrokeTarget[] = [];
const styleChanges = new Map<string, PreviewAnnotationStyleChange>();
const toolButtons = new Map<AnnotationTool, HTMLButtonElement>();
- let tool: AnnotationTool = "select";
+ // Chromium draws a PDF in its own viewer frame, so pointer and key input over it
+ // never reaches this document. A transparent layer above the viewer catches the
+ // pointer and takes focus for Escape and the tool keys. A PDF has no elements to
+ // select, so it starts on Region.
+ const isPdf = document.contentType === "application/pdf";
+ const inputLayer = isPdf ? document.createElement("div") : null;
+ if (inputLayer) {
+ inputLayer.setAttribute(INPUT_LAYER_ATTRIBUTE, "");
+ inputLayer.tabIndex = -1;
+ inputLayer.style.cssText = `position:fixed;inset:0;z-index:${Z_INDEX_OVERLAY - 1};cursor:crosshair;outline:none`;
+ }
+ let tool: AnnotationTool = isPdf ? "marquee" : "select";
let dragStart: PreviewAnnotationPoint | null = null;
let activeStroke: { target: PreviewAnnotationStrokeTarget; path: SVGPathElement } | null = null;
let pendingCapture = false;
@@ -1356,6 +1368,7 @@ function startAnnotation(sendEnabled: boolean): void {
ipcRenderer.off(ANNOTATION_CAPTURED_CHANNEL, onCaptured);
document.documentElement.removeAttribute("data-t3code-annotation-tool");
cursorStyle.remove();
+ inputLayer?.remove();
host.remove();
activeSession = null;
if (notifyMain) ipcRenderer.send(ELEMENT_PICKED_CHANNEL, null);
@@ -1467,6 +1480,10 @@ function startAnnotation(sendEnabled: boolean): void {
window.addEventListener("resize", repaint, { passive: true });
ipcRenderer.on(CANCEL_PICK_CHANNEL, onCancel);
ipcRenderer.on(ANNOTATION_CAPTURED_CHANNEL, onCaptured);
+ if (inputLayer) {
+ document.documentElement.appendChild(inputLayer);
+ inputLayer.focus({ preventScroll: true });
+ }
document.documentElement.appendChild(host);
rootResizeObserver.observe(document.documentElement);
refreshToolButtons();