mirror of
https://github.com/john-okeefe/foliate-js.git
synced 2026-09-09 11:29:14 -04:00
Add panel-aware keyboard shortcuts to reader
Extend keyboard navigation to support panel mode when enabled. Changes: - Check for panel-mode attribute before routing key events - Route Arrow keys and Vim keys (h/l) to panel navigation in panel mode - Add 'P' key to toggle panel mode - Add 'Escape' key to exit panel mode - Maintain backward compatibility with existing page navigation When panel mode is active: - ArrowRight/l: next panel - ArrowLeft/h: previous panel - Escape/P: exit panel mode When panel mode is inactive: - ArrowRight/l: next page (existing behavior) - ArrowLeft/h: previous page (existing behavior) - P: enter panel mode (new feature)
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import './view.js'
|
import "./view.js";
|
||||||
import { createTOCView } from './ui/tree.js'
|
import { createTOCView } from "./ui/tree.js";
|
||||||
import { createMenu } from './ui/menu.js'
|
import { createMenu } from "./ui/menu.js";
|
||||||
import { Overlayer } from './overlayer.js'
|
import { Overlayer } from "./overlayer.js";
|
||||||
|
|
||||||
const getCSS = ({ spacing, justify, hyphenate }) => `
|
const getCSS = ({ spacing, justify, hyphenate }) => `
|
||||||
@namespace epub "http://www.idpf.org/2007/ops";
|
@namespace epub "http://www.idpf.org/2007/ops";
|
||||||
@@ -16,9 +16,9 @@ const getCSS = ({ spacing, justify, hyphenate }) => `
|
|||||||
}
|
}
|
||||||
p, li, blockquote, dd {
|
p, li, blockquote, dd {
|
||||||
line-height: ${spacing};
|
line-height: ${spacing};
|
||||||
text-align: ${justify ? 'justify' : 'start'};
|
text-align: ${justify ? "justify" : "start"};
|
||||||
-webkit-hyphens: ${hyphenate ? 'auto' : 'manual'};
|
-webkit-hyphens: ${hyphenate ? "auto" : "manual"};
|
||||||
hyphens: ${hyphenate ? 'auto' : 'manual'};
|
hyphens: ${hyphenate ? "auto" : "manual"};
|
||||||
-webkit-hyphenate-limit-before: 3;
|
-webkit-hyphenate-limit-before: 3;
|
||||||
-webkit-hyphenate-limit-after: 2;
|
-webkit-hyphenate-limit-after: 2;
|
||||||
-webkit-hyphenate-limit-lines: 2;
|
-webkit-hyphenate-limit-lines: 2;
|
||||||
@@ -40,200 +40,231 @@ const getCSS = ({ spacing, justify, hyphenate }) => `
|
|||||||
aside[epub|type~="rearnote"] {
|
aside[epub|type~="rearnote"] {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
`
|
`;
|
||||||
|
|
||||||
const $ = document.querySelector.bind(document)
|
const $ = document.querySelector.bind(document);
|
||||||
|
|
||||||
const locales = 'en'
|
const locales = "en";
|
||||||
const percentFormat = new Intl.NumberFormat(locales, { style: 'percent' })
|
const percentFormat = new Intl.NumberFormat(locales, { style: "percent" });
|
||||||
const listFormat = new Intl.ListFormat(locales, { style: 'short', type: 'conjunction' })
|
const listFormat = new Intl.ListFormat(locales, {
|
||||||
|
style: "short",
|
||||||
|
type: "conjunction",
|
||||||
|
});
|
||||||
|
|
||||||
const formatLanguageMap = x => {
|
const formatLanguageMap = (x) => {
|
||||||
if (!x) return ''
|
if (!x) return "";
|
||||||
if (typeof x === 'string') return x
|
if (typeof x === "string") return x;
|
||||||
const keys = Object.keys(x)
|
const keys = Object.keys(x);
|
||||||
return x[keys[0]]
|
return x[keys[0]];
|
||||||
}
|
};
|
||||||
|
|
||||||
const formatOneContributor = contributor => typeof contributor === 'string'
|
const formatOneContributor = (contributor) =>
|
||||||
? contributor : formatLanguageMap(contributor?.name)
|
typeof contributor === "string"
|
||||||
|
? contributor
|
||||||
|
: formatLanguageMap(contributor?.name);
|
||||||
|
|
||||||
const formatContributor = contributor => Array.isArray(contributor)
|
const formatContributor = (contributor) =>
|
||||||
|
Array.isArray(contributor)
|
||||||
? listFormat.format(contributor.map(formatOneContributor))
|
? listFormat.format(contributor.map(formatOneContributor))
|
||||||
: formatOneContributor(contributor)
|
: formatOneContributor(contributor);
|
||||||
|
|
||||||
class Reader {
|
class Reader {
|
||||||
#tocView
|
#tocView;
|
||||||
style = {
|
style = {
|
||||||
spacing: 1.4,
|
spacing: 1.4,
|
||||||
justify: true,
|
justify: true,
|
||||||
hyphenate: true,
|
hyphenate: true,
|
||||||
|
};
|
||||||
|
annotations = new Map();
|
||||||
|
annotationsByValue = new Map();
|
||||||
|
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.view?.renderer.setAttribute("flow", value);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
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 = document.createElement("foliate-view");
|
||||||
|
document.body.append(this.view);
|
||||||
|
await this.view.open(file);
|
||||||
|
this.view.addEventListener("load", this.#onLoad.bind(this));
|
||||||
|
this.view.addEventListener("relocate", this.#onRelocate.bind(this));
|
||||||
|
|
||||||
|
const { book } = this.view;
|
||||||
|
book.transformTarget?.addEventListener("data", ({ detail }) => {
|
||||||
|
detail.data = Promise.resolve(detail.data).catch((e) => {
|
||||||
|
console.error(new Error(`Failed to load ${detail.name}`, { cause: e }));
|
||||||
|
return "";
|
||||||
|
});
|
||||||
|
});
|
||||||
|
this.view.renderer.setStyles?.(getCSS(this.style));
|
||||||
|
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());
|
||||||
|
|
||||||
|
const slider = $("#progress-slider");
|
||||||
|
slider.dir = book.dir;
|
||||||
|
slider.addEventListener("input", (e) =>
|
||||||
|
this.view.goToFraction(parseFloat(e.target.value)),
|
||||||
|
);
|
||||||
|
for (const fraction of this.view.getSectionFractions()) {
|
||||||
|
const option = document.createElement("option");
|
||||||
|
option.value = fraction;
|
||||||
|
$("#tick-marks").append(option);
|
||||||
}
|
}
|
||||||
annotations = new Map()
|
|
||||||
annotationsByValue = new Map()
|
document.addEventListener("keydown", this.#handleKeydown.bind(this));
|
||||||
closeSideBar() {
|
|
||||||
$('#dimming-overlay').classList.remove('show')
|
const title = formatLanguageMap(book.metadata?.title) || "Untitled Book";
|
||||||
$('#side-bar').classList.remove('show')
|
document.title = title;
|
||||||
|
$("#side-bar-title").innerText = title;
|
||||||
|
$("#side-bar-author").innerText = formatContributor(book.metadata?.author);
|
||||||
|
Promise.resolve(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);
|
||||||
}
|
}
|
||||||
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([
|
// load and show highlights embedded in the file by Calibre
|
||||||
{
|
const bookmarks = await book.getCalibreBookmarks?.();
|
||||||
name: 'layout',
|
if (bookmarks) {
|
||||||
label: 'Layout',
|
const { fromCalibreHighlight } = await import("./epubcfi.js");
|
||||||
type: 'radio',
|
for (const obj of bookmarks) {
|
||||||
items: [
|
if (obj.type === "highlight") {
|
||||||
['Paginated', 'paginated'],
|
const value = fromCalibreHighlight(obj);
|
||||||
['Scrolled', 'scrolled'],
|
const color = obj.style.which;
|
||||||
],
|
const note = obj.notes;
|
||||||
onclick: value => {
|
const annotation = { value, color, note };
|
||||||
this.view?.renderer.setAttribute('flow', value)
|
const list = this.annotations.get(obj.spine_index);
|
||||||
},
|
if (list) list.push(annotation);
|
||||||
},
|
else this.annotations.set(obj.spine_index, [annotation]);
|
||||||
])
|
this.annotationsByValue.set(value, annotation);
|
||||||
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 = document.createElement('foliate-view')
|
|
||||||
document.body.append(this.view)
|
|
||||||
await this.view.open(file)
|
|
||||||
this.view.addEventListener('load', this.#onLoad.bind(this))
|
|
||||||
this.view.addEventListener('relocate', this.#onRelocate.bind(this))
|
|
||||||
|
|
||||||
const { book } = this.view
|
|
||||||
book.transformTarget?.addEventListener('data', ({ detail }) => {
|
|
||||||
detail.data = Promise.resolve(detail.data).catch(e => {
|
|
||||||
console.error(new Error(`Failed to load ${detail.name}`, { cause: e }))
|
|
||||||
return ''
|
|
||||||
})
|
|
||||||
})
|
|
||||||
this.view.renderer.setStyles?.(getCSS(this.style))
|
|
||||||
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())
|
|
||||||
|
|
||||||
const slider = $('#progress-slider')
|
|
||||||
slider.dir = book.dir
|
|
||||||
slider.addEventListener('input', e =>
|
|
||||||
this.view.goToFraction(parseFloat(e.target.value)))
|
|
||||||
for (const fraction of this.view.getSectionFractions()) {
|
|
||||||
const option = document.createElement('option')
|
|
||||||
option.value = fraction
|
|
||||||
$('#tick-marks').append(option)
|
|
||||||
}
|
|
||||||
|
|
||||||
document.addEventListener('keydown', this.#handleKeydown.bind(this))
|
|
||||||
|
|
||||||
const title = formatLanguageMap(book.metadata?.title) || 'Untitled Book'
|
|
||||||
document.title = title
|
|
||||||
$('#side-bar-title').innerText = title
|
|
||||||
$('#side-bar-author').innerText = formatContributor(book.metadata?.author)
|
|
||||||
Promise.resolve(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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// load and show highlights embedded in the file by Calibre
|
|
||||||
const bookmarks = await book.getCalibreBookmarks?.()
|
|
||||||
if (bookmarks) {
|
|
||||||
const { fromCalibreHighlight } = await import('./epubcfi.js')
|
|
||||||
for (const obj of bookmarks) {
|
|
||||||
if (obj.type === 'highlight') {
|
|
||||||
const value = fromCalibreHighlight(obj)
|
|
||||||
const color = obj.style.which
|
|
||||||
const note = obj.notes
|
|
||||||
const annotation = { value, color, note }
|
|
||||||
const list = this.annotations.get(obj.spine_index)
|
|
||||||
if (list) list.push(annotation)
|
|
||||||
else this.annotations.set(obj.spine_index, [annotation])
|
|
||||||
this.annotationsByValue.set(value, annotation)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.view.addEventListener('create-overlay', e => {
|
|
||||||
const { index } = e.detail
|
|
||||||
const list = this.annotations.get(index)
|
|
||||||
if (list) for (const annotation of list)
|
|
||||||
this.view.addAnnotation(annotation)
|
|
||||||
})
|
|
||||||
this.view.addEventListener('draw-annotation', e => {
|
|
||||||
const { draw, annotation } = e.detail
|
|
||||||
const { color } = annotation
|
|
||||||
draw(Overlayer.highlight, { color })
|
|
||||||
})
|
|
||||||
this.view.addEventListener('show-annotation', e => {
|
|
||||||
const annotation = this.annotationsByValue.get(e.detail.value)
|
|
||||||
if (annotation.note) alert(annotation.note)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
this.view.addEventListener("create-overlay", (e) => {
|
||||||
|
const { index } = e.detail;
|
||||||
|
const list = this.annotations.get(index);
|
||||||
|
if (list)
|
||||||
|
for (const annotation of list) this.view.addAnnotation(annotation);
|
||||||
|
});
|
||||||
|
this.view.addEventListener("draw-annotation", (e) => {
|
||||||
|
const { draw, annotation } = e.detail;
|
||||||
|
const { color } = annotation;
|
||||||
|
draw(Overlayer.highlight, { color });
|
||||||
|
});
|
||||||
|
this.view.addEventListener("show-annotation", (e) => {
|
||||||
|
const annotation = this.annotationsByValue.get(e.detail.value);
|
||||||
|
if (annotation.note) alert(annotation.note);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
#handleKeydown(event) {
|
}
|
||||||
const k = event.key
|
#handleKeydown(event) {
|
||||||
if (k === 'ArrowLeft' || k === 'h') this.view.goLeft()
|
const k = event.key;
|
||||||
else if(k === 'ArrowRight' || k === 'l') this.view.goRight()
|
const renderer = this.view?.renderer;
|
||||||
|
|
||||||
|
// Check if in panel mode
|
||||||
|
if (renderer?.hasAttribute?.("panel-mode")) {
|
||||||
|
if (k === "ArrowRight" || k === "l") {
|
||||||
|
event.preventDefault();
|
||||||
|
renderer.nextPanel();
|
||||||
|
} else if (k === "ArrowLeft" || k === "h") {
|
||||||
|
event.preventDefault();
|
||||||
|
renderer.prevPanel();
|
||||||
|
} else if (k === "Escape" || k === "p") {
|
||||||
|
event.preventDefault();
|
||||||
|
renderer.togglePanelMode();
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
#onLoad({ detail: { doc } }) {
|
if (k === "ArrowLeft" || k === "h") this.view.goLeft();
|
||||||
doc.addEventListener('keydown', this.#handleKeydown.bind(this))
|
else if (k === "ArrowRight" || k === "l") this.view.goRight();
|
||||||
}
|
else if (k === "p") {
|
||||||
#onRelocate({ detail }) {
|
event.preventDefault();
|
||||||
const { fraction, location, tocItem, pageItem } = detail
|
renderer?.togglePanelMode?.();
|
||||||
const percent = percentFormat.format(fraction)
|
|
||||||
const loc = pageItem
|
|
||||||
? `Page ${pageItem.label}`
|
|
||||||
: `Loc ${location.current}`
|
|
||||||
const slider = $('#progress-slider')
|
|
||||||
slider.style.visibility = 'visible'
|
|
||||||
slider.value = fraction
|
|
||||||
slider.title = `${percent} · ${loc}`
|
|
||||||
if (tocItem?.href) this.#tocView?.setCurrentHref?.(tocItem.href)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
#onLoad({ detail: { doc } }) {
|
||||||
|
doc.addEventListener("keydown", this.#handleKeydown.bind(this));
|
||||||
|
}
|
||||||
|
#onRelocate({ detail }) {
|
||||||
|
const { fraction, location, tocItem, pageItem } = detail;
|
||||||
|
const percent = percentFormat.format(fraction);
|
||||||
|
const loc = pageItem ? `Page ${pageItem.label}` : `Loc ${location.current}`;
|
||||||
|
const slider = $("#progress-slider");
|
||||||
|
slider.style.visibility = "visible";
|
||||||
|
slider.value = fraction;
|
||||||
|
slider.title = `${percent} · ${loc}`;
|
||||||
|
if (tocItem?.href) this.#tocView?.setCurrentHref?.(tocItem.href);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const open = async file => {
|
const open = async (file) => {
|
||||||
document.body.removeChild($('#drop-target'))
|
document.body.removeChild($("#drop-target"));
|
||||||
const reader = new Reader()
|
const reader = new Reader();
|
||||||
globalThis.reader = reader
|
globalThis.reader = reader;
|
||||||
await reader.open(file)
|
await reader.open(file);
|
||||||
}
|
};
|
||||||
|
|
||||||
const dragOverHandler = e => e.preventDefault()
|
const dragOverHandler = (e) => e.preventDefault();
|
||||||
const dropHandler = e => {
|
const dropHandler = (e) => {
|
||||||
e.preventDefault()
|
e.preventDefault();
|
||||||
const item = Array.from(e.dataTransfer.items)
|
const item = Array.from(e.dataTransfer.items).find(
|
||||||
.find(item => item.kind === 'file')
|
(item) => item.kind === "file",
|
||||||
if (item) {
|
);
|
||||||
const entry = item.webkitGetAsEntry()
|
if (item) {
|
||||||
open(entry.isFile ? item.getAsFile() : entry).catch(e => console.error(e))
|
const entry = item.webkitGetAsEntry();
|
||||||
}
|
open(entry.isFile ? item.getAsFile() : entry).catch((e) =>
|
||||||
}
|
console.error(e),
|
||||||
const dropTarget = $('#drop-target')
|
);
|
||||||
dropTarget.addEventListener('drop', dropHandler)
|
}
|
||||||
dropTarget.addEventListener('dragover', dragOverHandler)
|
};
|
||||||
|
const dropTarget = $("#drop-target");
|
||||||
|
dropTarget.addEventListener("drop", dropHandler);
|
||||||
|
dropTarget.addEventListener("dragover", dragOverHandler);
|
||||||
|
|
||||||
$('#file-input').addEventListener('change', e =>
|
$("#file-input").addEventListener("change", (e) =>
|
||||||
open(e.target.files[0]).catch(e => console.error(e)))
|
open(e.target.files[0]).catch((e) => console.error(e)),
|
||||||
$('#file-button').addEventListener('click', () => $('#file-input').click())
|
);
|
||||||
|
$("#file-button").addEventListener("click", () => $("#file-input").click());
|
||||||
|
|
||||||
const params = new URLSearchParams(location.search)
|
const params = new URLSearchParams(location.search);
|
||||||
const url = params.get('url')
|
const url = params.get("url");
|
||||||
if (url) open(url).catch(e => console.error(e))
|
if (url) open(url).catch((e) => console.error(e));
|
||||||
else dropTarget.style.visibility = 'visible'
|
else dropTarget.style.visibility = "visible";
|
||||||
|
|||||||
Reference in New Issue
Block a user