forge
Explore
Log in

Don't you dare go Hollow

Live logs (run log, job tails, runner log) keep the reader's lines in place when scrolled away from the end

Browse files at this commit

7aca449414b1903381696744a8b9bc4f3efe29eaAhmet Kaan GümüşParent: 08e885b
Checks pending
Show checks
  • fix
    • live logs (run log, job tails, runner log) keep the reader's lines in place when scrolled away from the end
    • at the end they keep following
Show full message
🐛 fix: live logs (run log, job tails, runner log) keep the reader's lines in place when scrolled away from the end; at the end they keep following
crates/forge-web/assets/nav.jsmodified+37 -16
@@ -175,7 +175,7 @@
175175 if (pending === 0 && !waiting) { dropExtras(); }
176176 }
177177
178- function adopt(doc, keepFields, dd, jobs) {
178+ function adopt(doc, keepFields, dd, jobs, logs) {
179179 document.title = doc.title;
180180 ['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) {
181181 if (doc.documentElement.hasAttribute(name)) { root.setAttribute(name, doc.documentElement.getAttribute(name)); }
@@ -208,6 +208,7 @@
208208 syncLog();
209209 followTail();
210210 restoreJobs(jobs);
211+ restoreLogs(logs);
211212 syncLive();
212213 syncPush();
213214 syncTick();
@@ -271,26 +272,51 @@
271272 var open = li.getAttribute('data-open') === 'true';
272273 out[li.getAttribute('data-job-row')] = {
273274 touched: li.hasAttribute('data-touched'),
274- open: open,
275- top: pre && open ? pre.scrollTop : 0,
276- end: pre && open ? pre.scrollTop + pre.clientHeight >= pre.scrollHeight - 24 : true
275+ open: open
277276 };
278277 });
279278 return out;
280279 }
281280
282- // Every open tail starts at its end; a captured one goes back to where the reader had it.
283281 function restoreJobs(kept) {
284282 jobTails().forEach(function (li) {
285283 var c = kept ? kept[li.getAttribute('data-job-row')] : null;
286284 if (c && c.touched) { setJobRow(li, c.open, true); }
287- var pre = li.querySelector('[data-job-tail] pre');
288- if (!pre || li.getAttribute('data-open') !== 'true') { return; }
289- pre.scrollTop = c && !c.end ? c.top : pre.scrollHeight;
290285 });
291286 syncJobAll();
292287 }
293288
289+ // Live logs: the run's log, the job row tails, the runner log. A live swap brings the server's last
290+ // window of the log, which slides as lines come. A log at its end stays at its end. A log the reader
291+ // scrolled away from keeps its own element, so the lines under their eyes do not move.
292+ function liveLogs() { return document.querySelectorAll('pre[data-live-log], [data-job-tail] pre, pre#runner-log'); }
293+
294+ function logKey(pre) {
295+ var row = pre.closest('[data-job-row]');
296+ return row ? 'job:' + row.getAttribute('data-job-row') : (pre.id || 'live');
297+ }
298+
299+ function captureLogs() {
300+ var out = {};
301+ Array.prototype.forEach.call(liveLogs(), function (pre) {
302+ if (!pre.clientHeight) { return; }
303+ var end = pre.scrollTop + pre.clientHeight >= pre.scrollHeight - 24;
304+ out[logKey(pre)] = { pre: pre, top: pre.scrollTop, follow: end && !pre.hasAttribute('data-log-start') };
305+ });
306+ return out;
307+ }
308+
309+ function restoreLogs(kept) {
310+ if (!kept) { return; }
311+ Array.prototype.forEach.call(liveLogs(), function (pre) {
312+ var c = kept[logKey(pre)];
313+ if (!c) { return; }
314+ if (c.follow) { pre.scrollTop = pre.scrollHeight; return; }
315+ pre.replaceWith(c.pre);
316+ c.pre.scrollTop = c.top;
317+ });
318+ }
319+
294320 // A job's steps dropdown (details[data-steps-job]) the reader opened or shut is marked data-touched;
295321 // a live swap or a soft navigation keeps that state for the same job. Untouched ones take the server's
296322 // default: open for the selected job.
@@ -357,9 +383,7 @@
357383 if (!doc.body) { location.assign(dest.href); return; }
358384 var x = window.scrollX, y = window.scrollY;
359385 var saved = history.state;
360- // A live swap keeps the log tail where the reader had it: at the end it stays at the end.
361- var tail = how === 'live' ? document.querySelector('[data-live-log]') : null;
362- var tailAt = tail ? { top: tail.scrollTop, end: tail.scrollTop + tail.clientHeight >= tail.scrollHeight - 24, start: tail.hasAttribute('data-log-start') } : null;
386+ var logs = how === 'live' ? captureLogs() : null;
363387 // A live change must not shut a menu the reader has open (a run's branch picker, say).
364388 var dd = how === 'live' ? captureDropdowns() : null;
365389 var at = how === 'live' ? captureAnchor() : null;
@@ -373,15 +397,12 @@
373397 var jumped = jump ? jump.value : '';
374398 liveJump = null;
375399 var steps = captureSteps();
376- adopt(doc, keepFields, dd, how === 'live' ? captureJobs() : null);
400+ adopt(doc, keepFields, dd, how === 'live' ? captureJobs() : null, logs);
377401 restoreSteps(steps);
378402 placeRun(listTop);
379403 if (bars) { growBars(bars); }
380404 if (paneTops) { setPaneScrolls(paneTops); }
381- if (tailAt) {
382- var nextTail = document.querySelector('[data-live-log]');
383- if (nextTail) { nextTail.scrollTop = tailAt.end && !tailAt.start ? nextTail.scrollHeight : tailAt.top; }
384- }
405+ if (logs) { restoreLogs(logs); }
385406 if (how === 'nav') { history.pushState(null, '', dest.href); }
386407 else if (how === 'post' || how === 'query') { history.replaceState(null, '', dest.href); }
387408 shown = dest.pathname + dest.search;