aksos-react-native-epub-reader 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +351 -2
- package/lib/EpubReader.d.ts +76 -0
- package/lib/EpubReader.js +284 -0
- package/lib/EpubReaderView.d.ts +31 -0
- package/lib/EpubReaderView.js +599 -0
- package/lib/assets/page-turn-1.wav +0 -0
- package/lib/assets/page-turn-2.wav +0 -0
- package/lib/assets/page-turn-3.wav +0 -0
- package/lib/assets/page-turn-4.wav +0 -0
- package/lib/engine/html.d.ts +1 -0
- package/lib/engine/html.js +5 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +34 -0
- package/lib/sound.d.ts +1 -0
- package/lib/sound.js +71 -0
- package/lib/strings.d.ts +94 -0
- package/lib/strings.js +96 -0
- package/lib/themes.d.ts +60 -0
- package/lib/themes.js +166 -0
- package/lib/types.d.ts +149 -0
- package/lib/types.js +2 -0
- package/lib/ui/AppearanceSheet.d.ts +15 -0
- package/lib/ui/AppearanceSheet.js +134 -0
- package/lib/ui/ContentsSheet.d.ts +27 -0
- package/lib/ui/ContentsSheet.js +89 -0
- package/lib/ui/CustomizeSheet.d.ts +16 -0
- package/lib/ui/CustomizeSheet.js +141 -0
- package/lib/ui/Glass.d.ts +15 -0
- package/lib/ui/Glass.js +27 -0
- package/lib/ui/HighlightMenu.d.ts +25 -0
- package/lib/ui/HighlightMenu.js +45 -0
- package/lib/ui/Icons.d.ts +17 -0
- package/lib/ui/Icons.js +193 -0
- package/lib/ui/LineGuide.d.ts +21 -0
- package/lib/ui/LineGuide.js +92 -0
- package/lib/ui/NoteEditor.d.ts +12 -0
- package/lib/ui/NoteEditor.js +63 -0
- package/lib/ui/Scrubber.d.ts +15 -0
- package/lib/ui/Scrubber.js +86 -0
- package/lib/ui/SearchSheet.d.ts +14 -0
- package/lib/ui/SearchSheet.js +79 -0
- package/lib/ui/Sheet.d.ts +19 -0
- package/lib/ui/Sheet.js +40 -0
- package/lib/ui/Slider.d.ts +14 -0
- package/lib/ui/Slider.js +53 -0
- package/package.json +76 -6
- package/src/EpubReader.tsx +356 -0
- package/src/EpubReaderView.tsx +865 -0
- package/src/assets/page-turn-1.wav +0 -0
- package/src/assets/page-turn-2.wav +0 -0
- package/src/assets/page-turn-3.wav +0 -0
- package/src/assets/page-turn-4.wav +0 -0
- package/src/engine/html.ts +2 -0
- package/src/engine/reader.js +2047 -0
- package/src/index.ts +9 -0
- package/src/sound.ts +66 -0
- package/src/strings.ts +187 -0
- package/src/themes.ts +188 -0
- package/src/types.ts +154 -0
- package/src/ui/AppearanceSheet.tsx +176 -0
- package/src/ui/ContentsSheet.tsx +146 -0
- package/src/ui/CustomizeSheet.tsx +191 -0
- package/src/ui/Glass.tsx +37 -0
- package/src/ui/HighlightMenu.tsx +66 -0
- package/src/ui/Icons.tsx +246 -0
- package/src/ui/LineGuide.tsx +87 -0
- package/src/ui/NoteEditor.tsx +57 -0
- package/src/ui/Scrubber.tsx +78 -0
- package/src/ui/SearchSheet.tsx +95 -0
- package/src/ui/Sheet.tsx +71 -0
- package/src/ui/Slider.tsx +30 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { EpubReaderView } from './EpubReaderView';
|
|
2
|
+
export type { EpubReaderViewProps } from './EpubReaderView';
|
|
3
|
+
export { EpubReader } from './EpubReader';
|
|
4
|
+
export type { EpubReaderProps, EpubReaderHandle, ReaderErrorInfo } from './EpubReader';
|
|
5
|
+
export { THEMES, FONTS, HIGHLIGHT_COLORS, DEFAULT_SETTINGS, getTheme, getFont, chromeColors, themeColors, isDark } from './themes';
|
|
6
|
+
export type { ChromeColors } from './themes';
|
|
7
|
+
export { DEFAULT_LABELS } from './strings';
|
|
8
|
+
export type { ReaderLabels } from './strings';
|
|
9
|
+
export * from './types';
|
package/src/sound.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Page-turn sound: real recorded page flips (see README for credit), a different one each turn.
|
|
2
|
+
// expo-audio is optional: without it the reader is simply silent.
|
|
3
|
+
//
|
|
4
|
+
// One player per recording, and every play first waits for the rewind to finish.
|
|
5
|
+
// (Calling play() right after an un-awaited seekTo(0) on a player that already finished plays nothing.)
|
|
6
|
+
let audio: any = null;
|
|
7
|
+
let missing = false;
|
|
8
|
+
let players: any[] = [];
|
|
9
|
+
let last = -1;
|
|
10
|
+
let modeSet = false;
|
|
11
|
+
|
|
12
|
+
function sources(): any[] {
|
|
13
|
+
// static requires so the bundler can find the assets
|
|
14
|
+
return [
|
|
15
|
+
require('./assets/page-turn-1.wav'),
|
|
16
|
+
require('./assets/page-turn-2.wav'),
|
|
17
|
+
require('./assets/page-turn-3.wav'),
|
|
18
|
+
require('./assets/page-turn-4.wav')
|
|
19
|
+
];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function load(): boolean {
|
|
23
|
+
if (audio) return true;
|
|
24
|
+
if (missing) return false;
|
|
25
|
+
try {
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
27
|
+
audio = require('expo-audio');
|
|
28
|
+
return true;
|
|
29
|
+
} catch {
|
|
30
|
+
missing = true;
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function player(i: number): any {
|
|
36
|
+
if (!players[i]) {
|
|
37
|
+
players[i] = audio.createAudioPlayer(sources()[i]);
|
|
38
|
+
players[i].volume = 0.7;
|
|
39
|
+
}
|
|
40
|
+
return players[i];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function playPageTurn(): void {
|
|
44
|
+
if (!load()) return;
|
|
45
|
+
try {
|
|
46
|
+
if (!modeSet) {
|
|
47
|
+
modeSet = true;
|
|
48
|
+
// do not stop the user's music, and follow the silent switch
|
|
49
|
+
Promise.resolve(audio.setAudioModeAsync?.({ playsInSilentMode: false, interruptionMode: 'mixWithOthers' })).catch(() => undefined);
|
|
50
|
+
}
|
|
51
|
+
// random recording, never the same one twice in a row
|
|
52
|
+
let i = Math.floor(Math.random() * 4);
|
|
53
|
+
if (i === last) i = (i + 1) % 4;
|
|
54
|
+
last = i;
|
|
55
|
+
const p = player(i);
|
|
56
|
+
Promise.resolve(p.seekTo(0))
|
|
57
|
+
.catch(() => undefined)
|
|
58
|
+
.then(() => p.play())
|
|
59
|
+
.catch(() => {
|
|
60
|
+
// a broken player is replaced on its next use
|
|
61
|
+
players = [];
|
|
62
|
+
});
|
|
63
|
+
} catch {
|
|
64
|
+
players = [];
|
|
65
|
+
}
|
|
66
|
+
}
|
package/src/strings.ts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Every text the reader shows. Pass a partial object as the `labels` prop of EpubReaderView to translate
|
|
5
|
+
* (for example into Pashto, Dari or Arabic); anything you leave out stays English.
|
|
6
|
+
*/
|
|
7
|
+
export interface ReaderLabels {
|
|
8
|
+
contents: string;
|
|
9
|
+
chapters: string;
|
|
10
|
+
font: string;
|
|
11
|
+
lineSpacing: string;
|
|
12
|
+
characterSpacing: string;
|
|
13
|
+
wordSpacing: string;
|
|
14
|
+
margins: string;
|
|
15
|
+
columns: string;
|
|
16
|
+
columnsAuto: string;
|
|
17
|
+
columnsOne: string;
|
|
18
|
+
columnsTwo: string;
|
|
19
|
+
slide: string;
|
|
20
|
+
curl: string;
|
|
21
|
+
fastFade: string;
|
|
22
|
+
scrollMode: string;
|
|
23
|
+
light: string;
|
|
24
|
+
dark: string;
|
|
25
|
+
matchDevice: string;
|
|
26
|
+
matchSurroundings: string;
|
|
27
|
+
appearance: string;
|
|
28
|
+
pageTurnStyle: string;
|
|
29
|
+
delete: string;
|
|
30
|
+
deleteBookmark: string;
|
|
31
|
+
deleteHighlight: string;
|
|
32
|
+
search: string;
|
|
33
|
+
searchBook: string;
|
|
34
|
+
searchInBook: string;
|
|
35
|
+
searching: string;
|
|
36
|
+
noResults: string;
|
|
37
|
+
themesSettings: string;
|
|
38
|
+
customizeTheme: string;
|
|
39
|
+
customize: string;
|
|
40
|
+
resetTheme: string;
|
|
41
|
+
boldText: string;
|
|
42
|
+
justifyText: string;
|
|
43
|
+
pageTurnSound: string;
|
|
44
|
+
accessibilityLayout: string;
|
|
45
|
+
smallerText: string;
|
|
46
|
+
largerText: string;
|
|
47
|
+
readerMenu: string;
|
|
48
|
+
closeBook: string;
|
|
49
|
+
share: string;
|
|
50
|
+
rotationLock: string;
|
|
51
|
+
rotationLockNeedsPackage: string;
|
|
52
|
+
lineGuide: string;
|
|
53
|
+
turnOffLineGuide: string;
|
|
54
|
+
backgroundDimming: string;
|
|
55
|
+
dimHigh: string;
|
|
56
|
+
dimMedium: string;
|
|
57
|
+
dimLow: string;
|
|
58
|
+
dimNone: string;
|
|
59
|
+
bookmarkPage: string;
|
|
60
|
+
highlight: string;
|
|
61
|
+
note: string;
|
|
62
|
+
addNote: string;
|
|
63
|
+
addANote: string;
|
|
64
|
+
underline: string;
|
|
65
|
+
removeHighlight: string;
|
|
66
|
+
dismiss: string;
|
|
67
|
+
done: string;
|
|
68
|
+
close: string;
|
|
69
|
+
cancel: string;
|
|
70
|
+
save: string;
|
|
71
|
+
ok: string;
|
|
72
|
+
noToc: string;
|
|
73
|
+
noBookmarks: string;
|
|
74
|
+
noHighlights: string;
|
|
75
|
+
bookmarks: string;
|
|
76
|
+
highlights: string;
|
|
77
|
+
loading: string;
|
|
78
|
+
couldNotOpen: string;
|
|
79
|
+
tryAgain: string;
|
|
80
|
+
openLink: string;
|
|
81
|
+
open: string;
|
|
82
|
+
lastPageInChapter: string;
|
|
83
|
+
/** "N pages left in chapter" */
|
|
84
|
+
pagesLeft: (n: number) => string;
|
|
85
|
+
/** "3 of 120" */
|
|
86
|
+
pageOfTotal: (page: number, total: number) => string;
|
|
87
|
+
/** "Page 3 of 120" */
|
|
88
|
+
pageOfTotalLong: (page: number, total: number) => string;
|
|
89
|
+
returnToPage: (page: number) => string;
|
|
90
|
+
/** how numbers are written (e.g. Arabic-Indic digits for an Arabic UI) */
|
|
91
|
+
formatNumber: (n: number) => string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const DEFAULT_LABELS: ReaderLabels = {
|
|
95
|
+
contents: 'Contents',
|
|
96
|
+
chapters: 'Chapters',
|
|
97
|
+
font: 'Font',
|
|
98
|
+
lineSpacing: 'LINE SPACING',
|
|
99
|
+
characterSpacing: 'CHARACTER SPACING',
|
|
100
|
+
wordSpacing: 'WORD SPACING',
|
|
101
|
+
margins: 'MARGINS',
|
|
102
|
+
columns: 'Columns',
|
|
103
|
+
columnsAuto: 'Auto Set',
|
|
104
|
+
columnsOne: 'One',
|
|
105
|
+
columnsTwo: 'Two',
|
|
106
|
+
slide: 'Slide',
|
|
107
|
+
curl: 'Curl',
|
|
108
|
+
fastFade: 'Fast Fade',
|
|
109
|
+
scrollMode: 'Scroll',
|
|
110
|
+
light: 'Light',
|
|
111
|
+
dark: 'Dark',
|
|
112
|
+
matchDevice: 'Match Device',
|
|
113
|
+
matchSurroundings: 'Match Surroundings',
|
|
114
|
+
appearance: 'Appearance',
|
|
115
|
+
pageTurnStyle: 'Page turn style',
|
|
116
|
+
delete: 'Delete',
|
|
117
|
+
deleteBookmark: 'Delete bookmark?',
|
|
118
|
+
deleteHighlight: 'Delete highlight?',
|
|
119
|
+
search: 'Search',
|
|
120
|
+
searchBook: 'Search Book',
|
|
121
|
+
searchInBook: 'Search in book',
|
|
122
|
+
searching: 'Searching…',
|
|
123
|
+
noResults: 'No results',
|
|
124
|
+
themesSettings: 'Themes & Settings',
|
|
125
|
+
customizeTheme: 'Customize Theme',
|
|
126
|
+
customize: 'Customize',
|
|
127
|
+
resetTheme: 'Reset Theme',
|
|
128
|
+
boldText: 'Bold Text',
|
|
129
|
+
justifyText: 'Justify Text',
|
|
130
|
+
pageTurnSound: 'Page Turn Sound',
|
|
131
|
+
accessibilityLayout: 'Accessibility & Layout Options',
|
|
132
|
+
smallerText: 'Smaller text',
|
|
133
|
+
largerText: 'Larger text',
|
|
134
|
+
readerMenu: 'Reader menu',
|
|
135
|
+
closeBook: 'Close book',
|
|
136
|
+
share: 'Share',
|
|
137
|
+
rotationLock: 'Rotation lock',
|
|
138
|
+
rotationLockNeedsPackage: 'Install expo-screen-orientation to enable the rotation lock.',
|
|
139
|
+
lineGuide: 'Line guide',
|
|
140
|
+
turnOffLineGuide: 'Turn Off Line Guide',
|
|
141
|
+
backgroundDimming: 'Background Dimming',
|
|
142
|
+
dimHigh: 'High',
|
|
143
|
+
dimMedium: 'Medium',
|
|
144
|
+
dimLow: 'Low',
|
|
145
|
+
dimNone: 'None',
|
|
146
|
+
bookmarkPage: 'Bookmark this page',
|
|
147
|
+
highlight: 'Highlight',
|
|
148
|
+
note: 'Note',
|
|
149
|
+
addNote: 'Add note',
|
|
150
|
+
addANote: 'Add a note',
|
|
151
|
+
underline: 'Underline',
|
|
152
|
+
removeHighlight: 'Remove highlight',
|
|
153
|
+
dismiss: 'Dismiss',
|
|
154
|
+
done: 'Done',
|
|
155
|
+
close: 'Close',
|
|
156
|
+
cancel: 'Cancel',
|
|
157
|
+
save: 'Save',
|
|
158
|
+
ok: 'OK',
|
|
159
|
+
noToc: 'No table of contents',
|
|
160
|
+
noBookmarks: 'No bookmarks yet. Tap the bookmark button while reading.',
|
|
161
|
+
noHighlights: 'No highlights or notes yet. Select text to add one.',
|
|
162
|
+
bookmarks: 'Bookmarks',
|
|
163
|
+
highlights: 'Highlights',
|
|
164
|
+
loading: 'Opening book…',
|
|
165
|
+
couldNotOpen: 'This book could not be opened',
|
|
166
|
+
tryAgain: 'Try again',
|
|
167
|
+
openLink: 'Open this link?',
|
|
168
|
+
open: 'Open',
|
|
169
|
+
lastPageInChapter: 'Last page in chapter',
|
|
170
|
+
pagesLeft: (n) => `${n} page${n === 1 ? '' : 's'} left in chapter`,
|
|
171
|
+
pageOfTotal: (p, t) => `${p} of ${t}`,
|
|
172
|
+
pageOfTotalLong: (p, t) => `Page ${p} of ${t}`,
|
|
173
|
+
returnToPage: (p) => `Return to page ${p}`,
|
|
174
|
+
formatNumber: (n) => String(n)
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const LabelsContext = createContext<ReaderLabels>(DEFAULT_LABELS);
|
|
178
|
+
|
|
179
|
+
export const LabelsProvider = LabelsContext.Provider;
|
|
180
|
+
export function useLabels(): ReaderLabels {
|
|
181
|
+
return useContext(LabelsContext);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function mergeLabels(partial?: Partial<ReaderLabels>): ReaderLabels {
|
|
185
|
+
return partial ? { ...DEFAULT_LABELS, ...partial } : DEFAULT_LABELS;
|
|
186
|
+
}
|
|
187
|
+
|
package/src/themes.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { Platform } from 'react-native';
|
|
2
|
+
import type { HighlightColor, ReaderFont, ReaderSettings, ReaderTheme, ThemeColors } from './types';
|
|
3
|
+
|
|
4
|
+
export const THEMES: ReaderTheme[] = [
|
|
5
|
+
{
|
|
6
|
+
id: 'original',
|
|
7
|
+
name: 'Original',
|
|
8
|
+
light: { bg: '#ffffff', fg: '#1c1c1e', link: '#0a60ff' },
|
|
9
|
+
dark: { bg: '#000000', fg: '#f2f2f7', link: '#64a8ff' }
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
id: 'quiet',
|
|
13
|
+
name: 'Quiet',
|
|
14
|
+
light: { bg: '#e8e8ea', fg: '#58585c', link: '#3a6ea5' },
|
|
15
|
+
dark: { bg: '#121212', fg: '#8e8e93', link: '#6c9bd1' }
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
id: 'paper',
|
|
19
|
+
name: 'Paper',
|
|
20
|
+
light: { bg: '#f6f0e1', fg: '#3b3226', link: '#8a5a2b' },
|
|
21
|
+
dark: { bg: '#1d1d1f', fg: '#e9e0cd', link: '#d6a86a' }
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
id: 'bold',
|
|
25
|
+
name: 'Bold',
|
|
26
|
+
bold: true,
|
|
27
|
+
light: { bg: '#ffffff', fg: '#000000', link: '#0a60ff' },
|
|
28
|
+
dark: { bg: '#000000', fg: '#ffffff', link: '#64a8ff' }
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: 'calm',
|
|
32
|
+
name: 'Calm',
|
|
33
|
+
light: { bg: '#ece1c9', fg: '#4a3f2c', link: '#7a5a1f' },
|
|
34
|
+
dark: { bg: '#3d372d', fg: '#f0e7d3', link: '#e0b66f' }
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
id: 'focus',
|
|
38
|
+
name: 'Focus',
|
|
39
|
+
light: { bg: '#f3efe2', fg: '#2d2a24', link: '#9a4b1c' },
|
|
40
|
+
dark: { bg: '#1a1913', fg: '#e8e2d0', link: '#d98f5a' }
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
const SERIF = Platform.OS === 'ios' ? 'Georgia' : 'serif';
|
|
45
|
+
|
|
46
|
+
/** Font stacks. iOS ships most of these, on Android the stacks fall back to the system serif / sans. */
|
|
47
|
+
export const FONTS: ReaderFont[] = [
|
|
48
|
+
{ id: 'original', name: 'Original', css: 'original' },
|
|
49
|
+
{ id: 'system', name: 'System', css: "-apple-system, system-ui, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif" },
|
|
50
|
+
{ id: 'newyork', name: 'New York', css: "ui-serif, 'New York', Georgia, serif" },
|
|
51
|
+
{ id: 'athelas', name: 'Athelas', css: "Athelas, Georgia, serif" },
|
|
52
|
+
{ id: 'charter', name: 'Charter', css: "Charter, 'Bitstream Charter', Georgia, serif" },
|
|
53
|
+
{ id: 'georgia', name: 'Georgia', css: "Georgia, 'Times New Roman', serif" },
|
|
54
|
+
{ id: 'iowan', name: 'Iowan', css: "'Iowan Old Style', Georgia, serif" },
|
|
55
|
+
{ id: 'palatino', name: 'Palatino', css: "Palatino, 'Palatino Linotype', 'Book Antiqua', serif" },
|
|
56
|
+
{ id: 'seravek', name: 'Seravek', css: "Seravek, 'Avenir Next', -apple-system, sans-serif" },
|
|
57
|
+
{ id: 'times', name: 'Times New Roman', css: "'Times New Roman', Times, serif" },
|
|
58
|
+
{ id: 'naskh', name: 'Naskh (عربي / پښتو)', css: "'Noto Naskh Arabic', 'Geeza Pro', 'Droid Arabic Naskh', 'Scheherazade New', serif" }
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
export const HIGHLIGHT_COLORS: Record<HighlightColor, string> = {
|
|
62
|
+
yellow: '#ffd60a',
|
|
63
|
+
green: '#34c759',
|
|
64
|
+
blue: '#0a84ff',
|
|
65
|
+
pink: '#ff375f',
|
|
66
|
+
purple: '#bf5af2'
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const DEFAULT_SETTINGS: ReaderSettings = {
|
|
70
|
+
themeId: 'original',
|
|
71
|
+
appearance: 'device',
|
|
72
|
+
fontId: 'original',
|
|
73
|
+
fontSize: 19,
|
|
74
|
+
bold: false,
|
|
75
|
+
customize: false,
|
|
76
|
+
lineHeight: 1.5,
|
|
77
|
+
letterSpacing: 0,
|
|
78
|
+
wordSpacing: 0,
|
|
79
|
+
margins: 0,
|
|
80
|
+
columns: 'auto',
|
|
81
|
+
justify: false,
|
|
82
|
+
pageTurn: 'slide',
|
|
83
|
+
pageSound: true,
|
|
84
|
+
brightness: 1,
|
|
85
|
+
direction: 'auto'
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export const getTheme = (id: string): ReaderTheme => THEMES.find((t) => t.id === id) || THEMES[0];
|
|
89
|
+
export const getFont = (id: string): ReaderFont => FONTS.find((f) => f.id === id) || FONTS[0];
|
|
90
|
+
export const serifFont = SERIF;
|
|
91
|
+
|
|
92
|
+
export function isDark(settings: ReaderSettings, systemDark: boolean): boolean {
|
|
93
|
+
return settings.appearance === 'device' ? systemDark : settings.appearance === 'dark';
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function themeColors(settings: ReaderSettings, systemDark: boolean): ThemeColors {
|
|
97
|
+
const t = getTheme(settings.themeId);
|
|
98
|
+
return isDark(settings, systemDark) ? t.dark : t.light;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const BASE_MARGIN = 28;
|
|
102
|
+
|
|
103
|
+
/** Settings object understood by the WebView engine. */
|
|
104
|
+
export function toEngineSettings(settings: ReaderSettings, systemDark: boolean, insets: { top: number; bottom: number; left?: number; right?: number }) {
|
|
105
|
+
const theme = getTheme(settings.themeId);
|
|
106
|
+
const c = themeColors(settings, systemDark);
|
|
107
|
+
const custom = settings.customize;
|
|
108
|
+
return {
|
|
109
|
+
bg: c.bg,
|
|
110
|
+
fg: c.fg,
|
|
111
|
+
link: c.link,
|
|
112
|
+
bold: !!theme.bold || settings.bold,
|
|
113
|
+
fontFamily: getFont(settings.fontId).css,
|
|
114
|
+
fontSize: settings.fontSize,
|
|
115
|
+
lineHeight: custom ? settings.lineHeight : 1.5,
|
|
116
|
+
letterSpacing: custom ? (settings.letterSpacing / 100) * 0.25 : 0,
|
|
117
|
+
wordSpacing: custom ? (settings.wordSpacing / 100) * 0.5 : 0,
|
|
118
|
+
marginX: Math.round(BASE_MARGIN * (1 + (custom ? settings.margins : 0) / 100)),
|
|
119
|
+
columns: settings.columns,
|
|
120
|
+
justify: settings.justify,
|
|
121
|
+
mode: settings.pageTurn,
|
|
122
|
+
direction: settings.direction,
|
|
123
|
+
insets: { top: insets.top + 40, bottom: insets.bottom + 40, left: insets.left || 0, right: insets.right || 0 }
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface ChromeColors {
|
|
128
|
+
/** translucent control background (pills, circles), tinted with the page colour */
|
|
129
|
+
glass: string;
|
|
130
|
+
glassBorder: string;
|
|
131
|
+
/** solid sheet colour and a translucent one for sheets that float over the book */
|
|
132
|
+
sheet: string;
|
|
133
|
+
sheetGlass: string;
|
|
134
|
+
/** rows / cards inside a sheet: a neutral wash that works on any theme */
|
|
135
|
+
card: string;
|
|
136
|
+
text: string;
|
|
137
|
+
subtext: string;
|
|
138
|
+
separator: string;
|
|
139
|
+
/** highlight for the current chapter / selected items */
|
|
140
|
+
accent: string;
|
|
141
|
+
dark: boolean;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function mix(hex: string, other: string, amount: number): string {
|
|
145
|
+
const p = (h: string) => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16));
|
|
146
|
+
const a = p(hex);
|
|
147
|
+
const b = p(other);
|
|
148
|
+
const m = a.map((v, i) => Math.round(v + (b[i] - v) * amount));
|
|
149
|
+
return '#' + m.map((v) => v.toString(16).padStart(2, '0')).join('');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function rgba(hex: string, alpha: number): string {
|
|
153
|
+
const v = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16));
|
|
154
|
+
return `rgba(${v[0]},${v[1]},${v[2]},${alpha})`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Chrome (buttons, menus, sheets) is frosted glass tinted with the page colour, so a Calm page gets warm glass and a
|
|
159
|
+
* black page gets dark glass. Text and icons use the neutral text colour of the light / dark variant.
|
|
160
|
+
*/
|
|
161
|
+
export function chromeColors(colors: ThemeColors, dark: boolean): ChromeColors {
|
|
162
|
+
if (dark) {
|
|
163
|
+
return {
|
|
164
|
+
glass: rgba(mix(colors.bg, '#ffffff', 0.22), 0.4),
|
|
165
|
+
glassBorder: 'rgba(255,255,255,0.16)',
|
|
166
|
+
sheet: mix(colors.bg, '#ffffff', 0.1),
|
|
167
|
+
sheetGlass: rgba(mix(colors.bg, '#ffffff', 0.1), 0.84),
|
|
168
|
+
card: 'rgba(255,255,255,0.1)',
|
|
169
|
+
text: '#ffffff',
|
|
170
|
+
subtext: '#a09d96',
|
|
171
|
+
separator: 'rgba(255,255,255,0.14)',
|
|
172
|
+
accent: '#ffb340',
|
|
173
|
+
dark
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
glass: rgba(mix(colors.bg, '#ffffff', 0.55), 0.5),
|
|
178
|
+
glassBorder: 'rgba(0,0,0,0.1)',
|
|
179
|
+
sheet: mix(colors.bg, '#ffffff', 0.5),
|
|
180
|
+
sheetGlass: rgba(mix(colors.bg, '#ffffff', 0.5), 0.86),
|
|
181
|
+
card: 'rgba(0,0,0,0.06)',
|
|
182
|
+
text: '#000000',
|
|
183
|
+
subtext: '#6c6c70',
|
|
184
|
+
separator: 'rgba(0,0,0,0.14)',
|
|
185
|
+
accent: '#ff9500',
|
|
186
|
+
dark
|
|
187
|
+
};
|
|
188
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
export type HighlightColor = 'yellow' | 'green' | 'blue' | 'pink' | 'purple';
|
|
2
|
+
export type HighlightStyle = 'highlight' | 'underline';
|
|
3
|
+
|
|
4
|
+
/** Position in the book. Offsets are text-character offsets inside the spine item, so they survive font/size changes. */
|
|
5
|
+
export interface BookLocation {
|
|
6
|
+
spine: number;
|
|
7
|
+
offset: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface Highlight {
|
|
11
|
+
id: string;
|
|
12
|
+
spine: number;
|
|
13
|
+
start: number;
|
|
14
|
+
end: number;
|
|
15
|
+
text: string;
|
|
16
|
+
color: HighlightColor;
|
|
17
|
+
style?: HighlightStyle;
|
|
18
|
+
note?: string;
|
|
19
|
+
chapter?: string;
|
|
20
|
+
createdAt: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface Bookmark {
|
|
24
|
+
id: string;
|
|
25
|
+
spine: number;
|
|
26
|
+
offset: number;
|
|
27
|
+
chapter: string;
|
|
28
|
+
excerpt: string;
|
|
29
|
+
progress: number;
|
|
30
|
+
createdAt: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface TocItem {
|
|
34
|
+
label: string;
|
|
35
|
+
spine: number;
|
|
36
|
+
fragment: string;
|
|
37
|
+
level: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface BookMetadata {
|
|
41
|
+
title: string;
|
|
42
|
+
author: string;
|
|
43
|
+
language: string;
|
|
44
|
+
publisher: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface RelocateEvent {
|
|
48
|
+
spine: number;
|
|
49
|
+
/** offset of the first character on the page */
|
|
50
|
+
offset: number;
|
|
51
|
+
/** offset just past the last character on the page */
|
|
52
|
+
end: number;
|
|
53
|
+
page: number;
|
|
54
|
+
pageCount: number;
|
|
55
|
+
spineCount: number;
|
|
56
|
+
/** number of characters in this spine item */
|
|
57
|
+
textLength: number;
|
|
58
|
+
/** 0..1 across the whole book (exact once `indexed` is true) */
|
|
59
|
+
progress: number;
|
|
60
|
+
chapter: string;
|
|
61
|
+
excerpt: string;
|
|
62
|
+
indexed: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface SelectionEvent {
|
|
66
|
+
spine: number;
|
|
67
|
+
start: number;
|
|
68
|
+
end: number;
|
|
69
|
+
text: string;
|
|
70
|
+
rect: { x: number; y: number; width: number; height: number };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface SearchResult {
|
|
74
|
+
spine: number;
|
|
75
|
+
offset: number;
|
|
76
|
+
length: number;
|
|
77
|
+
chapter: string;
|
|
78
|
+
before: string;
|
|
79
|
+
match: string;
|
|
80
|
+
after: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface ThemeColors {
|
|
84
|
+
/** page background */
|
|
85
|
+
bg: string;
|
|
86
|
+
/** body text */
|
|
87
|
+
fg: string;
|
|
88
|
+
link: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A theme has a light and a dark variant, like the themes in Apple Books. */
|
|
92
|
+
export interface ReaderTheme {
|
|
93
|
+
id: string;
|
|
94
|
+
name: string;
|
|
95
|
+
light: ThemeColors;
|
|
96
|
+
dark: ThemeColors;
|
|
97
|
+
bold?: boolean;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export type Appearance = 'light' | 'dark' | 'device';
|
|
101
|
+
export type PageTurn = 'slide' | 'curl' | 'fade' | 'scroll';
|
|
102
|
+
export type ColumnsSetting = 'auto' | 1 | 2;
|
|
103
|
+
|
|
104
|
+
export interface ReaderFont {
|
|
105
|
+
id: string;
|
|
106
|
+
name: string;
|
|
107
|
+
/** CSS font-family, or 'original' to keep the book's own fonts */
|
|
108
|
+
css: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface ReaderSettings {
|
|
112
|
+
themeId: string;
|
|
113
|
+
appearance: Appearance;
|
|
114
|
+
fontId: string;
|
|
115
|
+
fontSize: number;
|
|
116
|
+
/** extra bold weight on top of the theme's own */
|
|
117
|
+
bold: boolean;
|
|
118
|
+
/** when false the spacing values below are ignored (defaults are used) */
|
|
119
|
+
customize: boolean;
|
|
120
|
+
lineHeight: number;
|
|
121
|
+
/** percent, 0 = default */
|
|
122
|
+
letterSpacing: number;
|
|
123
|
+
/** percent, 0 = default */
|
|
124
|
+
wordSpacing: number;
|
|
125
|
+
/** percent change of the side margins, 0 = default */
|
|
126
|
+
margins: number;
|
|
127
|
+
columns: ColumnsSetting;
|
|
128
|
+
justify: boolean;
|
|
129
|
+
pageTurn: PageTurn;
|
|
130
|
+
/** paper sound when a page is turned (Curl) */
|
|
131
|
+
pageSound: boolean;
|
|
132
|
+
/** 0.3 .. 1, dims the page like the Books brightness slider */
|
|
133
|
+
brightness: number;
|
|
134
|
+
/** 'auto' follows the book's page-progression-direction / language */
|
|
135
|
+
direction: 'auto' | 'ltr' | 'rtl';
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Where the book comes from.
|
|
140
|
+
* - uri: file://, content:// (Android), https://, or an asset/bundle URI that fetch() can read. `headers` are sent with the request.
|
|
141
|
+
* - base64: the EPUB bytes as base64 (a data: prefix is accepted).
|
|
142
|
+
* `key` identifies the book: when it changes the reader opens the new source. Defaults to the uri, or to a fingerprint of the data.
|
|
143
|
+
*/
|
|
144
|
+
export type EpubSource =
|
|
145
|
+
| { uri: string; headers?: Record<string, string>; key?: string }
|
|
146
|
+
| { base64: string; key?: string };
|
|
147
|
+
|
|
148
|
+
/** Safe-area space around the page, in points. Left/right matter in landscape on notched phones. */
|
|
149
|
+
export interface SafeInsets {
|
|
150
|
+
top: number;
|
|
151
|
+
bottom: number;
|
|
152
|
+
left?: number;
|
|
153
|
+
right?: number;
|
|
154
|
+
}
|