Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added a new property to the useReader hook #73

Merged
merged 5 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions src/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function View({
registerBook,
setTotalLocations,
setCurrentLocation,
setMeta,
setProgress,
setLocations,
setAtStart,
Expand All @@ -72,6 +73,11 @@ export function View({

delete parsedEvent.type;

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

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

Expand Down
40 changes: 40 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,7 @@ 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 +71,7 @@ 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 +114,7 @@ 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 +164,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 +205,7 @@ 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 +238,12 @@ 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 +324,12 @@ 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 +365,7 @@ const ReaderContext = createContext<ReaderContextProps>({
setAtEnd: () => {},
setTotalLocations: () => {},
setCurrentLocation: () => {},
setMeta: () => {},
setProgress: () => {},
setLocations: () => {},
setIsLoading: () => {},
Expand All @@ -353,6 +376,7 @@ const ReaderContext = createContext<ReaderContextProps>({
goNext: () => {},
getLocations: () => [],
getCurrentLocation: () => null,
getMeta: () => ({ cover: '', author: '', title: '', description: '', language: '', publisher: '', rights: '', }),
search: () => {},

changeTheme: () => {},
Expand All @@ -370,6 +394,7 @@ 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 +451,13 @@ 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 +492,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 +555,7 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
setAtEnd,
setTotalLocations,
setCurrentLocation,
setMeta,
setProgress,
setLocations,
setIsLoading,
Expand All @@ -531,6 +566,7 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
goNext,
getLocations,
getCurrentLocation,
getMeta,
search,

addMark,
Expand All @@ -548,6 +584,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 +599,7 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
changeFontSize,
changeTheme,
getCurrentLocation,
getMeta,
getLocations,
goNext,
goPrevious,
Expand All @@ -572,6 +610,7 @@ function ReaderProvider({ children }: { children: React.ReactNode }) {
setAtEnd,
setAtStart,
setCurrentLocation,
setMeta,
setIsLoading,
setIsRendering,
setKey,
Expand All @@ -582,6 +621,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
39 changes: 39 additions & 0 deletions src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,45 @@ export default `
}));
});

book
.coverUrl()
.then(async (url) => {
var reader = new FileReader();
reader.onload = (res) => {
window.ReactNativeWebView.postMessage(
JSON.stringify({
type: "meta",
metadata: {
cover: reader.result,
author: book.package.metadata.creator,
title: book.package.metadata.title,
description: book.package.metadata.description,
language: book.package.metadata.language,
publisher: book.package.metadata.publisher,
rights: book.package.metadata.rights,
},
})
);
};
reader.readAsDataURL(await fetch(url).then((res) => res.blob()));
})
.catch(() => {
window.ReactNativeWebView.postMessage(
JSON.stringify({
type: "meta",
metadata: {
cover: undefined,
author: book.package.metadata.creator,
title: book.package.metadata.title,
description: book.package.metadata.description,
language: book.package.metadata.language,
publisher: book.package.metadata.publisher,
rights: book.package.metadata.rights,
},
})
);
});

window.ReactNativeWebView.postMessage(JSON.stringify({
type: "onLocationsReady",
epubKey: book.key(),
Expand Down