Skip to content

Commit df06ee6

Browse files
committed
pdf: make double click work with annotation layer
1 parent c5d8fd0 commit df06ee6

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/packages/frontend/frame-editors/latex-editor/pdfjs-page.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
PDFDocumentProxy,
1313
PDFPageProxy,
1414
} from "pdfjs-dist/webpack.mjs";
15-
15+
import { useRef } from "react";
1616
import { SyncHighlight } from "./pdfjs-annotation";
1717
import CanvasPage from "./pdfjs-canvas-page";
1818

@@ -38,6 +38,8 @@ export default function Page({
3838
page,
3939
syncHighlight,
4040
}: PageProps) {
41+
const divRef = useRef<HTMLDivElement | null>(null);
42+
4143
async function clickAnnotation(
4244
annotation0: PDFAnnotationData,
4345
): Promise<void> {
@@ -78,19 +80,27 @@ export default function Page({
7880
style={{ height: `${PAGE_GAP + viewport.height}px`, background: BG_COL }}
7981
>
8082
<div
83+
ref={divRef}
8184
style={{
8285
height: `${viewport.height}px`,
8386
width: `${viewport.width}px`,
8487
background: "white",
8588
margin: "auto",
8689
}}
8790
onDoubleClick={(event) => {
88-
if (!actions.synctex_pdf_to_tex) {
91+
const elt = divRef.current;
92+
if (!actions.synctex_pdf_to_tex || !elt) {
8993
// no support for synctex for whatever is using this.
9094
return;
9195
}
92-
const x: number = event.nativeEvent.offsetX / scale;
93-
const y: number = event.nativeEvent.offsetY / scale;
96+
// we cannot directly use event.nativeEvent.offsetX since user may
97+
// have double clicked on a span in the text layer, in which case
98+
// offset would be relative to that span.
99+
const divRect = elt.getBoundingClientRect();
100+
const offsetX = event.nativeEvent.clientX - divRect.left;
101+
const offsetY = event.nativeEvent.clientY - divRect.top;
102+
const x: number = offsetX / scale;
103+
const y: number = offsetY / scale;
94104
actions.synctex_pdf_to_tex(n, x, y);
95105
}}
96106
>

0 commit comments

Comments
 (0)