agent-loft 0.2.101 → 0.2.103
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 +2 -2
- package/package.json +1 -1
- package/src/core/expert-prompt.ts +2 -2
- package/web/css/albums.css +48 -37
- package/web/css/messages.css +163 -47
- package/web/lib/albums.js +97 -85
- package/web/lib/diagrams.js +22 -0
- package/web/lib/image-viewer.js +175 -0
- package/web/views/messages.js +3 -24
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 ````
|
|
52
|
-
- **回复里的相册**:agent 用 ```` ```album ```` JSON
|
|
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
|
@@ -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
|
|
91
|
+
"- 每个相册最多 200 张,Web 可左右滑动切换,也可点击缩略图跳转。只填实际取得的照片和路径,不编造;不要批量下载原图。需要清晰预览时,只为相关照片下载 preview。",
|
|
92
92
|
"- 路径直接写在 JSON 字符串里,不做 URL 编码;不要填远程 URL、API 密钥或 auth_code。图片仍受当前会话的文件权限控制。多张照片优先用相册,单张图片可继续用 Markdown。",
|
|
93
93
|
];
|
|
94
94
|
|
package/web/css/albums.css
CHANGED
|
@@ -1,40 +1,51 @@
|
|
|
1
|
-
.photo-album {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
.album-
|
|
10
|
-
.album-
|
|
11
|
-
.album-
|
|
12
|
-
.album-
|
|
13
|
-
.album-
|
|
14
|
-
.album-
|
|
15
|
-
.album-
|
|
16
|
-
.
|
|
17
|
-
.album-
|
|
18
|
-
.album-
|
|
19
|
-
.album-
|
|
20
|
-
.album-
|
|
21
|
-
.album-
|
|
22
|
-
.album-
|
|
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
|
-
.
|
|
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; }
|
|
34
38
|
@media (max-width: 600px) {
|
|
35
|
-
.
|
|
36
|
-
.album-
|
|
37
|
-
.
|
|
38
|
-
.
|
|
39
|
-
.
|
|
39
|
+
.album-head { padding: 14px; flex-wrap: wrap; gap: 6px; }
|
|
40
|
+
.album-head strong { font-size: 16px; }
|
|
41
|
+
.album-total { font-size: 10px; }
|
|
42
|
+
.album-slide { height: clamp(280px, 95vw, 420px); }
|
|
43
|
+
.album-nav { inset-inline: 8px; }
|
|
44
|
+
.album-arrow { width: 30px; height: 30px; }
|
|
45
|
+
.album-info { padding: 12px 14px 10px; }
|
|
46
|
+
.album-filmstrip { padding-inline: 14px; gap: 6px; }
|
|
47
|
+
.album-thumb { flex-basis: 48px; width: 48px; height: 48px; }
|
|
48
|
+
.album-footer { padding: 10px 14px 14px; }
|
|
49
|
+
.album-slide-hint { display: none; }
|
|
40
50
|
}
|
|
51
|
+
@media (prefers-reduced-motion: reduce) { .album-thumb, .album-slide-hint { transition: none; } }
|
package/web/css/messages.css
CHANGED
|
@@ -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,165 @@ 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
|
+
width: 100%;
|
|
1553
|
+
height: 100dvh;
|
|
1554
|
+
max-width: none;
|
|
1555
|
+
max-height: none;
|
|
1556
|
+
margin: 0;
|
|
1557
|
+
padding: 0;
|
|
1558
|
+
border: 0;
|
|
1559
|
+
border-radius: 0;
|
|
1560
|
+
background: var(--bg-side);
|
|
1561
|
+
color: var(--fg);
|
|
1562
|
+
overflow: hidden;
|
|
1563
|
+
--viewer-surface: var(--bg);
|
|
1564
|
+
}
|
|
1565
|
+
.diagram-viewer[open] { display: block; animation: viewer-appear 150ms ease-out; }
|
|
1566
|
+
.diagram-viewer::backdrop { background: var(--bg-side); }
|
|
1567
|
+
.diagram-viewer-head {
|
|
1568
|
+
position: absolute;
|
|
1569
|
+
z-index: 2;
|
|
1570
|
+
inset: 0 0 auto;
|
|
1571
|
+
display: flex;
|
|
1572
|
+
align-items: center;
|
|
1573
|
+
gap: 24px;
|
|
1574
|
+
min-height: 76px;
|
|
1575
|
+
padding: max(16px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) 16px max(28px, env(safe-area-inset-left));
|
|
1576
|
+
}
|
|
1577
|
+
.viewer-meta { flex: 1; min-width: 0; }
|
|
1578
|
+
.diagram-viewer .photo-name { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 500; }
|
|
1579
|
+
.diagram-viewer .photo-detail { margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11px; color: var(--fg-faint); }
|
|
1580
|
+
.diagram-viewer .photo-detail:empty { display: none; }
|
|
1581
|
+
.diagram-viewer .photo-count { flex-shrink: 0; color: var(--fg-faint); font-size: 12px; font-variant-numeric: tabular-nums; }
|
|
1582
|
+
.viewer-actions { display: flex; align-items: center; gap: 8px; }
|
|
1583
|
+
.diagram-viewer .viewer-button {
|
|
1584
|
+
display: inline-grid;
|
|
1585
|
+
place-items: center;
|
|
1586
|
+
flex-shrink: 0;
|
|
1587
|
+
width: 36px;
|
|
1588
|
+
height: 36px;
|
|
1589
|
+
padding: 0;
|
|
1590
|
+
border: 0;
|
|
1591
|
+
border-radius: 50%;
|
|
1592
|
+
background: transparent;
|
|
1593
|
+
color: var(--fg-soft);
|
|
1594
|
+
font: inherit;
|
|
1595
|
+
line-height: 1;
|
|
1596
|
+
text-decoration: none;
|
|
1597
|
+
cursor: pointer;
|
|
1598
|
+
-webkit-tap-highlight-color: transparent;
|
|
1599
|
+
transition: background 120ms, color 120ms;
|
|
1600
|
+
}
|
|
1601
|
+
.diagram-viewer .viewer-button:hover { background: var(--bg-hover); color: var(--fg); }
|
|
1602
|
+
.diagram-viewer .viewer-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
|
|
1603
|
+
.diagram-viewer .viewer-button:disabled { opacity: 0.2; cursor: default; }
|
|
1604
|
+
.diagram-viewer .viewer-button svg { display: block; width: 18px; height: 18px; }
|
|
1605
|
+
.diagram-viewer .viewer-close { width: 40px; height: 40px; }
|
|
1606
|
+
.diagram-viewer .viewer-close svg { width: 20px; height: 20px; }
|
|
1607
|
+
.diagram-stage {
|
|
1608
|
+
position: absolute;
|
|
1609
|
+
inset: 80px 64px 104px;
|
|
1610
|
+
min-height: 0;
|
|
1611
|
+
overflow: hidden;
|
|
1612
|
+
touch-action: none;
|
|
1613
|
+
cursor: grab;
|
|
1614
|
+
user-select: none;
|
|
1615
|
+
}
|
|
1616
|
+
.diagram-stage.dragging { cursor: grabbing; }
|
|
1617
|
+
.diagram-stage img, .md .diagram-stage img {
|
|
1618
|
+
position: absolute;
|
|
1619
|
+
left: 0;
|
|
1620
|
+
top: 0;
|
|
1621
|
+
display: block;
|
|
1622
|
+
max-width: none;
|
|
1623
|
+
max-height: none;
|
|
1624
|
+
margin: 0;
|
|
1625
|
+
border-radius: 6px;
|
|
1626
|
+
transform-origin: 0 0;
|
|
1627
|
+
pointer-events: none;
|
|
1628
|
+
}
|
|
1629
|
+
.diagram-viewer-status {
|
|
1630
|
+
position: absolute;
|
|
1631
|
+
left: 50%;
|
|
1632
|
+
bottom: 16px;
|
|
1633
|
+
transform: translateX(-50%);
|
|
1634
|
+
max-width: calc(100% - 24px);
|
|
1635
|
+
padding: 8px 14px;
|
|
1636
|
+
border-radius: 12px;
|
|
1637
|
+
background: var(--viewer-surface);
|
|
1638
|
+
box-shadow: 0 2px 16px rgb(0 0 0 / 6%);
|
|
1639
|
+
color: var(--fg-soft);
|
|
1640
|
+
font-size: 12px;
|
|
1641
|
+
text-align: center;
|
|
1642
|
+
pointer-events: none;
|
|
1643
|
+
}
|
|
1644
|
+
.diagram-viewer-status:empty { display: none; }
|
|
1645
|
+
.diagram-viewer-controls {
|
|
1646
|
+
position: absolute;
|
|
1647
|
+
z-index: 2;
|
|
1648
|
+
left: 50%;
|
|
1649
|
+
bottom: max(24px, env(safe-area-inset-bottom));
|
|
1650
|
+
transform: translateX(-50%);
|
|
1651
|
+
display: flex;
|
|
1652
|
+
align-items: center;
|
|
1653
|
+
gap: 4px;
|
|
1654
|
+
padding: 6px;
|
|
1655
|
+
border: 1px solid var(--border);
|
|
1656
|
+
border-radius: 999px;
|
|
1657
|
+
background: var(--viewer-surface);
|
|
1658
|
+
box-shadow: 0 4px 24px rgb(0 0 0 / 7%), 0 1px 3px rgb(0 0 0 / 3%);
|
|
1659
|
+
}
|
|
1660
|
+
@supports (background: color-mix(in srgb, white 90%, transparent)) {
|
|
1661
|
+
.diagram-viewer { --viewer-surface: color-mix(in srgb, var(--bg) 92%, transparent); }
|
|
1662
|
+
.diagram-viewer-controls, .diagram-viewer .viewer-prev, .diagram-viewer .viewer-next { backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); }
|
|
1663
|
+
}
|
|
1664
|
+
.diagram-viewer .viewer-original { width: 64px; border-radius: 999px; }
|
|
1665
|
+
.diagram-scale { font-size: 12px; font-variant-numeric: tabular-nums; }
|
|
1666
|
+
.viewer-divider { width: 1px; height: 18px; margin: 0 4px; background: var(--border); }
|
|
1667
|
+
.diagram-viewer .viewer-prev, .diagram-viewer .viewer-next {
|
|
1668
|
+
position: absolute;
|
|
1669
|
+
z-index: 2;
|
|
1670
|
+
top: 50%;
|
|
1671
|
+
transform: translateY(-50%);
|
|
1672
|
+
width: 40px;
|
|
1673
|
+
height: 40px;
|
|
1674
|
+
background: var(--viewer-surface);
|
|
1675
|
+
}
|
|
1676
|
+
.diagram-viewer .viewer-prev { left: max(16px, env(safe-area-inset-left)); }
|
|
1677
|
+
.diagram-viewer .viewer-next { right: max(16px, env(safe-area-inset-right)); }
|
|
1678
|
+
.viewer-next svg { transform: rotate(180deg); }
|
|
1679
|
+
.diagram-stage[data-loading="true"]::after {
|
|
1680
|
+
content: "";
|
|
1681
|
+
position: absolute;
|
|
1682
|
+
top: calc(50% - 10px);
|
|
1683
|
+
left: calc(50% - 10px);
|
|
1684
|
+
width: 20px;
|
|
1685
|
+
height: 20px;
|
|
1686
|
+
border: 2px solid var(--border);
|
|
1687
|
+
border-top-color: var(--fg-faint);
|
|
1688
|
+
border-radius: 50%;
|
|
1689
|
+
animation: viewer-spin 700ms linear infinite;
|
|
1690
|
+
}
|
|
1691
|
+
@keyframes viewer-spin { to { transform: rotate(360deg); } }
|
|
1692
|
+
@keyframes viewer-appear { from { opacity: 0; } to { opacity: 1; } }
|
|
1693
|
+
@media (max-width: 600px) {
|
|
1694
|
+
.diagram-viewer-head { gap: 12px; min-height: 68px; padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 12px max(20px, env(safe-area-inset-left)); }
|
|
1695
|
+
.viewer-actions { gap: 0; }
|
|
1696
|
+
.diagram-viewer .photo-name { font-size: 12px; }
|
|
1697
|
+
.diagram-viewer .viewer-button { width: 44px; height: 44px; }
|
|
1698
|
+
.diagram-viewer .viewer-original { width: 64px; }
|
|
1699
|
+
.diagram-stage { inset: 80px 12px 104px; }
|
|
1700
|
+
.diagram-viewer-controls { bottom: max(20px, env(safe-area-inset-bottom)); padding: 4px; gap: 0; }
|
|
1701
|
+
.diagram-viewer .viewer-prev { left: 8px; }
|
|
1702
|
+
.diagram-viewer .viewer-next { right: 8px; }
|
|
1703
|
+
}
|
|
1704
|
+
@media (prefers-reduced-motion: reduce) {
|
|
1705
|
+
.diagram-viewer[open], .diagram-stage[data-loading="true"]::after { animation: none; }
|
|
1706
|
+
.diagram-viewer .viewer-button { transition: none; }
|
|
1707
|
+
}
|
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) ||
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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 ===
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
}
|
package/web/lib/diagrams.js
CHANGED
|
@@ -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,175 @@
|
|
|
1
|
+
import { fill, h, icon } 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 }, 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: "viewer-button viewer-prev", "aria-label": "上一张照片", title: "上一张照片", onclick: () => show(index - 1) }, icon("back"));
|
|
14
|
+
const next = h("button", { class: "viewer-button viewer-next", "aria-label": "下一张照片", title: "下一张照片", onclick: () => show(index + 1) }, icon("back"));
|
|
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
|
+
stage.dataset.loading = "true";
|
|
24
|
+
notice = status.textContent = "";
|
|
25
|
+
fallback = item.fallback;
|
|
26
|
+
image.alt = heading.textContent = item.title;
|
|
27
|
+
heading.title = item.title;
|
|
28
|
+
detail.textContent = item.detail;
|
|
29
|
+
count.textContent = `${index + 1} / ${gallery.length}`;
|
|
30
|
+
previous.disabled = index === 0;
|
|
31
|
+
next.disabled = index === gallery.length - 1;
|
|
32
|
+
image.src = item.src;
|
|
33
|
+
onIndexChange?.(index);
|
|
34
|
+
}
|
|
35
|
+
const percent = h("output", { class: "diagram-scale", "aria-label": "缩放比例" });
|
|
36
|
+
let width = 0, height = 0, scale = 1, x = 0, y = 0, closed = false;
|
|
37
|
+
const pointers = new Map();
|
|
38
|
+
const paint = () => {
|
|
39
|
+
image.style.transform = `translate(${x}px, ${y}px) scale(${scale})`;
|
|
40
|
+
percent.textContent = `${Math.round(scale * 100)}%`;
|
|
41
|
+
};
|
|
42
|
+
// 图表是矢量图,打开时充分利用画布;照片保留原始分辨率上限。
|
|
43
|
+
const fitScale = () => Math.min(kind === "图表" ? 16 : 1, stage.clientWidth / width, stage.clientHeight / height);
|
|
44
|
+
const center = (s) => {
|
|
45
|
+
if (!width) return;
|
|
46
|
+
scale = s;
|
|
47
|
+
x = (stage.clientWidth - width * scale) / 2;
|
|
48
|
+
y = (stage.clientHeight - height * scale) / 2;
|
|
49
|
+
paint();
|
|
50
|
+
};
|
|
51
|
+
const fit = () => center(fitScale());
|
|
52
|
+
const zoom = (next, cx = stage.clientWidth / 2, cy = stage.clientHeight / 2) => {
|
|
53
|
+
if (!width) return;
|
|
54
|
+
next = Math.max(Math.min(fitScale(), 0.1), Math.min(16, next));
|
|
55
|
+
const ratio = next / scale;
|
|
56
|
+
x = cx - (cx - x) * ratio;
|
|
57
|
+
y = cy - (cy - y) * ratio;
|
|
58
|
+
scale = next;
|
|
59
|
+
paint();
|
|
60
|
+
};
|
|
61
|
+
const control = (label, content, onclick, className = "") => h("button", { class: `viewer-button ${className}`, "aria-label": label, title: label, onclick }, content);
|
|
62
|
+
const close = () => {
|
|
63
|
+
if (closed) return;
|
|
64
|
+
closed = true;
|
|
65
|
+
resize.disconnect();
|
|
66
|
+
if (dialog.open) dialog.close();
|
|
67
|
+
dialog.remove();
|
|
68
|
+
if (owner._closePreview === close) delete owner._closePreview;
|
|
69
|
+
if (trigger?.isConnected) trigger.focus({ preventScroll: true });
|
|
70
|
+
};
|
|
71
|
+
fill(dialog,
|
|
72
|
+
h("div", { class: "diagram-viewer-head" },
|
|
73
|
+
h("div", { class: "viewer-meta" }, heading, gallery ? detail : null),
|
|
74
|
+
gallery ? count : null,
|
|
75
|
+
h("div", { class: "viewer-actions" },
|
|
76
|
+
download ? h("a", { class: "viewer-button", href: download, download: title, "aria-label": "下载", title: "下载" },
|
|
77
|
+
h("span", { html: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 3v12m-5-5 5 5 5-5M4 16v4h16v-4"/></svg>' })) : null,
|
|
78
|
+
control(`关闭${kind}预览`, icon("x"), close, "viewer-close"))),
|
|
79
|
+
stage,
|
|
80
|
+
gallery ? [previous, next] : null,
|
|
81
|
+
h("div", { class: "diagram-viewer-controls", role: "toolbar", "aria-label": "缩放控制" },
|
|
82
|
+
control(`缩小${kind}`, icon("minus"), () => zoom(scale / 1.25)),
|
|
83
|
+
control("原始大小", percent, () => center(1), "viewer-original"),
|
|
84
|
+
control(`放大${kind}`, icon("plus"), () => zoom(scale * 1.25)),
|
|
85
|
+
h("span", { class: "viewer-divider", "aria-hidden": "true" }),
|
|
86
|
+
control("适应窗口", icon("expand"), fit)),
|
|
87
|
+
);
|
|
88
|
+
stage.addEventListener("dblclick", () => {
|
|
89
|
+
if (width) Math.abs(scale - fitScale()) < 0.001 ? center(1) : fit();
|
|
90
|
+
});
|
|
91
|
+
stage.addEventListener("wheel", (e) => {
|
|
92
|
+
e.preventDefault();
|
|
93
|
+
const rect = stage.getBoundingClientRect();
|
|
94
|
+
const delta = e.deltaY * (e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? stage.clientHeight : 1);
|
|
95
|
+
zoom(scale * Math.exp(-Math.max(-300, Math.min(300, delta)) * 0.002), e.clientX - rect.left, e.clientY - rect.top);
|
|
96
|
+
}, { passive: false });
|
|
97
|
+
stage.addEventListener("pointerdown", (e) => {
|
|
98
|
+
if (e.button !== 0 || pointers.size >= 2) return;
|
|
99
|
+
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
|
|
100
|
+
swipe = gallery && pointers.size === 1 && width && scale <= fitScale() * 1.01 ? { x: e.clientX, y: e.clientY } : null;
|
|
101
|
+
stage.setPointerCapture(e.pointerId);
|
|
102
|
+
stage.classList.add("dragging");
|
|
103
|
+
});
|
|
104
|
+
stage.addEventListener("pointermove", (e) => {
|
|
105
|
+
const old = pointers.get(e.pointerId);
|
|
106
|
+
if (!old) return;
|
|
107
|
+
const other = [...pointers.entries()].find(([id]) => id !== e.pointerId)?.[1];
|
|
108
|
+
if (swipe && !other) return;
|
|
109
|
+
if (other) {
|
|
110
|
+
const before = Math.hypot(old.x - other.x, old.y - other.y);
|
|
111
|
+
const after = Math.hypot(e.clientX - other.x, e.clientY - other.y);
|
|
112
|
+
const rect = stage.getBoundingClientRect();
|
|
113
|
+
if (before > 0) zoom(scale * after / before, (old.x + other.x) / 2 - rect.left, (old.y + other.y) / 2 - rect.top);
|
|
114
|
+
x += (e.clientX - old.x) / 2;
|
|
115
|
+
y += (e.clientY - old.y) / 2;
|
|
116
|
+
} else {
|
|
117
|
+
x += e.clientX - old.x;
|
|
118
|
+
y += e.clientY - old.y;
|
|
119
|
+
}
|
|
120
|
+
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
|
|
121
|
+
paint();
|
|
122
|
+
});
|
|
123
|
+
const release = (e) => {
|
|
124
|
+
if (swipe && e.type === "pointerup") {
|
|
125
|
+
const dx = e.clientX - swipe.x, dy = e.clientY - swipe.y;
|
|
126
|
+
if (Math.abs(dx) > 50 && Math.abs(dx) > Math.abs(dy) * 1.4) show(index + (dx < 0 ? 1 : -1));
|
|
127
|
+
}
|
|
128
|
+
swipe = null;
|
|
129
|
+
pointers.delete(e.pointerId);
|
|
130
|
+
if (!pointers.size) stage.classList.remove("dragging");
|
|
131
|
+
};
|
|
132
|
+
for (const event of ["pointerup", "pointercancel", "lostpointercapture"]) stage.addEventListener(event, release);
|
|
133
|
+
dialog.addEventListener("keydown", (e) => {
|
|
134
|
+
e.stopPropagation();
|
|
135
|
+
if (gallery && ["ArrowLeft", "ArrowRight"].includes(e.key)) {
|
|
136
|
+
e.preventDefault();
|
|
137
|
+
show(index + (e.key === "ArrowRight" ? 1 : -1));
|
|
138
|
+
}
|
|
139
|
+
if (["+", "=", "-", "0"].includes(e.key)) {
|
|
140
|
+
e.preventDefault();
|
|
141
|
+
if (e.key === "0") fit();
|
|
142
|
+
else zoom(scale * (e.key === "-" ? 0.8 : 1.25));
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
dialog.addEventListener("click", (e) => { if (e.target === dialog) close(); });
|
|
146
|
+
dialog.addEventListener("close", close);
|
|
147
|
+
const resize = new ResizeObserver(() => {
|
|
148
|
+
if (!dialog.isConnected) close();
|
|
149
|
+
else fit();
|
|
150
|
+
});
|
|
151
|
+
image.addEventListener("load", () => {
|
|
152
|
+
delete stage.dataset.loading;
|
|
153
|
+
status.textContent = notice;
|
|
154
|
+
image.style.visibility = "";
|
|
155
|
+
width = image.naturalWidth;
|
|
156
|
+
height = image.naturalHeight;
|
|
157
|
+
image.style.width = `${width}px`;
|
|
158
|
+
image.style.height = `${height}px`;
|
|
159
|
+
fit();
|
|
160
|
+
});
|
|
161
|
+
image.addEventListener("error", () => {
|
|
162
|
+
delete stage.dataset.loading;
|
|
163
|
+
if (fallback && image.getAttribute("src") !== fallback) {
|
|
164
|
+
notice = status.textContent = "预览不可用,显示缩略图";
|
|
165
|
+
image.src = fallback;
|
|
166
|
+
} else status.textContent = `${kind}无法加载`;
|
|
167
|
+
});
|
|
168
|
+
// 随消息销毁;关闭时释放观察器并恢复键盘焦点。
|
|
169
|
+
owner._closePreview = close;
|
|
170
|
+
owner.append(dialog);
|
|
171
|
+
dialog.showModal();
|
|
172
|
+
if (gallery) show(start);
|
|
173
|
+
else image.src = src;
|
|
174
|
+
resize.observe(stage);
|
|
175
|
+
}
|
package/web/views/messages.js
CHANGED
|
@@ -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
|
-
/**
|
|
500
|
+
/** 普通图片预览:显式关闭按钮,支持缩放和拖动,保留下载入口。 */
|
|
500
501
|
export function openLightbox(src, name, download) {
|
|
501
|
-
|
|
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;只多出一两行的不收 */
|