agent-loft 0.2.101 → 0.2.102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -48,8 +48,8 @@ Logo 就是这个意思:一大两小几颗星(多个 agent)站在同一张
48
48
  - 授权卡、提问卡直接在页面上点。
49
49
  - 附件:点击输入框的「+」直接上传,也可以粘贴、拖拽,手机上可以拍照;可以预览和下载。附件存在服务端的专用上传目录里,发出去的都收进资料库,开新会话、切换目录也不删;删除会话时立即清理。
50
50
  - 回复里提到的文件(链接、`路径`、正文里的绝对路径,Claude 和 Codex 都认)点一下就能预览:图片、PDF、音视频、markdown、代码和文本直接看,网页渲染出来看(也能切到源码),其它的下载。Codex 的 visualize 插件画的图表(回复里的 `visualize{"path":…}`)和官方客户端一样直接嵌在回复里。网页和图表放在沙箱里显示,碰不到登录态和本站接口。
51
- - **回复里的图**:专家和 agent 在回复里写 ```` ```chart ````(一段 JSON:折线、面积、柱状、饼图、散点)就画成可悬停的图,可以切到表格看具体数字;写 ```` ```mermaid ```` 画流程图、时序图等。跟着系统深浅色走,图库用到时才加载;飞书上 chart 换成表格。写法自动附在每个专家(包括内置的 Claude Code、Codex)的提示词里。只认这条对话里 agent 回复写过的路径;私钥、`.ssh`、`.env`、各种凭据和 agent-loft 自己的配置数据一律不给看。
52
- - **回复里的相册**:agent 用 ```` ```album ```` JSON 描述多张本地照片,网页显示缩略图网格,每页 12 张、懒加载;点开大图可左右切换,也支持方向键和 Esc。可附名称、拍摄日期、说明和查询总数,每个相册最多 200 张;飞书显示简短摘要。写法自动附在所有专家的提示词里,图片沿用会话文件权限。
51
+ - **回复里的图**:专家和 agent 在回复里写 ```` ```chart ````(一段 JSON:折线、面积、柱状、饼图、散点)就画成可悬停的图,可以切到表格看具体数字;写 ```` ```mermaid ```` 画流程图、时序图等。Mermaid 可点击图放大,chart 可点「放大查看」;图片和图表预览都支持缩放、拖动、适应窗口和显式关闭,手机支持双指缩放。跟着系统深浅色走,图库用到时才加载;飞书上 chart 换成表格。写法自动附在每个专家(包括内置的 Claude Code、Codex)的提示词里。只认这条对话里 agent 回复写过的路径;私钥、`.ssh`、`.env`、各种凭据和 agent-loft 自己的配置数据一律不给看。
52
+ - **回复里的相册**:agent 用 ```` ```album ```` JSON 描述多张本地照片,网页以大图轮播展示,可左右滑动、点击箭头或底部缩略图切换,按需加载照片;点开预览后支持左右切换、缩放、拖动、方向键和 Esc。可附名称、拍摄日期、说明和查询总数,每个相册最多 200 张;飞书显示简短摘要。写法自动附在所有专家的提示词里,图片沿用会话文件权限。
53
53
  - 聊天右上角「更多」和侧栏会话右键菜单共用分叉、重命名、在飞书继续、删除操作,按会话类型和可用机器人显示。
54
54
  - 语音输入:用浏览器自带的语音识别(Chrome / Edge / Safari,需要 HTTPS)。
55
55
  - 新对话页:点大标题里的目录名,在固定高度的目录弹窗里逐层选工作目录,也能新建文件夹;输入框上方选专家,鼠标停在专家上看简介,右下角选模型和思考程度。对话从头到尾一个专家,要换专家就新建对话;模型和思考程度随时可换。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-loft",
3
- "version": "0.2.101",
3
+ "version": "0.2.102",
4
4
  "type": "module",
5
5
  "description": "多个 agent 的工作台:把本机 Claude Code / Codex 放到一处统一管理,用飞书和 Web 页面随时驱动——会话、审批、模型切换都共用",
6
6
  "keywords": [
@@ -83,12 +83,12 @@ export const RICH_REPLY_LINES = [
83
83
  "",
84
84
  "## 回复里的相册",
85
85
  "",
86
- "- 展示多张照片、图片搜索结果时,用 ```album JSON 代码块,Web 会显示缩略图网格、分页和大图预览,飞书显示摘要。例如:",
86
+ "- 展示多张照片、图片搜索结果时,用 ```album JSON 代码块,Web 会显示可左右滑动的大图相册、底部缩略图和可缩放预览,飞书显示摘要。例如:",
87
87
  " ```album",
88
88
  ' {"title":"家庭相片","total":30,"items":[{"src":"/tmp/photos/thumb.webp","preview":"/tmp/photos/preview.jpg","name":"海边合影","date":"2026-10-31","caption":"香港"}]}',
89
89
  " ```",
90
90
  "- items 每项 src 是已下载缩略图的本地绝对路径;preview 是可选的已下载大图路径,省略时预览缩略图。name、date、caption 可选。total 可选,表示查询结果总数,不能小于 items 数量。",
91
- "- 每个相册最多 200 张,Web 每页 12 张。只填实际取得的照片和路径,不编造;不要批量下载原图。需要清晰预览时,只为相关照片下载 preview。",
91
+ "- 每个相册最多 200 张,Web 可左右滑动切换,也可点击缩略图跳转。只填实际取得的照片和路径,不编造;不要批量下载原图。需要清晰预览时,只为相关照片下载 preview。",
92
92
  "- 路径直接写在 JSON 字符串里,不做 URL 编码;不要填远程 URL、API 密钥或 auth_code。图片仍受当前会话的文件权限控制。多张照片优先用相册,单张图片可继续用 Markdown。",
93
93
  ];
94
94
 
@@ -1,40 +1,57 @@
1
- .photo-album {
2
- margin: 14px 0;
3
- padding: 14px;
4
- border: 1px solid var(--border);
5
- border-radius: var(--radius);
6
- background: var(--bg);
7
- min-width: 0;
8
- }
9
- .album-head, .album-pager { display: flex; align-items: center; gap: 12px; }
10
- .album-head { justify-content: space-between; margin-bottom: 12px; flex-wrap: wrap; }
11
- .album-head span, .album-page, .album-date, .album-empty, .album-pending { color: var(--fg-soft); font-size: 13px; }
12
- .album-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
13
- .album-tile { padding: 0; min-width: 0; text-align: left; border-radius: 8px; }
14
- .album-tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
15
- .album-pic { display: grid; aspect-ratio: 1; border-radius: 8px; overflow: hidden; background: var(--bg-soft); }
16
- .md .album-pic img { display: block; width: 100%; height: 100%; max-height: none; margin: 0; border-radius: 0; object-fit: cover; }
17
- .album-missing { align-self: center; text-align: center; color: var(--fg-faint); font-size: 12px; }
18
- .album-name, .album-date { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
19
- .album-name { margin-top: 5px; font-size: 12px; }
20
- .album-date { font-size: 11px; }
21
- .album-pager { justify-content: center; margin-top: 14px; }
22
- .album-pager .btn, .photo-controls .btn { font-size: 13px; }
1
+ .photo-album { margin: 18px 0; min-width: 0; overflow: hidden; border: 1px solid var(--border); border-radius: 18px; background: var(--bg); box-shadow: 0 8px 32px rgb(0 0 0 / 5%); }
2
+ .album-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 20px; }
3
+ .album-head > div { min-width: 0; }
4
+ .album-eyebrow { display: block; margin-bottom: 3px; color: var(--fg-faint); font-size: 9px; font-weight: 600; letter-spacing: 0.18em; }
5
+ .album-head strong { display: block; font-size: 18px; font-weight: 600; letter-spacing: -0.025em; overflow-wrap: anywhere; }
6
+ .album-total { flex-shrink: 0; font-size: 11px; color: var(--fg-soft); }
7
+ .album-hero { position: relative; background: #121315; }
8
+ .album-track { display: flex; width: 100%; overflow-x: auto; scrollbar-width: none; scroll-snap-type: x mandatory; overscroll-behavior-x: contain; }
9
+ .album-track::-webkit-scrollbar, .album-filmstrip::-webkit-scrollbar { display: none; }
10
+ .album-slide { position: relative; display: grid; place-items: center; flex: 0 0 100%; min-width: 0; height: clamp(260px, 52vw, 480px); padding: 0; border: 0; border-radius: 0; background: radial-gradient(ellipse at 50% 40%, #292b30, #121315 85%); scroll-snap-align: start; cursor: zoom-in; }
11
+ .md .album-slide img { display: block; width: 100%; height: 100%; max-width: 100%; max-height: 100%; margin: 0; border-radius: 0; object-fit: contain; user-select: none; -webkit-user-drag: none; }
12
+ .album-slide-hint { position: absolute; right: 14px; top: 14px; display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-radius: 999px; color: #fff; background: rgb(0 0 0 / 38%); font-size: 11px; opacity: 0; transition: opacity 0.15s; }
13
+ .album-slide:hover .album-slide-hint, .album-slide:focus-visible .album-slide-hint { opacity: 1; }
14
+ .album-slide-hint svg, .album-open svg { width: 14px; height: 14px; }
15
+ .album-nav { position: absolute; inset: 0 14px; display: flex; align-items: center; justify-content: space-between; pointer-events: none; }
16
+ .album-arrow { display: grid; place-items: center; width: 36px; height: 36px; padding: 0; border: 1px solid rgb(255 255 255 / 20%); border-radius: 50%; background: rgb(15 15 16 / 52%); color: #fff; backdrop-filter: blur(12px); cursor: pointer; pointer-events: auto; }
17
+ .album-arrow svg { width: 18px; height: 18px; }
18
+ .album-arrow:last-child svg { transform: rotate(180deg); }
19
+ .album-arrow:hover { background: rgb(15 15 16 / 80%); }
20
+ .album-arrow:disabled { opacity: 0.25; cursor: default; }
21
+ .album-count { position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); padding: 4px 12px; border: 1px solid rgb(255 255 255 / 12%); border-radius: 999px; background: rgb(0 0 0 / 42%); color: #fff; font-size: 11px; font-variant-numeric: tabular-nums; pointer-events: none; backdrop-filter: blur(12px); }
22
+ .album-info { display: flex; flex-direction: column; gap: 2px; padding: 16px 20px 12px; }
23
+ .album-date { font-size: 14px; font-weight: 500; font-variant-numeric: tabular-nums; }
24
+ .album-caption { color: var(--fg-soft); font-size: 12px; overflow-wrap: anywhere; }
25
+ .album-caption:empty { display: none; }
26
+ .album-filmstrip { position: relative; display: flex; gap: 8px; padding: 3px 20px 5px; overflow-x: auto; scrollbar-width: none; overscroll-behavior-x: contain; }
27
+ .album-thumb { flex: 0 0 56px; width: 56px; height: 56px; padding: 0; border: 2px solid transparent; border-radius: 9px; overflow: hidden; background: var(--bg-soft); opacity: 0.5; cursor: pointer; transition: opacity 0.15s, border-color 0.15s; }
28
+ .album-thumb:hover { opacity: 0.85; }
29
+ .album-thumb[aria-pressed="true"] { border-color: var(--accent); opacity: 1; }
30
+ .md .album-thumb img { display: block; width: 100%; height: 100%; max-height: none; margin: 0; border-radius: 0; object-fit: cover; }
31
+ .album-track:focus-visible, .album-thumb:focus-visible, .album-arrow:focus-visible, .album-open:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
32
+ .album-footer { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 20px 16px; color: var(--fg-faint); font-size: 11px; }
33
+ .album-open { display: inline-flex; align-items: center; gap: 6px; padding: 0; border: 0; background: none; color: var(--fg-soft); font-size: 12px; cursor: pointer; }
34
+ .album-open:hover { color: var(--fg); }
35
+ .album-empty, .album-pending { padding: 20px; color: var(--fg-soft); font-size: 13px; }
23
36
  .album-error { color: var(--danger); font-size: 13px; }
24
- .photo-viewer { position: fixed; inset: 0; padding: 0; margin: auto; width: min(1100px, calc(100% - 32px)); max-width: none; max-height: calc(100dvh - 32px); border: 1px solid var(--border); border-radius: 14px; background: var(--bg); color: var(--fg); }
25
- .photo-viewer::backdrop { background: rgb(0 0 0 / 75%); }
26
- .photo-panel { display: flex; flex-direction: column; max-height: calc(100dvh - 34px); }
27
- .photo-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 14px; }
28
- .photo-stage { display: flex; align-items: center; justify-content: center; min-height: 0; height: 65dvh; background: #111; }
29
- .md .photo-stage img { display: block; width: 100%; height: 100%; object-fit: contain; margin: 0; border-radius: 0; max-height: none; }
30
- .photo-info { display: grid; gap: 2px; padding: 10px 16px 0; min-width: 0; overflow: auto; }
31
- .photo-name, .photo-detail { overflow-wrap: anywhere; }
32
- .photo-detail, .photo-status { color: var(--fg-soft); font-size: 13px; }
33
- .photo-controls { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 16px; }
37
+ .album-missing { padding: 20px; color: #b4b4b4; font-size: 12px; }
38
+ .photo-viewer .photo-detail { padding: 8px 16px; color: var(--fg-soft); font-size: 12px; text-align: center; overflow-wrap: anywhere; }
39
+ .photo-viewer .photo-detail:empty { display: none; }
40
+ .photo-count { flex-shrink: 0; color: var(--fg-soft); font-size: 12px; font-variant-numeric: tabular-nums; }
41
+ .photo-navigation { display: flex; align-items: center; justify-content: center; gap: 16px; padding: 6px 12px 0; }
42
+ .photo-navigation .btn { font-size: 12px; }
43
+ .photo-viewer .diagram-viewer-hint { font-size: 11px; }
34
44
  @media (max-width: 600px) {
35
- .photo-album { padding: 10px; }
36
- .album-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
37
- .photo-viewer { width: calc(100% - 16px); max-height: calc(100dvh - 16px); }
38
- .photo-panel { max-height: calc(100dvh - 18px); }
39
- .photo-stage { height: 55dvh; }
45
+ .album-head { padding: 14px; flex-wrap: wrap; gap: 6px; }
46
+ .album-head strong { font-size: 16px; }
47
+ .album-total { font-size: 10px; }
48
+ .album-slide { height: clamp(280px, 95vw, 420px); }
49
+ .album-nav { inset-inline: 8px; }
50
+ .album-arrow { width: 30px; height: 30px; }
51
+ .album-info { padding: 12px 14px 10px; }
52
+ .album-filmstrip { padding-inline: 14px; gap: 6px; }
53
+ .album-thumb { flex-basis: 48px; width: 48px; height: 48px; }
54
+ .album-footer { padding: 10px 14px 14px; }
55
+ .album-slide-hint { display: none; }
40
56
  }
57
+ @media (prefers-reduced-motion: reduce) { .album-thumb, .album-slide-hint { transition: none; } }
@@ -881,53 +881,6 @@ a.att-file:hover {
881
881
  }
882
882
 
883
883
 
884
- .lightbox {
885
- position: fixed;
886
- inset: 0;
887
- z-index: 60;
888
- background: rgba(0, 0, 0, 0.82);
889
- display: flex;
890
- flex-direction: column;
891
- align-items: center;
892
- justify-content: center;
893
- gap: 10px;
894
- padding: 24px;
895
- cursor: zoom-out;
896
- animation: fade-in 0.12s ease-out;
897
- }
898
- .lightbox img {
899
- max-width: 100%;
900
- max-height: calc(100% - 32px);
901
- object-fit: contain;
902
- border-radius: 6px;
903
- box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
904
- }
905
-
906
- .lightbox-bar {
907
- display: flex;
908
- align-items: center;
909
- gap: 14px;
910
- cursor: default;
911
- }
912
-
913
- .lightbox-name {
914
- color: rgba(255, 255, 255, 0.75);
915
- font-size: 13px;
916
- }
917
-
918
- .lightbox-dl {
919
- color: #fff;
920
- font-size: 13px;
921
- text-decoration: none;
922
- padding: 4px 14px;
923
- border-radius: 999px;
924
- background: rgba(255, 255, 255, 0.16);
925
- }
926
-
927
- .lightbox-dl:hover {
928
- background: rgba(255, 255, 255, 0.26);
929
- }
930
-
931
884
  /* ---------- 右边的快速定位:每条用户消息一道横线,鼠标移上去展开成列表 ---------- */
932
885
  .msg-nav {
933
886
  position: absolute;
@@ -1532,6 +1485,7 @@ a.att-file:hover {
1532
1485
  .diagram.mermaid .diagram-body {
1533
1486
  overflow-x: auto;
1534
1487
  text-align: center;
1488
+ cursor: zoom-in;
1535
1489
  }
1536
1490
 
1537
1491
  .diagram-svg {
@@ -1589,3 +1543,39 @@ a.att-file:hover {
1589
1543
  background: var(--bg-hover);
1590
1544
  color: var(--fg);
1591
1545
  }
1546
+
1547
+ .md .diagram-btn:disabled { opacity: 0.45; cursor: default; }
1548
+
1549
+ .diagram-viewer {
1550
+ position: fixed;
1551
+ inset: 0;
1552
+ margin: auto;
1553
+ padding: 0;
1554
+ width: calc(100% - 32px);
1555
+ height: calc(100dvh - 32px);
1556
+ max-width: none;
1557
+ max-height: none;
1558
+ border: 1px solid var(--border);
1559
+ border-radius: 12px;
1560
+ background: var(--bg);
1561
+ color: var(--fg);
1562
+ overflow: hidden;
1563
+ }
1564
+ .diagram-viewer[open] { display: flex; flex-direction: column; }
1565
+ .diagram-viewer::backdrop { background: rgb(0 0 0 / 75%); }
1566
+ .diagram-viewer-head, .diagram-viewer-controls { display: flex; align-items: center; gap: 8px; padding: 8px 12px; }
1567
+ .diagram-viewer-head { justify-content: space-between; }
1568
+ .diagram-viewer-head strong { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
1569
+ .diagram-viewer-head .btn { flex-shrink: 0; }
1570
+ .diagram-stage { position: relative; flex: 1; min-height: 0; overflow: hidden; touch-action: none; cursor: grab; user-select: none; }
1571
+ .diagram-stage.dragging { cursor: grabbing; }
1572
+ .diagram-stage img, .md .diagram-stage img { position: absolute; left: 0; top: 0; display: block; max-width: none; max-height: none; margin: 0; border-radius: 0; transform-origin: 0 0; pointer-events: none; }
1573
+ .diagram-viewer-status { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; color: var(--fg-soft); }
1574
+ .diagram-viewer-controls { justify-content: center; flex-wrap: wrap; }
1575
+ .diagram-viewer-controls .btn { font-size: 13px; }
1576
+ .diagram-scale { min-width: 48px; text-align: center; font-size: 13px; font-variant-numeric: tabular-nums; }
1577
+ .diagram-viewer-hint { padding: 0 8px 10px; text-align: center; font-size: 12px; color: var(--fg-soft); }
1578
+ @media (max-width: 600px) {
1579
+ .diagram-viewer { width: calc(100% - 16px); height: calc(100dvh - 16px); }
1580
+ .diagram-viewer-controls { gap: 4px; padding: 8px; }
1581
+ }
package/web/lib/albums.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { h, icon } from "./dom.js";
2
+ import { openImageViewer } from "./image-viewer.js";
2
3
 
3
4
  // 相册只接收本地文件路径,沿用会话文件接口的授权、敏感路径检查;不接收远程地址或 HTML。
4
- const PAGE_SIZE = 12;
5
5
  const MAX_ITEMS = 200;
6
6
  const fileUrl = (key, path) => `/api/conversations/${encodeURIComponent(key)}/file?path=${encodeURIComponent(path)}`;
7
7
  const localPath = (p) => typeof p === "string" && p.startsWith("/") && !p.startsWith("//") && !/[\0\r\n]/.test(p);
@@ -14,7 +14,7 @@ function parseAlbum(source) {
14
14
  const items = [];
15
15
  for (const item of spec.items) {
16
16
  if (!item || !localPath(item.src) || (item.preview !== undefined && !localPath(item.preview))) return "相册图片需要使用已下载的本地绝对路径";
17
- items.push({ src: item.src, preview: item.preview || item.src, name: text(item.name) || item.src.split("/").pop(), date: text(item.date), caption: text(item.caption) });
17
+ items.push({ src: item.src, preview: item.preview || item.src, name: text(item.name) || `照片 ${items.length + 1}`, date: text(item.date), caption: text(item.caption) });
18
18
  }
19
19
  return { title: text(spec.title) || "相册", items, total: Number.isSafeInteger(spec.total) && spec.total >= items.length ? spec.total : items.length };
20
20
  }
@@ -38,91 +38,103 @@ export function mountAlbums(root, key, final) {
38
38
 
39
39
  function renderAlbum(spec, key) {
40
40
  const { items, total } = spec;
41
- const box = h("section", { class: "photo-album", "aria-label": spec.title });
42
- const grid = h("div", { class: "album-grid" });
43
- const status = h("span", { class: "album-page", "aria-live": "polite" });
44
- let page = 0;
45
- const pages = Math.ceil(items.length / PAGE_SIZE);
46
- const prev = h("button", { class: "btn ghost", "aria-label": "相册上一页", onclick: () => showPage(page - 1) }, "上一页");
47
- const next = h("button", { class: "btn ghost", "aria-label": "相册下一页", onclick: () => showPage(page + 1) }, "下一页");
48
- const showPage = (n) => {
49
- page = Math.max(0, Math.min(n, pages - 1));
50
- grid.replaceChildren(...items.slice(page * PAGE_SIZE, (page + 1) * PAGE_SIZE).map((item, offset) => {
51
- const index = page * PAGE_SIZE + offset;
52
- const img = h("img", { src: fileUrl(key, item.src), alt: item.name, loading: "lazy", decoding: "async", width: "240", height: "240" });
53
- const pic = h("span", { class: "album-pic" }, img);
54
- img.onerror = () => pic.replaceChildren(h("span", { class: "album-missing" }, "图片无法加载"));
55
- return h("button", {
56
- class: "album-tile", "aria-label": `查看第 ${index + 1} 张:${item.name}`, title: [item.name, item.date, item.caption].filter(Boolean).join("\n"),
57
- onclick: (e) => openPhoto(box, spec, key, index, e.currentTarget),
58
- }, pic, h("span", { class: "album-name" }, item.name), item.date ? h("span", { class: "album-date" }, item.date) : null);
59
- }));
60
- status.textContent = `第 ${page + 1} / ${pages} 页`;
61
- prev.disabled = page === 0;
62
- next.disabled = page >= pages - 1;
63
- // 切页后按钮仍然留在原位置,键盘焦点也留在原按钮上。
64
- };
65
- box.append(h("div", { class: "album-head" }, h("strong", {}, spec.title), h("span", {}, total > items.length ? `已载入 ${items.length} / 共 ${total} 张` : `共 ${items.length} 张`)));
66
- if (!items.length) box.append(h("div", { class: "album-empty" }, "没有照片"));
67
- else {
68
- box.append(grid);
69
- if (pages > 1) box.append(h("div", { class: "album-pager" }, prev, status, next));
70
- showPage(0);
41
+ const box = h("section", { class: "photo-album", "aria-label": spec.title, "aria-roledescription": "轮播相册" });
42
+ box.append(h("div", { class: "album-head" },
43
+ h("div", {}, h("span", { class: "album-eyebrow" }, "PHOTO COLLECTION"), h("strong", {}, spec.title)),
44
+ h("span", { class: "album-total" }, total > items.length ? `已载入 ${items.length} / 共 ${total} 张` : `共 ${items.length} 张`)));
45
+ if (!items.length) {
46
+ box.append(h("div", { class: "album-empty" }, "没有照片"));
47
+ return box;
71
48
  }
72
- return box;
73
- }
74
-
75
- function openPhoto(album, spec, key, start, trigger) {
76
- let index = start;
77
- const dialog = h("dialog", { class: "photo-viewer", "aria-label": "照片预览" });
78
- const image = h("img", { alt: "" });
79
- const stage = h("div", { class: "photo-stage" }, image);
80
- const name = h("strong", { class: "photo-name" });
81
- const detail = h("span", { class: "photo-detail" });
82
- const status = h("span", { class: "photo-status", role: "status" });
83
- const count = h("span", { class: "photo-count", "aria-live": "polite" });
84
- const open = h("a", { target: "_blank", rel: "noopener noreferrer" }, "打开图片");
85
- const close = () => dialog.close();
86
- const prev = h("button", { class: "btn ghost", "aria-label": "上一张照片", onclick: () => show(index - 1) }, "上一张");
87
- const next = h("button", { class: "btn ghost", "aria-label": "下一张照片", onclick: () => show(index + 1) }, "下一张");
88
- const show = (n) => {
89
- index = Math.max(0, Math.min(n, spec.items.length - 1));
90
- const item = spec.items[index];
91
- const thumb = fileUrl(key, item.src);
92
- const preview = fileUrl(key, item.preview);
93
- status.textContent = "";
94
- image.onerror = () => {
95
- if (image.getAttribute("src") !== thumb) {
96
- status.textContent = "预览不可用,显示缩略图";
97
- image.src = thumb;
98
- } else status.textContent = "图片无法加载,文件可能已被清理";
99
- };
100
- image.src = preview;
101
- image.alt = item.name;
102
- name.textContent = item.name;
103
- detail.textContent = [item.date, item.caption].filter(Boolean).join(" · ");
104
- count.textContent = `${index + 1} / ${spec.items.length}`;
105
- open.href = preview;
49
+ let index = 0;
50
+ let trackWidth = 0, target = null;
51
+ const reduced = matchMedia("(prefers-reduced-motion: reduce)");
52
+ const track = h("div", { class: "album-track", tabindex: "0", "aria-label": "左右滑动切换照片" });
53
+ const strip = h("div", { class: "album-filmstrip", "aria-label": "选择照片" });
54
+ const count = h("span", { class: "album-count", "aria-live": "polite", "aria-atomic": "true" });
55
+ const date = h("span", { class: "album-date" });
56
+ const caption = h("span", { class: "album-caption" });
57
+ const gallery = items.map((item) => ({ src: fileUrl(key, item.preview), fallback: fileUrl(key, item.src), title: item.name, detail: [item.date, item.caption].filter(Boolean).join(" · ") }));
58
+ const open = (trigger) => openImageViewer(box, gallery[index].src, items[index].name, trigger, {
59
+ kind: "照片", gallery, start: index, onIndexChange: (n) => select(n, false),
60
+ });
61
+ const slides = items.map((item, i) => h("button", {
62
+ class: "album-slide", tabindex: i === 0 ? "0" : "-1", "aria-label": `放大第 ${i + 1} 张:${item.name}`,
63
+ onclick: (e) => { if (i !== index) select(i, false); open(e.currentTarget); },
64
+ }, h("span", { class: "album-slide-hint" }, icon("expand"), "放大查看")));
65
+ const thumbs = items.map((item, i) => h("button", {
66
+ class: "album-thumb", "aria-label": `查看第 ${i + 1} 张:${item.name}`, "aria-pressed": "false",
67
+ onclick: () => select(i), title: [item.date, item.caption || item.name].filter(Boolean).join(" · "),
68
+ }, h("img", { src: fileUrl(key, item.src), alt: "", loading: "lazy", decoding: "async", width: "64", height: "64" })));
69
+ const prev = h("button", { class: "album-arrow", "aria-label": "上一张照片", onclick: () => select(index - 1) }, icon("back"));
70
+ const next = h("button", { class: "album-arrow", "aria-label": "下一张照片", onclick: () => select(index + 1) }, icon("back"));
71
+ function select(n, smooth = true, scroll = true) {
72
+ index = Math.max(0, Math.min(n, items.length - 1));
73
+ count.textContent = `${index + 1} / ${items.length}`;
74
+ date.textContent = items[index].date || items[index].name;
75
+ caption.textContent = items[index].caption;
106
76
  prev.disabled = index === 0;
107
- next.disabled = index === spec.items.length - 1;
108
- };
109
- dialog.append(h("div", { class: "photo-panel" },
110
- h("div", { class: "photo-head" }, count, h("button", { class: "icon-btn", "aria-label": "关闭照片预览", onclick: close }, icon("x"))),
111
- stage,
112
- h("div", { class: "photo-info" }, name, detail, status),
113
- h("div", { class: "photo-controls" }, prev, open, next),
114
- ));
115
- dialog.addEventListener("click", (e) => { if (e.target === dialog) close(); });
116
- dialog.addEventListener("keydown", (e) => {
117
- if (["ArrowLeft", "ArrowRight", "Escape"].includes(e.key)) e.stopPropagation();
118
- if (e.key === "ArrowLeft" || e.key === "ArrowRight") {
119
- e.preventDefault();
120
- show(index + (e.key === "ArrowRight" ? 1 : -1));
77
+ next.disabled = index === items.length - 1;
78
+ slides.forEach((slide, i) => {
79
+ slide.tabIndex = i === index ? 0 : -1;
80
+ thumbs[i].setAttribute("aria-pressed", String(i === index));
81
+ if (Math.abs(i - index) > 1 || slide.querySelector("img, .album-missing")) return;
82
+ const img = h("img", { src: gallery[i].src, alt: items[i].name, decoding: "async", draggable: "false" });
83
+ img.onerror = () => {
84
+ if (img.getAttribute("src") !== gallery[i].fallback) img.src = gallery[i].fallback;
85
+ else img.replaceWith(h("span", { class: "album-missing" }, "图片无法加载,仍可切换其他照片"));
86
+ };
87
+ slide.prepend(img);
88
+ });
89
+ const behavior = smooth && !reduced.matches ? "smooth" : "instant";
90
+ if (scroll) {
91
+ target = smooth && !reduced.matches ? index : null;
92
+ track.scrollTo({ left: index * track.clientWidth, behavior });
121
93
  }
94
+ const thumb = thumbs[index];
95
+ // 只滚动缩略图条,不让聊天页面跟着 scrollIntoView 跳动。
96
+ strip.scrollTo({ left: thumb.offsetLeft - (strip.clientWidth - thumb.offsetWidth) / 2, behavior });
97
+ }
98
+ // 原生横向滚动保留手机滑动、触控板惯性和浏览器的手势处理。
99
+ let frame = 0;
100
+ track.addEventListener("scroll", () => {
101
+ if (!frame) frame = requestAnimationFrame(() => {
102
+ frame = 0;
103
+ if (!track.clientWidth || track.clientWidth !== trackWidth) return;
104
+ if (target !== null && Math.abs(track.scrollLeft - target * trackWidth) > 1) return;
105
+ target = null;
106
+ const n = Math.round(track.scrollLeft / track.clientWidth);
107
+ if (n !== index) select(n, false, false);
108
+ });
109
+ });
110
+ for (const event of ["pointerdown", "wheel"]) track.addEventListener(event, () => { target = null; }, { passive: true });
111
+ track.addEventListener("scrollend", () => {
112
+ target = null;
113
+ if (trackWidth && trackWidth === track.clientWidth) select(Math.round(track.scrollLeft / trackWidth), false, false);
122
114
  });
123
- dialog.addEventListener("close", () => { dialog.remove(); if (trigger.isConnected) trigger.focus({ preventScroll: true }); });
124
- // dialog 随相册一起移除,切换会话、重画流式块时不会留下全局监听器或悬空弹层。
125
- album.append(dialog);
126
- show(index);
127
- dialog.showModal();
115
+ track.addEventListener("keydown", (e) => {
116
+ if (!["ArrowLeft", "ArrowRight", "Home", "End"].includes(e.key)) return;
117
+ e.preventDefault();
118
+ e.stopPropagation();
119
+ select(e.key === "Home" ? 0 : e.key === "End" ? items.length - 1 : index + (e.key === "ArrowRight" ? 1 : -1));
120
+ });
121
+ track.append(...slides);
122
+ strip.append(...thumbs);
123
+ box.append(h("div", { class: "album-hero" }, track, h("div", { class: "album-nav" }, prev, next), count),
124
+ h("div", { class: "album-info" }, date, caption), strip,
125
+ h("div", { class: "album-footer" }, "左右滑动,重温每个瞬间", h("button", { class: "album-open", onclick: (e) => open(e.currentTarget) }, "放大查看", icon("expand"))));
126
+ select(0, false);
127
+ const resize = new ResizeObserver(() => {
128
+ if (!box.isConnected) {
129
+ resize.disconnect();
130
+ cancelAnimationFrame(frame);
131
+ return;
132
+ }
133
+ if (track.clientWidth && track.clientWidth !== trackWidth) {
134
+ trackWidth = track.clientWidth;
135
+ select(index, false);
136
+ }
137
+ });
138
+ resize.observe(track);
139
+ return box;
128
140
  }
@@ -1,4 +1,5 @@
1
1
  import { h } from "./dom.js";
2
+ import { openImageViewer } from "./image-viewer.js";
2
3
 
3
4
  // 回复里的 ```chart(专家用一小段 JSON 描述的曲线图、柱状图、饼图)和 ```mermaid(流程图、时序图…)。
4
5
  //
@@ -45,6 +46,22 @@ export function mountDiagram(pre, code, lang, final) {
45
46
  box._lang = lang;
46
47
  box._source = source;
47
48
  box._body = body;
49
+ const expand = h("button", { class: "diagram-btn", disabled: true, onclick: () => {
50
+ let src;
51
+ if (lang === "mermaid") src = body.querySelector("img")?.src;
52
+ else {
53
+ // 导出先结束入场动画,再序列化真实 DOM。ECharts 的字符串导出不会转义字体名里的引号。
54
+ box._chart.getDataURL({ type: "svg" });
55
+ box._chart.getZr().flush();
56
+ src = svgImage(new XMLSerializer().serializeToString(body.querySelector("svg"))).src;
57
+ }
58
+ if (src) openImageViewer(box, src, box._spec?.title || (lang === "mermaid" ? "Mermaid 图" : "图表"), expand);
59
+ } }, "放大查看");
60
+ box._expand = expand;
61
+ if (lang === "mermaid") {
62
+ body.title = "点击放大查看";
63
+ body.addEventListener("click", () => { if (!expand.disabled) expand.click(); });
64
+ }
48
65
  pre.replaceWith(box);
49
66
  // 原来的代码块(带复制按钮)留着,点「源码」看
50
67
  pre.classList.add("diagram-src");
@@ -67,6 +84,7 @@ export function mountDiagram(pre, code, lang, final) {
67
84
  h(
68
85
  "div",
69
86
  { class: "diagram-actions" },
87
+ expand,
70
88
  views.map(([label, el]) => {
71
89
  const btn = h("button", { class: "diagram-btn" }, label);
72
90
  btn.onclick = () => {
@@ -102,6 +120,8 @@ export function unmountDiagrams(root) {
102
120
  if (root.nodeType !== Node.ELEMENT_NODE) return;
103
121
  for (const box of [...(root.matches(".diagram") ? [root] : []), ...root.querySelectorAll(".diagram")]) {
104
122
  lazyRender.unobserve(box);
123
+ box._closePreview?.();
124
+ box._resize?.disconnect();
105
125
  box._chart?.dispose();
106
126
  }
107
127
  }
@@ -111,7 +131,9 @@ async function draw(box) {
111
131
  try {
112
132
  if (box._lang === "chart") await drawChart(box);
113
133
  else await drawMermaid(box);
134
+ box._expand.disabled = false;
114
135
  } catch (err) {
136
+ box._expand.disabled = true;
115
137
  box._body.replaceChildren(h("div", { class: "diagram-error" }, `画不出来:${err?.message ?? err}`));
116
138
  const src = box.querySelector(".diagram-src");
117
139
  if (src) src.hidden = false;
@@ -0,0 +1,162 @@
1
+ import { h } from "./dom.js";
2
+
3
+ /** 图片与图表共用预览:SVG 保持图片隔离,普通图片沿用同一套缩放与拖动。 */
4
+ export function openImageViewer(owner, src, title, trigger, { kind = "图表", download, gallery, start = 0, onIndexChange } = {}) {
5
+ owner._closePreview?.();
6
+ const dialog = h("dialog", { class: `diagram-viewer${gallery ? " photo-viewer" : ""}`, "aria-label": `${kind}预览` });
7
+ const image = h("img", { alt: title, draggable: "false" });
8
+ const status = h("div", { class: "diagram-viewer-status photo-status", role: "status" });
9
+ const stage = h("div", { class: `diagram-stage${gallery ? " photo-stage" : ""}`, "aria-label": "拖动查看,滚轮或双指缩放" }, image, status);
10
+ const heading = h("strong", { class: "photo-name" }, title);
11
+ const detail = h("div", { class: "photo-detail" });
12
+ const count = h("span", { class: "photo-count", "aria-live": "polite" });
13
+ const previous = h("button", { class: "btn ghost", "aria-label": "上一张照片", onclick: () => show(index - 1) }, "上一张");
14
+ const next = h("button", { class: "btn ghost", "aria-label": "下一张照片", onclick: () => show(index + 1) }, "下一张");
15
+ let index = start, fallback = "", notice = "", swipe;
16
+ function show(n) {
17
+ n = Math.max(0, Math.min(n, gallery.length - 1));
18
+ if (n === index && width) return;
19
+ index = n;
20
+ const item = gallery[index];
21
+ width = height = 0;
22
+ image.style.visibility = "hidden";
23
+ notice = status.textContent = "";
24
+ fallback = item.fallback;
25
+ image.alt = heading.textContent = item.title;
26
+ detail.textContent = item.detail;
27
+ count.textContent = `${index + 1} / ${gallery.length}`;
28
+ previous.disabled = index === 0;
29
+ next.disabled = index === gallery.length - 1;
30
+ image.src = item.src;
31
+ onIndexChange?.(index);
32
+ }
33
+ const percent = h("output", { class: "diagram-scale", "aria-label": "缩放比例" });
34
+ let width = 0, height = 0, scale = 1, x = 0, y = 0, closed = false;
35
+ const pointers = new Map();
36
+ const paint = () => {
37
+ image.style.transform = `translate(${x}px, ${y}px) scale(${scale})`;
38
+ percent.textContent = `${Math.round(scale * 100)}%`;
39
+ };
40
+ const fitScale = () => Math.min(1, stage.clientWidth / width, stage.clientHeight / height);
41
+ const center = (s) => {
42
+ if (!width) return;
43
+ scale = s;
44
+ x = (stage.clientWidth - width * scale) / 2;
45
+ y = (stage.clientHeight - height * scale) / 2;
46
+ paint();
47
+ };
48
+ const fit = () => center(fitScale());
49
+ const zoom = (next, cx = stage.clientWidth / 2, cy = stage.clientHeight / 2) => {
50
+ if (!width) return;
51
+ next = Math.max(Math.min(fitScale(), 0.1), Math.min(16, next));
52
+ const ratio = next / scale;
53
+ x = cx - (cx - x) * ratio;
54
+ y = cy - (cy - y) * ratio;
55
+ scale = next;
56
+ paint();
57
+ };
58
+ const control = (label, text, onclick) => h("button", { class: "btn ghost", "aria-label": label, title: label, onclick }, text);
59
+ const close = () => {
60
+ if (closed) return;
61
+ closed = true;
62
+ resize.disconnect();
63
+ if (dialog.open) dialog.close();
64
+ dialog.remove();
65
+ if (owner._closePreview === close) delete owner._closePreview;
66
+ if (trigger?.isConnected) trigger.focus({ preventScroll: true });
67
+ };
68
+ dialog.append(
69
+ h("div", { class: "diagram-viewer-head" }, heading, control(`关闭${kind}预览`, "关闭", close)),
70
+ stage,
71
+ gallery ? detail : null,
72
+ gallery ? h("div", { class: "photo-navigation" }, previous, count, next) : null,
73
+ h("div", { class: "diagram-viewer-controls" },
74
+ control(`缩小${kind}`, "−", () => zoom(scale / 1.25)), percent,
75
+ control(`放大${kind}`, "+", () => zoom(scale * 1.25)),
76
+ control("适应窗口", "适应窗口", fit), control("原始大小", "原始大小", () => center(1)),
77
+ download ? h("a", { class: "btn ghost", href: download, download: title }, "下载") : null),
78
+ h("div", { class: "diagram-viewer-hint" }, gallery ? "左右滑动切换 · 双指 / 滚轮缩放 · 放大后拖动 · Esc 关闭" : "滚轮 / 双指缩放 · 拖动查看 · Esc 关闭"),
79
+ );
80
+ stage.addEventListener("wheel", (e) => {
81
+ e.preventDefault();
82
+ const rect = stage.getBoundingClientRect();
83
+ const delta = e.deltaY * (e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? stage.clientHeight : 1);
84
+ zoom(scale * Math.exp(-Math.max(-300, Math.min(300, delta)) * 0.002), e.clientX - rect.left, e.clientY - rect.top);
85
+ }, { passive: false });
86
+ stage.addEventListener("pointerdown", (e) => {
87
+ if (e.button !== 0 || pointers.size >= 2) return;
88
+ pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
89
+ swipe = gallery && pointers.size === 1 && width && scale <= fitScale() * 1.01 ? { x: e.clientX, y: e.clientY } : null;
90
+ stage.setPointerCapture(e.pointerId);
91
+ stage.classList.add("dragging");
92
+ });
93
+ stage.addEventListener("pointermove", (e) => {
94
+ const old = pointers.get(e.pointerId);
95
+ if (!old) return;
96
+ const other = [...pointers.entries()].find(([id]) => id !== e.pointerId)?.[1];
97
+ if (swipe && !other) return;
98
+ if (other) {
99
+ const before = Math.hypot(old.x - other.x, old.y - other.y);
100
+ const after = Math.hypot(e.clientX - other.x, e.clientY - other.y);
101
+ const rect = stage.getBoundingClientRect();
102
+ if (before > 0) zoom(scale * after / before, (old.x + other.x) / 2 - rect.left, (old.y + other.y) / 2 - rect.top);
103
+ x += (e.clientX - old.x) / 2;
104
+ y += (e.clientY - old.y) / 2;
105
+ } else {
106
+ x += e.clientX - old.x;
107
+ y += e.clientY - old.y;
108
+ }
109
+ pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
110
+ paint();
111
+ });
112
+ const release = (e) => {
113
+ if (swipe && e.type === "pointerup") {
114
+ const dx = e.clientX - swipe.x, dy = e.clientY - swipe.y;
115
+ if (Math.abs(dx) > 50 && Math.abs(dx) > Math.abs(dy) * 1.4) show(index + (dx < 0 ? 1 : -1));
116
+ }
117
+ swipe = null;
118
+ pointers.delete(e.pointerId);
119
+ if (!pointers.size) stage.classList.remove("dragging");
120
+ };
121
+ for (const event of ["pointerup", "pointercancel", "lostpointercapture"]) stage.addEventListener(event, release);
122
+ dialog.addEventListener("keydown", (e) => {
123
+ e.stopPropagation();
124
+ if (gallery && ["ArrowLeft", "ArrowRight"].includes(e.key)) {
125
+ e.preventDefault();
126
+ show(index + (e.key === "ArrowRight" ? 1 : -1));
127
+ }
128
+ if (["+", "=", "-", "0"].includes(e.key)) {
129
+ e.preventDefault();
130
+ if (e.key === "0") fit();
131
+ else zoom(scale * (e.key === "-" ? 0.8 : 1.25));
132
+ }
133
+ });
134
+ dialog.addEventListener("click", (e) => { if (e.target === dialog) close(); });
135
+ dialog.addEventListener("close", close);
136
+ const resize = new ResizeObserver(() => {
137
+ if (!dialog.isConnected) close();
138
+ else fit();
139
+ });
140
+ image.addEventListener("load", () => {
141
+ status.textContent = notice;
142
+ image.style.visibility = "";
143
+ width = image.naturalWidth;
144
+ height = image.naturalHeight;
145
+ image.style.width = `${width}px`;
146
+ image.style.height = `${height}px`;
147
+ fit();
148
+ });
149
+ image.addEventListener("error", () => {
150
+ if (fallback && image.getAttribute("src") !== fallback) {
151
+ notice = status.textContent = "预览不可用,显示缩略图";
152
+ image.src = fallback;
153
+ } else status.textContent = `${kind}无法加载`;
154
+ });
155
+ // 随消息销毁;关闭时释放观察器并恢复键盘焦点。
156
+ owner._closePreview = close;
157
+ owner.append(dialog);
158
+ dialog.showModal();
159
+ if (gallery) show(start);
160
+ else image.src = src;
161
+ resize.observe(stage);
162
+ }
@@ -15,6 +15,7 @@ import { linkReplyFiles } from "./file-preview.js";
15
15
  import { setTodos } from "./todos.js";
16
16
  import { expertIcon, findExpert, setDraftExpert } from "./experts.js";
17
17
  import { preloadCommands } from "./slash.js";
18
+ import { openImageViewer } from "../lib/image-viewer.js";
18
19
 
19
20
  // ---------- 打开会话 ----------
20
21
 
@@ -496,31 +497,9 @@ function renderUserFiles(files) {
496
497
  );
497
498
  }
498
499
 
499
- /** 点缩略图看大图:点任意处或按 Esc 关闭 */
500
+ /** 普通图片预览:显式关闭按钮,支持缩放和拖动,保留下载入口。 */
500
501
  export function openLightbox(src, name, download) {
501
- const close = () => {
502
- overlay.remove();
503
- document.removeEventListener("keydown", onKey, true);
504
- };
505
- const onKey = (e) => {
506
- if (e.key === "Escape") {
507
- e.preventDefault();
508
- close();
509
- }
510
- };
511
- const overlay = h(
512
- "div",
513
- { class: "lightbox", onclick: close },
514
- h("img", { src, alt: name ?? "" }),
515
- h(
516
- "div",
517
- { class: "lightbox-bar", onclick: (e) => e.stopPropagation() },
518
- name ? h("span", { class: "lightbox-name" }, name) : null,
519
- download ? h("a", { class: "lightbox-dl", href: download, download: name ?? "" }, "下载") : null,
520
- ),
521
- );
522
- document.body.append(overlay);
523
- document.addEventListener("keydown", onKey, true);
502
+ openImageViewer(document.body, src, name || "图片", document.activeElement, { kind: "图片", download });
524
503
  }
525
504
 
526
505
  /** 用户消息超过这个高度(约 10 行)就先收起来,像 ChatGPT;只多出一两行的不收 */