menus.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import type { DocKind } from "#office/contract.ts";
  2. import type { Strings } from "#ui/i18n.ts";
  3. import { FileType, Icon } from "#ui/icons.tsx";
  4. import { baseName, dirName } from "#ui/rpc.ts";
  5. import { Menu, MenuItem } from "#ui/shell/parts.tsx";
  6. import type { FitMode } from "#ui/viewers/types.ts";
  7. // 面板标题栏的两个下拉菜单(切换文档、更多操作),从 main.tsx 拆出以控制单文件规模,行为不变。
  8. export function SwitchMenu({
  9. t,
  10. recent,
  11. currentPath,
  12. onOpen,
  13. onBrowse,
  14. onClose,
  15. }: {
  16. t: Strings;
  17. recent: { path: string; kind: DocKind }[];
  18. currentPath: string | undefined;
  19. onOpen(path: string): void;
  20. onBrowse(): void;
  21. onClose(): void;
  22. }) {
  23. return (
  24. <Menu className="switcher" label={t.switchDocument} onClose={onClose}>
  25. {recent.length ? <div className="mlabel">{t.recent}</div> : null}
  26. {recent.map((item) => (
  27. <MenuItem
  28. key={item.path}
  29. icon={<FileType kind={item.kind} />}
  30. hint={
  31. item.path === currentPath ? (
  32. <Icon name="check" size={16} />
  33. ) : (
  34. baseName(dirName(item.path))
  35. )
  36. }
  37. onSelect={() => (item.path === currentPath ? onClose() : onOpen(item.path))}
  38. >
  39. {baseName(item.path)}
  40. </MenuItem>
  41. ))}
  42. {recent.length ? <div className="msep" /> : null}
  43. <MenuItem icon={<Icon name="folder-open" size={16} />} onSelect={onBrowse}>
  44. {t.openFile}
  45. </MenuItem>
  46. </Menu>
  47. );
  48. }
  49. export function MoreMenu({
  50. t,
  51. zoomable,
  52. zoomMode,
  53. onFit,
  54. onFind,
  55. onReload,
  56. onClose,
  57. }: {
  58. t: Strings;
  59. zoomable: boolean;
  60. zoomMode: FitMode | null | undefined;
  61. onFit(mode: FitMode): void;
  62. onFind(): void;
  63. onReload(): void;
  64. onClose(): void;
  65. }) {
  66. return (
  67. <Menu className="more" label={t.more} onClose={onClose}>
  68. {zoomable ? (
  69. <>
  70. <MenuItem checked={zoomMode === "width"} onSelect={() => onFit("width")}>
  71. {t.fitWidth}
  72. </MenuItem>
  73. <MenuItem checked={zoomMode === "page"} onSelect={() => onFit("page")}>
  74. {t.fitPage}
  75. </MenuItem>
  76. <MenuItem checked={zoomMode === "actual"} hint="100%" onSelect={() => onFit("actual")}>
  77. {t.actualSize}
  78. </MenuItem>
  79. <div className="msep" />
  80. </>
  81. ) : null}
  82. <MenuItem icon={<Icon name="search" size={16} />} hint="⌘F" onSelect={onFind}>
  83. {t.find}
  84. </MenuItem>
  85. <MenuItem icon={<Icon name="rotate-cw" size={16} />} onSelect={onReload}>
  86. {t.reload}
  87. </MenuItem>
  88. </Menu>
  89. );
  90. }