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 铺开)。渲染详情与展开文件时各同步一次。
This commit is contained in:
2026-11-13 16:12:40 +08:00
parent 67dff34a89
commit f5099345d0
3 changed files with 23 additions and 4 deletions
+6
View File
@@ -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); }
+3 -2
View File
@@ -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('<div class="pv-cl">' + (useHl ? parts[i] : esc(shown[i])) + '</div>');
// 行号交给 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);
+14 -2
View File
@@ -130,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; // 少量文件默认展开,多文件默认折叠
@@ -157,6 +168,7 @@
if (!d.files.length) parts.push('<div class="vcs-empty">没有文件变动</div>');
parts.push('</div>');
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;