reader-view.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. "use client";
  2. import Link from "next/link";
  3. import { CSSProperties, useEffect, useMemo, useRef, useState } from "react";
  4. import { getBookEntries } from "@/lib/book-helpers";
  5. import { Book } from "@/types/book";
  6. type ReaderViewProps = {
  7. book: Book;
  8. chapterIndex: number;
  9. initialThemeKey?: string;
  10. initialFontKey?: string;
  11. initialWidthKey?: string;
  12. };
  13. const themeOptions = [
  14. { key: "warm", label: "暖米", page: "#f7f0e4", stage: "#d8d0c4", text: "#2b241e", catalog: "#e6ddd0" },
  15. { key: "mist", label: "浅灰", page: "#efede7", stage: "#d6d4cf", text: "#2a2723", catalog: "#dfddd7" },
  16. { key: "sepia", label: "茶褐", page: "#eee0cf", stage: "#d4c3b1", text: "#35281f", catalog: "#dfcdbb" }
  17. ] as const;
  18. const fontOptions = [
  19. { key: "sm", label: "小", size: "1.02rem", lineHeight: 2.05 },
  20. { key: "md", label: "中", size: "1.18rem", lineHeight: 2.28 },
  21. { key: "lg", label: "大", size: "1.34rem", lineHeight: 2.55 }
  22. ] as const;
  23. const widthOptions = [
  24. { key: "narrow", label: "窄", width: 760 },
  25. { key: "medium", label: "中", width: 920 },
  26. { key: "wide", label: "宽", width: 1080 }
  27. ] as const;
  28. export function ReaderView({
  29. book,
  30. chapterIndex,
  31. initialThemeKey,
  32. initialFontKey,
  33. initialWidthKey
  34. }: ReaderViewProps) {
  35. const entries = useMemo(() => getBookEntries(book), [book]);
  36. const entry = entries[chapterIndex];
  37. const prevChapterIndex = chapterIndex > 0 ? chapterIndex - 1 : null;
  38. const nextChapterIndex = chapterIndex < entries.length - 1 ? chapterIndex + 1 : null;
  39. const initialThemeIndex = Math.max(0, themeOptions.findIndex((item) => item.key === initialThemeKey));
  40. const initialFontIndex = Math.max(0, fontOptions.findIndex((item) => item.key === initialFontKey));
  41. const initialWidthIndex = Math.max(0, widthOptions.findIndex((item) => item.key === initialWidthKey));
  42. const [themeIndex, setThemeIndex] = useState(initialThemeKey ? initialThemeIndex : 0);
  43. const [fontIndex, setFontIndex] = useState(initialFontKey ? initialFontIndex : 1);
  44. const [widthIndex, setWidthIndex] = useState(initialWidthKey ? initialWidthIndex : 1);
  45. const [catalogOpen, setCatalogOpen] = useState(false);
  46. const [progress, setProgress] = useState(0);
  47. const stageRef = useRef<HTMLElement | null>(null);
  48. useEffect(() => {
  49. const pageColor = themeOptions[themeIndex].page;
  50. document.body.classList.add("reader-body");
  51. const previousBodyBackground = document.body.style.background;
  52. const previousHtmlBackground = document.documentElement.style.background;
  53. document.body.style.background = pageColor;
  54. document.documentElement.style.background = pageColor;
  55. return () => {
  56. document.body.classList.remove("reader-body");
  57. document.body.style.background = previousBodyBackground;
  58. document.documentElement.style.background = previousHtmlBackground;
  59. };
  60. }, [themeIndex]);
  61. useEffect(() => {
  62. const stage = stageRef.current;
  63. if (!stage) return;
  64. stage.scrollTo({ top: 0, behavior: "auto" });
  65. setCatalogOpen(false);
  66. const updateProgress = () => {
  67. const maxScroll = stage.scrollHeight - stage.clientHeight;
  68. const nextProgress = maxScroll <= 0 ? 0 : (stage.scrollTop / maxScroll) * 100;
  69. setProgress(nextProgress);
  70. };
  71. updateProgress();
  72. stage.addEventListener("scroll", updateProgress);
  73. window.addEventListener("resize", updateProgress);
  74. return () => {
  75. stage.removeEventListener("scroll", updateProgress);
  76. window.removeEventListener("resize", updateProgress);
  77. };
  78. }, [chapterIndex, widthIndex, fontIndex, themeIndex]);
  79. const currentTheme = themeOptions[themeIndex];
  80. const currentFont = fontOptions[fontIndex];
  81. const currentWidth = widthOptions[widthIndex];
  82. const entryWords = entry.content.join("").length;
  83. const isLoreEntry = entry.kind === "lore";
  84. const shellWidthStyle = useMemo(
  85. () =>
  86. ({
  87. width: "var(--reader-shell-width)"
  88. }) as CSSProperties,
  89. []
  90. );
  91. const cycleFont = () => setFontIndex((value) => (value + 1) % fontOptions.length);
  92. const cycleTheme = () => setThemeIndex((value) => (value + 1) % themeOptions.length);
  93. const cycleWidth = () => setWidthIndex((value) => (value + 1) % widthOptions.length);
  94. const catalogContent = (
  95. <>
  96. <div className="reader-catalog__header">
  97. <h2>目录</h2>
  98. <button type="button" className="reader-catalog__close" onClick={() => setCatalogOpen(false)}>
  99. 关闭
  100. </button>
  101. </div>
  102. <div className="reader-catalog-groups">
  103. {book.sections
  104. .slice()
  105. .sort((a, b) => a.order - b.order)
  106. .map((section) => {
  107. const sectionEntries = entries.filter((item) => item.sectionId === section.id);
  108. return (
  109. <section className="reader-catalog__section" key={section.id}>
  110. <div className="reader-catalog__section-title">
  111. <span className={`reader-catalog__kind reader-catalog__kind--${section.kind}`}>
  112. {section.kind === "lore" ? "资料" : "正文"}
  113. </span>
  114. <strong>{section.title}</strong>
  115. </div>
  116. <div className="reader-catalog__grid">
  117. {sectionEntries.map((item, index) => (
  118. <Link
  119. key={item.id}
  120. href={`/reader/${book.id}?entry=${item.flatIndex}`}
  121. className={`reader-catalog__item${item.flatIndex === chapterIndex ? " reader-catalog__item--active" : ""}`}
  122. onClick={() => setCatalogOpen(false)}
  123. >
  124. <span>
  125. {section.kind === "lore" ? `资料 ${index + 1}` : `第 ${index + 1} 章`}
  126. </span>
  127. <strong>{item.title}</strong>
  128. </Link>
  129. ))}
  130. </div>
  131. </section>
  132. );
  133. })}
  134. </div>
  135. </>
  136. );
  137. return (
  138. <main
  139. className="reader-stage"
  140. style={
  141. {
  142. background: currentTheme.stage,
  143. "--reader-page": currentTheme.page,
  144. "--reader-shell-width": `min(${currentWidth.width}px, calc(100vw - 280px))`
  145. } as CSSProperties
  146. }
  147. ref={stageRef}
  148. >
  149. <div className="reader-progress-rail" aria-hidden="true">
  150. <div className="reader-progress-rail__track">
  151. <div className="reader-progress-rail__fill" style={{ height: `${progress}%` }} />
  152. </div>
  153. </div>
  154. <div className="reader-mobile-bar reader-mobile-bar--top" style={{ background: currentTheme.page }}>
  155. <Link className="reader-mobile-bar__icon reader-mobile-bar__icon--back" href="/library" aria-label="返回书架">
  156. 返回
  157. </Link>
  158. <button className="reader-mobile-bar__icon" type="button" onClick={cycleTheme} aria-label="切换主题">
  159. 主题 {currentTheme.label}
  160. </button>
  161. <button className="reader-mobile-bar__icon" type="button" onClick={cycleFont} aria-label="切换字体">
  162. 字体 {currentFont.label}
  163. </button>
  164. </div>
  165. <div className="reader-mobile-bar reader-mobile-bar--bottom" style={{ background: currentTheme.page }}>
  166. <button className="reader-mobile-bar__action" type="button" onClick={() => setCatalogOpen(true)}>
  167. 目录
  168. </button>
  169. {prevChapterIndex !== null ? (
  170. <Link className="reader-mobile-bar__action" href={`/reader/${book.id}?entry=${prevChapterIndex}`}>
  171. 上一章
  172. </Link>
  173. ) : (
  174. <span className="reader-mobile-bar__action reader-mobile-bar__action--disabled">上一章</span>
  175. )}
  176. {nextChapterIndex !== null ? (
  177. <Link className="reader-mobile-bar__action" href={`/reader/${book.id}?entry=${nextChapterIndex}`}>
  178. 下一章
  179. </Link>
  180. ) : (
  181. <span className="reader-mobile-bar__action reader-mobile-bar__action--disabled">下一章</span>
  182. )}
  183. </div>
  184. <div className="reader-desktop-layout">
  185. <div className="reader-float reader-float--left" aria-label="阅读左侧工具">
  186. <Link className="reader-float__button" href="/library">
  187. <strong>返回</strong>
  188. <span>回到书架</span>
  189. </Link>
  190. <button className="reader-float__button" type="button" onClick={() => setCatalogOpen(true)}>
  191. <strong>目录</strong>
  192. <span>弹出目录</span>
  193. </button>
  194. </div>
  195. <section className="reader-qq-shell" style={shellWidthStyle}>
  196. <div className="reader-catalog-mobile-mask" hidden={!catalogOpen} onClick={() => setCatalogOpen(false)} />
  197. <div
  198. className={`reader-catalog-mobile-sheet${catalogOpen ? " is-open" : ""}`}
  199. style={{ background: currentTheme.catalog }}
  200. aria-hidden={!catalogOpen}
  201. >
  202. {catalogContent}
  203. </div>
  204. {catalogOpen ? (
  205. <div className="reader-catalog-inline" style={{ background: currentTheme.catalog }}>
  206. {catalogContent}
  207. </div>
  208. ) : null}
  209. <div
  210. className={`reader-qq-paper${catalogOpen ? " reader-qq-paper--desktop-hidden" : ""}`}
  211. style={{ background: currentTheme.page }}
  212. >
  213. <header className={`reader-qq-header${isLoreEntry ? " reader-qq-header--lore" : ""}`}>
  214. <div className="reader-entry-badges">
  215. <span className={`reader-entry-badge reader-entry-badge--${entry.kind}`}>
  216. {isLoreEntry ? "资料" : "正文"}
  217. </span>
  218. <span className="reader-entry-badge reader-entry-badge--section">{entry.sectionTitle}</span>
  219. </div>
  220. <h1>{entry.title}</h1>
  221. <div className="reader-qq-meta">
  222. <span>书名:{book.title}</span>
  223. <span>作者:{book.author}</span>
  224. <span>当前:{entry.sectionTitle}</span>
  225. <span>本节字数:{entryWords} 字</span>
  226. <span>总字数:{book.wordCount}</span>
  227. </div>
  228. </header>
  229. <article
  230. className={`reader-qq-content${isLoreEntry ? " reader-qq-content--lore" : ""}`}
  231. style={{ color: currentTheme.text }}
  232. >
  233. {entry.content.map((paragraph) => (
  234. <p key={paragraph} style={{ fontSize: currentFont.size, lineHeight: currentFont.lineHeight }}>
  235. {paragraph}
  236. </p>
  237. ))}
  238. </article>
  239. <footer className="reader-qq-footer">
  240. <button className="reader-qq-footer__ghost" type="button" onClick={() => setCatalogOpen(true)}>
  241. 目录
  242. </button>
  243. {prevChapterIndex !== null ? (
  244. <Link className="reader-qq-footer__button" href={`/reader/${book.id}?entry=${prevChapterIndex}`}>
  245. 上一章
  246. </Link>
  247. ) : (
  248. <span className="reader-qq-footer__button reader-qq-footer__button--disabled">已是第一节</span>
  249. )}
  250. {nextChapterIndex !== null ? (
  251. <Link className="reader-qq-footer__button" href={`/reader/${book.id}?entry=${nextChapterIndex}`}>
  252. 下一章
  253. </Link>
  254. ) : (
  255. <span className="reader-qq-footer__button reader-qq-footer__button--disabled">已是最后一节</span>
  256. )}
  257. </footer>
  258. </div>
  259. </section>
  260. <div className="reader-float reader-float--right" aria-label="阅读右侧工具">
  261. <button className="reader-float__button" type="button" onClick={cycleFont}>
  262. <strong>字号</strong>
  263. <span>当前{currentFont.label}</span>
  264. </button>
  265. <button className="reader-float__button" type="button" onClick={cycleTheme}>
  266. <strong>主题</strong>
  267. <span>当前{currentTheme.label}</span>
  268. </button>
  269. <button className="reader-float__button" type="button" onClick={cycleWidth}>
  270. <strong>版心</strong>
  271. <span>当前{currentWidth.label}</span>
  272. </button>
  273. </div>
  274. </div>
  275. </main>
  276. );
  277. }