diff --git a/e2e/web/tests/issue.spec.mjs b/e2e/web/tests/issue.spec.mjs index 39f509e6..8fe8b0b8 100644 --- a/e2e/web/tests/issue.spec.mjs +++ b/e2e/web/tests/issue.spec.mjs @@ -130,6 +130,31 @@ test('alt+i lists the attachments with who added them, and it stays', async ({ p await expect(files.locator('.thumb', { hasText: 'shot.png' })).toBeVisible(); }); +// Pictures load after the comments are shown and make them taller: the end stays the end. +test('the comments stay at the latest while their pictures load', async ({ page, app }) => { + const svg = ''; + await page.route('**/api/attachments/slow-*', async route => { + await new Promise(r => setTimeout(r, 800)); // past the frames it takes to size the comments + await route.fulfill({ contentType: 'image/svg+xml', body: svg }); + }); + for (const n of [1, 2, 3]) { + const res = await page.request.post(app.url + '/api/issues/DEMO-4/comments', { data: { Markdown: `Shot ${n}\n\n![shot](attachment:slow-${n})` } }); + expect(res.ok()).toBeTruthy(); + } + await page.reload(); + const p = panel(page); + await expect(p.locator('.iss-key')).toHaveText('DEMO-4'); + await p.locator('.tab', { hasText: 'Comments' }).click(); + const last = p.locator('.cm img.md-img').last(); + await expect.poll(() => last.evaluate(n => n.complete && n.naturalHeight)).toBeGreaterThan(0); + const fromEnd = () => p.locator('.iss-scroll').evaluate(n => n.scrollHeight - n.scrollTop - n.clientHeight); + await expect.poll(fromEnd).toBeLessThan(2); + // scrolling yourself lets go of it + await p.locator('.iss-scroll').hover(); + await page.mouse.wheel(0, -400); + await expect.poll(fromEnd).toBeGreaterThan(100); +}); + // laneway-server's change stream (the demo has none: this stands in for it): someone else's comment // shows in the open issue without a reload. test.describe('on laneway-server', () => { diff --git a/internal/web/static/js/views/issue.js b/internal/web/static/js/views/issue.js index 999f2a35..5fbe143e 100644 --- a/internal/web/static/js/views/issue.js +++ b/internal/web/static/js/views/issue.js @@ -199,6 +199,15 @@ export function mountIssue(el, key, { app, full, card }) { s ? h('span.it-st.st-' + s, { title: T('agent %s', LABEL[s] || s) }, icon(ICON[s] || ICON.unknown)) : null, label); })); } + // hold keeps a place in the comments (the end, a linked comment) while they still change height: as + // content-visibility sizes the ones scrolled past and as their images load. Until you scroll yourself. + let held = null; + const release = () => { held = null; }; + const sized = new ResizeObserver(() => { if (held && st.tab === 'comments') held(); else release(); }); + sized.observe(box.list); + for (const ev of ['wheel', 'touchmove', 'pointerdown']) root.addEventListener(ev, release, { capture: true, passive: true }); + window.addEventListener('keydown', release, true); // on window: before the key itself opens the comments and holds + function hold(place) { held = place; place(); } function setTab(t) { const was = st.tab; st.tab = t; @@ -212,10 +221,7 @@ export function mountIssue(el, key, { app, full, card }) { if (t === 'history') loadHistory(); if (t === 'comments' && st.focusId == null) focusComment(null); if (t === 'comments' && was !== 'comments' && !newest() && !wantComment) { // oldest first: the latest is at the end - // Again each frame while content-visibility sizes the comments it scrolls past. - let h = -1, n = 0; - const end = () => { if (st.tab !== 'comments' || (scroll.scrollHeight === h && n > 1) || ++n > 20) return; h = scroll.scrollHeight; scroll.scrollTop = h; requestAnimationFrame(end); }; - end(); + hold(() => { scroll.scrollTop = scroll.scrollHeight; }); } } // The Terminal tab: shown (4, a click) or typing (ctrl+\, enter); 4 on it steps through several agents. @@ -632,13 +638,10 @@ export function mountIssue(el, key, { app, full, card }) { setTab('comments'); wantComment = null; focusComment(id, false); - // Again each frame while content-visibility sizes the comments it scrolls past. - let n = 0; - const center = () => { - const node = st.tab === 'comments' && st.focusId === id && ++n <= 20 && box.list.querySelector('[data-id="' + CSS.escape(id) + '"]'); - if (node) { node.scrollIntoView({ block: 'center' }); requestAnimationFrame(center); } - }; - center(); + hold(() => { + const node = st.focusId === id && box.list.querySelector('[data-id="' + CSS.escape(id) + '"]'); + if (node) node.scrollIntoView({ block: 'center' }); + }); } const commentEls = () => [...box.list.children].filter(n => n.dataset.id && !n.dataset.id.startsWith('tmp-')); function focusComment(id, scrollTo = true) { @@ -983,7 +986,7 @@ export function mountIssue(el, key, { app, full, card }) { if (startTerm) openTerm(true); return () => { - dead = true; scope.dispose(); offChanged(); offSite(); clearTimeout(siteTimer); offFocus(); offTimer.forEach(f => f()); + dead = true; sized.disconnect(); window.removeEventListener('keydown', release, true); scope.dispose(); offChanged(); offSite(); clearTimeout(siteTimer); offFocus(); offTimer.forEach(f => f()); term.dispose(); el.classList.remove('term-on'); for (const e of editors) e.dispose(); if (offNotes) offNotes();