aksos-react-native-epub-reader 0.1.0 → 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 +178 -20
- package/lib/EpubReader.d.ts +66 -15
- package/lib/EpubReader.js +168 -30
- package/lib/EpubReaderView.d.ts +37 -5
- package/lib/EpubReaderView.js +194 -93
- 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 +5 -2
- package/lib/index.js +5 -1
- package/lib/strings.d.ts +94 -0
- package/lib/strings.js +96 -0
- package/lib/themes.d.ts +28 -6
- package/lib/themes.js +80 -20
- package/lib/types.d.ts +82 -1
- package/lib/ui/AppearanceSheet.d.ts +4 -2
- package/lib/ui/AppearanceSheet.js +40 -26
- package/lib/ui/ContentsSheet.d.ts +2 -0
- package/lib/ui/ContentsSheet.js +32 -22
- package/lib/ui/CustomizeSheet.d.ts +4 -2
- package/lib/ui/CustomizeSheet.js +31 -27
- package/lib/ui/HighlightMenu.d.ts +4 -2
- package/lib/ui/HighlightMenu.js +13 -11
- package/lib/ui/NoteEditor.js +12 -9
- package/lib/ui/SearchSheet.js +19 -6
- package/lib/ui/Sheet.js +7 -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 +239 -43
- package/src/EpubReaderView.tsx +287 -99
- 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 +1273 -157
- package/src/index.ts +5 -2
- package/src/strings.ts +187 -0
- package/src/themes.ts +97 -21
- package/src/types.ts +88 -2
- package/src/ui/AppearanceSheet.tsx +47 -29
- package/src/ui/ContentsSheet.tsx +34 -22
- package/src/ui/CustomizeSheet.tsx +27 -21
- package/src/ui/HighlightMenu.tsx +17 -13
- package/src/ui/NoteEditor.tsx +12 -9
- package/src/ui/SearchSheet.tsx +22 -5
- package/src/ui/Sheet.tsx +8 -5
- package/src/ui/Text.tsx +27 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
- The 20-second start timer only counts while the app is in front. Before, leaving the app while a book opened
|
|
7
|
+
could show "The reader could not be started" on return.
|
|
8
|
+
|
|
9
|
+
## 0.2.0
|
|
10
|
+
|
|
11
|
+
### New
|
|
12
|
+
- epub.js CFIs on highlights, bookmarks, selections and `onRelocate` (`startCfi`, `endCfi`).
|
|
13
|
+
- epub.js locations: `onLocations`, and `locations` on `onRelocate`. Same numbers as epub.js.
|
|
14
|
+
- `goTo({ cfi })` and `initialLocation={{ cfi }}`.
|
|
15
|
+
- Highlights can use any CSS colour, or a key of the new `highlightColors` prop.
|
|
16
|
+
- Custom book fonts (`fonts`), fonts for the reader's own buttons and sheets (`uiFonts`), and `chrome` colour overrides.
|
|
17
|
+
- `bookColors`: `'theme'` or `'keep'`.
|
|
18
|
+
- `themes` prop for your own reading themes.
|
|
19
|
+
|
|
20
|
+
### Breaking
|
|
21
|
+
- `menuItems` and `onMenuAction` are removed from `EpubReader`. They added items to the system selection menu,
|
|
22
|
+
which is now off. Use `onSelection` and your own UI instead.
|
|
23
|
+
- `TocItem.href` is new and required.
|
|
24
|
+
- `RelocateEvent.href` and `RelocateEvent.tocIndex` are new and required.
|
|
25
|
+
- `Highlight.color` is now any string, not only `HighlightColor`. Code like `MAP[h.color]` needs a check or a cast.
|
|
26
|
+
|
|
27
|
+
### Changed
|
|
28
|
+
- The system text selection is off. The reader's own long press and menu replace it.
|
|
29
|
+
- A highlight whose CFI is not found is drawn from its `spine` / `start` / `end` offsets.
|
|
30
|
+
- `goTo({ cfi })` with a CFI that is not found goes to the given `spine` / `offset`, or else the nearest chapter. It
|
|
31
|
+
no longer jumps to the start of the book.
|
|
32
|
+
- A book with no text (only images) has no `locations` on `onRelocate`.
|
|
33
|
+
- `.html` chapters are read like epub.js reads them (as HTML). A newline right after `<pre>` no longer counts, so
|
|
34
|
+
offsets saved after one in an `.html` chapter move by one. CFIs are not affected.
|
|
35
|
+
|
|
36
|
+
### Fixed
|
|
37
|
+
- Dragging a selection to a paragraph's full stop, or just below it, no longer takes the first word of the next
|
|
38
|
+
paragraph. A dragged selection also takes the punctuation touching its ends, so it can end on the full stop.
|
|
39
|
+
- An `.html` chapter written as XHTML (for example with `<title/>`) is no longer blank.
|
|
40
|
+
- Changing `fonts` twice while a book opens no longer loses the fonts.
|
|
41
|
+
- Font data that is not base64 is skipped with a non-fatal `onError`, instead of going into the page script.
|
|
42
|
+
- Theme `bg` colours other than `#rrggbb` no longer break the reader's buttons and sheets.
|
|
43
|
+
- Tapping a bookmark or highlight goes to its CFI. The notes list is sorted by CFI.
|
|
44
|
+
- Host highlight colours (names like `tomato`, `hsl()`, 8-digit hex) are plain `rgba()`, so they show on iOS 15.
|
|
45
|
+
- `bookColors: 'keep'`: a theme `bg` in any CSS form is read correctly. Plain links follow a theme change.
|
|
46
|
+
- TOC `href` is the manifest href as written (`chapter%201.xhtml`), so it equals `relocate.href`, like epub.js.
|
|
47
|
+
- A page with only a picture has `endCfi` equal to `startCfi`.
|
|
48
|
+
- `selection.cfi` is always set. It is no longer `undefined` when the chapter's CFI data was dropped from the cache.
|
|
49
|
+
- A chapter is shown before its CFI data is built, so it opens faster.
|
|
50
|
+
- Fonts that share an `id` (regular and bold files) show once in the font list.
|
|
51
|
+
- A `chrome` key set to `undefined` keeps the default. A host `strong` without `onStrong` gets black or white text,
|
|
52
|
+
whichever reads on it. The progress pill uses `strong` on dark pages too.
|
|
53
|
+
- The search and note fields use the `uiFonts` regular font.
|
|
54
|
+
|
|
55
|
+
### Tests
|
|
56
|
+
- epub.js 0.3.93 is now a pinned devDependency. The compare test no longer needs another repo.
|
|
57
|
+
- New test books: nested TOC with a missing file, a file name with a space, special ids, `<img>`, vertical text,
|
|
58
|
+
XHTML saved as `.html`, a missing spine file, a picture-only book. A small test font is built in code.
|
|
59
|
+
- Old books are deleted before each run. Checks that cannot run are shown as skipped, not as passed.
|
|
60
|
+
- A missing Chrome gives a clear message (set `CHROME_PATH`).
|
package/README.md
CHANGED
|
@@ -19,13 +19,14 @@ It is one component with a full reading UI (`EpubReaderView`), plus a headless e
|
|
|
19
19
|
3. [Saving and restoring](#saving-and-restoring)
|
|
20
20
|
4. [`EpubReaderView` props](#epubreaderview-props)
|
|
21
21
|
5. [Headless `EpubReader`](#headless-epubreader)
|
|
22
|
-
6. [
|
|
23
|
-
7. [
|
|
24
|
-
8. [
|
|
25
|
-
9. [
|
|
26
|
-
10. [
|
|
27
|
-
11. [
|
|
28
|
-
12. [
|
|
22
|
+
6. [epub.js compatibility](#epubjs-compatibility)
|
|
23
|
+
7. [Data types](#data-types)
|
|
24
|
+
8. [Settings reference](#settings-reference)
|
|
25
|
+
9. [Right-to-left books](#right-to-left-books)
|
|
26
|
+
10. [What the user can do](#what-the-user-can-do)
|
|
27
|
+
11. [Troubleshooting](#troubleshooting)
|
|
28
|
+
12. [Limitations](#limitations)
|
|
29
|
+
13. [Credits and license](#credits-and-license)
|
|
29
30
|
|
|
30
31
|
---
|
|
31
32
|
|
|
@@ -154,10 +155,12 @@ changes font size, theme or rotates the device.
|
|
|
154
155
|
|
|
155
156
|
| Prop | Type | Description |
|
|
156
157
|
|---|---|---|
|
|
157
|
-
| `source` | `{ uri
|
|
158
|
+
| `source` | `{ uri; headers?; key? } \| { base64; key? }` | **Required.** The EPUB: a `file://`, `content://` (Android) or `https://` uri, or base64 data. `key` identifies the book; a different source starts a fresh reader (highlights, bookmarks and position of one book never reach another). |
|
|
158
159
|
| `title` | `string` | Shown in Contents until the book's own title is read. |
|
|
159
|
-
| `safeInsets` | `{ top
|
|
160
|
-
| `
|
|
160
|
+
| `safeInsets` | `{ top; bottom; left?; right? }` | Status-bar, home-indicator and (landscape) notch space. Use `useSafeAreaInsets()` from `react-native-safe-area-context`. Without it a guess from the screen size is used. |
|
|
161
|
+
| `labels` | `Partial<ReaderLabels>` | Every text of the interface, for translation (Pashto, Dari, Arabic ...). `formatNumber` lets you show Arabic-Indic digits. See `DEFAULT_LABELS`. |
|
|
162
|
+
| `maxFileSize` | `number` | Largest EPUB in bytes (default 150 MB, `0` = no limit). Bigger files raise an error instead of crashing low-memory phones. |
|
|
163
|
+
| `initialLocation` | `{ spine; offset } \| { cfi }` | Where to open: an offset, or an epub.js CFI. Defaults to the start. |
|
|
161
164
|
| `initialSettings` | `Partial<ReaderSettings>` | Theme, font, size, spacing ... |
|
|
162
165
|
| `initialHighlights` | `Highlight[]` | |
|
|
163
166
|
| `initialBookmarks` | `Bookmark[]` | |
|
|
@@ -168,7 +171,19 @@ changes font size, theme or rotates the device.
|
|
|
168
171
|
| `onSettingsChange` | `(s: ReaderSettings) => void` | |
|
|
169
172
|
| `onMetadata` | `(m: BookMetadata) => void` | `title`, `author`, `language`, `publisher`. |
|
|
170
173
|
| `onCover` | `(dataUri: string) => void` | The cover image, if the book has one. |
|
|
171
|
-
| `onError` | `(message: string) => void` | Unreadable file, DRM, ... |
|
|
174
|
+
| `onError` | `(message: string) => void` | Unreadable file, DRM, ... The reader itself shows an error screen with **Try again** and **Close**, so the user is never stuck. |
|
|
175
|
+
| `onLocations` | `(info: { count }) => void` | epub.js locations are counted. From now on `onLocationChange` has `locations`. |
|
|
176
|
+
| `highlightColors` | `Record<string, string>` | Colours of the highlight menu by name, e.g. `{ yellow: '#ffff00' }`. Default: the built-in five. |
|
|
177
|
+
| `highlightOpacity` | `number` | Fill opacity of `highlightColors`. Default `0.3`. |
|
|
178
|
+
| `fonts` | `CustomFont[]` | App fonts as base64. The ones with an `id` are added to the font list. |
|
|
179
|
+
| `bookColors` | `'theme' \| 'keep'` | Default `'theme'`. See the headless table below. |
|
|
180
|
+
| `maxZoom` | `number` | Largest pinch zoom. Default `3`, `1` turns it off. |
|
|
181
|
+
| `locationChars` | `number` | Characters per epub.js location. Default `150`, `0` turns them off. Read when the book opens. |
|
|
182
|
+
| `repairZeroWidth` | `boolean` | Default `true`. See [hidden zero-width runs](#epubjs-compatibility). Read when the book opens. |
|
|
183
|
+
| `themes` | `ReaderTheme[]` | Your own page themes, instead of the built-in six. |
|
|
184
|
+
| `chrome` | `ChromeOverrides` | Colours of buttons, sheets and menus: `{ light?: {...}, dark?: {...} }`. Missing keys keep the default. |
|
|
185
|
+
| `uiFonts` | `UiFonts` | Fonts of the interface (not the book): `{ regular, medium?, bold? }`. Give `bold`, or iOS draws bold text in the regular font. |
|
|
186
|
+
| `colorScheme` | `'light' \| 'dark'` | Light or dark for the "device" appearance. Default: the phone's setting. |
|
|
172
187
|
|
|
173
188
|
---
|
|
174
189
|
|
|
@@ -200,13 +215,129 @@ ref.current?.prev();
|
|
|
200
215
|
ref.current?.goTo({ spine: 3 });
|
|
201
216
|
ref.current?.goTo({ spine: 3, fragment: 'section-2' });
|
|
202
217
|
ref.current?.goTo({ progress: 0.5 }); // 0..1 across the whole book
|
|
218
|
+
ref.current?.goTo({ cfi, spine, offset }); // epub.js CFI; spine / offset are used if it is not found
|
|
203
219
|
ref.current?.search('word', 1); // results arrive in onSearchResults(1, results, done)
|
|
204
220
|
ref.current?.showSearchResult(result);
|
|
205
221
|
ref.current?.clearSelection();
|
|
206
222
|
ref.current?.setLabels('Chapter 3', '42 of 300'); // running header / footer drawn on the page
|
|
207
223
|
```
|
|
208
224
|
|
|
209
|
-
Extra callbacks: `onReady`, `onCover`, `onIndexed`, `
|
|
225
|
+
Extra callbacks: `onReady`, `onCover`, `onIndexed`, `onLocations`, `onHighlightTap`, `onLink`, `onLinkJump`,
|
|
226
|
+
`onDirectionChange`, `onSearchResults`, `onEndReached`, `onPageTurn`, `onZoom`, and `onError(message, { fatal })`.
|
|
227
|
+
A non-fatal error (a font that was skipped, a CFI that was not found) leaves the book open.
|
|
228
|
+
|
|
229
|
+
More props:
|
|
230
|
+
|
|
231
|
+
| Prop | Default | What it does |
|
|
232
|
+
|---|---|---|
|
|
233
|
+
| `highlightColors` | built-in five | Colours by name, e.g. `{ yellow: '#ffff00', blue: '#add8e6' }`. A highlight's `color` can also be a CSS colour itself. |
|
|
234
|
+
| `highlightOpacity` | `0.3` | Fill opacity for `highlightColors` (same as epub.js). |
|
|
235
|
+
| `fonts` | none | App fonts as base64, loaded with `@font-face` in every chapter. A font with an `id` can be picked with `settings.fontId`. |
|
|
236
|
+
| `bookColors` | `'theme'` | `'keep'`: the book's own colours stay (coloured headings, links). Grey and black text gets the theme colour. Colours that are hard to read are made lighter or darker. |
|
|
237
|
+
| `maxZoom` | `3` | Two-finger pinch zoom. `1` turns it off. While zoomed, one finger moves the page, and taps and swipes never turn it. Double tap resets. Paged modes only. |
|
|
238
|
+
| `locationChars` | `150` | Characters per epub.js location (see below). `0` turns locations off. Read when the book opens: change `source.key` to apply a new value. |
|
|
239
|
+
| `repairZeroWidth` | `true` | Shows runs of 3+ zero-width characters as U+2060, one for one (see below). Read when the book opens. |
|
|
240
|
+
| `themes` | built-in six | Your own page themes. `settings.themeId` picks one; an unknown id gets the first. `bg` must be `#rgb`, `#rrggbb` (with or without alpha), `rgb()`, `white` or `black`. |
|
|
241
|
+
| `runningLabels` | `true` | The chapter header and page footer drawn on the page. `false`: none, and no space is kept for them. |
|
|
242
|
+
|
|
243
|
+
```tsx
|
|
244
|
+
<EpubReader
|
|
245
|
+
fonts={[{ id: 'nazanin', name: 'Bahij Nazanin', family: 'BahijNazanin', base64: NAZANIN_B64, mime: 'font/ttf' }]}
|
|
246
|
+
settings={{ ...settings, fontId: 'nazanin' }}
|
|
247
|
+
...
|
|
248
|
+
/>
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
What the events give your own interface:
|
|
252
|
+
|
|
253
|
+
| Need | Where |
|
|
254
|
+
|---|---|
|
|
255
|
+
| Selection and its place on screen | `onSelection(sel)`: `sel.text`, `sel.rect`, `sel.cfi` |
|
|
256
|
+
| Tap on a highlight | `onHighlightTap(id, rect)` |
|
|
257
|
+
| Table of contents | `onToc(items)`: `label`, `level`, `href`, `spine`, `fragment` |
|
|
258
|
+
| Current chapter | `onRelocate(e)`: `e.chapter`, `e.tocIndex` (index in the TOC list), `e.href` |
|
|
259
|
+
| Position to save | `onRelocate(e)`: `e.startCfi` (or `e.spine` + `e.offset`) |
|
|
260
|
+
| Chunk range of the page | `onRelocate(e)`: `e.locations.start` .. `e.locations.end`, of `e.locations.count` |
|
|
261
|
+
| Search | `search(query, id)` then `onSearchResults(id, results, done)` |
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
265
|
+
## epub.js compatibility
|
|
266
|
+
|
|
267
|
+
Use this when your app or server already stores epub.js data, or when another app still uses epub.js.
|
|
268
|
+
|
|
269
|
+
**CFIs.** The reader reads and writes epub.js CFIs. They are the same strings epub.js makes for the same spot.
|
|
270
|
+
|
|
271
|
+
- In: `initialLocation={{ cfi }}`, `goTo({ cfi })`, and highlights with a `cfi`. A highlight can be only
|
|
272
|
+
`{ id, cfi, color, style }`; the reader finds the text itself.
|
|
273
|
+
- Out: `onRelocate` gives `startCfi` and `endCfi`. `onSelection` gives `cfi` (a range CFI). `EpubReaderView` also
|
|
274
|
+
stores `cfi` on new highlights and bookmarks.
|
|
275
|
+
- A CFI you pass in is never rewritten. It is only read.
|
|
276
|
+
- When a CFI is not found in the book:
|
|
277
|
+
- A highlight that also has `spine`, `start` and `end` is drawn from those offsets.
|
|
278
|
+
- `goTo({ cfi, spine, offset })` goes to `spine` / `offset`. Without them, it goes to the nearest chapter. If there
|
|
279
|
+
is none, it stays on the page and `onError` gets a non-fatal error.
|
|
280
|
+
|
|
281
|
+
The conversion runs inside the reader, not as plain functions. It needs the chapter's DOM, and only the reader has it.
|
|
282
|
+
|
|
283
|
+
**Locations ("chunks").** The reader counts locations exactly like epub.js `book.locations.generate(150)`. The list
|
|
284
|
+
is the same, string by string. Once it is ready, `onLocations({ count })` fires, and every `onRelocate` has:
|
|
285
|
+
|
|
286
|
+
```ts
|
|
287
|
+
e.locations = {
|
|
288
|
+
start, // epub.js locationFromCfi(startCfi)
|
|
289
|
+
end, // epub.js locationFromCfi(endCfi)
|
|
290
|
+
count, // number of locations (epub.js locations.total + 1)
|
|
291
|
+
progress // 0..1, epub.js percentageFromCfi(startCfi)
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
`count` is epub.js `locations.total + 1`. So it is 1 more than `totalLocations` in `@epubjs-react-native/core`.
|
|
296
|
+
A book with no text (only images) has no locations. Then `e.locations` is not set.
|
|
297
|
+
|
|
298
|
+
Keep `locationChars` the same for a book forever if you store location numbers.
|
|
299
|
+
|
|
300
|
+
**What stays different.** Which text fits on one page depends on the layout. So the start and end of a page can
|
|
301
|
+
differ from epub.js on the same screen. For the same CFI, the location number and progress are always the same.
|
|
302
|
+
|
|
303
|
+
The chunk list can also differ from epub.js in these books. Each case shifts every chunk number after that chapter:
|
|
304
|
+
|
|
305
|
+
- A chapter that is not UTF-8 (UTF-16, or a declared charset like windows-1252). The reader decodes it; epub.js reads
|
|
306
|
+
it as UTF-8.
|
|
307
|
+
- A broken `.xhtml` chapter. The reader reads it again as HTML; epub.js counts its error page.
|
|
308
|
+
- A chapter file that is damaged. The reader counts its short "could not be read" text. A missing file counts as
|
|
309
|
+
empty.
|
|
310
|
+
|
|
311
|
+
**Limits copied from epub.js.** These CFIs work the same as in epub.js, so they also fail the same way:
|
|
312
|
+
|
|
313
|
+
- An `[id]` with `,`, `'` or `:` in it. epub.js cannot find its own CFI for those, and neither can this reader. A
|
|
314
|
+
highlight with such a CFI is not drawn from the CFI.
|
|
315
|
+
- `^` escapes (`^[`, `^]`, `^,`) are not decoded.
|
|
316
|
+
- Spatial (`@x:y`) and temporal (`~t`) parts are ignored.
|
|
317
|
+
|
|
318
|
+
A highlight that also has offsets is then drawn from its offsets.
|
|
319
|
+
|
|
320
|
+
**Hidden zero-width runs (watermarks).** Some servers hide data in the text as long runs of zero-width characters.
|
|
321
|
+
The reader never deletes them: that would move every offset and CFI after them. Instead, a run of 3 or more of
|
|
322
|
+
U+200B, U+200C, U+200D, U+FEFF is shown as U+2060 WORD JOINER, one for one. Arabic-script letters on both sides
|
|
323
|
+
still join, and the run never breaks a line. A single U+200C is real Pashto and Persian spelling and is kept.
|
|
324
|
+
Search ignores these characters. Text the reader sends to the app (excerpts, selection, TOC labels) leaves the runs out.
|
|
325
|
+
|
|
326
|
+
**Tests.** `npm run test:compat` loads the same books in epub.js and in this reader, in headless Chrome, and compares
|
|
327
|
+
chunk lists, CFIs in both directions, location numbers and progress. Set `EPUBJS_BUNDLE` to the epub.js build you
|
|
328
|
+
use, and `EPUB_DIR` to a folder of your own books.
|
|
329
|
+
|
|
330
|
+
### The book stays in memory
|
|
331
|
+
|
|
332
|
+
A `{ base64 }` source is never written to disk:
|
|
333
|
+
|
|
334
|
+
- The base64 string is sent into the WebView in pieces, with `injectJavaScript`.
|
|
335
|
+
- It is unzipped in memory (JSZip). Images and fonts become `data:` URIs.
|
|
336
|
+
- The WebView page is an inline HTML string. File access, DOM storage and the cache are turned off.
|
|
337
|
+
- No file system, storage or network API is used for it, on iOS or Android.
|
|
338
|
+
|
|
339
|
+
A `{ uri }` source is read with `fetch`. Over `https`, the system HTTP cache may keep a copy; send
|
|
340
|
+
`Cache-Control: no-store` from your server, or pass `{ base64 }` instead.
|
|
210
341
|
|
|
211
342
|
---
|
|
212
343
|
|
|
@@ -215,17 +346,23 @@ Extra callbacks: `onReady`, `onCover`, `onIndexed`, `onHighlightTap`, `onLink`,
|
|
|
215
346
|
```ts
|
|
216
347
|
interface Highlight {
|
|
217
348
|
id: string; spine: number; start: number; end: number; // character offsets in that chapter
|
|
218
|
-
text: string; color:
|
|
349
|
+
text: string; color: string; // a colour name or a CSS colour
|
|
350
|
+
cfi?: string; // epub.js range CFI; wins over the offsets,
|
|
351
|
+
// which are the fallback when it is not found
|
|
219
352
|
style?: 'highlight' | 'underline'; note?: string; chapter?: string; createdAt: number;
|
|
220
353
|
}
|
|
221
354
|
|
|
355
|
+
interface CfiHighlight { id: string; cfi: string; color: string; style?: 'highlight' | 'underline' }
|
|
356
|
+
|
|
222
357
|
interface Bookmark {
|
|
223
|
-
id: string; spine: number; offset: number; chapter: string; excerpt: string; progress: number; createdAt: number;
|
|
358
|
+
id: string; spine: number; offset: number; cfi?: string; chapter: string; excerpt: string; progress: number; createdAt: number;
|
|
224
359
|
}
|
|
225
360
|
|
|
226
361
|
interface RelocateEvent {
|
|
227
362
|
spine: number; offset: number; end: number; page: number; pageCount: number; spineCount: number;
|
|
228
363
|
textLength: number; progress: number; chapter: string; excerpt: string; indexed: boolean;
|
|
364
|
+
href: string; tocIndex: number; startCfi?: string; endCfi?: string;
|
|
365
|
+
locations?: { start: number; end: number; count: number; progress: number };
|
|
229
366
|
}
|
|
230
367
|
```
|
|
231
368
|
|
|
@@ -298,21 +435,30 @@ The sound can be disabled by the user in Themes & Settings, Customize.
|
|
|
298
435
|
|
|
299
436
|
**Controls are not blurred on Android.** `expo-blur` blur is limited on Android; the controls fall back to translucent colour.
|
|
300
437
|
|
|
301
|
-
**A book will not open.** `onError` receives the reason. DRM-protected files (Adobe, Apple FairPlay) cannot be opened.
|
|
438
|
+
**A book will not open.** `onError` receives the reason and the reader shows it with Try again / Close. DRM-protected files (Adobe, Apple FairPlay, Readium LCP) cannot be opened and are reported as such.
|
|
439
|
+
|
|
440
|
+
**The reader goes blank after the app was in the background.** If the system kills the web view process (low memory), the reader restarts by itself at the last position.
|
|
302
441
|
|
|
303
|
-
**Opening is slow for a very large book.** The file is sent to the page engine as base64. Typical novels take well under
|
|
304
|
-
a second; a 50 MB illustrated book will be slow.
|
|
442
|
+
**Opening is slow for a very large book.** The file is sent to the page engine as base64 in pieces. Typical novels take well under
|
|
443
|
+
a second; a 50 MB illustrated book will be slow, and files above `maxFileSize` are refused.
|
|
305
444
|
|
|
306
445
|
---
|
|
307
446
|
|
|
308
447
|
## Limitations
|
|
309
448
|
|
|
310
449
|
- Reflowable EPUBs only. Fixed-layout EPUBs and media overlays are not supported.
|
|
311
|
-
-
|
|
312
|
-
|
|
450
|
+
- Theme `bg` colours must be `#rgb`, `#rrggbb` (with or without alpha), `rgb()`, `white` or `black`. The reader's
|
|
451
|
+
buttons and sheets are tinted from it; any other colour gives them a plain white or black tint.
|
|
452
|
+
- Vertical writing modes (Japanese `writing-mode: vertical-rl`) are laid out horizontally.
|
|
453
|
+
- Character spacing is ignored in right-to-left books: it would break the joins of Arabic-script letters.
|
|
454
|
+
- Audio and video elements in a book are removed.
|
|
455
|
+
- The system text selection is turned off, because it does not work well on iPhone. The reader has its own: a long
|
|
456
|
+
press picks a word, a drag extends it, and the reader's menu (Highlight, Underline, Note) replaces the
|
|
457
|
+
system menu. So the system Copy, Look Up and Translate items are not offered.
|
|
313
458
|
- Text selection and highlights work in the chapter that is on screen; in Scroll mode a neighbouring chapter becomes
|
|
314
459
|
selectable once it scrolls into the middle of the screen.
|
|
315
|
-
-
|
|
460
|
+
- Pinch zoom works in the paged modes, not in Scroll mode.
|
|
461
|
+
- Search ignores case, diacritics (tashkeel), tatweel, ZWNJ and other zero-width characters, unifies Arabic / Persian letter variants (for example ي and ی) and all digit systems.
|
|
316
462
|
- The page curl is a geometric corner fold, not a physical simulation of paper.
|
|
317
463
|
|
|
318
464
|
---
|
|
@@ -326,6 +472,16 @@ The page-turn sounds (`src/assets/page-turn-*.wav`) are trimmed and softened rec
|
|
|
326
472
|
|
|
327
473
|
EPUB parsing uses [JSZip](https://stuk.github.io/jszip/).
|
|
328
474
|
|
|
475
|
+
The epub.js compatibility code (`src/engine/epubcfi.js`) is ported from [epub.js](https://github.com/futurepress/epub.js),
|
|
476
|
+
Copyright (c) 2013 FuturePress, BSD 2-Clause licence (the full notice is in that file).
|
|
477
|
+
|
|
478
|
+
### Security
|
|
479
|
+
|
|
480
|
+
Books are untrusted input. Chapter HTML is sanitized (scripts, event handlers, `javascript:` URLs, forms, `<base>`, remote images and
|
|
481
|
+
styles are removed), chapters render in frames under a Content-Security-Policy that blocks script and network, and only
|
|
482
|
+
`http(s)` and `mailto` links can leave the book, after a confirmation. Publisher font obfuscation (IDPF and Adobe) is decoded, so
|
|
483
|
+
embedded fonts work; real DRM is detected and refused.
|
|
484
|
+
|
|
329
485
|
### Developing
|
|
330
486
|
|
|
331
487
|
```bash
|
|
@@ -333,6 +489,8 @@ npm install
|
|
|
333
489
|
npm run build:engine # rebuilds src/engine/html.ts from src/engine/reader.js
|
|
334
490
|
npm run build:lib # compiles src/ to lib/
|
|
335
491
|
npm run typecheck
|
|
492
|
+
npm run test:compat # compares with epub.js in headless Chrome (needs Chrome and an epub.js build)
|
|
493
|
+
npm run test:features # watermark, search, CFI in/out, colours, fonts, book colours, zoom
|
|
336
494
|
```
|
|
337
495
|
|
|
338
496
|
The reading engine is plain JavaScript in `src/engine/reader.js`, bundled with JSZip into a single HTML page for the WebView.
|
package/lib/EpubReader.d.ts
CHANGED
|
@@ -1,30 +1,62 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import type { BookLocation, BookMetadata, EpubSource, Highlight, ReaderSettings, RelocateEvent, SearchResult, SelectionEvent, TocItem } from './types';
|
|
2
|
+
import type { BookColors, BookLocation, BookMetadata, CfiHighlight, CustomFont, EpubSource, Highlight, ReaderSettings, ReaderTheme, RelocateEvent, SafeInsets, SearchResult, SelectionEvent, TocItem } from './types';
|
|
3
|
+
export interface ReaderErrorInfo {
|
|
4
|
+
/** true when the book cannot be shown (failed to open, reader engine stopped); false for a problem on one page */
|
|
5
|
+
fatal: boolean;
|
|
6
|
+
}
|
|
3
7
|
export interface EpubReaderProps {
|
|
4
8
|
source: EpubSource;
|
|
5
9
|
settings: ReaderSettings;
|
|
6
10
|
/** system colour scheme, used when settings.appearance is 'device' */
|
|
7
11
|
systemDark?: boolean;
|
|
8
|
-
/** Space reserved for status bar / home indicator. The engine adds room for the running header and footer. */
|
|
9
|
-
safeInsets?:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
12
|
+
/** Space reserved for status bar / home indicator (and the notch in landscape). The engine adds room for the running header and footer. */
|
|
13
|
+
safeInsets?: SafeInsets;
|
|
14
|
+
/** Running header (chapter) and footer (page) on the page. false: none, and no space is kept for them. Default true. */
|
|
15
|
+
runningLabels?: boolean;
|
|
16
|
+
/** where to open: an offset, a share of the book, or an epub.js CFI ({ cfi: 'epubcfi(...)' }) */
|
|
13
17
|
initialLocation?: BookLocation | {
|
|
14
18
|
progress: number;
|
|
19
|
+
} | {
|
|
20
|
+
cfi: string;
|
|
15
21
|
};
|
|
16
|
-
highlights
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
/** highlights as offsets, or as epub.js CFIs only (CfiHighlight). A `cfi` always wins over offsets. */
|
|
23
|
+
highlights?: (Highlight | CfiHighlight)[];
|
|
24
|
+
/**
|
|
25
|
+
* Characters per epub.js location ("chunk"), as in epub.js book.locations.generate(n). Default 150 (the epub.js
|
|
26
|
+
* default). Keep it the same for a book forever if you store or pay by location numbers. 0 turns locations off.
|
|
27
|
+
*/
|
|
28
|
+
locationChars?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Show runs of 3+ zero-width characters (U+200B/C/D, U+FEFF), such as a hidden watermark, as U+2060 WORD JOINER,
|
|
31
|
+
* one for one, so they do not break words. Nothing is deleted, so offsets and CFIs do not change. Default true.
|
|
32
|
+
*/
|
|
33
|
+
repairZeroWidth?: boolean;
|
|
34
|
+
/** Highlight colours by name, e.g. { yellow: '#ffff00' }. A Highlight's `color` may also be a CSS colour itself. */
|
|
35
|
+
highlightColors?: Record<string, string>;
|
|
36
|
+
/** Fill opacity for highlightColors (default 0.3, like epub.js). */
|
|
37
|
+
highlightOpacity?: number;
|
|
38
|
+
/** Fonts loaded into every chapter with @font-face. A font with an `id` can be chosen with settings.fontId. */
|
|
39
|
+
fonts?: CustomFont[];
|
|
40
|
+
/** 'theme' (default): all text in the theme colour. 'keep': the book's own colours stay, made readable. */
|
|
41
|
+
bookColors?: BookColors;
|
|
42
|
+
/** Largest pinch zoom (default 3). 1 turns pinch zoom off. Paged modes only. */
|
|
43
|
+
maxZoom?: number;
|
|
44
|
+
/** Page themes to use instead of the built-in ones. settings.themeId picks one; an unknown id gets the first. */
|
|
45
|
+
themes?: ReaderTheme[];
|
|
46
|
+
/**
|
|
47
|
+
* Largest EPUB (bytes) that will be loaded. The whole book is held in memory while it is opened, so very large
|
|
48
|
+
* files can crash low-memory phones. Default 150 MB, 0 disables the check.
|
|
49
|
+
*/
|
|
50
|
+
maxFileSize?: number;
|
|
23
51
|
onReady?: (info: {
|
|
24
52
|
metadata: BookMetadata;
|
|
25
53
|
rtl: boolean;
|
|
26
54
|
}) => void;
|
|
27
|
-
|
|
55
|
+
/**
|
|
56
|
+
* items: the entries whose file is in the book. all: every entry, in the same order, with spine -1 for an entry
|
|
57
|
+
* whose file is missing (for example the chapters a preview leaves out). tocIndex points into items.
|
|
58
|
+
*/
|
|
59
|
+
onToc?: (items: TocItem[], all: TocItem[]) => void;
|
|
28
60
|
onCover?: (dataUri: string) => void;
|
|
29
61
|
onRelocate?: (e: RelocateEvent) => void;
|
|
30
62
|
/** fired once the whole book has been indexed: cumulative character offset of every spine item */
|
|
@@ -37,10 +69,21 @@ export interface EpubReaderProps {
|
|
|
37
69
|
onTap?: () => void;
|
|
38
70
|
/** a page turn animation is starting (use it to play a sound) */
|
|
39
71
|
onPageTurn?: () => void;
|
|
72
|
+
/** a web or mail link was tapped (other schemes are never forwarded). Open it yourself, ideally after asking. */
|
|
40
73
|
onLink?: (href: string) => void;
|
|
74
|
+
/** an internal link (footnote, cross reference) is about to jump; `from` is where the reader was, so you can offer a way back */
|
|
75
|
+
onLinkJump?: (from: BookLocation) => void;
|
|
76
|
+
/** the reading direction changed (the user overrode it in the settings) */
|
|
77
|
+
onDirectionChange?: (rtl: boolean) => void;
|
|
41
78
|
onSearchResults?: (searchId: number, results: SearchResult[], done: boolean) => void;
|
|
79
|
+
/** epub.js locations are counted (after the book is indexed). From now on onRelocate carries `locations`. */
|
|
80
|
+
onLocations?: (info: {
|
|
81
|
+
count: number;
|
|
82
|
+
}) => void;
|
|
83
|
+
/** the page was zoomed with two fingers (scale 1 = back to normal) */
|
|
84
|
+
onZoom?: (scale: number) => void;
|
|
42
85
|
onEndReached?: (edge: 'start' | 'end') => void;
|
|
43
|
-
onError?: (message: string) => void;
|
|
86
|
+
onError?: (message: string, info: ReaderErrorInfo) => void;
|
|
44
87
|
}
|
|
45
88
|
export interface EpubReaderHandle {
|
|
46
89
|
next(): void;
|
|
@@ -51,6 +94,12 @@ export interface EpubReaderHandle {
|
|
|
51
94
|
fragment?: string;
|
|
52
95
|
} | {
|
|
53
96
|
progress: number;
|
|
97
|
+
}
|
|
98
|
+
/** spine / offset: where to go when the CFI is not found in this book */
|
|
99
|
+
| {
|
|
100
|
+
cfi: string;
|
|
101
|
+
spine?: number;
|
|
102
|
+
offset?: number;
|
|
54
103
|
}): void;
|
|
55
104
|
search(query: string, searchId: number): void;
|
|
56
105
|
cancelSearch(searchId?: number): void;
|
|
@@ -60,4 +109,6 @@ export interface EpubReaderHandle {
|
|
|
60
109
|
/** Replace the running header / footer text drawn on the page. */
|
|
61
110
|
setLabels(header: string, footer: string): void;
|
|
62
111
|
}
|
|
112
|
+
/** Identity of a source: the explicit `key`, the uri, or a fingerprint sampled across the base64 data. */
|
|
113
|
+
export declare function sourceKeyOf(source: EpubSource): string;
|
|
63
114
|
export declare const EpubReader: React.ForwardRefExoticComponent<EpubReaderProps & React.RefAttributes<EpubReaderHandle>>;
|