From 7cf4e40a5fe55c561645ddd46566efaa404c2762 Mon Sep 17 00:00:00 2001 From: MathMan05 Date: Mon, 9 Sep 2024 11:24:02 -0500 Subject: [PATCH] fix snapping to new messages --- .dist/infiniteScroller.js | 25 +++++++++++-------------- webpage/channel.ts | 2 +- webpage/infiniteScroller.ts | 23 ++++++++++------------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/.dist/infiniteScroller.js b/.dist/infiniteScroller.js index 7d15e63..5295117 100644 --- a/.dist/infiniteScroller.js +++ b/.dist/infiniteScroller.js @@ -89,7 +89,6 @@ class InfiniteScroller { this.div.appendChild(html); this.HTMLElements.push([html, id]); } - currrunning = false; async addedBottom() { await this.updatestuff(); const func = this.snapBottom(); @@ -98,9 +97,11 @@ class InfiniteScroller { } snapBottom() { const scrollBottom = this.scrollBottom; + console.log(scrollBottom); return () => { if (this.div && scrollBottom < 4) { - this.div.scrollTop = this.div.scrollHeight + 20; + console.log("snap"); + this.div.scrollTop = this.div.scrollHeight; } }; } @@ -154,6 +155,9 @@ class InfiniteScroller { } } async watchForBottom(already = false, fragement = new DocumentFragment()) { + let func; + if (!already) + func = this.snapBottom(); if (!this.div) return false; try { @@ -192,8 +196,8 @@ class InfiniteScroller { finally { if (!already) { this.div.append(fragement); - if (this.scrollBottom < 30) { - this.div.scrollTop = this.div.scrollHeight; + if (func) { + func(); } } } @@ -201,18 +205,12 @@ class InfiniteScroller { watchtime = false; changePromise; async watchForChange() { - if (this.currrunning) { + if (this.changePromise) { this.watchtime = true; - if (this.changePromise) { - return await this.changePromise; - } - else { - return false; - } + return await this.changePromise; } else { this.watchtime = false; - this.currrunning = true; } this.changePromise = new Promise(async (res) => { try { @@ -226,7 +224,7 @@ class InfiniteScroller { if (this.timeout === null && changed) { this.timeout = setTimeout(this.updatestuff.bind(this), 300); } - if (!this.currrunning) { + if (!this.changePromise) { console.error("something really bad happened"); } res(Boolean(changed)); @@ -244,7 +242,6 @@ class InfiniteScroller { finally { setTimeout(_ => { this.changePromise = undefined; - this.currrunning = false; if (this.watchtime) { this.watchForChange(); } diff --git a/webpage/channel.ts b/webpage/channel.ts index 05178bb..334dedb 100644 --- a/webpage/channel.ts +++ b/webpage/channel.ts @@ -1099,7 +1099,7 @@ class Channel extends SnowFlake{ if(!this.infinitefocus){ this.tryfocusinfinate(); } - this.infinite.addedBottom(); + this.infinite.addedBottom() } if(messagez.author===this.localuser.user){ return; diff --git a/webpage/infiniteScroller.ts b/webpage/infiniteScroller.ts index a9bc076..6e91ad5 100644 --- a/webpage/infiniteScroller.ts +++ b/webpage/infiniteScroller.ts @@ -90,7 +90,6 @@ class InfiniteScroller{ this.div.appendChild(html); this.HTMLElements.push([html,id]); } - currrunning:boolean=false; async addedBottom(){ await this.updatestuff(); const func=this.snapBottom(); @@ -99,9 +98,11 @@ class InfiniteScroller{ } snapBottom(){ const scrollBottom=this.scrollBottom; + console.log(scrollBottom); return()=>{ if(this.div&&scrollBottom<4){ - this.div.scrollTop=this.div.scrollHeight+20; + console.log("snap"); + this.div.scrollTop=this.div.scrollHeight; } }; } @@ -155,6 +156,8 @@ class InfiniteScroller{ } } async watchForBottom(already=false,fragement=new DocumentFragment()):Promise{ + let func:Function|undefined; + if(!already) func=this.snapBottom(); if(!this.div)return false; try{ let again=false; @@ -190,8 +193,8 @@ class InfiniteScroller{ }finally{ if(!already){ this.div.append(fragement); - if(this.scrollBottom<30){ - this.div.scrollTop=this.div.scrollHeight; + if(func){ + func(); } } } @@ -199,16 +202,11 @@ class InfiniteScroller{ watchtime:boolean=false; changePromise:Promise|undefined; async watchForChange():Promise{ - if(this.currrunning){ + if(this.changePromise){ this.watchtime=true; - if(this.changePromise){ - return await this.changePromise; - }else{ - return false; - } + return await this.changePromise; }else{ this.watchtime=false; - this.currrunning=true; } this.changePromise=new Promise(async res=>{ try{ @@ -221,7 +219,7 @@ class InfiniteScroller{ if(this.timeout===null&&changed){ this.timeout=setTimeout(this.updatestuff.bind(this),300); } - if(!this.currrunning){ + if(!this.changePromise){ console.error("something really bad happened"); } @@ -237,7 +235,6 @@ class InfiniteScroller{ }finally{ setTimeout(_=>{ this.changePromise=undefined; - this.currrunning=false; if(this.watchtime){ this.watchForChange(); }