mirror of
https://github.com/john-okeefe/foliate-js.git
synced 2026-09-09 11:29:14 -04:00
263 lines
9.1 KiB
JavaScript
263 lines
9.1 KiB
JavaScript
/* global zip: false, fflate: false */
|
|
import { View } from './view.js'
|
|
import { createTOCView } from './ui/tree.js'
|
|
import { createMenu } from './ui/menu.js'
|
|
import { createPopover } from './ui/popover.js'
|
|
|
|
const { ZipReader, BlobReader, TextWriter, BlobWriter } = zip
|
|
zip.configure({ useWebWorkers: false })
|
|
|
|
const isZip = async file => {
|
|
const arr = new Uint8Array(await file.slice(0, 4).arrayBuffer())
|
|
return arr[0] === 0x50 && arr[1] === 0x4b && arr[2] === 0x03 && arr[3] === 0x04
|
|
}
|
|
|
|
const makeZipLoader = async file => {
|
|
const reader = new ZipReader(new BlobReader(file))
|
|
const entries = await reader.getEntries()
|
|
const map = new Map(entries.map(entry => [entry.filename, entry]))
|
|
const load = f => (name, ...args) =>
|
|
map.has(name) ? f(map.get(name), ...args) : null
|
|
const loadText = load(entry => entry.getData(new TextWriter()))
|
|
const loadBlob = load((entry, type) => entry.getData(new BlobWriter(type)))
|
|
const getSize = name => map.get(name)?.uncompressedSize ?? 0
|
|
return { entries, loadText, loadBlob, getSize }
|
|
}
|
|
|
|
const isCBZ = ({ name, type }) =>
|
|
type === 'application/vnd.comicbook+zip' || name.endsWith('.cbz')
|
|
|
|
const isFB2 = ({ name, type }) =>
|
|
type === 'application/x-fictionbook+xml' || name.endsWith('.fb2')
|
|
|
|
const isFBZ = ({ name, type }) =>
|
|
type === 'application/x-zip-compressed-fb2'
|
|
|| name.endsWith('.fb2.zip') || name.endsWith('.fbz')
|
|
|
|
const getView = async (file, emit) => {
|
|
if (!file.size) throw new Error('File not found')
|
|
let book
|
|
if (await isZip(file)) {
|
|
const loader = await makeZipLoader(file)
|
|
if (isCBZ(file)) {
|
|
const { makeComicBook } = await import('./comic-book.js')
|
|
book = makeComicBook(loader, file)
|
|
} else if (isFBZ(file)) {
|
|
const { makeFB2 } = await import('./fb2.js')
|
|
const { entries } = loader
|
|
const entry = entries.find(entry => entry.filename.endsWith('.fb2'))
|
|
const blob = await loader.loadBlob((entry ?? entries[0]).filename)
|
|
book = await makeFB2(blob)
|
|
} else {
|
|
const { EPUB } = await import('./epub.js')
|
|
book = await new EPUB(loader).init()
|
|
}
|
|
} else {
|
|
const { isMOBI, MOBI } = await import('./mobi.js')
|
|
if (await isMOBI(file))
|
|
book = await new MOBI({ unzlib: fflate.unzlibSync }).open(file)
|
|
else if (isFB2(file)) {
|
|
const { makeFB2 } = await import('./fb2.js')
|
|
book = await makeFB2(file)
|
|
}
|
|
}
|
|
if (!book) throw new Error('File type not supported')
|
|
const view = new View(book, emit)
|
|
const element = await view.display()
|
|
document.body.append(element)
|
|
return view
|
|
}
|
|
|
|
const getCSS = ({ spacing, justify, hyphenate }) => `
|
|
@namespace epub "http://www.idpf.org/2007/ops";
|
|
html {
|
|
color-scheme: light dark;
|
|
}
|
|
/* https://github.com/whatwg/html/issues/5426 */
|
|
@media (prefers-color-scheme: dark) {
|
|
a:link {
|
|
color: lightblue;
|
|
}
|
|
}
|
|
p, li, blockquote, dd {
|
|
line-height: ${spacing};
|
|
text-align: ${justify ? 'justify' : 'start'};
|
|
-webkit-hyphens: ${hyphenate ? 'auto' : 'manual'};
|
|
-webkit-hyphenate-limit-before: 3;
|
|
-webkit-hyphenate-limit-after: 2;
|
|
-webkit-hyphenate-limit-lines: 2;
|
|
hanging-punctuation: allow-end last;
|
|
widows: 2;
|
|
}
|
|
pre {
|
|
white-space: pre-wrap !important;
|
|
}
|
|
aside[epub|type~="endnote"],
|
|
aside[epub|type~="footnote"],
|
|
aside[epub|type~="note"],
|
|
aside[epub|type~="rearnote"] {
|
|
display: none;
|
|
}
|
|
`
|
|
|
|
const $ = document.querySelector.bind(document)
|
|
|
|
const locales = 'en'
|
|
const percentFormat = new Intl.NumberFormat(locales, { style: 'percent' })
|
|
|
|
class Reader {
|
|
#tocView
|
|
style = {
|
|
spacing: 1.4,
|
|
justify: true,
|
|
hyphenate: true,
|
|
}
|
|
layout = {
|
|
margin: 48,
|
|
gap: 48,
|
|
maxColumnWidth: 720,
|
|
}
|
|
closeSideBar() {
|
|
$('#dimming-overlay').classList.remove('show')
|
|
$('#side-bar').classList.remove('show')
|
|
}
|
|
constructor() {
|
|
$('#side-bar-button').addEventListener('click', () => {
|
|
$('#dimming-overlay').classList.add('show')
|
|
$('#side-bar').classList.add('show')
|
|
})
|
|
$('#dimming-overlay').addEventListener('click', () => this.closeSideBar())
|
|
|
|
const menu = createMenu([
|
|
{
|
|
name: 'layout',
|
|
label: 'Layout',
|
|
type: 'radio',
|
|
items: [
|
|
['Paginated', 'paginated'],
|
|
['Scrolled', 'scrolled'],
|
|
],
|
|
onclick: value => {
|
|
this.layout.flow = value
|
|
this.setAppearance()
|
|
},
|
|
}
|
|
])
|
|
menu.element.classList.add('menu')
|
|
|
|
$('#menu-button').append(menu.element)
|
|
$('#menu-button > button').addEventListener('click', () =>
|
|
menu.element.classList.toggle('show'))
|
|
menu.groups.layout.select('paginated')
|
|
}
|
|
async open(file) {
|
|
this.view = await getView(file, this.#handleEvent.bind(this))
|
|
this.setAppearance()
|
|
this.view.renderer.next()
|
|
|
|
$('#header-bar').style.visibility = 'visible'
|
|
$('#nav-bar').style.visibility = 'visible'
|
|
$('#left-button').addEventListener('click', () => this.view.goLeft())
|
|
$('#right-button').addEventListener('click', () => this.view.goRight())
|
|
|
|
document.addEventListener('keydown', this.#handleKeydown.bind(this))
|
|
|
|
const { book } = this.view
|
|
const title = book.metadata?.title ?? 'Untitled Book'
|
|
document.title = title
|
|
$('#side-bar-title').innerText = title
|
|
const author = book.metadata?.author
|
|
$('#side-bar-author').innerText = typeof author === 'string' ? author
|
|
: author
|
|
?.map(author => typeof author === 'string' ? author : author.name)
|
|
?.join(', ')
|
|
?? ''
|
|
book.getCover?.()?.then(blob =>
|
|
blob ? $('#side-bar-cover').src = URL.createObjectURL(blob) : null)
|
|
|
|
const toc = book.toc
|
|
if (toc) {
|
|
this.#tocView = createTOCView(toc, href => {
|
|
this.view.goTo(href).catch(e => console.error(e))
|
|
this.closeSideBar()
|
|
})
|
|
$('#toc-view').append(this.#tocView.element)
|
|
}
|
|
}
|
|
setAppearance = () => {
|
|
this.view?.setAppearance({ css: getCSS(this.style), layout: this.layout })
|
|
const scrolled = this.layout.flow === 'scrolled'
|
|
document.documentElement.classList.toggle('scrolled', scrolled)
|
|
}
|
|
#handleEvent(obj) {
|
|
console.debug(obj)
|
|
switch (obj.type) {
|
|
case 'loaded': this.#onLoaded(obj); break
|
|
case 'relocated': this.#onRelocated(obj); break
|
|
case 'reference': this.#onReference(obj); break
|
|
}
|
|
}
|
|
#handleKeydown(event) {
|
|
const k = event.key
|
|
if (k === 'ArrowLeft' || k === 'h') this.view.goLeft()
|
|
else if(k === 'ArrowRight' || k === 'l') this.view.goRight()
|
|
}
|
|
#onLoaded({ doc }) {
|
|
doc.addEventListener('keydown', this.#handleKeydown.bind(this))
|
|
}
|
|
#onRelocated(obj) {
|
|
const { fraction, location, tocItem, pageItem } = obj
|
|
const percent = percentFormat.format(fraction)
|
|
const loc = pageItem
|
|
? `Page ${pageItem.label}`
|
|
: `Loc ${location.current}`
|
|
$('#progress-label').innerText = `${percent} · ${loc}`
|
|
if (tocItem?.href) this.#tocView.setCurrentHref?.(tocItem.href)
|
|
}
|
|
#onReference(obj) {
|
|
const { content, pos: { point, dir } } = obj
|
|
const iframe = document.createElement('iframe')
|
|
iframe.sandbox = 'allow-same-origin'
|
|
iframe.srcdoc = content
|
|
iframe.onload = () => {
|
|
const doc = iframe.contentDocument
|
|
doc.documentElement.style.colorScheme = 'light dark'
|
|
doc.body.style.margin = '18px'
|
|
}
|
|
Object.assign(iframe.style, {
|
|
border: '0',
|
|
width: '100%',
|
|
height: '100%',
|
|
})
|
|
const { popover, arrow, overlay } = createPopover(300, 250, point, dir)
|
|
overlay.style.zIndex = 3
|
|
popover.style.zIndex = 3
|
|
arrow.style.zIndex = 3
|
|
popover.append(iframe)
|
|
document.body.append(overlay)
|
|
document.body.append(popover)
|
|
document.body.append(arrow)
|
|
}
|
|
}
|
|
|
|
const open = async file => {
|
|
document.body.removeChild($('#drop-target'))
|
|
const reader = new Reader()
|
|
await reader.open(file)
|
|
}
|
|
|
|
const dragOverHandler = e => e.preventDefault()
|
|
const dropHandler = e => {
|
|
e.preventDefault()
|
|
const file = Array.from(e.dataTransfer.items)
|
|
.find(item => item.kind === 'file')?.getAsFile()
|
|
if (file) open(file).catch(e => console.error(e))
|
|
}
|
|
const dropTarget = $('#drop-target')
|
|
dropTarget.addEventListener('drop', dropHandler)
|
|
dropTarget.addEventListener('dragover', dragOverHandler)
|
|
|
|
$('#file-input').addEventListener('change', e =>
|
|
open(e.target.files[0]).catch(e => console.error(e)))
|
|
$('#file-button').addEventListener('click', () => $('#file-input').click())
|