-
Notifications
You must be signed in to change notification settings - Fork 432
/
App.tsx
217 lines (195 loc) · 6 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import React, { useState, useEffect, useCallback, useRef } from "react";
import {
AreaHighlight,
Highlight,
PdfHighlighter,
PdfLoader,
Popup,
Tip,
} from "./react-pdf-highlighter";
import type {
Content,
IHighlight,
NewHighlight,
ScaledPosition,
} from "./react-pdf-highlighter";
import { Sidebar } from "./Sidebar";
import { Spinner } from "./Spinner";
import { testHighlights as _testHighlights } from "./test-highlights";
import "./style/App.css";
import "../../dist/style.css";
const testHighlights: Record<string, Array<IHighlight>> = _testHighlights;
const getNextId = () => String(Math.random()).slice(2);
const parseIdFromHash = () =>
document.location.hash.slice("#highlight-".length);
const resetHash = () => {
document.location.hash = "";
};
const HighlightPopup = ({
comment,
}: {
comment: { text: string; emoji: string };
}) =>
comment.text ? (
<div className="Highlight__popup">
{comment.emoji} {comment.text}
</div>
) : null;
const PRIMARY_PDF_URL = "https://arxiv.org/pdf/1708.08021";
const SECONDARY_PDF_URL = "https://arxiv.org/pdf/1604.02480";
export function App() {
const searchParams = new URLSearchParams(document.location.search);
const initialUrl = searchParams.get("url") || PRIMARY_PDF_URL;
const [url, setUrl] = useState(initialUrl);
const [highlights, setHighlights] = useState<Array<IHighlight>>(
testHighlights[initialUrl] ? [...testHighlights[initialUrl]] : [],
);
const resetHighlights = () => {
setHighlights([]);
};
const toggleDocument = () => {
const newUrl =
url === PRIMARY_PDF_URL ? SECONDARY_PDF_URL : PRIMARY_PDF_URL;
setUrl(newUrl);
setHighlights(testHighlights[newUrl] ? [...testHighlights[newUrl]] : []);
};
const scrollViewerTo = useRef((highlight: IHighlight) => {});
const scrollToHighlightFromHash = useCallback(() => {
const highlight = getHighlightById(parseIdFromHash());
if (highlight) {
scrollViewerTo.current(highlight);
}
}, []);
useEffect(() => {
window.addEventListener("hashchange", scrollToHighlightFromHash, false);
return () => {
window.removeEventListener(
"hashchange",
scrollToHighlightFromHash,
false,
);
};
}, [scrollToHighlightFromHash]);
const getHighlightById = (id: string) => {
return highlights.find((highlight) => highlight.id === id);
};
const addHighlight = (highlight: NewHighlight) => {
console.log("Saving highlight", highlight);
setHighlights((prevHighlights) => [
{ ...highlight, id: getNextId() },
...prevHighlights,
]);
};
const updateHighlight = (
highlightId: string,
position: Partial<ScaledPosition>,
content: Partial<Content>,
) => {
console.log("Updating highlight", highlightId, position, content);
setHighlights((prevHighlights) =>
prevHighlights.map((h) => {
const {
id,
position: originalPosition,
content: originalContent,
...rest
} = h;
return id === highlightId
? {
id,
position: { ...originalPosition, ...position },
content: { ...originalContent, ...content },
...rest,
}
: h;
}),
);
};
return (
<div className="App" style={{ display: "flex", height: "100vh" }}>
<Sidebar
highlights={highlights}
resetHighlights={resetHighlights}
toggleDocument={toggleDocument}
/>
<div
style={{
height: "100vh",
width: "75vw",
position: "relative",
}}
>
<PdfLoader url={url} beforeLoad={<Spinner />}>
{(pdfDocument) => (
<PdfHighlighter
pdfDocument={pdfDocument}
enableAreaSelection={(event) => event.altKey}
onScrollChange={resetHash}
scrollRef={(scrollTo) => {
scrollViewerTo.current = scrollTo;
scrollToHighlightFromHash();
}}
onSelectionFinished={(
position,
content,
hideTipAndSelection,
transformSelection,
) => (
<Tip
onOpen={transformSelection}
onConfirm={(comment) => {
addHighlight({ content, position, comment });
hideTipAndSelection();
}}
/>
)}
highlightTransform={(
highlight,
index,
setTip,
hideTip,
viewportToScaled,
screenshot,
isScrolledTo,
) => {
const isTextHighlight = !highlight.content?.image;
const component = isTextHighlight ? (
<Highlight
isScrolledTo={isScrolledTo}
position={highlight.position}
comment={highlight.comment}
/>
) : (
<AreaHighlight
isScrolledTo={isScrolledTo}
highlight={highlight}
onChange={(boundingRect) => {
updateHighlight(
highlight.id,
{ boundingRect: viewportToScaled(boundingRect) },
{ image: screenshot(boundingRect) },
);
}}
/>
);
return (
<Popup
popupContent={<HighlightPopup {...highlight} />}
onMouseOver={(popupContent) =>
setTip(highlight, (highlight) => popupContent)
}
onMouseOut={hideTip}
key={index}
>
{component}
</Popup>
);
}}
highlights={highlights}
/>
)}
</PdfLoader>
</div>
</div>
);
}