Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open document reader from last position #95

Merged
merged 3 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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())
Expand All @@ -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()
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/webserver/embedded/views/reader.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<link href="/css/reader.css" rel="stylesheet">

<input type="hidden" id="url" value="{{.fqdn}}/documents/{{.Slug}}/download">
<input type="hidden" id="slug" value="{{.Slug}}">

<div id="spinner-container" class="filter">
<div>
Expand Down Expand Up @@ -79,4 +80,4 @@
}}</script>

<script src="/js/foliate-js/vendor/pdfjs/pdf.js"></script>
<script src="/js/foliate-js/reader.js" type="module"></script>
<script src="/js/reader.js" type="module"></script>