Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes. Co-authored-by: Cursor <cursoragent@cursor.com>
447 lines
13 KiB
TypeScript
447 lines
13 KiB
TypeScript
'use client';
|
||
|
||
import Link from 'next/link';
|
||
import { useEffect, useState } from 'react';
|
||
import { createPortal } from 'react-dom';
|
||
import type { Book, LoadedChapter } from '@/lib/book-types';
|
||
import { calcBookProgress, getReadingProgress } from '@/lib/reading-progress';
|
||
import { BOOKMARKS_EVENT, getBookmarks, type Bookmark } from '@/lib/bookmarks';
|
||
import {
|
||
getHighlights,
|
||
HIGHLIGHTS_EVENT,
|
||
removeHighlight,
|
||
type Highlight,
|
||
} from '@/lib/highlights';
|
||
import { extractHeadings } from '@/lib/headings';
|
||
import { useT } from '@/components/LocaleProvider';
|
||
|
||
function scrollToId(id: string) {
|
||
document.getElementById(id)?.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
||
}
|
||
|
||
function scrollToChapterSlug(slug: string) {
|
||
scrollToId(slug);
|
||
history.replaceState(null, '', `/read#${slug}`);
|
||
}
|
||
|
||
function ChapterNavList({
|
||
book,
|
||
chapters,
|
||
activeSlug,
|
||
filter,
|
||
onNavigate,
|
||
}: {
|
||
book: Book;
|
||
chapters: LoadedChapter[];
|
||
activeSlug: string;
|
||
filter: string;
|
||
onNavigate?: () => void;
|
||
}) {
|
||
const t = useT();
|
||
const [openSlugs, setOpenSlugs] = useState<Set<string>>(() => new Set());
|
||
|
||
function toggle(slug: string) {
|
||
setOpenSlugs((prev) => {
|
||
const next = new Set(prev);
|
||
if (next.has(slug)) next.delete(slug);
|
||
else next.add(slug);
|
||
return next;
|
||
});
|
||
}
|
||
|
||
return (
|
||
<>
|
||
{book.chapters
|
||
.map((chapter, i) => ({ chapter, i }))
|
||
.filter(
|
||
({ chapter }) =>
|
||
!filter.trim() ||
|
||
chapter.title.toLowerCase().includes(filter.trim().toLowerCase())
|
||
)
|
||
.map(({ chapter, i }) => {
|
||
const active = activeSlug === chapter.slug;
|
||
const loaded = chapters.find((c) => c.slug === chapter.slug);
|
||
const headings = loaded
|
||
? extractHeadings(loaded.content).filter((h) => h.level === 2)
|
||
: [];
|
||
const expanded = openSlugs.has(chapter.slug);
|
||
|
||
return (
|
||
<div key={chapter.slug}>
|
||
<div
|
||
style={{
|
||
display: 'flex',
|
||
alignItems: 'center',
|
||
width: '100%',
|
||
marginBottom: 4,
|
||
}}
|
||
>
|
||
{headings.length > 0 ? (
|
||
<button
|
||
type="button"
|
||
aria-expanded={expanded}
|
||
aria-label={expanded ? t('reader.collapseToc') : t('reader.expandToc')}
|
||
onClick={() => toggle(chapter.slug)}
|
||
style={{
|
||
flexShrink: 0,
|
||
display: 'flex',
|
||
alignItems: 'center',
|
||
justifyContent: 'center',
|
||
width: 22,
|
||
height: 36,
|
||
border: 'none',
|
||
background: 'transparent',
|
||
color: 'var(--book-muted)',
|
||
cursor: 'pointer',
|
||
padding: 0,
|
||
}}
|
||
>
|
||
<svg
|
||
width="12"
|
||
height="12"
|
||
viewBox="0 0 24 24"
|
||
fill="none"
|
||
stroke="currentColor"
|
||
strokeWidth="2.4"
|
||
style={{
|
||
transform: expanded ? 'rotate(90deg)' : 'none',
|
||
transition: 'transform 0.15s ease',
|
||
}}
|
||
>
|
||
<path d="M9 6l6 6-6 6" />
|
||
</svg>
|
||
</button>
|
||
) : (
|
||
<span style={{ width: 22, flexShrink: 0 }} aria-hidden />
|
||
)}
|
||
<a
|
||
href={`/read#${chapter.slug}`}
|
||
onClick={(e) => {
|
||
e.preventDefault();
|
||
scrollToChapterSlug(chapter.slug);
|
||
onNavigate?.();
|
||
}}
|
||
className={`chapter-sidebar-item ${active ? 'is-active' : ''}`}
|
||
style={{ flex: 1, minWidth: 0, marginBottom: 0, padding: '8px 8px 8px 6px' }}
|
||
>
|
||
<span className="chapter-sidebar-index">
|
||
{String(i + 1).padStart(2, '0')}
|
||
</span>
|
||
<span
|
||
className="chapter-sidebar-name"
|
||
style={{ display: 'flex', alignItems: 'baseline', gap: 4, minWidth: 0 }}
|
||
>
|
||
<span className="chapter-sidebar-emoji" aria-hidden style={{ flexShrink: 0 }}>
|
||
{chapter.emoji}
|
||
</span>
|
||
<span
|
||
style={{
|
||
minWidth: 0,
|
||
overflow: 'hidden',
|
||
textOverflow: 'ellipsis',
|
||
whiteSpace: 'nowrap',
|
||
}}
|
||
>
|
||
{chapter.title}
|
||
</span>
|
||
</span>
|
||
</a>
|
||
</div>
|
||
{expanded && headings.length > 0 && (
|
||
<ul
|
||
style={{
|
||
listStyle: 'none',
|
||
margin: '0 0 8px 50px',
|
||
padding: '0 0 0 10px',
|
||
borderLeft: '1px solid var(--book-border)',
|
||
}}
|
||
>
|
||
{headings.map((h) => (
|
||
<li key={h.id}>
|
||
<a
|
||
href={`#${chapter.slug}--${h.id}`}
|
||
onClick={(e) => {
|
||
e.preventDefault();
|
||
scrollToId(`${chapter.slug}--${h.id}`);
|
||
onNavigate?.();
|
||
}}
|
||
style={{
|
||
display: 'block',
|
||
padding: '4px 0',
|
||
fontSize: 12,
|
||
color: 'var(--book-muted)',
|
||
textDecoration: 'none',
|
||
lineHeight: 1.4,
|
||
}}
|
||
>
|
||
{h.text}
|
||
</a>
|
||
</li>
|
||
))}
|
||
</ul>
|
||
)}
|
||
</div>
|
||
);
|
||
})}
|
||
</>
|
||
);
|
||
}
|
||
|
||
function SidebarContent({
|
||
book,
|
||
chapters,
|
||
activeSlug,
|
||
readChapters,
|
||
remainingMins,
|
||
bookmarks,
|
||
highlights,
|
||
filter,
|
||
setFilter,
|
||
onNavigate,
|
||
}: {
|
||
book: Book;
|
||
chapters: LoadedChapter[];
|
||
activeSlug: string;
|
||
readChapters: string[];
|
||
remainingMins: number;
|
||
bookmarks: Bookmark[];
|
||
highlights: Highlight[];
|
||
filter: string;
|
||
setFilter: (v: string) => void;
|
||
onNavigate?: () => void;
|
||
}) {
|
||
const t = useT();
|
||
const percent = calcBookProgress(readChapters.length, book.chapters.length);
|
||
|
||
return (
|
||
<>
|
||
<div className="chapter-sidebar-header">
|
||
<p className="chapter-sidebar-label">《{book.title}》</p>
|
||
<p className="chapter-sidebar-title">{t('reader.toc')}</p>
|
||
<div className="chapter-sidebar-progress">
|
||
<p>
|
||
{t('reader.readProgress', {
|
||
read: readChapters.length,
|
||
total: book.chapters.length,
|
||
mins: remainingMins,
|
||
})}
|
||
</p>
|
||
<div className="chapter-sidebar-progress-track" aria-hidden>
|
||
<div className="chapter-sidebar-progress-fill" style={{ width: `${percent}%` }} />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="chapter-sidebar-search-wrap">
|
||
<input
|
||
type="search"
|
||
value={filter}
|
||
onChange={(e) => setFilter(e.target.value)}
|
||
placeholder={t('reader.filterChapters')}
|
||
className="chapter-sidebar-search"
|
||
/>
|
||
</div>
|
||
|
||
<nav className="chapter-sidebar-list" aria-label={t('reader.chapterPreview')}>
|
||
<ChapterNavList
|
||
book={book}
|
||
chapters={chapters}
|
||
activeSlug={activeSlug}
|
||
filter={filter}
|
||
onNavigate={onNavigate}
|
||
/>
|
||
</nav>
|
||
|
||
{bookmarks.length > 0 && (
|
||
<div className="chapter-sidebar-bookmarks">
|
||
<p className="chapter-sidebar-bookmarks-label">{t('reader.bookmarks')}</p>
|
||
{bookmarks.map((bm) => (
|
||
<button
|
||
key={`${bm.slug}-${bm.createdAt}`}
|
||
type="button"
|
||
className="chapter-sidebar-bookmark"
|
||
onClick={() => {
|
||
scrollToChapterSlug(bm.slug);
|
||
window.setTimeout(() => {
|
||
window.scrollTo({ top: bm.scrollY, behavior: 'smooth' });
|
||
}, 80);
|
||
onNavigate?.();
|
||
}}
|
||
>
|
||
★ {bm.title}
|
||
</button>
|
||
))}
|
||
</div>
|
||
)}
|
||
|
||
{highlights.length > 0 && (
|
||
<div className="chapter-sidebar-bookmarks">
|
||
<p className="chapter-sidebar-bookmarks-label">{t('reader.highlights')}</p>
|
||
{highlights.map((hl) => (
|
||
<div key={hl.id} className="chapter-sidebar-highlight">
|
||
<button
|
||
type="button"
|
||
className="chapter-sidebar-bookmark"
|
||
onClick={() => {
|
||
scrollToChapterSlug(hl.slug);
|
||
window.setTimeout(() => {
|
||
document
|
||
.querySelector(`mark[data-highlight="${hl.id}"]`)
|
||
?.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||
}, 320);
|
||
onNavigate?.();
|
||
}}
|
||
>
|
||
{hl.text}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
className="chapter-sidebar-highlight-del"
|
||
aria-label={t('reader.deleteHighlight')}
|
||
onClick={() => removeHighlight(hl.id)}
|
||
>
|
||
×
|
||
</button>
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
|
||
<div className="chapter-sidebar-footer">
|
||
<Link href="/" className="chapter-sidebar-back" onClick={onNavigate}>
|
||
{t('reader.backCover')}
|
||
</Link>
|
||
</div>
|
||
</>
|
||
);
|
||
}
|
||
|
||
export function ChapterSidebarToggle({ onOpen }: { onOpen: () => void }) {
|
||
const t = useT();
|
||
return (
|
||
<button
|
||
type="button"
|
||
className="chapter-sidebar-toggle"
|
||
aria-label={t('reader.openToc')}
|
||
onClick={onOpen}
|
||
>
|
||
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||
<path d="M4 6h16M4 12h16M4 18h10" />
|
||
</svg>
|
||
<span>{t('reader.toc')}</span>
|
||
</button>
|
||
);
|
||
}
|
||
|
||
export function ChapterSidebar({
|
||
book,
|
||
chapters,
|
||
activeSlug,
|
||
remainingMins,
|
||
mobileOpen,
|
||
onMobileClose,
|
||
}: {
|
||
book: Book;
|
||
chapters: LoadedChapter[];
|
||
activeSlug: string;
|
||
remainingMins: number;
|
||
mobileOpen: boolean;
|
||
onMobileClose: () => void;
|
||
}) {
|
||
const t = useT();
|
||
const [readChapters, setReadChapters] = useState<string[]>([]);
|
||
const [bookmarks, setBookmarks] = useState<Bookmark[]>([]);
|
||
const [highlights, setHighlights] = useState<Highlight[]>([]);
|
||
const [filter, setFilter] = useState('');
|
||
const [mounted, setMounted] = useState(false);
|
||
|
||
useEffect(() => setMounted(true), []);
|
||
|
||
useEffect(() => {
|
||
const saved = getReadingProgress();
|
||
setReadChapters(saved?.readChapters ?? []);
|
||
}, [activeSlug]);
|
||
|
||
useEffect(() => {
|
||
const refresh = () => setBookmarks(getBookmarks());
|
||
refresh();
|
||
window.addEventListener(BOOKMARKS_EVENT, refresh);
|
||
return () => window.removeEventListener(BOOKMARKS_EVENT, refresh);
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
const refresh = () => setHighlights(getHighlights());
|
||
refresh();
|
||
window.addEventListener(HIGHLIGHTS_EVENT, refresh);
|
||
return () => window.removeEventListener(HIGHLIGHTS_EVENT, refresh);
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
if (!mobileOpen) return;
|
||
document.documentElement.classList.add('drawer-lock');
|
||
document.body.classList.add('drawer-lock');
|
||
return () => {
|
||
document.documentElement.classList.remove('drawer-lock');
|
||
document.body.classList.remove('drawer-lock');
|
||
};
|
||
}, [mobileOpen]);
|
||
|
||
useEffect(() => {
|
||
if (!mobileOpen) return;
|
||
const onKeyDown = (e: KeyboardEvent) => {
|
||
if (e.key === 'Escape') onMobileClose();
|
||
};
|
||
window.addEventListener('keydown', onKeyDown);
|
||
return () => window.removeEventListener('keydown', onKeyDown);
|
||
}, [mobileOpen, onMobileClose]);
|
||
|
||
const contentProps = {
|
||
book,
|
||
chapters,
|
||
activeSlug,
|
||
readChapters,
|
||
remainingMins,
|
||
bookmarks,
|
||
highlights,
|
||
filter,
|
||
setFilter,
|
||
};
|
||
|
||
const mobileDrawer =
|
||
mounted &&
|
||
createPortal(
|
||
<div
|
||
className={`chapter-sidebar-mobile ${mobileOpen ? 'is-open' : ''}`}
|
||
aria-hidden={!mobileOpen}
|
||
>
|
||
<button
|
||
type="button"
|
||
className="chapter-sidebar-backdrop"
|
||
aria-label={t('reader.closeToc')}
|
||
onClick={onMobileClose}
|
||
/>
|
||
<aside className="chapter-sidebar-panel" role="dialog" aria-modal="true" aria-label={t('reader.chapterPreview')}>
|
||
<button
|
||
type="button"
|
||
className="chapter-sidebar-close"
|
||
onClick={onMobileClose}
|
||
aria-label={t('common.close')}
|
||
>
|
||
×
|
||
</button>
|
||
<SidebarContent {...contentProps} onNavigate={onMobileClose} />
|
||
</aside>
|
||
</div>,
|
||
document.body
|
||
);
|
||
|
||
return (
|
||
<>
|
||
<aside className="chapter-sidebar">
|
||
<SidebarContent {...contentProps} />
|
||
</aside>
|
||
{mobileDrawer}
|
||
</>
|
||
);
|
||
}
|