From 7aca449414b1903381696744a8b9bc4f3efe29ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Dec 2026 23:51:45 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20live=20logs=20(run=20log,?= =?UTF-8?q?=20job=20tails,=20runner=20log)=20keep=20the=20reader's=20lines?= =?UTF-8?q?=20in=20place=20when=20scrolled=20away=20from=20the=20end;=20at?= =?UTF-8?q?=20the=20end=20they=20keep=20following?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/forge-web/assets/nav.js | 53 ++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index 7bd7541..1267459 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -175,7 +175,7 @@ if (pending === 0 && !waiting) { dropExtras(); } } - function adopt(doc, keepFields, dd, jobs) { + function adopt(doc, keepFields, dd, jobs, logs) { document.title = doc.title; ['lang', 'data-theme', 'data-accent-light', 'data-accent-dark', 'data-base-light', 'data-base-dark', 'data-sidebar', 'data-ui', 'data-rel-words', 'data-rel-now'].forEach(function (name) { if (doc.documentElement.hasAttribute(name)) { root.setAttribute(name, doc.documentElement.getAttribute(name)); } @@ -208,6 +208,7 @@ syncLog(); followTail(); restoreJobs(jobs); + restoreLogs(logs); syncLive(); syncPush(); syncTick(); @@ -271,26 +272,51 @@ var open = li.getAttribute('data-open') === 'true'; out[li.getAttribute('data-job-row')] = { touched: li.hasAttribute('data-touched'), - open: open, - top: pre && open ? pre.scrollTop : 0, - end: pre && open ? pre.scrollTop + pre.clientHeight >= pre.scrollHeight - 24 : true + open: open }; }); return out; } - // Every open tail starts at its end; a captured one goes back to where the reader had it. function restoreJobs(kept) { jobTails().forEach(function (li) { var c = kept ? kept[li.getAttribute('data-job-row')] : null; if (c && c.touched) { setJobRow(li, c.open, true); } - var pre = li.querySelector('[data-job-tail] pre'); - if (!pre || li.getAttribute('data-open') !== 'true') { return; } - pre.scrollTop = c && !c.end ? c.top : pre.scrollHeight; }); syncJobAll(); } + // Live logs: the run's log, the job row tails, the runner log. A live swap brings the server's last + // window of the log, which slides as lines come. A log at its end stays at its end. A log the reader + // scrolled away from keeps its own element, so the lines under their eyes do not move. + function liveLogs() { return document.querySelectorAll('pre[data-live-log], [data-job-tail] pre, pre#runner-log'); } + + function logKey(pre) { + var row = pre.closest('[data-job-row]'); + return row ? 'job:' + row.getAttribute('data-job-row') : (pre.id || 'live'); + } + + function captureLogs() { + var out = {}; + Array.prototype.forEach.call(liveLogs(), function (pre) { + if (!pre.clientHeight) { return; } + var end = pre.scrollTop + pre.clientHeight >= pre.scrollHeight - 24; + out[logKey(pre)] = { pre: pre, top: pre.scrollTop, follow: end && !pre.hasAttribute('data-log-start') }; + }); + return out; + } + + function restoreLogs(kept) { + if (!kept) { return; } + Array.prototype.forEach.call(liveLogs(), function (pre) { + var c = kept[logKey(pre)]; + if (!c) { return; } + if (c.follow) { pre.scrollTop = pre.scrollHeight; return; } + pre.replaceWith(c.pre); + c.pre.scrollTop = c.top; + }); + } + // A job's steps dropdown (details[data-steps-job]) the reader opened or shut is marked data-touched; // a live swap or a soft navigation keeps that state for the same job. Untouched ones take the server's // default: open for the selected job. @@ -357,9 +383,7 @@ if (!doc.body) { location.assign(dest.href); return; } var x = window.scrollX, y = window.scrollY; var saved = history.state; - // A live swap keeps the log tail where the reader had it: at the end it stays at the end. - var tail = how === 'live' ? document.querySelector('[data-live-log]') : null; - var tailAt = tail ? { top: tail.scrollTop, end: tail.scrollTop + tail.clientHeight >= tail.scrollHeight - 24, start: tail.hasAttribute('data-log-start') } : null; + var logs = how === 'live' ? captureLogs() : null; // A live change must not shut a menu the reader has open (a run's branch picker, say). var dd = how === 'live' ? captureDropdowns() : null; var at = how === 'live' ? captureAnchor() : null; @@ -373,15 +397,12 @@ var jumped = jump ? jump.value : ''; liveJump = null; var steps = captureSteps(); - adopt(doc, keepFields, dd, how === 'live' ? captureJobs() : null); + adopt(doc, keepFields, dd, how === 'live' ? captureJobs() : null, logs); restoreSteps(steps); placeRun(listTop); if (bars) { growBars(bars); } if (paneTops) { setPaneScrolls(paneTops); } - if (tailAt) { - var nextTail = document.querySelector('[data-live-log]'); - if (nextTail) { nextTail.scrollTop = tailAt.end && !tailAt.start ? nextTail.scrollHeight : tailAt.top; } - } + if (logs) { restoreLogs(logs); } if (how === 'nav') { history.pushState(null, '', dest.href); } else if (how === 'post' || how === 'query') { history.replaceState(null, '', dest.href); } shown = dest.pathname + dest.search;