Merge pull request #2 from altmshfkgudtjr/demo

Add ViewerRef type
main
NB 4 years ago committed by GitHub
commit b199718edb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      package.json
  2. 3
      src/containers/Reader.tsx
  3. 14
      src/modules/epubViewer/EpubViewer.tsx
  4. 17
      src/modules/reactViewer/ReactViewer.tsx
  5. 38
      src/types/index.d.ts

@ -91,5 +91,5 @@
"last 1 safari version" "last 1 safari version"
] ]
}, },
"types": "dist/types/index.d.ts" "types": "lib/types/index.d.ts"
} }

@ -24,6 +24,7 @@ import 'lib/styles/readerStyle.css'
import viewerLayout from 'lib/styles/viewerLayout' import viewerLayout from 'lib/styles/viewerLayout'
// types // types
import { RootState } from 'slices' import { RootState } from 'slices'
import { ViewerRef } from 'types'
import Book, { BookStyle, BookOption } from 'types/book' import Book, { BookStyle, BookOption } from 'types/book'
import Page from 'types/page' import Page from 'types/page'
import Toc from 'types/toc' import Toc from 'types/toc'
@ -34,7 +35,7 @@ const Reader = ({ url, loadingView }: Props) => {
const book = useSelector<RootState, Book>(state => state.book.book); const book = useSelector<RootState, Book>(state => state.book.book);
const currentLocation = useSelector<RootState, Page>(state => state.book.currentLocation); const currentLocation = useSelector<RootState, Page>(state => state.book.currentLocation);
const viewerRef = useRef<any>(null); const viewerRef = useRef<ViewerRef | null>(null);
const navRef = useRef<HTMLDivElement | null>(null); const navRef = useRef<HTMLDivElement | null>(null);
const optionRef = useRef<HTMLDivElement | null>(null); const optionRef = useRef<HTMLDivElement | null>(null);
const learningRef = useRef<HTMLDivElement | null>(null); const learningRef = useRef<HTMLDivElement | null>(null);

@ -3,7 +3,7 @@ import { Book, Rendition } from "epubjs";
// style // style
import EpubViewerWrapper from 'modules/epubViewer/style' import EpubViewerWrapper from 'modules/epubViewer/style'
// types // types
import { EpubViewerProps } from 'types' import { EpubViewerProps, ViewerRef } from 'types'
import Toc from 'types/toc' import Toc from 'types/toc'
import Loc from 'types/loc' import Loc from 'types/loc'
@ -37,7 +37,8 @@ const EpubViewer = ({
tocChanged, tocChanged,
selectionChanged, selectionChanged,
loadingView, loadingView,
}: EpubViewerProps, ref: any) => { }: EpubViewerProps, ref: React.RefObject<ViewerRef> | any) => {
// TODO Fix the ref type correctly instead 'any' type.
const [isLoaded, setIsLoaded] = useState(false); const [isLoaded, setIsLoaded] = useState(false);
const [book, setBook] = useState<Book | null>(null); const [book, setBook] = useState<Book | null>(null);
@ -229,11 +230,17 @@ const EpubViewer = ({
/** Ref checker */
useEffect(() => {
if (!ref) {
throw new Error("[React-Epub-Viewer] Put a ref argument that has a ViewerRef type.");
}
}, [ref]);
/** Epub Init */ /** Epub Init */
/* eslint-disable */ /* eslint-disable */
useEffect(() => { useEffect(() => {
if (!url) return; if (!url) return;
initBook(); initBook();
}, [url]); }, [url]);
/* eslint-enable */ /* eslint-enable */
@ -277,7 +284,6 @@ const EpubViewer = ({
}, [location, rendition]); }, [location, rendition]);
return (<> return (<>
{!isLoaded && loadingView} {!isLoaded && loadingView}
<EpubViewerWrapper ref={ref} style={style} /> <EpubViewerWrapper ref={ref} style={style} />

@ -1,4 +1,4 @@
import React, { useState, useEffect, useRef, useCallback } from "react" import React, { useState, useEffect, useRef, useCallback, useMemo } from "react"
import { Book, Rendition, Contents } from "epubjs" import { Book, Rendition, Contents } from "epubjs"
import { debounce } from "throttle-debounce" import { debounce } from "throttle-debounce"
// modules // modules
@ -10,7 +10,7 @@ import { timeFormatter } from 'lib/utils/commonUtil'
// styles // styles
import viewerDefaultStyles from 'modules/reactViewer/viewerStyle' import viewerDefaultStyles from 'modules/reactViewer/viewerStyle'
// types // types
import { ReactViewerProps } from 'types' import { ReactViewerProps, ViewerRef } from 'types'
import BookType, { BookStyle, BookOption } from 'types/book' import BookType, { BookStyle, BookOption } from 'types/book'
@ -39,7 +39,8 @@ const ReactViewer = ({
onTocChange, onTocChange,
onSelection, onSelection,
loadingView loadingView
}: ReactViewerProps, ref: any) => { }: ReactViewerProps, ref: React.RefObject<ViewerRef> | any) => {
// TODO Fix the ref type correctly instead 'any' type.
const [book, setBook] = useState<Book | null>(null); const [book, setBook] = useState<Book | null>(null);
const [rendition, setRendition] = useState<Rendition | null>(null); const [rendition, setRendition] = useState<Rendition | null>(null);
@ -95,7 +96,7 @@ const ReactViewer = ({
* Viewer resizing function * Viewer resizing function
* @method * @method
*/ */
const onResize = useCallback(debounce(250, () => { const onResize = useMemo(() => debounce(250, () => {
if (!rendition) return; if (!rendition) return;
const viewerLayout_ = viewerLayout || { const viewerLayout_ = viewerLayout || {
@ -149,6 +150,7 @@ const ReactViewer = ({
if (!iframeWin) return; if (!iframeWin) return;
const selection_ = iframeWin.getSelection(); const selection_ = iframeWin.getSelection();
if (!selection_) return;
const selectionText = selection_.toString(); const selectionText = selection_.toString();
if (selectionText === "") return; if (selectionText === "") return;
@ -166,6 +168,13 @@ const ReactViewer = ({
/** Ref checker */
useEffect(() => {
if (!ref) {
throw new Error("[React-Epub-Viewer] Put a ref argument that has a ViewerRef type.");
}
}, [ref]);
/** Epub parsing */ /** Epub parsing */
// TODO Fix the infinite re-rendering issue, when inlcude `onBookInfoChange` to dependencies array. // TODO Fix the infinite re-rendering issue, when inlcude `onBookInfoChange` to dependencies array.
/* eslint-disable */ /* eslint-disable */

@ -7,6 +7,40 @@ import Page from 'types/page'
import ViewerLayout from 'types/viewerLayout' import ViewerLayout from 'types/viewerLayout'
/**
* DOM Element wrapping the Epub viewer
* - Provides special methods.
*/
export interface ViewerRef extends HTMLDivElement {
/** Move the viewer to the previous page */
prevPage: () => void;
/** Move the viewer to the next page */
nextPage: () => void;
/** Get CFI in current page */
getCurrentCfi: () => string;
/**
* Highlighting specific CFIRange
* @param cfiRange CFIRange
* @param callback Callback after click highlight
* @param color Highlight color
*/
onHighlight: (
cfiRange: string,
callback: (e: any) => void,
color?: string
) => void;
/**
* Remove specific highlight
* @param cfiRange CFIRange
*/
offHighlight: (cfiRange: string) => void;
/**
* Move the viewer to the cfi or href
* @param location CFI or Href
*/
setLocation: (location: string) => void;
}
/** /**
* Epub Viewer Props * Epub Viewer Props
* @type * @type
@ -36,7 +70,7 @@ export interface EpubViewerProps {
loadingView?: React.ReactNode; loadingView?: React.ReactNode;
} }
declare class EpubViewer extends React.Component<EpubViewerProps> {} declare class EpubViewer extends React.Component<EpubViewerProps, ViewerRef> {}
/** /**
@ -64,6 +98,6 @@ export interface ReactViewerProps {
loadingView?: React.ReactNode; loadingView?: React.ReactNode;
} }
declare class ReactEpubViewer extends React.Component<ReactViewerProps> {} declare class ReactEpubViewer extends React.Component<ReactViewerProps, ViewerRef> {}
export { EpubViewer, ReactEpubViewer }; export { EpubViewer, ReactEpubViewer };
Loading…
Cancel
Save