panes.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import { importSelectedFile } from "#ui/rpc.ts";
  2. import { useEffect, useRef, useState, type RefObject } from "react";
  3. import { DOC_KINDS, MAX_FILE_BYTES, type DocKind, type DocumentInfo } from "#office/contract.ts";
  4. import { type Strings } from "#ui/i18n.ts";
  5. import { Icon } from "#ui/icons.tsx";
  6. import type { PageState, ViewerHandle } from "#ui/viewers/types.ts";
  7. // 面板的文档导航与状态界面(抽屉、文件选择、空态、骨架、错误、文件变更提示),从 parts.tsx 拆出以控制单文件规模。
  8. /* ---------------------------------------------------------------- Drawer */
  9. export type DrawerTab = "pages" | "outline";
  10. function Thumbnail({
  11. handle,
  12. page,
  13. width,
  14. ratio,
  15. current,
  16. onPick,
  17. root,
  18. }: {
  19. handle: ViewerHandle;
  20. page: number;
  21. width: number;
  22. ratio: number;
  23. current: boolean;
  24. onPick(page: number): void;
  25. root: RefObject<HTMLElement | null>;
  26. }) {
  27. const slot = useRef<HTMLDivElement>(null);
  28. useEffect(() => {
  29. const element = slot.current;
  30. if (!element || !handle.thumbnail) return;
  31. let done = false;
  32. const observer = new IntersectionObserver(
  33. (entries) => {
  34. if (done || !entries.some((entry) => entry.isIntersecting)) return;
  35. done = true;
  36. observer.disconnect();
  37. void handle.thumbnail!(page, width).then(
  38. (thumb) => {
  39. element.replaceChildren(thumb);
  40. element.style.height = "";
  41. },
  42. () => undefined,
  43. );
  44. },
  45. { root: root.current, rootMargin: "300px 0px" },
  46. );
  47. observer.observe(element);
  48. return () => observer.disconnect();
  49. }, [handle, page, width, root]);
  50. return (
  51. <button
  52. type="button"
  53. className={`thumb${current ? " on" : ""}`}
  54. aria-current={current ? "page" : undefined}
  55. onClick={() => onPick(page)}
  56. data-page={page}
  57. >
  58. <div ref={slot} className="mini" style={{ width, height: Math.round(width * ratio) }} />
  59. <span>{page}</span>
  60. </button>
  61. );
  62. }
  63. const outlinePage = (at: string) => /^(?:page|slide):(\d+)/.exec(at)?.[1] ?? "";
  64. export function Drawer({
  65. t,
  66. info,
  67. handle,
  68. page,
  69. tab,
  70. docked,
  71. onTab,
  72. onPage,
  73. onOutline,
  74. }: {
  75. t: Strings;
  76. info: DocumentInfo;
  77. handle: ViewerHandle | null;
  78. page: PageState | null;
  79. tab: DrawerTab;
  80. docked: boolean;
  81. onTab(tab: DrawerTab): void;
  82. onPage(page: number): void;
  83. onOutline(at: string): void;
  84. }) {
  85. const scroller = useRef<HTMLDivElement>(null);
  86. const hasPages = Boolean(handle?.thumbnail && page);
  87. const hasOutline = info.outline.length > 0;
  88. const shown: DrawerTab = hasPages && (tab === "pages" || !hasOutline) ? "pages" : "outline";
  89. const ratio = info.kind === "pptx" ? 9 / 16 : 1.294;
  90. // Bring the current page into the drawer's view when it opens or switches tab.
  91. useEffect(() => {
  92. const current = scroller.current?.querySelector<HTMLElement>("[aria-current=page]");
  93. if (current && scroller.current)
  94. scroller.current.scrollTop = current.offsetTop - scroller.current.clientHeight / 3;
  95. }, [shown]);
  96. const currentOutline = (() => {
  97. if (!page) return -1;
  98. let found = -1;
  99. info.outline.forEach((item, i) => {
  100. const n = Number(outlinePage(item.at));
  101. if (n && n <= page.current) found = i;
  102. });
  103. return found;
  104. })();
  105. return (
  106. <nav className={`drawer${docked ? " docked" : ""}`} aria-label={t.pagesAndOutline}>
  107. {hasPages && hasOutline ? (
  108. <div className="seg" role="tablist">
  109. <button
  110. type="button"
  111. role="tab"
  112. aria-selected={shown === "pages"}
  113. className={shown === "pages" ? "on" : ""}
  114. onClick={() => onTab("pages")}
  115. >
  116. {info.kind === "pptx" ? t.slides : t.pages}
  117. </button>
  118. <button
  119. type="button"
  120. role="tab"
  121. aria-selected={shown === "outline"}
  122. className={shown === "outline" ? "on" : ""}
  123. onClick={() => onTab("outline")}
  124. >
  125. {t.outline}
  126. </button>
  127. </div>
  128. ) : null}
  129. <div ref={scroller} className={shown === "pages" ? "thumbs" : "outline"}>
  130. {shown === "pages" && handle && page
  131. ? Array.from({ length: page.count }, (_, i) => (
  132. <Thumbnail
  133. key={i}
  134. handle={handle}
  135. page={i + 1}
  136. width={136}
  137. ratio={ratio}
  138. current={page.current === i + 1}
  139. onPick={onPage}
  140. root={scroller}
  141. />
  142. ))
  143. : info.outline.map((item, i) => (
  144. <button
  145. type="button"
  146. key={`${item.at}-${i}`}
  147. className={`oi level-${Math.min(3, Math.max(1, item.level))}${i === currentOutline ? " on" : ""}`}
  148. aria-current={i === currentOutline ? "page" : undefined}
  149. title={item.label}
  150. onClick={() => onOutline(item.at)}
  151. >
  152. <span className="label">{item.label}</span>
  153. <span className="n">{outlinePage(item.at)}</span>
  154. </button>
  155. ))}
  156. </div>
  157. </nav>
  158. );
  159. }
  160. /* ---------------------------------------------------------------- Empty, picker, skeleton, error */
  161. export function EmptyState({
  162. t,
  163. onBrowse,
  164. importing,
  165. }: {
  166. t: Strings;
  167. onBrowse(): void;
  168. importing: boolean;
  169. }) {
  170. return (
  171. <div className="empty">
  172. <div className="empty-content">
  173. <h3>{t.emptyTitle}</h3>
  174. <p className="empty-description">{t.emptyDescription}</p>
  175. <button type="button" className="btn empty-open" onClick={onBrowse} disabled={importing}>
  176. <Icon name="folder-open" size={16} />
  177. {importing ? t.importing : t.openFile}
  178. </button>
  179. <p className="empty-caption">{t.emptyCaption}</p>
  180. </div>
  181. </div>
  182. );
  183. }
  184. const SKELETON_LINES = [
  185. "title",
  186. "subtitle",
  187. "line",
  188. "line",
  189. "short",
  190. "heading",
  191. "line",
  192. "line",
  193. "line",
  194. "mid",
  195. "figure",
  196. ];
  197. export function Skeleton({ kind }: { kind: DocKind | null }) {
  198. if (kind === "xlsx")
  199. return (
  200. <div className="skeleton sheet" aria-busy="true">
  201. {Array.from({ length: 18 }, (_, i) => (
  202. <div key={i} className="skel-row" />
  203. ))}
  204. </div>
  205. );
  206. const slide = kind === "pptx";
  207. return (
  208. <div className="skeleton" aria-busy="true">
  209. {[0, 1].map((n) => (
  210. <div key={n} className={`skelpage${slide ? " slide" : ""}`}>
  211. {n === 0 && !slide
  212. ? SKELETON_LINES.map((line, i) => <div key={i} className={`skel ${line}`} />)
  213. : null}
  214. {n === 0 && slide ? (
  215. <>
  216. <div className="skel title left" />
  217. <div className="skel line" />
  218. <div className="skel mid" />
  219. </>
  220. ) : null}
  221. </div>
  222. ))}
  223. </div>
  224. );
  225. }
  226. export function ErrorState({
  227. t,
  228. title,
  229. detail,
  230. onOpenAnother,
  231. }: {
  232. t: Strings;
  233. title: string;
  234. detail: string;
  235. onOpenAnother(): void;
  236. }) {
  237. return (
  238. <div className="failed" role="alert">
  239. <Icon name="file-text" size={28} className="subtlest" />
  240. <div>
  241. <h3>{title}</h3>
  242. {detail ? <p>{detail}</p> : null}
  243. </div>
  244. <button type="button" className="btn secondary" onClick={onOpenAnother}>
  245. {t.openAnother}
  246. </button>
  247. </div>
  248. );
  249. }
  250. export function ChangedBanner({
  251. t,
  252. onReload,
  253. onDismiss,
  254. }: {
  255. t: Strings;
  256. onReload(): void;
  257. onDismiss(): void;
  258. }) {
  259. return (
  260. <div className="banner" role="status">
  261. <span>{t.changed}</span>
  262. <button type="button" className="btn sm primary" onClick={onReload}>
  263. {t.reloadShort}
  264. </button>
  265. <button
  266. type="button"
  267. className="btn icon sm dim"
  268. title={t.dismiss}
  269. aria-label={t.dismiss}
  270. onClick={onDismiss}
  271. >
  272. <Icon name="x" />
  273. </button>
  274. </div>
  275. );
  276. }
  277. // 使用系统文件选择器,取消时不改变当前文档;选中的内容通过受限导入接口进入工作区。
  278. export function NativeFileInput({
  279. inputRef,
  280. t,
  281. importing,
  282. onImporting,
  283. onOpen,
  284. onError,
  285. }: {
  286. inputRef: RefObject<HTMLInputElement | null>;
  287. t: Strings;
  288. importing: boolean;
  289. onImporting(value: boolean): void;
  290. onOpen(path: string): void;
  291. onError(error: unknown, name: string): void;
  292. }) {
  293. return (
  294. <input
  295. ref={inputRef}
  296. type="file"
  297. hidden
  298. accept={DOC_KINDS.map((kind) => `.${kind}`).join(",")}
  299. disabled={importing}
  300. onChange={async (event) => {
  301. const file = event.currentTarget.files?.[0];
  302. event.currentTarget.value = "";
  303. if (!file) return;
  304. onImporting(true);
  305. try {
  306. if (file.size > MAX_FILE_BYTES) throw new Error(t.importTooLarge);
  307. onOpen(await importSelectedFile(file));
  308. } catch (error) {
  309. onError(error, file.name);
  310. } finally {
  311. onImporting(false);
  312. }
  313. }}
  314. />
  315. );
  316. }
  317. export function useNativeFileInput(
  318. t: Strings,
  319. onOpen: (path: string) => void,
  320. onError: (error: unknown, name: string) => void,
  321. ) {
  322. const inputRef = useRef<HTMLInputElement>(null);
  323. const [importing, onImporting] = useState(false);
  324. return {
  325. importing,
  326. browse: () => inputRef.current?.click(),
  327. input: <NativeFileInput {...{ inputRef, t, importing, onImporting, onOpen, onError }} />,
  328. };
  329. }