Skip to content

Commit 8a4eae1

Browse files
Merge pull request #108 from victorsoares96/v1.3.0
V1.3.0
2 parents 4b8c398 + fabb9b3 commit 8a4eae1

17 files changed

Lines changed: 24550 additions & 4951 deletions

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules/
22
lib/
33
*.js
4+
epubjs.ts
5+
jszip.ts

example-bare/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,11 @@ SPEC CHECKSUMS:
562562
Flipper-RSocket: d9d9ade67cbecf6ac10730304bf5607266dd2541
563563
FlipperKit: cbdee19bdd4e7f05472a66ce290f1b729ba3cb86
564564
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
565-
glog: 72967d82937d9ee58f236577e0f1f47436b6e8ad
565+
glog: 476ee3e89abb49e07f822b48323c51c57124b572
566566
hermes-engine: 84e3af1ea01dd7351ac5d8689cbbea1f9903ffc3
567567
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
568568
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
569-
RCT-Folly: c4b972745ff748fcfa1aae9dccea0b67148ef4d8
569+
RCT-Folly: 4d8508a426467c48885f1151029bc15fa5d7b3b8
570570
RCTRequired: 3e917ea5377751094f38145fdece525aa90545a0
571571
RCTTypeSafety: c43c072a4bd60feb49a9570b0517892b4305c45e
572572
React: 176dd882de001854ced260fad41bb68a31aa4bd0
@@ -604,4 +604,4 @@ SPEC CHECKSUMS:
604604

605605
PODFILE CHECKSUM: e5d18f3a04d5ea19c74e92767979b34830093293
606606

607-
COCOAPODS: 1.11.3
607+
COCOAPODS: 1.12.1

example-expo/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,11 @@ SPEC CHECKSUMS:
600600
Flipper-RSocket: d9d9ade67cbecf6ac10730304bf5607266dd2541
601601
FlipperKit: cbdee19bdd4e7f05472a66ce290f1b729ba3cb86
602602
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
603-
glog: 72967d82937d9ee58f236577e0f1f47436b6e8ad
603+
glog: 476ee3e89abb49e07f822b48323c51c57124b572
604604
hermes-engine: 84e3af1ea01dd7351ac5d8689cbbea1f9903ffc3
605605
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
606606
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
607-
RCT-Folly: c4b972745ff748fcfa1aae9dccea0b67148ef4d8
607+
RCT-Folly: 4d8508a426467c48885f1151029bc15fa5d7b3b8
608608
RCTRequired: 3e917ea5377751094f38145fdece525aa90545a0
609609
RCTTypeSafety: c43c072a4bd60feb49a9570b0517892b4305c45e
610610
React: 176dd882de001854ced260fad41bb68a31aa4bd0
@@ -641,4 +641,4 @@ SPEC CHECKSUMS:
641641

642642
PODFILE CHECKSUM: 16b5f037e3a5be29705c940c1dc167cf765e6c9c
643643

644-
COCOAPODS: 1.11.3
644+
COCOAPODS: 1.12.1

example-expo/package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
},
1313
"dependencies": {
1414
"@epubjs-react-native/expo-file-system": "0.0.1",
15-
"expo": "45.0.0",
16-
"expo-file-system": "14.1.0",
1715
"@react-navigation/native": "6.0.11",
1816
"@react-navigation/native-stack": "6.7.0",
19-
"react": "17.0.2",
20-
"react-native": "0.68.2",
17+
"expo": "^49.0.0",
18+
"expo-file-system": "~15.4.2",
19+
"react": "18.2.0",
20+
"react-native": "0.72.3",
2121
"react-native-document-picker": "8.1.1",
22-
"react-native-gesture-handler": "2.1.1",
23-
"react-native-safe-area-context": "3.4.1",
24-
"react-native-screens": "3.15.0",
25-
"react-native-webview": "11.15.0"
22+
"react-native-gesture-handler": "~2.12.0",
23+
"react-native-safe-area-context": "4.6.3",
24+
"react-native-screens": "~3.22.0",
25+
"react-native-webview": "13.2.2"
2626
},
2727
"devDependencies": {
28-
"@babel/core": "^7.12.10",
28+
"@babel/core": "^7.20.0",
2929
"@babel/runtime": "^7.12.5",
3030
"babel-plugin-module-resolver": "^4.1.0",
3131
"metro-react-native-babel-preset": "^0.67.0",

example-expo/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"compilerOptions": {},
3+
"extends": "expo/tsconfig.base"
4+
}

example-expo/yarn.lock

Lines changed: 2155 additions & 1954 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@epubjs-react-native/core",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "A digital book reader in .opf .epub format for react native using epub.js library inside a webview.",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",
@@ -63,8 +63,8 @@
6363
"@testing-library/jest-native": "4.0.13",
6464
"@testing-library/react-native": "11.0.0",
6565
"@types/jest": "28.1.8",
66-
"@types/react": "17.0.21",
67-
"@types/react-native": "0.68.0",
66+
"@types/react": "~18.0.14",
67+
"@types/react-native": "^0.72.2",
6868
"@types/react-test-renderer": "17.0.2",
6969
"@typescript-eslint/eslint-plugin": "5.13.0",
7070
"@typescript-eslint/parser": "5.0.0",
@@ -81,29 +81,32 @@
8181
"eslint-plugin-react": "7.28.0",
8282
"eslint-plugin-react-hooks": "4.3.0",
8383
"eslint-plugin-testing-library": "5.5.1",
84+
"expo-file-system": "~15.4.2",
8485
"husky": "8.0.3",
8586
"jest": "28.1.3",
8687
"lint-staged": "13.0.3",
8788
"pod-install": "0.1.0",
8889
"prettier": "2.0.5",
8990
"react": "17.0.2",
90-
"react-native": "0.68.2",
91+
"react-native": "0.72.3",
9192
"react-native-builder-bob": "0.18.3",
92-
"react-native-gesture-handler": "2.1.1",
93-
"react-native-webview": "11.15.0",
93+
"react-native-gesture-handler": "^2.12.0",
94+
"react-native-webview": "^13.3.0",
9495
"react-test-renderer": "17.0.2",
9596
"ts-jest": "28.0.8",
9697
"ts-node": "10.9.1",
97-
"typescript": "4.4.4"
98+
"typescript": "^5.1.3"
9899
},
99100
"resolutions": {
100-
"@types/react": "17.0.21"
101+
"@types/react": "~18.0.14",
102+
"expo-file-system": "15.4.2"
101103
},
102104
"peerDependencies": {
103105
"react": "*",
104106
"react-native": "*",
105-
"react-native-gesture-handler": "2.1.1",
106-
"react-native-webview": "11.15.0"
107+
"expo-file-system": "15.4.2",
108+
"react-native-gesture-handler": "^2.12.0",
109+
"react-native-webview": "^13.3.0"
107110
},
108111
"config": {
109112
"commitizen": {

src/Reader.tsx

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import React, { useContext, useEffect, useState } from 'react';
2+
import * as FileSystem from 'expo-file-system';
23
import { LoadingFile } from './utils/LoadingFile';
34
import type { ReaderProps } from './types';
45
import { View } from './View';
5-
import { useInjectBookVariables } from './hooks/useInjectBookVariables';
6+
import { useInjectWebVieWVariables } from './hooks/useInjectWebviewVariables';
67
import { ReaderContext, defaultTheme as initialTheme } from './context';
78
import { isURL } from './utils/isURL';
89
import { getSourceType } from './utils/getSourceType';
910
import { getSourceName } from './utils/getPathname';
1011
import { SourceType } from './utils/enums/source-type.enum';
12+
import { isFsUri } from './utils/isFsUri';
13+
import jszip from './jszip';
14+
import epubjs from './epubjs';
1115

1216
// ...
1317
export function Reader({
@@ -31,26 +35,50 @@ export function Reader({
3135
} = useFileSystem();
3236

3337
const { setIsLoading, isLoading } = useContext(ReaderContext);
34-
const { injectBookVariables } = useInjectBookVariables();
35-
38+
const { injectWebVieWVariables } = useInjectWebVieWVariables();
3639
const [template, setTemplate] = useState<string | null>(null);
40+
const [templateUrl, setTemplateUrl] = useState<string | null>(null);
41+
const [allowedUris, setAllowedUris] = useState<string | null>(null);
3742

3843
useEffect(() => {
3944
(async () => {
4045
setIsLoading(true);
4146

47+
const jszipFileUri = `${FileSystem.documentDirectory}jszip.min.js`;
48+
const epubjsFileUri = `${FileSystem.documentDirectory}epub.min.js`;
49+
50+
try {
51+
await FileSystem.writeAsStringAsync(jszipFileUri, jszip);
52+
} catch (e) {
53+
throw new Error('failed to write jszip js file');
54+
}
55+
56+
try {
57+
await FileSystem.writeAsStringAsync(epubjsFileUri, epubjs);
58+
} catch (e) {
59+
throw new Error('failed to write epubjs js file');
60+
}
61+
62+
setAllowedUris(`${jszipFileUri},${epubjsFileUri}`);
63+
4264
if (src) {
4365
const sourceType = getSourceType(src);
4466
const isExternalSource = isURL(src);
67+
const isSrcInFs = isFsUri(src);
4568

4669
if (!sourceType) {
4770
throw new Error(`Invalid source type: ${src}`);
4871
}
4972

5073
if (!isExternalSource) {
74+
if (isSrcInFs) {
75+
setAllowedUris(`${src}${jszipFileUri},${epubjsFileUri}`);
76+
}
5177
if (sourceType === SourceType.BASE64) {
5278
setTemplate(
53-
injectBookVariables({
79+
injectWebVieWVariables({
80+
jszip: jszipFileUri,
81+
epubjs: epubjsFileUri,
5482
type: SourceType.BASE64,
5583
book: src,
5684
theme: defaultTheme,
@@ -62,7 +90,9 @@ export function Reader({
6290
setIsLoading(false);
6391
} else {
6492
setTemplate(
65-
injectBookVariables({
93+
injectWebVieWVariables({
94+
jszip: jszipFileUri,
95+
epubjs: epubjsFileUri,
6696
type: SourceType.BINARY,
6797
book: src,
6898
theme: defaultTheme,
@@ -82,9 +112,11 @@ export function Reader({
82112
throw new Error(`Invalid source name: ${src}`);
83113
}
84114

85-
if (sourceType === SourceType.OPF) {
115+
if (sourceType === SourceType.OPF || sourceType === SourceType.EPUB) {
86116
setTemplate(
87-
injectBookVariables({
117+
injectWebVieWVariables({
118+
jszip: jszipFileUri,
119+
epubjs: epubjsFileUri,
88120
type: sourceType,
89121
book: src,
90122
theme: defaultTheme,
@@ -95,14 +127,18 @@ export function Reader({
95127

96128
setIsLoading(false);
97129
} else {
98-
const { uri: bookFile } = await downloadFile(src, sourceName);
130+
const { uri: bookFileUri } = await downloadFile(src, sourceName);
99131

100-
if (!bookFile) throw new Error("Couldn't download book");
132+
if (!bookFileUri) throw new Error("Couldn't download book");
133+
134+
setAllowedUris(`${bookFileUri},${jszipFileUri},${epubjsFileUri}`);
101135

102136
setTemplate(
103-
injectBookVariables({
137+
injectWebVieWVariables({
138+
jszip: jszipFileUri,
139+
epubjs: epubjsFileUri,
104140
type: sourceType,
105-
book: bookFile,
141+
book: bookFileUri,
106142
theme: defaultTheme,
107143
locations: initialLocations,
108144
enableSelection: true,
@@ -118,11 +154,30 @@ export function Reader({
118154
defaultTheme,
119155
downloadFile,
120156
initialLocations,
121-
injectBookVariables,
157+
injectWebVieWVariables,
122158
setIsLoading,
123159
src,
124160
]);
125161

162+
useEffect(() => {
163+
const saveTemplateFileToDoc = async () => {
164+
try {
165+
if (template) {
166+
const content = template;
167+
168+
const fileUri = `${FileSystem.documentDirectory}index.html`;
169+
await FileSystem.writeAsStringAsync(fileUri, content);
170+
setTemplateUrl(fileUri);
171+
}
172+
} catch (error) {
173+
throw new Error('Error saving index.html file:');
174+
}
175+
};
176+
if (template) {
177+
saveTemplateFileToDoc();
178+
}
179+
}, [template]);
180+
126181
if (isLoading) {
127182
return renderLoadingFileComponent({
128183
fileSize,
@@ -132,6 +187,21 @@ export function Reader({
132187
});
133188
}
134189

135-
if (!template) throw new Error('Template is not set');
136-
return <View template={template} width={width} height={height} {...rest} />;
190+
if (!templateUrl || !allowedUris) {
191+
return renderLoadingFileComponent({
192+
fileSize,
193+
downloadProgress,
194+
downloadSuccess,
195+
downloadError,
196+
});
197+
}
198+
return (
199+
<View
200+
templateUri={templateUrl}
201+
allowedUris={allowedUris}
202+
width={width}
203+
height={height}
204+
{...rest}
205+
/>
206+
);
137207
}

src/View.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import type { ReaderProps } from './types';
1616
import { OpeningBook } from './utils/OpeningBook';
1717

1818
export type ViewProps = Omit<ReaderProps, 'src' | 'fileSystem'> & {
19-
template: string;
19+
templateUri: string;
20+
allowedUris: string;
2021
};
2122

2223
export function View({
23-
template,
24+
templateUri,
25+
allowedUris,
2426
onStarted = () => {},
2527
onReady = () => {},
2628
onDisplayError = () => {},
@@ -264,13 +266,14 @@ export function View({
264266
<TouchableWithoutFeedback onPress={handleDoublePress}>
265267
<WebView
266268
ref={book}
267-
source={{ html: template, baseUrl: 'file:///' }}
269+
source={{ uri: templateUri }}
268270
showsVerticalScrollIndicator={false}
269271
javaScriptEnabled
270272
originWhitelist={['*']}
271273
scrollEnabled={false}
272274
mixedContentMode="compatibility"
273275
onMessage={onMessage}
276+
allowingReadAccessToURL={allowedUris}
274277
allowUniversalAccessFromFileURLs
275278
allowFileAccessFromFileURLs
276279
allowFileAccess

0 commit comments

Comments
 (0)