long overdue fix to last pipeline push, where scroll position did not persist across sessions

This commit is contained in:
Disco DeDisco
2026-03-24 21:36:02 -04:00
parent 588358a20f
commit 2896efa8e0
2 changed files with 20 additions and 13 deletions

View File

@@ -22,8 +22,12 @@
}
}
// Restore: position stored is bottom-of-viewport; subtract clientHeight to align it
scroll.scrollTop = Math.max(0, {{ scroll_position }} - scroll.clientHeight);
// Only restore if there's a meaningful saved position — avoids a
// no-op scrollTop assignment (0→0) that can fire a spurious scroll
// event and reset the debounce timer in tests / headless browsers.
if ({{ scroll_position }} > 0) {
scroll.scrollTop = Math.max(0, {{ scroll_position }} - scroll.clientHeight);
}
});
// Animate "What happens next. . . ?" buffer dots — 4th span shows '?'
@@ -37,21 +41,24 @@
}, 400);
}
// Debounced save on scroll — store bottom-of-viewport so the last-read line is restored
// Debounced save on scroll — store bottom-of-viewport so the last-read line is restored.
// Position is captured at event time so layout changes during the debounce window
// (e.g. rAF adjusting marginTop) don't produce a stale clientHeight.
var remPx = parseFloat(getComputedStyle(document.documentElement).fontSize);
var saveTimer;
scroll.addEventListener('scroll', function() {
var pos = Math.round(scroll.scrollTop + scroll.clientHeight + remPx * 2.5);
clearTimeout(saveTimer);
saveTimer = setTimeout(function() {
var csrfToken = document.querySelector('[name=csrfmiddlewaretoken]');
var token = csrfToken ? csrfToken.value : '';
var remPx = parseFloat(getComputedStyle(document.documentElement).fontSize);
fetch("{% url 'billboard:save_scroll_position' room.id %}", {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-CSRFToken': token,
},
body: 'position=' + Math.round(scroll.scrollTop + scroll.clientHeight + remPx * 2.5),
body: 'position=' + pos,
});
}, 800);
});