feat(ui): 文件树新增 Git 面板(提交历史 / 提交详情 / 未提交改动)
原来想看仓库改了什么只能开终端敲 git,界面里没有入口。复用预览栏做二选一 (body.pv-mode-vcs):打开时占预览的位置,点文件自动切回预览,不再各占一块屏。 三个视图只用一个渲染器:列表 → 点提交进详情,顶部「未提交的改动」进工作区, 两者仅头部信息不同(有无 commit 元信息)。只读,不做 commit / checkout / pull。 不做任何自动刷新:只在打开面板、切视图、点刷新时请求,避免边干活边被打断。 后端 git.js 用 execFile 调 git,加 -c core.quotepath=false 保中文路径不被转义成 八进制;status 用 -z 输出,否则含空格的路径带引号、和 diff 里的裸路径对不上。 patch 按单文件 120KB / 合计 400KB 截断,未跟踪文件用 diff --no-index 现算。 不是仓库或没装 git 返回 HTTP 200 + reason,由界面给空状态。 镜像补装 git,apt 源照 executor/Dockerfile 换成腾讯云镜像(默认源在国内 update 会卡死)。
This commit is contained in:
@@ -1640,6 +1640,73 @@ body.pv-resizing #pv-pane iframe { pointer-events: none; }
|
||||
.pv-none button { margin-top: 8px; border: 0; background: var(--active); color: #fff; border-radius: 8px; padding: 9px 22px; font-size: 13px; cursor: pointer; }
|
||||
.pv-none button:hover { opacity: .88; }
|
||||
|
||||
/* ---------- Git 面板(复用预览栏:body.pv-mode-vcs。列表 / 提交详情 / 未提交改动)---------- */
|
||||
/* 文件树头的 Git 钮开启时高亮用通用的 .icon-btn.active(如显示隐藏文件那种),不另造类 */
|
||||
.vcs-list { padding-bottom: 24px; }
|
||||
/* 列表顶部的仓库行:会话目录可能只是仓库的某个子目录,所以显式写出 root */
|
||||
.vcs-repo { padding: 9px 16px; border-bottom: 1px solid var(--border); background: var(--drawer-bg);
|
||||
font: 11.5px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--gray);
|
||||
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.vcs-empty { padding: 44px 24px; text-align: center; font-size: 13px; color: var(--gray-light); }
|
||||
.vcs-item { padding: 10px 16px; border-bottom: 1px solid var(--border); cursor: pointer; }
|
||||
.vcs-item:hover { background: var(--surface); }
|
||||
.vcs-item .t { font-size: 13.5px; font-weight: 600; color: var(--ink); word-break: break-word; line-height: 1.45; }
|
||||
.vcs-item .m { margin-top: 3px; font-size: 12px; color: var(--gray); }
|
||||
.vcs-item .m code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
|
||||
/* 「未提交的改动」入口:列表顶部单独一行,与提交行区分 */
|
||||
.vcs-item.wt { background: var(--active-light); }
|
||||
.vcs-item.wt:hover { background: #e3ecfe; }
|
||||
.vcs-more { display: block; margin: 14px auto 0; padding: 7px 18px; font-size: 12.5px; color: var(--ink);
|
||||
background: var(--page); border: 1px solid var(--border); border-radius: 8px; cursor: pointer; }
|
||||
.vcs-more:hover { background: var(--surface); }
|
||||
.vcs-back { display: flex; align-items: center; gap: 6px; padding: 8px 14px; font-size: 12.5px; color: var(--active);
|
||||
background: var(--page); border-bottom: 1px solid var(--border); cursor: pointer; position: sticky; top: 0; z-index: 2; }
|
||||
.vcs-back:hover { background: var(--surface); }
|
||||
.vcs-commit { padding: 12px 16px 14px; border-bottom: 1px solid var(--border); background: var(--drawer-bg); }
|
||||
.vcs-subject { font-size: 14px; font-weight: 700; line-height: 1.5; word-break: break-word; }
|
||||
.vcs-meta { margin-top: 6px; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 12px; color: var(--gray); }
|
||||
.vcs-meta code { padding: 1px 6px; border-radius: 5px; background: var(--surface); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
|
||||
.vcs-meta .vcs-hash { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--active); }
|
||||
.vcs-err-t { max-width: 420px; }
|
||||
.vcs-msg { margin: 8px 0 0; font: 12.5px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre-wrap; color: #444; }
|
||||
.vcs-sum { display: flex; align-items: center; gap: 10px; padding: 8px 16px; font-size: 12px; color: var(--gray); border-bottom: 1px solid var(--border); }
|
||||
.vcs-sum b.a { color: #1a7f37; }
|
||||
.vcs-sum b.d { color: #cf222e; }
|
||||
.vcs-file { border-bottom: 1px solid var(--border); }
|
||||
.vcs-fh { display: flex; align-items: center; gap: 8px; padding: 8px 12px; font-size: 12.5px; cursor: pointer; user-select: none; }
|
||||
.vcs-fh:hover { background: var(--surface); }
|
||||
.vcs-caret { flex: none; width: 10px; color: var(--gray); transition: transform .12s ease; }
|
||||
.vcs-file.open .vcs-caret { transform: rotate(90deg); }
|
||||
.vcs-path { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--ink); }
|
||||
.vcs-badge { flex: none; padding: 0 5px; border-radius: 4px; font-size: 11px; line-height: 17px; background: var(--surface); color: var(--gray); }
|
||||
.vcs-badge.m { color: #9a6700; background: #fff8c5; }
|
||||
.vcs-badge.a { color: #1a7f37; background: #dafbe1; }
|
||||
.vcs-badge.d { color: #cf222e; background: #ffebe9; }
|
||||
.vcs-badge.r { color: #0550ae; background: #ddf4ff; }
|
||||
.vcs-badge.u { color: #6639ba; background: #f3e8ff; }
|
||||
.vcs-stat { flex: none; font-size: 11.5px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
|
||||
.vcs-stat .a { color: #1a7f37; }
|
||||
.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; }
|
||||
/* 行:旧号 / 新号 / 代码三列。代码不换行,横向滚动(diff 语义上就该逐行对齐) */
|
||||
.vcs-line { display: grid; grid-template-columns: 44px 44px minmax(0, 1fr);
|
||||
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); }
|
||||
.vcs-line .c { padding: 0 8px; white-space: pre; color: var(--ink); }
|
||||
.vcs-line.a { background: #e6ffec; }
|
||||
.vcs-line.a .n { background: #ccffd8; }
|
||||
.vcs-line.d { background: #ffebe9; }
|
||||
.vcs-line.d .n { background: #ffd7d5; }
|
||||
.vcs-line.h { background: #ddf4ff; }
|
||||
.vcs-line.h .c { color: #0550ae; }
|
||||
.vcs-line.h .n { background: #ddf4ff; }
|
||||
.vcs-line.meta .c { color: var(--gray); font-style: italic; }
|
||||
.vcs-nodiff { padding: 12px 16px; font-size: 12px; color: var(--gray); }
|
||||
.vcs-note { padding: 8px 16px; font-size: 12px; color: var(--gray); background: var(--drawer-bg); }
|
||||
|
||||
/* 通用 UI 卡片:SSE ui_card → 聊天流可点卡片(动作注册表驱动,见 preview.js) */
|
||||
.ui-card { display: flex; align-items: center; gap: 10px; max-width: 420px; margin: 8px 0; padding: 10px 14px;
|
||||
border: 1px solid var(--border); border-radius: 12px; background: var(--page); cursor: pointer; }
|
||||
|
||||
@@ -553,6 +553,7 @@
|
||||
<script src="/js/files.js"></script>
|
||||
<script src="/js/voice.js"></script>
|
||||
<script src="/js/preview.js"></script>
|
||||
<script src="/js/vcs.js"></script>
|
||||
<script src="/js/files-tree.js"></script>
|
||||
<script src="/js/settings.js"></script>
|
||||
<script>
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
const SVG_REVEAL = '<svg class="ic sm" viewBox="0 0 24 24"><path d="M14 4h6v6"/><path d="M20 4l-9 9"/><path d="M19 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1h5"/></svg>';
|
||||
const SVG_REFRESH = '<svg class="ic sm" viewBox="0 0 24 24"><path d="M20 12a8 8 0 1 1-2.3-5.7"/><path d="M20 4v5h-5"/></svg>';
|
||||
const SVG_TERM = '<svg class="ic sm" viewBox="0 0 24 24"><rect x="3" y="4" width="18" height="16" rx="2"/><path d="M7 9l3 3-3 3"/><path d="M13 15h4"/></svg>';
|
||||
const SVG_VCS = '<svg class="ic sm" viewBox="0 0 24 24"><circle cx="6" cy="5" r="2"/><circle cx="6" cy="19" r="2"/><circle cx="18" cy="7" r="2"/><path d="M6 7v10"/><path d="M18 9v1a4 4 0 0 1-4 4H9"/></svg>';
|
||||
const SVG_EYE = '<svg class="ic sm" viewBox="0 0 24 24"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12z"/><circle cx="12" cy="12" r="2.6"/></svg>';
|
||||
const SVG_DL = '<svg viewBox="0 0 24 24"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>';
|
||||
const SVG_DEL = '<svg viewBox="0 0 24 24"><polyline points="3 6 5 6 21 6"/><path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/><line x1="10" y1="11" x2="10" y2="17"/><line x1="14" y1="11" x2="14" y2="17"/></svg>';
|
||||
@@ -408,6 +409,7 @@
|
||||
'<span class="pv-actions">' +
|
||||
'<button class="icon-btn" type="button" id="tree-hidden" title="显示隐藏文件" aria-label="显示隐藏文件">' + SVG_EYE + '</button>' +
|
||||
'<button class="icon-btn" type="button" id="tree-refresh" title="刷新" aria-label="刷新">' + SVG_REFRESH + '</button>' +
|
||||
'<button class="icon-btn" type="button" id="tree-vcs" title="Git 改动与历史" aria-label="Git 改动与历史">' + SVG_VCS + '</button>' +
|
||||
'<button class="icon-btn hidden" type="button" id="tree-reveal" title="在 Finder 中打开" aria-label="在 Finder 中打开">' + SVG_REVEAL + '</button>' +
|
||||
'<button class="icon-btn hidden" type="button" id="tree-terminal" title="在终端中打开" aria-label="在终端中打开">' + SVG_TERM + '</button>' +
|
||||
'</span>';
|
||||
@@ -441,6 +443,9 @@
|
||||
});
|
||||
head.querySelector('#tree-reveal').addEventListener('click', function () { localAction('reveal', ''); });
|
||||
head.querySelector('#tree-terminal').addEventListener('click', function () { localAction('terminal', ''); });
|
||||
head.querySelector('#tree-vcs').addEventListener('click', function () {
|
||||
if (typeof window.openVcsPane === 'function') window.openVcsPane();
|
||||
});
|
||||
foot.querySelector('#tree-check-all').addEventListener('click', toggleSelAll);
|
||||
foot.querySelector('#tree-bulk-download').addEventListener('click', bulkDownload);
|
||||
foot.querySelector('#tree-bulk-delete').addEventListener('click', bulkDelete);
|
||||
|
||||
@@ -130,6 +130,7 @@ function pvOpenPane() {
|
||||
function pvCloseDoc() {
|
||||
if (!document.body.classList.contains('pv-open')) return;
|
||||
document.body.classList.remove('pv-open', 'pv-max');
|
||||
if (typeof vcsSetMode === 'function') vcsSetMode(false); // 关面板即离开 Git 模式
|
||||
if (typeof pvSetMaxUI === 'function') pvSetMaxUI(false); // 收起时复位放大钮图标(函数声明提升,定义在后亦可)
|
||||
pvClearBody();
|
||||
pvState.name = null; pvState.url = null;
|
||||
@@ -206,6 +207,7 @@ async function pvOpenOffice(name, sizeText) {
|
||||
// ---------- 统一入口:所有文件点击都走这 ----------
|
||||
async function openFilePreview(name, sizeText) {
|
||||
if (!state.sessionId) return;
|
||||
if (typeof vcsSetMode === 'function') vcsSetMode(false); // 点文件即离开 Git 面板(预览/Git 二选一)
|
||||
if (PV_OFFICE.test(name)) { try { await pvOpenOffice(name, sizeText); } catch (e) { toast('预览失败:' + e.message); } return; }
|
||||
let kind = pvKind(name);
|
||||
if (kind === 'pdf' && !navigator.pdfViewerEnabled) kind = 'none'; // WebView 内嵌不了 PDF:给占位卡,不再自动下载
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
// Git 面板(复用 #pv-pane:预览 / Git 二选一,由 body.pv-mode-vcs 切换)。
|
||||
// 三个视图:list(提交历史 + 未提交入口)/ detail(提交详情)/ worktree(未提交改动)。
|
||||
// 后两者共用一个渲染器,区别只在头部有没有 commit 信息。
|
||||
// 不做自动刷新:打开、切视图、点刷新时才请求。
|
||||
// 依赖(全局):state esc toast req pvOpenPane pvHeader pvToolbar pvClearBody pvState pvDesktop openFilePreview
|
||||
'use strict';
|
||||
(function () {
|
||||
const PAGE = 30;
|
||||
// view: list | detail | worktree;cur: 当前详情响应;lastPreview: 进 Git 前预览的文件(切回用)
|
||||
const st = { view: 'list', sid: '', repo: null, commits: [], next: null, cur: null, lastPreview: null, seq: 0 };
|
||||
|
||||
function bodyEl() { return document.getElementById('pv-body'); }
|
||||
function sid() { return (state && state.sessionId) || ''; }
|
||||
|
||||
function api(p) {
|
||||
return req('GET', '/api/sessions/' + encodeURIComponent(sid()) + p);
|
||||
}
|
||||
|
||||
// 仓库路径显示在地址条上(地址条是现成的路径位,不另开头部)
|
||||
function setAddr(repo) {
|
||||
const t = document.getElementById('pv-title');
|
||||
if (!t || !repo) return;
|
||||
t.value = repo.root || '';
|
||||
t.title = (repo.root || '') + (repo.branch ? ' · ' + repo.branch : '');
|
||||
}
|
||||
|
||||
function fmtTime(iso) {
|
||||
const d = new Date(iso);
|
||||
if (isNaN(d.getTime())) return String(iso || '');
|
||||
const p = (n) => (n < 10 ? '0' + n : '' + n);
|
||||
return d.getFullYear() + '-' + p(d.getMonth() + 1) + '-' + p(d.getDate()) + ' ' + p(d.getHours()) + ':' + p(d.getMinutes());
|
||||
}
|
||||
|
||||
const BADGE = { A: '新增', D: '删除', R: '改名', M: '修改', U: '未跟踪' };
|
||||
|
||||
function errBox(text, retry) {
|
||||
bodyEl().innerHTML =
|
||||
'<div class="pv-err"><div class="vcs-err-t"></div>' + (retry ? '<button type="button" class="vcs-retry">重试</button>' : '') + '</div>';
|
||||
bodyEl().querySelector('.vcs-err-t').textContent = text;
|
||||
const b = bodyEl().querySelector('.vcs-retry');
|
||||
if (b) b.addEventListener('click', retry);
|
||||
}
|
||||
|
||||
function reasonText(reason) {
|
||||
if (reason === 'no_git') return '这个环境没有 git 命令(服务器镜像未装 git)。';
|
||||
if (reason === 'bad_rev') return '提交不存在,可能已被改写或清理。';
|
||||
if (reason === 'timeout') return 'git 执行超时,仓库可能过大。';
|
||||
if (reason === 'not_repo') return '当前会话目录不是 git 仓库。';
|
||||
return '读取失败:' + (reason || '未知错误');
|
||||
}
|
||||
|
||||
// ---------- 列表 ----------
|
||||
function renderList() {
|
||||
const parts = [];
|
||||
parts.push('<div class="vcs-list">');
|
||||
if (st.repo) {
|
||||
parts.push('<div class="vcs-repo">' + esc(st.repo.root) + (st.repo.branch ? ' · ' + esc(st.repo.branch) : '') + '</div>');
|
||||
}
|
||||
if (st.worktree && st.worktree.files) {
|
||||
parts.push('<div class="vcs-item wt" data-act="worktree">' +
|
||||
'<div class="t">未提交的改动</div>' +
|
||||
'<div class="m">' + st.worktree.files + ' 个文件有变动</div></div>');
|
||||
}
|
||||
for (const c of st.commits) {
|
||||
parts.push('<div class="vcs-item" data-act="commit" data-id="' + esc(c.id) + '">' +
|
||||
'<div class="t">' + esc(c.subject) + '</div>' +
|
||||
'<div class="m">' + esc(c.author) + ' · ' + esc(fmtTime(c.time)) + ' · ' + esc(c.short) + '</div></div>');
|
||||
}
|
||||
if (!st.commits.length && !(st.worktree && st.worktree.files)) {
|
||||
parts.push('<div class="vcs-empty">还没有提交记录</div>');
|
||||
}
|
||||
if (st.next) parts.push('<button type="button" class="vcs-more" data-act="more">加载更多</button>');
|
||||
parts.push('</div>');
|
||||
bodyEl().innerHTML = parts.join('');
|
||||
}
|
||||
|
||||
// ---------- 详情(提交 / 未提交共用)----------
|
||||
function lineHtml(cls, oldNo, newNo, text) {
|
||||
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>';
|
||||
}
|
||||
|
||||
// 行号自己推:@@ -a,b +c,d @@ 之后的上下文/+/- 分别推进两个计数器
|
||||
function patchHtml(f) {
|
||||
if (f.binary) return '<div class="vcs-nodiff">二进制文件,不显示差异</div>';
|
||||
if (!f.patch) return '<div class="vcs-nodiff">没有可显示的差异</div>';
|
||||
const out = [];
|
||||
const lines = String(f.patch).split('\n');
|
||||
let oldNo = 0, newNo = 0, inHunk = false, rows = 0;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (i === lines.length - 1 && line === '') break; // 末尾换行不算内容行
|
||||
if (line.slice(0, 2) === '@@') {
|
||||
const m = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(line);
|
||||
if (m) { oldNo = Number(m[1]); newNo = Number(m[2]); inHunk = true; }
|
||||
out.push('<div class="vcs-line h"><span class="n"></span><span class="n"></span><span class="c">' + esc(line) + '</span></div>');
|
||||
continue;
|
||||
}
|
||||
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 (++rows > 3000) { out.push('<div class="vcs-nodiff">行数过多,剩余部分已隐藏</div>'); break; }
|
||||
}
|
||||
return out.join('') || '<div class="vcs-nodiff">仅改名或模式变更,没有内容差异</div>';
|
||||
}
|
||||
|
||||
function fileHtml(f, idx, openAll) {
|
||||
const open = openAll ? ' open' : '';
|
||||
return '<div class="vcs-file' + open + '" data-i="' + idx + '">' +
|
||||
'<div class="vcs-fh">' +
|
||||
'<span class="vcs-caret"></span>' +
|
||||
'<span class="vcs-path">' + esc(f.path) + '</span>' +
|
||||
'<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>';
|
||||
}
|
||||
|
||||
function renderDetail(d) {
|
||||
st.cur = d;
|
||||
const openAll = d.files.length <= 3; // 少量文件默认展开,多文件默认折叠
|
||||
const parts = [];
|
||||
parts.push('<div class="vcs-detail">');
|
||||
parts.push('<button type="button" class="vcs-back" data-act="back">返回提交列表</button>');
|
||||
if (d.commit) {
|
||||
parts.push('<div class="vcs-commit">' +
|
||||
'<div class="vcs-subject">' + esc(d.commit.subject) + '</div>' +
|
||||
'<div class="vcs-meta"><span>' + esc(d.commit.author) + '</span><span>' + esc(fmtTime(d.commit.time)) + '</span>' +
|
||||
'<span class="vcs-hash">' + esc(d.commit.short) + '</span>' +
|
||||
(d.commit.parents && d.commit.parents.length > 1 ? '<span>合并提交</span>' : '') + '</div>' +
|
||||
(d.commit.body ? '<pre class="vcs-msg">' + esc(d.commit.body) + '</pre>' : '') +
|
||||
'</div>');
|
||||
} else {
|
||||
parts.push('<div class="vcs-commit"><div class="vcs-subject">未提交的改动</div>' +
|
||||
'<div class="vcs-meta"><span>' + esc(d.repo.branch || 'HEAD') + '</span><span>' + esc(d.repo.root) + '</span></div></div>');
|
||||
}
|
||||
parts.push('<div class="vcs-sum">' + d.stats.files + ' 个文件 · <b class="a">+' + d.stats.adds + '</b> <b class="d">-' + d.stats.dels + '</b></div>');
|
||||
d.files.forEach(function (f, i) { parts.push(fileHtml(f, i, openAll)); });
|
||||
if (!d.files.length) parts.push('<div class="vcs-empty">没有文件变动</div>');
|
||||
parts.push('</div>');
|
||||
bodyEl().innerHTML = parts.join('');
|
||||
}
|
||||
|
||||
// ---------- 数据加载 ----------
|
||||
async function loadList(more) {
|
||||
const seq = ++st.seq;
|
||||
const keepTop = more ? bodyEl().scrollTop : 0; // 加载更多是整体重绘,得自己把滚动位置补回去
|
||||
st.view = 'list';
|
||||
st.sid = sid();
|
||||
if (!more) { st.commits = []; st.next = null; bodyEl().innerHTML = '<div class="pv-loading"></div>'; }
|
||||
const q = '?limit=' + PAGE + (more && st.next ? '&before=' + encodeURIComponent(st.next) : '');
|
||||
let d;
|
||||
try { d = await api('/git/commits' + q); }
|
||||
catch (e) { if (seq === st.seq) errBox('请求失败:' + (e.message || e), function () { loadList(false); }); return; }
|
||||
if (seq !== st.seq) return;
|
||||
if (!d || !d.ok) { errBox(reasonText(d && d.reason), function () { loadList(false); }); return; }
|
||||
st.repo = d.repo;
|
||||
st.worktree = d.worktree;
|
||||
st.commits = more ? st.commits.concat(d.commits) : d.commits;
|
||||
st.next = d.next;
|
||||
setAddr(d.repo);
|
||||
renderList();
|
||||
if (more) bodyEl().scrollTop = keepTop;
|
||||
}
|
||||
|
||||
async function loadDetail(rev) {
|
||||
const seq = ++st.seq;
|
||||
st.view = rev === 'worktree' ? 'worktree' : 'detail';
|
||||
st.sid = sid();
|
||||
bodyEl().innerHTML = '<div class="pv-loading"></div>';
|
||||
let d;
|
||||
try { d = await api('/git/commit/' + encodeURIComponent(rev)); }
|
||||
catch (e) {
|
||||
if (seq === st.seq) errBox('请求失败:' + (e.message || e), function () { loadDetail(rev); });
|
||||
return;
|
||||
}
|
||||
if (seq !== st.seq) return;
|
||||
if (!d || !d.ok) { errBox(reasonText(d && d.reason), function () { loadDetail(rev); }); return; }
|
||||
renderDetail(d);
|
||||
}
|
||||
|
||||
function refresh() {
|
||||
if (st.view === 'list') return loadList(false);
|
||||
if (st.view === 'worktree') return loadDetail('worktree');
|
||||
if (st.cur && st.cur.commit) return loadDetail(st.cur.commit.id);
|
||||
return loadList(false);
|
||||
}
|
||||
|
||||
// ---------- 面板开关 ----------
|
||||
function syncBtn() {
|
||||
const b = document.getElementById('tree-vcs');
|
||||
if (b) b.classList.toggle('active', document.body.classList.contains('pv-mode-vcs'));
|
||||
}
|
||||
|
||||
function setMode(on) {
|
||||
document.body.classList.toggle('pv-mode-vcs', !!on);
|
||||
syncBtn();
|
||||
}
|
||||
|
||||
function openPane() {
|
||||
if (!sid()) { toast('请先打开一个会话'); return; }
|
||||
if (typeof pvDesktop === 'function' && !pvDesktop()) { toast('Git 面板仅在桌面端可用'); return; }
|
||||
const on = document.body.classList.contains('pv-mode-vcs');
|
||||
if (on && st.sid === sid()) { // 再点一次:有上次预览就切回预览,否则关面板
|
||||
setMode(false);
|
||||
if (st.lastPreview) { openFilePreview(st.lastPreview); return; }
|
||||
document.getElementById('pv-close').click();
|
||||
return;
|
||||
}
|
||||
if (!on) st.lastPreview = (typeof pvState === 'object' && pvState && pvState.name) || null;
|
||||
if (typeof pvOpenPane === 'function') pvOpenPane();
|
||||
setMode(true);
|
||||
pvClearBody();
|
||||
pvHeader(null); // 清掉预览的文件名与下载钮;地址条下面再写仓库路径
|
||||
pvToolbar({ refresh: true, popup: false, reload: refresh });
|
||||
loadList(false);
|
||||
}
|
||||
|
||||
// 事件委托:pv-body 的内容每次整体重建,逐元素绑定没必要
|
||||
document.getElementById('pv-body').addEventListener('click', function (e) {
|
||||
if (!document.body.classList.contains('pv-mode-vcs')) return;
|
||||
const it = e.target.closest('.vcs-item');
|
||||
if (it) {
|
||||
if (it.dataset.act === 'worktree') loadDetail('worktree');
|
||||
else if (it.dataset.act === 'commit') loadDetail(it.dataset.id);
|
||||
return;
|
||||
}
|
||||
const more = e.target.closest('.vcs-more');
|
||||
if (more) { loadList(true); return; }
|
||||
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; }
|
||||
});
|
||||
|
||||
window.openVcsPane = openPane;
|
||||
window.vcsSetMode = setMode;
|
||||
window.vcsRefresh = refresh;
|
||||
})();
|
||||
Reference in New Issue
Block a user