refactor(reader): rewrite reader module for foliate-js pan/zoom integration
Major rewrite of the web reader to properly interface with
@bookhoard/foliate-js, replacing the abandoned panel-detection
architecture with direct pan and zoom support built into the
foliate-js FixedLayout renderer.
Template (reader.templ):
- Fix critical bug: x-init config was using literal strings
'{ readerData.X }' inside a quoted attribute, which templ
treated as raw text and never interpolated. Values were never
actually passed to JavaScript. Now uses fmt.Sprintf() with
templ's expression attribute syntax ={ }.
- Pass fileUrl from server so foliate-js can open books directly.
- Redesign bottom bar with foliate-js parity: left/right navigation
buttons, progress slider with tick marks, and zoom controls
(zoom out, percentage display, zoom in, magnifier, pan/select
mode toggle for PDFs).
- Remove panel editor button and enablePanelDetection config.
- Add SVG icon styles for consistent reader controls.
Go types (templates/types.go):
- Expand ReaderMetadata with FormatGroup, MangaType,
ReadingDirection, FileURL, and LibraryID fields needed by
the reader frontend.
Router (internal/router/reader.go):
- Populate new ReaderMetadata fields from database values.
- Construct FileURL from library ID and file path for the
/uploads/library-{id}/* file serving route.
Reader JS (reader.ts):
- Full rewrite modeled on foliate-js Reader class, adapted for
Alpine.js. Opens books via view.open(fileUrl), accesses
view.renderer for zoom/pan/navigation, and wires up keyboard
shortcuts (+/-/0 for zoom, arrows for nav, Escape for magnifier).
- Uses view.isFixedLayout instead of importing FixedLayout class,
avoiding a TypeScript module resolution issue with the Vite alias.
Settings manager (settings-manager.ts):
- Remove dependency on deleted ReaderContext event bus.
- Export loadSettings/saveSettings/syncSettings directly as
standalone async functions.
Cleanup:
- Delete reader-context.ts and reader-events.ts (over-engineered
event system replaced by direct function calls).
- Remove panel_zoom_enabled from ReaderSettings type.
This commit is contained in:
@@ -1,64 +1,12 @@
|
||||
// Per-user settings with localStorage fallback
|
||||
// Feature Registration Pattern implementation
|
||||
|
||||
import type { ReaderContext } from "./core/reader-context";
|
||||
import { apiGet, apiPut } from "../api";
|
||||
import { getToken } from "../storage";
|
||||
|
||||
const LOCALSTORAGE_KEY = "reader_settings_local";
|
||||
|
||||
export function init(context: ReaderContext): void {
|
||||
let currentSettings: ReaderSettings | null = null;
|
||||
|
||||
context.events.on("reader:init", async () => {
|
||||
currentSettings = await loadSettings();
|
||||
context.events.emit("settings:loaded", currentSettings);
|
||||
});
|
||||
|
||||
context.events.on(
|
||||
"settings:save",
|
||||
async (detail: { settings: Partial<ReaderSettings> }) => {
|
||||
await saveSettings(detail.settings);
|
||||
currentSettings = await loadSettings();
|
||||
context.events.emit("settings:changed", currentSettings);
|
||||
},
|
||||
);
|
||||
|
||||
context.events.on(
|
||||
"settings:get",
|
||||
(detail: { key?: keyof ReaderSettings }) => {
|
||||
if (currentSettings) {
|
||||
const value = detail.key
|
||||
? currentSettings[detail.key]
|
||||
: currentSettings;
|
||||
context.events.emit("settings:current", { value });
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
context.events.on(
|
||||
"settings:set",
|
||||
async (detail: { key: keyof ReaderSettings; value: any }) => {
|
||||
await saveSettings({ [detail.key]: detail.value });
|
||||
currentSettings = await loadSettings();
|
||||
context.events.emit("settings:changed", currentSettings);
|
||||
},
|
||||
);
|
||||
|
||||
context.events.on("settings:sync", async () => {
|
||||
await syncSettings();
|
||||
currentSettings = await loadSettings();
|
||||
context.events.emit("settings:synced", currentSettings);
|
||||
});
|
||||
}
|
||||
|
||||
export async function loadSettings(): Promise<ReaderSettings> {
|
||||
const token = getToken();
|
||||
if (!token) {
|
||||
const local = localStorage.getItem(LOCALSTORAGE_KEY);
|
||||
return local ? JSON.parse(local) : getDefaultSettings();
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await apiGet("/readers/settings");
|
||||
const settings = await response.json();
|
||||
@@ -69,17 +17,16 @@ export async function loadSettings(): Promise<ReaderSettings> {
|
||||
return local ? JSON.parse(local) : getDefaultSettings();
|
||||
}
|
||||
}
|
||||
|
||||
async function saveSettings(settings: Partial<ReaderSettings>): Promise<void> {
|
||||
export async function saveSettings(
|
||||
settings: Partial<ReaderSettings>,
|
||||
): Promise<void> {
|
||||
const token = getToken();
|
||||
const current = await loadSettings();
|
||||
const updated = { ...current, ...settings };
|
||||
|
||||
if (!token) {
|
||||
localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(updated));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await apiPut("/readers/settings", settings);
|
||||
localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(updated));
|
||||
@@ -87,14 +34,11 @@ async function saveSettings(settings: Partial<ReaderSettings>): Promise<void> {
|
||||
localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(updated));
|
||||
}
|
||||
}
|
||||
|
||||
async function syncSettings(): Promise<void> {
|
||||
export async function syncSettings(): Promise<void> {
|
||||
const local = localStorage.getItem(LOCALSTORAGE_KEY);
|
||||
if (!local) return;
|
||||
|
||||
const settings = JSON.parse(local);
|
||||
const token = getToken();
|
||||
|
||||
if (token) {
|
||||
try {
|
||||
await apiPut("/readers/settings", settings);
|
||||
@@ -103,7 +47,6 @@ async function syncSettings(): Promise<void> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getDefaultSettings(): ReaderSettings {
|
||||
return {
|
||||
chrome_behavior: "auto-hide",
|
||||
@@ -113,7 +56,6 @@ export function getDefaultSettings(): ReaderSettings {
|
||||
reading_font: "literata",
|
||||
tap_zone_size: 30,
|
||||
auto_scroll: false,
|
||||
panel_zoom_enabled: true,
|
||||
font_size: 16,
|
||||
line_height: 1.6,
|
||||
margin_width: 20,
|
||||
|
||||
Reference in New Issue
Block a user