mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 01:46:18 -05:00
Resume EPUBs and comic books where the reader left off (#798)
* Resume EPUBs and comic books where the reader left off The EPUB viewer (epub.html) accepts `?cfi=` and posts its location and progress to the page that embeds it on every page turn. Progress comes from the chapter and the page within it, rather than book.locations.generate(), which stalls a Raspberry Pi on a large book. A search result's `loc` still wins over the saved position. The comic viewer opens at the saved page and saves each page shown. Both resume on the doc page and in the file preview. The resume point is decided once per opened file, so saves made while reading do not change the EPUB viewer's URL and reload it. The doc page tracks only the FileGroup's primary file; another format beside it is not tracked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * EPUB/comic progress: never save a place the server would discard The server treats a position at the start (the cover, under 1%) as not started, and replaces the saved place with progress 0. Both viewers reported the place they opened on, so opening a finished book un-finished it, and peeking at the cover erased the reader's page. - The comic viewer does not save the page it opens on, nor the cover; a peek at the cover keeps the last page saved. - The EPUB viewer posts nothing at the start of the book. - Progress before the end is kept between 1% and 94% (resumableProgress), so early pages of a long book and the last pages before the end keep their position. Only the last page (comics) or the end (EPUB) is finished. - Non-finite EPUB progress is ignored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1ad451cddf
commit
3c362799b5
8 changed files with 393 additions and 18 deletions
|
|
@ -17,6 +17,17 @@ const CACHE_PREFIX = 'wrolpi-progress:';
|
|||
const FINISHED_PROGRESS = 0.95;
|
||||
const MINIMUM_PROGRESS = 0.01;
|
||||
const MINIMUM_SECONDS = 10;
|
||||
// The most a reader who has not reached the end can be through a file. The server drops the position of a
|
||||
// file at FINISHED_PROGRESS, so the last pages before the end stay resumable.
|
||||
const BEFORE_FINISHED = 0.94;
|
||||
|
||||
/**
|
||||
* Progress to save for a reader who is past the start and not at the end: kept between the server's floor (below
|
||||
* which it discards the position) and its finished line.
|
||||
*/
|
||||
export function resumableProgress(fraction) {
|
||||
return Math.min(Math.max(fraction, MINIMUM_PROGRESS), BEFORE_FINISHED);
|
||||
}
|
||||
|
||||
function readCache(path) {
|
||||
try {
|
||||
|
|
@ -198,3 +209,73 @@ export function useMediaProgress(path, fileGroup, {startSeconds = null, enabled
|
|||
|
||||
return {onLoadedMetadata, onTimeUpdate, onPause, onEnded};
|
||||
}
|
||||
|
||||
/**
|
||||
* Where to resume `path`, decided once when it is opened. Saves made while it is open do not move it,
|
||||
* so a viewer whose URL holds the start (the EPUB viewer) is not reloaded as the user reads.
|
||||
*/
|
||||
export function useResumePosition(path, fileGroup) {
|
||||
const resumeRef = React.useRef({path: null, position: null});
|
||||
if (path && fileGroup && resumeRef.current.path !== path) {
|
||||
resumeRef.current = {path, position: getResumePosition(path, fileGroup)};
|
||||
}
|
||||
return path && resumeRef.current.path === path ? resumeRef.current.position : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Track and resume the EPUB viewer (public/epub/epub.html), which posts its location to this window.
|
||||
*
|
||||
* @returns {{iframeRef, resumeCfi}} Put `iframeRef` on the viewer's <iframe>, and pass `resumeCfi` to it as `cfi`.
|
||||
*/
|
||||
export function useEpubProgress(path, fileGroup, {enabled = true} = {}) {
|
||||
const trackedPath = enabled ? path : null;
|
||||
const {report} = useFileProgress(trackedPath);
|
||||
const resume = useResumePosition(trackedPath, fileGroup);
|
||||
const iframeRef = React.useRef(null);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!trackedPath) {
|
||||
return;
|
||||
}
|
||||
const onMessage = (event) => {
|
||||
// Only our own viewer, in this iframe.
|
||||
if (event.origin !== window.location.origin || !iframeRef.current
|
||||
|| event.source !== iframeRef.current.contentWindow) {
|
||||
return;
|
||||
}
|
||||
const {type, cfi, progress} = event.data || {};
|
||||
if (type !== 'wrolpi:epub-location' || typeof cfi !== 'string' || !Number.isFinite(progress)) {
|
||||
return;
|
||||
}
|
||||
// The viewer posts nothing at the start of the book, so a finished book opened there stays finished.
|
||||
report({progress: progress >= 1 ? 1 : resumableProgress(progress), position: {kind: 'epub', cfi}});
|
||||
};
|
||||
window.addEventListener('message', onMessage);
|
||||
return () => window.removeEventListener('message', onMessage);
|
||||
}, [trackedPath, report]);
|
||||
|
||||
return {iframeRef, resumeCfi: resume && resume.kind === 'epub' ? resume.cfi : null};
|
||||
}
|
||||
|
||||
/**
|
||||
* Track and resume a paged viewer (comic books).
|
||||
*
|
||||
* @returns {{initialPage, onPageChange}} The 0-based page to open at, and a callback for each page shown.
|
||||
*/
|
||||
export function usePageProgress(path, fileGroup, {enabled = true} = {}) {
|
||||
const trackedPath = enabled ? path : null;
|
||||
const {report} = useFileProgress(trackedPath);
|
||||
const resume = useResumePosition(trackedPath, fileGroup);
|
||||
|
||||
const onPageChange = React.useCallback((page, pageCount) => {
|
||||
// The cover is not a place to resume; saving it would clear the reader's place, so a peek at it keeps
|
||||
// the last page saved.
|
||||
if (pageCount <= 0 || page <= 0) {
|
||||
return;
|
||||
}
|
||||
const progress = page >= pageCount - 1 ? 1 : resumableProgress((page + 1) / pageCount);
|
||||
report({progress, position: {kind: 'page', page}});
|
||||
}, [report]);
|
||||
|
||||
return {initialPage: resume && resume.kind === 'page' ? resume.page : 0, onPageChange};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue