11import React , { useContext , useEffect , useState } from 'react' ;
2+ import * as FileSystem from 'expo-file-system' ;
23import { LoadingFile } from './utils/LoadingFile' ;
34import type { ReaderProps } from './types' ;
45import { View } from './View' ;
5- import { useInjectBookVariables } from './hooks/useInjectBookVariables ' ;
6+ import { useInjectWebVieWVariables } from './hooks/useInjectWebviewVariables ' ;
67import { ReaderContext , defaultTheme as initialTheme } from './context' ;
78import { isURL } from './utils/isURL' ;
89import { getSourceType } from './utils/getSourceType' ;
910import { getSourceName } from './utils/getPathname' ;
1011import { 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// ...
1317export 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}
0 commit comments