From f5099345d0df6d0abd12951bed93f9f7cb2ada0c Mon Sep 17 00:00:00 2001 From: kicer Date: Fri, 13 Nov 2026 16:12:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(ui):=20=E4=BB=A3=E7=A0=81=E9=A2=84=E8=A7=88?= =?UTF-8?q?=E7=9A=84=E7=BC=A9=E8=BF=9B=E7=A9=BA=E7=99=BD=E8=A2=AB=E5=90=83?= =?UTF-8?q?=E6=8E=89=EF=BC=8CGit=20diff=20=E6=A8=AA=E5=90=91=E6=BB=9A?= =?UTF-8?q?=E5=8A=A8=E6=97=B6=E8=A1=8C=E8=83=8C=E6=99=AF=E9=9C=B2=E7=99=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 两个渲染层的坑: 1) 代码预览每行是
直接放文本节点,而 .pv-cl 是 display:flex,裸文本成了匿名 flex 项——浏览器会折叠它的前导空白,整行 只有缩进时该文本节点直接被当成「flex 项之间的空白」丢掉。现在代码包进 显式的 ,.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 铺开)。渲染详情与展开文件时各同步一次。 --- app/server/public/css/style.css | 6 ++++++ app/server/public/js/preview.js | 5 +++-- app/server/public/js/vcs.js | 16 ++++++++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/server/public/css/style.css b/app/server/public/css/style.css index 1f2a281..2c2e05b 100644 --- a/app/server/public/css/style.css +++ b/app/server/public/css/style.css @@ -1667,6 +1667,8 @@ body.pv-max .pv-pre { max-width: none; } /* 放大时源码面板同样铺满 */ 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; @@ -1746,8 +1748,12 @@ body.pv-max .pv-code { max-width: none; padding-left: 16px; padding-right: 16px; .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); } diff --git a/app/server/public/js/preview.js b/app/server/public/js/preview.js index b224a90..63c9c5c 100644 --- a/app/server/public/js/preview.js +++ b/app/server/public/js/preview.js @@ -564,8 +564,9 @@ async function pvTextNode(name, sizeText, r) { 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)生成:伪元素不进选区,复制不会把行号带走 - rows.push('
' + (useHl ? parts[i] : esc(shown[i])) + '
'); + // 行号交给 CSS counter(.pv-cl::before)生成:伪元素不进选区,复制不会把行号带走。 + // 代码必须包在 .c 里:裸文本在 flex 容器里是匿名 flex 项,浏览器会折叠它的前导空白。 + rows.push('
' + (useHl ? parts[i] : esc(shown[i])) + '
'); } code.innerHTML = rows.join(''); frag.appendChild(code); diff --git a/app/server/public/js/vcs.js b/app/server/public/js/vcs.js index fb9304d..add314a 100644 --- a/app/server/public/js/vcs.js +++ b/app/server/public/js/vcs.js @@ -130,10 +130,21 @@ '' + esc(BADGE[f.status] || f.status) + '' + '+' + f.adds + '-' + f.dels + '' + '
' + - '
' + patchHtml(f) + (f.truncated ? '
差异过大,已截断
' : '') + '
' + + '
' + patchHtml(f) + (f.truncated ? '
差异过大,已截断
' : '') + '
' + ''; } + // 行背景要盖满「可视宽度 ∩ 内容宽度」:只量 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; // 少量文件默认展开,多文件默认折叠 @@ -157,6 +168,7 @@ if (!d.files.length) parts.push('
没有文件变动
'); parts.push(''); bodyEl().innerHTML = parts.join(''); + syncPatchWidth(bodyEl()); } // ---------- 数据加载 ---------- @@ -248,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;