From c1e4768c13a9d95d228eb65d37785cffb716279d Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Sat, 3 Aug 2024 21:21:34 +0100 Subject: [PATCH] Open document reader from last position --- .../embedded/js/{foliate-js => }/reader.js | 24 ++++++++++++------- internal/webserver/embedded/views/reader.html | 3 ++- 2 files changed, 18 insertions(+), 9 deletions(-) rename internal/webserver/embedded/js/{foliate-js => }/reader.js (92%) diff --git a/internal/webserver/embedded/js/foliate-js/reader.js b/internal/webserver/embedded/js/reader.js similarity index 92% rename from internal/webserver/embedded/js/foliate-js/reader.js rename to internal/webserver/embedded/js/reader.js index 9dc4ee6..d292dad 100644 --- a/internal/webserver/embedded/js/foliate-js/reader.js +++ b/internal/webserver/embedded/js/reader.js @@ -1,7 +1,7 @@ -import './view.js' -import { createTOCView } from './ui/tree.js' -import { createMenu } from './ui/menu.js' -import { Overlayer } from './overlayer.js' +import './foliate-js/view.js' +import { createTOCView } from './foliate-js/ui/tree.js' +import { createMenu } from './foliate-js/ui/menu.js' +import { Overlayer } from './foliate-js/overlayer.js' const isZip = async file => { const arr = new Uint8Array(await file.slice(0, 4).arrayBuffer()) @@ -17,7 +17,7 @@ const isPDF = async file => { const makeZipLoader = async file => { const { configure, ZipReader, BlobReader, TextWriter, BlobWriter } = - await import('./vendor/zip.js') + await import('./foliate-js/vendor/zip.js') configure({ useWebWorkers: false }) const reader = new ZipReader(new BlobReader(file)) const entries = await reader.getEntries() @@ -35,17 +35,21 @@ const getView = async file => { if (!file.size) throw new Error('File not found') else if (await isZip(file)) { const loader = await makeZipLoader(file) - const { EPUB } = await import('./epub.js') + const { EPUB } = await import('./foliate-js/epub.js') book = await new EPUB(loader).init() } else if (await isPDF(file)) { - const { makePDF } = await import('./pdf.js') + const { makePDF } = await import('./foliate-js/pdf.js') book = await makePDF(file) } if (!book) throw new Error('File type not supported') const view = document.createElement('foliate-view') + const storage = window.localStorage + const slug = document.getElementById('slug').value + document.body.append(view) await view.open(book) + await view.init({lastLocation: storage.getItem(slug)}) return view } @@ -183,7 +187,7 @@ class Reader { // load and show highlights embedded in the file by Calibre const bookmarks = await book.getCalibreBookmarks?.() if (bookmarks) { - const { fromCalibreHighlight } = await import('./epubcfi.js') + const { fromCalibreHighlight } = await import('./foliate-js/epubcfi.js') for (const obj of bookmarks) { if (obj.type === 'highlight') { const value = fromCalibreHighlight(obj) @@ -222,6 +226,10 @@ class Reader { doc.addEventListener('keydown', this.#handleKeydown.bind(this)) } #onRelocate({ detail }) { + const storage = window.localStorage + const slug = document.getElementById('slug').value + + storage.setItem(slug, detail.cfi) const { fraction, location, tocItem, pageItem } = detail const percent = percentFormat.format(fraction) const loc = pageItem diff --git a/internal/webserver/embedded/views/reader.html b/internal/webserver/embedded/views/reader.html index 1746b0a..67d5daf 100644 --- a/internal/webserver/embedded/views/reader.html +++ b/internal/webserver/embedded/views/reader.html @@ -14,6 +14,7 @@ +
@@ -79,4 +80,4 @@ }} - +