-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(epub-view): epub viewer with persisted location (#1164)
Co-authored-by: Andrejs <Leitans>
- Loading branch information
1 parent
6bd4bf1
commit 0130c3b
Showing
8 changed files
with
215 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React, { | ||
CSSProperties, | ||
ComponentProps, | ||
useCallback, | ||
useMemo, | ||
} from 'react'; | ||
import { ReactReader } from 'react-reader'; | ||
import useEPubLocation from 'src/hooks/useEPubLocation'; | ||
|
||
interface IProps { | ||
url: string; | ||
search?: boolean; | ||
style?: CSSProperties; | ||
} | ||
|
||
type EpubInitOptions = ComponentProps<typeof ReactReader>['epubInitOptions']; | ||
const epubInitOptions: EpubInitOptions = { openAs: 'epub' }; | ||
|
||
function EPubView({ url, search, style }: IProps) { | ||
const [location, setLocation] = useEPubLocation(url); | ||
const currentLocation = location && !search ? location : 0; | ||
|
||
const currentStyle = useMemo( | ||
() => ({ height: search ? '300px' : '60vh', ...style }), | ||
[style, search] | ||
); | ||
|
||
const onLocationChange = useCallback( | ||
(loc: string) => { | ||
if (!search) { | ||
setLocation(loc); | ||
} | ||
}, | ||
[search, setLocation] | ||
); | ||
|
||
return ( | ||
<div style={currentStyle}> | ||
<ReactReader | ||
url={url} | ||
location={currentLocation} | ||
locationChanged={onLocationChange} | ||
epubInitOptions={epubInitOptions} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default React.memo(EPubView); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import { useCallback } from 'react'; | ||
import type { EpubView } from 'react-reader'; | ||
|
||
const epubKey = 'cyb:epub'; | ||
|
||
const getEPubMap = (): Record<string, EpubView['location']> => { | ||
try { | ||
const epubMapString = localStorage.getItem(epubKey) || ''; | ||
const epubMap = JSON.parse(epubMapString); | ||
|
||
return epubMap; | ||
} catch (error) { | ||
console.error('Failed to parse epub locations map:', error); | ||
|
||
return {}; | ||
} | ||
}; | ||
|
||
const getLocation = (url: string) => { | ||
const epubMap = getEPubMap(); | ||
|
||
return epubMap[url] ?? null; | ||
}; | ||
|
||
const getSetEPubLocation = | ||
(url: string) => (location: EpubView['location']) => { | ||
const epubMap = getEPubMap(); | ||
|
||
try { | ||
epubMap[url] = location; | ||
localStorage.setItem(epubKey, JSON.stringify(epubMap)); | ||
} catch (error) { | ||
console.error('Failed to save EPub location:', error); | ||
} | ||
}; | ||
|
||
const useEPubLocation = ( | ||
url: string | ||
): [EpubView['location'], (url: string) => void] => { | ||
const currentLocation = getLocation(url); | ||
const setEPubLocation = useCallback(getSetEPubLocation(url), [url]); | ||
|
||
return [currentLocation, setEPubLocation]; | ||
}; | ||
|
||
export default useEPubLocation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.