1.2.0 • Published 11 months ago

@shlomizadok-epubjs-react-native/core v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

epubjs-react-native

A digital book reader in .opf .epub format for react native using epub.js library inside a webview.

Installation

npm install @epubjs-react-native/core
yarn add @epubjs-react-native/core

follow these steps below based on the type of project you want to apply this library:

Bare Installation

npm install @epubjs-react-native/file-system react-native-fs react-native-webview react-native-gesture-handler
yarn add @epubjs-react-native/file-system react-native-fs react-native-webview react-native-gesture-handler

Expo Installation

expo install @epubjs-react-native/expo-file-system react-native-fs react-native-webview react-native-gesture-handler

If you develop for iOS use this command for install CocoaPods deps (if you use an expo managed project you don't need this)

In your ios project folder run:

npx pod install

Usage

import * as React from 'react';

import { SafeAreaView, useWindowDimensions } from 'react-native';
import { Reader, ReaderProvider } from '@epubjs-react-native/core';
// import { useFileSystem } from '@epubjs-react-native/file-system'; // for Bare React Native project
// import { useFileSystem } from '@epubjs-react-native/expo-file-system'; // for Expo project

export default function App() {
  const { width, height } = useWindowDimensions();
  return (
    <SafeAreaView>
      <ReaderProvider>
        <Reader
          src="https://s3.amazonaws.com/moby-dick/OPS/package.opf"
          width={width}
          height={height}
          fileSystem={useFileSystem}
        />
      </ReaderProvider>
    </SafeAreaView>
  );
}

Reader Params

ParamTypeDescription
srcstringCan be a base64, epub, opf. Required.
widthnumberThe width of the ePub Rendition. Required.
heightnumberThe height of the ePub Rendition. Required.
fileSystemfunctionA function that returns a FileSystem object. Required.
initialLocationePubCfiCan be an ePubCfi or chapter url. Optional.
enableSwipebooleanEnable swipe actions. Default is true.
onSwipeLeftfunctionCalled when swipe left gesture is detected. Optional.
onSwipeRightfunctionCalled when swipe right gesture is detected. Optional.
renderLoadingFileComponentJSX.ElementRender when the book is loading. Optional.
renderOpeningBookComponentJSX.ElementAppears when the book is been rendering. Optional.
onStartedfunctionCalled once the book loads is started. Optional.
onReadyfunctionCalled once book has been displayed. Optional.
onDisplayErrorfunctionCalled once book has not been displayed. Optional.
onRenderedfunctionEmit that a section has been rendered. Optional.
onResizedfunctionCalled when occurred a page change. Optional.
onLocationChangefunctionCalled when occurred a page change. Optional.
onSearchfunctionCalled once when the book has been searched. Optional.
onLocationsReadyfunctionCalled once the locations has been generated. Optional.
onSelectedfunctionCalled once a text selection has occurred. Optional.
onMarkPressedfunctionCalled when marked text is pressed. Optional.
onOrientationChangefunctionCalled when screen orientation change is detected. Optional.
onPressfunctionCalled when the book was pressed. Optional.
onDoublePressfunctionCalled when the book was double pressed. Optional.
onBeginningfunctionCalled when the book is on the homepage. Optional.
onFinishfunctionCalled when the book is on the final page. Optional.
onLayoutfunctionCalled when book layout is change. Optional.
defaultThemeobjectTheme object. Optional.

Hooks

useReader

useReader() is a customized hook that will return all Book states and the methods that will help you. Make sure you use it within the ReaderProvider

const { changeFontSize, goToLocation, ... } = useReader();
Methods
MethodReceivesDescription
changeFontSizesizeChange font size of all elements in the book. Can be a px, pt or percent.
changeFontFamilyfontChange font family of all elements in the book
goToLocationcfiGo to specific location in the book
getLocationsGet the total locations of the book
goPreviousGo to previous page in the book
goNextGo to next page in the book
searchquerySearch for a specific text in the book
changeThemethemeChange active theme
getCurrentLocationReturns the current location of the book
addMarkAdd mark a specific cfi in the book
removeMarkRemove mark a specific cfi in the book
getMetaReturns 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
  • theme: A object containing theme.
  • key: Works like a unique id for book.
  • atStart: Indicates if you are at the beginning of the book.
  • atEnd: Indicates if you are at the end of the book.
  • currentLocation: The current location of the book.
  • totalLocations: The total number of locations.
  • 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

You can see the examples in these repositories:

Did you like this project? Consider sponsoring the development of this project to keep it alive! ❤️

License

MIT