agent-loft 0.2.90 → 0.2.93
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 +9 -0
- package/package.json +23 -1
- package/src/channels/web/api.ts +14 -66
- package/src/channels/web/file-serve.ts +72 -0
- package/src/channels/web/workspace-api.ts +249 -0
- package/src/core/db.ts +19 -0
- package/src/core/footers.ts +26 -10
- package/src/core/git.ts +782 -0
- package/src/core/hub.ts +61 -5
- package/src/core/reply-files.ts +2 -1
- package/src/core/turn-changes.ts +179 -0
- package/src/core/types.ts +13 -0
- package/src/core/users.ts +15 -2
- package/src/core/workspace-files.ts +315 -0
- package/web/css/experts.css +6 -3
- package/web/css/settings.css +31 -0
- package/web/css/workspace.css +1064 -0
- package/web/index.html +1 -0
- package/web/lib/api.js +1 -0
- package/web/lib/csv.js +53 -0
- package/web/lib/diff-parse.js +85 -0
- package/web/lib/dom.js +5 -0
- package/web/lib/eol.js +15 -0
- package/web/settings/users.js +23 -2
- package/web/vendor-built/LICENSE.codemirror +21 -0
- package/web/vendor-built/codemirror.js +41 -0
- package/web/views/diff-view.js +91 -0
- package/web/views/file-editor.js +146 -0
- package/web/views/file-preview.js +83 -6
- package/web/views/messages.js +55 -2
- package/web/views/sidebar-gestures.js +1 -1
- package/web/views/topbar.js +3 -0
- package/web/views/workspace-entry.js +130 -0
- package/web/views/workspace.js +1346 -0
|
@@ -0,0 +1,1346 @@
|
|
|
1
|
+
// 工作区面板:对话右边的一栏(手机上全屏),看和改工作目录里的文件,看 Git 改动。
|
|
2
|
+
//
|
|
3
|
+
// 三个页签:「文件」是一棵按需展开的树,「改动」是 git status 的结果(能暂存、丢弃、提交、切分支),
|
|
4
|
+
// 「历史」是提交记录。点进去都是详情页(预览 / 编辑 / diff),有返回键:面板不宽,树和内容放不下并排。
|
|
5
|
+
//
|
|
6
|
+
// 编辑:保存时带着读出来时的 hash,文件被 agent 或别人改过就不覆盖,让用户选(见 saveEdit)。
|
|
7
|
+
// 没保存的内容随时存一份在本机(见「草稿」),换会话、刷新页面都不丢,再打开这个文件时恢复。
|
|
8
|
+
// 接口见 channels/web/workspace-api.ts;分支标记和状态刷新在 views/workspace-entry.js。
|
|
9
|
+
import { fill, h, icon, starsLoader, toast } from "../lib/dom.js";
|
|
10
|
+
import { api } from "../lib/api.js";
|
|
11
|
+
import { closeMenu, confirmDialog, modal, placePopover, showMenu, showPopover } from "../lib/overlay.js";
|
|
12
|
+
import { relTime } from "../lib/time.js";
|
|
13
|
+
import { S, ui } from "../state.js";
|
|
14
|
+
import { editableName, fmtSize, renderFileView, textPreview } from "./file-preview.js";
|
|
15
|
+
import { renderDiff } from "./diff-view.js";
|
|
16
|
+
import { createEditor } from "./file-editor.js";
|
|
17
|
+
import { git, refreshGit, rememberOpen } from "./workspace-entry.js";
|
|
18
|
+
|
|
19
|
+
const WIDTH_KEY = "aim.wsW";
|
|
20
|
+
const MODE_KEY = "aim.wsDiffMode";
|
|
21
|
+
const MIN_WIDTH = 300;
|
|
22
|
+
/** 面板比这窄就不给并排了 */
|
|
23
|
+
const SPLIT_MIN_WIDTH = 640;
|
|
24
|
+
|
|
25
|
+
const P = {
|
|
26
|
+
open: false,
|
|
27
|
+
tab: "files",
|
|
28
|
+
/** 这份状态是哪条会话、哪个目录的;换了就清空 */
|
|
29
|
+
id: null,
|
|
30
|
+
/** 相对路径 → 目录内容(已经加载的) */
|
|
31
|
+
tree: new Map(),
|
|
32
|
+
expanded: new Set([""]),
|
|
33
|
+
treeError: null,
|
|
34
|
+
/** 详情页:null / { kind: "file", path, from } / { kind: "edit", path, from } / { kind: "turn", changes, turnState, from } / { kind: "commit", commit, files, from } / { kind: "diff", path, staged, oldPath, turn, commit, from } */
|
|
35
|
+
detail: null,
|
|
36
|
+
/** 正在编辑:{ key, path, name, hash, saved, editor, host, previewEl, saving, loadError } */
|
|
37
|
+
edit: null,
|
|
38
|
+
/** 提交历史:{ commits, more, error },切到页签时才拉 */
|
|
39
|
+
log: null,
|
|
40
|
+
/** 详情页的内容:diff 接口的结果 */
|
|
41
|
+
diff: null,
|
|
42
|
+
diffError: null,
|
|
43
|
+
diffMode: localStorage.getItem(MODE_KEY) === "split" ? "split" : "unified",
|
|
44
|
+
loadToken: 0,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const base = () => `/api/conversations/${encodeURIComponent(S.current)}`;
|
|
48
|
+
const fileUrl = (rel) => `${base()}/workspace-file?path=${encodeURIComponent(rel)}`;
|
|
49
|
+
const pageUrl = (rel) => `${base()}/workspace-html?path=${encodeURIComponent(rel)}`;
|
|
50
|
+
const usable = () => !!S.current && !S.view;
|
|
51
|
+
const conversationId = () => (usable() ? `${S.current}\n${S.convs.get(S.current)?.cwd ?? ""}` : null);
|
|
52
|
+
|
|
53
|
+
// ---------- 面板 ----------
|
|
54
|
+
|
|
55
|
+
function ensurePanel() {
|
|
56
|
+
if (ui.wsPanel) return;
|
|
57
|
+
ui.wsTabs = h("div", { class: "ws-tabs", role: "tablist" });
|
|
58
|
+
ui.wsBody = h("div", { class: "ws-body" });
|
|
59
|
+
ui.wsPanel = h(
|
|
60
|
+
"aside",
|
|
61
|
+
{ class: "ws-panel", hidden: true, "aria-label": "工作区" },
|
|
62
|
+
h("div", { class: "ws-resizer", title: "拖动调整宽度,双击恢复默认", onpointerdown: startResize, ondblclick: () => setWidth(null) }),
|
|
63
|
+
h(
|
|
64
|
+
"div",
|
|
65
|
+
{ class: "ws-head" },
|
|
66
|
+
ui.wsTabs,
|
|
67
|
+
h("span", { class: "grow" }),
|
|
68
|
+
h("button", { class: "icon-btn", title: "刷新", "aria-label": "刷新", onclick: () => void reload() }, icon("refresh")),
|
|
69
|
+
h("button", { class: "icon-btn", title: "关闭", "aria-label": "关闭工作区", onclick: () => hidePanel() }, icon("x")),
|
|
70
|
+
),
|
|
71
|
+
ui.wsBody,
|
|
72
|
+
);
|
|
73
|
+
ui.layout.append(ui.wsPanel);
|
|
74
|
+
bindPanelDrop(ui.wsPanel);
|
|
75
|
+
setWidth(Number(localStorage.getItem(WIDTH_KEY)) || null);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function setWidth(px) {
|
|
79
|
+
if (px) {
|
|
80
|
+
ui.wsPanel.style.setProperty("--ws-w", `${Math.round(px)}px`);
|
|
81
|
+
localStorage.setItem(WIDTH_KEY, String(Math.round(px)));
|
|
82
|
+
} else {
|
|
83
|
+
ui.wsPanel.style.removeProperty("--ws-w");
|
|
84
|
+
localStorage.removeItem(WIDTH_KEY);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function startResize(e) {
|
|
89
|
+
if (e.button !== 0) return;
|
|
90
|
+
e.preventDefault();
|
|
91
|
+
const handle = e.currentTarget;
|
|
92
|
+
const right = ui.wsPanel.getBoundingClientRect().right;
|
|
93
|
+
handle.setPointerCapture(e.pointerId);
|
|
94
|
+
document.body.classList.add("side-resizing");
|
|
95
|
+
const move = (ev) => setWidth(Math.max(MIN_WIDTH, right - ev.clientX));
|
|
96
|
+
const up = () => {
|
|
97
|
+
handle.removeEventListener("pointermove", move);
|
|
98
|
+
handle.removeEventListener("pointerup", up);
|
|
99
|
+
handle.removeEventListener("pointercancel", up);
|
|
100
|
+
document.body.classList.remove("side-resizing");
|
|
101
|
+
// 宽窄变了,并排还能不能用要重新判断
|
|
102
|
+
if (P.detail?.kind === "diff") renderBody();
|
|
103
|
+
};
|
|
104
|
+
handle.addEventListener("pointermove", move);
|
|
105
|
+
handle.addEventListener("pointerup", up);
|
|
106
|
+
handle.addEventListener("pointercancel", up);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export async function showPanel(tab) {
|
|
110
|
+
ensurePanel();
|
|
111
|
+
P.open = true;
|
|
112
|
+
rememberOpen(true);
|
|
113
|
+
const fresh = syncPanel();
|
|
114
|
+
// 换页签会离开编辑页,没保存的要问一下。留下的话页签就不换了
|
|
115
|
+
if (tab && tab !== P.tab && (await leaveEdit())) {
|
|
116
|
+
P.tab = tab;
|
|
117
|
+
P.detail = null;
|
|
118
|
+
}
|
|
119
|
+
render();
|
|
120
|
+
if (!fresh && !P.tree.has("")) void loadTree("");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function hidePanel() {
|
|
124
|
+
P.open = false;
|
|
125
|
+
rememberOpen(false);
|
|
126
|
+
syncPanel();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function togglePanel() {
|
|
130
|
+
if (P.open && usable()) hidePanel();
|
|
131
|
+
else void showPanel();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** 打开工作目录里的一个文件(相对路径) */
|
|
135
|
+
export async function openFile(rel) {
|
|
136
|
+
await showPanel();
|
|
137
|
+
if (!(await leaveEdit())) return;
|
|
138
|
+
P.tab = "files";
|
|
139
|
+
renderTabs();
|
|
140
|
+
openDetail({ kind: "file", path: rel, from: null });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 打开某一轮的改动。file:直接看这个文件的 diff,返回时是这一轮的文件列表 */
|
|
144
|
+
export async function openTurn(changes, file) {
|
|
145
|
+
await showPanel();
|
|
146
|
+
if (!(await leaveEdit())) return;
|
|
147
|
+
P.tab = "changes";
|
|
148
|
+
renderTabs();
|
|
149
|
+
const list = { kind: "turn", changes, from: null };
|
|
150
|
+
if (file) openDetail(turnDiffDetail(changes, file, list));
|
|
151
|
+
else openDetail(list);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** 换了会话、切到别的页面:内容清掉重新来,面板跟着显示 / 藏起来。重新加载了返回 true */
|
|
155
|
+
export function syncPanel() {
|
|
156
|
+
if (!ui.wsPanel) return false;
|
|
157
|
+
ui.wsPanel.hidden = !P.open || !usable();
|
|
158
|
+
const id = conversationId();
|
|
159
|
+
if (id && id !== P.id) {
|
|
160
|
+
if (P.edit) {
|
|
161
|
+
if (editDirty()) {
|
|
162
|
+
flushDraft();
|
|
163
|
+
toast("没保存的修改已留在这台设备上,回到这条对话打开文件时会恢复");
|
|
164
|
+
}
|
|
165
|
+
dropEditor();
|
|
166
|
+
}
|
|
167
|
+
P.id = id;
|
|
168
|
+
P.tree = new Map();
|
|
169
|
+
P.expanded = new Set([""]);
|
|
170
|
+
P.treeError = null;
|
|
171
|
+
P.detail = null;
|
|
172
|
+
P.diff = null;
|
|
173
|
+
P.log = null;
|
|
174
|
+
// 写给上一个仓库的提交说明,不能留着提交到这一个
|
|
175
|
+
if (commitUi.message) commitUi.message.value = "";
|
|
176
|
+
if (P.open) {
|
|
177
|
+
void loadTree("");
|
|
178
|
+
render();
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** 手动刷新:文件树、Git 状态,打开着的详情也重新拿 */
|
|
186
|
+
async function reload() {
|
|
187
|
+
P.log = null;
|
|
188
|
+
await Promise.all([reloadTree(), refreshGit()]);
|
|
189
|
+
if (P.detail?.kind === "file") render();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function reloadTree() {
|
|
193
|
+
await Promise.all([...P.expanded].map((dir) => loadTree(dir)));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// 一轮结束、Git 状态刷新了:跟着更新看得到的东西
|
|
197
|
+
document.addEventListener("aim:git", () => {
|
|
198
|
+
if (!P.open || !usable()) return;
|
|
199
|
+
void reloadTree();
|
|
200
|
+
if (P.tab === "log" && !git.status?.repo) P.tab = "changes";
|
|
201
|
+
renderTabs();
|
|
202
|
+
// 某一轮、某个提交的 diff 是定死的,不用重新拿
|
|
203
|
+
if (P.detail?.kind === "diff" && !P.detail.turn && !P.detail.commit) void loadDiff();
|
|
204
|
+
// 撤销状态看的是工作区现在的样子
|
|
205
|
+
else if (P.detail?.kind === "turn") void loadTurnState(P.detail);
|
|
206
|
+
// 正在看的文件不自动重画:视频、网页会从头开始。要看新内容点刷新
|
|
207
|
+
else if (!P.detail) renderBody();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// ---------- 画 ----------
|
|
211
|
+
|
|
212
|
+
function renderTabs() {
|
|
213
|
+
const count = git.status?.repo ? git.status.files.length : 0;
|
|
214
|
+
fill(ui.wsTabs, tabButton("files", "文件"), tabButton("changes", "改动", count), git.status?.repo ? tabButton("log", "历史") : null);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function render() {
|
|
218
|
+
if (!ui.wsPanel || ui.wsPanel.hidden) return;
|
|
219
|
+
renderTabs();
|
|
220
|
+
renderBody();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function tabButton(id, label, count = 0) {
|
|
224
|
+
return h(
|
|
225
|
+
"button",
|
|
226
|
+
{
|
|
227
|
+
class: `ws-tab${P.tab === id ? " on" : ""}`,
|
|
228
|
+
role: "tab",
|
|
229
|
+
"aria-selected": P.tab === id ? "true" : "false",
|
|
230
|
+
onclick: async () => {
|
|
231
|
+
if (!(await leaveEdit())) return;
|
|
232
|
+
P.tab = id;
|
|
233
|
+
P.detail = null;
|
|
234
|
+
render();
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
label,
|
|
238
|
+
count ? h("span", { class: "ws-tab-count" }, count) : null,
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function renderBody() {
|
|
243
|
+
if (P.detail) return renderDetail();
|
|
244
|
+
if (P.tab === "files") return renderTree();
|
|
245
|
+
if (P.tab === "log") return renderLog();
|
|
246
|
+
renderChanges();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ---------- 小弹窗 ----------
|
|
250
|
+
|
|
251
|
+
/** 输入名字的弹窗。submit 抛错就提示、弹窗留着;selectStem:选中时不带后缀,改名时直接打字不会把后缀改掉 */
|
|
252
|
+
function promptName({ title, value = "", confirmText = "确定", selectStem = false, submit }) {
|
|
253
|
+
const input = h("input", { class: "rename-input", value, maxlength: "255", spellcheck: "false", autocapitalize: "off", autocomplete: "off" });
|
|
254
|
+
const ok = h("button", { class: "btn primary" }, confirmText);
|
|
255
|
+
const go = async () => {
|
|
256
|
+
const name = input.value.trim();
|
|
257
|
+
if (!name) return input.focus();
|
|
258
|
+
ok.disabled = true;
|
|
259
|
+
try {
|
|
260
|
+
await submit(name);
|
|
261
|
+
close();
|
|
262
|
+
} catch (err) {
|
|
263
|
+
ok.disabled = false;
|
|
264
|
+
toast(err.message);
|
|
265
|
+
input.focus();
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
ok.onclick = () => void go();
|
|
269
|
+
input.addEventListener("keydown", (e) => {
|
|
270
|
+
if (e.key === "Enter" && !e.isComposing) {
|
|
271
|
+
e.preventDefault();
|
|
272
|
+
void go();
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
const close = modal({
|
|
276
|
+
title,
|
|
277
|
+
body: h("div", { class: "modal-body" }, input),
|
|
278
|
+
foot: h("div", { class: "modal-foot" }, h("button", { class: "btn ghost", onclick: () => close() }, "取消"), ok),
|
|
279
|
+
});
|
|
280
|
+
input.focus();
|
|
281
|
+
const dot = value.lastIndexOf(".");
|
|
282
|
+
input.setSelectionRange(0, selectStem && dot > 0 ? dot : value.length);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const loading = () => h("div", { class: "ws-empty" }, starsLoader());
|
|
286
|
+
const emptyNote = (...text) => h("div", { class: "ws-empty" }, ...text);
|
|
287
|
+
|
|
288
|
+
// ---------- 文件树 ----------
|
|
289
|
+
|
|
290
|
+
async function loadTree(rel) {
|
|
291
|
+
const id = P.id;
|
|
292
|
+
try {
|
|
293
|
+
const listing = await api(`${base()}/workspace-tree?path=${encodeURIComponent(rel)}`);
|
|
294
|
+
if (id !== P.id) return;
|
|
295
|
+
P.tree.set(rel, listing);
|
|
296
|
+
if (!rel) P.treeError = null;
|
|
297
|
+
} catch (err) {
|
|
298
|
+
if (id !== P.id) return;
|
|
299
|
+
P.tree.delete(rel);
|
|
300
|
+
// 目录没了:不再当它展开着
|
|
301
|
+
if (rel) P.expanded.delete(rel);
|
|
302
|
+
else P.treeError = err.message;
|
|
303
|
+
}
|
|
304
|
+
if (!P.detail && P.tab === "files") renderTree();
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function toggleDir(rel) {
|
|
308
|
+
if (P.expanded.has(rel)) P.expanded.delete(rel);
|
|
309
|
+
else {
|
|
310
|
+
P.expanded.add(rel);
|
|
311
|
+
if (!P.tree.has(rel)) void loadTree(rel);
|
|
312
|
+
}
|
|
313
|
+
renderTree();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const parentOf = (rel) => (rel.includes("/") ? rel.slice(0, rel.lastIndexOf("/")) : "");
|
|
317
|
+
|
|
318
|
+
/** 改了文件之后:这一层重新列出来,Git 状态也变了(刷新 Git 时整棵树都会跟着对一遍) */
|
|
319
|
+
async function afterChange(dir) {
|
|
320
|
+
await loadTree(dir);
|
|
321
|
+
void refreshGit();
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function newEntry(dir, type) {
|
|
325
|
+
promptName({
|
|
326
|
+
title: type === "dir" ? "新建文件夹" : "新建文件",
|
|
327
|
+
confirmText: "新建",
|
|
328
|
+
submit: async (name) => {
|
|
329
|
+
const created = await api(`${base()}/workspace-new`, { method: "POST", body: { dir, name, type } });
|
|
330
|
+
if (dir) P.expanded.add(dir);
|
|
331
|
+
await afterChange(dir);
|
|
332
|
+
if (type === "dir") P.expanded.add(created.path);
|
|
333
|
+
else openEditor(created.path, null);
|
|
334
|
+
},
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function renameEntry(entry) {
|
|
339
|
+
promptName({
|
|
340
|
+
title: "重命名",
|
|
341
|
+
value: entry.name,
|
|
342
|
+
confirmText: "重命名",
|
|
343
|
+
selectStem: entry.type !== "dir",
|
|
344
|
+
submit: async (name) => {
|
|
345
|
+
if (name === entry.name) return;
|
|
346
|
+
const renamed = await api(`${base()}/workspace-rename`, { method: "POST", body: { path: entry.path, name } });
|
|
347
|
+
// 展开着的目录跟着换名字
|
|
348
|
+
for (const dir of [...P.expanded]) {
|
|
349
|
+
if (dir === entry.path || dir.startsWith(`${entry.path}/`)) {
|
|
350
|
+
P.expanded.delete(dir);
|
|
351
|
+
P.tree.delete(dir);
|
|
352
|
+
P.expanded.add(renamed.path + dir.slice(entry.path.length));
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
await afterChange(parentOf(entry.path));
|
|
356
|
+
},
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
async function deleteEntry(entry) {
|
|
361
|
+
const dir = entry.type === "dir" && !entry.link;
|
|
362
|
+
const sure = await confirmDialog({
|
|
363
|
+
title: `删除「${entry.name}」?`,
|
|
364
|
+
message: dir
|
|
365
|
+
? "文件夹里的所有内容都会被删除,包括面板里看不到的隐藏文件、敏感文件和 .git 目录。删除后不能恢复。"
|
|
366
|
+
: entry.link
|
|
367
|
+
? "只删除这个链接,它指向的内容不受影响。"
|
|
368
|
+
: "删除后不能恢复。被 Git 跟踪的文件可以用 Git 找回最后一次提交的版本。",
|
|
369
|
+
confirmText: "删除",
|
|
370
|
+
danger: true,
|
|
371
|
+
});
|
|
372
|
+
if (!sure) return;
|
|
373
|
+
try {
|
|
374
|
+
await api(`${base()}/workspace-delete`, { method: "POST", body: { path: entry.path } });
|
|
375
|
+
} catch (err) {
|
|
376
|
+
return toast(err.message);
|
|
377
|
+
}
|
|
378
|
+
for (const open of [...P.expanded]) {
|
|
379
|
+
if (open === entry.path || open.startsWith(`${entry.path}/`)) {
|
|
380
|
+
P.expanded.delete(open);
|
|
381
|
+
P.tree.delete(open);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
await afterChange(parentOf(entry.path));
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function pickFiles(dir) {
|
|
388
|
+
const input = h("input", { type: "file", multiple: true });
|
|
389
|
+
input.onchange = () => void uploadFiles(dir, [...input.files]);
|
|
390
|
+
input.click();
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async function uploadFiles(dir, files) {
|
|
394
|
+
let done = 0;
|
|
395
|
+
for (const file of files) {
|
|
396
|
+
const url = (overwrite) => `${base()}/workspace-upload?dir=${encodeURIComponent(dir)}&name=${encodeURIComponent(file.name)}${overwrite ? "&overwrite=1" : ""}`;
|
|
397
|
+
try {
|
|
398
|
+
await api(url(false), { method: "POST", body: file });
|
|
399
|
+
done++;
|
|
400
|
+
} catch (err) {
|
|
401
|
+
if (err.data?.conflict !== "exists") {
|
|
402
|
+
toast(`「${file.name}」上传失败:${err.message}`);
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
const sure = await confirmDialog({ title: `「${file.name}」已经存在,要覆盖吗?`, message: "原来的文件会被替换,不能恢复。", confirmText: "覆盖", danger: true });
|
|
406
|
+
if (!sure) continue;
|
|
407
|
+
try {
|
|
408
|
+
await api(url(true), { method: "POST", body: file });
|
|
409
|
+
done++;
|
|
410
|
+
} catch (again) {
|
|
411
|
+
toast(`「${file.name}」上传失败:${again.message}`);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
if (done) toast(`已上传 ${done} 个文件`);
|
|
416
|
+
if (dir) P.expanded.add(dir);
|
|
417
|
+
await afterChange(dir);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function entryMenu(entry, at) {
|
|
421
|
+
const items = [];
|
|
422
|
+
if (entry.type === "dir") {
|
|
423
|
+
items.push(
|
|
424
|
+
{ label: "新建文件", onclick: () => newEntry(entry.path, "file") },
|
|
425
|
+
{ label: "新建文件夹", onclick: () => newEntry(entry.path, "dir") },
|
|
426
|
+
{ label: "上传到这里", onclick: () => pickFiles(entry.path) },
|
|
427
|
+
);
|
|
428
|
+
} else {
|
|
429
|
+
if (editableName(entry.name)) items.push({ label: "编辑", onclick: () => openEditor(entry.path, null) });
|
|
430
|
+
items.push({ label: "下载", onclick: () => h("a", { href: `${fileUrl(entry.path)}&download=1`, download: entry.name }).click() });
|
|
431
|
+
}
|
|
432
|
+
items.push("sep", { label: "重命名", onclick: () => renameEntry(entry) }, { label: "删除", danger: true, onclick: () => void deleteEntry(entry) });
|
|
433
|
+
showMenu(at, items);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function treeRows(rel, depth) {
|
|
437
|
+
const listing = P.tree.get(rel);
|
|
438
|
+
if (!listing) return [h("div", { class: "ws-row ws-row-note", style: { paddingLeft: `${12 + depth * 16}px` } }, "加载中…")];
|
|
439
|
+
const rows = [];
|
|
440
|
+
for (const entry of listing.entries) {
|
|
441
|
+
const dir = entry.type === "dir";
|
|
442
|
+
const open = dir && P.expanded.has(entry.path);
|
|
443
|
+
rows.push(
|
|
444
|
+
h(
|
|
445
|
+
"div",
|
|
446
|
+
{
|
|
447
|
+
class: "ws-entry",
|
|
448
|
+
...(dir ? { "data-dir": entry.path } : {}),
|
|
449
|
+
oncontextmenu: (e) => {
|
|
450
|
+
e.preventDefault();
|
|
451
|
+
entryMenu(entry, { x: e.clientX, y: e.clientY });
|
|
452
|
+
},
|
|
453
|
+
},
|
|
454
|
+
h(
|
|
455
|
+
"button",
|
|
456
|
+
{
|
|
457
|
+
class: `ws-row${entry.name.startsWith(".") ? " faint" : ""}`,
|
|
458
|
+
style: { paddingLeft: `${12 + depth * 16}px` },
|
|
459
|
+
title: entry.path,
|
|
460
|
+
...(dir ? { "aria-expanded": open ? "true" : "false" } : {}),
|
|
461
|
+
onclick: () => (dir ? toggleDir(entry.path) : openDetail({ kind: "file", path: entry.path, from: null })),
|
|
462
|
+
},
|
|
463
|
+
h("span", { class: "ws-icon" }, icon(dir ? (open ? "folderOpen" : "folder") : "file")),
|
|
464
|
+
h("span", { class: "ws-name" }, entry.name),
|
|
465
|
+
entry.link ? h("span", { class: "ws-tag" }, "链接") : null,
|
|
466
|
+
dir ? null : h("span", { class: "ws-size" }, fmtSize(entry.size)),
|
|
467
|
+
),
|
|
468
|
+
h("button", { class: "icon-btn ws-entry-more", title: "更多操作", "aria-label": "更多操作", onclick: (e) => entryMenu(entry, e.currentTarget) }, icon("more")),
|
|
469
|
+
),
|
|
470
|
+
);
|
|
471
|
+
if (open) rows.push(treeRows(entry.path, depth + 1));
|
|
472
|
+
}
|
|
473
|
+
if (listing.truncated) rows.push(h("div", { class: "ws-row ws-row-note", style: { paddingLeft: `${12 + depth * 16}px` } }, "文件太多,只列了前一部分"));
|
|
474
|
+
if (!listing.entries.length && depth) rows.push(h("div", { class: "ws-row ws-row-note", style: { paddingLeft: `${12 + depth * 16}px` } }, "空目录"));
|
|
475
|
+
return rows;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function treeToolbar() {
|
|
479
|
+
const tool = (label, onclick) => h("button", { class: "btn ghost", onclick }, label);
|
|
480
|
+
return h("div", { class: "ws-toolbar" }, tool("新建文件", () => newEntry("", "file")), tool("新建文件夹", () => newEntry("", "dir")), tool("上传", () => pickFiles("")));
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function renderTree() {
|
|
484
|
+
if (P.treeError) return fill(ui.wsBody, emptyNote(P.treeError));
|
|
485
|
+
if (!P.tree.has("")) return fill(ui.wsBody, loading());
|
|
486
|
+
const scroller = P.tree.get("").entries.length ? h("div", { class: "ws-list" }, treeRows("", 0)) : emptyNote("这个目录是空的");
|
|
487
|
+
const old = ui.wsBody.querySelector(".ws-list");
|
|
488
|
+
const top = old?.scrollTop ?? 0;
|
|
489
|
+
fill(ui.wsBody, treeToolbar(), scroller);
|
|
490
|
+
scroller.scrollTop = top;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// 拖文件到面板上:放到哪个目录上就传到哪,其余的传到工作目录
|
|
494
|
+
function bindPanelDrop(el) {
|
|
495
|
+
const hasFiles = (e) => e.dataTransfer?.types.includes("Files");
|
|
496
|
+
// 不是能放的地方也要拦住:不拦的话,浏览器会直接打开这个文件,把整个页面换掉
|
|
497
|
+
const canDrop = () => P.tab === "files" && !P.detail;
|
|
498
|
+
el.addEventListener("dragover", (e) => {
|
|
499
|
+
if (!hasFiles(e)) return;
|
|
500
|
+
e.preventDefault();
|
|
501
|
+
e.dataTransfer.dropEffect = canDrop() ? "copy" : "none";
|
|
502
|
+
el.classList.toggle("ws-dropping", canDrop());
|
|
503
|
+
});
|
|
504
|
+
el.addEventListener("dragleave", (e) => {
|
|
505
|
+
if (!el.contains(e.relatedTarget)) el.classList.remove("ws-dropping");
|
|
506
|
+
});
|
|
507
|
+
el.addEventListener("drop", (e) => {
|
|
508
|
+
el.classList.remove("ws-dropping");
|
|
509
|
+
if (!hasFiles(e)) return;
|
|
510
|
+
e.preventDefault();
|
|
511
|
+
if (!canDrop()) return;
|
|
512
|
+
const dir = e.target.closest?.("[data-dir]")?.dataset.dir ?? "";
|
|
513
|
+
void uploadFiles(dir, [...e.dataTransfer.files]);
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// ---------- 改动 ----------
|
|
518
|
+
|
|
519
|
+
const LETTER = { modified: "M", added: "A", deleted: "D", renamed: "R", copied: "C", typechange: "T", untracked: "U", conflicted: "!" };
|
|
520
|
+
const STATE_LABEL = { modified: "已修改", added: "新增", deleted: "已删除", renamed: "重命名", copied: "复制", typechange: "类型变更", untracked: "未跟踪", conflicted: "有冲突" };
|
|
521
|
+
|
|
522
|
+
const gitPost = (sub, body) => api(`${base()}/${sub}`, { method: "POST", body });
|
|
523
|
+
|
|
524
|
+
/** Git 操作的报错可能很长,多显示一会 */
|
|
525
|
+
const gitFail = (err) => toast(err.message, 8000);
|
|
526
|
+
|
|
527
|
+
/** 做一个 Git 操作,做完刷新。成功返回结果,失败提示并返回 undefined */
|
|
528
|
+
async function gitAct(sub, body) {
|
|
529
|
+
try {
|
|
530
|
+
const result = await gitPost(sub, body);
|
|
531
|
+
P.log = null;
|
|
532
|
+
await refreshGit();
|
|
533
|
+
return result;
|
|
534
|
+
} catch (err) {
|
|
535
|
+
gitFail(err);
|
|
536
|
+
return undefined;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function discard(file) {
|
|
541
|
+
const untracked = file.unstaged === "untracked";
|
|
542
|
+
void confirmDialog({
|
|
543
|
+
title: untracked ? `删除「${file.path}」?` : `丢弃「${file.path}」的改动?`,
|
|
544
|
+
message: untracked ? "这个文件还没有被 Git 跟踪,删除后找不回来。" : "没有暂存的改动会全部丢掉,恢复成暂存区的样子,找不回来。",
|
|
545
|
+
confirmText: untracked ? "删除" : "丢弃",
|
|
546
|
+
danger: true,
|
|
547
|
+
}).then((sure) => {
|
|
548
|
+
if (sure) void gitAct("git-discard", { paths: [file.path] });
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** 文件行右边的按钮。敏感文件不能暂存、丢弃,但可以取消暂存:那是把它从下次提交里拿掉 */
|
|
553
|
+
function rowActions(file, staged) {
|
|
554
|
+
if (file.sensitive && !staged) return null;
|
|
555
|
+
const act = (name, title, onclick) =>
|
|
556
|
+
h(
|
|
557
|
+
"button",
|
|
558
|
+
{
|
|
559
|
+
class: "icon-btn ws-row-act",
|
|
560
|
+
title,
|
|
561
|
+
"aria-label": title,
|
|
562
|
+
onclick: (e) => {
|
|
563
|
+
e.stopPropagation();
|
|
564
|
+
onclick();
|
|
565
|
+
},
|
|
566
|
+
},
|
|
567
|
+
icon(name),
|
|
568
|
+
);
|
|
569
|
+
const conflicted = file.unstaged === "conflicted";
|
|
570
|
+
return h(
|
|
571
|
+
"div",
|
|
572
|
+
{ class: "ws-row-actions" },
|
|
573
|
+
staged
|
|
574
|
+
? act("minus", "取消暂存", () => void gitAct("git-unstage", { paths: [file.path] }))
|
|
575
|
+
: [
|
|
576
|
+
conflicted ? null : act("undo", file.unstaged === "untracked" ? "删除" : "丢弃改动", () => discard(file)),
|
|
577
|
+
act("plus", conflicted ? "标记为已解决并暂存" : "暂存", () => void gitAct("git-stage", { paths: [file.path] })),
|
|
578
|
+
],
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function changeRow(file, state, staged, onclick, actions) {
|
|
583
|
+
const slash = file.path.lastIndexOf("/");
|
|
584
|
+
const name = file.path.slice(slash + 1);
|
|
585
|
+
const dir = slash >= 0 ? file.path.slice(0, slash) : "";
|
|
586
|
+
const row = h(
|
|
587
|
+
"button",
|
|
588
|
+
{
|
|
589
|
+
class: "ws-row ws-change",
|
|
590
|
+
title: `${file.oldPath ? `${file.oldPath} → ` : ""}${file.path}\n${STATE_LABEL[state]}`,
|
|
591
|
+
onclick: onclick ?? (() => openDetail({ kind: "diff", path: file.path, staged, oldPath: file.oldPath, state, from: null })),
|
|
592
|
+
},
|
|
593
|
+
h("span", { class: `ws-state s-${state}` }, LETTER[state]),
|
|
594
|
+
h("span", { class: "ws-name" }, name),
|
|
595
|
+
dir ? h("span", { class: "ws-dir" }, dir) : null,
|
|
596
|
+
file.sensitive ? h("span", { class: "ws-tag" }, "敏感") : null,
|
|
597
|
+
);
|
|
598
|
+
return actions ? h("div", { class: "ws-entry" }, row, actions) : row;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/** 分组标题。action:整组的操作(全部暂存 / 全部取消暂存) */
|
|
602
|
+
function group(title, rows, action) {
|
|
603
|
+
if (!rows.length) return null;
|
|
604
|
+
return [
|
|
605
|
+
h(
|
|
606
|
+
"div",
|
|
607
|
+
{ class: "ws-group" },
|
|
608
|
+
title,
|
|
609
|
+
h("span", { class: "ws-group-count" }, rows.length),
|
|
610
|
+
action ? h("button", { class: "icon-btn ws-group-act", title: action.title, "aria-label": action.title, onclick: action.onclick }, icon(action.icon)) : null,
|
|
611
|
+
),
|
|
612
|
+
rows,
|
|
613
|
+
];
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
function branchBar(s) {
|
|
617
|
+
const name = s.branch ?? (s.head ? `分离头指针 ${s.head}` : "未知");
|
|
618
|
+
return h(
|
|
619
|
+
"button",
|
|
620
|
+
{ class: "ws-branch", title: "查看和切换分支", onclick: (e) => void openBranches(e.currentTarget) },
|
|
621
|
+
icon("fork"),
|
|
622
|
+
h("span", { class: "ws-branch-name" }, name),
|
|
623
|
+
s.upstream ? h("span", { class: "ws-branch-up" }, s.upstream) : null,
|
|
624
|
+
s.ahead ? h("span", { class: "ws-sync" }, `↑${s.ahead}`) : null,
|
|
625
|
+
s.behind ? h("span", { class: "ws-sync" }, `↓${s.behind}`) : null,
|
|
626
|
+
h("span", { class: "ws-caret" }, icon("caret")),
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// 提交框:一直是同一组元素。改动列表刷新时不动它,打到一半的字和光标都还在
|
|
631
|
+
const HOOKS_KEY = "aim.wsCommitHooks";
|
|
632
|
+
const commitUi = {
|
|
633
|
+
box: null,
|
|
634
|
+
message: null,
|
|
635
|
+
hooks: null,
|
|
636
|
+
button: null,
|
|
637
|
+
stagedCount: 0,
|
|
638
|
+
busy: false,
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
function ensureCommitBox() {
|
|
642
|
+
if (commitUi.box) return;
|
|
643
|
+
commitUi.message = h("textarea", {
|
|
644
|
+
class: "ws-commit-msg",
|
|
645
|
+
rows: "2",
|
|
646
|
+
placeholder: "提交说明",
|
|
647
|
+
"aria-label": "提交说明",
|
|
648
|
+
spellcheck: "false",
|
|
649
|
+
oninput: updateCommitButton,
|
|
650
|
+
onkeydown: (e) => {
|
|
651
|
+
if (e.key === "Enter" && (e.ctrlKey || e.metaKey) && !e.isComposing) {
|
|
652
|
+
e.preventDefault();
|
|
653
|
+
void commit();
|
|
654
|
+
}
|
|
655
|
+
},
|
|
656
|
+
});
|
|
657
|
+
commitUi.hooks = h("input", { type: "checkbox", onchange: () => localStorage.setItem(HOOKS_KEY, commitUi.hooks.checked ? "1" : "0") });
|
|
658
|
+
commitUi.hooks.checked = localStorage.getItem(HOOKS_KEY) === "1";
|
|
659
|
+
commitUi.button = h("button", { class: "btn primary", onclick: () => void commit() }, "提交");
|
|
660
|
+
commitUi.box = h(
|
|
661
|
+
"div",
|
|
662
|
+
{ class: "ws-commit" },
|
|
663
|
+
commitUi.message,
|
|
664
|
+
h(
|
|
665
|
+
"div",
|
|
666
|
+
{ class: "ws-commit-foot" },
|
|
667
|
+
h("label", { class: "ws-commit-hooks", title: "Git 钩子是仓库里的程序,只有你信任这个仓库时才打开" }, commitUi.hooks, h("span", {}, "运行 Git 钩子")),
|
|
668
|
+
commitUi.button,
|
|
669
|
+
),
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
function updateCommitButton() {
|
|
674
|
+
const n = commitUi.stagedCount;
|
|
675
|
+
commitUi.button.disabled = commitUi.busy || !n || !commitUi.message.value.trim();
|
|
676
|
+
commitUi.button.textContent = commitUi.busy ? "提交中…" : n ? `提交 ${n} 个文件` : "没有已暂存的文件";
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
async function commit() {
|
|
680
|
+
const message = commitUi.message.value.trim();
|
|
681
|
+
if (commitUi.busy || !message || !commitUi.stagedCount) return;
|
|
682
|
+
commitUi.busy = true;
|
|
683
|
+
updateCommitButton();
|
|
684
|
+
const result = await gitAct("git-commit", { message, hooks: commitUi.hooks.checked });
|
|
685
|
+
commitUi.busy = false;
|
|
686
|
+
if (result) {
|
|
687
|
+
commitUi.message.value = "";
|
|
688
|
+
toast(`已提交 ${result.short}`);
|
|
689
|
+
}
|
|
690
|
+
updateCommitButton();
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
function renderChanges() {
|
|
694
|
+
const s = git.status;
|
|
695
|
+
if (!s) return fill(ui.wsBody, loading());
|
|
696
|
+
if (!s.repo) return fill(ui.wsBody, emptyNote(s.error ?? s.reason ?? "当前目录不是 git 仓库"));
|
|
697
|
+
ensureCommitBox();
|
|
698
|
+
ui.wsBranchSlot ??= h("div", { class: "ws-branch-slot" });
|
|
699
|
+
ui.wsChangeList ??= h("div", { class: "ws-list" });
|
|
700
|
+
ui.wsChanges ??= h("div", { class: "ws-changes" }, ui.wsBranchSlot, commitUi.box, ui.wsChangeList);
|
|
701
|
+
// 已经在页面上就不动,只换里面的内容
|
|
702
|
+
if (ui.wsChanges.parentNode !== ui.wsBody || ui.wsBody.childElementCount !== 1) fill(ui.wsBody, ui.wsChanges);
|
|
703
|
+
|
|
704
|
+
const conflicted = s.files.filter((f) => f.unstaged === "conflicted");
|
|
705
|
+
const staged = s.files.filter((f) => f.staged);
|
|
706
|
+
const unstaged = s.files.filter((f) => f.unstaged && f.unstaged !== "untracked" && f.unstaged !== "conflicted");
|
|
707
|
+
const untracked = s.files.filter((f) => f.unstaged === "untracked");
|
|
708
|
+
commitUi.stagedCount = staged.length;
|
|
709
|
+
updateCommitButton();
|
|
710
|
+
fill(ui.wsBranchSlot, branchBar(s));
|
|
711
|
+
|
|
712
|
+
const paths = (files) => files.filter((f) => !f.sensitive).map((f) => f.path);
|
|
713
|
+
const stageAll = (files) => (paths(files).length ? { title: "全部暂存", icon: "plus", onclick: () => void gitAct("git-stage", { paths: paths(files) }) } : null);
|
|
714
|
+
fill(
|
|
715
|
+
ui.wsChangeList,
|
|
716
|
+
s.files.length === 0 ? h("div", { class: "ws-empty ws-clean" }, "工作区没有改动") : null,
|
|
717
|
+
group("有冲突", conflicted.map((f) => changeRow(f, "conflicted", false, null, rowActions(f, false)))),
|
|
718
|
+
group("已暂存", staged.map((f) => changeRow(f, f.staged, true, null, rowActions(f, true))), { title: "全部取消暂存", icon: "minus", onclick: () => void gitAct("git-unstage", { paths: staged.map((f) => f.path) }) }),
|
|
719
|
+
group("未暂存", unstaged.map((f) => changeRow(f, f.unstaged, false, null, rowActions(f, false))), stageAll(unstaged)),
|
|
720
|
+
group("未跟踪", untracked.map((f) => changeRow(f, "untracked", false, null, rowActions(f, false))), stageAll(untracked)),
|
|
721
|
+
s.truncated ? h("div", { class: "ws-row ws-row-note" }, "改动的文件太多,只列了前一部分") : null,
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// ---------- 提交历史 ----------
|
|
726
|
+
|
|
727
|
+
async function loadLog(skip = 0) {
|
|
728
|
+
const id = P.id;
|
|
729
|
+
try {
|
|
730
|
+
const page = await api(`${base()}/git-log?skip=${skip}`);
|
|
731
|
+
if (id !== P.id) return;
|
|
732
|
+
P.log = { commits: [...(skip ? (P.log?.commits ?? []) : []), ...page.commits], more: page.more };
|
|
733
|
+
} catch (err) {
|
|
734
|
+
if (id !== P.id) return;
|
|
735
|
+
P.log = { commits: [], more: false, error: err.message };
|
|
736
|
+
}
|
|
737
|
+
if (P.tab === "log" && !P.detail) renderLog();
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
function renderLog() {
|
|
741
|
+
if (!P.log) {
|
|
742
|
+
fill(ui.wsBody, loading());
|
|
743
|
+
P.log = { loading: true, commits: [] };
|
|
744
|
+
return void loadLog();
|
|
745
|
+
}
|
|
746
|
+
if (P.log.loading) return fill(ui.wsBody, loading());
|
|
747
|
+
if (P.log.error) return fill(ui.wsBody, emptyNote(P.log.error));
|
|
748
|
+
if (!P.log.commits.length) return fill(ui.wsBody, emptyNote("还没有提交"));
|
|
749
|
+
const old = ui.wsBody.querySelector(".ws-list");
|
|
750
|
+
const top = old?.scrollTop ?? 0;
|
|
751
|
+
const list = h(
|
|
752
|
+
"div",
|
|
753
|
+
{ class: "ws-list" },
|
|
754
|
+
P.log.commits.map((c) =>
|
|
755
|
+
h(
|
|
756
|
+
"button",
|
|
757
|
+
{ class: "ws-commit-row", title: `${c.hash}\n${c.author}`, onclick: () => void openCommit(c) },
|
|
758
|
+
h("div", { class: "ws-commit-subject" }, c.subject || "(没有说明)"),
|
|
759
|
+
h("div", { class: "ws-commit-meta" }, [c.short, c.author, c.date ? relTime(c.date) : ""].filter(Boolean).join(" · ")),
|
|
760
|
+
),
|
|
761
|
+
),
|
|
762
|
+
P.log.more ? h("button", { class: "btn ghost d-more", onclick: () => void loadLog(P.log.commits.length) }, "加载更多") : null,
|
|
763
|
+
);
|
|
764
|
+
fill(ui.wsBody, list);
|
|
765
|
+
list.scrollTop = top;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
async function openCommit(commit) {
|
|
769
|
+
const detail = { kind: "commit", commit, files: null, error: null, from: null };
|
|
770
|
+
openDetail(detail);
|
|
771
|
+
try {
|
|
772
|
+
detail.files = (await api(`${base()}/git-commit-files?hash=${encodeURIComponent(commit.hash)}`)).files;
|
|
773
|
+
} catch (err) {
|
|
774
|
+
detail.error = err.message;
|
|
775
|
+
}
|
|
776
|
+
if (P.detail === detail) renderCommitDetail();
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
function renderCommitDetail() {
|
|
780
|
+
const detail = P.detail;
|
|
781
|
+
const { commit, files } = detail;
|
|
782
|
+
const head = detailHead(commit.subject || commit.short, [commit.short, commit.author, commit.date ? relTime(commit.date) : ""].filter(Boolean).join(" · "));
|
|
783
|
+
if (detail.error) return fill(ui.wsBody, head, emptyNote(detail.error));
|
|
784
|
+
if (!files) return fill(ui.wsBody, head, loading());
|
|
785
|
+
const list = files.length
|
|
786
|
+
? h(
|
|
787
|
+
"div",
|
|
788
|
+
{ class: "ws-list" },
|
|
789
|
+
files.map((file) =>
|
|
790
|
+
changeRow(file, file.status, false, () => openDetail({ kind: "diff", path: file.path, oldPath: file.oldPath, state: file.status, commit: commit.hash, from: detail })),
|
|
791
|
+
),
|
|
792
|
+
)
|
|
793
|
+
: emptyNote("这个提交没有改动文件");
|
|
794
|
+
fill(ui.wsBody, head, list);
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
async function openBranches(anchor) {
|
|
798
|
+
const pop = showPopover(anchor, { place: "below", align: "left" });
|
|
799
|
+
pop.classList.add("ws-branches");
|
|
800
|
+
fill(pop, h("div", { class: "ws-branches-note" }, "加载中…"));
|
|
801
|
+
placePopover(pop, anchor);
|
|
802
|
+
let data;
|
|
803
|
+
try {
|
|
804
|
+
data = await api(`${base()}/git-branches`);
|
|
805
|
+
} catch (err) {
|
|
806
|
+
closeMenu();
|
|
807
|
+
return toast(err.message);
|
|
808
|
+
}
|
|
809
|
+
if (ui.menu !== pop) return;
|
|
810
|
+
const row = (b) =>
|
|
811
|
+
h(
|
|
812
|
+
"button",
|
|
813
|
+
{
|
|
814
|
+
class: `ws-branch-row${b.current ? " on" : ""}`,
|
|
815
|
+
title: b.current ? "当前分支" : b.remote ? `切换过去(建一个跟踪它的本地分支)` : "切换到这个分支",
|
|
816
|
+
onclick: async () => {
|
|
817
|
+
closeMenu();
|
|
818
|
+
if (b.current) return;
|
|
819
|
+
if ((await gitAct("git-switch", { branch: b.name })) !== undefined) toast(`已切换到 ${b.remote ? b.name.slice(b.name.indexOf("/") + 1) : b.name}`);
|
|
820
|
+
},
|
|
821
|
+
},
|
|
822
|
+
h("span", { class: "ws-branch-check" }, b.current ? icon("check") : null),
|
|
823
|
+
h(
|
|
824
|
+
"div",
|
|
825
|
+
{ class: "ws-branch-text" },
|
|
826
|
+
h("div", { class: "ws-branch-title" }, b.name),
|
|
827
|
+
h("div", { class: "ws-branch-sub" }, [b.hash, b.date ? relTime(b.date) : "", b.subject].filter(Boolean).join(" · ")),
|
|
828
|
+
),
|
|
829
|
+
);
|
|
830
|
+
const local = data.branches.filter((b) => !b.remote);
|
|
831
|
+
const remote = data.branches.filter((b) => b.remote);
|
|
832
|
+
fill(
|
|
833
|
+
pop,
|
|
834
|
+
h(
|
|
835
|
+
"button",
|
|
836
|
+
{
|
|
837
|
+
class: "ws-branch-new",
|
|
838
|
+
onclick: () => {
|
|
839
|
+
closeMenu();
|
|
840
|
+
promptName({
|
|
841
|
+
title: "新建分支",
|
|
842
|
+
confirmText: "新建并切换",
|
|
843
|
+
submit: async (name) => {
|
|
844
|
+
await gitPost("git-branch-new", { name });
|
|
845
|
+
P.log = null;
|
|
846
|
+
await refreshGit();
|
|
847
|
+
},
|
|
848
|
+
});
|
|
849
|
+
},
|
|
850
|
+
},
|
|
851
|
+
icon("plus"),
|
|
852
|
+
"新建分支…",
|
|
853
|
+
),
|
|
854
|
+
local.length ? h("div", { class: "ws-group" }, "本地分支") : null,
|
|
855
|
+
local.map(row),
|
|
856
|
+
remote.length ? h("div", { class: "ws-group" }, "远程分支") : null,
|
|
857
|
+
remote.map(row),
|
|
858
|
+
!data.branches.length ? h("div", { class: "ws-branches-note" }, "还没有分支,先提交一次") : null,
|
|
859
|
+
data.truncated ? h("div", { class: "ws-branches-note" }, "分支太多,只列了最近的一部分") : null,
|
|
860
|
+
);
|
|
861
|
+
placePopover(pop, anchor);
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
// ---------- 详情 ----------
|
|
865
|
+
|
|
866
|
+
function openDetail(detail) {
|
|
867
|
+
P.detail = detail;
|
|
868
|
+
P.diff = null;
|
|
869
|
+
P.diffError = null;
|
|
870
|
+
renderBody();
|
|
871
|
+
if (detail.kind === "diff") void loadDiff();
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
async function closeDetail() {
|
|
875
|
+
if (!(await leaveEdit())) return;
|
|
876
|
+
const from = P.detail?.from;
|
|
877
|
+
P.detail = from ?? null;
|
|
878
|
+
P.diff = null;
|
|
879
|
+
P.diffError = null;
|
|
880
|
+
renderBody();
|
|
881
|
+
if (from?.kind === "diff") void loadDiff();
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
function detailHead(name, sub, ...actions) {
|
|
885
|
+
return h(
|
|
886
|
+
"div",
|
|
887
|
+
{ class: "ws-detail-head" },
|
|
888
|
+
h("button", { class: "icon-btn", title: "返回", "aria-label": "返回", onclick: () => void closeDetail() }, icon("back")),
|
|
889
|
+
h("div", { class: "ws-detail-title", title: sub }, h("div", { class: "ws-detail-name" }, name), h("div", { class: "ws-detail-sub" }, sub)),
|
|
890
|
+
...actions,
|
|
891
|
+
);
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
function renderDetail() {
|
|
895
|
+
if (P.detail.kind === "file") return renderFileDetail();
|
|
896
|
+
if (P.detail.kind === "edit") return renderEditDetail();
|
|
897
|
+
if (P.detail.kind === "turn") return renderTurnDetail();
|
|
898
|
+
if (P.detail.kind === "commit") return renderCommitDetail();
|
|
899
|
+
renderDiffDetail();
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// ---------- 一轮的改动 ----------
|
|
903
|
+
|
|
904
|
+
function turnDiffDetail(changes, file, from) {
|
|
905
|
+
return { kind: "diff", path: file.path, oldPath: file.oldPath, state: file.status, turn: changes.id, from };
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
async function loadTurnState(detail) {
|
|
909
|
+
try {
|
|
910
|
+
detail.turnState = await api(`${base()}/git-turn-state?id=${encodeURIComponent(detail.changes.id)}`);
|
|
911
|
+
} catch {
|
|
912
|
+
detail.turnState = { state: "expired" };
|
|
913
|
+
}
|
|
914
|
+
if (P.detail === detail) renderTurnDetail();
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
async function revert(detail, direction, force = false) {
|
|
918
|
+
const { changes } = detail;
|
|
919
|
+
if (!force) {
|
|
920
|
+
const sure = await confirmDialog(
|
|
921
|
+
direction === "undo"
|
|
922
|
+
? {
|
|
923
|
+
title: `撤销这一轮改的 ${changes.fileCount} 个文件?`,
|
|
924
|
+
message: "这些文件会恢复成这一轮开始前的样子,别的文件不动。之后可以重做。对话记录不变,agent 不知道文件被撤销了,接着聊之前最好告诉它。",
|
|
925
|
+
confirmText: "撤销",
|
|
926
|
+
danger: true,
|
|
927
|
+
}
|
|
928
|
+
: { title: `重做这一轮改的 ${changes.fileCount} 个文件?`, message: "这些文件会恢复成这一轮结束时的样子。", confirmText: "重做" },
|
|
929
|
+
);
|
|
930
|
+
if (!sure) return;
|
|
931
|
+
}
|
|
932
|
+
try {
|
|
933
|
+
detail.turnState = await gitPost("git-turn-revert", { id: changes.id, direction, force });
|
|
934
|
+
toast(direction === "undo" ? "已撤销" : "已重做");
|
|
935
|
+
await refreshGit();
|
|
936
|
+
} catch (err) {
|
|
937
|
+
if (err.data?.conflict !== "diverged") return gitFail(err);
|
|
938
|
+
const files = err.data.files ?? [];
|
|
939
|
+
const sure = await confirmDialog({
|
|
940
|
+
title: "这些文件之后又被改过了",
|
|
941
|
+
message: `${files.slice(0, 8).join("、")}${files.length > 8 ? ` 等 ${files.length} 个文件` : ""} 和这一轮结束时不一样了。继续的话,之后的改动会丢掉,找不回来。`,
|
|
942
|
+
confirmText: "仍然继续",
|
|
943
|
+
danger: true,
|
|
944
|
+
});
|
|
945
|
+
if (sure) return revert(detail, direction, true);
|
|
946
|
+
}
|
|
947
|
+
if (P.detail === detail) renderTurnDetail();
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
function renderTurnDetail() {
|
|
951
|
+
const detail = P.detail;
|
|
952
|
+
const { changes } = detail;
|
|
953
|
+
const state = detail.turnState?.state;
|
|
954
|
+
const action =
|
|
955
|
+
state === "undone"
|
|
956
|
+
? h("button", { class: "btn ghost", onclick: () => void revert(detail, "redo") }, "重做")
|
|
957
|
+
: state === "applied" || state === "diverged"
|
|
958
|
+
? h("button", { class: "btn ghost", onclick: () => void revert(detail, "undo") }, "撤销这一轮")
|
|
959
|
+
: null;
|
|
960
|
+
const head = detailHead(
|
|
961
|
+
`这一轮改了 ${changes.fileCount} 个文件`,
|
|
962
|
+
state === "undone" ? "已经撤销了" : state === "diverged" ? "这些文件之后又被改过" : "和这一轮开始前相比",
|
|
963
|
+
h("span", { class: "ws-stat" }, h("span", { class: "ws-add" }, `+${changes.added}`), h("span", { class: "ws-del" }, `−${changes.removed}`)),
|
|
964
|
+
action,
|
|
965
|
+
);
|
|
966
|
+
const rows = changes.files.map((file) =>
|
|
967
|
+
changeRow(file, file.status, false, () => openDetail(turnDiffDetail(changes, file, detail))),
|
|
968
|
+
);
|
|
969
|
+
fill(
|
|
970
|
+
ui.wsBody,
|
|
971
|
+
head,
|
|
972
|
+
h(
|
|
973
|
+
"div",
|
|
974
|
+
{ class: "ws-list" },
|
|
975
|
+
changes.shared ? h("div", { class: "d-note-bar" }, "这一轮期间同一仓库里还有别的会话在跑,可能混入了它们的改动") : null,
|
|
976
|
+
rows,
|
|
977
|
+
changes.fileCount > rows.length ? h("div", { class: "ws-row ws-row-note" }, `文件太多,只列了前 ${rows.length} 个`) : null,
|
|
978
|
+
),
|
|
979
|
+
);
|
|
980
|
+
if (!detail.turnState) void loadTurnState(detail);
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
async function renderFileDetail() {
|
|
984
|
+
const detail = P.detail;
|
|
985
|
+
const rel = detail.path;
|
|
986
|
+
const body = h("div", { class: "fv-body ws-preview" }, h("div", { class: "fv-loading" }, starsLoader()));
|
|
987
|
+
const actions = h("div", { class: "fv-actions" });
|
|
988
|
+
const head = detailHead(rel.split("/").pop(), rel, actions);
|
|
989
|
+
const sub = head.querySelector(".ws-detail-sub");
|
|
990
|
+
fill(ui.wsBody, head, body);
|
|
991
|
+
const url = fileUrl(rel);
|
|
992
|
+
const alive = () => P.detail === detail && body.isConnected;
|
|
993
|
+
let info;
|
|
994
|
+
try {
|
|
995
|
+
info = await api(`${url}&stat=1`);
|
|
996
|
+
} catch (err) {
|
|
997
|
+
if (alive()) body.replaceChildren(emptyNote(err.message));
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
if (!alive()) return;
|
|
1001
|
+
sub.textContent = `${rel} · ${fmtSize(info.size)}`;
|
|
1002
|
+
await renderFileView({ info, url, page: pageUrl(rel), body, actions, alive });
|
|
1003
|
+
if (alive() && editableName(info.name) && info.size <= EDIT_MAX) {
|
|
1004
|
+
actions.prepend(h("button", { class: "btn", onclick: () => openEditor(rel, detail) }, "编辑"));
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
// ---------- 编辑 ----------
|
|
1009
|
+
|
|
1010
|
+
/** 和服务端的 EDIT_MAX_BYTES 一致 */
|
|
1011
|
+
const EDIT_MAX = 1024 * 1024;
|
|
1012
|
+
|
|
1013
|
+
const editDirty = () => !!P.edit?.editor && P.edit.editor.text() !== P.edit.saved;
|
|
1014
|
+
const editBase = (e) => `/api/conversations/${encodeURIComponent(e.key)}`;
|
|
1015
|
+
|
|
1016
|
+
function dropEditor() {
|
|
1017
|
+
P.edit?.editor?.destroy();
|
|
1018
|
+
P.edit = null;
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
/** 离开编辑页之前:有没保存的内容就问一下。返回 false 表示留在这里 */
|
|
1022
|
+
async function leaveEdit() {
|
|
1023
|
+
if (P.detail?.kind !== "edit" || !P.edit) return true;
|
|
1024
|
+
if (editDirty()) {
|
|
1025
|
+
const choice = await confirmDialog({
|
|
1026
|
+
title: "还有没保存的修改",
|
|
1027
|
+
message: `要保存对「${P.edit.name}」的修改吗?`,
|
|
1028
|
+
confirmText: "保存",
|
|
1029
|
+
altText: "不保存",
|
|
1030
|
+
});
|
|
1031
|
+
if (choice === false) {
|
|
1032
|
+
P.edit?.editor.focus();
|
|
1033
|
+
return false;
|
|
1034
|
+
}
|
|
1035
|
+
if (choice === true && !(await saveEdit())) return false;
|
|
1036
|
+
if (choice === "alt") clearDraft(P.edit);
|
|
1037
|
+
}
|
|
1038
|
+
dropEditor();
|
|
1039
|
+
return true;
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
async function openEditor(rel, from) {
|
|
1043
|
+
P.detail = { kind: "edit", path: rel, from };
|
|
1044
|
+
const detail = P.detail;
|
|
1045
|
+
dropEditor();
|
|
1046
|
+
const edit = { key: S.current, path: rel, name: rel.split("/").pop(), hash: "", saved: "", editor: null, host: h("div", { class: "ws-editor" }), previewEl: null, saving: false, loadError: null };
|
|
1047
|
+
P.edit = edit;
|
|
1048
|
+
renderEditDetail();
|
|
1049
|
+
const alive = () => P.edit === edit && P.detail === detail;
|
|
1050
|
+
try {
|
|
1051
|
+
const file = await api(`${editBase(edit)}/workspace-text?path=${encodeURIComponent(rel)}`);
|
|
1052
|
+
if (!alive()) return;
|
|
1053
|
+
edit.hash = file.hash;
|
|
1054
|
+
edit.saved = file.content;
|
|
1055
|
+
// 上次没保存的内容:放回编辑器,还是按当时读出来的版本算冲突
|
|
1056
|
+
const draft = readDraft(edit);
|
|
1057
|
+
const restored = draft && draft.text !== file.content;
|
|
1058
|
+
if (restored) edit.hash = draft.hash;
|
|
1059
|
+
else if (draft) clearDraft(edit);
|
|
1060
|
+
const editor = await createEditor({
|
|
1061
|
+
parent: edit.host,
|
|
1062
|
+
text: restored ? draft.text : file.content,
|
|
1063
|
+
name: edit.name,
|
|
1064
|
+
onChange: () => {
|
|
1065
|
+
updateEditHead();
|
|
1066
|
+
scheduleDraft(edit);
|
|
1067
|
+
},
|
|
1068
|
+
onSave: () => void saveEdit(),
|
|
1069
|
+
});
|
|
1070
|
+
if (!alive()) return editor.destroy();
|
|
1071
|
+
edit.editor = editor;
|
|
1072
|
+
if (restored) {
|
|
1073
|
+
// 跟文件现在的内容比才知道有没有改;draft 的基准不是当前文件时,存的时候会走冲突
|
|
1074
|
+
toast("已恢复上次没保存的修改");
|
|
1075
|
+
}
|
|
1076
|
+
renderEditDetail();
|
|
1077
|
+
editor.focus();
|
|
1078
|
+
} catch (err) {
|
|
1079
|
+
if (!alive()) return;
|
|
1080
|
+
edit.loadError = err.message;
|
|
1081
|
+
renderEditDetail();
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
let editHead = null;
|
|
1086
|
+
|
|
1087
|
+
function updateEditHead() {
|
|
1088
|
+
if (!editHead || !P.edit) return;
|
|
1089
|
+
const dirty = editDirty();
|
|
1090
|
+
editHead.save.disabled = !dirty || P.edit.saving;
|
|
1091
|
+
editHead.save.textContent = P.edit.saving ? "保存中…" : "保存";
|
|
1092
|
+
editHead.dot.hidden = !dirty;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
function renderEditDetail() {
|
|
1096
|
+
const edit = P.edit;
|
|
1097
|
+
if (!edit) return;
|
|
1098
|
+
const save = h("button", { class: "btn primary", disabled: true, onclick: () => void saveEdit() }, "保存");
|
|
1099
|
+
const dot = h("span", { class: "ws-dirty", title: "有没保存的修改", hidden: true });
|
|
1100
|
+
const hasPreview = edit.editor && textPreview(edit.name, "") !== null;
|
|
1101
|
+
const previewBtn = hasPreview
|
|
1102
|
+
? h(
|
|
1103
|
+
"button",
|
|
1104
|
+
{
|
|
1105
|
+
class: "btn ghost",
|
|
1106
|
+
onclick: () => {
|
|
1107
|
+
edit.previewEl = edit.previewEl ? null : h("div", { class: "fv-body ws-preview" }, textPreview(edit.name, edit.editor.text()));
|
|
1108
|
+
renderEditDetail();
|
|
1109
|
+
},
|
|
1110
|
+
},
|
|
1111
|
+
edit.previewEl ? "继续编辑" : "预览",
|
|
1112
|
+
)
|
|
1113
|
+
: null;
|
|
1114
|
+
const head = detailHead(edit.name, edit.path, ...(edit.editor ? [previewBtn, save] : []));
|
|
1115
|
+
head.querySelector(".ws-detail-name").append(dot);
|
|
1116
|
+
editHead = { save, dot };
|
|
1117
|
+
if (edit.loadError) return fill(ui.wsBody, head, emptyNote(edit.loadError));
|
|
1118
|
+
if (!edit.editor) return fill(ui.wsBody, head, loading());
|
|
1119
|
+
edit.host.hidden = !!edit.previewEl;
|
|
1120
|
+
fill(ui.wsBody, head, edit.host, edit.previewEl);
|
|
1121
|
+
edit.editor.measure();
|
|
1122
|
+
updateEditHead();
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
/** 保存。成功返回 true;失败、用户取消返回 false。force:明确要覆盖别人的改动 */
|
|
1126
|
+
async function saveEdit(force = false) {
|
|
1127
|
+
const edit = P.edit;
|
|
1128
|
+
if (!edit?.editor || edit.saving) return false;
|
|
1129
|
+
const content = edit.editor.text();
|
|
1130
|
+
if (!force && content === edit.saved) return true;
|
|
1131
|
+
edit.saving = true;
|
|
1132
|
+
updateEditHead();
|
|
1133
|
+
try {
|
|
1134
|
+
const saved = await api(`${editBase(edit)}/workspace-save`, { method: "POST", body: { path: edit.path, content, baseHash: edit.hash, force } });
|
|
1135
|
+
edit.hash = saved.hash;
|
|
1136
|
+
edit.saved = content;
|
|
1137
|
+
clearDraft(edit);
|
|
1138
|
+
toast("已保存");
|
|
1139
|
+
void refreshGit();
|
|
1140
|
+
return true;
|
|
1141
|
+
} catch (err) {
|
|
1142
|
+
if (err.status !== 409 || !err.data?.conflict) {
|
|
1143
|
+
toast(err.message);
|
|
1144
|
+
return false;
|
|
1145
|
+
}
|
|
1146
|
+
// 等用户选的时候不算在保存
|
|
1147
|
+
edit.saving = false;
|
|
1148
|
+
updateEditHead();
|
|
1149
|
+
return await resolveConflict(edit, err.data.conflict);
|
|
1150
|
+
} finally {
|
|
1151
|
+
edit.saving = false;
|
|
1152
|
+
if (P.edit === edit) updateEditHead();
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
/** 文件在编辑期间被改了、被删了:不替用户决定 */
|
|
1157
|
+
async function resolveConflict(edit, conflict) {
|
|
1158
|
+
if (conflict === "deleted") {
|
|
1159
|
+
const sure = await confirmDialog({
|
|
1160
|
+
title: "文件已经被删掉了",
|
|
1161
|
+
message: `「${edit.name}」在你编辑期间被删除了。要用你正在编辑的内容重新创建它吗?`,
|
|
1162
|
+
confirmText: "重新创建",
|
|
1163
|
+
});
|
|
1164
|
+
if (sure) return saveEdit(true);
|
|
1165
|
+
if (P.edit === edit) edit.editor.focus();
|
|
1166
|
+
return false;
|
|
1167
|
+
}
|
|
1168
|
+
const choice = await confirmDialog({
|
|
1169
|
+
title: "文件已经被改过了",
|
|
1170
|
+
message: `「${edit.name}」在你编辑期间被改过了,可能是 agent 改的。覆盖保存会丢掉那些改动;重新加载会丢掉你的修改。`,
|
|
1171
|
+
confirmText: "覆盖保存",
|
|
1172
|
+
danger: true,
|
|
1173
|
+
altText: "重新加载",
|
|
1174
|
+
});
|
|
1175
|
+
if (choice === true) return saveEdit(true);
|
|
1176
|
+
if (choice === "alt") await reloadEditor(edit);
|
|
1177
|
+
// 弹窗关了,焦点放回编辑器,接着打字
|
|
1178
|
+
if (P.edit === edit) edit.editor.focus();
|
|
1179
|
+
return false;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
async function reloadEditor(edit) {
|
|
1183
|
+
try {
|
|
1184
|
+
const file = await api(`${editBase(edit)}/workspace-text?path=${encodeURIComponent(edit.path)}`);
|
|
1185
|
+
if (P.edit !== edit) return;
|
|
1186
|
+
edit.hash = file.hash;
|
|
1187
|
+
edit.saved = file.content;
|
|
1188
|
+
edit.editor.setText(file.content);
|
|
1189
|
+
clearDraft(edit);
|
|
1190
|
+
updateEditHead();
|
|
1191
|
+
toast("已重新加载");
|
|
1192
|
+
} catch (err) {
|
|
1193
|
+
toast(err.message);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
// ---------- 草稿 ----------
|
|
1198
|
+
// 存在本机的 localStorage:{ text, hash, at }。空间不够存不下就算了,有离开时的提示兜底
|
|
1199
|
+
|
|
1200
|
+
const DRAFT_PREFIX = "aim.wsDraft.";
|
|
1201
|
+
const DRAFT_MAX_AGE = 30 * 86400_000;
|
|
1202
|
+
const draftKey = (edit) => `${DRAFT_PREFIX}${edit.key}\n${edit.path}`;
|
|
1203
|
+
let draftTimer = 0;
|
|
1204
|
+
|
|
1205
|
+
function writeDraft(edit) {
|
|
1206
|
+
if (!edit.editor) return;
|
|
1207
|
+
try {
|
|
1208
|
+
if (edit.editor.text() === edit.saved) localStorage.removeItem(draftKey(edit));
|
|
1209
|
+
else localStorage.setItem(draftKey(edit), JSON.stringify({ text: edit.editor.text(), hash: edit.hash, at: Date.now() }));
|
|
1210
|
+
} catch {
|
|
1211
|
+
// 空间不够
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
function scheduleDraft(edit) {
|
|
1216
|
+
clearTimeout(draftTimer);
|
|
1217
|
+
draftTimer = setTimeout(() => writeDraft(edit), 500);
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
function flushDraft() {
|
|
1221
|
+
clearTimeout(draftTimer);
|
|
1222
|
+
if (P.edit) writeDraft(P.edit);
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
function clearDraft(edit) {
|
|
1226
|
+
clearTimeout(draftTimer);
|
|
1227
|
+
localStorage.removeItem(draftKey(edit));
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
function readDraft(edit) {
|
|
1231
|
+
try {
|
|
1232
|
+
const draft = JSON.parse(localStorage.getItem(draftKey(edit)) ?? "null");
|
|
1233
|
+
return typeof draft?.text === "string" ? draft : null;
|
|
1234
|
+
} catch {
|
|
1235
|
+
return null;
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
/** 太久没回来的草稿不留着占地方 */
|
|
1240
|
+
function sweepDrafts() {
|
|
1241
|
+
for (let i = localStorage.length - 1; i >= 0; i--) {
|
|
1242
|
+
const key = localStorage.key(i);
|
|
1243
|
+
if (!key?.startsWith(DRAFT_PREFIX)) continue;
|
|
1244
|
+
try {
|
|
1245
|
+
if (Date.now() - (JSON.parse(localStorage.getItem(key)).at ?? 0) > DRAFT_MAX_AGE) localStorage.removeItem(key);
|
|
1246
|
+
} catch {
|
|
1247
|
+
localStorage.removeItem(key);
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
sweepDrafts();
|
|
1252
|
+
addEventListener("pagehide", flushDraft);
|
|
1253
|
+
|
|
1254
|
+
async function loadDiff() {
|
|
1255
|
+
const detail = P.detail;
|
|
1256
|
+
const token = ++P.loadToken;
|
|
1257
|
+
try {
|
|
1258
|
+
const path = encodeURIComponent(detail.path);
|
|
1259
|
+
const diff = await api(
|
|
1260
|
+
detail.turn
|
|
1261
|
+
? `${base()}/git-turn-diff?id=${encodeURIComponent(detail.turn)}&path=${path}`
|
|
1262
|
+
: detail.commit
|
|
1263
|
+
? `${base()}/git-commit-diff?hash=${encodeURIComponent(detail.commit)}&path=${path}`
|
|
1264
|
+
: `${base()}/git-diff?path=${path}${detail.staged ? "&staged=1" : ""}`,
|
|
1265
|
+
);
|
|
1266
|
+
if (token !== P.loadToken || P.detail !== detail) return;
|
|
1267
|
+
P.diff = diff;
|
|
1268
|
+
P.diffError = null;
|
|
1269
|
+
} catch (err) {
|
|
1270
|
+
if (token !== P.loadToken || P.detail !== detail) return;
|
|
1271
|
+
P.diff = null;
|
|
1272
|
+
P.diffError = err.status === 404 ? (detail.turn ? "这一轮的快照已经清理了,看不到具体内容" : detail.commit ? "找不到这个提交里的这个文件" : "这个文件现在已经没有这处改动了") : err.message;
|
|
1273
|
+
}
|
|
1274
|
+
renderDiffDetail();
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
/** 仓库路径(相对根目录)换算成相对工作目录的路径;在工作目录之外:null */
|
|
1278
|
+
function repoToWorkspace(p) {
|
|
1279
|
+
const prefix = git.status?.prefix ?? "";
|
|
1280
|
+
if (!prefix) return p;
|
|
1281
|
+
return p.startsWith(`${prefix}/`) ? p.slice(prefix.length + 1) : null;
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
function renderDiffDetail() {
|
|
1285
|
+
const detail = P.detail;
|
|
1286
|
+
const canSplit = ui.wsPanel.getBoundingClientRect().width >= SPLIT_MIN_WIDTH;
|
|
1287
|
+
const mode = canSplit ? P.diffMode : "unified";
|
|
1288
|
+
const rel = repoToWorkspace(detail.path);
|
|
1289
|
+
const old = ui.wsBody.querySelector(".ws-diff-scroll");
|
|
1290
|
+
const top = old?.scrollTop ?? 0;
|
|
1291
|
+
|
|
1292
|
+
const rendered = P.diff?.diff ? renderDiff(P.diff.diff, { path: detail.path, mode }) : null;
|
|
1293
|
+
const stat = rendered?.stat;
|
|
1294
|
+
const modeBtn = canSplit && rendered?.stat
|
|
1295
|
+
? h(
|
|
1296
|
+
"button",
|
|
1297
|
+
{
|
|
1298
|
+
class: "btn ghost",
|
|
1299
|
+
onclick: () => {
|
|
1300
|
+
P.diffMode = P.diffMode === "split" ? "unified" : "split";
|
|
1301
|
+
localStorage.setItem(MODE_KEY, P.diffMode);
|
|
1302
|
+
renderDiffDetail();
|
|
1303
|
+
},
|
|
1304
|
+
},
|
|
1305
|
+
P.diffMode === "split" ? "单栏" : "并排",
|
|
1306
|
+
)
|
|
1307
|
+
: null;
|
|
1308
|
+
const openBtn = rel && detail.state !== "deleted"
|
|
1309
|
+
? h("button", { class: "btn ghost", onclick: () => openDetail({ kind: "file", path: rel, from: detail }) }, "看文件")
|
|
1310
|
+
: null;
|
|
1311
|
+
const sub = [
|
|
1312
|
+
detail.turn ? "这一轮" : detail.commit ? `提交 ${detail.commit.slice(0, 7)}` : "",
|
|
1313
|
+
STATE_LABEL[detail.state],
|
|
1314
|
+
detail.staged ? "已暂存" : "",
|
|
1315
|
+
detail.oldPath ? `从 ${detail.oldPath}` : "",
|
|
1316
|
+
].filter(Boolean).join(" · ");
|
|
1317
|
+
const statEl = stat && (stat.added || stat.removed)
|
|
1318
|
+
? h("span", { class: "ws-stat" }, h("span", { class: "ws-add" }, `+${stat.added}`), h("span", { class: "ws-del" }, `−${stat.removed}`))
|
|
1319
|
+
: null;
|
|
1320
|
+
const head = detailHead(detail.path.split("/").pop(), detail.path, statEl, modeBtn, openBtn);
|
|
1321
|
+
head.querySelector(".ws-detail-sub").textContent = `${detail.path}${sub ? ` · ${sub}` : ""}`;
|
|
1322
|
+
|
|
1323
|
+
const scroll = h("div", { class: "ws-diff-scroll" });
|
|
1324
|
+
if (P.diffError) scroll.append(emptyNote(P.diffError));
|
|
1325
|
+
else if (!P.diff) scroll.append(loading());
|
|
1326
|
+
else if (P.diff.note) scroll.append(emptyNote(P.diff.note));
|
|
1327
|
+
else if (P.diff.binary) scroll.append(emptyNote("二进制文件,没有文字差异"));
|
|
1328
|
+
else if (!P.diff.diff) scroll.append(emptyNote("没有内容上的差异"));
|
|
1329
|
+
else {
|
|
1330
|
+
if (P.diff.truncated) scroll.append(h("div", { class: "d-note-bar" }, "这个 diff 太大,只显示了前 2 MB"));
|
|
1331
|
+
if (rendered.empty) scroll.append(emptyNote(metaSummary(rendered.meta)));
|
|
1332
|
+
else scroll.append(rendered.el);
|
|
1333
|
+
}
|
|
1334
|
+
fill(ui.wsBody, head, scroll);
|
|
1335
|
+
scroll.scrollTop = top;
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
/** 没有内容改动,只改了权限、改了名字:把 git 说的翻出来 */
|
|
1339
|
+
function metaSummary(meta) {
|
|
1340
|
+
const oldMode = meta.find((l) => l.startsWith("old mode "));
|
|
1341
|
+
const newMode = meta.find((l) => l.startsWith("new mode "));
|
|
1342
|
+
if (oldMode && newMode) return `文件权限从 ${oldMode.slice(9)} 改成 ${newMode.slice(9)},内容没变`;
|
|
1343
|
+
if (meta.some((l) => l.startsWith("rename from "))) return "只改了名字,内容没变";
|
|
1344
|
+
if (meta.some((l) => l.startsWith("new file mode "))) return "空文件";
|
|
1345
|
+
return "没有内容上的差异";
|
|
1346
|
+
}
|