forked from agentcooper/react-pdf-highlighter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
181 lines (156 loc) · 4.86 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React, { useEffect, useState } from "react";
import {
AreaHighlight,
Highlight,
PdfHighlighter,
PdfLoader,
Popup,
} from "./react-pdf-highlighter";
import type { IHighlight } from "./react-pdf-highlighter";
import { Sidebar } from "./Sidebar";
import { Spinner } from "./Spinner";
import { testHighlights as _testHighlights } from "./test-highlights";
import "./style/App.css";
const testHighlights: Record<string, Array<IHighlight>> = _testHighlights;
const parseIdFromHash = () =>
document.location.hash.slice("#highlight-".length);
const resetHash = () => {
document.location.hash = "";
};
const HighlightPopup = ({ label }: { label: string }) =>
label ? <div className="Highlight__popup">{label}</div> : null;
const PRIMARY_PDF_URL = "https://arxiv.org/pdf/1708.08021.pdf";
const SECONDARY_PDF_URL = "https://arxiv.org/pdf/1604.02480.pdf";
const searchParams = new URLSearchParams(document.location.search);
const initialUrl = searchParams.get("url") || PRIMARY_PDF_URL;
const App = () => {
const [url, setUrl] = useState<string>(initialUrl);
const [highlights, setHighlights] = useState<IHighlight[]>(
testHighlights[initialUrl] ? [...testHighlights[initialUrl]] : []
);
const toggleDocument = () => {
const newUrl =
url === PRIMARY_PDF_URL ? SECONDARY_PDF_URL : PRIMARY_PDF_URL;
setUrl(newUrl);
setHighlights(testHighlights[newUrl] ? [...testHighlights[newUrl]] : []);
};
let scrollViewerTo = (highlight: any) => {};
const scrollToHighlightFromHash = () => {
const highlight = getHighlightById(parseIdFromHash());
if (highlight) {
scrollViewerTo(highlight);
}
};
useEffect(() => {
window.addEventListener("hashchange", scrollToHighlightFromHash, false);
return () => {
window.removeEventListener(
"hashchange",
scrollToHighlightFromHash,
false
);
};
}, []);
const getHighlightById = (id: string) => {
return highlights.find((highlight) => highlight.id === id);
};
const updateHighlight = (
highlightId: string,
position: Object,
content?: string,
image?: string
) => {
setHighlights(
highlights.map((h) => {
const {
id,
position: originalPosition,
content: originalContent,
...rest
} = h;
return id === highlightId
? {
id,
position: { ...originalPosition, ...position },
content: content,
image: image,
...rest,
}
: h;
})
);
};
return (
<div className="App" style={{ display: "flex", height: "100vh" }}>
<Sidebar
highlights={highlights}
resetHighlights={() => setHighlights([])}
toggleDocument={toggleDocument}
/>
<div
style={{
height: "100vh",
width: "75vw",
position: "relative",
}}
>
<PdfLoader url={url} beforeLoad={<Spinner />}>
{(pdfDocument) => (
<PdfHighlighter
pdfDocument={pdfDocument}
onScrollChange={resetHash}
scrollRef={(scrollTo) => {
scrollViewerTo = scrollTo;
scrollToHighlightFromHash();
}}
highlightTransform={(
highlight,
index,
setTip,
hideTip,
viewportToScaled,
screenshot,
isScrolledTo
) => {
const isTextHighlight = !Boolean(highlight.image);
const component = isTextHighlight ? (
<Highlight
isScrolledTo={isScrolledTo}
position={highlight.position}
label={highlight.label}
/>
) : (
<AreaHighlight
isScrolledTo={isScrolledTo}
highlight={highlight}
onChange={(boundingRect) => {
updateHighlight(
highlight.id,
{ boundingRect: viewportToScaled(boundingRect) },
highlight?.content,
screenshot(boundingRect)
);
}}
/>
);
return (
<Popup
popupContent={<HighlightPopup label={highlight.label} />}
onMouseOver={(popupContent) =>
setTip(highlight, (highlight) => popupContent)
}
onMouseOut={hideTip}
key={index}
children={component}
/>
);
}}
highlights={highlights}
/>
)}
</PdfLoader>
</div>
</div>
);
};
export default App;