Public: anyone can read this; the crew can change it.
A Talk reply reads cleanly while it streams: long code blocks scroll in their own box with Copy, long links wrap, and the reader's place holds #15919
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/15815-a-talk-reply-reads-cleanly-while-it-stre → main 0 commits fork 3 threads · 3 unresolved
3 files
changed +62
−7
src/web/pages/talk_client.ts +13 −1
| @@ -57,6 +57,7 @@ export const talkCss = /* css */ ` | ||
| 57 | 57 | .talk-turn p { margin: 0 0 8px; } |
| 58 | 58 | .talk-turn p:last-child { margin-bottom: 0; } |
| 59 | 59 | .talk-turn pre { max-width: 100%; overflow-x: auto; } |
| 60 | .talk-body a { overflow-wrap: anywhere; } | |
| 60 | 61 | .talk-body.prose { padding: 0; font-size: inherit; } |
| 61 | 62 | .talk-body.prose > * { max-width: none; } |
| 62 | 63 | .talk-body.prose table { display: block; max-width: 100%; overflow-x: auto; } |
| @@ -156,6 +157,15 @@ export const dictateJs = ` | ||
| 156 | 157 | } |
| 157 | 158 | `; |
| 158 | 159 | |
| 160 | /** | |
| 161 | * The follow rule: while a reply streams the view follows only when the reader is within 120 px | |
| 162 | * of the end; scrolled further up it stays put and "jump to latest" shows. Defines | |
| 163 | * `follows(viewHeight, scrollY, pageHeight)`. | |
| 164 | */ | |
| 165 | export const followJs = ` | |
| 166 | const follows = (viewHeight, scrollY, pageHeight) => viewHeight + scrollY >= pageHeight - 120; | |
| 167 | `; | |
| 168 | ||
| 159 | 169 | /** |
| 160 | 170 | * The unsent draft (#14488): one localStorage entry per conversation, `{t, v}` (time, text). `load` |
| 161 | 171 | * drops every talk draft older than seven days, then returns this key's text; `save` of an empty |
| @@ -233,7 +243,8 @@ if (root) { | ||
| 233 | 243 | const l = e.key === " " && e.target.closest && e.target.closest("a[data-act]"); |
| 234 | 244 | if (l) { e.preventDefault(); l.click(); } |
| 235 | 245 | }); |
| 236 | const nearEnd = () => window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 120; | |
| 246 | ${followJs} | |
| 247 | const nearEnd = () => follows(window.innerHeight, window.scrollY, document.documentElement.scrollHeight); | |
| 237 | 248 | let stuck = true; |
| 238 | 249 | const toEnd = (force) => { |
| 239 | 250 | if (!force && !stuck) { jump.hidden = false; say("New message"); return; } |
| @@ -286,6 +297,7 @@ if (root) { | ||
| 286 | 297 | const decorate = (scope) => { |
Non-blocking: a code box that takes focus should show it. Add |
||
| 287 | 298 | scope.querySelectorAll(".talk-body pre:not([data-c])").forEach((pre) => { |
| 288 | 299 | pre.dataset.c = "1"; |
| 300 | pre.tabIndex = 0; | |
| 289 | 301 | pre.insertBefore(copyButton("Copy this code"), pre.firstChild); |
| 290 | 302 | }); |
| 291 | 303 | scope.querySelectorAll(".talk-turn:not([data-m]) > .talk-meta").forEach((m) => { |
test/talk-layout.test.ts +25 −5
| @@ -26,7 +26,7 @@ const chromium = found !== undefined && chromiumRuns(found) ? found : undefined; | ||
| 26 | 26 | if (chromium === undefined) console.log("SKIPPING: no chromium that starts on this machine, so the Talk layout is not measured"); |
| 27 | 27 | |
| 28 | 28 | interface Box { left: number; right: number; top: number; bottom: number; width: number } |
| 29 | interface Seen { scrollWidth: number; vw: number; vh: number; side: Box | null; chat: Box | null; pane: Box | null; compose: Box | null; build: Box | null; tabs: boolean; back: Box | null } | |
| 29 | interface Seen { scrollWidth: number; vw: number; vh: number; side: Box | null; chat: Box | null; pane: Box | null; compose: Box | null; build: Box | null; tabs: boolean; back: Box | null; long: { boxScrolls: boolean; boxWidth: number; linkRight: number } | null } | |
| 30 | 30 | |
| 31 | 31 | let dir: string; |
| 32 | 32 | let app: App; |
| @@ -46,6 +46,8 @@ beforeAll(async () => { | ||
| 46 | 46 | const chat = openConversation(app.db, made.value.id); |
| 47 | 47 | appendTurn(app.db, made.value.id, chat.id, { role: "person", text: "hello" }); |
| 48 | 48 | appendTurn(app.db, made.value.id, chat.id, { role: "forge", text: "Hello." }); |
| 49 | // A reply with a 300-character code line and a 200-character link: the page must not widen. | |
| 50 | appendTurn(app.db, made.value.id, chat.id, { role: "agent", text: `See [https://example.com/${"a".repeat(180)}](https://example.com/${"a".repeat(180)}) for more.\n\n\`\`\`\n${"x".repeat(300)}\n\`\`\`\n` }); | |
| 49 | 51 | token = createSession(app.db, made.value.id).token; |
| 50 | 52 | // A person who has said nothing yet: the first visit, with the build box above the shell. |
| 51 | 53 | const fresh = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" }); |
| @@ -60,6 +62,10 @@ afterAll(async () => { | ||
| 60 | 62 | }); |
| 61 | 63 | |
| 62 | 64 | const EXPRESSION = `(() => { |
| 65 | const longLine = () => { | |
| 66 | const pre = document.querySelector(".talk-body pre"), a = document.querySelector(".talk-body a"); | |
| 67 | return pre && a ? { boxScrolls: pre.scrollWidth > pre.clientWidth, boxWidth: pre.getBoundingClientRect().width, linkRight: a.getBoundingClientRect().right } : null; | |
| 68 | }; | |
| 63 | 69 | const box = (sel) => { const el = document.querySelector(sel); if (!el) return null; const r = el.getBoundingClientRect(); return r.width === 0 || r.height === 0 ? null : { left: r.left, right: r.right, top: r.top, bottom: r.bottom, width: r.width }; }; |
| 64 | 70 | const tabs = document.querySelector(".talk-tabs"); |
| 65 | 71 | return JSON.stringify({ |
| @@ -68,6 +74,7 @@ const EXPRESSION = `(() => { | ||
| 68 | 74 | vh: window.innerHeight, |
| 69 | 75 | side: box(".talk-chats"), chat: box(".talk-log"), pane: box(".talk-pane"), compose: box(".talk-compose"), build: box(".talk-build-box"), back: box(".talk-tabs .talk-back"), |
| 70 | 76 | tabs: !!tabs && getComputedStyle(tabs).display !== "none", |
| 77 | long: longLine(), | |
| 71 | 78 | }); |
| 72 | 79 | })()`; |
| 73 | 80 | |
| @@ -82,6 +89,17 @@ afterEach(() => { | ||
| 82 | 89 | measured = ""; |
| 83 | 90 | }); |
| 84 | 91 | |
| 92 | /** | |
| 93 | * A 300-character code line scrolls inside its own box and a 200-character link wraps: the page | |
| 94 | * does not widen. | |
| 95 | */ | |
| 96 | const longFits = (s: Seen, width: number): void => { | |
| 97 | expect(s.long).not.toBeNull(); | |
| 98 | expect(s.long!.boxScrolls).toBe(true); | |
| 99 | expect(s.long!.boxWidth).toBeLessThanOrEqual(width); | |
| 100 | expect(s.long!.linkRight).toBeLessThanOrEqual(width); | |
| 101 | }; | |
| 102 | ||
| 85 | 103 | const fits = (s: Seen, width: number): void => { |
| 86 | 104 | expect(s.vw).toBe(width); |
| 87 | 105 | expect(s.scrollWidth).toBeLessThanOrEqual(width); |
| @@ -93,6 +111,7 @@ const fits = (s: Seen, width: number): void => { | ||
| 93 | 111 | test.skipIf(chromium === undefined)("a phone shows the chat alone, the composer on screen, nothing scrolling sideways", async () => { |
| 94 | 112 | const s = await look(360, 740); |
| 95 | 113 | fits(s, 360); |
| 114 | longFits(s, 360); | |
| 96 | 115 | expect(s.chat).not.toBeNull(); |
| 97 | 116 | expect(s.pane).toBeNull(); |
| 98 | 117 | expect(s.tabs).toBe(true); |
| @@ -134,6 +153,7 @@ test.skipIf(chromium === undefined)("a tablet puts the chats beside the chat, th | ||
| 134 | 153 | |
| 135 | 154 | test.skipIf(chromium === undefined)("a laptop shows chats, chat and preview side by side without tabs", async () => { |
| 136 | 155 | const s = await look(1280, 800); |
| 156 | longFits(s, 1280); | |
| 137 | 157 | fits(s, 1280); |
| 138 | 158 | expect(s.side!.right).toBeLessThanOrEqual(s.chat!.left); |
| 139 | 159 | expect(s.pane).not.toBeNull(); |
| @@ -149,11 +169,11 @@ test.skipIf(chromium === undefined)("a wide monitor keeps the reading column rea | ||
| 149 | 169 | expect(s.tabs).toBe(false); |
| 150 | 170 | }, 30_000); |
| 151 | 171 | |
| 152 | test.skipIf(chromium === undefined)("a first visit, the conversation empty, keeps the composer on screen at a tablet and a laptop", async () => { | |
| 153 | for (const [w, h] of [[900, 1100], [1280, 800]] as const) { | |
| 172 | for (const [w, h, name] of [[900, 1100, "a tablet"], [1280, 800, "a laptop"]] as const) { | |
| 173 | test.skipIf(chromium === undefined)(`a first visit, the conversation empty, keeps the composer on screen at ${name}`, async () => { | |
| 154 | 174 | const s = await look(w, h, emptyToken); |
| 155 | 175 | fits(s, w); |
| 156 | 176 | expect(s.build).not.toBeNull(); |
| 157 | 177 | expect(s.build!.bottom).toBeLessThanOrEqual(s.compose!.top); |
| 158 | } | |
| 159 | }, 30_000); | |
| 178 | }, 30_000); | |
| 179 | } |
test/talk-page.test.ts +24 −1
| @@ -241,6 +241,29 @@ describe("the page", () => { | ||
| 241 | 241 | expect(page).toContain("<table>"); |
| 242 | 242 | }); |
| 243 | 243 | |
| 244 | test("a long code line scrolls in its own box and a long URL wraps", async () => { | |
| 245 | const { turnHtml } = await import("../src/web/pages/talk.ts"); | |
| 246 | const { talkCss, talkJs } = await import("../src/web/pages/talk_client.ts"); | |
| 247 | const code = "x".repeat(300); | |
| 248 | const url = "https://example.com/" + "a".repeat(180); | |
| 249 | const text = `See ${url} now.\n\n\`\`\`\n${code}\n\`\`\`\n`; | |
| 250 | const out = turnHtml({ id: "9", role: "agent", text, createdAt: "2026-01-01T00:00:00Z" } as any, new Set()).text; | |
| 251 | expect(out).toContain(`<pre><code>${code}`); | |
| 252 | expect(out).toContain(url); | |
| 253 | expect(talkCss).toContain(".talk-turn pre { max-width: 100%; overflow-x: auto; }"); | |
| 254 | expect(talkCss).toContain(".talk-body a { overflow-wrap: anywhere; }"); | |
| 255 | expect(talkJs).toContain('pre.insertBefore(copyButton("Copy this code")'); | |
| 256 | expect(talkJs).toContain("pre.tabIndex = 0"); | |
| 257 | }); | |
| 258 | ||
| 259 | test("the view follows a streaming reply only when the reader is at the bottom", async () => { | |
| 260 | const { followJs } = await import("../src/web/pages/talk_client.ts"); | |
| 261 | const follows = new Function(`${followJs}; return follows;`)() as (v: number, y: number, h: number) => boolean; | |
| 262 | expect(follows(800, 1200, 2000)).toBe(true); | |
| 263 | expect(follows(800, 1100, 2000)).toBe(true); | |
| 264 | expect(follows(800, 400, 2000)).toBe(false); | |
| 265 | }); | |
| 266 | ||
| 244 | 267 | test("an unfinished code block closes itself and leaves what is above it as it was", async () => { |
| 245 | 268 | const { turnHtml } = await import("../src/web/pages/talk.ts"); |
| 246 | 269 | const turn = (text: string) => ({ id: "9", role: "agent", text, createdAt: "2026-01-01T00:00:00Z" }) as any; |
| @@ -346,7 +369,7 @@ describe("the page", () => { | ||
| 346 | 369 | test("the page's own bytes are held: stylesheet and script stay small", async () => { |
| 347 | 370 | const { talkCss, talkJs } = await import("../src/web/pages/talk_client.ts"); |
| 348 | 371 | expect(Buffer.byteLength(talkCss)).toBeLessThan(6_300); |
| 349 | expect(Buffer.byteLength(talkJs)).toBeLessThan(18_900); | |
| 372 | expect(Buffer.byteLength(talkJs)).toBeLessThan(19_100); | |
| 350 | 373 | say(alice, "agent", "```ts\nlet x = 1;\n```"); |
| 351 | 374 | const page = await get("/talk", alice); |
| 352 | 375 | const size = (await page.arrayBuffer()).byteLength; |
new above
offline
Change to
overflow-wrap: anywhere;. break-all splits ordinary short links at any character as soon as they wrap, which hurts reading on a 360px phone (principle 8: plain, readable).