mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-17 04:45:33 -08:00
Add the Changes tab at sign-off: snapshot at start, diff at the end, drawn in the console
AI Assisted
This commit is contained in:
1 parent
72ae44745f
commit
f61ef6e0a7
25 files changed
+1324
-21
No files matched your search
@@ -0,0 +1,242 @@
|
||||
// Plan2Code Web Console - a unified diff (changes.mjs diff) read into files, hunks and numbered lines; defines only.
|
||||
//
|
||||
// The Changes tab draws what this returns. It never throws: text it cannot
|
||||
// read comes back as `{ files: [], tooLarge: false, bad: true }`, which the tab
|
||||
// shows as its error state.
|
||||
|
||||
// The lines changes.mjs writes in place of a file's lines, and after the last
|
||||
// file it kept whole when the change set was over its cap.
|
||||
export const HIDDEN_LINE = "Lines hidden: this file may hold a secret.";
|
||||
export const TOO_LARGE_LINE = "\\ No more lines shown: this change is too large to show in full.";
|
||||
|
||||
const HUNK = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)$/;
|
||||
|
||||
// A path git quoted ("a/with \"quote\".txt"): C escapes, octal bytes as UTF-8.
|
||||
// Returns [path, rest of the line] or null.
|
||||
function unquote(s) {
|
||||
if (s[0] !== '"') return null;
|
||||
const bytes = [];
|
||||
const enc = new TextEncoder();
|
||||
const named = { n: 10, t: 9, r: 13, a: 7, b: 8, f: 12, v: 11, '"': 34, "\\": 92 };
|
||||
let i = 1;
|
||||
while (i < s.length) {
|
||||
const c = s[i];
|
||||
if (c === '"') return [new TextDecoder().decode(new Uint8Array(bytes)), s.slice(i + 1)];
|
||||
if (c === "\\") {
|
||||
const n = s[i + 1];
|
||||
if (/[0-7]/.test(n || "")) {
|
||||
const oct = /^[0-7]{1,3}/.exec(s.slice(i + 1))[0];
|
||||
bytes.push(parseInt(oct, 8) & 0xff);
|
||||
i += 1 + oct.length;
|
||||
continue;
|
||||
}
|
||||
if (n in named) {
|
||||
bytes.push(named[n]);
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// One whole character, so a surrogate pair stays one.
|
||||
const ch = String.fromCodePoint(s.codePointAt(i));
|
||||
bytes.push(...enc.encode(ch));
|
||||
i += ch.length;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const strip = (p, prefix) => (p.startsWith(prefix) ? p.slice(prefix.length) : p);
|
||||
|
||||
// The two paths of a `diff --git a/x b/y` line. Unquoted paths with spaces are
|
||||
// ambiguous, so the halves are taken equal when they can be (the usual case:
|
||||
// the same file on both sides); the ---, +++ and rename lines correct it later.
|
||||
function gitLinePaths(rest) {
|
||||
if (rest[0] === '"') {
|
||||
const a = unquote(rest);
|
||||
if (!a) return null;
|
||||
const after = a[1].replace(/^ /, "");
|
||||
const b = after[0] === '"' ? unquote(after) : [after, ""];
|
||||
if (!b) return null;
|
||||
return [strip(a[0], "a/"), strip(b[0], "b/")];
|
||||
}
|
||||
const half = (rest.length - 1) / 2;
|
||||
if (Number.isInteger(half) && rest[half] === " " && rest.slice(0, half).slice(2) === rest.slice(half + 1).slice(2)) {
|
||||
return [strip(rest.slice(0, half), "a/"), strip(rest.slice(half + 1), "b/")];
|
||||
}
|
||||
const quotedB = rest.indexOf(' "b/');
|
||||
if (quotedB > 0) {
|
||||
const b = unquote(rest.slice(quotedB + 1));
|
||||
if (b) return [strip(rest.slice(0, quotedB), "a/"), strip(b[0], "b/")];
|
||||
}
|
||||
const at = rest.indexOf(" b/");
|
||||
if (at < 0) return null;
|
||||
return [strip(rest.slice(0, at), "a/"), strip(rest.slice(at + 1), "b/")];
|
||||
}
|
||||
|
||||
// The path on a ---, +++, rename from or rename to line: quoted or plain, with
|
||||
// the trailing tab git adds after a name holding a space dropped.
|
||||
function linePath(raw) {
|
||||
const q = unquote(raw);
|
||||
if (q) return q[0];
|
||||
return raw.replace(/\t$/, "");
|
||||
}
|
||||
|
||||
function newFile(paths) {
|
||||
return {
|
||||
path: paths[1],
|
||||
oldPath: paths[0],
|
||||
status: "modified",
|
||||
added: 0,
|
||||
removed: 0,
|
||||
binary: false,
|
||||
hidden: false,
|
||||
truncated: false,
|
||||
hunks: [],
|
||||
};
|
||||
}
|
||||
|
||||
function finish(file, afterCut) {
|
||||
if (file.status !== "renamed") file.oldPath = null;
|
||||
if (afterCut && !file.hunks.length && !file.binary && !file.hidden) file.truncated = true;
|
||||
file.afterCut = afterCut;
|
||||
return file;
|
||||
}
|
||||
|
||||
export function parseDiff(text) {
|
||||
const bad = { files: [], tooLarge: false, bad: true };
|
||||
if (typeof text !== "string") return bad;
|
||||
try {
|
||||
return readDiff(text);
|
||||
} catch {
|
||||
return bad;
|
||||
}
|
||||
}
|
||||
|
||||
function readDiff(text) {
|
||||
const lines = text.replace(/\r\n/g, "\n").split("\n");
|
||||
const files = [];
|
||||
let tooLarge = false;
|
||||
let file = null;
|
||||
let fileAfterCut = false;
|
||||
let hunk = null;
|
||||
let oldNo = 0;
|
||||
let newNo = 0;
|
||||
let oldLeft = 0;
|
||||
let newLeft = 0;
|
||||
let lastWasLine = false;
|
||||
const close = () => {
|
||||
if (file) files.push(finish(file, fileAfterCut));
|
||||
file = null;
|
||||
hunk = null;
|
||||
};
|
||||
|
||||
for (const line of lines) {
|
||||
// Inside a hunk, its counts decide: a line starting "+++" or "---" there
|
||||
// is a changed line, never a header.
|
||||
if (hunk && (oldLeft > 0 || newLeft > 0)) {
|
||||
const c = line[0];
|
||||
if (c === "+" && newLeft > 0) {
|
||||
hunk.lines.push({ kind: "add", oldNo: null, newNo: newNo++, text: line.slice(1) });
|
||||
newLeft--;
|
||||
file.added++;
|
||||
lastWasLine = true;
|
||||
continue;
|
||||
}
|
||||
if (c === "-" && oldLeft > 0) {
|
||||
hunk.lines.push({ kind: "del", oldNo: oldNo++, newNo: null, text: line.slice(1) });
|
||||
oldLeft--;
|
||||
file.removed++;
|
||||
lastWasLine = true;
|
||||
continue;
|
||||
}
|
||||
// An empty line stands for an empty context line some tools trim.
|
||||
if ((c === " " || line === "") && oldLeft > 0 && newLeft > 0) {
|
||||
hunk.lines.push({ kind: "ctx", oldNo: oldNo++, newNo: newNo++, text: line.slice(1) });
|
||||
oldLeft--;
|
||||
newLeft--;
|
||||
lastWasLine = true;
|
||||
continue;
|
||||
}
|
||||
if (c === "\\" && line !== TOO_LARGE_LINE && lastWasLine) {
|
||||
hunk.lines.push({ kind: "meta", oldNo: null, newNo: null, text: line });
|
||||
lastWasLine = false;
|
||||
continue;
|
||||
}
|
||||
// Anything else ends the hunk early and is read below.
|
||||
hunk = null;
|
||||
}
|
||||
|
||||
if (line === TOO_LARGE_LINE) {
|
||||
close();
|
||||
tooLarge = true;
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("diff --git ")) {
|
||||
close();
|
||||
const paths = gitLinePaths(line.slice("diff --git ".length));
|
||||
if (!paths) throw new Error("unreadable diff header");
|
||||
file = newFile(paths);
|
||||
fileAfterCut = tooLarge;
|
||||
lastWasLine = false;
|
||||
continue;
|
||||
}
|
||||
if (!file) continue;
|
||||
// The marker after the last line of a file with no newline at its end.
|
||||
if (hunk && line[0] === "\\" && lastWasLine) {
|
||||
hunk.lines.push({ kind: "meta", oldNo: null, newNo: null, text: line });
|
||||
lastWasLine = false;
|
||||
continue;
|
||||
}
|
||||
const h = HUNK.exec(line);
|
||||
if (h) {
|
||||
oldNo = Number(h[1]);
|
||||
oldLeft = h[2] === undefined ? 1 : Number(h[2]);
|
||||
newNo = Number(h[3]);
|
||||
newLeft = h[4] === undefined ? 1 : Number(h[4]);
|
||||
hunk = { header: line, lines: [] };
|
||||
file.hunks.push(hunk);
|
||||
lastWasLine = false;
|
||||
continue;
|
||||
}
|
||||
if (file.hunks.length) continue;
|
||||
// The extended header lines, before any hunk.
|
||||
if (line.startsWith("new file mode")) file.status = "added";
|
||||
else if (line.startsWith("deleted file mode")) file.status = "deleted";
|
||||
else if (line.startsWith("rename from ")) {
|
||||
file.status = "renamed";
|
||||
file.oldPath = linePath(line.slice("rename from ".length));
|
||||
} else if (line.startsWith("rename to ")) {
|
||||
file.status = "renamed";
|
||||
file.path = linePath(line.slice("rename to ".length));
|
||||
} else if (line.startsWith("--- ")) {
|
||||
const p = linePath(line.slice(4));
|
||||
if (p !== "/dev/null") file.oldPath = strip(p, "a/");
|
||||
} else if (line.startsWith("+++ ")) {
|
||||
const p = linePath(line.slice(4));
|
||||
if (p !== "/dev/null") file.path = strip(p, "b/");
|
||||
} else if (/^Binary files .* differ$/.test(line) || line === "GIT binary patch") file.binary = true;
|
||||
else if (line === HIDDEN_LINE) file.hidden = true;
|
||||
}
|
||||
close();
|
||||
// A change set too big for git to print at all comes as every file's name
|
||||
// first and the too-large line last (changes.mjs): no file follows the
|
||||
// line, and each file it names is listed only.
|
||||
if (tooLarge && !files.some((f) => f.afterCut)) {
|
||||
for (const f of files) if (!f.hunks.length && !f.binary && !f.hidden) f.truncated = true;
|
||||
}
|
||||
// A hidden or cut file has no lines to count: changes.mjs sends its counts as trailer lines.
|
||||
for (const line of lines) {
|
||||
const m = /^# counts: (\d+)\t(\d+)\t(.+)$/.exec(line);
|
||||
if (!m) continue;
|
||||
for (const f of files) {
|
||||
if (f.path === m[3] && !f.hunks.length) {
|
||||
f.added = Number(m[1]);
|
||||
f.removed = Number(m[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const f of files) delete f.afterCut;
|
||||
// Text with no file section at all is not a diff.
|
||||
if (!files.length && text.trim()) return { files: [], tooLarge: false, bad: true };
|
||||
return { files, tooLarge };
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
// Plan2Code Web Console - the Changes tab: a diff block drawn as a file list and one unified diff; defines only.
|
||||
//
|
||||
// The block is `{ id, diff: "changes.diff", text }` (or `{ id, error }` when
|
||||
// the agent could not read the changes). Diff text is file content from the
|
||||
// repository, so every node is built with el() and filled with textContent:
|
||||
// nothing from it ever goes through innerHTML or md().
|
||||
|
||||
import { el } from "./render.js";
|
||||
import { parseDiff } from "./diff-parse.js";
|
||||
import { agentWorking, workingMs, patience, WORKING_GRACE_MS } from "./selectors.js";
|
||||
import { hooks, page, redraw } from "./state.js";
|
||||
import { sendState } from "./send-state.js";
|
||||
|
||||
// The picked file and the scroll of each pane, per diff block, so a redraw
|
||||
// of the same block keeps the person where they were.
|
||||
const picks = new Map();
|
||||
// The last parse of each block's text: a redraw does not parse again.
|
||||
const parsed = new Map();
|
||||
|
||||
const blockKey = (b) => String(b.id || "") + "|" + String(b.diff || "");
|
||||
|
||||
function parseOnce(b) {
|
||||
const key = blockKey(b);
|
||||
const hit = parsed.get(key);
|
||||
if (hit && hit.text === b.text) return hit.result;
|
||||
const result = parseDiff(b.text);
|
||||
parsed.set(key, { text: b.text, result });
|
||||
return result;
|
||||
}
|
||||
|
||||
const baseName = (p) => String(p || "").split("/").pop();
|
||||
const folderOf = (p) => {
|
||||
const parts = String(p || "").split("/");
|
||||
parts.pop();
|
||||
return parts.join("/");
|
||||
};
|
||||
|
||||
// What a rename row reads: old -> new, by name when the folder stayed.
|
||||
function renameLabel(f) {
|
||||
return folderOf(f.oldPath) === folderOf(f.path) ? `${baseName(f.oldPath)} → ${baseName(f.path)}` : `${f.oldPath} → ${f.path}`;
|
||||
}
|
||||
|
||||
const STATUS_WORD = { added: "Added", deleted: "Deleted", renamed: "Renamed" };
|
||||
|
||||
function counts(f) {
|
||||
const box = el("span", "diff-counts");
|
||||
box.appendChild(el("span", "diff-plus", `+${f.added}`));
|
||||
box.appendChild(el("span", "diff-minus", `-${f.removed}`));
|
||||
return box;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- states */
|
||||
|
||||
// Asks the agent to read the changes again. Refused whenever Send itself
|
||||
// would be (a finished or paused session, the loop building, no connection),
|
||||
// and, as the brief button is, while a turn is in flight.
|
||||
async function askAgain(button, status, reply) {
|
||||
const state = sendState(true);
|
||||
if (state.disabled || page.gone || page.pendingResult || (agentWorking() && workingMs() <= patience(WORKING_GRACE_MS))) {
|
||||
status.textContent = state.why || "Wait for Plan2Code to finish, then ask again.";
|
||||
return;
|
||||
}
|
||||
const label = button.textContent;
|
||||
button.disabled = true;
|
||||
button.textContent = "Asking…";
|
||||
try {
|
||||
const res = await fetch("/submit", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ actions: [{ i: "__changes", type: "changes" }], reply }),
|
||||
});
|
||||
if (res.status === 409) throw Object.assign(new Error("pending"), { pending: true });
|
||||
if (!res.ok) throw new Error(String(res.status));
|
||||
hooks.banner("Asked for the changes again. The tab updates when they are read.", "good");
|
||||
setTimeout(() => hooks.banner(null), 5000);
|
||||
page.pendingResult = true;
|
||||
redraw();
|
||||
} catch (err) {
|
||||
if (err && err.pending) page.pendingResult = true;
|
||||
status.textContent =
|
||||
err && err.pending ? "Your last answers are still being picked up. Try again in a moment." : "That did not go through. Try again in a moment.";
|
||||
} finally {
|
||||
button.disabled = false;
|
||||
button.textContent = label;
|
||||
}
|
||||
}
|
||||
|
||||
function stateBox(kind, message, buttonLabel, reply) {
|
||||
const box = el("div", "diff-state diff-" + kind);
|
||||
box.appendChild(el("p", "diff-state-msg", message));
|
||||
if (buttonLabel) {
|
||||
const btn = el("button", "btn small diff-again", buttonLabel);
|
||||
btn.type = "button";
|
||||
const status = el("p", "diff-state-why");
|
||||
status.setAttribute("role", "status");
|
||||
btn.addEventListener("click", () => askAgain(btn, status, reply));
|
||||
box.appendChild(btn);
|
||||
box.appendChild(status);
|
||||
}
|
||||
return box;
|
||||
}
|
||||
|
||||
function drawLoading() {
|
||||
const view = el("div", "diff-view is-loading");
|
||||
view.setAttribute("aria-busy", "true");
|
||||
view.appendChild(el("p", "diff-totals", "Reading the changes…"));
|
||||
const cols = el("div", "diff-cols");
|
||||
const list = el("div", "diff-list diff-skeleton");
|
||||
for (let i = 0; i < 4; i++) list.appendChild(el("span", "diff-skel-row"));
|
||||
const pane = el("div", "diff-pane diff-skeleton");
|
||||
for (let i = 0; i < 8; i++) pane.appendChild(el("span", "diff-skel-row"));
|
||||
cols.appendChild(list);
|
||||
cols.appendChild(pane);
|
||||
view.appendChild(cols);
|
||||
return view;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- ready */
|
||||
|
||||
function drawHunks(f) {
|
||||
if (f.binary) return el("p", "diff-plain", "Binary file: no lines to show");
|
||||
if (f.hidden) return el("p", "diff-plain", "Lines hidden: this file may hold a secret");
|
||||
if (f.truncated) return el("p", "diff-plain", "Too large to show in full: this file is listed only");
|
||||
if (!f.hunks.length) return el("p", "diff-plain", f.status === "renamed" ? "Renamed only: no lines changed" : "No lines changed");
|
||||
const code = el("div", "diff-code");
|
||||
const rows = el("div", "diff-rows");
|
||||
for (const h of f.hunks) {
|
||||
rows.appendChild(el("div", "diff-hunk", h.header));
|
||||
for (const l of h.lines) {
|
||||
const row = el("div", "diff-line " + l.kind);
|
||||
row.appendChild(el("span", "diff-no", l.oldNo == null ? "" : String(l.oldNo)));
|
||||
row.appendChild(el("span", "diff-no", l.newNo == null ? "" : String(l.newNo)));
|
||||
row.appendChild(el("span", "diff-sign", l.kind === "add" ? "+" : l.kind === "del" ? "-" : ""));
|
||||
row.appendChild(el("code", "diff-text", l.text));
|
||||
rows.appendChild(row);
|
||||
}
|
||||
}
|
||||
code.appendChild(rows);
|
||||
return code;
|
||||
}
|
||||
|
||||
function drawPane(f) {
|
||||
const pane = el("section", "diff-pane");
|
||||
pane.setAttribute("aria-label", "Lines of " + f.path);
|
||||
const head = el("header", "diff-pane-head");
|
||||
head.appendChild(el("strong", "diff-pane-path", f.status === "renamed" ? `${f.oldPath} → ${f.path}` : f.path));
|
||||
head.appendChild(counts(f));
|
||||
pane.appendChild(head);
|
||||
pane.appendChild(drawHunks(f));
|
||||
return pane;
|
||||
}
|
||||
|
||||
function drawReady(b, result) {
|
||||
const key = blockKey(b);
|
||||
const files = result.files;
|
||||
const memory = picks.get(key) || {};
|
||||
let picked = files.findIndex((f) => f.path === memory.path);
|
||||
if (picked < 0) picked = 0;
|
||||
|
||||
const view = el("div", "diff-view");
|
||||
const added = files.reduce((n, f) => n + f.added, 0);
|
||||
const removed = files.reduce((n, f) => n + f.removed, 0);
|
||||
const totals = el("p", "diff-totals");
|
||||
totals.appendChild(el("span", null, `${files.length} ${files.length === 1 ? "file" : "files"} changed, `));
|
||||
totals.appendChild(el("span", "diff-plus", `+${added}`));
|
||||
totals.appendChild(document.createTextNode(" "));
|
||||
totals.appendChild(el("span", "diff-minus", `-${removed}`));
|
||||
view.appendChild(totals);
|
||||
if (result.tooLarge) {
|
||||
view.appendChild(el("p", "diff-notice", "This change is too large to show in full: every file is listed, but some show no lines."));
|
||||
}
|
||||
|
||||
const cols = el("div", "diff-cols");
|
||||
const list = el("ul", "diff-list");
|
||||
list.setAttribute("role", "listbox");
|
||||
list.setAttribute("aria-label", "Changed files");
|
||||
list.tabIndex = 0;
|
||||
const idBase = "diff-" + String(b.id || "block").replace(/[^A-Za-z0-9_-]/g, "") + "-";
|
||||
const rows = files.map((f, i) => {
|
||||
const row = el("li", "diff-file");
|
||||
row.id = idBase + i;
|
||||
row.setAttribute("role", "option");
|
||||
const name = el("span", "diff-file-name");
|
||||
name.appendChild(el("strong", null, f.status === "renamed" ? renameLabel(f) : baseName(f.path)));
|
||||
if (STATUS_WORD[f.status]) name.appendChild(el("span", "diff-status diff-status-" + f.status, STATUS_WORD[f.status]));
|
||||
row.appendChild(name);
|
||||
const folder = folderOf(f.path);
|
||||
if (folder) row.appendChild(el("span", "diff-file-dir", folder));
|
||||
row.appendChild(counts(f));
|
||||
row.addEventListener("click", () => pick(i, false));
|
||||
list.appendChild(row);
|
||||
return row;
|
||||
});
|
||||
cols.appendChild(list);
|
||||
let pane = drawPane(files[picked]);
|
||||
cols.appendChild(pane);
|
||||
view.appendChild(cols);
|
||||
|
||||
const remember = () => {
|
||||
picks.set(key, {
|
||||
path: files[picked].path,
|
||||
listTop: list.scrollTop,
|
||||
paneTop: pane.scrollTop,
|
||||
paneLeft: pane.scrollLeft,
|
||||
});
|
||||
};
|
||||
const watch = () => pane.addEventListener("scroll", remember, { passive: true });
|
||||
function mark() {
|
||||
rows.forEach((r, i) => {
|
||||
r.setAttribute("aria-selected", i === picked ? "true" : "false");
|
||||
r.classList.toggle("is-picked", i === picked);
|
||||
});
|
||||
list.setAttribute("aria-activedescendant", rows[picked].id);
|
||||
}
|
||||
function pick(i, fromKeys) {
|
||||
if (i < 0 || i >= files.length) return;
|
||||
const changed = i !== picked;
|
||||
picked = i;
|
||||
mark();
|
||||
if (changed) {
|
||||
const next = drawPane(files[picked]);
|
||||
pane.replaceWith(next);
|
||||
pane = next;
|
||||
watch();
|
||||
}
|
||||
if (fromKeys) rows[picked].scrollIntoView({ block: "nearest" });
|
||||
remember();
|
||||
}
|
||||
list.addEventListener("keydown", (e) => {
|
||||
const step = { ArrowDown: 1, ArrowUp: -1 }[e.key];
|
||||
const jump = { Home: 0, End: files.length - 1 }[e.key];
|
||||
if (step === undefined && jump === undefined) return;
|
||||
e.preventDefault();
|
||||
pick(step !== undefined ? picked + step : jump, true);
|
||||
});
|
||||
list.addEventListener("scroll", remember, { passive: true });
|
||||
watch();
|
||||
mark();
|
||||
|
||||
// Back to where the person was once the view is on the page (a fresh
|
||||
// element has no layout to scroll yet).
|
||||
if (memory.path === files[picked].path) {
|
||||
requestAnimationFrame(() => {
|
||||
list.scrollTop = memory.listTop || 0;
|
||||
pane.scrollTop = memory.paneTop || 0;
|
||||
pane.scrollLeft = memory.paneLeft || 0;
|
||||
});
|
||||
}
|
||||
picks.set(key, { ...memory, path: files[picked].path });
|
||||
return view;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------- entry */
|
||||
|
||||
// One diff block as the Changes tab. `doc` is the block's document, when
|
||||
// known: a doc marked stale is still being read again.
|
||||
export function drawDiff(b, doc) {
|
||||
if (typeof b.error === "string" && b.error.trim()) {
|
||||
return wrap(stateBox("error", `The changes could not be read: ${b.error.trim()}`, "Reload the changes", "Please refresh the Changes tab."));
|
||||
}
|
||||
if (typeof b.text !== "string" || (doc && doc.stale)) return drawLoading();
|
||||
const result = parseOnce(b);
|
||||
if (result.bad) {
|
||||
return wrap(stateBox("error", "The changes could not be read: the diff is not in a shape this page can draw.", "Reload the changes", "Please refresh the Changes tab."));
|
||||
}
|
||||
if (!result.files.length) {
|
||||
return wrap(stateBox("empty", "Nothing has changed since this run started.", "Check again", "Please refresh the Changes tab."));
|
||||
}
|
||||
return drawReady(b, result);
|
||||
}
|
||||
|
||||
function wrap(box) {
|
||||
const view = el("div", "diff-view");
|
||||
view.appendChild(box);
|
||||
return view;
|
||||
}
|
||||
@@ -3,10 +3,14 @@
|
||||
// Shared by the documents tab and the finished screen, so neither imports the other.
|
||||
|
||||
import { el, md } from "./render.js";
|
||||
import { drawDiff } from "./diff-view.js";
|
||||
|
||||
// A block's content: its markdown, or for an image block (a screenshot the
|
||||
// agent saved under the session's shots/ folder) a captioned picture.
|
||||
export function blockBody(b) {
|
||||
// A block's content: its markdown, for an image block (a screenshot the
|
||||
// agent saved under the session's shots/ folder) a captioned picture, and for
|
||||
// a diff block (changes.mjs diff) the Changes tab. `doc` is the block's
|
||||
// document, when known.
|
||||
export function blockBody(b, doc) {
|
||||
if (isDiffBlock(b)) return drawDiff(b, doc);
|
||||
if (typeof b.image !== "string") return md(b.md, "md");
|
||||
const fig = el("figure", "doc-figure");
|
||||
const img = el("img", "doc-img");
|
||||
@@ -27,13 +31,31 @@ export function downloadDoc(doc) {
|
||||
// assumptions or drafts. Without the marks a half-finished document reads,
|
||||
// once it leaves the page, as a set of decisions.
|
||||
const mark = { assumed: "> **Assumed for now, still open.**\n\n", draft: "> **Draft.**\n\n" };
|
||||
const blocks = (doc.blocks || []).map((b) => (mark[b.state] || "") + (typeof b.image === "string" ? b.alt || "" : b.md || ""));
|
||||
const blocks = (doc.blocks || []).map((b) => (mark[b.state] || "") + blockText(b));
|
||||
const first = (doc.blocks || [])[0];
|
||||
const titled = first && /^\s*#\s/.test(first.md || "");
|
||||
const body = [titled || !doc.title ? null : `# ${doc.title}`, ...blocks].filter((x) => x != null).join("\n\n");
|
||||
saveFile(`${docFileName(doc)}.md`, body, "text/markdown");
|
||||
}
|
||||
|
||||
// A diff block names its .diff file, or carries only the agent's reason it
|
||||
// could not read one.
|
||||
function isDiffBlock(b) {
|
||||
return typeof b.diff === "string" || (typeof b.error === "string" && b.md == null && b.image == null);
|
||||
}
|
||||
|
||||
// What one block contributes to a saved file: a diff block's text as a fenced
|
||||
// diff (its error line when it has none), an image's caption, else markdown.
|
||||
function blockText(b) {
|
||||
if (isDiffBlock(b)) {
|
||||
if (typeof b.error === "string" && b.error.trim()) return `The changes could not be read: ${b.error.trim()}`;
|
||||
if (typeof b.text !== "string") return "";
|
||||
const fence = b.text.includes("```") ? "~~~~" : "```";
|
||||
return `${fence}diff\n${b.text.replace(/\n$/, "")}\n${fence}`;
|
||||
}
|
||||
return typeof b.image === "string" ? b.alt || "" : b.md || "";
|
||||
}
|
||||
|
||||
function docFileName(doc) {
|
||||
const name = (doc.title || "document").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
||||
return name || "document";
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
<link rel="modulepreload" href="/spec-states.js" />
|
||||
<link rel="modulepreload" href="/state.js" />
|
||||
<link rel="modulepreload" href="/doc-files.js" />
|
||||
<link rel="modulepreload" href="/diff-view.js" />
|
||||
<link rel="modulepreload" href="/diff-parse.js" />
|
||||
<link rel="modulepreload" href="/send-state.js" />
|
||||
<link rel="modulepreload" href="/mention-box.js" />
|
||||
<link rel="modulepreload" href="/uploads.js" />
|
||||
|
||||
@@ -191,7 +191,7 @@ export function renderDoc(main, id) {
|
||||
box.appendChild(el("span", "block-tag", "Assumed for now, still open"));
|
||||
}
|
||||
if (b.state === "draft") box.appendChild(el("span", "block-tag", "Draft"));
|
||||
box.appendChild(blockBody(b));
|
||||
box.appendChild(blockBody(b, doc));
|
||||
main.appendChild(box);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/* diff.css: The Changes tab, a diff block drawn as a file list and one unified diff. */
|
||||
|
||||
/* Every color is a token, so light and dark both come from tokens.css. Added
|
||||
lines take the success pair (--ok, --ok-soft), removed lines the danger
|
||||
pair (--warn, --warn-soft). */
|
||||
|
||||
.diff-view { --diff-gutter: 3.4em; --diff-sign: 1.6em; min-width: 0; }
|
||||
|
||||
.diff-totals { margin: 0 0 10px; color: var(--ink-2); font-size: 13.5px; font-variant-numeric: tabular-nums; }
|
||||
.diff-notice {
|
||||
margin: 0 0 12px; padding: 8px 12px; border: 1px solid var(--stage); border-radius: 8px;
|
||||
background: var(--stage-soft); color: var(--ink); font-size: 13px;
|
||||
}
|
||||
.diff-plus { color: var(--ok); font-weight: 600; }
|
||||
.diff-minus { color: var(--warn); font-weight: 600; }
|
||||
.diff-counts { display: inline-flex; gap: 6px; font-size: 12px; font-variant-numeric: tabular-nums; }
|
||||
|
||||
/* Two columns, each scrolling inside itself: the tab never grows past the
|
||||
window, so the doc's head (with its way back to the sign-off card) stays in
|
||||
view however long the diff is. */
|
||||
.diff-cols {
|
||||
display: grid; grid-template-columns: 240px minmax(0, 1fr); gap: 12px; align-items: start;
|
||||
--diff-max: max(260px, calc(100vh - 280px));
|
||||
}
|
||||
|
||||
.diff-list {
|
||||
list-style: none; margin: 0; padding: 6px; max-height: var(--diff-max); overflow-y: auto; overscroll-behavior: contain;
|
||||
background: var(--panel); border: 1px solid var(--rule); border-radius: var(--radius);
|
||||
}
|
||||
.diff-list:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
|
||||
.diff-file {
|
||||
display: grid; gap: 2px; padding: 7px 10px; border-radius: calc(var(--radius) / 2); cursor: pointer;
|
||||
}
|
||||
.diff-file:hover { background: var(--panel-2); }
|
||||
.diff-file.is-picked { background: var(--accent-soft); box-shadow: inset 3px 0 var(--accent); }
|
||||
.diff-file-name { display: flex; flex-wrap: wrap; gap: 6px; align-items: baseline; min-width: 0; }
|
||||
.diff-file-name strong { font-weight: 600; overflow-wrap: anywhere; }
|
||||
.diff-file-dir { color: var(--ink-3); font-size: 12px; overflow-wrap: anywhere; }
|
||||
.diff-status {
|
||||
font: 700 10px/1.4 var(--mono); letter-spacing: 0.04em; text-transform: uppercase;
|
||||
padding: 0 5px; border-radius: 4px; border: 1px solid var(--rule-2); color: var(--ink-2);
|
||||
}
|
||||
.diff-status-added { border-color: var(--ok); color: var(--ok); }
|
||||
.diff-status-deleted { border-color: var(--warn); color: var(--warn); }
|
||||
.diff-status-renamed { border-color: var(--accent); color: var(--accent); }
|
||||
|
||||
/* The pane scrolls both ways, so the sideways scrollbar for a long line sits
|
||||
at the bottom of the pane, never under the file's last row. */
|
||||
.diff-pane {
|
||||
min-width: 0; max-height: var(--diff-max); overflow: auto; overscroll-behavior: contain;
|
||||
background: var(--panel); border: 1px solid var(--rule); border-radius: var(--radius);
|
||||
}
|
||||
.diff-pane-head {
|
||||
display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap;
|
||||
padding: 9px 14px; border-bottom: 1px solid var(--rule);
|
||||
position: sticky; top: 0; left: 0; z-index: 1; background: var(--panel);
|
||||
}
|
||||
.diff-pane-path { font-weight: 600; overflow-wrap: anywhere; }
|
||||
|
||||
/* Long lines scroll sideways with the pane, all rows together, never the page. */
|
||||
.diff-rows { display: grid; min-width: 100%; width: max-content; font: 12px/1.55 var(--mono); }
|
||||
.diff-hunk { padding: 3px 14px; color: var(--ink-3); background: var(--panel-2); white-space: pre; }
|
||||
.diff-line { display: grid; grid-template-columns: var(--diff-gutter) var(--diff-gutter) var(--diff-sign) auto; }
|
||||
.diff-no { text-align: right; padding-right: 8px; color: var(--ink-3); font-variant-numeric: tabular-nums; user-select: none; }
|
||||
.diff-sign { text-align: center; color: var(--ink-3); user-select: none; }
|
||||
.diff-text { font: inherit; white-space: pre; padding-right: 14px; color: var(--ink); background: none; border: 0; }
|
||||
.diff-line.add { background: var(--ok-soft); }
|
||||
.diff-line.add .diff-sign { color: var(--ok); font-weight: 700; }
|
||||
.diff-line.del { background: var(--warn-soft); }
|
||||
.diff-line.del .diff-sign { color: var(--warn); font-weight: 700; }
|
||||
.diff-line.meta .diff-text { color: var(--ink-3); font-style: italic; }
|
||||
|
||||
.diff-plain { margin: 0; padding: 16px 14px; color: var(--ink-2); }
|
||||
|
||||
/* Empty and error: a plain line and a button to ask again. */
|
||||
.diff-state { padding: 16px 18px; border: 1px solid var(--rule); border-radius: var(--radius); background: var(--panel); }
|
||||
.diff-state.diff-error { border-color: var(--warn); background: var(--warn-soft); }
|
||||
.diff-state-msg { margin: 0 0 12px; }
|
||||
.diff-state-why { margin: 8px 0 0; color: var(--ink-2); font-size: 13px; }
|
||||
.diff-state-why:empty { display: none; }
|
||||
|
||||
/* Loading: muted rows where the list and the lines will be. */
|
||||
.diff-skeleton { display: grid; gap: 10px; padding: 14px; }
|
||||
.diff-skel-row { display: block; height: 14px; border-radius: 4px; background: var(--rule); }
|
||||
.diff-pane.diff-skeleton .diff-skel-row:nth-child(3n) { width: 70%; }
|
||||
.diff-pane.diff-skeleton .diff-skel-row:nth-child(4n + 1) { width: 85%; }
|
||||
|
||||
/* Narrow window: the file list stacks above the diff as a short list. */
|
||||
@media (max-width: 720px) {
|
||||
.diff-cols { grid-template-columns: minmax(0, 1fr); --diff-max: max(240px, calc(100vh - 340px)); }
|
||||
.diff-list { max-height: 152px; }
|
||||
}
|
||||
|
||||
@media print {
|
||||
.diff-list, .diff-pane { max-height: none; overflow: visible; }
|
||||
.diff-text { white-space: pre-wrap; overflow-wrap: anywhere; }
|
||||
.diff-rows { width: auto; }
|
||||
}
|
||||
Reference in new issue
Block a user