okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3cabb9e3f
main
1/** 2* When a chat follows the newest token, and when the reader has let go (#4014). 3* 4* The old rule asked "is the reader within 120 px of the bottom?" on every streamed token and pulled the view 5* back if so. One wheel notch is about 100 px, so it always landed inside that band and was undone by the next 6* token; only a scrollbar drag, which jumps further, got away. The rule now follows what the reader did: 7* a wheel, finger or key moving up lets go at once, and the chat follows again when the reader is back at 8* the bottom. 9*/ 10export interface Scroller { scrollHeight :number ; scrollTop :number ; clientHeight :number } 11 12/** Pixels of slack that still count as "at the bottom" (the same as `atBottom` in reasoning.ts). */ 13export const FOLLOW_SLACK = 24 ; 14/** Past this far from the bottom the "jump to latest" button is offered. */ 15export const PILL_DISTANCE = 400 ; 16/** A scroll this soon after the reader's own input was the reader's. */ 17export const INPUT_WINDOW_MS = 400 ; 18 19export const distanceFromBottom = ( el :Scroller ) :number => el . scrollHeight - el . scrollTop - el . clientHeight ; 20 21/** 22* Whether the chat should keep following after a scroll event. 23* - back at the bottom: follow again; 24* - the reader moved it (a wheel, finger, key or the scrollbar a moment ago) and it is not at the bottom: let go; 25* - far away whatever moved it (a jump): let go; 26* - otherwise (the page grew under a following reader and no input came): keep the state it had. 27*/ 28export function followAfterScroll ( distance :number , wasFollowing :boolean , msSinceInput :number ) :boolean { 29if ( distance <= FOLLOW_SLACK ) return true ; 30if ( msSinceInput <= INPUT_WINDOW_MS || distance > PILL_DISTANCE ) return false ; 31return wasFollowing ; 32} 33 34/** A wheel turned up (negative delta) or a finger dragging the content down: the reader wants to go back. */ 35export const wheelLeavesBottom = ( deltaY :number ) :boolean => deltaY < 0 ; 36 37/** Keys that move a scroller up. */ 38export const KEYS_UP = new Set ([ 'PageUp' , 'ArrowUp' , 'Home' ]);