Skip to content

Commit

Permalink
Merge pull request #108 from victorsoares96/v1.3.0
Browse files Browse the repository at this point in the history
V1.3.0
  • Loading branch information
victorsoares96 authored Sep 11, 2023
2 parents 4b8c398 + fabb9b3 commit 8a4eae1
Show file tree
Hide file tree
Showing 17 changed files with 24,550 additions and 4,951 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/
lib/
*.js
epubjs.ts
jszip.ts
6 changes: 3 additions & 3 deletions example-bare/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,11 @@ SPEC CHECKSUMS:
Flipper-RSocket: d9d9ade67cbecf6ac10730304bf5607266dd2541
FlipperKit: cbdee19bdd4e7f05472a66ce290f1b729ba3cb86
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: 72967d82937d9ee58f236577e0f1f47436b6e8ad
glog: 476ee3e89abb49e07f822b48323c51c57124b572
hermes-engine: 84e3af1ea01dd7351ac5d8689cbbea1f9903ffc3
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
RCT-Folly: c4b972745ff748fcfa1aae9dccea0b67148ef4d8
RCT-Folly: 4d8508a426467c48885f1151029bc15fa5d7b3b8
RCTRequired: 3e917ea5377751094f38145fdece525aa90545a0
RCTTypeSafety: c43c072a4bd60feb49a9570b0517892b4305c45e
React: 176dd882de001854ced260fad41bb68a31aa4bd0
Expand Down Expand Up @@ -604,4 +604,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: e5d18f3a04d5ea19c74e92767979b34830093293

COCOAPODS: 1.11.3
COCOAPODS: 1.12.1
6 changes: 3 additions & 3 deletions example-expo/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,11 @@ SPEC CHECKSUMS:
Flipper-RSocket: d9d9ade67cbecf6ac10730304bf5607266dd2541
FlipperKit: cbdee19bdd4e7f05472a66ce290f1b729ba3cb86
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: 72967d82937d9ee58f236577e0f1f47436b6e8ad
glog: 476ee3e89abb49e07f822b48323c51c57124b572
hermes-engine: 84e3af1ea01dd7351ac5d8689cbbea1f9903ffc3
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
RCT-Folly: c4b972745ff748fcfa1aae9dccea0b67148ef4d8
RCT-Folly: 4d8508a426467c48885f1151029bc15fa5d7b3b8
RCTRequired: 3e917ea5377751094f38145fdece525aa90545a0
RCTTypeSafety: c43c072a4bd60feb49a9570b0517892b4305c45e
React: 176dd882de001854ced260fad41bb68a31aa4bd0
Expand Down Expand Up @@ -641,4 +641,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 16b5f037e3a5be29705c940c1dc167cf765e6c9c

COCOAPODS: 1.11.3
COCOAPODS: 1.12.1
18 changes: 9 additions & 9 deletions example-expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
},
"dependencies": {
"@epubjs-react-native/expo-file-system": "0.0.1",
"expo": "45.0.0",
"expo-file-system": "14.1.0",
"@react-navigation/native": "6.0.11",
"@react-navigation/native-stack": "6.7.0",
"react": "17.0.2",
"react-native": "0.68.2",
"expo": "^49.0.0",
"expo-file-system": "~15.4.2",
"react": "18.2.0",
"react-native": "0.72.3",
"react-native-document-picker": "8.1.1",
"react-native-gesture-handler": "2.1.1",
"react-native-safe-area-context": "3.4.1",
"react-native-screens": "3.15.0",
"react-native-webview": "11.15.0"
"react-native-gesture-handler": "~2.12.0",
"react-native-safe-area-context": "4.6.3",
"react-native-screens": "~3.22.0",
"react-native-webview": "13.2.2"
},
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/core": "^7.20.0",
"@babel/runtime": "^7.12.5",
"babel-plugin-module-resolver": "^4.1.0",
"metro-react-native-babel-preset": "^0.67.0",
Expand Down
4 changes: 4 additions & 0 deletions example-expo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"compilerOptions": {},
"extends": "expo/tsconfig.base"
}
4,109 changes: 2,155 additions & 1,954 deletions example-expo/yarn.lock

Large diffs are not rendered by default.

23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@epubjs-react-native/core",
"version": "1.2.0",
"version": "1.3.0",
"description": "A digital book reader in .opf .epub format for react native using epub.js library inside a webview.",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down Expand Up @@ -63,8 +63,8 @@
"@testing-library/jest-native": "4.0.13",
"@testing-library/react-native": "11.0.0",
"@types/jest": "28.1.8",
"@types/react": "17.0.21",
"@types/react-native": "0.68.0",
"@types/react": "~18.0.14",
"@types/react-native": "^0.72.2",
"@types/react-test-renderer": "17.0.2",
"@typescript-eslint/eslint-plugin": "5.13.0",
"@typescript-eslint/parser": "5.0.0",
Expand All @@ -81,29 +81,32 @@
"eslint-plugin-react": "7.28.0",
"eslint-plugin-react-hooks": "4.3.0",
"eslint-plugin-testing-library": "5.5.1",
"expo-file-system": "~15.4.2",
"husky": "8.0.3",
"jest": "28.1.3",
"lint-staged": "13.0.3",
"pod-install": "0.1.0",
"prettier": "2.0.5",
"react": "17.0.2",
"react-native": "0.68.2",
"react-native": "0.72.3",
"react-native-builder-bob": "0.18.3",
"react-native-gesture-handler": "2.1.1",
"react-native-webview": "11.15.0",
"react-native-gesture-handler": "^2.12.0",
"react-native-webview": "^13.3.0",
"react-test-renderer": "17.0.2",
"ts-jest": "28.0.8",
"ts-node": "10.9.1",
"typescript": "4.4.4"
"typescript": "^5.1.3"
},
"resolutions": {
"@types/react": "17.0.21"
"@types/react": "~18.0.14",
"expo-file-system": "15.4.2"
},
"peerDependencies": {
"react": "*",
"react-native": "*",
"react-native-gesture-handler": "2.1.1",
"react-native-webview": "11.15.0"
"expo-file-system": "15.4.2",
"react-native-gesture-handler": "^2.12.0",
"react-native-webview": "^13.3.0"
},
"config": {
"commitizen": {
Expand Down
98 changes: 84 additions & 14 deletions src/Reader.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React, { useContext, useEffect, useState } from 'react';
import * as FileSystem from 'expo-file-system';
import { LoadingFile } from './utils/LoadingFile';
import type { ReaderProps } from './types';
import { View } from './View';
import { useInjectBookVariables } from './hooks/useInjectBookVariables';
import { useInjectWebVieWVariables } from './hooks/useInjectWebviewVariables';
import { ReaderContext, defaultTheme as initialTheme } from './context';
import { isURL } from './utils/isURL';
import { getSourceType } from './utils/getSourceType';
import { getSourceName } from './utils/getPathname';
import { SourceType } from './utils/enums/source-type.enum';
import { isFsUri } from './utils/isFsUri';
import jszip from './jszip';
import epubjs from './epubjs';

// ...
export function Reader({
Expand All @@ -31,26 +35,50 @@ export function Reader({
} = useFileSystem();

const { setIsLoading, isLoading } = useContext(ReaderContext);
const { injectBookVariables } = useInjectBookVariables();

const { injectWebVieWVariables } = useInjectWebVieWVariables();
const [template, setTemplate] = useState<string | null>(null);
const [templateUrl, setTemplateUrl] = useState<string | null>(null);
const [allowedUris, setAllowedUris] = useState<string | null>(null);

useEffect(() => {
(async () => {
setIsLoading(true);

const jszipFileUri = `${FileSystem.documentDirectory}jszip.min.js`;
const epubjsFileUri = `${FileSystem.documentDirectory}epub.min.js`;

try {
await FileSystem.writeAsStringAsync(jszipFileUri, jszip);
} catch (e) {
throw new Error('failed to write jszip js file');
}

try {
await FileSystem.writeAsStringAsync(epubjsFileUri, epubjs);
} catch (e) {
throw new Error('failed to write epubjs js file');
}

setAllowedUris(`${jszipFileUri},${epubjsFileUri}`);

if (src) {
const sourceType = getSourceType(src);
const isExternalSource = isURL(src);
const isSrcInFs = isFsUri(src);

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

if (!isExternalSource) {
if (isSrcInFs) {
setAllowedUris(`${src}${jszipFileUri},${epubjsFileUri}`);
}
if (sourceType === SourceType.BASE64) {
setTemplate(
injectBookVariables({
injectWebVieWVariables({
jszip: jszipFileUri,
epubjs: epubjsFileUri,
type: SourceType.BASE64,
book: src,
theme: defaultTheme,
Expand All @@ -62,7 +90,9 @@ export function Reader({
setIsLoading(false);
} else {
setTemplate(
injectBookVariables({
injectWebVieWVariables({
jszip: jszipFileUri,
epubjs: epubjsFileUri,
type: SourceType.BINARY,
book: src,
theme: defaultTheme,
Expand All @@ -82,9 +112,11 @@ export function Reader({
throw new Error(`Invalid source name: ${src}`);
}

if (sourceType === SourceType.OPF) {
if (sourceType === SourceType.OPF || sourceType === SourceType.EPUB) {
setTemplate(
injectBookVariables({
injectWebVieWVariables({
jszip: jszipFileUri,
epubjs: epubjsFileUri,
type: sourceType,
book: src,
theme: defaultTheme,
Expand All @@ -95,14 +127,18 @@ export function Reader({

setIsLoading(false);
} else {
const { uri: bookFile } = await downloadFile(src, sourceName);
const { uri: bookFileUri } = await downloadFile(src, sourceName);

if (!bookFile) throw new Error("Couldn't download book");
if (!bookFileUri) throw new Error("Couldn't download book");

setAllowedUris(`${bookFileUri},${jszipFileUri},${epubjsFileUri}`);

setTemplate(
injectBookVariables({
injectWebVieWVariables({
jszip: jszipFileUri,
epubjs: epubjsFileUri,
type: sourceType,
book: bookFile,
book: bookFileUri,
theme: defaultTheme,
locations: initialLocations,
enableSelection: true,
Expand All @@ -118,11 +154,30 @@ export function Reader({
defaultTheme,
downloadFile,
initialLocations,
injectBookVariables,
injectWebVieWVariables,
setIsLoading,
src,
]);

useEffect(() => {
const saveTemplateFileToDoc = async () => {
try {
if (template) {
const content = template;

const fileUri = `${FileSystem.documentDirectory}index.html`;
await FileSystem.writeAsStringAsync(fileUri, content);
setTemplateUrl(fileUri);
}
} catch (error) {
throw new Error('Error saving index.html file:');
}
};
if (template) {
saveTemplateFileToDoc();
}
}, [template]);

if (isLoading) {
return renderLoadingFileComponent({
fileSize,
Expand All @@ -132,6 +187,21 @@ export function Reader({
});
}

if (!template) throw new Error('Template is not set');
return <View template={template} width={width} height={height} {...rest} />;
if (!templateUrl || !allowedUris) {
return renderLoadingFileComponent({
fileSize,
downloadProgress,
downloadSuccess,
downloadError,
});
}
return (
<View
templateUri={templateUrl}
allowedUris={allowedUris}
width={width}
height={height}
{...rest}
/>
);
}
9 changes: 6 additions & 3 deletions src/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import type { ReaderProps } from './types';
import { OpeningBook } from './utils/OpeningBook';

export type ViewProps = Omit<ReaderProps, 'src' | 'fileSystem'> & {
template: string;
templateUri: string;
allowedUris: string;
};

export function View({
template,
templateUri,
allowedUris,
onStarted = () => {},
onReady = () => {},
onDisplayError = () => {},
Expand Down Expand Up @@ -264,13 +266,14 @@ export function View({
<TouchableWithoutFeedback onPress={handleDoublePress}>
<WebView
ref={book}
source={{ html: template, baseUrl: 'file:///' }}
source={{ uri: templateUri }}
showsVerticalScrollIndicator={false}
javaScriptEnabled
originWhitelist={['*']}
scrollEnabled={false}
mixedContentMode="compatibility"
onMessage={onMessage}
allowingReadAccessToURL={allowedUris}
allowUniversalAccessFromFileURLs
allowFileAccessFromFileURLs
allowFileAccess
Expand Down
Loading

0 comments on commit 8a4eae1

Please sign in to comment.