Skip to content

Commit c5d8fd0

Browse files
committed
fix #7853 -- this totally works... but
- inverse search on doubleclick in latex is broken The cool thing is that drag to pan *and* selection both work together, which is something chrome's native pdf doesn't support.
1 parent 3d9033f commit c5d8fd0

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,8 @@ export default function CanvasPage({
111111
clickAnnotation={clickAnnotation}
112112
syncHighlight={syncHighlight}
113113
/>
114-
<TextLayer
115-
page={page}
116-
scale={scale}
117-
viewport={viewport}
118-
/>
119114
<div ref={divRef} />
115+
<TextLayer page={page} scale={scale} viewport={viewport} />
120116
</div>
121117
);
122118
}

src/packages/frontend/frame-editors/latex-editor/pdfjs-text.css

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* This file is part of CoCalc: Copyright © 2024 Sagemath, Inc.
3+
* License: MS-RSL – see LICENSE.md for details
4+
*/
5+
16
/* See https://github.com/mozilla/pdf.js/blob/a7e1bf64c4c7a42c7577ce9490054faa1c4eda99/test/text_layer_test.css */
27

38
.cocalc-pdfjs-text-layer {
@@ -7,13 +12,12 @@
712
opacity: 1;
813

914
:is(span, br) {
10-
color: black;
15+
color: transparent;
1116
position: absolute;
1217
white-space: pre;
1318
transform-origin: 0% 0%;
14-
border: solid 1px rgb(255 0 0 / 0.5);
15-
background-color: rgb(255 255 32 / 0.1);
1619
box-sizing: border-box;
20+
cursor: text;
1721
}
1822

1923
.markedContent {

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
2+
* This file is part of CoCalc: Copyright © 2024 Sagemath, Inc.
33
* License: MS-RSL – see LICENSE.md for details
44
*/
55

@@ -8,16 +8,11 @@ Render the text layer on top of a page to support selection.
88
99
How the hell did I figure this code out? First, there is nightmare of misleading and
1010
useless outdated google hits from the last 10+ years. Finally, searching the
11-
pdfjs git repo yielded this, which was helpful, which is I guess how this is
12-
implemented for their own viewer:
13-
11+
pdfjs git repo yielded a complicated trail and I eventually figured out what pdfjs's
12+
own complete renderer does (we can't use it since we need integration with the latex
13+
editor, etc.):
1414
1515
https://github.com/mozilla/pdf.js/blob/a7e1bf64c4c7a42c7577ce9490054faa1c4eda99/web/text_layer_builder.js#L40
16-
17-
18-
This wasn't helpful:
19-
20-
https://github.com/mozilla/pdf.js/blob/a7e1bf64c4c7a42c7577ce9490054faa1c4eda99/examples/text-only/pdf2svg.mjs#L24
2116
*/
2217

2318
import type { PDFPageProxy } from "pdfjs-dist/webpack.mjs";
@@ -34,10 +29,6 @@ interface Props {
3429
export default function PdfjsTextLayer({ page, scale, viewport }: Props) {
3530
const divRef = useRef<HTMLDivElement | null>(null);
3631

37-
// useEffect(() => {
38-
// (async () => setTextContent(await page.getTextContent()))();
39-
// }, [page]);
40-
4132
useEffect(() => {
4233
const elt = divRef.current;
4334
if (!elt) {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,22 @@ export function PDFJS({
582582
const lastMousePosRef = useRef<null | { x: number; y: number }>(null);
583583

584584
const onMouseDown = useCallback((e) => {
585+
if (e.target?.nodeName == "SPAN") {
586+
// selection layer text -- allows for selecting instead of dragging around
587+
return;
588+
}
585589
lastMousePosRef.current = getClientPos(e);
586590
setCursor("grabbing");
587591
}, []);
588592

589593
const onMouseMove = useCallback((e) => {
590-
if (!e.buttons || lastMousePosRef.current == null) return;
594+
if (
595+
!e.buttons ||
596+
lastMousePosRef.current == null ||
597+
!window.getSelection()?.isCollapsed
598+
) {
599+
return;
600+
}
591601
const { x, y } = getClientPos(e);
592602
const deltaX = lastMousePosRef.current.x - x;
593603
const deltaY = lastMousePosRef.current.y - y;

0 commit comments

Comments
 (0)