1.4.2 • Published 4 days ago

@epubjs-react-native/core v1.4.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 days 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

npx 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

For bare react-native projects, add the following permissions to Info.plist:

<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>

Usage

Wrap your root component in ReaderProvider. If you have a vanilla React Native project, it's a good idea to add it in the component which is passed to AppRegistry.registerComponent. This will usually be in the index.js file. If you have an Expo project, you can do this inside the exported component in the App.js file.

import * as React from 'react';
import { AppRegistry } from 'react-native';
import { ReaderProvider } from '@epubjs-react-native/core';
import { name as appName } from './app.json';
import App from './src/App';

export default function Main() {
  return (
    <ReaderProvider>
      <App />
    </ReaderProvider>
  );
}

AppRegistry.registerComponent(appName, () => Main);

The <ReaderProvider> component provides <Reader> manipulation methods for all components of the framework. Without this the useReader hook will not work.

After the above step you can initialize the reader as follows:

import * as React from 'react';

import { SafeAreaView } from 'react-native';
import { Reader, useReader } 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 { goToLocation } = useReader();
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <Reader
        src="https://s3.amazonaws.com/moby-dick/OPS/package.opf"
        fileSystem={useFileSystem}
      />
    </SafeAreaView>
  );
}

Reader Params

ParamTypeDescription
srcstringCan be a base64, epub, opf. Required.
widthnumber stringThe width of the ePub Rendition. Optional.
heightnumber stringThe height of the ePub Rendition. Optional.
fileSystemfunctionA function that returns a FileSystem object. Required.
initialLocationePubCfiCan be an ePubCfi or chapter url. Optional.
enableSwipebooleanEnable swipe actions. Default is true.
enableSelectionbooleanEnable text selection feature on the book. 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.
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.
allowScriptedContentbooleanAllow run scripted content on sandbox. Default is false on Android and true in iOS
allowPopupsbooleanEpubjs is rendering the epub-content inside and iframe which defaults to sandbox="allow-same-origin", to enable opening links or running javascript in an epub, you will need to pass this param.
onPressExternalLinkfunctionFunction that is invoked when external link is pressed. When used, the allowPopups property is automatically enabled
menuItemsarrayAn array of objects which will be shown when selecting text. An empty array will suppress the menu.
onAddAnnotationfunctionFunction that is invoked when annotation is added in book.
onChangeAnnotationsfunctionFunction that is invoked when annotations array is modified.
onPressAnnotationfunctionFunction that is invoked when annotation is pressed.
initialAnnotationsarrayUsed for load book with annotations attached
onAddBookmarkfunction
onRemoveBookmarkfunction
onRemoveBookmarksfunction
onUpdateBookmarkfunction
onChangeBookmarksfunctionFunction that is invoked when bookmarks array is modified.
initialBookmarksarrayload bookmarks after render book
injectedJavascriptstringProvide JavaScript that will be injected after the book loads.
getInjectionJavascriptFnfunctionProvides injectJavascript function, is same the webView function
onWebViewMessagefunctionA webview event listener, is same the webView onMessage
managerdefault continuous
flowauto paginated scrolled scrolled-doc scrolled-continuous
snapboolean
spreadauto none always
fullsizeboolean
waitForLocationsReadybooleanonly render book after locations generated
keepScrollOffsetOnLocationChangebooleanPrevents scroll top when change location. Works with scrolled-doc flow.

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
goPreviousPaginateOptionsGo to previous page in the book
goNextPaginateOptionsGo to next page in the book
searchquerySearch for a specific text in the book
clearSearchResultsvoidClear search results
changeThemethemeChange active theme
getCurrentLocationReturns the current location of the book
getMetaReturns an object containing the book's metadata.
addAnnotationannotationAttach annotation in the book.
updateAnnotationannotation, data, stylesUpdate annotation data and style
removeAnnotationannotationDetach annotation in the book.
removeAnnotationByCfi cfiDetach annotations in the book by provided cfi.
removeAnnotationstype?: optionalDetach all annotations in the book. Can be detach by type
removeSelectionRemove selection
addBookmarkbookmarkAttach bookmark
updateBookmarkid, dataUpdate bookmark data
removeBookmarkbookmarkRemove bookmark
removeBookmarksRemove all bookmarks
injectJavascriptfunctionProvide JavaScript that will be injected after the book loads.
changeFlowFlowAdjust the flow of the rendition to paginated or scrolled (scrolled-continuous vs scrolled-doc are handled by different view managers)
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.
  • annotations: A array containing the book's annotations.
  • section: A current section(chapter) of the book.
  • toc: Also called table of contents, is an ordered list of links into the content.
  • landmarks: Containing a array of major sections of the content
  • bookmarks: A array containing the bookmarks.
  • isBookmarked: Indicates if current location is bookmarked.

The meta 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.

Examples

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

License

MIT

1.4.2

4 days ago

1.4.1

16 days ago

1.4.0

20 days ago

1.4.0-beta.50

20 days ago

1.4.0-beta.49

23 days ago

1.4.0-beta.48

25 days ago

1.4.0-beta.47

28 days ago

1.4.0-beta.46

29 days ago

1.4.0-beta.45

30 days ago

1.4.0-beta.44

1 month ago

1.4.0-beta.43

1 month ago

1.4.0-beta.42

1 month ago

1.4.0-alpha.0

1 month ago

1.4.0-alpha.1

1 month ago

1.4.0-alpha.2

1 month ago

1.4.0-alpha.3

1 month ago

1.4.0-alpha.4

1 month ago

1.4.0-beta.41

1 month ago

1.4.0-beta.40

1 month ago

1.4.0-beta.39

1 month ago

1.4.0-staging.34

2 months ago

1.4.0-staging.32

2 months ago

1.4.0-staging.33

2 months ago

1.4.0-staging.30

2 months ago

1.4.0-staging.31

2 months ago

1.4.0-staging.29

2 months ago

1.4.0-staging.27

2 months ago

1.4.0-staging.28

2 months ago

1.4.0-staging.25

2 months ago

1.4.0-staging.26

2 months ago

1.4.0-staging.23

2 months ago

1.4.0-staging.24

2 months ago

1.4.0-staging.21

2 months ago

1.4.0-staging.22

2 months ago

1.4.0-staging.20

2 months ago

1.4.0-staging.18

2 months ago

1.4.0-staging.19

2 months ago

1.4.0-staging.16

2 months ago

1.4.0-staging.17

2 months ago

1.4.0-staging.15

2 months ago

1.4.0-beta.24

2 months ago

1.4.0-beta.25

2 months ago

1.4.0-beta.26

2 months ago

1.4.0-beta.27

2 months ago

1.4.0-beta.20

2 months ago

1.4.0-beta.21

2 months ago

1.4.0-beta.22

2 months ago

1.4.0-beta.23

2 months ago

1.4.0-beta.28

2 months ago

1.4.0-beta.29

2 months ago

1.4.0-staging.9

2 months ago

1.4.0-staging.7

2 months ago

1.4.0-staging.8

2 months ago

1.4.0-beta.13

2 months ago

1.4.0-beta.14

2 months ago

1.4.0-beta.15

2 months ago

1.4.0-beta.16

2 months ago

1.4.0-beta.17

2 months ago

1.4.0-beta.18

2 months ago

1.4.0-beta.19

2 months ago

1.4.0-staging.1

2 months ago

1.4.0-staging.2

2 months ago

1.4.0-staging.0

2 months ago

1.4.0-staging.5

2 months ago

1.4.0-staging.6

2 months ago

1.4.0-staging.3

2 months ago

1.4.0-staging.4

2 months ago

1.4.0-beta.30

2 months ago

1.4.0-staging.14

2 months ago

1.4.0-staging.12

2 months ago

1.4.0-staging.13

2 months ago

1.4.0-staging.10

2 months ago

1.4.0-beta.35

2 months ago

1.4.0-staging.11

2 months ago

1.4.0-beta.36

2 months ago

1.4.0-beta.37

2 months ago

1.4.0-beta.38

2 months ago

1.4.0-beta.31

2 months ago

1.4.0-beta.32

2 months ago

1.4.0-beta.33

2 months ago

1.4.0-beta.34

2 months ago

1.4.0-beta.10

2 months ago

1.4.0-beta.11

2 months ago

1.4.0-beta.12

2 months ago

1.4.0-beta.9

2 months ago

1.4.0-beta.7

2 months ago

1.4.0-beta.6

2 months ago

1.4.0-beta.5

2 months ago

1.4.0-beta.4

2 months ago

1.4.0-beta.3

2 months ago

1.4.0-beta.2

2 months ago

1.4.0-beta.1

2 months ago

1.4.0-beta.0

2 months ago

1.3.0

8 months ago

1.2.0

11 months ago

1.1.0

2 years ago

0.0.0-20220813-2

2 years ago

0.0.0-20220813

2 years ago