-
-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathReader.tsx
More file actions
457 lines (402 loc) · 11.7 KB
/
Reader.tsx
File metadata and controls
457 lines (402 loc) · 11.7 KB
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
import { useEventListener } from '@literal-ui/hooks'
import clsx from 'clsx'
import React, {
ComponentProps,
useCallback,
useEffect,
useRef,
useState,
} from 'react'
import { MdChevronRight, MdWebAsset } from 'react-icons/md'
import { RiBookLine } from 'react-icons/ri'
import { PhotoSlider } from 'react-photo-view'
import { useSetRecoilState } from 'recoil'
import useTilg from 'tilg'
import { useSnapshot } from 'valtio'
import { RenditionSpread } from '@flow/epubjs/types/rendition'
import { navbarState } from '@flow/reader/state'
import { db } from '../db'
import { handleFiles } from '../file'
import {
useBackground,
useColorScheme,
useDisablePinchZooming,
useMobile,
useSync,
useTranslation,
useTypography,
useTouchEvent,
} from '../hooks'
import { BookTab, reader, useReaderSnapshot } from '../models'
import { isTouchScreen } from '../platform'
import { updateCustomStyle } from '../styles'
import {
getClickedAnnotation,
setClickedAnnotation,
Annotations,
} from './Annotation'
import { Tab } from './Tab'
import { TextSelectionMenu } from './TextSelectionMenu'
import { DropZone, SplitView, useDndContext, useSplitViewItem } from './base'
import * as pages from './pages'
function handleKeyDown(tab?: BookTab) {
return (e: KeyboardEvent) => {
try {
switch (e.code) {
case 'ArrowLeft':
case 'ArrowUp':
tab?.prev()
break
case 'ArrowRight':
case 'ArrowDown':
tab?.next()
break
case 'Space':
e.shiftKey ? tab?.prev() : tab?.next()
}
} catch (error) {
// ignore `rendition is undefined` error
}
}
}
export function ReaderGridView() {
const { groups } = useReaderSnapshot()
useEventListener('keydown', handleKeyDown(reader.focusedBookTab))
if (!groups.length) return null
return (
<SplitView className={clsx('ReaderGridView')}>
{groups.map(({ id }, i) => (
<ReaderGroup key={id} index={i} />
))}
</SplitView>
)
}
interface ReaderGroupProps {
index: number
}
function ReaderGroup({ index }: ReaderGroupProps) {
const group = reader.groups[index]!
const { focusedIndex } = useReaderSnapshot()
const { tabs, selectedIndex } = useSnapshot(group)
const t = useTranslation()
const { size } = useSplitViewItem(`${ReaderGroup.name}.${index}`, {
// to disable sash resize
visible: false,
})
const handleMouseDown = useCallback(() => {
reader.selectGroup(index)
}, [index])
return (
<div
className="ReaderGroup flex flex-1 flex-col overflow-hidden focus:outline-none"
onMouseDown={handleMouseDown}
style={{ width: size }}
>
<Tab.List
className="hidden sm:flex"
onDelete={() => reader.removeGroup(index)}
>
{tabs.map((tab, i) => {
const selected = i === selectedIndex
const focused = index === focusedIndex && selected
return (
<Tab
key={tab.id}
selected={selected}
focused={focused}
onClick={() => group.selectTab(i)}
onDelete={() => reader.removeTab(i, index)}
Icon={tab instanceof BookTab ? RiBookLine : MdWebAsset}
draggable
onDragStart={(e) => {
e.dataTransfer.setData('text/plain', `${index},${i}`)
}}
>
{tab.isBook ? tab.title : t(`${tab.title}.title`)}
</Tab>
)
})}
</Tab.List>
<DropZone
className={clsx('flex-1', isTouchScreen || 'h-0')}
split
onDrop={async (e, position) => {
// read `e.dataTransfer` first to avoid get empty value after `await`
const files = e.dataTransfer.files
let tabs = []
if (files.length) {
tabs = await handleFiles(files)
} else {
const text = e.dataTransfer.getData('text/plain')
const fromTab = text.includes(',')
if (fromTab) {
const indexes = text.split(',')
const groupIdx = Number(indexes[0])
if (index === groupIdx) {
if (group.tabs.length === 1) return
if (position === 'universe') return
}
const tabIdx = Number(indexes[1])
const tab = reader.removeTab(tabIdx, groupIdx)
if (tab) tabs.push(tab)
} else {
const id = text
const tabParam =
Object.values(pages).find((p) => p.displayName === id) ??
(await db?.books.get(id))
if (tabParam) tabs.push(tabParam)
}
}
if (tabs.length) {
switch (position) {
case 'left':
reader.addGroup(tabs, index)
break
case 'right':
reader.addGroup(tabs, index + 1)
break
default:
tabs.forEach((t) => reader.addTab(t, index))
}
}
}}
>
{group.tabs.map((tab, i) => (
<PaneContainer active={i === selectedIndex} key={tab.id}>
{tab instanceof BookTab ? (
<BookPane tab={tab} onMouseDown={handleMouseDown} />
) : (
<tab.Component />
)}
</PaneContainer>
))}
</DropZone>
</div>
)
}
interface PaneContainerProps {
active: boolean
}
const PaneContainer: React.FC<PaneContainerProps> = ({ active, children }) => {
return <div className={clsx('h-full', active || 'hidden')}>{children}</div>
}
interface BookPaneProps {
tab: BookTab
onMouseDown: () => void
}
function BookPane({ tab, onMouseDown }: BookPaneProps) {
const ref = useRef<HTMLDivElement>(null)
const prevSize = useRef(0)
const typography = useTypography(tab)
const { dark } = useColorScheme()
const [background] = useBackground()
const { iframe, rendition, rendered, container } = useSnapshot(tab)
useTilg()
useEffect(() => {
const el = ref.current
if (!el) return
const observer = new ResizeObserver(([e]) => {
const size = e?.contentRect.width ?? 0
// `display: hidden` will lead `rect` to 0
if (size !== 0 && prevSize.current !== 0) {
reader.resize()
}
prevSize.current = size
})
observer.observe(el)
return () => {
observer.disconnect()
}
}, [])
useSync(tab)
const setNavbar = useSetRecoilState(navbarState)
const mobile = useMobile()
const applyCustomStyle = useCallback(() => {
const contents = rendition?.getContents()[0]
updateCustomStyle(contents, typography)
}, [rendition, typography])
useEffect(() => {
tab.onRender = applyCustomStyle
}, [applyCustomStyle, tab])
useEffect(() => {
if (ref.current) tab.render(ref.current)
}, [tab])
useEffect(() => {
/**
* when `spread` changes, we should call `spread()` to re-layout,
* then call {@link updateCustomStyle} to update custom style
* according to the latest layout
*/
rendition?.spread(typography.spread ?? RenditionSpread.Auto)
}, [typography.spread, rendition])
useEffect(() => applyCustomStyle(), [applyCustomStyle])
useEffect(() => {
if (dark === undefined) return
// set `!important` when in dark mode
rendition?.themes.override('color', dark ? '#bfc8ca' : '#3f484a', dark)
}, [rendition, dark])
const [src, setSrc] = useState<string>()
useEffect(() => {
if (src) {
if (document.activeElement instanceof HTMLElement)
document.activeElement?.blur()
}
}, [src])
const { setDragEvent } = useDndContext()
// `dragenter` not fired in iframe when the count of times is even, so use `dragover`
useEventListener(iframe, 'dragover', (e: any) => {
console.log('drag enter in iframe')
setDragEvent(e)
})
useEventListener(iframe, 'mousedown', onMouseDown)
useEventListener(iframe, 'click', (e) => {
// https://developer.chrome.com/blog/tap-to-search
e.preventDefault()
for (const el of e.composedPath() as any) {
// `instanceof` may not work in iframe
if (el.tagName === 'A' && el.href) {
tab.showPrevLocation()
return
}
if (mobile === false && el.tagName === 'IMG') {
setSrc(el.src)
return
}
}
if (isTouchScreen && container) {
if (getClickedAnnotation()) {
setClickedAnnotation(false)
return
}
const w = container.clientWidth
const x = e.clientX % w
const threshold = 0.3
const side = w * threshold
if (x < side) {
tab.prev()
} else if (w - x < side) {
tab.next()
} else if (mobile) {
setNavbar((a) => !a)
}
}
})
useEventListener(iframe, 'wheel', (e) => {
if (e.deltaY < 0) {
tab.prev()
} else {
tab.next()
}
})
useEventListener(iframe, 'keydown', handleKeyDown(tab))
useTouchEvent({iframe, tab});
useDisablePinchZooming(iframe)
return (
<div className={clsx('flex h-full flex-col', mobile && 'py-[3vw]')}>
<PhotoSlider
images={[{ src, key: 0 }]}
visible={!!src}
onClose={() => setSrc(undefined)}
maskOpacity={0.6}
bannerVisible={false}
/>
<ReaderPaneHeader tab={tab} />
<div
ref={ref}
className={clsx('relative flex-1', isTouchScreen || 'h-0')}
// `color-scheme: dark` will make iframe background white
style={{ colorScheme: 'auto' }}
>
<div
className={clsx(
'absolute inset-0',
// do not cover `sash`
'z-20',
rendered && 'hidden',
background,
)}
/>
<TextSelectionMenu tab={tab} />
<Annotations tab={tab} />
</div>
<ReaderPaneFooter tab={tab} />
</div>
)
}
interface ReaderPaneHeaderProps {
tab: BookTab
}
const ReaderPaneHeader: React.FC<ReaderPaneHeaderProps> = ({ tab }) => {
const { location } = useSnapshot(tab)
const navPath = tab.getNavPath()
useEffect(() => {
navPath.forEach((i) => (i.expanded = true))
}, [navPath])
return (
<Bar>
<div className="scroll-h flex">
{navPath.map((item, i) => (
<button
key={i}
className="hover:text-on-surface flex shrink-0 items-center"
>
{item.label}
{i !== navPath.length - 1 && <MdChevronRight size={20} />}
</button>
))}
</div>
{location && (
<div className="shrink-0">
{location.start.displayed.page} / {location.start.displayed.total}
</div>
)}
</Bar>
)
}
interface FooterProps {
tab: BookTab
}
const ReaderPaneFooter: React.FC<FooterProps> = ({ tab }) => {
const { locationToReturn, location, book } = useSnapshot(tab)
return (
<Bar>
{locationToReturn ? (
<>
<button
className={clsx(locationToReturn || 'invisible')}
onClick={() => {
tab.hidePrevLocation()
tab.display(locationToReturn.end.cfi, false)
}}
>
Return to {locationToReturn.end.cfi}
</button>
<button
onClick={() => {
tab.hidePrevLocation()
}}
>
Stay
</button>
</>
) : (
<>
<div>{location?.start.href}</div>
<div>{((book.percentage ?? 0) * 100).toFixed()}%</div>
</>
)}
</Bar>
)
}
interface LineProps extends ComponentProps<'div'> {}
const Bar: React.FC<LineProps> = ({ className, ...props }) => {
return (
<div
className={clsx(
'typescale-body-small text-outline flex h-6 items-center justify-between gap-2 px-[4vw] sm:px-2',
className,
)}
{...props}
></div>
)
}