Skip to content
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
4 changes: 4 additions & 0 deletions sample-book/OEBPS/ch_1.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
Uji skenario batas lokal (Intra-file Edge Case): tautan catatan lokal yang mengarah ke ID yang tidak ada di bab ini<a href="#anchor-lokal-hilang" id="ref-local-broken"><sup>[100]</sup></a>.
</p>

<p style="margin-top: 2rem; padding: 1rem; border: 1px dashed rgba(156, 163, 175, 0.4); border-radius: 8px;">
📖 <strong>Eksplorasi Navigasi Antar-Bab:</strong> Klik tautan ini untuk melompat langsung ke <a href="notes.xhtml">Dokumen Endnotes Terpisah</a>. Setelah melompat, floating return pill dan shortcut <code>Alt+Left</code> akan aktif untuk mengembalikan posisi baca ke paragraf ini.
</p>

<hr style="margin: 3rem 0; border: none; border-top: 1px solid #e5e7eb;"/>

<!-- Local Footnote Section -->
Expand Down
125 changes: 120 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from 'node:path';
import { sanitizeXhtml, extractElementById } from './xhtml';
import { readerShell, ReaderNavInfo } from './reader-shell';
import { locateEpubContainer, parseContainerXml, parseOpfXml, loadEpubToc, EpubPackage } from './epub';
import { StateManager, ReaderPreferences } from './state-manager';
import { StateManager, ReaderPreferences, NavigationHistory, ReadingLocation } from './state-manager';
import { EpubCacheManager, ArchiveSecurityError, ArchiveCorruptError } from './archive';

const supportedExtensions = new Set(['.xhtml', '.html', '.htm', '.epub']);
Expand Down Expand Up @@ -200,9 +200,10 @@ async function attachReaderToWebview(

panel.webview.options = { enableScripts: true, localResourceRoots };

const navigationHistory = new NavigationHistory();
let timer: NodeJS.Timeout | undefined;

const render = async (resetToTop = false, startAtBottom = false): Promise<void> => {
const render = async (resetToTop = false, startAtBottom = false, targetLocation?: ReadingLocation): Promise<void> => {
try {
const xhtml = await readSource(currentSource);
const chapterDir = parentUri(currentSource);
Expand All @@ -225,19 +226,27 @@ async function attachReaderToWebview(
};
});

const peek = navigationHistory.peekBack();
const historyBackInfo = peek ? {
canGoBack: true,
chapterTitle: peek.chapterTitle,
scrollRatio: peek.scrollRatio
} : undefined;

const navInfo: ReaderNavInfo = {
hasPrev: spineIndex > 0,
hasNext: spineIndex >= 0 && spineIndex < (epubPackage?.spine.length ?? 0) - 1,
bookTitle: epubPackage?.title,
creator: epubPackage?.creator,
chapterIndex: spineIndex >= 0 ? spineIndex + 1 : undefined,
totalChapters: epubPackage?.spine.length,
toc: tocWithActive
toc: tocWithActive,
historyBackInfo
};

panel.title = getPanelTitle(currentSource, epubPackage);
const preferences = stateManager?.getPreferences();
const location = (resetToTop || startAtBottom) ? undefined : stateManager?.getReadingLocation(currentSource.toString());
const location = targetLocation || ((resetToTop || startAtBottom) ? undefined : stateManager?.getReadingLocation(currentSource.toString()));
panel.webview.html = readerShell(content, currentSource, pendingFragment, panel.webview, navInfo, preferences, location, resetToTop, startAtBottom);
pendingFragment = undefined;
} catch (error) {
Expand Down Expand Up @@ -352,10 +361,71 @@ async function attachReaderToWebview(
if (isOpenLinkMessage(message)) {
const target = resolveChapterLink(message.href, currentSource, watchRoot, epubPackage?.opfDir);
if (target) {
const chapterTitle = getChapterTitle(currentSource, epubPackage);
const pos = message.currentPosition || stateManager?.getReadingLocation(currentSource.toString()) || { scrollY: 0, scrollRatio: 0 };
navigationHistory.push({
uri: currentSource.toString(),
chapterTitle,
scrollY: pos.scrollY,
scrollRatio: pos.scrollRatio,
isPaginated: Boolean(pos.isPaginated)
});

currentSource = target.source;
pendingFragment = target.fragment;
void render(!target.fragment);
}
return;
}

if (isHistoryBackMessage(message)) {
if (navigationHistory.canGoBack()) {
const chapterTitle = getChapterTitle(currentSource, epubPackage);
const pos = message.currentPosition || stateManager?.getReadingLocation(currentSource.toString()) || { scrollY: 0, scrollRatio: 0 };
const prev = navigationHistory.goBack({
uri: currentSource.toString(),
chapterTitle,
scrollY: pos.scrollY,
scrollRatio: pos.scrollRatio,
isPaginated: Boolean(pos.isPaginated)
});
if (prev) {
currentSource = vscode.Uri.parse(prev.uri);
pendingFragment = prev.anchorId;
void render(false, false, {
scrollY: prev.scrollY,
scrollRatio: prev.scrollRatio,
anchorId: prev.anchorId,
timestamp: Date.now()
});
}
}
return;
}

if (isHistoryForwardMessage(message)) {
if (navigationHistory.canGoForward()) {
const chapterTitle = getChapterTitle(currentSource, epubPackage);
const pos = message.currentPosition || stateManager?.getReadingLocation(currentSource.toString()) || { scrollY: 0, scrollRatio: 0 };
const next = navigationHistory.goForward({
uri: currentSource.toString(),
chapterTitle,
scrollY: pos.scrollY,
scrollRatio: pos.scrollRatio,
isPaginated: Boolean(pos.isPaginated)
});
if (next) {
currentSource = vscode.Uri.parse(next.uri);
pendingFragment = next.anchorId;
void render(false, false, {
scrollY: next.scrollY,
scrollRatio: next.scrollRatio,
anchorId: next.anchorId,
timestamp: Date.now()
});
}
}
return;
}
});

Expand All @@ -369,6 +439,7 @@ async function attachReaderToWebview(
if (timer) {
clearTimeout(timer);
}
navigationHistory.clear();
watcher.dispose();
documentChange.dispose();
messageSubscription.dispose();
Expand All @@ -390,6 +461,28 @@ function getPanelTitle(source: vscode.Uri, epub?: EpubPackage): string {
return `${epub.title} — ${fileName}`;
}

function getChapterTitle(source: vscode.Uri, epub?: EpubPackage): string {
const fileName = source.path.slice(source.path.lastIndexOf('/') + 1);
if (epub) {
const currentPath = source.path.toLowerCase();
const tocMatch = epub.toc.find((item) => item.uri.path.toLowerCase() === currentPath);
if (tocMatch && tocMatch.label) {
return tocMatch.label;
}
const spineIdx = epub.spine.findIndex((item) => item.uri.path.toLowerCase() === currentPath);
if (spineIdx >= 0) {
return `Chapter ${spineIdx + 1}`;
}
}
return fileName;
}

export interface ReadingPositionPayload {
scrollY: number;
scrollRatio: number;
isPaginated?: boolean;
}

function isPrevChapterMessage(message: unknown): message is { type: 'prevChapter'; startAtBottom?: boolean } {
return typeof message === 'object' && message !== null && (message as { type?: unknown }).type === 'prevChapter';
}
Expand All @@ -398,13 +491,35 @@ function isNextChapterMessage(message: unknown): message is { type: 'nextChapter
return typeof message === 'object' && message !== null && (message as { type?: unknown }).type === 'nextChapter';
}

function isOpenLinkMessage(message: unknown): message is { type: 'openLink'; href: string } {
function isOpenLinkMessage(message: unknown): message is {
type: 'openLink';
href: string;
currentPosition?: ReadingPositionPayload;
} {
return typeof message === 'object'
&& message !== null
&& (message as { type?: unknown }).type === 'openLink'
&& typeof (message as { href?: unknown }).href === 'string';
}

function isHistoryBackMessage(message: unknown): message is {
type: 'historyBack';
currentPosition?: ReadingPositionPayload;
} {
return typeof message === 'object'
&& message !== null
&& (message as { type?: unknown }).type === 'historyBack';
}

function isHistoryForwardMessage(message: unknown): message is {
type: 'historyForward';
currentPosition?: ReadingPositionPayload;
} {
return typeof message === 'object'
&& message !== null
&& (message as { type?: unknown }).type === 'historyForward';
}

function isRequestFootnoteMessage(message: unknown): message is {
type: 'requestFootnote';
href: string;
Expand Down
Loading
Loading