Skip to content

Commit

Permalink
Merge pull request #93 from victorsoares96/v1.2.0
Browse files Browse the repository at this point in the history
V1.2.0
  • Loading branch information
victorsoares96 authored Jun 7, 2023
2 parents e7a8068 + 9cdb582 commit 393b608
Show file tree
Hide file tree
Showing 8 changed files with 648 additions and 262 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ const { changeFontSize, goToLocation, ... } = useReader();
| `getCurrentLocation` | | Returns the current location of the book |
| `addMark` | | Add mark a specific cfi in the book |
| `removeMark` | | Remove mark a specific cfi in the book |
`getMeta` | | Returns an object containing the book's metadata.

The metadata object contains:
- **cover** *(string, ArrayBuffer, null or undefined)*: The book's cover image `e.g.data:image/jpeg;base64,/9j/4AAQSkZJ...`
- **author** *(string)*: The name of the book's creator/author `e.g. Herman Melville`
- **title** *(string)*: The book's title `e.g. Moby-Dick`
- **description** *(string)*: The book's description/summary.
- **language** *(string)* : The book's language `e.g. en-US`
- **publisher** *(string)*: The eBook's publisher `e.g. Harper & Brothers, Publishers`
- **rights** *(string)*: The book's rights `e.g. This work is shared with the public using the Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.`


##### States

Expand All @@ -134,6 +145,7 @@ const { changeFontSize, goToLocation, ... } = useReader();
- `progress`: The progress of the book.
- `isLoading`: Indicates if the book is loading.
- `searchResults`: Search results.
- `meta`: A object containing the book's metadata.

#### Examples

Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@epubjs-react-native/core",
"version": "1.1.0",
"version": "1.2.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 @@ -60,28 +60,28 @@
"@formidable-webview/ersatz": "2.1.3",
"@formidable-webview/ersatz-testing": "2.0.5",
"@react-native-community/eslint-config": "3.0.3",
"@testing-library/jest-native": "4.0.5",
"@testing-library/jest-native": "4.0.13",
"@testing-library/react-native": "11.0.0",
"@types/jest": "28.1.6",
"@types/jest": "28.1.8",
"@types/react": "17.0.21",
"@types/react-native": "0.68.0",
"@types/react-test-renderer": "17.0.1",
"@types/react-test-renderer": "17.0.2",
"@typescript-eslint/eslint-plugin": "5.13.0",
"@typescript-eslint/parser": "5.0.0",
"commitizen": "4.2.5",
"commitizen-emoji": "1.0.5",
"eslint": "8.2.0",
"eslint": "8.42.0",
"eslint-config-airbnb": "19.0.4",
"eslint-config-airbnb-typescript": "17.0.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.25.3",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-jest": "^26.7.0",
"eslint-plugin-jsx-a11y": "6.5.1",
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-react": "7.28.0",
"eslint-plugin-react-hooks": "4.3.0",
"eslint-plugin-testing-library": "5.5.1",
"husky": "8.0.1",
"husky": "8.0.3",
"jest": "28.1.3",
"lint-staged": "13.0.3",
"pod-install": "0.1.0",
Expand All @@ -92,7 +92,7 @@
"react-native-gesture-handler": "2.1.1",
"react-native-webview": "11.15.0",
"react-test-renderer": "17.0.2",
"ts-jest": "28.0.7",
"ts-jest": "28.0.8",
"ts-node": "10.9.1",
"typescript": "4.4.4"
},
Expand Down
16 changes: 13 additions & 3 deletions src/View.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import React, { useContext, useEffect, useRef } from 'react';
import { TouchableWithoutFeedback, View as RNView } from 'react-native';
import {
TouchableWithoutFeedback,
I18nManager,
View as RNView,
} from 'react-native';
import {
Directions,
FlingGestureHandler,
Expand Down Expand Up @@ -49,6 +53,7 @@ export function View({
registerBook,
setTotalLocations,
setCurrentLocation,
setMeta,
setProgress,
setLocations,
setAtStart,
Expand All @@ -72,6 +77,11 @@ export function View({

delete parsedEvent.type;

if (type === 'meta') {
const { metadata } = parsedEvent;
setMeta(metadata);
}

if (type === 'onStarted') {
setIsRendering(true);

Expand Down Expand Up @@ -213,7 +223,7 @@ export function View({
return (
<GestureHandlerRootView style={{ width, height }}>
<FlingGestureHandler
direction={Directions.RIGHT}
direction={I18nManager.isRTL ? Directions.LEFT : Directions.RIGHT}
onHandlerStateChange={({ nativeEvent }) => {
if (nativeEvent.state === State.ACTIVE && enableSwipe) {
goPrevious();
Expand All @@ -222,7 +232,7 @@ export function View({
}}
>
<FlingGestureHandler
direction={Directions.LEFT}
direction={I18nManager.isRTL ? Directions.RIGHT : Directions.LEFT}
onHandlerStateChange={({ nativeEvent }) => {
if (nativeEvent.state === State.ACTIVE && enableSwipe) {
goNext();
Expand Down
112 changes: 112 additions & 0 deletions src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ enum Types {
SET_KEY = 'SET_KEY',
SET_TOTAL_LOCATIONS = 'SET_TOTAL_LOCATIONS',
SET_CURRENT_LOCATION = 'SET_CURRENT_LOCATION',
SET_META = 'SET_META',
SET_PROGRESS = 'SET_PROGRESS',
SET_LOCATIONS = 'SET_LOCATIONS',
SET_IS_LOADING = 'SET_IS_LOADING',
Expand All @@ -51,6 +52,15 @@ type BookPayload = {
[Types.SET_KEY]: string;
[Types.SET_TOTAL_LOCATIONS]: number;
[Types.SET_CURRENT_LOCATION]: Location;
[Types.SET_META]: {
cover: string | ArrayBuffer | null | undefined;
author: string;
title: string;
description: string;
language: string;
publisher: string;
rights: string;
};
[Types.SET_PROGRESS]: number;
[Types.SET_LOCATIONS]: ePubCfi[];
[Types.SET_IS_LOADING]: boolean;
Expand All @@ -69,6 +79,15 @@ type InitialState = {
key: string;
totalLocations: number;
currentLocation: Location | null;
meta: {
cover: string | ArrayBuffer | null | undefined;
author: string;
title: string;
description: string;
language: string;
publisher: string;
rights: string;
};
progress: number;
locations: ePubCfi[];
isLoading: boolean;
Expand Down Expand Up @@ -111,6 +130,15 @@ const initialState: InitialState = {
key: '',
totalLocations: 0,
currentLocation: null,
meta: {
cover: '',
author: '',
title: '',
description: '',
language: '',
publisher: '',
rights: '',
},
progress: 0,
locations: [],
isLoading: true,
Expand Down Expand Up @@ -160,6 +188,11 @@ function bookReducer(state: InitialState, action: BookActions): InitialState {
...state,
currentLocation: action.payload,
};
case Types.SET_META:
return {
...state,
meta: action.payload,
};
case Types.SET_PROGRESS:
return {
...state,
Expand Down Expand Up @@ -196,6 +229,15 @@ export interface ReaderContextProps {
setAtEnd: (atEnd: boolean) => void;
setTotalLocations: (totalLocations: number) => void;
setCurrentLocation: (location: Location) => void;
setMeta: (meta: {
cover: string | ArrayBuffer | null | undefined;
author: string;
title: string;
description: string;
language: string;
publisher: string;
rights: string;
}) => void;
setProgress: (progress: number) => void;
setLocations: (locations: ePubCfi[]) => void;
setIsLoading: (isLoading: boolean) => void;
Expand Down Expand Up @@ -228,6 +270,20 @@ export interface ReaderContextProps {
*/
getCurrentLocation: () => Location | null;

/**
* Returns an object containing the book's metadata
* @returns { cover: string | ArrayBuffer | null | undefined, author: string, title: string, description: string, language: string, publisher: string, rights: string, }
*/
getMeta: () => {
cover: string | ArrayBuffer | null | undefined;
author: string;
title: string;
description: string;
language: string;
publisher: string;
rights: string;
};

/**
* Search for a specific text in the book
* @param {string} query {@link string} text to search
Expand Down Expand Up @@ -308,6 +364,20 @@ export interface ReaderContextProps {
*/
currentLocation: Location | null;

/**
* An object containing the book's metadata
* { cover: string | ArrayBuffer | null | undefined, author: string, title: string, description: string, language: string, publisher: string, rights: string, }
*/
meta: {
cover: string | ArrayBuffer | null | undefined;
author: string;
title: string;
description: string;
language: string;
publisher: string;
rights: string;
};

/**
* The progress of the book
* @returns {number} {@link number}
Expand Down Expand Up @@ -343,6 +413,7 @@ const ReaderContext = createContext<ReaderContextProps>({
setAtEnd: () => {},
setTotalLocations: () => {},
setCurrentLocation: () => {},
setMeta: () => {},
setProgress: () => {},
setLocations: () => {},
setIsLoading: () => {},
Expand All @@ -353,6 +424,15 @@ const ReaderContext = createContext<ReaderContextProps>({
goNext: () => {},
getLocations: () => [],
getCurrentLocation: () => null,
getMeta: () => ({
cover: '',
author: '',
title: '',
description: '',
language: '',
publisher: '',
rights: '',
}),
search: () => {},

changeTheme: () => {},
Expand All @@ -370,6 +450,15 @@ const ReaderContext = createContext<ReaderContextProps>({
atEnd: false,
totalLocations: 0,
currentLocation: null,
meta: {
cover: '',
author: '',
title: '',
description: '',
language: '',
publisher: '',
rights: '',
},
progress: 0,
locations: [],
isLoading: true,
Expand Down Expand Up @@ -426,6 +515,21 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
dispatch({ type: Types.SET_CURRENT_LOCATION, payload: location });
}, []);

const setMeta = useCallback(
(meta: {
cover: string | ArrayBuffer | null | undefined;
author: string;
title: string;
description: string;
language: string;
publisher: string;
rights: string;
}) => {
dispatch({ type: Types.SET_META, payload: meta });
},
[]
);

const setProgress = useCallback((progress: number) => {
dispatch({ type: Types.SET_PROGRESS, payload: progress });
}, []);
Expand Down Expand Up @@ -460,6 +564,8 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
state.currentLocation,
]);

const getMeta = useCallback(() => state.meta, [state.meta]);

const search = useCallback((query: string) => {
book.current?.injectJavaScript(`
Promise.all(
Expand Down Expand Up @@ -521,6 +627,7 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
setAtEnd,
setTotalLocations,
setCurrentLocation,
setMeta,
setProgress,
setLocations,
setIsLoading,
Expand All @@ -531,6 +638,7 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
goNext,
getLocations,
getCurrentLocation,
getMeta,
search,

addMark,
Expand All @@ -548,6 +656,7 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
atEnd: state.atEnd,
totalLocations: state.totalLocations,
currentLocation: state.currentLocation,
meta: state.meta,
progress: state.progress,
locations: state.locations,
isLoading: state.isLoading,
Expand All @@ -562,6 +671,7 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
changeFontSize,
changeTheme,
getCurrentLocation,
getMeta,
getLocations,
goNext,
goPrevious,
Expand All @@ -572,6 +682,7 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
setAtEnd,
setAtStart,
setCurrentLocation,
setMeta,
setIsLoading,
setIsRendering,
setKey,
Expand All @@ -582,6 +693,7 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
state.atEnd,
state.atStart,
state.currentLocation,
state.meta,
state.isLoading,
state.isRendering,
state.key,
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/useReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function useReader() {
goNext,
getLocations,
getCurrentLocation,
getMeta,
search,
addMark,
removeMark,
Expand All @@ -35,6 +36,7 @@ export function useReader() {
goNext,
getLocations,
getCurrentLocation,
getMeta,
search,
addMark,
removeMark,
Expand All @@ -58,6 +60,7 @@ export function useReader() {
| 'goNext'
| 'getLocations'
| 'getCurrentLocation'
| 'getMeta'
| 'search'
| 'addMark'
| 'removeMark'
Expand Down
Loading

0 comments on commit 393b608

Please sign in to comment.