-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.tsx
468 lines (448 loc) · 14.7 KB
/
index.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import 'react-app-polyfill/ie11';
import 'regenerator-runtime/runtime';
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import {
BrowserRouter,
Switch,
Route,
Link,
useParams,
} from 'react-router-dom';
import WebReader, { addTocToManifest } from '../src';
import {
ChakraProvider,
Heading,
UnorderedList,
ListItem,
Box,
Text,
Input,
Button,
Stack,
} from '@chakra-ui/react';
import { getTheme } from '../src/ui/theme';
import readiumBefore from 'url:../src/HtmlReader/ReadiumCss/ReadiumCSS-before.css';
import readiumDefault from 'url:../src/HtmlReader/ReadiumCss/ReadiumCSS-default.css';
import readiumAfter from 'url:../src/HtmlReader/ReadiumCss/ReadiumCSS-after.css';
import Tests from './Tests';
import { Injectable } from '../src/Readium/Injectable';
import useSWR, { Fetcher } from 'swr';
import UseHtmlReader from './use-html-reader';
import mobyEpub2Manifest from './static/samples/moby-epub2-exploded/manifest.json';
import pdfSingleResourceManifest from './static/samples/pdf/single-resource-short.json';
import { WebpubManifest } from '../src/types';
import UsePdfReader from './use-pdf-reader';
const origin = window.location.origin;
const pdfProxyUrl = process.env.CORS_PROXY_URL as string | undefined;
const pdfWorkerSrc = `${origin}/pdf-worker/pdf.worker.min.js`;
const cssInjectables: Injectable[] = [
{
type: 'style',
url: readiumBefore,
},
{
type: 'style',
url: readiumDefault,
},
{
type: 'style',
url: readiumAfter,
},
];
const fontInjectable: Injectable[] = [
{
type: 'style',
url: `${origin}/fonts/opendyslexic/opendyslexic.css`,
fontFamily: 'opendyslexic',
},
];
const scriptInjectable: Injectable[] = [
{
type: 'script',
url: `${origin}/js/sample.js`,
},
];
const htmlInjectablesReflowable = [
...cssInjectables,
...fontInjectable,
...scriptInjectable,
];
const App = () => {
return (
<ChakraProvider theme={getTheme('day')}>
<BrowserRouter>
<Switch>
<Route exact path="/">
<HomePage />
</Route>
<Route path="/pdf">
<PdfReaders />
</Route>
<Route path="/html">
<HtmlReaders />
</Route>
<Route path="*">
<h1>404</h1>
<p>Page not found.</p>
</Route>
</Switch>
</BrowserRouter>
</ChakraProvider>
);
};
const PdfReaders = () => {
return (
<>
<Route path={`/pdf/single-resource-short`}>
<SingleResourcePdf />
</Route>
<Route path={`/pdf/use-pdf-reader-hook`}>
<UsePdfReader
webpubManifestUrl="/samples/pdf/single-resource-short.json"
manifest={pdfSingleResourceManifest as WebpubManifest}
proxyUrl={pdfProxyUrl}
pdfWorkerSrc={`${origin}/pdf-worker/pdf.worker.min.js`}
/>
</Route>
<Route path={`/pdf/large`}>
<WebReader
webpubManifestUrl="/samples/pdf/single-resource-long.json"
proxyUrl={pdfProxyUrl}
pdfWorkerSrc={`${origin}/pdf-worker/pdf.worker.min.js`}
/>
</Route>
<Route path={`/pdf/collection`}>
<WebReader
webpubManifestUrl="/samples/pdf/multi-resource.json"
proxyUrl={pdfProxyUrl}
pdfWorkerSrc={`${origin}/pdf-worker/pdf.worker.min.js`}
/>
</Route>
<Route path={`/pdf/fixed-height-embedded-collection`}>
<Box bg="lavenderblush" p={6} w="100vw">
<Heading>Fixed-height Embedded PDF</Heading>
<Text as="p">
This example shows how a web reader looks embedded within a page
instead of taking over the full page. It is fixed height, which
means it will not grow to fit content in scrolling mode.
</Text>
<WebReader
webpubManifestUrl={`${origin}/samples/pdf/multi-resource.json`}
proxyUrl={pdfProxyUrl}
pdfWorkerSrc={`${origin}/pdf-worker/pdf.worker.min.js`}
growWhenScrolling={false}
/>
<Heading>The page continues...</Heading>
<Text as="p">Here is some more content below the reader</Text>
</Box>
</Route>
<Route path={`/pdf/growing-height-embedded-collection`}>
<Box bg="lavenderblush" p={6} w="100vw">
<Heading>Growing-height Embedded PDF</Heading>
<Text as="p">
This example shows how a web reader looks embedded within a page
instead of taking over the full page. This example lets the PDF grow
to fit content of the resource in scrolling mode. In paginated mode,
however, it will use the value of the `height` prop.
</Text>
<WebReader
webpubManifestUrl={`${origin}/samples/pdf/multi-resource.json`}
proxyUrl={pdfProxyUrl}
pdfWorkerSrc={`${origin}/pdf-worker/pdf.worker.min.js`}
/>
<Heading>The page continues...</Heading>
<Text as="p">Here is some more content below the reader</Text>
</Box>
</Route>
</>
);
};
/**
* This is a function we will use to get the resource through a given proxy url.
* It will eventually be passed to the web reader instead of passing a proxy url directly.
*/
const getProxiedResource = (proxyUrl?: string) => async (href: string) => {
// Generate the resource URL using the proxy
const url: string = proxyUrl
? `${proxyUrl}${encodeURIComponent(href)}`
: href;
const response = await fetch(url, { mode: 'cors' });
const array = new Uint8Array(await response.arrayBuffer());
if (!response.ok) {
throw new Error('Response not Ok for URL: ' + url);
}
return array;
};
/**
* - Fetches manifest
* - Adds the TOC to the manifest
* - Generates a syncthetic url for the manifest to be passed to
* web reader.
* - Returns the synthetic url
*/
const fetchAndModifyManifest: Fetcher<string, string> = async (url) => {
const response = await fetch(url);
const manifest = await response.json();
const modifiedManifest = await addTocToManifest(
manifest,
getProxiedResource(pdfProxyUrl),
pdfWorkerSrc
);
const syntheticUrl = URL.createObjectURL(
new Blob([JSON.stringify(modifiedManifest)])
);
return syntheticUrl;
};
const SingleResourcePdf = () => {
const { data: modifiedManifestUrl, isLoading } = useSWR<string>(
'/samples/pdf/single-resource-short.json',
fetchAndModifyManifest,
{
revalidateOnFocus: false,
}
);
if (isLoading || !modifiedManifestUrl) return <div>Loading...</div>;
return (
<WebReader
webpubManifestUrl={modifiedManifestUrl}
proxyUrl={pdfProxyUrl}
pdfWorkerSrc={`${origin}/pdf-worker/pdf.worker.min.js`}
/>
);
};
const HtmlReaders = () => {
return (
<Switch>
<Route path={`/html/moby-epub2`}>
<WebReader
injectablesReflowable={htmlInjectablesReflowable}
webpubManifestUrl={`${origin}/samples/moby-epub2-exploded/manifest.json`}
/>
</Route>
<Route path={`/html/moby-epub3`}>
<WebReader
injectablesReflowable={htmlInjectablesReflowable}
webpubManifestUrl={`${origin}/samples/moby-epub3-exploded/manifest.json`}
/>
</Route>
<Route path={`/html/moby-epub3-no-local-storage`}>
<WebReader
injectablesReflowable={htmlInjectablesReflowable}
persistLastLocation={false}
persistSettings={false}
webpubManifestUrl={`${origin}/samples/moby-epub3-exploded/manifest.json`}
/>
</Route>
<Route path={`/html/fixed-layout`}>
<WebReader
injectablesReflowable={htmlInjectablesReflowable}
webpubManifestUrl={`${origin}/samples/fixed-layout/manifest.json`}
/>
</Route>
<Route path={`/html/fxl-poems`}>
<WebReader
injectablesReflowable={htmlInjectablesReflowable}
webpubManifestUrl={`${origin}/samples/fxl-poems/manifest.json`}
/>
</Route>
<Route path={`/html/fixed-height-embedded-moby-epub2`}>
<Box bg="lavenderblush" p={6} w="100vw">
<Heading>Fixed-height Embedded Example</Heading>
<Text as="p">
This example shows how a web reader looks embedded within a page
instead of taking over the full page. It is fixed height, which
means it will not grow to fit content in scrolling mode.
</Text>
<WebReader
injectablesReflowable={htmlInjectablesReflowable}
webpubManifestUrl={`${origin}/samples/moby-epub2-exploded/manifest.json`}
growWhenScrolling={false}
height="70vh"
/>
<Heading>The page continues...</Heading>
<Text as="p">Here is some more content below the reader</Text>
</Box>
</Route>
<Route path={`/html/growing-embedded-moby-epub2`}>
<Box bg="lavenderblush" p={6} w="100vw">
<Heading>Growing-height Embedded Example</Heading>
<Text as="p">
This example shows how a web reader looks embedded within a page and
with a growing-height policy. This example lets the PDF grow to fit
content of the resource in scrolling mode. In paginated mode,
however, it will use the value of the `height` prop.
</Text>
<WebReader
injectablesReflowable={htmlInjectablesReflowable}
webpubManifestUrl={`${origin}/samples/moby-epub2-exploded/manifest.json`}
/>
<Heading>The page continues...</Heading>
<Text as="p">Here is some more content below the reader</Text>
</Box>
</Route>
<Route path={`/html/streamed-alice-epub`}>
<WebReader
injectablesReflowable={htmlInjectablesReflowable}
webpubManifestUrl="https://alice.dita.digital/manifest.json"
/>
</Route>
<Route path={`/html/readium-css-docs`}>
<WebReader
injectablesReflowable={htmlInjectablesReflowable}
webpubManifestUrl={`${origin}/samples/ReadiumCSS-docs/manifest.json`}
/>
</Route>
<Route path={`/html/use-html-reader-hook`}>
<UseHtmlReader
injectablesReflowable={htmlInjectablesReflowable}
webpubManifestUrl={`${origin}/samples/moby-epub2-exploded/manifest.json`}
manifest={mobyEpub2Manifest as WebpubManifest}
/>
</Route>
<Route path={`/html/url/:manifestUrl`}>
<DynamicReader />
</Route>
<Route path={`/html/test`}>
<Tests />
</Route>
</Switch>
);
};
const HomePage = () => {
const [dynamicHref, setValue] = React.useState('');
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) =>
setValue(event.target.value);
return (
<Box m={2}>
<Heading as="h1">NYPL Web Reader</Heading>
<Heading as="h2" fontSize={2} mt={3}>
Generic Examples
</Heading>
<UnorderedList p={4}>
<ListItem>
EPUB2 Based Webpubs
<UnorderedList>
<ListItem>
<Link to="/html/moby-epub2">Moby Dick </Link>
</ListItem>
<ListItem>
<Link to="/html/use-html-reader-hook">useHtmlReader hook</Link>
</ListItem>
</UnorderedList>
</ListItem>
<ListItem>
EPUB3 Based Webpubs
<UnorderedList>
<ListItem>
<Link to="/html/moby-epub3">Moby Dick (EPUB 3)</Link>
</ListItem>
<ListItem>
<Link to="/html/moby-epub3-no-local-storage">
Moby Dick (EPUB 3)
</Link>{' '}
- does not persist location or settings to local storage
</ListItem>
<ListItem>
<Link to="/html/readium-css-docs">
Readium CSS Documentation (as Webpub)
</Link>
</ListItem>
<ListItem>
<Link to="/html/fixed-layout">Fixed Layout (Illustrated)</Link>
</ListItem>
<ListItem>
<Link to="/html/fxl-poems">Fixed Layout (Poems)</Link>
</ListItem>
</UnorderedList>
</ListItem>
<ListItem>
Remote hosted WebPubs
<UnorderedList>
<ListItem>
<Link to="/html/streamed-alice-epub">
Alice's Adventures in Wonderland
</Link>
<Text as="i">
(streamed from https://alice.dita.digital)
</Text>
</ListItem>
</UnorderedList>
</ListItem>
<ListItem>
Embedded Reader
<UnorderedList>
<ListItem>
<Link to="/html/fixed-height-embedded-moby-epub2">
Fixed-height Embedded Moby Dick
</Link>
</ListItem>
<ListItem>
<Link to="/html/growing-embedded-moby-epub2">
Growing-height Embedded Moby Dick
</Link>
</ListItem>
</UnorderedList>
</ListItem>
<ListItem>
PDFs
<UnorderedList>
<ListItem>
<Link to="/pdf/single-resource-short">Single-PDF Webpub</Link>
</ListItem>
<ListItem>
<Link to="/pdf/use-pdf-reader-hook">usePdfReader hook</Link>
</ListItem>
<ListItem>
<Link to="/pdf/large">Single-PDF Webpub (large file)</Link>
</ListItem>
<ListItem>
<Link to="/pdf/collection">Multi-PDF Webpub</Link>
</ListItem>
<ListItem>
<Link to="/pdf/fixed-height-embedded-collection">
Fixed-height embedded PDF
</Link>
</ListItem>
<ListItem>
<Link to="/pdf/growing-height-embedded-collection">
Growing-height embedded PDF
</Link>
</ListItem>
</UnorderedList>
</ListItem>
<ListItem>
Bring your own manifest:
<Stack direction="row" alignItems="center">
<Input
maxW={500}
value={dynamicHref}
onChange={handleChange}
placeholder="Webpub Manifest URL"
mr={2}
/>
<Button
as={Link}
to={`/html/url/${encodeURIComponent(dynamicHref)}`}
>
Go
</Button>
</Stack>
</ListItem>
</UnorderedList>
</Box>
);
};
const DynamicReader: React.FC = () => {
const { manifestUrl } = useParams<{ manifestUrl: string }>();
const decoded = decodeURIComponent(manifestUrl);
return (
<WebReader
injectablesReflowable={htmlInjectablesReflowable}
webpubManifestUrl={decoded}
/>
);
};
const container = document.getElementById('root') as HTMLElement;
const root = createRoot(container);
root.render(<App />);