6 Commits
Author SHA1 Message Date
kicer 2be92e1106 perf(ui): 预览栏拖拽调宽改为每帧落值,拖动期间圈住重排
pointermove 每来一次就写 --pv-w:这个变量同时驱动 #pv-pane 宽度、#app 的 width 与
margin-left、#toast 的 left,等于每个鼠标事件都让整棵会话 DOM 重新布局、消息文字重新折行,
长会话下明显卡顿,鼠标 120Hz 时一帧还不止一次。

改成 pointermove 只记 lastX,rAF 里落一次值;pointerup 若还有挂起的帧先取消再补一次,
保证落点准确。拖动期间 body.pv-resizing 下:#app 关掉 pointer-events(图片/链接的 HTML5
拖拽不会再冒出来)、#chat-view 与 #pv-pane 各加 contain,把重排重绘圈在列内。

选中文字是同一处实现的两个缺口:pointerdown 没有 preventDefault,按下那一刻就可能开始
拖选;手柄只有 6px,点偏到会话区就完全没进 resizer 的 pointerdown。前者补上,后者把热区
放宽到 10px(hover 透明度同步从 .3 调到 .25)。
2026-11-13 16:16:03 +08:00
kicer f5099345d0 fix(ui): 代码预览的缩进空白被吃掉,Git diff 横向滚动时行背景露白
两个渲染层的坑:

1) 代码预览每行是 <div class="pv-cl"> 直接放文本节点,而 .pv-cl 是
display:flex,裸文本成了匿名 flex 项——浏览器会折叠它的前导空白,整行
只有缩进时该文本节点直接被当成「flex 项之间的空白」丢掉。现在代码包进
显式的 <span class="c">,.c 上 white-space: pre,缩进和 tab 都回来了。

2) Git diff 的行背景只到可视宽度:块级元素在 overflow-x 容器里宽度恒为
容器宽,内容溢出时背景不跟着延伸,横滑后短行右侧一片白。滚动区里包一层
.vcs-fb-in,行 width:100% 撑满它,宽度用 syncPatchWidth() 实测——
Math.max(clientWidth, scrollWidth),只量 clientWidth 会漏掉最长行的内容宽
(实测 699 vs 1129,13 行全部按 1129 铺开)。渲染详情与展开文件时各同步一次。
2026-11-13 16:12:40 +08:00
kicer 67dff34a89 fix(ui): 修好右下角运行中转圈的两段描边
上一轮把图标收进 sprite 后,转圈变成一个灰环:.scroll-bottom-btn .sb-spinner circle
和 ...path 是后代选择器,<use> 的内部元素在 shadow tree 里选不中,浅灰圆环与蓝色进度弧
的描边宽度、颜色全部回退成外层 svg 的 #556/2px。

改用 CSS 自定义属性穿透:--sb-ring / --sb-arc 定义在 .sb-spinner 上,symbol 里那两个元素
用 style 引用——presentation attribute 不解析 var(),必须写在 style 里。颜色仍归 CSS 管。

全量扫过一遍:CSS 里命中 svg 内部元素的后代选择器只有这一处,JS 里也没有 querySelector
直接改 path/circle 的写法。
2026-11-13 16:00:26 +08:00
kicer 8694061068 fix(ui): 空目录不再显示「(空目录)」,mtime 没变就不再请求
点开空文件夹只多一行「(空目录)」,本来就没有展开箭头(.tree-row.open 没有任何样式),
这行提示是唯一的视觉变化,去掉后点击就是看不出变化。根目录的空提示保留:整个文件树
空白时分不清是空还是没加载。

顺带省掉自动刷新时对空目录的重复请求:tree 接口给目录 entry 补 mtimeMs(readdir 的
Dirent 不带时间,statSync 一次即可),前端把确认空的目录记成 rel -> mtimeMs。目录内
增删子项会更新目录自身 mtime,父目录重拉时比对不上就重新请求——所以 AI 往空目录里写了
文件依然会自动出现,不会因为缓存看不见。invalidate(true) 保留这份记录,切会话/新建/
切隐藏文件(invalidate(false))清空。
2026-11-13 15:57:49 +08:00
kicer 95047678f3 feat(ui): 代码预览接入 Prism 语法高亮,md 代码块与 Git diff 共用一套配色
js/json/yml/sh/py 这类文件原来只有行号、没有颜色。vendor 里加 prism.min.js
(core + 20 种常用语言,52KB / gzip 18.7KB),UMD 直接 <script> 引入,与
marked 同一种加载方式,不需要构建。

代码预览是整块高亮后再按行拆:pvSplitHighlighted 维护一个打开的 span 栈,
行尾补 </span>、下一行行首重开同名标签。逐行调 Prism.highlight 会把多行注释、
三引号字符串、模板字符串切花;拆完行数与原文对不上时退回纯文本,保证行号不错位。

配色(GitHub 亮色系)用 :is(.pv-code, .pv-doc, .vcs-line) 覆盖三处:
- 代码预览 .pv-code
- md 里的围栏代码块:pvHighlightMdCode 用 highlightElement,只处理标了
  language-xx 且语言已打包的块,没有语言标记的保持纯文本
- Git 面板 diff:逐行着色(diff 是片段,跨行 token 本来也拼不完整),
  超过 2000 行的 patch 不上色,避免同步高亮把面板卡住
2026-11-13 15:53:46 +08:00
kicer baec30398a feat(ui): 代码预览加行号,放大时可铺满
js/json/yml/sh/py 这类文件原来只有一个 pre + textContent,纯等宽,看不出行,
放大预览也只是把 920px 的内容框居中摆着。

现在按行渲染 .pv-cl,行号用 CSS counter(.pv-cl::before)生成——伪元素不进
选区,拖选复制不会把行号带进剪贴板。行号列宽按最大行号位数(--nw)自适应,
不折行、横向滚动,视觉与 Git 面板的 .vcs-line 一致;超过 5000 行只渲染前段,
避免几万个逐行节点把面板拖死。

放大预览(body.pv-max)时代码区与源码面板都去掉 920px 上限铺满窗口。
2026-11-13 15:44:26 +08:00
7 changed files with 206 additions and 30 deletions
+39 -5
View File
@@ -1107,9 +1107,9 @@ body.voice-active .scroll-bottom-btn { display: none; }
}
.scroll-bottom-btn:active { opacity: 1; }
.scroll-bottom-btn svg { width: 20px; height: 20px; fill: none; stroke: #556; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.scroll-bottom-btn .sb-spinner { animation: sb-spin 0.8s linear infinite; }
.scroll-bottom-btn .sb-spinner circle { stroke: #dde; stroke-width: 2.5; fill: none; }
.scroll-bottom-btn .sb-spinner path { stroke: #3964FE; stroke-width: 2.5; stroke-linecap: round; fill: none; }
.scroll-bottom-btn .sb-spinner { --sb-ring: #dde; --sb-arc: #3964FE; animation: sb-spin 0.8s linear infinite; }
/* 圆环与蓝色进度弧的颜色用自定义属性传进 <use> 的 shadow tree:
后代选择器(.sb-spinner circle)选不中 symbol 里的元素,只有 CSS 变量能继承穿透。 */
@keyframes sb-spin { from { transform: rotate(0); } to { transform: rotate(360deg); } }
@@ -1656,12 +1656,42 @@ label.icon-btn { cursor: pointer; }
/* ---------- preview pane v2: drag resize / info card / plain text ---------- */
/* 拖拽调宽的手柄:预览在左半侧,所以手柄在它的右缘(与会话列的分界) */
.pv-resizer { position: absolute; right: 0; top: 0; bottom: 0; width: 6px; cursor: col-resize; z-index: 6; touch-action: none; }
.pv-resizer:hover, .pv-resizer:active { background: var(--active); opacity: .3; }
.pv-resizer { position: absolute; right: 0; top: 0; bottom: 0; width: 10px; cursor: col-resize; z-index: 6; touch-action: none; }
.pv-resizer:hover, .pv-resizer:active { background: var(--active); opacity: .25; }
body.pv-resizing { cursor: col-resize; user-select: none; }
/* 拖动期间把重排/重绘圈在各自列里:会话列宽度跟着变,不 contain 就是整页重排 */
body.pv-resizing #app { pointer-events: none; }
body.pv-resizing #chat-view { contain: paint; }
body.pv-resizing #pv-pane { contain: layout paint; }
body.pv-resizing #pv-pane iframe { pointer-events: none; }
.pv-plain { width: 100%; }
.pv-pre { width: 100%; max-width: 920px; margin: 0 auto; padding: 14px 20px 40px; font: 12.5px/1.65 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre-wrap; word-break: break-all; }
body.pv-max .pv-pre { max-width: none; } /* 放大时源码面板同样铺满 */
.pv-code { width: 100%; max-width: 920px; margin: 0 auto; padding: 10px 0 40px; overflow-x: auto;
counter-reset: pvline; font: 12.5px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; tab-size: 4; }
/* 单行:行号用 ::before + counter 生成。伪元素不参与选区,复制代码时不会把行号带进去 */
.pv-cl { display: flex; min-height: 1.6em; counter-increment: pvline; white-space: pre; padding-right: 12px; }
/* 代码必须落在显式元素里:flex 容器的裸文本是匿名 flex 项,前导空白会被折叠、纯缩进行会被丢掉 */
.pv-cl .c { flex: 1 0 auto; white-space: pre; }
.pv-cl::before {
content: counter(pvline); flex: none; width: calc(var(--nw, 3ch) + 18px); margin-right: 10px; padding-right: 8px;
text-align: right; color: #b0b6bd; user-select: none;
background: #fafbfc; border-right: 1px solid rgba(0,0,0,.04);
}
/* 放大预览时铺满:#pv-pane 已经是全屏,内容别再卡在 920px 的阅读宽度里 */
body.pv-max .pv-code { max-width: none; padding-left: 16px; padding-right: 16px; }
/* Prism token 配色(GitHub 亮色系):代码预览、md 围栏代码块、Git 面板 diff 共用 */
:is(.pv-code, .pv-doc, .vcs-line) :is(.token.comment, .token.prolog, .token.doctype, .token.cdata) { color: #6a737d; font-style: italic; }
:is(.pv-code, .pv-doc, .vcs-line) .token.punctuation { color: #24292e; }
:is(.pv-code, .pv-doc, .vcs-line) .token.namespace { opacity: .7; }
:is(.pv-code, .pv-doc, .vcs-line) :is(.token.property, .token.tag, .token.boolean, .token.number, .token.constant, .token.symbol, .token.deleted) { color: #005cc5; }
:is(.pv-code, .pv-doc, .vcs-line) :is(.token.selector, .token.attr-name, .token.string, .token.char, .token.builtin, .token.inserted) { color: #032f62; }
:is(.pv-code, .pv-doc, .vcs-line) :is(.token.operator, .token.entity, .token.url) { color: #d73a49; }
:is(.pv-code, .pv-doc, .vcs-line) :is(.token.atrule, .token.attr-value, .token.keyword) { color: #d73a49; }
:is(.pv-code, .pv-doc, .vcs-line) :is(.token.function, .token.class-name) { color: #6f42c1; }
:is(.pv-code, .pv-doc, .vcs-line) :is(.token.regex, .token.important, .token.variable) { color: #e36209; }
:is(.pv-code, .pv-doc, .vcs-line) .token.bold { font-weight: 600; }
:is(.pv-code, .pv-doc, .vcs-line) .token.italic { font-style: italic; }
.pv-trunc { max-width: 880px; margin: 12px auto 0; padding: 0 20px; font-size: 12px; color: var(--gray-light); }
.pv-loading.inline { position: static; background: transparent; padding: 56px 0; }
.pv-none { margin: auto; display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 44px 24px; text-align: center; }
@@ -1722,8 +1752,12 @@ body.pv-resizing #pv-pane iframe { pointer-events: none; }
.vcs-stat .d { color: #cf222e; }
.vcs-fb { display: none; border-top: 1px solid var(--border); overflow-x: auto; background: #fff; }
.vcs-file.open .vcs-fb { display: block; }
/* 滚动内容包一层:inline-block 的宽度由内容决定(max-content),行再撑满它。
否则块级行只有容器宽,横向滚动后短行右侧露白,红绿底色盖不住 */
.vcs-fb-in { display: block; min-width: 100%; }
/* 行:旧号 / 新号 / 代码三列。代码不换行,横向滚动(diff 语义上就该逐行对齐) */
.vcs-line { display: grid; grid-template-columns: 44px 44px minmax(0, 1fr);
width: 100%;
font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.vcs-line .n { padding: 0 6px; text-align: right; color: #b0b6bd; user-select: none;
background: #fafbfc; border-right: 1px solid rgba(0,0,0,.04); }
+2 -1
View File
@@ -8,6 +8,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover, interactive-widget=resizes-content">
<title>Agent 任务台</title>
<script src="/vendor/marked.min.js"></script>
<script src="/vendor/prism.min.js"></script>
<meta name="theme-color" content="#3964FE">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
@@ -106,7 +107,7 @@
<symbol id="i-mic" viewBox="0 0 24 24"><rect x="9" y="2" width="6" height="12" rx="3"></rect><path d="M5 10a7 7 0 0 0 14 0"></path><line x1="12" y1="19" x2="12" y2="22"></line></symbol>
<symbol id="i-keyboard" viewBox="0 0 24 24"><rect x="3" y="5" width="18" height="14" rx="2.5"></rect><circle cx="7" cy="9" r="1.1" fill="currentColor" stroke="none"></circle><circle cx="12" cy="9" r="1.1" fill="currentColor" stroke="none"></circle><circle cx="17" cy="9" r="1.1" fill="currentColor" stroke="none"></circle><circle cx="7" cy="12.5" r="1.1" fill="currentColor" stroke="none"></circle><circle cx="12" cy="12.5" r="1.1" fill="currentColor" stroke="none"></circle><circle cx="17" cy="12.5" r="1.1" fill="currentColor" stroke="none"></circle><line x1="8" y1="16" x2="16" y2="16"></line></symbol>
<symbol id="i-chevron-down" viewBox="0 0 24 24"><polyline points="6 9 12 15 18 9"></polyline></symbol>
<symbol id="i-spinner" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10"></circle><path d="M22 12a10 10 0 0 1-10 10"></path></symbol>
<symbol id="i-spinner" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10" style="fill:none;stroke:var(--sb-ring,#dde);stroke-width:2.5"></circle><path d="M22 12a10 10 0 0 1-10 10" style="fill:none;stroke:var(--sb-arc,#3964FE);stroke-width:2.5;stroke-linecap:round"></path></symbol>
<symbol id="i-search" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></symbol>
<symbol id="i-gear" viewBox="0 0 24 24"><circle cx="12" cy="12" r="3"></circle><path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z"></path></symbol>
<symbol id="i-upload-cloud" viewBox="0 0 24 24"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="17 8 12 3 7 8"></polyline><line x1="12" y1="3" x2="12" y2="15"></line></symbol>
+18 -7
View File
@@ -25,6 +25,7 @@
let expanded = new Set(['']); // 已展开的相对目录('' = 根)
let cache = new Map(); // rel -> /tree 响应
let loading = new Map(); // rel -> Promise(并发去重)
let emptyDirs = new Map(); // rel -> 目录 mtimeMs:确认空着的目录,mtime 没变就不必再请求
let gen = 0; // 会话切换/刷新时自增,防止旧请求回填到新树的缓存
// 多选:rel -> 'dir' | 'file'。勾目录表示整个目录(下载打成一个 zip、删除递归),
// 所以勾目录会吃掉子项,勾子项会把祖先目录降级掉,见 toggleSel。
@@ -35,7 +36,7 @@
gen++;
cache = new Map();
loading = new Map();
if (!keepExpanded) { expanded = new Set(['']); sel.clear(); }
if (!keepExpanded) { expanded = new Set(['']); sel.clear(); emptyDirs = new Map(); }
}
function sid() { return (state && state.sessionId) || ''; }
@@ -47,15 +48,24 @@
parent.appendChild(el);
}
function openDir(rel) {
function openDir(rel, dirMtime) {
if (cache.has(rel)) return Promise.resolve(cache.get(rel));
if (loading.has(rel)) return loading.get(rel);
if (!sid()) return Promise.resolve(null);
// 已知空目录且目录自身 mtime 未变(增删子项才会变):跳过请求,直接给空结果
if (dirMtime != null && emptyDirs.get(rel) === dirMtime) {
return Promise.resolve({ dir: rel, total: 0, capped: false, entries: [] });
}
const url = '/api/sessions/' + encodeURIComponent(sid()) + '/tree?dir=' +
encodeURIComponent(rel) + (showHidden ? '&hidden=1' : '');
const myGen = gen;
const p = req('GET', url).then(function (data) {
if (myGen === gen) { cache.set(rel, data); loading.delete(rel); }
if (myGen === gen) {
cache.set(rel, data); loading.delete(rel);
const n = (data && data.entries) ? data.entries.length : 0;
if (!n && dirMtime != null) emptyDirs.set(rel, dirMtime);
else if (n) emptyDirs.delete(rel);
}
return data;
}, function (e) {
if (myGen === gen) loading.delete(rel);
@@ -126,20 +136,21 @@
return row;
}
async function renderDirInto(container, rel, depth) {
async function renderDirInto(container, rel, depth, dirMtime) {
container.innerHTML = '';
const data = await openDir(rel);
const data = await openDir(rel, dirMtime);
if (!data) return;
const frag = document.createDocumentFragment();
const list = data.entries || [];
if (!list.length) tip(frag, '(空目录)');
// 只在根目录给空提示:子目录展开后本来就该「看不出变化」,根节点全空白则分不清是空还是没加载
if (!list.length && !rel) tip(frag, '(空目录)');
for (const ent of list) {
frag.appendChild(rowFor(ent, depth));
if (ent.type === 'dir' && !ent.ignored && expanded.has(ent.rel)) {
const kids = document.createElement('div');
kids.className = 'tree-children';
frag.appendChild(kids);
renderDirInto(kids, ent.rel, depth + 1).catch(function () {}); // 异步填充子层
renderDirInto(kids, ent.rel, depth + 1, ent.mtimeMs).catch(function () {}); // 异步填充子层
}
}
if (data.capped) tip(frag, '仅显示前 ' + list.length + ' 项(共 ' + data.total + ' 项)', 'tree-more');
+109 -9
View File
@@ -15,6 +15,59 @@ const PV_PLAIN = /\.(txt|log)$/i;
// 常见代码/配置/数据文本扩展名;无扩展名的文件(Dockerfile/LICENSE 等)也按文本尝试,二进制嗅探兜底
const PV_TEXT = /\.(md|markdown|txt|log|csv|tsv|json|jsonl|xml|yml|yaml|toml|ini|conf|cfg|properties|env|py|pyw|js|mjs|cjs|ts|tsx|jsx|java|kt|c|h|cpp|hpp|cc|cs|go|rs|rb|php|swift|sh|bash|zsh|bat|ps1|sql|lua|r|dart|vue|css|less|scss|srt|vtt)$/i;
const PV_TEXT_MAX = 512 * 1024; // 文本预览截断阈值(字符),超出提示下载看全文
const PV_CODE_MAX = 5000; // 代码预览最多逐行渲染的行数:行号要逐行建节点,几万行会把面板拖死
// 扩展名 → Prism 语言 id。只映射打包进来的语言(见 vendor/prism.min.js),命中不了就当纯文本
const PV_LANG = {
js: 'javascript', mjs: 'javascript', cjs: 'javascript', jsx: 'jsx',
ts: 'typescript', mts: 'typescript', cts: 'typescript', tsx: 'tsx',
json: 'json', jsonl: 'json', json5: 'json',
yml: 'yaml', yaml: 'yaml',
sh: 'bash', bash: 'bash', zsh: 'bash', ksh: 'bash', env: 'bash',
py: 'python', pyw: 'python',
go: 'go', rs: 'rust', java: 'java',
c: 'c', h: 'c', cpp: 'cpp', hpp: 'cpp', cc: 'cpp', cxx: 'cpp', hh: 'cpp',
sql: 'sql', css: 'css', wxss: 'css',
html: 'markup', htm: 'markup', xml: 'markup', svg: 'markup', vue: 'markup',
md: 'markdown', markdown: 'markdown',
toml: 'toml', ini: 'ini', conf: 'ini', cfg: 'ini', properties: 'ini',
diff: 'diff', patch: 'diff',
};
// 整块高亮;Prism 没加载、语言没打包、或抛异常都返回 null,调用方退回纯文本
function pvHighlightCode(text, name) {
if (typeof Prism === 'undefined' || !Prism.languages) return null;
const ext = (String(name).match(/\.([^.\\/]+)$/) || [])[1];
const lang = PV_LANG[String(ext || '').toLowerCase()];
const grammar = lang && Prism.languages[lang];
if (!grammar) return null;
try { return Prism.highlight(text, grammar, lang); } catch (_) { return null; }
}
// 把整块高亮 HTML 按行拆开:跨行的 <span> 在行尾补 </span>、下一行行首重开。
// 逐行调用 Prism.highlight 会把多行注释/三引号字符串切花,所以必须先整块高亮再拆。
// Prism 输出的文本已转义(&lt; 等),不存在裸 '<',因此只匹配标签与换行就够了。
function pvSplitHighlighted(html) {
const out = [];
const stack = [];
let cur = '';
let last = 0;
const re = /(\n|<span\b[^>]*>|<\/span>)/g;
let m;
while ((m = re.exec(html))) {
cur += html.slice(last, m.index);
last = m.index + m[0].length;
if (m[0] === '\n') {
out.push(cur + '</span>'.repeat(stack.length));
cur = stack.join('');
} else if (m[0] === '</span>') {
stack.pop();
cur += '</span>';
} else {
stack.push(m[0]);
cur += m[0];
}
}
out.push(cur + html.slice(last)); // 末尾无条件留一行:文本以 \n 结尾时最后是空行
return out;
}
const PV_SRC_MAX = 2 * 1024 * 1024; // 源码面板的显示上限,超过就只给前 2MB(大文件塞 pre 会卡)
let pvSeq = 0; // 预览序号:快速连点时旧请求不许再动 DOM
// reload: 当前内容的"重新载入"动作(文本类无 iframe,按名重开);为 null 时回退到 reload iframe
@@ -97,7 +150,7 @@ function pvToggleSrc() {
let text = pvState.source;
if (text.length > PV_SRC_MAX) text = text.slice(0, PV_SRC_MAX) + '\n\n…(源码较大,仅显示前 ' + Math.round(PV_SRC_MAX / 1024) + ' KB)';
const pre = document.createElement('pre');
pre.className = 'pv-pre pv-src-pre'; // 代码类预览本身也是 .pv-pre,多一个类便于区分/定位
pre.className = 'pv-pre pv-src-pre'; // 源码面板:不加行号的原文(代码预览本身走 .pv-code)
pre.textContent = text; // 不用 innerHTML:源码里的标签必须当文本
pvState.srcPre = pre;
document.getElementById('pv-body').appendChild(pre);
@@ -113,6 +166,20 @@ function pvToggleSrc() {
btn.setAttribute('aria-label', lbl);
}
// md 里的围栏代码块上色:marked 输出 <pre><code class="language-xx">,命中 PV_LANG 才处理。
// highlightElement 会重写 code 的 innerHTML,复制按钮挂在父级 .code-wrap 上,不受影响。
function pvHighlightMdCode(root) {
if (typeof Prism === 'undefined' || !Prism.languages) return;
root.querySelectorAll('pre code').forEach((el) => {
if (el.dataset.hl) return; // 幂等:同一节点被重复装饰时不再处理
const m = /language-([\w-]+)/.exec(String(el.className || ''));
const lang = m && PV_LANG[m[1].toLowerCase()];
if (!lang || !Prism.languages[lang]) return;
el.dataset.hl = '1';
try { Prism.highlightElement(el); } catch (_) {}
});
}
// 每次展示内容都过一遍:标题显示相对路径(子目录可见),右上角下载钮随文件走
function pvHeader(name, sizeText) {
pvState.name = name;
@@ -459,6 +526,7 @@ async function pvTextNode(name, sizeText, r) {
wrap.className = 'msg other';
wrap.innerHTML = '<div class="' + cls + ' pv-doc">' + html + '</div>';
decorateCodePre(wrap);
pvHighlightMdCode(wrap); // 围栏代码块上色(与代码预览同一套配色)
const n = wrap.firstChild;
// q.fmt=false 表示 marked 不可用、面板就是原文排版:新窗口也按纯文本开,避免空白被折叠
n.__tb = q.fmt
@@ -483,10 +551,31 @@ async function pvTextNode(name, sizeText, r) {
tip.textContent = '文件较大,仅预览前 ' + Math.round(PV_TEXT_MAX / 1024) + ' KB,完整内容请下载查看。';
frag.appendChild(tip);
}
const pre = document.createElement('pre');
pre.className = 'pv-pre';
pre.textContent = bodyText;
frag.appendChild(pre);
// 代码/配置/数据:逐行渲染以便左侧打行号(视觉同 Git 面板的 .vcs-line)。
// 超过 PV_CODE_MAX 行只渲染前段,剩下的交代一句——几万个逐行节点会让面板卡到没法滚。
const lines = bodyText.split('\n');
const total = lines.length;
const shown = total > PV_CODE_MAX ? lines.slice(0, PV_CODE_MAX) : lines;
const code = document.createElement('div');
code.className = 'pv-code';
code.style.setProperty('--nw', Math.max(2, String(shown.length).length) + 'ch'); // 行号列宽按最大行号位数
const rows = [];
const hl = pvHighlightCode(shown.join('\n'), name); // 整块高亮再按行拆,跨行 token 才不会花
const parts = hl ? pvSplitHighlighted(hl) : null;
const useHl = parts && parts.length === shown.length; // 行数对不上就退回纯文本,不让行号错位
for (let i = 0; i < shown.length; i++) {
// 行号交给 CSS counter(.pv-cl::before)生成:伪元素不进选区,复制不会把行号带走。
// 代码必须包在 .c 里:裸文本在 flex 容器里是匿名 flex 项,浏览器会折叠它的前导空白。
rows.push('<div class="pv-cl"><span class="c">' + (useHl ? parts[i] : esc(shown[i])) + '</span></div>');
}
code.innerHTML = rows.join('');
frag.appendChild(code);
if (total > PV_CODE_MAX) {
const tip2 = document.createElement('div');
tip2.className = 'pv-trunc';
tip2.textContent = '文件较大,仅显示前 ' + PV_CODE_MAX + ' 行(共 ' + total + ' 行),完整内容请下载查看。';
frag.appendChild(tip2);
}
const holder = document.createElement('div');
holder.className = 'pv-plain';
holder.appendChild(frag);
@@ -546,6 +635,7 @@ async function pvRenderLegacy(name, r, my) {
wrap.className = 'msg other';
wrap.innerHTML = '<div class="' + cls + '">' + html + '</div>';
decorateCodePre(wrap);
pvHighlightMdCode(wrap);
pvOverlayShow(wrap.firstChild, { bar: true, name: name, source: text }); // 全宽沉浸,eye/复制看原文
}
}
@@ -699,21 +789,31 @@ window.matchMedia('(min-width: 960px)').addEventListener('change', () => { if (!
const h = document.createElement('div');
h.className = 'pv-resizer';
pane.appendChild(h);
let st = null;
let st = null; // { x: 起始 clientX, w: 起始宽度, lastX: 最新 clientX }
let rafId = 0;
// 改 --pv-w 会带着 #app 的 width/margin 一起重排(会话里的文字跟着重新折行),
// 一帧可能来十几个 pointermove,所以只在 rAF 里落一次值。
function commit() {
if (!st) return;
document.documentElement.style.setProperty('--pv-w', clamp(st.w + (st.lastX - st.x)) + 'px');
}
h.addEventListener('pointerdown', (e) => {
st = { x: e.clientX, w: pane.offsetWidth || clamp(window.innerWidth * 0.5) };
e.preventDefault(); // 从按下这一刻就掐掉拖选,否则会顺手选中一大片消息文字
st = { x: e.clientX, w: pane.offsetWidth || clamp(window.innerWidth * 0.5), lastX: e.clientX };
try { h.setPointerCapture(e.pointerId); } catch (_) {}
document.body.classList.add('pv-resizing');
});
h.addEventListener('pointermove', (e) => {
if (!st) return;
document.documentElement.style.setProperty('--pv-w', clamp(st.w + (e.clientX - st.x)) + 'px');
st.lastX = e.clientX;
if (!rafId) rafId = requestAnimationFrame(() => { rafId = 0; commit(); });
});
function end() {
if (!st) return;
if (rafId) { cancelAnimationFrame(rafId); rafId = 0; commit(); } // 把最后一帧补上
const w = pane.offsetWidth;
st = null;
document.body.classList.remove('pv-resizing');
const w = pane.offsetWidth;
if (w) localStorage.setItem('pv-w', String(w));
}
h.addEventListener('pointerup', end);
+33 -7
View File
@@ -75,9 +75,21 @@
}
// ---------- 详情(提交 / 未提交共用)----------
function lineHtml(cls, oldNo, newNo, text) {
// 语法高亮:与预览共用 PV_LANG(preview.js)。只能逐行高亮:diff 是片段,多行注释/三引号串
// 本来就拼不完整;逐行反而不会把行号与红绿底色搞乱。
function prismLang(path) {
if (typeof PV_LANG === 'undefined' || typeof Prism === 'undefined' || !Prism.languages) return null;
const m = /\.([^.\\/]+)$/.exec(String(path || ''));
const lang = m && PV_LANG[m[1].toLowerCase()];
return lang && Prism.languages[lang] ? lang : null;
}
function codeEsc(text, lang) {
if (!lang) return esc(text);
try { return Prism.highlight(text, Prism.languages[lang], lang); } catch (_) { return esc(text); }
}
function lineHtml(cls, oldNo, newNo, text, lang) {
return '<div class="vcs-line' + (cls ? ' ' + cls : '') + '"><span class="n">' + (oldNo == null ? '' : oldNo) +
'</span><span class="n">' + (newNo == null ? '' : newNo) + '</span><span class="c">' + esc(text) + '</span></div>';
'</span><span class="n">' + (newNo == null ? '' : newNo) + '</span><span class="c">' + codeEsc(text, lang) + '</span></div>';
}
// 行号自己推:@@ -a,b +c,d @@ 之后的上下文/+/- 分别推进两个计数器
@@ -86,6 +98,8 @@
if (!f.patch) return '<div class="vcs-nodiff">没有可显示的差异</div>';
const out = [];
const lines = String(f.patch).split('\n');
// 大 patch 不上色:逐行跑 Prism 是同步的,几千行会把面板卡一下
const lang = lines.length <= 2000 ? prismLang(f.path) : null;
let oldNo = 0, newNo = 0, inHunk = false, rows = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
@@ -99,9 +113,9 @@
if (!inHunk) continue; // 文件头(diff --git / index / --- / +++)由文件行本身表达,不重复渲染
if (line.charAt(0) === '\\') { out.push('<div class="vcs-line meta"><span class="n"></span><span class="n"></span><span class="c">' + esc(line) + '</span></div>'); continue; }
const c = line.charAt(0);
if (c === '+') out.push(lineHtml('a', null, newNo++, line.slice(1)));
else if (c === '-') out.push(lineHtml('d', oldNo++, null, line.slice(1)));
else out.push(lineHtml('', oldNo++, newNo++, line.slice(1)));
if (c === '+') out.push(lineHtml('a', null, newNo++, line.slice(1), lang));
else if (c === '-') out.push(lineHtml('d', oldNo++, null, line.slice(1), lang));
else out.push(lineHtml('', oldNo++, newNo++, line.slice(1), lang));
if (++rows > 3000) { out.push('<div class="vcs-nodiff">行数过多,剩余部分已隐藏</div>'); break; }
}
return out.join('') || '<div class="vcs-nodiff">仅改名或模式变更,没有内容差异</div>';
@@ -116,10 +130,21 @@
'<span class="vcs-badge ' + esc(String(f.status || '').toLowerCase()) + '">' + esc(BADGE[f.status] || f.status) + '</span>' +
'<span class="vcs-stat"><b class="a">+' + f.adds + '</b><b class="d">-' + f.dels + '</b></span>' +
'</div>' +
'<div class="vcs-fb">' + patchHtml(f) + (f.truncated ? '<div class="vcs-nodiff">差异过大,已截断</div>' : '') + '</div>' +
'<div class="vcs-fb"><div class="vcs-fb-in">' + patchHtml(f) + (f.truncated ? '<div class="vcs-nodiff">差异过大,已截断</div>' : '') + '</div></div>' +
'</div>';
}
// 行背景要盖满「可视宽度 ∩ 内容宽度」:只量 clientWidth 不够,长行溢出到 1129 时行背景仍只有 699,
// 横滑右边就露白。scrollWidth 正好是最长行的内容宽,取两者较大值写成 px 给内层。
function syncPatchWidth(scope) {
(scope || document).querySelectorAll('.vcs-fb').forEach(function (fb) {
const inner = fb.firstElementChild;
if (!inner) return;
const w = Math.max(fb.clientWidth, fb.scrollWidth);
if (w > 0) inner.style.minWidth = w + 'px';
});
}
function renderDetail(d) {
st.cur = d;
const openAll = d.files.length <= 3; // 少量文件默认展开,多文件默认折叠
@@ -143,6 +168,7 @@
if (!d.files.length) parts.push('<div class="vcs-empty">没有文件变动</div>');
parts.push('</div>');
bodyEl().innerHTML = parts.join('');
syncPatchWidth(bodyEl());
}
// ---------- 数据加载 ----------
@@ -234,7 +260,7 @@
const back = e.target.closest('.vcs-back');
if (back) { loadList(false); return; }
const fh = e.target.closest('.vcs-fh');
if (fh) { fh.parentNode.classList.toggle('open'); return; }
if (fh) { fh.parentNode.classList.toggle('open'); syncPatchWidth(fh.parentNode); return; }
});
window.openVcsPane = openPane;
File diff suppressed because one or more lines are too long
+4 -1
View File
@@ -119,7 +119,10 @@ router.get('/:id/tree', function (req, res) {
const childRel = rel ? rel + '/' + e.name : e.name;
if (e.isSymbolicLink()) { entries.push({ name: e.name, rel: childRel, type: 'link' }); continue; }
if (e.isDirectory()) {
entries.push({ name: e.name, rel: childRel, type: 'dir', ignored: TREE_IGNORE_DIRS.has(e.name) });
// mtimeMs 给前端判断空目录是否还是空的:目录内增删子项会更新它
let mtimeMs = null;
try { mtimeMs = fs.statSync(path.join(abs, e.name)).mtimeMs; } catch (_) {}
entries.push({ name: e.name, rel: childRel, type: 'dir', ignored: TREE_IGNORE_DIRS.has(e.name), mtimeMs: mtimeMs });
} else if (e.isFile()) {
let size = null;
try { size = fs.statSync(path.join(abs, e.name)).size; } catch (_) {}