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
All checks have passed
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 @@
175 if (pending === 0 && !waiting) { dropExtras(); }175 if (pending === 0 && !waiting) { dropExtras(); }
176 }176 }
177 177
178- function adopt(doc, keepFields, dd, jobs) {178+ function adopt(doc, keepFields, dd, jobs, logs) {
179 document.title = doc.title;179 document.title = doc.title;
180 ['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) {180 ['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) {
181 if (doc.documentElement.hasAttribute(name)) { root.setAttribute(name, doc.documentElement.getAttribute(name)); }181 if (doc.documentElement.hasAttribute(name)) { root.setAttribute(name, doc.documentElement.getAttribute(name)); }
@@ -208,6 +208,7 @@
208 syncLog();208 syncLog();
209 followTail();209 followTail();
210 restoreJobs(jobs);210 restoreJobs(jobs);
211+ restoreLogs(logs);
211 syncLive();212 syncLive();
212 syncPush();213 syncPush();
213 syncTick();214 syncTick();
@@ -271,26 +272,51 @@
271 var open = li.getAttribute('data-open') === 'true';272 var open = li.getAttribute('data-open') === 'true';
272 out[li.getAttribute('data-job-row')] = {273 out[li.getAttribute('data-job-row')] = {
273 touched: li.hasAttribute('data-touched'),274 touched: li.hasAttribute('data-touched'),
274- open: open,275+ open: open
275- top: pre && open ? pre.scrollTop : 0,
276- end: pre && open ? pre.scrollTop + pre.clientHeight >= pre.scrollHeight - 24 : true
277 };276 };
278 });277 });
279 return out;278 return out;
280 }279 }
281 280
282- // Every open tail starts at its end; a captured one goes back to where the reader had it.
283 function restoreJobs(kept) {281 function restoreJobs(kept) {
284 jobTails().forEach(function (li) {282 jobTails().forEach(function (li) {
285 var c = kept ? kept[li.getAttribute('data-job-row')] : null;283 var c = kept ? kept[li.getAttribute('data-job-row')] : null;
286 if (c && c.touched) { setJobRow(li, c.open, true); }284 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;
290 });285 });
291 syncJobAll();286 syncJobAll();
292 }287 }
293 288
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+
294 // A job's steps dropdown (details[data-steps-job]) the reader opened or shut is marked data-touched;320 // A job's steps dropdown (details[data-steps-job]) the reader opened or shut is marked data-touched;
295 // a live swap or a soft navigation keeps that state for the same job. Untouched ones take the server's321 // a live swap or a soft navigation keeps that state for the same job. Untouched ones take the server's
296 // default: open for the selected job.322 // default: open for the selected job.
@@ -357,9 +383,7 @@
357 if (!doc.body) { location.assign(dest.href); return; }383 if (!doc.body) { location.assign(dest.href); return; }
358 var x = window.scrollX, y = window.scrollY;384 var x = window.scrollX, y = window.scrollY;
359 var saved = history.state;385 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.386+ var logs = how === 'live' ? captureLogs() : null;
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;
363 // A live change must not shut a menu the reader has open (a run's branch picker, say).387 // A live change must not shut a menu the reader has open (a run's branch picker, say).
364 var dd = how === 'live' ? captureDropdowns() : null;388 var dd = how === 'live' ? captureDropdowns() : null;
365 var at = how === 'live' ? captureAnchor() : null;389 var at = how === 'live' ? captureAnchor() : null;
@@ -373,15 +397,12 @@
373 var jumped = jump ? jump.value : '';397 var jumped = jump ? jump.value : '';
374 liveJump = null;398 liveJump = null;
375 var steps = captureSteps();399 var steps = captureSteps();
376- adopt(doc, keepFields, dd, how === 'live' ? captureJobs() : null);400+ adopt(doc, keepFields, dd, how === 'live' ? captureJobs() : null, logs);
377 restoreSteps(steps);401 restoreSteps(steps);
378 placeRun(listTop);402 placeRun(listTop);
379 if (bars) { growBars(bars); }403 if (bars) { growBars(bars); }
380 if (paneTops) { setPaneScrolls(paneTops); }404 if (paneTops) { setPaneScrolls(paneTops); }
381- if (tailAt) {405+ if (logs) { restoreLogs(logs); }
382- var nextTail = document.querySelector('[data-live-log]');
383- if (nextTail) { nextTail.scrollTop = tailAt.end && !tailAt.start ? nextTail.scrollHeight : tailAt.top; }
384- }
385 if (how === 'nav') { history.pushState(null, '', dest.href); }406 if (how === 'nav') { history.pushState(null, '', dest.href); }
386 else if (how === 'post' || how === 'query') { history.replaceState(null, '', dest.href); }407 else if (how === 'post' || how === 'query') { history.replaceState(null, '', dest.href); }
387 shown = dest.pathname + dest.search;408 shown = dest.pathname + dest.search;