Compare commits

...
Author SHA1 Message Date
Miguel Palhas 525dc9a04a fix(maestro): reuse draft forms across renders (#68)
render() destroyed and rebuilt every draft form, so an unrelated
session exiting tore down a draft mid-edit: focus, caret and
selection were lost and /api/info, /api/projects, /api/names were
refetched for a form already on screen.

Key draft forms by leaf id; rebuild only the pane DOM around them
and destroy a form when its leaf is removed or filled by a session.
2026-08-23 22:43:42 +01:00
Miguel Palhas 37338def71 merge: delete the spawn modal, amend DESIGN.md (#62) 2026-08-23 22:38:53 +01:00
2 changed files with 80 additions and 29 deletions
+25
View File
@@ -122,6 +122,31 @@ describe("multiview openDraft (#62)", () => {
expect(tab.focusedLeafId).toBe(empty); expect(tab.focusedLeafId).toBe(empty);
}); });
it("keeps the draft form instance when an unrelated session closes (#68)", async () => {
const layout = emptyLayout();
const tab = layout.tabs[0];
const s1 = splitLeaf(tab, "", "s1", "row", true);
const draftLeaf = splitLeaf(tab, s1, null, "row", true);
tab.focusedLeafId = draftLeaf;
const mv = makeMv(layout);
document.body.appendChild(mv.el);
mv.render();
await flush();
const nameInput = mv.el.querySelector(".mv-draft input[name=name]") as HTMLInputElement;
nameInput.value = "wip";
nameInput.dispatchEvent(new Event("input"));
await flush();
expect(leaves(tab.root).find((l) => l.id === draftLeaf)?.draft?.name).toBe("wip");
const formBefore = mv.el.querySelector(".mv-draft");
mv.closeSession("s1");
await flush();
expect(mv.el.querySelector(".mv-draft")).toBe(formBefore);
expect((mv.el.querySelector(".mv-draft input[name=name]") as HTMLInputElement).value).toBe("wip");
});
it("seeds a clone's cwd and command into the draft", async () => { it("seeds a clone's cwd and command into the draft", async () => {
const layout = emptyLayout(); const layout = emptyLayout();
const mv = makeMv(layout); const mv = makeMv(layout);
+55 -29
View File
@@ -70,9 +70,11 @@ export function createMultiView(opts: MultiViewOptions): MultiView {
const { pool, layout, sidebar, infoFor, onDisconnect, renderWelcome } = opts; const { pool, layout, sidebar, infoFor, onDisconnect, renderWelcome } = opts;
const persist = () => opts.onLayoutChange?.(); const persist = () => opts.onLayoutChange?.();
// Draft-pane forms (#57) carry global listeners (dir autocomplete), so each // Draft-pane forms (#57) carry global listeners (dir autocomplete), so forms
// render tears down the previous generation before rebuilding the canvas. // whose leaves are gone are destroyed on each render. Forms for draft leaves
const draftForms = new Set<SpawnFormHandle>(); // that survive the render are reused in place (#68): rebuilding one mid-edit
// would drop focus, caret and selection, and re-fetch info/projects/names.
const draftForms = new Map<string, SpawnFormHandle>();
const el = document.createElement("div"); const el = document.createElement("div");
el.className = "mv"; el.className = "mv";
@@ -150,30 +152,35 @@ export function createMultiView(opts: MultiViewOptions): MultiView {
// Draft pane (#57): an empty pane is a spawn form. Values hang off the // Draft pane (#57): an empty pane is a spawn form. Values hang off the
// leaf (never persisted) so each draft keeps its own half-filled state; // leaf (never persisted) so each draft keeps its own half-filled state;
// submitting fills this leaf with the new session in place. // submitting fills this leaf with the new session in place.
const form = createSpawnForm({ const form = draftForms.get(leaf.id);
seed: leaf.draft ?? undefined, if (form) {
class: "mv-draft", host.appendChild(form.el);
onChange: (values) => { } else {
leaf.draft = values; const created = createSpawnForm({
}, seed: leaf.draft ?? undefined,
onSuccess: (session) => { class: "mv-draft",
// The pane can be closed while the POST is in flight. Mounting the onChange: (values) => {
// session onto a leaf no longer in the tree would strand it in no leaf.draft = values;
// pane at all, so only claim the leaf while it is still there — },
// searching every tab, since the operator may have switched tabs onSuccess: (session) => {
// while the request was in flight. // The pane can be closed while the POST is in flight. Mounting the
const host = layout.tabs.find((t) => leaves(t.root).includes(leaf)); // session onto a leaf no longer in the tree would strand it in no
if (host) { // pane at all, so only claim the leaf while it is still there —
leaf.sessionId = session.id; // searching every tab, since the operator may have switched tabs
leaf.draft = null; // while the request was in flight.
host.focusedLeafId = leaf.id; const host = layout.tabs.find((t) => leaves(t.root).includes(leaf));
} if (host) {
render(); leaf.sessionId = session.id;
opts.onOpenSessionsChange?.(); leaf.draft = null;
}, host.focusedLeafId = leaf.id;
}); }
draftForms.add(form); render();
host.appendChild(form.el); opts.onOpenSessionsChange?.();
},
});
draftForms.set(leaf.id, created);
host.appendChild(created.el);
}
} }
const tools = document.createElement("div"); const tools = document.createElement("div");
@@ -395,9 +402,25 @@ export function createMultiView(opts: MultiViewOptions): MultiView {
// ---- top-level render ---- // ---- top-level render ----
/** Destroy forms whose leaf is no longer a draft (leaf removed or filled by
* a session). Surviving draft leaves keep their form instance (#68). */
function pruneForms(tab: { root: Node | null }): void {
const alive = new Set(
tab.root
? leaves(tab.root)
.filter((l) => !l.sessionId)
.map((l) => l.id)
: [],
);
for (const [id, f] of draftForms) {
if (!alive.has(id)) {
f.destroy();
draftForms.delete(id);
}
}
}
function render(): void { function render(): void {
for (const f of draftForms) f.destroy();
draftForms.clear();
renderTabs(); renderTabs();
canvas.replaceChildren(); canvas.replaceChildren();
const tab = activeTab(layout); const tab = activeTab(layout);
@@ -406,6 +429,7 @@ export function createMultiView(opts: MultiViewOptions): MultiView {
el.classList.toggle("mv-mobile-solo", isMobile && layout.tabs.length === 1); el.classList.toggle("mv-mobile-solo", isMobile && layout.tabs.length === 1);
if (!tab.root) { if (!tab.root) {
pruneForms(tab);
pool.setVisible([]); pool.setVisible([]);
pool.setFocus(null); pool.setFocus(null);
canvas.appendChild(renderWelcome()); canvas.appendChild(renderWelcome());
@@ -421,12 +445,14 @@ export function createMultiView(opts: MultiViewOptions): MultiView {
if (isMobile) { if (isMobile) {
renderMobile(tab); renderMobile(tab);
pruneForms(tab);
persist(); persist();
return; return;
} }
const hosts: { id: string; host: HTMLElement; info?: AttachInfo }[] = []; const hosts: { id: string; host: HTMLElement; info?: AttachInfo }[] = [];
canvas.appendChild(buildTree(tab.root, hosts)); canvas.appendChild(buildTree(tab.root, hosts));
pruneForms(tab);
pool.setVisible(hosts.map((h) => ({ id: h.id, pane: h.host, info: h.info }))); pool.setVisible(hosts.map((h) => ({ id: h.id, pane: h.host, info: h.info })));
syncFocus(); syncFocus();
persist(); persist();