@@ -12,7 +12,7 @@ import type {
12
12
PDFDocumentProxy ,
13
13
PDFPageProxy ,
14
14
} from "pdfjs-dist/webpack.mjs" ;
15
-
15
+ import { useRef } from "react" ;
16
16
import { SyncHighlight } from "./pdfjs-annotation" ;
17
17
import CanvasPage from "./pdfjs-canvas-page" ;
18
18
@@ -38,6 +38,8 @@ export default function Page({
38
38
page,
39
39
syncHighlight,
40
40
} : PageProps ) {
41
+ const divRef = useRef < HTMLDivElement | null > ( null ) ;
42
+
41
43
async function clickAnnotation (
42
44
annotation0 : PDFAnnotationData ,
43
45
) : Promise < void > {
@@ -78,19 +80,27 @@ export default function Page({
78
80
style = { { height : `${ PAGE_GAP + viewport . height } px` , background : BG_COL } }
79
81
>
80
82
< div
83
+ ref = { divRef }
81
84
style = { {
82
85
height : `${ viewport . height } px` ,
83
86
width : `${ viewport . width } px` ,
84
87
background : "white" ,
85
88
margin : "auto" ,
86
89
} }
87
90
onDoubleClick = { ( event ) => {
88
- if ( ! actions . synctex_pdf_to_tex ) {
91
+ const elt = divRef . current ;
92
+ if ( ! actions . synctex_pdf_to_tex || ! elt ) {
89
93
// no support for synctex for whatever is using this.
90
94
return ;
91
95
}
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 ;
94
104
actions . synctex_pdf_to_tex ( n , x , y ) ;
95
105
} }
96
106
>
0 commit comments