import React, { useContext, useEffect, useRef } from 'react'; import { TouchableWithoutFeedback, View as RNView } from 'react-native'; import { Directions, FlingGestureHandler, GestureHandlerRootView, State, } from 'react-native-gesture-handler'; import { WebView, WebViewMessageEvent } from 'react-native-webview'; import { defaultTheme as initialTheme, ReaderContext } from './context'; import type { ReaderProps } from './types'; import { OpeningBook } from './utils/OpeningBook'; export type ViewProps = Omit & { template: string; }; export function View({ template, onStarted = () => {}, onReady = () => {}, onDisplayError = () => {}, onResized = () => {}, onLocationChange = () => {}, onRendered = () => {}, onSearch = () => {}, onLocationsReady = () => {}, onSelected = () => {}, onMarkPressed = () => {}, onOrientationChange = () => {}, onLayout = () => {}, onNavigationLoaded = () => {}, onBeginning = () => {}, onFinish = () => {}, onPress = () => {}, onDoublePress = () => {}, width, height, initialLocation, enableSwipe = true, onSwipeLeft = () => {}, onSwipeRight = () => {}, defaultTheme = initialTheme, renderOpeningBookComponent = () => ( ), }: ViewProps) { const { registerBook, setTotalLocations, setCurrentLocation, setProgress, setLocations, setAtStart, setAtEnd, goNext, goPrevious, isRendering, setIsRendering, goToLocation, changeTheme, setKey, setSearchResults, theme, } = useContext(ReaderContext); const book = useRef(null); const onMessage = (event: WebViewMessageEvent) => { const parsedEvent = JSON.parse(event.nativeEvent.data); const { type } = parsedEvent; delete parsedEvent.type; if (type === 'onStarted') { setIsRendering(true); changeTheme(defaultTheme); return onStarted(); } if (type === 'onReady') { const { totalLocations, currentLocation, progress } = parsedEvent; setIsRendering(false); setTotalLocations(totalLocations); setCurrentLocation(currentLocation); setProgress(progress); if (initialLocation) { goToLocation(initialLocation); } return onReady(totalLocations, currentLocation, progress); } if (type === 'onDisplayError') { const { reason } = parsedEvent; setIsRendering(false); return onDisplayError(reason); } if (type === 'onResized') { const { layout } = parsedEvent; return onResized(layout); } if (type === 'onLocationChange') { const { totalLocations, currentLocation, progress } = parsedEvent; setTotalLocations(totalLocations); setCurrentLocation(currentLocation); setProgress(progress); if (currentLocation.atStart) setAtStart(true); else if (currentLocation.atEnd) setAtEnd(true); else { setAtStart(false); setAtEnd(false); } return onLocationChange(totalLocations, currentLocation, progress); } if (type === 'onSearch') { const { results } = parsedEvent; setSearchResults(results); return onSearch(results); } if (type === 'onLocationsReady') { const { epubKey } = parsedEvent; setLocations(parsedEvent.locations); setKey(epubKey); return onLocationsReady(epubKey, parsedEvent.locations); } if (type === 'onSelected') { const { cfiRange, text } = parsedEvent; return onSelected(text, cfiRange); } if (type === 'onMarkPressed') { const { cfiRange, text } = parsedEvent; return onMarkPressed(cfiRange, text); } if (type === 'onOrientationChange') { const { orientation } = parsedEvent; return onOrientationChange(orientation); } if (type === 'onBeginning') { setAtStart(true); return onBeginning(); } if (type === 'onFinish') { setAtEnd(true); return onFinish(); } if (type === 'onRendered') { const { section, currentSection } = parsedEvent; return onRendered(section, currentSection); } if (type === 'onLayout') { const { layout } = parsedEvent; return onLayout(layout); } if (type === 'onNavigationLoaded') { const { toc } = parsedEvent; return onNavigationLoaded(toc); } return () => {}; }; useEffect(() => { if (book.current) registerBook(book.current); }, [registerBook]); let lastTap: number | null = null; let timer: NodeJS.Timeout; const handleDoublePress = () => { if (lastTap) { onDoublePress(); clearTimeout(timer); lastTap = null; } else { lastTap = Date.now(); timer = setTimeout(() => { onPress(); lastTap = null; clearTimeout(timer); }, 300); } }; return ( { if (nativeEvent.state === State.ACTIVE && enableSwipe) { goPrevious(); onSwipeRight(); } }} > { if (nativeEvent.state === State.ACTIVE && enableSwipe) { goNext(); onSwipeLeft(); } }} > {isRendering && ( {renderOpeningBookComponent()} )} { if ( !isRendering && request.mainDocumentURL && request.url !== request.mainDocumentURL ) { goToLocation( request.url.replace(request.mainDocumentURL, '') ); } return true; }} style={{ width, backgroundColor: theme.body.background, height, }} /> ); }