react-native-simple-epub-reader 0.1.0

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.
Files changed (90) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +37 -0
  3. package/lib/module/components/GestureHandler/index.js +39 -0
  4. package/lib/module/components/GestureHandler/index.js.map +1 -0
  5. package/lib/module/components/Reader.js +207 -0
  6. package/lib/module/components/Reader.js.map +1 -0
  7. package/lib/module/constants/template.js +402 -0
  8. package/lib/module/constants/template.js.map +1 -0
  9. package/lib/module/constants/theme.js +43 -0
  10. package/lib/module/constants/theme.js.map +1 -0
  11. package/lib/module/context/ReaderContext.js +213 -0
  12. package/lib/module/context/ReaderContext.js.map +1 -0
  13. package/lib/module/context/types.js +4 -0
  14. package/lib/module/context/types.js.map +1 -0
  15. package/lib/module/helpers/downloadEpub.js +17 -0
  16. package/lib/module/helpers/downloadEpub.js.map +1 -0
  17. package/lib/module/helpers/loadScripts.js +21 -0
  18. package/lib/module/helpers/loadScripts.js.map +1 -0
  19. package/lib/module/helpers/saveTemplateToFile.js +21 -0
  20. package/lib/module/helpers/saveTemplateToFile.js.map +1 -0
  21. package/lib/module/helpers/webViewInjectFunctions.js +14 -0
  22. package/lib/module/helpers/webViewInjectFunctions.js.map +1 -0
  23. package/lib/module/hooks/useInjectWebviewVariables.js +20 -0
  24. package/lib/module/hooks/useInjectWebviewVariables.js.map +1 -0
  25. package/lib/module/hooks/useReaderState.js +117 -0
  26. package/lib/module/hooks/useReaderState.js.map +1 -0
  27. package/lib/module/index.js +6 -0
  28. package/lib/module/index.js.map +1 -0
  29. package/lib/module/package.json +1 -0
  30. package/lib/module/scripts/epub.js +14326 -0
  31. package/lib/module/scripts/epub.js.map +1 -0
  32. package/lib/module/scripts/jszip.js +6646 -0
  33. package/lib/module/scripts/jszip.js.map +1 -0
  34. package/lib/module/types/index.js +14 -0
  35. package/lib/module/types/index.js.map +1 -0
  36. package/lib/module/types/state.types.js +23 -0
  37. package/lib/module/types/state.types.js.map +1 -0
  38. package/lib/typescript/package.json +1 -0
  39. package/lib/typescript/src/components/GestureHandler/index.d.ts +5 -0
  40. package/lib/typescript/src/components/GestureHandler/index.d.ts.map +1 -0
  41. package/lib/typescript/src/components/Reader.d.ts +4 -0
  42. package/lib/typescript/src/components/Reader.d.ts.map +1 -0
  43. package/lib/typescript/src/constants/template.d.ts +3 -0
  44. package/lib/typescript/src/constants/template.d.ts.map +1 -0
  45. package/lib/typescript/src/constants/theme.d.ts +3 -0
  46. package/lib/typescript/src/constants/theme.d.ts.map +1 -0
  47. package/lib/typescript/src/context/ReaderContext.d.ts +7 -0
  48. package/lib/typescript/src/context/ReaderContext.d.ts.map +1 -0
  49. package/lib/typescript/src/context/types.d.ts +60 -0
  50. package/lib/typescript/src/context/types.d.ts.map +1 -0
  51. package/lib/typescript/src/helpers/downloadEpub.d.ts +2 -0
  52. package/lib/typescript/src/helpers/downloadEpub.d.ts.map +1 -0
  53. package/lib/typescript/src/helpers/loadScripts.d.ts +2 -0
  54. package/lib/typescript/src/helpers/loadScripts.d.ts.map +1 -0
  55. package/lib/typescript/src/helpers/saveTemplateToFile.d.ts +3 -0
  56. package/lib/typescript/src/helpers/saveTemplateToFile.d.ts.map +1 -0
  57. package/lib/typescript/src/helpers/webViewInjectFunctions.d.ts +4 -0
  58. package/lib/typescript/src/helpers/webViewInjectFunctions.d.ts.map +1 -0
  59. package/lib/typescript/src/hooks/useInjectWebviewVariables.d.ts +12 -0
  60. package/lib/typescript/src/hooks/useInjectWebviewVariables.d.ts.map +1 -0
  61. package/lib/typescript/src/hooks/useReaderState.d.ts +6 -0
  62. package/lib/typescript/src/hooks/useReaderState.d.ts.map +1 -0
  63. package/lib/typescript/src/index.d.ts +4 -0
  64. package/lib/typescript/src/index.d.ts.map +1 -0
  65. package/lib/typescript/src/scripts/epub.d.ts +3 -0
  66. package/lib/typescript/src/scripts/epub.d.ts.map +1 -0
  67. package/lib/typescript/src/scripts/jszip.d.ts +3 -0
  68. package/lib/typescript/src/scripts/jszip.d.ts.map +1 -0
  69. package/lib/typescript/src/types/index.d.ts +72 -0
  70. package/lib/typescript/src/types/index.d.ts.map +1 -0
  71. package/lib/typescript/src/types/state.types.d.ts +83 -0
  72. package/lib/typescript/src/types/state.types.d.ts.map +1 -0
  73. package/package.json +171 -0
  74. package/src/components/GestureHandler/index.tsx +63 -0
  75. package/src/components/Reader.tsx +262 -0
  76. package/src/constants/template.ts +399 -0
  77. package/src/constants/theme.ts +42 -0
  78. package/src/context/ReaderContext.tsx +265 -0
  79. package/src/context/types.ts +62 -0
  80. package/src/helpers/downloadEpub.ts +21 -0
  81. package/src/helpers/loadScripts.ts +22 -0
  82. package/src/helpers/saveTemplateToFile.ts +20 -0
  83. package/src/helpers/webViewInjectFunctions.ts +18 -0
  84. package/src/hooks/useInjectWebviewVariables.ts +51 -0
  85. package/src/hooks/useReaderState.ts +118 -0
  86. package/src/index.tsx +3 -0
  87. package/src/scripts/epub.ts +14323 -0
  88. package/src/scripts/jszip.ts +6643 -0
  89. package/src/types/index.ts +83 -0
  90. package/src/types/state.types.ts +86 -0
@@ -0,0 +1,62 @@
1
+ import type WebView from 'react-native-webview';
2
+ import type { ePubCfi, Location, Theme } from '../types';
3
+
4
+ export type PaginateOptions = {
5
+ keepScrollOffset?: boolean;
6
+ };
7
+
8
+ export interface ReaderContextProps {
9
+ goToLocation: (cfi: ePubCfi) => void;
10
+ registerBook: (bookRef: WebView) => void;
11
+ goPrevious: (options?: PaginateOptions) => void;
12
+ goNext: (options?: PaginateOptions) => void;
13
+ setAtStart: (atStart: boolean) => void;
14
+ setAtEnd: (atEnd: boolean) => void;
15
+ setTotalLocations: (totalLocations: number) => void;
16
+ setCurrentLocation: (location: Location) => void;
17
+ setMeta: (meta: {
18
+ cover: string | ArrayBuffer | null | undefined;
19
+ author: string;
20
+ title: string;
21
+ description: string;
22
+ language: string;
23
+ publisher: string;
24
+ rights: string;
25
+ }) => void;
26
+ setProgress: (progress: number) => void;
27
+ setLocations: (locations: ePubCfi[]) => void;
28
+
29
+ getLocations: () => ePubCfi[];
30
+ getCurrentLocation: () => Location | null;
31
+ getMeta: () => {
32
+ cover: string | ArrayBuffer | null | undefined;
33
+ author: string;
34
+ title: string;
35
+ description: string;
36
+ language: string;
37
+ publisher: string;
38
+ rights: string;
39
+ };
40
+ theme: Theme;
41
+ atStart: boolean;
42
+ atEnd: boolean;
43
+ totalLocations: number;
44
+ currentLocation: Location | null;
45
+ meta: {
46
+ cover: string | ArrayBuffer | null | undefined;
47
+ author: string;
48
+ title: string;
49
+ description: string;
50
+ language: string;
51
+ publisher: string;
52
+ rights: string;
53
+ };
54
+ isLoading?: boolean;
55
+ setIsLoading: (isLoading: boolean) => void;
56
+ progress: number;
57
+ locations: ePubCfi[];
58
+ injectJavascript: (script: string) => void;
59
+ changeFontSize: (fontSize: string) => void;
60
+ changeTheme: (theme: Theme) => void;
61
+ fontSize: string;
62
+ }
@@ -0,0 +1,21 @@
1
+ import { File, Paths } from 'expo-file-system';
2
+
3
+ export const downloadEpub = async (
4
+ url: string,
5
+ fileName: string
6
+ ): Promise<string> => {
7
+ try {
8
+ const file = new File(Paths.document, fileName);
9
+
10
+ if (file.exists) {
11
+ return file.uri;
12
+ }
13
+
14
+ const downloadedFile = await File.downloadFileAsync(url, file);
15
+
16
+ return downloadedFile.uri;
17
+ } catch (error) {
18
+ console.error('Download Error:', error);
19
+ throw error;
20
+ }
21
+ };
@@ -0,0 +1,22 @@
1
+ import { Paths, File } from 'expo-file-system';
2
+ import jszip from '../scripts/jszip';
3
+ import epub from '../scripts/epub';
4
+
5
+ export const loadScripts = async () => {
6
+ const jszipFile = new File(Paths.document, 'jszip.min.js');
7
+ if (!jszipFile.exists) {
8
+ jszipFile.create();
9
+ jszipFile.write(jszip);
10
+ }
11
+
12
+ const epubjsFile = new File(Paths.document, 'epub.min.js');
13
+ if (!epubjsFile.exists) {
14
+ epubjsFile.create();
15
+ epubjsFile.write(epub);
16
+ }
17
+
18
+ const jszipFileUri = jszipFile.uri;
19
+ const epubjsFileUri = epubjsFile.uri;
20
+
21
+ return [`${jszipFileUri},${epubjsFileUri}`, jszipFileUri, epubjsFileUri];
22
+ };
@@ -0,0 +1,20 @@
1
+ import { File, Paths } from 'expo-file-system';
2
+
3
+ export const saveTemplateToFile = (template: string, fileName: string) => {
4
+ try {
5
+ const htmlFile = new File(Paths.document, fileName);
6
+ if (!htmlFile.exists) {
7
+ htmlFile.create();
8
+ }
9
+ htmlFile.write(template);
10
+ return htmlFile.uri;
11
+ } catch (error) {
12
+ console.error('Error saving template file:', error);
13
+ throw error;
14
+ }
15
+ };
16
+
17
+ export const checkTemplateFileExists = (fileName: string) => {
18
+ const htmlFile = new File(Paths.document, fileName);
19
+ return htmlFile.exists;
20
+ };
@@ -0,0 +1,18 @@
1
+ import type { RefObject } from 'react';
2
+
3
+ import WebView from 'react-native-webview';
4
+
5
+ export function injectJavaScript(
6
+ ref: RefObject<WebView | null>,
7
+ script: string
8
+ ) {
9
+ ref.current?.injectJavaScript(`
10
+ try {
11
+ ${script}
12
+ } catch (error) {
13
+ alert(error?.message);
14
+ }
15
+
16
+ true;
17
+ `);
18
+ }
@@ -0,0 +1,51 @@
1
+ import { useCallback } from 'react';
2
+
3
+ import type { SourceType, Theme } from '../types';
4
+ import template from '../constants/template';
5
+
6
+ export function useInjectWebViewVariables() {
7
+ const injectWebViewVariables = useCallback(
8
+ ({
9
+ jszip,
10
+ epubjs,
11
+ type,
12
+ book,
13
+ allowScriptedContent,
14
+ theme,
15
+ }: {
16
+ jszip: string;
17
+ epubjs: string;
18
+ type: SourceType;
19
+ book: string;
20
+ allowScriptedContent?: boolean;
21
+ theme: Theme;
22
+ }) => {
23
+ return template
24
+ .replace(
25
+ /<script id="jszip"><\/script>/,
26
+ `<script src="${jszip}"></script>`
27
+ )
28
+ .replace(
29
+ /<script id="epubjs"><\/script>/,
30
+ `<script src="${epubjs}"></script>`
31
+ )
32
+ .replace(/const type = window.type;/, `const type = '${type}';`)
33
+ .replace(/const file = window.book;/, `const file = '${book}';`)
34
+ .replace(
35
+ /const enableSelection = window.enable_selection;/,
36
+ `const enableSelection = false;`
37
+ )
38
+ .replace(
39
+ /allowScriptedContent: allowScriptedContent/,
40
+ `allowScriptedContent: ${allowScriptedContent}`
41
+ )
42
+ .replace(/allowPopups: allowPopups/, `allowPopups: false`)
43
+ .replace(
44
+ /const theme = window.theme;/,
45
+ `const theme = ${JSON.stringify(theme)};`
46
+ );
47
+ },
48
+ []
49
+ );
50
+ return { injectWebViewVariables };
51
+ }
@@ -0,0 +1,118 @@
1
+ import { defaultTheme } from '../constants/theme';
2
+ import {
3
+ Actions,
4
+ type BookActions,
5
+ type InitialState,
6
+ } from '../types/state.types';
7
+
8
+ export const useReaderState = () => {
9
+ const initialState: InitialState = {
10
+ theme: defaultTheme,
11
+ fontFamily: 'Helvetica',
12
+ fontSize: '9pt',
13
+ atStart: false,
14
+ atEnd: false,
15
+ key: '',
16
+ totalLocations: 0,
17
+ currentLocation: null,
18
+ meta: {
19
+ cover: '',
20
+ author: '',
21
+ title: '',
22
+ description: '',
23
+ language: '',
24
+ publisher: '',
25
+ rights: '',
26
+ },
27
+ progress: 0,
28
+ locations: [],
29
+ isLoading: true,
30
+ isRendering: true,
31
+ section: null,
32
+ flow: 'paginated',
33
+ };
34
+
35
+ function bookReducer(state: InitialState, action: BookActions): InitialState {
36
+ switch (action.type) {
37
+ case Actions.CHANGE_FONT_SIZE:
38
+ return {
39
+ ...state,
40
+ fontSize: action.payload,
41
+ };
42
+ case Actions.SET_AT_START:
43
+ return {
44
+ ...state,
45
+ atStart: action.payload,
46
+ };
47
+ case Actions.SET_AT_END:
48
+ return {
49
+ ...state,
50
+ atEnd: action.payload,
51
+ };
52
+ case Actions.SET_KEY:
53
+ return {
54
+ ...state,
55
+ key: action.payload,
56
+ };
57
+ case Actions.SET_TOTAL_LOCATIONS:
58
+ return {
59
+ ...state,
60
+ totalLocations: action.payload,
61
+ };
62
+ case Actions.SET_CURRENT_LOCATION:
63
+ return {
64
+ ...state,
65
+ currentLocation: action.payload,
66
+ };
67
+ case Actions.SET_META:
68
+ return {
69
+ ...state,
70
+ meta: action.payload,
71
+ };
72
+ case Actions.SET_PROGRESS:
73
+ return {
74
+ ...state,
75
+ progress: action.payload,
76
+ };
77
+ case Actions.SET_LOCATIONS:
78
+ return {
79
+ ...state,
80
+ locations: action.payload,
81
+ };
82
+ case Actions.SET_IS_LOADING:
83
+ return {
84
+ ...state,
85
+ isLoading: action.payload,
86
+ };
87
+ case Actions.SET_IS_RENDERING:
88
+ return {
89
+ ...state,
90
+ isRendering: action.payload,
91
+ };
92
+ case Actions.SET_SECTION:
93
+ return {
94
+ ...state,
95
+ section: action.payload,
96
+ };
97
+ case Actions.SET_FLOW:
98
+ return {
99
+ ...state,
100
+ flow: action.payload,
101
+ };
102
+ case Actions.CHANGE_THEME:
103
+ return {
104
+ ...state,
105
+ theme: action.payload,
106
+ };
107
+ case Actions.CHANGE_FONT_FAMILY:
108
+ return {
109
+ ...state,
110
+ fontFamily: action.payload,
111
+ };
112
+ default:
113
+ return state;
114
+ }
115
+ }
116
+
117
+ return { initialState, bookReducer };
118
+ };
package/src/index.tsx ADDED
@@ -0,0 +1,3 @@
1
+ export { default as Reader } from './components/Reader';
2
+ export { ReaderProvider, ReaderContext } from './context/ReaderContext';
3
+ export * from './types';