Public: anyone can read this; the crew can change it.

Undo covers every change and says what it reversed #32

Closed opened this
Closed without merging

scout/tempo:scout/23-undo-covers-every-change-and-says-what-i → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
8 files changed +202 −63
README.md +6 −2
@@ -34,8 +34,12 @@ FORGE_APP_DATA=./data bun run src/tty.ts # the terminal interface
3434 round trip) and keeps itself in the URL, so a filtered view is
3535 shareable too.
3636 - **Undo**: reverses the last change to the board — a create, edit,
37 move, or delete of a card or column. It is a shared, board-wide undo
38 (there's no per-person history), which fits a board anyone can edit.
37 move, or delete of a card or column, or a rename of the board itself.
38 It is a shared, board-wide undo (there's no per-person history), which
39 fits a board anyone can edit. The button always says what it would
40 undo (`Undo: moved "Ship it"`, read from the action log before it's
41 pressed) and disables itself with "Nothing to undo" once the log is
42 empty; after it runs, a toast in the corner says what it reversed.
3943 - **Live**: `static/board.js` opens `/b/<id>/events` (server-sent
4044 events); when someone else changes the board, your page quietly
4145 refetches and swaps in the fresh columns — no reload, and a change
src/db.test.ts +3 −3
@@ -62,7 +62,7 @@ test("undo of a renormalising move restores every renormalised card, not just th
6262 const afterMove = db.listCards(boardId).filter((c) => c.column_id === columnId);
6363 expect(afterMove.length).toBe(before.length + 1);
6464
65 expect(db.undo(boardId)).toBe(true);
65 expect(db.undo(boardId)).not.toBeNull();
6666 const afterUndo = db.listCards(boardId).filter((c) => c.column_id === columnId && c.id !== mover.id);
6767 expect(afterUndo.map((c) => [c.id, c.position])).toEqual(before.map((c) => [c.id, c.position]));
6868 expect(db.getCard(boardId, mover.id)!.position).toBe(moverBefore.position);
@@ -82,8 +82,8 @@ test("ties are ordered consistently by position then id", () => {
8282 expect(second.id).toBe(expectedSecond);
8383 });
8484
85 test("undo returns false with nothing to undo", () => {
86 expect(db.undo("no-such-board")).toBe(false);
85 test("undo returns null with nothing to undo", () => {
86 expect(db.undo("no-such-board")).toBeNull();
8787 });
8888
8989 // Simulates the ssh terminal and the web server as two separate processes
src/db.ts +45 −18
@@ -88,8 +88,14 @@ function randomId(length: number): string {
8888 type Payload = {
8989 restoreColumns?: Column[];
9090 restoreCards?: Card[];
91 restoreBoards?: Board[];
9192 deleteColumnIds?: string[];
9293 deleteCardIds?: string[];
94 // A short human label for what the action did — "moved "Ship it"",
95 // "deleted the column "Doing"" — shown before and after undo. A row
96 // logged before this field existed has none; undo still applies it,
97 // it just has no label to report back.
98 label: string;
9399 };
94100
95101 function logAction(boardId: string, payload: Payload): void {
@@ -140,8 +146,14 @@ export function claimBoard(boardId: string, owner: { id: string; name: string })
140146 return "claimed";
141147 }
142148
143 export function renameBoard(boardId: string, name: string): void {
144 db.query("UPDATE board SET name = ?2 WHERE id = ?1").run(boardId, name);
149 export function renameBoard(boardId: string, name: string): boolean {
150 return db.transaction((): boolean => {
151 const before = getBoard(boardId);
152 if (!before) return false;
153 db.query("UPDATE board SET name = ?2 WHERE id = ?1").run(boardId, name);
154 logAction(boardId, { restoreBoards: [before], label: "renamed the board" });
155 return true;
156 }).immediate();
145157 }
146158
147159 export function listColumns(boardId: string): Column[] {
@@ -176,7 +188,7 @@ export function createColumn(boardId: string, name: string, explicitPosition?: n
176188 name,
177189 position,
178190 );
179 logAction(boardId, { deleteColumnIds: [id] });
191 logAction(boardId, { deleteColumnIds: [id], label: `added the column "${name}"` });
180192 return { id, board_id: boardId, name, position };
181193 }).immediate();
182194 }
@@ -192,7 +204,7 @@ export function renameColumn(boardId: string, columnId: string, name: string): b
192204 const before = getColumn(boardId, columnId);
193205 if (!before) return false;
194206 db.query("UPDATE board_column SET name = ?2 WHERE id = ?1").run(columnId, name);
195 logAction(boardId, { restoreColumns: [before] });
207 logAction(boardId, { restoreColumns: [before], label: `renamed the column "${before.name}"` });
196208 return true;
197209 }).immediate();
198210 }
@@ -207,7 +219,7 @@ export function moveColumn(boardId: string, columnId: string, direction: "left"
207219 const before = [columns[i], columns[j]];
208220 db.query("UPDATE board_column SET position = ?2 WHERE id = ?1").run(columns[i].id, columns[j].position);
209221 db.query("UPDATE board_column SET position = ?2 WHERE id = ?1").run(columns[j].id, columns[i].position);
210 logAction(boardId, { restoreColumns: before });
222 logAction(boardId, { restoreColumns: before, label: `moved the column "${columns[i].name}"` });
211223 return true;
212224 }).immediate();
213225 }
@@ -218,7 +230,7 @@ export function deleteColumn(boardId: string, columnId: string): boolean {
218230 if (!before) return false;
219231 const cards = listCards(boardId).filter((c) => c.column_id === columnId);
220232 db.query("DELETE FROM board_column WHERE id = ?1").run(columnId);
221 logAction(boardId, { restoreColumns: [before], restoreCards: cards });
233 logAction(boardId, { restoreColumns: [before], restoreCards: cards, label: `deleted the column "${before.name}"` });
222234 return true;
223235 }).immediate();
224236 }
@@ -236,7 +248,7 @@ export function createCard(boardId: string, columnId: string, title: string): Ca
236248 title,
237249 position,
238250 );
239 logAction(boardId, { deleteCardIds: [id] });
251 logAction(boardId, { deleteCardIds: [id], label: `added "${title}"` });
240252 return getCard(boardId, id);
241253 }).immediate();
242254 }
@@ -255,7 +267,7 @@ export function editCard(boardId: string, cardId: string, title: string): boolea
255267 const before = getCard(boardId, cardId);
256268 if (!before) return false;
257269 db.query("UPDATE card SET title = ?2, updated_at = datetime('now') WHERE id = ?1").run(cardId, title);
258 logAction(boardId, { restoreCards: [before] });
270 logAction(boardId, { restoreCards: [before], label: `edited "${before.title}"` });
259271 return true;
260272 }).immediate();
261273 }
@@ -284,7 +296,7 @@ export function moveCard(boardId: string, cardId: string, columnId: string, inde
284296 columnId,
285297 position,
286298 );
287 logAction(boardId, { restoreCards: [before] });
299 logAction(boardId, { restoreCards: [before], label: `moved "${before.title}"` });
288300 return true;
289301 }
290302
@@ -301,7 +313,7 @@ export function moveCard(boardId: string, cardId: string, columnId: string, inde
301313 columnId,
302314 at + 1,
303315 );
304 logAction(boardId, { restoreCards });
316 logAction(boardId, { restoreCards, label: `moved "${before.title}"` });
305317 return true;
306318 }).immediate();
307319 }
@@ -311,29 +323,44 @@ export function deleteCard(boardId: string, cardId: string): boolean {
311323 const before = getCard(boardId, cardId);
312324 if (!before) return false;
313325 db.query("DELETE FROM card WHERE id = ?1").run(cardId);
314 logAction(boardId, { restoreCards: [before] });
326 logAction(boardId, { restoreCards: [before], label: `deleted "${before.title}"` });
315327 return true;
316328 }).immediate();
317329 }
318330
319 export function undo(boardId: string): boolean {
320 return db.transaction((): boolean => {
321 const row = db
322 .query<{ id: number; payload: string }, [string]>("SELECT id, payload FROM action WHERE board_id = ?1 ORDER BY id DESC LIMIT 1")
323 .get(boardId);
324 if (!row) return false;
331 const lastActionRow = (boardId: string): { id: number; payload: string } | null =>
332 db
333 .query<{ id: number; payload: string }, [string]>("SELECT id, payload FROM action WHERE board_id = ?1 ORDER BY id DESC LIMIT 1")
334 .get(boardId);
335
336 /** What Undo would reverse, without reversing it — for the button's label. A row logged before labels existed has none. */
337 export function peekUndo(boardId: string): string | null {
338 const row = lastActionRow(boardId);
339 if (!row) return null;
340 const payload = JSON.parse(row.payload) as Payload;
341 return payload.label ?? "a change";
342 }
343
344 export function undo(boardId: string): string | null {
345 return db.transaction((): string | null => {
346 const row = lastActionRow(boardId);
347 if (!row) return null;
325348 const payload = JSON.parse(row.payload) as Payload;
349 const restoreBoard = db.query(
350 "INSERT OR REPLACE INTO board (id, name, owner_id, owner_name, created_at) VALUES (?1, ?2, ?3, ?4, ?5)",
351 );
326352 const restoreColumn = db.query(
327353 "INSERT OR REPLACE INTO board_column (id, board_id, name, position) VALUES (?1, ?2, ?3, ?4)",
328354 );
329355 const restoreCard = db.query(
330356 "INSERT OR REPLACE INTO card (id, board_id, column_id, title, position, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
331357 );
358 for (const b of payload.restoreBoards ?? []) restoreBoard.run(b.id, b.name, b.owner_id, b.owner_name, b.created_at);
332359 for (const c of payload.restoreColumns ?? []) restoreColumn.run(c.id, c.board_id, c.name, c.position);
333360 for (const c of payload.restoreCards ?? []) restoreCard.run(c.id, c.board_id, c.column_id, c.title, c.position, c.created_at, c.updated_at);
334361 for (const id of payload.deleteColumnIds ?? []) db.query("DELETE FROM board_column WHERE id = ?1").run(id);
335362 for (const id of payload.deleteCardIds ?? []) db.query("DELETE FROM card WHERE id = ?1").run(id);
336363 db.query("DELETE FROM action WHERE id = ?1").run(row.id);
337 return true;
364 return payload.label ?? "a change";
338365 }).immediate();
339366 }
src/pages/board.ts +11 −6
@@ -64,9 +64,10 @@ const styles = `
6464 .add-card input { flex: 1; }
6565 .new-column { flex: none; width: 14rem; }
6666 .new-column form { display: flex; gap: .3rem; }
67 #toast { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); background: var(--danger); color: var(--danger-fg);
67 #toast { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); background: var(--btn-bg); color: var(--btn-fg);
6868 padding: .5rem 1rem; border-radius: .5rem; display: none; }
6969 #toast.show { display: block; }
70 #toast.error { background: var(--danger); color: var(--danger-fg); }
7071 dialog#shortcuts { border: 1px solid #8884; border-radius: .6rem; padding: 1rem 1.25rem; max-width: 24rem;
7172 background: canvas; color: canvastext; }
7273 dialog#shortcuts::backdrop { background: rgba(0, 0, 0, .4); }
@@ -140,7 +141,7 @@ ${list}`,
140141 );
141142 }
142143
143 export type BoardData = { board: Board; columns: Column[]; cards: Card[]; query: string; who: Who };
144 export type BoardData = { board: Board; columns: Column[]; cards: Card[]; query: string; who: Who; undoLabel: string | null };
144145
145146 // A guest's board belongs to no one until someone signed in keeps it
146147 // (POST /b/:id/claim — see routes.ts and db.ts's claimBoard).
@@ -150,7 +151,7 @@ function keepHtml(board: Board, who: Who): string {
150151 return `<form class="keep" method="post" action="/b/${board.id}/claim"><button class="ghost">Keep this board</button></form>`;
151152 }
152153
153 export function boardPage({ board, columns, cards, query, who }: BoardData): string {
154 export function boardPage({ board, columns, cards, query, who, undoLabel }: BoardData): string {
154155 const q = query.trim().toLowerCase();
155156 const matches = (c: Card) => q === "" || c.title.toLowerCase().includes(q);
156157 const cardsFor = (columnId: string) => cards.filter((c) => c.column_id === columnId);
@@ -223,8 +224,12 @@ export function boardPage({ board, columns, cards, query, who }: BoardData): str
223224 <input type="search" id="filter" name="q" value="${escape(query)}" placeholder="Filter cards">
224225 <button>Filter</button>
225226 </form>
226 <form method="post" action="/b/${board.id}/undo">
227 <button class="ghost">Undo</button>
227 <form class="undo" method="post" action="/b/${board.id}/undo">
228 ${
229 undoLabel === null
230 ? `<button class="ghost" disabled title="Nothing to undo" aria-label="Nothing to undo">Undo</button>`
231 : `<button class="ghost" title="Undo: ${escape(undoLabel)}" aria-label="Undo: ${escape(undoLabel)}">Undo</button>`
232 }
228233 </form>
229234 </div>
230235 <div class="board">
@@ -251,5 +256,5 @@ const boardShortcuts: [string, string][] = [
251256
252257 // Named by content — a change to static/board.js gets a new hash here,
253258 // so the year-long cache on /static/ never serves a stale script.
254 export const BOARD_JS = "board.519253746.js";
259 export const BOARD_JS = "board.3610624154.js";
255260 export const THEME_JS = "theme.1025564933.js";
src/routes.ts +5 −4
@@ -131,7 +131,8 @@ async function dispatch(request: Request, url: URL, env: string, who: Who): Prom
131131 const columns = db.listColumns(boardId);
132132 const cards = db.listCards(boardId);
133133 const query = url.searchParams.get("q") ?? "";
134 return html(boardPage({ board, columns, cards, query: query.slice(0, 200), who }));
134 const undoLabel = db.peekUndo(boardId);
135 return html(boardPage({ board, columns, cards, query: query.slice(0, 200), who, undoLabel }));
135136 }
136137
137138 if (method === "GET" && parts.length === 3 && parts[2] === "events") {
@@ -153,12 +154,12 @@ async function dispatch(request: Request, url: URL, env: string, who: Who): Prom
153154 const data = await form(request);
154155 const name = cleanText(data.get("name"), 100);
155156 if (!name) return bad("a board needs a name");
156 db.renameBoard(boardId, name);
157 return doneTouch(request, boardId, true);
157 return doneTouch(request, boardId, db.renameBoard(boardId, name));
158158 }
159159
160160 if (method === "POST" && parts.length === 3 && parts[2] === "undo") {
161 return doneTouch(request, boardId, db.undo(boardId));
161 const label = db.undo(boardId);
162 return doneTouch(request, boardId, label !== null, label !== null ? { label } : {});
162163 }
163164
164165 if (method === "POST" && parts.length === 3 && parts[2] === "columns") {
src/server.test.ts +84 −4
@@ -97,7 +97,9 @@ test("adds, moves, edits and deletes a card, and undo reverses the last change",
9797
9898 const deleted = await post(`${boardUrl}/cards/${cardId}/delete`);
9999 expect(deleted.status).toBe(200);
100 expect(await (await fetch(base + boardUrl)).text()).not.toContain("Write the README");
100 // "Write the README" still names the Undo button's target — the card
101 // itself must no longer be rendered.
102 expect(await (await fetch(base + boardUrl)).text()).not.toContain('data-title="Write the README"');
101103
102104 const undoneDelete = await post(`${boardUrl}/undo`);
103105 expect(undoneDelete.status).toBe(200);
@@ -115,7 +117,9 @@ test("creates, renames, reorders and deletes columns", async () => {
115117 await post(`${boardUrl}/columns/${columnId}`, { name: "On hold" });
116118 const renamed = await (await fetch(base + boardUrl)).text();
117119 expect(renamed).toContain("On hold");
118 expect(renamed).not.toContain("Blocked");
120 // "Blocked" still names the Undo button's target (it would restore that
121 // name) — the column itself must no longer be rendered with it.
122 expect(renamed).not.toContain('value="Blocked"');
119123
120124 const before = await (await fetch(base + boardUrl)).text();
121125 const order = [...before.matchAll(/data-column-id="([a-z0-9]{12})"/g)].map((m) => m[1]);
@@ -127,7 +131,9 @@ test("creates, renames, reorders and deletes columns", async () => {
127131
128132 const deleted = await post(`${boardUrl}/columns/${columnId}/delete`);
129133 expect(deleted.status).toBe(200);
130 expect(await (await fetch(base + boardUrl)).text()).not.toContain("On hold");
134 // "On hold" still names the Undo button's target — the column itself
135 // must no longer be rendered.
136 expect(await (await fetch(base + boardUrl)).text()).not.toContain('value="On hold"');
131137 });
132138
133139 test("deleting a column removes its cards, and undo restores both together", async () => {
@@ -142,7 +148,9 @@ test("deleting a column removes its cards, and undo restores both together", asy
142148 expect(deleted.status).toBe(200);
143149 const afterDelete = await (await fetch(base + boardUrl)).text();
144150 expect(afterDelete).not.toContain("Doomed card");
145 expect(afterDelete).not.toContain("To do");
151 // "To do" still names the Undo button's target (it would restore that
152 // column) — the column itself must no longer be rendered with it.
153 expect(afterDelete).not.toContain('value="To do"');
146154
147155 const undone = await post(`${boardUrl}/undo`);
148156 expect(undone.status).toBe(200);
@@ -151,6 +159,78 @@ test("deleting a column removes its cards, and undo restores both together", asy
151159 expect(afterUndo).toContain("Doomed card");
152160 });
153161
162 test("renaming a board is undoable, and undo says what it reversed", async () => {
163 const id = await newBoard("Original name");
164 const boardUrl = `/b/${id}`;
165
166 const renamed = await post(`${boardUrl}/rename`, { name: "New name" });
167 expect(renamed.status).toBe(200);
168 expect(await (await fetch(base + boardUrl)).text()).toContain("New name");
169
170 const undone = await post(`${boardUrl}/undo`);
171 expect(undone.status).toBe(200);
172 const { ok, label } = (await undone.json()) as { ok: boolean; label: string };
173 expect(ok).toBe(true);
174 expect(label).toBe("renamed the board");
175 const afterUndo = await (await fetch(base + boardUrl)).text();
176 expect(afterUndo).toContain("Original name");
177 expect(afterUndo).not.toContain('value="New name"');
178 });
179
180 test("a label round-trips through the action log into the JSON response and the rendered Undo button", async () => {
181 const id = await newBoard();
182 const boardUrl = `/b/${id}`;
183 const page = await (await fetch(base + boardUrl)).text();
184 const [, todoId] = page.match(/data-column-id="([a-z0-9]{12})"/)!;
185
186 await post(`${boardUrl}/columns/${todoId}/cards`, { title: "Ship it" });
187 const beforeUndo = await (await fetch(base + boardUrl)).text();
188 expect(beforeUndo).toContain('title="Undo: added &quot;Ship it&quot;"');
189 expect(beforeUndo).toContain('aria-label="Undo: added &quot;Ship it&quot;"');
190
191 const undone = await post(`${boardUrl}/undo`);
192 const { label } = (await undone.json()) as { label: string };
193 expect(label).toBe('added "Ship it"');
194 });
195
196 test("undo with nothing left to undo answers ok:false without throwing, and the button renders disabled", async () => {
197 const id = await newBoard();
198 const boardUrl = `/b/${id}`;
199
200 // A new board's three default columns are themselves undoable; drain
201 // that stack first so nothing is left.
202 for (let i = 0; i < 3; i++) expect((await post(`${boardUrl}/undo`)).status).toBe(200);
203
204 const page = await (await fetch(base + boardUrl)).text();
205 expect(page).toContain('title="Nothing to undo"');
206 expect(page).toMatch(/<button class="ghost" disabled title="Nothing to undo"/);
207
208 const undone = await post(`${boardUrl}/undo`);
209 expect(undone.status).toBe(404);
210 expect(await undone.json()).toEqual({ ok: false });
211 });
212
213 test("a card titled <b>\"x\"</b> is escaped in the Undo button's title attribute, and carried raw in the undo JSON", async () => {
214 const id = await newBoard();
215 const boardUrl = `/b/${id}`;
216 const page = await (await fetch(base + boardUrl)).text();
217 const [, todoId] = page.match(/data-column-id="([a-z0-9]{12})"/)!;
218
219 await post(`${boardUrl}/columns/${todoId}/cards`, { title: '<b>"x"</b>' });
220 const beforeUndo = await (await fetch(base + boardUrl)).text();
221 expect(beforeUndo).toContain('title="Undo: added &quot;&lt;b&gt;&quot;x&quot;&lt;/b&gt;&quot;"');
222 expect(beforeUndo).not.toContain('title="Undo: added "<b>"x"</b>""');
223
224 const undone = await post(`${boardUrl}/undo`);
225 const body = await undone.text();
226 const { label } = JSON.parse(body) as { label: string };
227 // The JSON payload carries the raw title — board.js writes it into the
228 // toast with textContent, which escapes on its own; pre-escaping here
229 // would show literal "&lt;b&gt;" in the toast instead of being inert.
230 expect(label).toBe('added "<b>"x"</b>"');
231 expect(body).not.toContain("&lt;b&gt;");
232 });
233
154234 test("filters cards server-side by title", async () => {
155235 const id = await newBoard();
156236 const boardUrl = `/b/${id}`;
src/tty.ts +2 −1
@@ -205,7 +205,8 @@ function handleBoardKey(key: string): void {
205205 cardCursor = 0;
206206 });
207207 } else if (key === "u") {
208 message = db.undo(boardId!) ? "" : "nothing to undo";
208 const label = db.undo(boardId!);
209 message = label ? `undid: ${label}` : "nothing to undo";
209210 }
210211 clampCursors();
211212 draw();
static/board.519253746.js → static/board.3610624154.js renamed +46 −25
@@ -10,12 +10,14 @@
1010
1111 const toast = document.getElementById("toast");
1212 let toastTimer;
13 function showError(message) {
13 function showToast(message, isError) {
1414 toast.textContent = message;
15 toast.classList.toggle("error", !!isError);
1516 toast.classList.add("show");
1617 clearTimeout(toastTimer);
1718 toastTimer = setTimeout(() => toast.classList.remove("show"), 3000);
1819 }
20 const showError = (message) => showToast(message, true);
1921
2022 // A random id for this tab, sent on every write so its own live event
2123 // (see "live" below) can be told apart from everyone else's.
@@ -158,6 +160,17 @@
158160 const form = e.target;
159161 const card = form.closest(".card");
160162
163 if (form.classList.contains("undo")) {
164 e.preventDefault();
165 post(`/b/${boardId}/undo`, {}).then(async (r) => {
166 if (!r.ok) return;
167 const { label } = await r.json();
168 showToast(`Undid: ${label}`);
169 refreshBoard();
170 });
171 return;
172 }
173
161174 if (form.classList.contains("edit") && card) {
162175 e.preventDefault();
163176 const title = form.title.value.trim();
@@ -287,33 +300,41 @@
287300 }
288301
289302 // ---- live: other tabs' changes appear here within a second, no reload ----
303 // refreshBoard() itself has no EventSource dependency — the undo handler
304 // below calls it too, so an undo's effect (and the board-wide Undo button
305 // it leaves behind) shows up without a full page navigation.
306 let pending = false;
307 async function refreshBoard() {
308 // Any input inside the board (a card's title, a new-card box, a column
309 // rename) keeps its refresh until the input loses focus — replacing it
310 // mid-word would wipe what someone is typing. A card itself focused
311 // for keyboard navigation (not editing) is not an input, so it does
312 // not defer; its focus is restored after the swap instead.
313 const focused = root.querySelector(".board :focus");
314 if (dragging || (focused && focused.tagName === "INPUT")) {
315 pending = true;
316 return;
317 }
318 pending = false;
319 const focusedCardId = focused && focused.classList.contains("card") ? focused.dataset.cardId : null;
320 const response = await fetch(location.pathname + location.search, { headers: { accept: "text/html" } });
321 if (!response.ok) return;
322 const fresh = new DOMParser().parseFromString(await response.text(), "text/html");
323 const next = fresh.querySelector(".board");
324 const current = root.querySelector(".board");
325 if (!next || !current) return;
326 current.replaceWith(next);
327 attachDropListeners();
328 if (focusedCardId) root.querySelector(`.card[data-card-id="${CSS.escape(focusedCardId)}"]`)?.focus();
329 // The Undo button describes the board's next undo, which just changed.
330 const nextUndo = fresh.querySelector("form.undo");
331 const currentUndo = root.querySelector("form.undo");
332 if (nextUndo && currentUndo) currentUndo.replaceWith(nextUndo);
333 }
334
290335 // Guarded so this file still runs where EventSource doesn't exist (the
291336 // drag test's synthetic environment); a real browser always has it.
292337 if (typeof EventSource !== "undefined") {
293 let pending = false;
294 async function refreshBoard() {
295 // Any input inside the board (a card's title, a new-card box, a column
296 // rename) keeps its refresh until the input loses focus — replacing it
297 // mid-word would wipe what someone is typing. A card itself focused
298 // for keyboard navigation (not editing) is not an input, so it does
299 // not defer; its focus is restored after the swap instead.
300 const focused = root.querySelector(".board :focus");
301 if (dragging || (focused && focused.tagName === "INPUT")) {
302 pending = true;
303 return;
304 }
305 pending = false;
306 const focusedCardId = focused && focused.classList.contains("card") ? focused.dataset.cardId : null;
307 const response = await fetch(location.pathname + location.search, { headers: { accept: "text/html" } });
308 if (!response.ok) return;
309 const fresh = new DOMParser().parseFromString(await response.text(), "text/html");
310 const next = fresh.querySelector(".board");
311 const current = root.querySelector(".board");
312 if (!next || !current) return;
313 current.replaceWith(next);
314 attachDropListeners();
315 if (focusedCardId) root.querySelector(`.card[data-card-id="${CSS.escape(focusedCardId)}"]`)?.focus();
316 }
317338 // A card mid-edit or mid-drag keeps its refresh until it's free again.
318339 root.addEventListener("focusout", () => pending && refreshBoard());
319340 root.addEventListener("dragend", () => pending && refreshBoard());

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs