react-native-readium 2.0.0 → 2.0.2
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.
|
@@ -1,6 +1,27 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'react';
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
2
2
|
export const useReaderRef = ({ file, onLocationChange, onTableOfContents, }) => {
|
|
3
3
|
const readerRef = useRef(null);
|
|
4
|
+
const readingOrder = useRef([]);
|
|
5
|
+
const onLocationChangeWithTotalProgression = useCallback((newLocation) => {
|
|
6
|
+
if (!onLocationChange ||
|
|
7
|
+
!readingOrder.current ||
|
|
8
|
+
!newLocation.locations) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if (!newLocation.locations.totalProgression) {
|
|
12
|
+
const newLocationIndex = readingOrder.current.findIndex((entry) => entry.href === newLocation.href);
|
|
13
|
+
if (newLocationIndex < 0 || !readingOrder.current[newLocationIndex]) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const readingOrderCount = readingOrder.current.length;
|
|
17
|
+
const chapterTotalProgression = readingOrder.current[newLocationIndex].locations?.totalProgression ||
|
|
18
|
+
0;
|
|
19
|
+
const intraChapterTotalProgression = newLocation.locations.progression / readingOrderCount;
|
|
20
|
+
newLocation.locations.totalProgression =
|
|
21
|
+
chapterTotalProgression + intraChapterTotalProgression;
|
|
22
|
+
}
|
|
23
|
+
onLocationChange(newLocation);
|
|
24
|
+
}, [onLocationChange]);
|
|
4
25
|
useEffect(() => {
|
|
5
26
|
async function run() {
|
|
6
27
|
const D2Reader = await import('@d-i-t-a/reader');
|
|
@@ -10,8 +31,7 @@ export const useReaderRef = ({ file, onLocationChange, onTableOfContents, }) =>
|
|
|
10
31
|
userSettings: { verticalScroll: false },
|
|
11
32
|
api: {
|
|
12
33
|
updateCurrentLocation: async (location) => {
|
|
13
|
-
|
|
14
|
-
onLocationChange(location);
|
|
34
|
+
onLocationChangeWithTotalProgression(location);
|
|
15
35
|
return location;
|
|
16
36
|
},
|
|
17
37
|
},
|
|
@@ -20,6 +40,23 @@ export const useReaderRef = ({ file, onLocationChange, onTableOfContents, }) =>
|
|
|
20
40
|
if (onTableOfContents) {
|
|
21
41
|
onTableOfContents(ref.tableOfContents);
|
|
22
42
|
}
|
|
43
|
+
// This way of estimating the totalProgression treats all reading order
|
|
44
|
+
// entries as equal in length.
|
|
45
|
+
// It is based on the implementation in the Readium Go toolkit
|
|
46
|
+
// https://github.com/readium/go-toolkit/blob/31c6a65b588f825ffb6b4f2445337ffdc53af685/pkg/pub/service_positions.go#L66
|
|
47
|
+
const oldReadingOrder = ref.readingOrder;
|
|
48
|
+
const readingOrderCount = oldReadingOrder.length;
|
|
49
|
+
readingOrder.current = oldReadingOrder.map((item, index) => {
|
|
50
|
+
const totalProgression = index / readingOrderCount;
|
|
51
|
+
return {
|
|
52
|
+
...item,
|
|
53
|
+
locations: {
|
|
54
|
+
...item.locations,
|
|
55
|
+
progression: 0,
|
|
56
|
+
totalProgression: totalProgression,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
});
|
|
23
60
|
readerRef.current = ref;
|
|
24
61
|
}
|
|
25
62
|
run();
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'react';
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
2
|
|
|
3
3
|
import type { ReadiumProps } from '../../src/components/ReadiumView';
|
|
4
4
|
import type { Locator } from '../../src/interfaces';
|
|
@@ -9,6 +9,37 @@ export const useReaderRef = ({
|
|
|
9
9
|
onTableOfContents,
|
|
10
10
|
}: Pick<ReadiumProps, 'file' | 'onLocationChange' | 'onTableOfContents' >) => {
|
|
11
11
|
const readerRef = useRef<D2Reader | null>(null);
|
|
12
|
+
const readingOrder = useRef<Locator[]>([]);
|
|
13
|
+
|
|
14
|
+
const onLocationChangeWithTotalProgression = useCallback(
|
|
15
|
+
(newLocation: Locator) => {
|
|
16
|
+
if (
|
|
17
|
+
!onLocationChange ||
|
|
18
|
+
!readingOrder.current ||
|
|
19
|
+
!newLocation.locations
|
|
20
|
+
) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (!newLocation.locations.totalProgression) {
|
|
24
|
+
const newLocationIndex = readingOrder.current.findIndex(
|
|
25
|
+
(entry) => entry.href === newLocation.href
|
|
26
|
+
);
|
|
27
|
+
if (newLocationIndex < 0 || !readingOrder.current[newLocationIndex]) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const readingOrderCount = readingOrder.current.length;
|
|
31
|
+
const chapterTotalProgression =
|
|
32
|
+
readingOrder.current[newLocationIndex].locations?.totalProgression ||
|
|
33
|
+
0;
|
|
34
|
+
const intraChapterTotalProgression =
|
|
35
|
+
newLocation.locations.progression / readingOrderCount;
|
|
36
|
+
newLocation.locations.totalProgression =
|
|
37
|
+
chapterTotalProgression + intraChapterTotalProgression;
|
|
38
|
+
}
|
|
39
|
+
onLocationChange(newLocation);
|
|
40
|
+
},
|
|
41
|
+
[onLocationChange]
|
|
42
|
+
);
|
|
12
43
|
|
|
13
44
|
useEffect(() => {
|
|
14
45
|
async function run() {
|
|
@@ -19,7 +50,7 @@ export const useReaderRef = ({
|
|
|
19
50
|
userSettings: { verticalScroll: false },
|
|
20
51
|
api: {
|
|
21
52
|
updateCurrentLocation: async (location: Locator) => {
|
|
22
|
-
|
|
53
|
+
onLocationChangeWithTotalProgression(location);
|
|
23
54
|
return location;
|
|
24
55
|
},
|
|
25
56
|
},
|
|
@@ -29,6 +60,25 @@ export const useReaderRef = ({
|
|
|
29
60
|
if (onTableOfContents) {
|
|
30
61
|
onTableOfContents(ref.tableOfContents);
|
|
31
62
|
}
|
|
63
|
+
|
|
64
|
+
// This way of estimating the totalProgression treats all reading order
|
|
65
|
+
// entries as equal in length.
|
|
66
|
+
// It is based on the implementation in the Readium Go toolkit
|
|
67
|
+
// https://github.com/readium/go-toolkit/blob/31c6a65b588f825ffb6b4f2445337ffdc53af685/pkg/pub/service_positions.go#L66
|
|
68
|
+
const oldReadingOrder: Locator[] = ref.readingOrder;
|
|
69
|
+
const readingOrderCount = oldReadingOrder.length;
|
|
70
|
+
readingOrder.current = oldReadingOrder.map((item, index) => {
|
|
71
|
+
const totalProgression = index / readingOrderCount;
|
|
72
|
+
return {
|
|
73
|
+
...item,
|
|
74
|
+
locations: {
|
|
75
|
+
...item.locations,
|
|
76
|
+
progression: 0,
|
|
77
|
+
totalProgression: totalProgression,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
|
|
32
82
|
readerRef.current = ref;
|
|
33
83
|
}
|
|
34
84
|
run();
|