aksos-react-native-epub-reader 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +60 -0
- package/README.md +158 -13
- package/lib/EpubReader.d.ts +47 -9
- package/lib/EpubReader.js +69 -16
- package/lib/EpubReaderView.d.ts +27 -2
- package/lib/EpubReaderView.js +38 -32
- package/lib/cfiOrder.d.ts +2 -0
- package/lib/cfiOrder.js +34 -0
- package/lib/engine/html.js +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/themes.d.ts +24 -6
- package/lib/themes.js +80 -20
- package/lib/types.d.ts +66 -1
- package/lib/ui/AppearanceSheet.d.ts +4 -2
- package/lib/ui/AppearanceSheet.js +20 -13
- package/lib/ui/ContentsSheet.d.ts +2 -0
- package/lib/ui/ContentsSheet.js +21 -13
- package/lib/ui/CustomizeSheet.d.ts +4 -2
- package/lib/ui/CustomizeSheet.js +20 -18
- package/lib/ui/HighlightMenu.d.ts +4 -2
- package/lib/ui/HighlightMenu.js +3 -3
- package/lib/ui/NoteEditor.js +6 -5
- package/lib/ui/SearchSheet.js +6 -5
- package/lib/ui/Sheet.js +5 -4
- package/lib/ui/Text.d.ts +9 -0
- package/lib/ui/Text.js +62 -0
- package/package.json +81 -76
- package/src/EpubReader.tsx +121 -25
- package/src/EpubReaderView.tsx +61 -17
- package/src/cfiOrder.ts +28 -0
- package/src/engine/epubcfi.js +527 -0
- package/src/engine/html.ts +1 -1
- package/src/engine/reader.js +830 -65
- package/src/index.ts +1 -1
- package/src/themes.ts +97 -21
- package/src/types.ts +71 -1
- package/src/ui/AppearanceSheet.tsx +19 -10
- package/src/ui/ContentsSheet.tsx +14 -4
- package/src/ui/CustomizeSheet.tsx +14 -10
- package/src/ui/HighlightMenu.tsx +6 -4
- package/src/ui/NoteEditor.tsx +2 -1
- package/src/ui/SearchSheet.tsx +2 -1
- package/src/ui/Sheet.tsx +4 -3
- package/src/ui/Text.tsx +27 -0
package/src/EpubReader.tsx
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
|
2
|
-
import { StyleSheet, View } from 'react-native';
|
|
2
|
+
import { AppState, StyleSheet, View } from 'react-native';
|
|
3
3
|
import { WebView } from 'react-native-webview';
|
|
4
4
|
import type { WebViewMessageEvent } from 'react-native-webview';
|
|
5
5
|
import { READER_HTML } from './engine/html';
|
|
6
6
|
import { themeColors, toEngineSettings } from './themes';
|
|
7
7
|
import type {
|
|
8
|
+
BookColors,
|
|
8
9
|
BookLocation,
|
|
9
10
|
BookMetadata,
|
|
11
|
+
CfiHighlight,
|
|
12
|
+
CustomFont,
|
|
10
13
|
EpubSource,
|
|
11
14
|
Highlight,
|
|
12
15
|
ReaderSettings,
|
|
16
|
+
ReaderTheme,
|
|
13
17
|
RelocateEvent,
|
|
14
18
|
SafeInsets,
|
|
15
19
|
SearchResult,
|
|
@@ -29,18 +33,45 @@ export interface EpubReaderProps {
|
|
|
29
33
|
systemDark?: boolean;
|
|
30
34
|
/** Space reserved for status bar / home indicator (and the notch in landscape). The engine adds room for the running header and footer. */
|
|
31
35
|
safeInsets?: SafeInsets;
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
/** Running header (chapter) and footer (page) on the page. false: none, and no space is kept for them. Default true. */
|
|
37
|
+
runningLabels?: boolean;
|
|
38
|
+
/** where to open: an offset, a share of the book, or an epub.js CFI ({ cfi: 'epubcfi(...)' }) */
|
|
39
|
+
initialLocation?: BookLocation | { progress: number } | { cfi: string };
|
|
40
|
+
/** highlights as offsets, or as epub.js CFIs only (CfiHighlight). A `cfi` always wins over offsets. */
|
|
41
|
+
highlights?: (Highlight | CfiHighlight)[];
|
|
42
|
+
/**
|
|
43
|
+
* Characters per epub.js location ("chunk"), as in epub.js book.locations.generate(n). Default 150 (the epub.js
|
|
44
|
+
* default). Keep it the same for a book forever if you store or pay by location numbers. 0 turns locations off.
|
|
45
|
+
*/
|
|
46
|
+
locationChars?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Show runs of 3+ zero-width characters (U+200B/C/D, U+FEFF), such as a hidden watermark, as U+2060 WORD JOINER,
|
|
49
|
+
* one for one, so they do not break words. Nothing is deleted, so offsets and CFIs do not change. Default true.
|
|
50
|
+
*/
|
|
51
|
+
repairZeroWidth?: boolean;
|
|
52
|
+
/** Highlight colours by name, e.g. { yellow: '#ffff00' }. A Highlight's `color` may also be a CSS colour itself. */
|
|
53
|
+
highlightColors?: Record<string, string>;
|
|
54
|
+
/** Fill opacity for highlightColors (default 0.3, like epub.js). */
|
|
55
|
+
highlightOpacity?: number;
|
|
56
|
+
/** Fonts loaded into every chapter with @font-face. A font with an `id` can be chosen with settings.fontId. */
|
|
57
|
+
fonts?: CustomFont[];
|
|
58
|
+
/** 'theme' (default): all text in the theme colour. 'keep': the book's own colours stay, made readable. */
|
|
59
|
+
bookColors?: BookColors;
|
|
60
|
+
/** Largest pinch zoom (default 3). 1 turns pinch zoom off. Paged modes only. */
|
|
61
|
+
maxZoom?: number;
|
|
62
|
+
/** Page themes to use instead of the built-in ones. settings.themeId picks one; an unknown id gets the first. */
|
|
63
|
+
themes?: ReaderTheme[];
|
|
34
64
|
/**
|
|
35
65
|
* Largest EPUB (bytes) that will be loaded. The whole book is held in memory while it is opened, so very large
|
|
36
66
|
* files can crash low-memory phones. Default 150 MB, 0 disables the check.
|
|
37
67
|
*/
|
|
38
68
|
maxFileSize?: number;
|
|
39
|
-
/** Items added to the native text-selection menu. `onMenuAction` fires with the item key. */
|
|
40
|
-
menuItems?: { label: string; key: string }[];
|
|
41
|
-
onMenuAction?: (key: string, selection: SelectionEvent | null) => void;
|
|
42
69
|
onReady?: (info: { metadata: BookMetadata; rtl: boolean }) => void;
|
|
43
|
-
|
|
70
|
+
/**
|
|
71
|
+
* items: the entries whose file is in the book. all: every entry, in the same order, with spine -1 for an entry
|
|
72
|
+
* whose file is missing (for example the chapters a preview leaves out). tocIndex points into items.
|
|
73
|
+
*/
|
|
74
|
+
onToc?: (items: TocItem[], all: TocItem[]) => void;
|
|
44
75
|
onCover?: (dataUri: string) => void;
|
|
45
76
|
onRelocate?: (e: RelocateEvent) => void;
|
|
46
77
|
/** fired once the whole book has been indexed: cumulative character offset of every spine item */
|
|
@@ -57,6 +88,10 @@ export interface EpubReaderProps {
|
|
|
57
88
|
/** the reading direction changed (the user overrode it in the settings) */
|
|
58
89
|
onDirectionChange?: (rtl: boolean) => void;
|
|
59
90
|
onSearchResults?: (searchId: number, results: SearchResult[], done: boolean) => void;
|
|
91
|
+
/** epub.js locations are counted (after the book is indexed). From now on onRelocate carries `locations`. */
|
|
92
|
+
onLocations?: (info: { count: number }) => void;
|
|
93
|
+
/** the page was zoomed with two fingers (scale 1 = back to normal) */
|
|
94
|
+
onZoom?: (scale: number) => void;
|
|
60
95
|
onEndReached?: (edge: 'start' | 'end') => void;
|
|
61
96
|
onError?: (message: string, info: ReaderErrorInfo) => void;
|
|
62
97
|
}
|
|
@@ -64,7 +99,13 @@ export interface EpubReaderProps {
|
|
|
64
99
|
export interface EpubReaderHandle {
|
|
65
100
|
next(): void;
|
|
66
101
|
prev(): void;
|
|
67
|
-
goTo(
|
|
102
|
+
goTo(
|
|
103
|
+
location:
|
|
104
|
+
| { spine: number; offset?: number; fragment?: string }
|
|
105
|
+
| { progress: number }
|
|
106
|
+
/** spine / offset: where to go when the CFI is not found in this book */
|
|
107
|
+
| { cfi: string; spine?: number; offset?: number }
|
|
108
|
+
): void;
|
|
68
109
|
search(query: string, searchId: number): void;
|
|
69
110
|
cancelSearch(searchId?: number): void;
|
|
70
111
|
/** Jump to a search result and flash the match. */
|
|
@@ -78,6 +119,21 @@ const DEFAULT_MAX_FILE = 150 * 1024 * 1024;
|
|
|
78
119
|
const ENGINE_START_TIMEOUT = 20000;
|
|
79
120
|
const CHUNK = 256 * 1024;
|
|
80
121
|
|
|
122
|
+
function stripDataPrefix(b64: string): string {
|
|
123
|
+
return b64.startsWith('data:') ? b64.slice(b64.indexOf(',') + 1) : b64;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Font data as plain base64, or '' when it is not base64. Only these characters may go into the injected script.
|
|
127
|
+
function cleanFontBase64(b64: string): string {
|
|
128
|
+
const data = stripDataPrefix(b64 || '').replace(/\s+/g, '').replace(/-/g, '+').replace(/_/g, '/');
|
|
129
|
+
return /^[A-Za-z0-9+/]+=*$/.test(data) ? data : '';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// identity of a font list: changes when a font is added, removed or replaced
|
|
133
|
+
function fontsKeyOf(fonts?: CustomFont[]): string {
|
|
134
|
+
return (fonts || []).map((f) => [f.id, f.family, f.weight, f.style, f.mime, sourceKeyOf({ base64: f.base64 })].join('|')).join(';');
|
|
135
|
+
}
|
|
136
|
+
|
|
81
137
|
/** Identity of a source: the explicit `key`, the uri, or a fingerprint sampled across the base64 data. */
|
|
82
138
|
export function sourceKeyOf(source: EpubSource): string {
|
|
83
139
|
if (source.key) return source.key;
|
|
@@ -131,7 +187,7 @@ function toScript(cmd: object): string {
|
|
|
131
187
|
}
|
|
132
188
|
|
|
133
189
|
export const EpubReader = forwardRef<EpubReaderHandle, EpubReaderProps>(function EpubReader(props, ref) {
|
|
134
|
-
const { source, settings, systemDark = false, safeInsets = { top: 0, bottom: 0 }, highlights,
|
|
190
|
+
const { source, settings, systemDark = false, safeInsets = { top: 0, bottom: 0 }, highlights, fonts } = props;
|
|
135
191
|
const web = useRef<WebView>(null);
|
|
136
192
|
const cb = useRef(props);
|
|
137
193
|
cb.current = props;
|
|
@@ -139,15 +195,24 @@ export const EpubReader = forwardRef<EpubReaderHandle, EpubReaderProps>(function
|
|
|
139
195
|
// bumping this remounts the WebView (after the system killed its process)
|
|
140
196
|
const [webKey, setWebKey] = useState(0);
|
|
141
197
|
const opened = useRef(false);
|
|
142
|
-
const selection = useRef<SelectionEvent | null>(null);
|
|
143
198
|
const metaRef = useRef<BookMetadata>({ title: '', author: '', language: '', publisher: '' });
|
|
144
199
|
const lastLoc = useRef<BookLocation | null>(null);
|
|
145
200
|
const lastKey = useRef('');
|
|
201
|
+
const fontGen = useRef(0);
|
|
146
202
|
|
|
203
|
+
const fontsKey = fontsKeyOf(fonts);
|
|
204
|
+
const hlColorsJson = JSON.stringify(props.highlightColors || null);
|
|
205
|
+
const themesJson = JSON.stringify(props.themes || null);
|
|
147
206
|
const engineSettings = useMemo(
|
|
148
|
-
() =>
|
|
207
|
+
() => ({
|
|
208
|
+
...toEngineSettings(settings, systemDark, safeInsets, fonts, props.themes, props.runningLabels !== false),
|
|
209
|
+
highlightColors: props.highlightColors || {},
|
|
210
|
+
highlightOpacity: props.highlightOpacity ?? 0.3,
|
|
211
|
+
bookColors: props.bookColors || 'theme',
|
|
212
|
+
maxZoom: props.maxZoom ?? 3
|
|
213
|
+
}),
|
|
149
214
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
150
|
-
[settings, systemDark, safeInsets.top, safeInsets.bottom, safeInsets.left, safeInsets.right]
|
|
215
|
+
[settings, systemDark, safeInsets.top, safeInsets.bottom, safeInsets.left, safeInsets.right, fontsKey, hlColorsJson, themesJson, props.highlightOpacity, props.bookColors, props.maxZoom, props.runningLabels]
|
|
151
216
|
);
|
|
152
217
|
// only what the engine actually uses decides about re-layout (the brightness slider, the page sound ... must not)
|
|
153
218
|
const engineJson = JSON.stringify(engineSettings);
|
|
@@ -158,12 +223,40 @@ export const EpubReader = forwardRef<EpubReaderHandle, EpubReaderProps>(function
|
|
|
158
223
|
web.current?.injectJavaScript(toScript(cmd));
|
|
159
224
|
}, []);
|
|
160
225
|
|
|
226
|
+
// iOS pauses the WebView in the background, so the start timer only counts while the app is in front
|
|
227
|
+
const [inFront, setInFront] = useState(AppState.currentState !== 'background');
|
|
228
|
+
useEffect(() => {
|
|
229
|
+
const sub = AppState.addEventListener('change', (state) => setInFront(state !== 'background'));
|
|
230
|
+
return () => sub.remove();
|
|
231
|
+
}, []);
|
|
232
|
+
|
|
161
233
|
// the engine never started (blocked WebView, out of memory ...)
|
|
162
234
|
useEffect(() => {
|
|
163
|
-
if (engineReady) return;
|
|
235
|
+
if (engineReady || !inFront) return;
|
|
164
236
|
const t = setTimeout(() => cb.current.onError?.('The reader could not be started.', { fatal: true }), ENGINE_START_TIMEOUT);
|
|
165
237
|
return () => clearTimeout(t);
|
|
166
|
-
}, [engineReady, webKey]);
|
|
238
|
+
}, [engineReady, webKey, inFront]);
|
|
239
|
+
|
|
240
|
+
// Fonts go before the book (so the first page already uses them), in pieces like the book. Only when they change.
|
|
241
|
+
useEffect(() => {
|
|
242
|
+
if (!engineReady) return;
|
|
243
|
+
// new keys for every update, so a later update cannot overwrite the data of one still waiting in the queue
|
|
244
|
+
const gen = ++fontGen.current;
|
|
245
|
+
const list: { key: string; family: string; mime?: string; weight?: CustomFont['weight']; style?: CustomFont['style'] }[] = [];
|
|
246
|
+
(cb.current.fonts || []).forEach((f, i) => {
|
|
247
|
+
const data = cleanFontBase64(f.base64);
|
|
248
|
+
if (!data) {
|
|
249
|
+
cb.current.onError?.(`The font "${f.family}" was skipped: its data is not valid base64.`, { fatal: false });
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const key = `f${gen}_${i}`;
|
|
253
|
+
for (let k = 0; k < data.length; k += CHUNK) {
|
|
254
|
+
web.current?.injectJavaScript(`window.__blob("${key}",${k === 0 ? 1 : 0},"${data.slice(k, k + CHUNK)}");true;`);
|
|
255
|
+
}
|
|
256
|
+
list.push({ key, family: f.family, mime: f.mime, weight: f.weight, style: f.style });
|
|
257
|
+
});
|
|
258
|
+
send({ cmd: 'fonts', list });
|
|
259
|
+
}, [engineReady, fontsKey, send]);
|
|
167
260
|
|
|
168
261
|
// open (or re-open) the book once the engine is up
|
|
169
262
|
const sourceKey = sourceKeyOf(source);
|
|
@@ -191,7 +284,9 @@ export const EpubReader = forwardRef<EpubReaderHandle, EpubReaderProps>(function
|
|
|
191
284
|
cmd: 'open',
|
|
192
285
|
settings: latest.current.engineSettings,
|
|
193
286
|
highlights: latest.current.highlights || [],
|
|
194
|
-
location: lastLoc.current || cb.current.initialLocation
|
|
287
|
+
location: lastLoc.current || cb.current.initialLocation,
|
|
288
|
+
locationChars: cb.current.locationChars ?? 150,
|
|
289
|
+
repairZeroWidth: cb.current.repairZeroWidth !== false
|
|
195
290
|
});
|
|
196
291
|
opened.current = true;
|
|
197
292
|
} catch (e) {
|
|
@@ -232,7 +327,6 @@ export const EpubReader = forwardRef<EpubReaderHandle, EpubReaderProps>(function
|
|
|
232
327
|
send({ cmd: 'showHit', offset: r.offset, length: r.length });
|
|
233
328
|
},
|
|
234
329
|
clearSelection: () => {
|
|
235
|
-
selection.current = null;
|
|
236
330
|
send({ cmd: 'clearSelection' });
|
|
237
331
|
},
|
|
238
332
|
setLabels: (header, footer) => send({ cmd: 'labels', header, footer })
|
|
@@ -259,7 +353,10 @@ export const EpubReader = forwardRef<EpubReaderHandle, EpubReaderProps>(function
|
|
|
259
353
|
break;
|
|
260
354
|
case 'toc':
|
|
261
355
|
p.onReady?.({ metadata: metaRef.current, rtl: !!d.rtl });
|
|
262
|
-
|
|
356
|
+
{
|
|
357
|
+
const items = Array.isArray(d.items) ? d.items : [];
|
|
358
|
+
p.onToc?.(items, Array.isArray(d.all) ? d.all : items);
|
|
359
|
+
}
|
|
263
360
|
break;
|
|
264
361
|
case 'cover':
|
|
265
362
|
if (typeof d.uri === 'string') p.onCover?.(d.uri);
|
|
@@ -272,11 +369,9 @@ export const EpubReader = forwardRef<EpubReaderHandle, EpubReaderProps>(function
|
|
|
272
369
|
p.onIndexed?.(d);
|
|
273
370
|
break;
|
|
274
371
|
case 'selection':
|
|
275
|
-
selection.current = d;
|
|
276
372
|
p.onSelection?.(d);
|
|
277
373
|
break;
|
|
278
374
|
case 'selectionClear':
|
|
279
|
-
selection.current = null;
|
|
280
375
|
p.onSelection?.(null);
|
|
281
376
|
break;
|
|
282
377
|
case 'highlightTap':
|
|
@@ -303,13 +398,19 @@ export const EpubReader = forwardRef<EpubReaderHandle, EpubReaderProps>(function
|
|
|
303
398
|
case 'end':
|
|
304
399
|
p.onEndReached?.(d.edge);
|
|
305
400
|
break;
|
|
401
|
+
case 'locations':
|
|
402
|
+
p.onLocations?.({ count: d.count });
|
|
403
|
+
break;
|
|
404
|
+
case 'zoom':
|
|
405
|
+
p.onZoom?.(typeof d.scale === 'number' ? d.scale : 1);
|
|
406
|
+
break;
|
|
306
407
|
case 'error':
|
|
307
408
|
p.onError?.(String(d.message || 'Unknown error'), { fatal: !!d.fatal });
|
|
308
409
|
break;
|
|
309
410
|
}
|
|
310
411
|
}, []);
|
|
311
412
|
|
|
312
|
-
const bg = themeColors(settings, systemDark).bg;
|
|
413
|
+
const bg = themeColors(settings, systemDark, props.themes).bg;
|
|
313
414
|
|
|
314
415
|
return (
|
|
315
416
|
<View style={[styles.fill, { backgroundColor: bg }]}>
|
|
@@ -339,11 +440,6 @@ export const EpubReader = forwardRef<EpubReaderHandle, EpubReaderProps>(function
|
|
|
339
440
|
mixedContentMode="never"
|
|
340
441
|
cacheEnabled={false}
|
|
341
442
|
textZoom={100}
|
|
342
|
-
menuItems={menuItems}
|
|
343
|
-
onCustomMenuSelection={(e) => {
|
|
344
|
-
const key = (e.nativeEvent as { key: string }).key;
|
|
345
|
-
cb.current.onMenuAction?.(key, selection.current);
|
|
346
|
-
}}
|
|
347
443
|
onShouldStartLoadWithRequest={(req) => req.url === 'about:blank' || req.url.startsWith('data:') || req.url.startsWith('about:')}
|
|
348
444
|
onRenderProcessGone={recover}
|
|
349
445
|
onContentProcessDidTerminate={recover}
|
package/src/EpubReaderView.tsx
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
-
import { AccessibilityInfo, ActivityIndicator, Alert, Animated, Linking, Platform, Pressable, Share, StatusBar, StyleSheet,
|
|
2
|
+
import { AccessibilityInfo, ActivityIndicator, Alert, Animated, Linking, Platform, Pressable, Share, StatusBar, StyleSheet, useColorScheme, useWindowDimensions, View } from 'react-native';
|
|
3
|
+
import { Text, UiFontsContext } from './ui/Text';
|
|
3
4
|
import { EpubReader, sourceKeyOf } from './EpubReader';
|
|
4
5
|
import type { EpubReaderHandle } from './EpubReader';
|
|
5
|
-
import { chromeColors, DEFAULT_SETTINGS, isDark, themeColors } from './themes';
|
|
6
|
+
import { chromeColors, DEFAULT_SETTINGS, fontChoices, isDark, themeColors } from './themes';
|
|
7
|
+
import type { ChromeOverrides } from './themes';
|
|
6
8
|
import type {
|
|
7
9
|
Bookmark,
|
|
10
|
+
BookColors,
|
|
8
11
|
BookLocation,
|
|
9
12
|
BookMetadata,
|
|
13
|
+
CustomFont,
|
|
10
14
|
EpubSource,
|
|
11
15
|
Highlight,
|
|
12
|
-
HighlightColor,
|
|
13
16
|
PageTurn,
|
|
14
17
|
ReaderSettings,
|
|
18
|
+
ReaderTheme,
|
|
15
19
|
RelocateEvent,
|
|
16
20
|
SafeInsets,
|
|
17
21
|
SearchResult,
|
|
18
22
|
SelectionEvent,
|
|
19
|
-
TocItem
|
|
23
|
+
TocItem,
|
|
24
|
+
UiFonts
|
|
20
25
|
} from './types';
|
|
21
26
|
import { AppearanceSheet } from './ui/AppearanceSheet';
|
|
22
27
|
import { ContentsSheet } from './ui/ContentsSheet';
|
|
@@ -38,7 +43,8 @@ export interface EpubReaderViewProps {
|
|
|
38
43
|
title?: string;
|
|
39
44
|
/** Space for the status bar / home indicator (and notch in landscape). Use useSafeAreaInsets() from react-native-safe-area-context. */
|
|
40
45
|
safeInsets?: SafeInsets;
|
|
41
|
-
|
|
46
|
+
/** where to open: an offset or an epub.js CFI ({ cfi: 'epubcfi(...)' }) */
|
|
47
|
+
initialLocation?: BookLocation | { cfi: string };
|
|
42
48
|
initialSettings?: Partial<ReaderSettings>;
|
|
43
49
|
initialHighlights?: Highlight[];
|
|
44
50
|
initialBookmarks?: Bookmark[];
|
|
@@ -54,6 +60,25 @@ export interface EpubReaderViewProps {
|
|
|
54
60
|
labels?: Partial<ReaderLabels>;
|
|
55
61
|
/** Largest EPUB in bytes that will be opened (default 150 MB, 0 = no limit). */
|
|
56
62
|
maxFileSize?: number;
|
|
63
|
+
/** Highlight colours by name, shown in the highlight menu (default: the built-in five). See EpubReader. */
|
|
64
|
+
highlightColors?: Record<string, string>;
|
|
65
|
+
highlightOpacity?: number;
|
|
66
|
+
/** App fonts; the ones with an `id` are added to the font list. See EpubReader. */
|
|
67
|
+
fonts?: CustomFont[];
|
|
68
|
+
bookColors?: BookColors;
|
|
69
|
+
maxZoom?: number;
|
|
70
|
+
/** characters per epub.js location (default 150, 0 = off). See EpubReader. */
|
|
71
|
+
locationChars?: number;
|
|
72
|
+
repairZeroWidth?: boolean;
|
|
73
|
+
onLocations?: (info: { count: number }) => void;
|
|
74
|
+
/** Page themes instead of the built-in six (Themes sheet and page colours). */
|
|
75
|
+
themes?: ReaderTheme[];
|
|
76
|
+
/** Colours of buttons, sheets and menus, for light and dark pages. Missing ones keep the default. */
|
|
77
|
+
chrome?: ChromeOverrides;
|
|
78
|
+
/** Fonts of the interface (not the book), e.g. the app's own font. */
|
|
79
|
+
uiFonts?: UiFonts;
|
|
80
|
+
/** Light or dark for the 'device' appearance. Default: the phone's setting. Pass the app's own when it has one. */
|
|
81
|
+
colorScheme?: 'light' | 'dark';
|
|
57
82
|
}
|
|
58
83
|
|
|
59
84
|
const RESET_THEME: Partial<ReaderSettings> = {
|
|
@@ -111,7 +136,9 @@ export function EpubReaderView(props: EpubReaderViewProps) {
|
|
|
111
136
|
const labels = useMemo(() => mergeLabels(props.labels), [props.labels]);
|
|
112
137
|
return (
|
|
113
138
|
<LabelsProvider value={labels}>
|
|
114
|
-
<
|
|
139
|
+
<UiFontsContext.Provider value={props.uiFonts || null}>
|
|
140
|
+
<ReaderScreen key={`${sourceKeyOf(props.source)}#${attempt}`} {...props} onRetry={() => setAttempt((a) => a + 1)} />
|
|
141
|
+
</UiFontsContext.Provider>
|
|
115
142
|
</LabelsProvider>
|
|
116
143
|
);
|
|
117
144
|
}
|
|
@@ -120,7 +147,8 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
120
147
|
const L = useLabels();
|
|
121
148
|
const screen = useWindowDimensions();
|
|
122
149
|
const safe = props.safeInsets || defaultInsets(screen.width, screen.height);
|
|
123
|
-
const
|
|
150
|
+
const phoneScheme = useColorScheme();
|
|
151
|
+
const systemDark = (props.colorScheme || phoneScheme) === 'dark';
|
|
124
152
|
const reader = useRef<EpubReaderHandle>(null);
|
|
125
153
|
|
|
126
154
|
const [settings, setSettings] = useState<ReaderSettings>({ ...DEFAULT_SETTINGS, ...props.initialSettings });
|
|
@@ -178,8 +206,8 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
178
206
|
);
|
|
179
207
|
|
|
180
208
|
const dark = isDark(settings, systemDark);
|
|
181
|
-
const page = themeColors(settings, systemDark);
|
|
182
|
-
const colors = useMemo(() => chromeColors(page, dark), [page, dark]);
|
|
209
|
+
const page = themeColors(settings, systemDark, props.themes);
|
|
210
|
+
const colors = useMemo(() => chromeColors(page, dark, props.chrome), [page, dark, props.chrome]);
|
|
183
211
|
|
|
184
212
|
// ----- chrome visibility animation
|
|
185
213
|
const fade = useRef(new Animated.Value(1)).current;
|
|
@@ -272,7 +300,7 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
272
300
|
} else {
|
|
273
301
|
updateBookmarks((prev) => [
|
|
274
302
|
...prev,
|
|
275
|
-
{ id: uid(), spine: loc.spine, offset: loc.offset, chapter: loc.chapter, excerpt: loc.excerpt, progress: loc.progress, createdAt: Date.now() }
|
|
303
|
+
{ id: uid(), spine: loc.spine, offset: loc.offset, cfi: loc.startCfi, chapter: loc.chapter, excerpt: loc.excerpt, progress: loc.progress, createdAt: Date.now() }
|
|
276
304
|
]);
|
|
277
305
|
}
|
|
278
306
|
};
|
|
@@ -286,7 +314,8 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
286
314
|
start: sel.start,
|
|
287
315
|
end: sel.end,
|
|
288
316
|
text: sel.text.replace(/\s+/g, ' ').trim(),
|
|
289
|
-
color: 'yellow',
|
|
317
|
+
color: props.highlightColors ? Object.keys(props.highlightColors)[0] || 'yellow' : 'yellow',
|
|
318
|
+
cfi: sel.cfi,
|
|
290
319
|
chapter: loc?.chapter,
|
|
291
320
|
createdAt: Date.now()
|
|
292
321
|
};
|
|
@@ -379,6 +408,8 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
379
408
|
|
|
380
409
|
const progressFill = Math.max(0, Math.min(1, loc?.progress ?? 0));
|
|
381
410
|
const tx = colors.text;
|
|
411
|
+
// text on the progress fill: the host's onStrong, else the default for that fill
|
|
412
|
+
const onFill = colors.onStrong || (colors.strong ? '#ffffff' : dark ? tx : '#ffffff');
|
|
382
413
|
|
|
383
414
|
return (
|
|
384
415
|
<View style={[styles.root, { backgroundColor: page.bg }]}>
|
|
@@ -391,6 +422,15 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
391
422
|
initialLocation={props.initialLocation}
|
|
392
423
|
highlights={highlights}
|
|
393
424
|
maxFileSize={props.maxFileSize}
|
|
425
|
+
highlightColors={props.highlightColors}
|
|
426
|
+
highlightOpacity={props.highlightOpacity}
|
|
427
|
+
fonts={props.fonts}
|
|
428
|
+
bookColors={props.bookColors}
|
|
429
|
+
themes={props.themes}
|
|
430
|
+
maxZoom={props.maxZoom}
|
|
431
|
+
locationChars={props.locationChars}
|
|
432
|
+
repairZeroWidth={props.repairZeroWidth}
|
|
433
|
+
onLocations={props.onLocations}
|
|
394
434
|
onReady={({ metadata, rtl: isRtl }) => {
|
|
395
435
|
setMeta(metadata);
|
|
396
436
|
setRtl(isRtl);
|
|
@@ -537,11 +577,11 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
537
577
|
{/* progress fill with inverted text on top of it */}
|
|
538
578
|
<View
|
|
539
579
|
pointerEvents="none"
|
|
540
|
-
style={[styles.fill, { width: progressFill * pillW, backgroundColor: dark ? 'rgba(255,255,255,0.35)' : '#1c1408', [rtl ? 'right' : 'left']: 0 }]}
|
|
580
|
+
style={[styles.fill, { width: progressFill * pillW, backgroundColor: colors.strong || (dark ? 'rgba(255,255,255,0.35)' : '#1c1408'), [rtl ? 'right' : 'left']: 0 }]}
|
|
541
581
|
>
|
|
542
582
|
<View style={[styles.pillRow, { position: 'absolute', top: 0, width: pillW, [rtl ? 'right' : 'left']: 0 }]}>
|
|
543
|
-
<Text style={[styles.pillText, { color:
|
|
544
|
-
<Icon name="list" color={
|
|
583
|
+
<Text style={[styles.pillText, { color: onFill }]} maxFontSizeMultiplier={1.3}>{L.contents} • {L.formatNumber(percent)}%</Text>
|
|
584
|
+
<Icon name="list" color={onFill} size={24} />
|
|
545
585
|
</View>
|
|
546
586
|
</View>
|
|
547
587
|
</Glass>
|
|
@@ -643,7 +683,8 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
643
683
|
rect={active.rect}
|
|
644
684
|
screen={screen}
|
|
645
685
|
colors={colors}
|
|
646
|
-
|
|
686
|
+
palette={props.highlightColors}
|
|
687
|
+
onColor={(c) => patchHighlight(activeHighlight.id, { color: c })}
|
|
647
688
|
onToggleUnderline={() => patchHighlight(activeHighlight.id, { style: activeHighlight.style === 'underline' ? 'highlight' : 'underline' })}
|
|
648
689
|
onNote={() => {
|
|
649
690
|
setNoteFor(activeHighlight.id);
|
|
@@ -752,6 +793,7 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
752
793
|
pageOf={pageOf}
|
|
753
794
|
bookmarks={bookmarks}
|
|
754
795
|
highlights={highlights}
|
|
796
|
+
palette={props.highlightColors}
|
|
755
797
|
onClose={closeSheet}
|
|
756
798
|
onGoToc={(item) => {
|
|
757
799
|
closeSheet();
|
|
@@ -759,11 +801,11 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
759
801
|
}}
|
|
760
802
|
onGoBookmark={(b) => {
|
|
761
803
|
closeSheet();
|
|
762
|
-
jump({ spine: b.spine, offset: b.offset });
|
|
804
|
+
jump(b.cfi ? { cfi: b.cfi, spine: b.spine, offset: b.offset } : { spine: b.spine, offset: b.offset });
|
|
763
805
|
}}
|
|
764
806
|
onGoHighlight={(h) => {
|
|
765
807
|
closeSheet();
|
|
766
|
-
jump({ spine: h.spine, offset: h.start });
|
|
808
|
+
jump(h.cfi ? { cfi: h.cfi, spine: h.spine, offset: h.start } : { spine: h.spine, offset: h.start });
|
|
767
809
|
}}
|
|
768
810
|
onDeleteBookmark={(id) => updateBookmarks((prev) => prev.filter((b) => b.id !== id))}
|
|
769
811
|
onDeleteHighlight={(id) => updateHighlights((prev) => prev.filter((h) => h.id !== id))}
|
|
@@ -787,6 +829,7 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
787
829
|
colors={colors}
|
|
788
830
|
settings={settings}
|
|
789
831
|
systemDark={systemDark}
|
|
832
|
+
themes={props.themes}
|
|
790
833
|
onChange={changeSettings}
|
|
791
834
|
onCustomize={() => switchSheet('customize')}
|
|
792
835
|
onClose={closeSheet}
|
|
@@ -797,6 +840,7 @@ function ReaderScreen(props: EpubReaderViewProps & { onRetry: () => void }) {
|
|
|
797
840
|
page={page}
|
|
798
841
|
settings={settings}
|
|
799
842
|
sample={loc?.excerpt || SAMPLE}
|
|
843
|
+
fontList={fontChoices(props.fonts)}
|
|
800
844
|
onChange={changeSettings}
|
|
801
845
|
onReset={() => changeSettings(RESET_THEME)}
|
|
802
846
|
onClose={() => switchSheet('appearance')}
|
package/src/cfiOrder.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Book order of two epub.js CFIs, without a DOM. A range CFI counts from its start.
|
|
2
|
+
// Only the step numbers and the character offset matter for order; [id] assertions are ignored.
|
|
3
|
+
|
|
4
|
+
function partsOf(cfi: string): { steps: number[]; offset: number } | null {
|
|
5
|
+
const m = /^epubcfi\((.*)\)$/.exec(cfi.trim());
|
|
6
|
+
if (!m) return null;
|
|
7
|
+
// drop [..] assertions first, they may contain ',' or ':'
|
|
8
|
+
const bits = m[1].replace(/\[[^\]]*\]/g, '').split(',');
|
|
9
|
+
const path = bits.length === 3 ? bits[0] + bits[1] : bits[0];
|
|
10
|
+
const steps: number[] = [];
|
|
11
|
+
const re = /\/(\d+)/g;
|
|
12
|
+
let s: RegExpExecArray | null;
|
|
13
|
+
while ((s = re.exec(path))) steps.push(+s[1]);
|
|
14
|
+
const off = /:(\d+)[^/]*$/.exec(path);
|
|
15
|
+
return steps.length ? { steps, offset: off ? +off[1] : 0 } : null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Negative when `a` comes first, positive when `b` comes first, 0 when equal or unreadable. */
|
|
19
|
+
export function compareCfi(a: string, b: string): number {
|
|
20
|
+
const x = partsOf(a);
|
|
21
|
+
const y = partsOf(b);
|
|
22
|
+
if (!x || !y) return 0;
|
|
23
|
+
for (let i = 0; i < Math.min(x.steps.length, y.steps.length); i++) {
|
|
24
|
+
if (x.steps[i] !== y.steps[i]) return x.steps[i] - y.steps[i];
|
|
25
|
+
}
|
|
26
|
+
if (x.steps.length !== y.steps.length) return x.steps.length - y.steps.length;
|
|
27
|
+
return x.offset - y.offset;
|
|
28
|
+
}
|