rob-revisited/ide/ui/js/follow.js

23 lines
1.9 KiB
JavaScript

// Pure decisions of the Claude log's "follow the end" behaviour (claude.js applies them to the DOM). No DOM here, so it is unit tested
// (follow.test.mjs).
export const TOLERANCE = 32; // px from the bottom that still counts as "at the bottom" (the reader's scroll back down)
export function distanceFromBottom(top, clientHeight, scrollHeight) { return scrollHeight - top - clientHeight; }
export function nearBottom(top, clientHeight, scrollHeight, tol = TOLERANCE) { return distanceFromBottom(top, clientHeight, scrollHeight) <= tol; }
/** A scroll event that is the echo of our own scrollTop assignment (same place, just after it): never the reader. */
export function isProgrammatic(top, expectTop, ageMs, maxAge = 400) { return expectTop !== null && ageMs < maxAge && Math.abs(top - expectTop) < 2; }
/** Reader gestures that leave the end at once, before any scroll event or content change is processed. */
export function releasesOnWheel(deltaY) { return typeof deltaY === 'number' && deltaY < 0; }
export function releasesOnKey(key) { return key === 'ArrowUp' || key === 'PageUp' || key === 'Home'; }
export function releasesOnTouchMove(startY, y) { return y - startY > 4; } // the finger goes down: the content goes down, the view goes up
/** The element the reader is looking at: the first whose bottom is below the top of the view (rects relative to the view's top). */
export function pickAnchor(rects) { const i = rects.findIndex((r) => r.bottom > 0); return i; }
/** How far to scroll to keep the anchor where it was on screen (positive: scroll down). Under half a pixel: nothing. */
export function anchorDelta(recordedTop, currentTop) { const d = currentTop - recordedTop; return Math.abs(d) < 0.5 ? 0 : d; }
/** Unread items while the reader is away from the end (reset by returning to the end). */
export function nextUnread(unread, following, counts) { return following ? 0 : counts ? unread + 1 : unread; }