Public: anyone can read this; the crew can change it.
Undo covers every change and says what it reversed #32
scout/tempo:scout/23-undo-covers-every-change-and-says-what-i → main 2 commits fork
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 | ||
| 34 | 34 | round trip) and keeps itself in the URL, so a filtered view is |
| 35 | 35 | shareable too. |
| 36 | 36 | - **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. | |
| 39 | 43 | - **Live**: `static/board.js` opens `/b/<id>/events` (server-sent |
| 40 | 44 | events); when someone else changes the board, your page quietly |
| 41 | 45 | 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 | ||
| 62 | 62 | const afterMove = db.listCards(boardId).filter((c) => c.column_id === columnId); |
| 63 | 63 | expect(afterMove.length).toBe(before.length + 1); |
| 64 | 64 | |
| 65 | expect(db.undo(boardId)).toBe(true); | |
| 65 | expect(db.undo(boardId)).not.toBeNull(); | |
| 66 | 66 | const afterUndo = db.listCards(boardId).filter((c) => c.column_id === columnId && c.id !== mover.id); |
| 67 | 67 | expect(afterUndo.map((c) => [c.id, c.position])).toEqual(before.map((c) => [c.id, c.position])); |
| 68 | 68 | expect(db.getCard(boardId, mover.id)!.position).toBe(moverBefore.position); |
| @@ -82,8 +82,8 @@ test("ties are ordered consistently by position then id", () => { | ||
| 82 | 82 | expect(second.id).toBe(expectedSecond); |
| 83 | 83 | }); |
| 84 | 84 | |
| 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(); | |
| 87 | 87 | }); |
| 88 | 88 | |
| 89 | 89 | // 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 { | ||
| 88 | 88 | type Payload = { |
| 89 | 89 | restoreColumns?: Column[]; |
| 90 | 90 | restoreCards?: Card[]; |
| 91 | restoreBoards?: Board[]; | |
| 91 | 92 | deleteColumnIds?: string[]; |
| 92 | 93 | 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; | |
| 93 | 99 | }; |
| 94 | 100 | |
| 95 | 101 | function logAction(boardId: string, payload: Payload): void { |
| @@ -140,8 +146,14 @@ export function claimBoard(boardId: string, owner: { id: string; name: string }) | ||
| 140 | 146 | return "claimed"; |
| 141 | 147 | } |
| 142 | 148 | |
| 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(); | |
| 145 | 157 | } |
| 146 | 158 | |
| 147 | 159 | export function listColumns(boardId: string): Column[] { |
| @@ -176,7 +188,7 @@ export function createColumn(boardId: string, name: string, explicitPosition?: n | ||
| 176 | 188 | name, |
| 177 | 189 | position, |
| 178 | 190 | ); |
| 179 | logAction(boardId, { deleteColumnIds: [id] }); | |
| 191 | logAction(boardId, { deleteColumnIds: [id], label: `added the column "${name}"` }); | |
| 180 | 192 | return { id, board_id: boardId, name, position }; |
| 181 | 193 | }).immediate(); |
| 182 | 194 | } |
| @@ -192,7 +204,7 @@ export function renameColumn(boardId: string, columnId: string, name: string): b | ||
| 192 | 204 | const before = getColumn(boardId, columnId); |
| 193 | 205 | if (!before) return false; |
| 194 | 206 | 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}"` }); | |
| 196 | 208 | return true; |
| 197 | 209 | }).immediate(); |
| 198 | 210 | } |
| @@ -207,7 +219,7 @@ export function moveColumn(boardId: string, columnId: string, direction: "left" | ||
| 207 | 219 | const before = [columns[i], columns[j]]; |
| 208 | 220 | db.query("UPDATE board_column SET position = ?2 WHERE id = ?1").run(columns[i].id, columns[j].position); |
| 209 | 221 | 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}"` }); | |
| 211 | 223 | return true; |
| 212 | 224 | }).immediate(); |
| 213 | 225 | } |
| @@ -218,7 +230,7 @@ export function deleteColumn(boardId: string, columnId: string): boolean { | ||
| 218 | 230 | if (!before) return false; |
| 219 | 231 | const cards = listCards(boardId).filter((c) => c.column_id === columnId); |
| 220 | 232 | 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}"` }); | |
| 222 | 234 | return true; |
| 223 | 235 | }).immediate(); |
| 224 | 236 | } |
| @@ -236,7 +248,7 @@ export function createCard(boardId: string, columnId: string, title: string): Ca | ||
| 236 | 248 | title, |
| 237 | 249 | position, |
| 238 | 250 | ); |
| 239 | logAction(boardId, { deleteCardIds: [id] }); | |
| 251 | logAction(boardId, { deleteCardIds: [id], label: `added "${title}"` }); | |
| 240 | 252 | return getCard(boardId, id); |
| 241 | 253 | }).immediate(); |
| 242 | 254 | } |
| @@ -255,7 +267,7 @@ export function editCard(boardId: string, cardId: string, title: string): boolea | ||
| 255 | 267 | const before = getCard(boardId, cardId); |
| 256 | 268 | if (!before) return false; |
| 257 | 269 | 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}"` }); | |
| 259 | 271 | return true; |
| 260 | 272 | }).immediate(); |
| 261 | 273 | } |
| @@ -284,7 +296,7 @@ export function moveCard(boardId: string, cardId: string, columnId: string, inde | ||
| 284 | 296 | columnId, |
| 285 | 297 | position, |
| 286 | 298 | ); |
| 287 | logAction(boardId, { restoreCards: [before] }); | |
| 299 | logAction(boardId, { restoreCards: [before], label: `moved "${before.title}"` }); | |
| 288 | 300 | return true; |
| 289 | 301 | } |
| 290 | 302 | |
| @@ -301,7 +313,7 @@ export function moveCard(boardId: string, cardId: string, columnId: string, inde | ||
| 301 | 313 | columnId, |
| 302 | 314 | at + 1, |
| 303 | 315 | ); |
| 304 | logAction(boardId, { restoreCards }); | |
| 316 | logAction(boardId, { restoreCards, label: `moved "${before.title}"` }); | |
| 305 | 317 | return true; |
| 306 | 318 | }).immediate(); |
| 307 | 319 | } |
| @@ -311,29 +323,44 @@ export function deleteCard(boardId: string, cardId: string): boolean { | ||
| 311 | 323 | const before = getCard(boardId, cardId); |
| 312 | 324 | if (!before) return false; |
| 313 | 325 | db.query("DELETE FROM card WHERE id = ?1").run(cardId); |
| 314 | logAction(boardId, { restoreCards: [before] }); | |
| 326 | logAction(boardId, { restoreCards: [before], label: `deleted "${before.title}"` }); | |
| 315 | 327 | return true; |
| 316 | 328 | }).immediate(); |
| 317 | 329 | } |
| 318 | 330 | |
| 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; | |
| 325 | 348 | 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 | ); | |
| 326 | 352 | const restoreColumn = db.query( |
| 327 | 353 | "INSERT OR REPLACE INTO board_column (id, board_id, name, position) VALUES (?1, ?2, ?3, ?4)", |
| 328 | 354 | ); |
| 329 | 355 | const restoreCard = db.query( |
| 330 | 356 | "INSERT OR REPLACE INTO card (id, board_id, column_id, title, position, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", |
| 331 | 357 | ); |
| 358 | for (const b of payload.restoreBoards ?? []) restoreBoard.run(b.id, b.name, b.owner_id, b.owner_name, b.created_at); | |
| 332 | 359 | for (const c of payload.restoreColumns ?? []) restoreColumn.run(c.id, c.board_id, c.name, c.position); |
| 333 | 360 | 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); |
| 334 | 361 | for (const id of payload.deleteColumnIds ?? []) db.query("DELETE FROM board_column WHERE id = ?1").run(id); |
| 335 | 362 | for (const id of payload.deleteCardIds ?? []) db.query("DELETE FROM card WHERE id = ?1").run(id); |
| 336 | 363 | db.query("DELETE FROM action WHERE id = ?1").run(row.id); |
| 337 | return true; | |
| 364 | return payload.label ?? "a change"; | |
| 338 | 365 | }).immediate(); |
| 339 | 366 | } |
src/pages/board.ts +11 −6
| @@ -64,9 +64,10 @@ const styles = ` | ||
| 64 | 64 | .add-card input { flex: 1; } |
| 65 | 65 | .new-column { flex: none; width: 14rem; } |
| 66 | 66 | .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); | |
| 68 | 68 | padding: .5rem 1rem; border-radius: .5rem; display: none; } |
| 69 | 69 | #toast.show { display: block; } |
| 70 | #toast.error { background: var(--danger); color: var(--danger-fg); } | |
| 70 | 71 | dialog#shortcuts { border: 1px solid #8884; border-radius: .6rem; padding: 1rem 1.25rem; max-width: 24rem; |
| 71 | 72 | background: canvas; color: canvastext; } |
| 72 | 73 | dialog#shortcuts::backdrop { background: rgba(0, 0, 0, .4); } |
| @@ -140,7 +141,7 @@ ${list}`, | ||
| 140 | 141 | ); |
| 141 | 142 | } |
| 142 | 143 | |
| 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 }; | |
| 144 | 145 | |
| 145 | 146 | // A guest's board belongs to no one until someone signed in keeps it |
| 146 | 147 | // (POST /b/:id/claim — see routes.ts and db.ts's claimBoard). |
| @@ -150,7 +151,7 @@ function keepHtml(board: Board, who: Who): string { | ||
| 150 | 151 | return `<form class="keep" method="post" action="/b/${board.id}/claim"><button class="ghost">Keep this board</button></form>`; |
| 151 | 152 | } |
| 152 | 153 | |
| 153 | export function boardPage({ board, columns, cards, query, who }: BoardData): string { | |
| 154 | export function boardPage({ board, columns, cards, query, who, undoLabel }: BoardData): string { | |
| 154 | 155 | const q = query.trim().toLowerCase(); |
| 155 | 156 | const matches = (c: Card) => q === "" || c.title.toLowerCase().includes(q); |
| 156 | 157 | 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 | ||
| 223 | 224 | <input type="search" id="filter" name="q" value="${escape(query)}" placeholder="Filter cards"> |
| 224 | 225 | <button>Filter</button> |
| 225 | 226 | </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 | } | |
| 228 | 233 | </form> |
| 229 | 234 | </div> |
| 230 | 235 | <div class="board"> |
| @@ -251,5 +256,5 @@ const boardShortcuts: [string, string][] = [ | ||
| 251 | 256 | |
| 252 | 257 | // Named by content — a change to static/board.js gets a new hash here, |
| 253 | 258 | // 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"; | |
| 255 | 260 | 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 | ||
| 131 | 131 | const columns = db.listColumns(boardId); |
| 132 | 132 | const cards = db.listCards(boardId); |
| 133 | 133 | 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 })); | |
| 135 | 136 | } |
| 136 | 137 | |
| 137 | 138 | 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 | ||
| 153 | 154 | const data = await form(request); |
| 154 | 155 | const name = cleanText(data.get("name"), 100); |
| 155 | 156 | 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)); | |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | 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 } : {}); | |
| 162 | 163 | } |
| 163 | 164 | |
| 164 | 165 | 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", | ||
| 97 | 97 | |
| 98 | 98 | const deleted = await post(`${boardUrl}/cards/${cardId}/delete`); |
| 99 | 99 | 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"'); | |
| 101 | 103 | |
| 102 | 104 | const undoneDelete = await post(`${boardUrl}/undo`); |
| 103 | 105 | expect(undoneDelete.status).toBe(200); |
| @@ -115,7 +117,9 @@ test("creates, renames, reorders and deletes columns", async () => { | ||
| 115 | 117 | await post(`${boardUrl}/columns/${columnId}`, { name: "On hold" }); |
| 116 | 118 | const renamed = await (await fetch(base + boardUrl)).text(); |
| 117 | 119 | 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"'); | |
| 119 | 123 | |
| 120 | 124 | const before = await (await fetch(base + boardUrl)).text(); |
| 121 | 125 | 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 () => { | ||
| 127 | 131 | |
| 128 | 132 | const deleted = await post(`${boardUrl}/columns/${columnId}/delete`); |
| 129 | 133 | 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"'); | |
| 131 | 137 | }); |
| 132 | 138 | |
| 133 | 139 | 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 | ||
| 142 | 148 | expect(deleted.status).toBe(200); |
| 143 | 149 | const afterDelete = await (await fetch(base + boardUrl)).text(); |
| 144 | 150 | 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"'); | |
| 146 | 154 | |
| 147 | 155 | const undone = await post(`${boardUrl}/undo`); |
| 148 | 156 | expect(undone.status).toBe(200); |
| @@ -151,6 +159,78 @@ test("deleting a column removes its cards, and undo restores both together", asy | ||
| 151 | 159 | expect(afterUndo).toContain("Doomed card"); |
| 152 | 160 | }); |
| 153 | 161 | |
| 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 "Ship it""'); | |
| 189 | expect(beforeUndo).toContain('aria-label="Undo: added "Ship it""'); | |
| 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 "<b>"x"</b>""'); | |
| 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 "<b>" in the toast instead of being inert. | |
| 230 | expect(label).toBe('added "<b>"x"</b>"'); | |
| 231 | expect(body).not.toContain("<b>"); | |
| 232 | }); | |
| 233 | ||
| 154 | 234 | test("filters cards server-side by title", async () => { |
| 155 | 235 | const id = await newBoard(); |
| 156 | 236 | const boardUrl = `/b/${id}`; |
src/tty.ts +2 −1
| @@ -205,7 +205,8 @@ function handleBoardKey(key: string): void { | ||
| 205 | 205 | cardCursor = 0; |
| 206 | 206 | }); |
| 207 | 207 | } 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"; | |
| 209 | 210 | } |
| 210 | 211 | clampCursors(); |
| 211 | 212 | draw(); |
static/board.519253746.js → static/board.3610624154.js renamed +46 −25
| @@ -10,12 +10,14 @@ | ||
| 10 | 10 | |
| 11 | 11 | const toast = document.getElementById("toast"); |
| 12 | 12 | let toastTimer; |
| 13 | function showError(message) { | |
| 13 | function showToast(message, isError) { | |
| 14 | 14 | toast.textContent = message; |
| 15 | toast.classList.toggle("error", !!isError); | |
| 15 | 16 | toast.classList.add("show"); |
| 16 | 17 | clearTimeout(toastTimer); |
| 17 | 18 | toastTimer = setTimeout(() => toast.classList.remove("show"), 3000); |
| 18 | 19 | } |
| 20 | const showError = (message) => showToast(message, true); | |
| 19 | 21 | |
| 20 | 22 | // A random id for this tab, sent on every write so its own live event |
| 21 | 23 | // (see "live" below) can be told apart from everyone else's. |
| @@ -158,6 +160,17 @@ | ||
| 158 | 160 | const form = e.target; |
| 159 | 161 | const card = form.closest(".card"); |
| 160 | 162 | |
| 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 | ||
| 161 | 174 | if (form.classList.contains("edit") && card) { |
| 162 | 175 | e.preventDefault(); |
| 163 | 176 | const title = form.title.value.trim(); |
| @@ -287,33 +300,41 @@ | ||
| 287 | 300 | } |
| 288 | 301 | |
| 289 | 302 | // ---- 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 | ||
| 290 | 335 | // Guarded so this file still runs where EventSource doesn't exist (the |
| 291 | 336 | // drag test's synthetic environment); a real browser always has it. |
| 292 | 337 | 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 | } | |
| 317 | 338 | // A card mid-edit or mid-drag keeps its refresh until it's free again. |
| 318 | 339 | root.addEventListener("focusout", () => pending && refreshBoard()); |
| 319 | 340 | root.addEventListener("dragend", () => pending && refreshBoard()); |
new above
offline