From f1fea713399e1eac8071d3f3b3a224ba26b29ff7 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Tue, 8 Sep 2026 18:01:55 +0300 Subject: [PATCH] restore old virtual scrolling --- src/components/conversation/conversation.js | 2 +- src/components/timeline/timeline.js | 66 ++++++++++++++++++++- src/components/timeline/timeline.vue | 29 +++------ 3 files changed, 75 insertions(+), 22 deletions(-) diff --git a/src/components/conversation/conversation.js b/src/components/conversation/conversation.js index 63c843057..4eef7e2bf 100644 --- a/src/components/conversation/conversation.js +++ b/src/components/conversation/conversation.js @@ -263,7 +263,7 @@ export default { const body = useTemplateRef('body') const virtualHeight = ref(120) const hiddenStyle = computed(() => ({ - height: this.virtualHeight + 'px', + height: virtualHeight.value + 'px', })) const updateVirtualHeight = () => { if (hide) return // no updates when not rendering diff --git a/src/components/timeline/timeline.js b/src/components/timeline/timeline.js index 79b8f493b..637c7fb82 100644 --- a/src/components/timeline/timeline.js +++ b/src/components/timeline/timeline.js @@ -38,6 +38,7 @@ const Timeline = { showScrollTop: false, paused: false, unfocused: false, + virtualScrollIndex: 0, blockingClicks: false, } }, @@ -106,7 +107,20 @@ const Timeline = { } }, statusesToDisplay() { - return new Set(this.filteredVisibleStatuses.map(({ id }) => id)) + if (!this.virtualScrollingEnabled) { + return new Set(this.filteredVisibleStatuses.map(({ id }) => id)) + } + + const amount = this.timeline.visibleStatusIds.size + const statusesPerSide = Math.ceil(Math.max(3, window.innerHeight / 80)) + const min = Math.max(0, this.virtualScrollIndex - statusesPerSide) + const max = Math.min(amount, this.virtualScrollIndex + statusesPerSide) + return new Set( + this.filteredVisibleStatuses.slice(min, max).map(({ id }) => id), + ) + }, + virtualScrollingEnabled() { + return useMergedConfigStore().mergedConfig.virtualScrolling }, ...mapState(useInterfaceStore, { mobileLayout: (store) => store.layoutType === 'mobile', @@ -126,6 +140,7 @@ const Timeline = { } window.addEventListener('keydown', this.handleShortKey) window.addEventListener('scroll', this.handleScroll) + setTimeout(this.determineVisibleStatuses, 250) }, unmounted() { this.timelineChange(null, this.timelineRef) @@ -183,6 +198,54 @@ const Timeline = { 1000, this, ), + determineVisibleStatuses() { + if (!this.$refs.timeline) return + if (!this.virtualScrollingEnabled) return + + const statuses = this.$refs.timeline.children + if (statuses.length === 0) return + const cappedScrollIndex = Math.max( + 0, + Math.min(this.virtualScrollIndex, statuses.length - 1), + ) + + const height = Math.max(document.body.offsetHeight, window.pageYOffset) + + const centerOfScreen = window.pageYOffset + window.innerHeight * 0.5 + + // Start from approximating the index of some visible status by using the + // the center of the screen on the timeline. + let approxIndex = Math.min( + Math.floor(statuses.length * (centerOfScreen / height)), + statuses.length - 1, + ) + let err = statuses[approxIndex].getBoundingClientRect().y + + // if we have a previous scroll index that can be used, test if it's + // closer than the previous approximation, use it if so + + const virtualScrollIndexY = + statuses[cappedScrollIndex].getBoundingClientRect().y + if (Math.abs(err) > virtualScrollIndexY) { + approxIndex = cappedScrollIndex + err = virtualScrollIndexY + } + + // if the status is too far from viewport, check the next/previous ones if + // they happen to be better + while (err < -20 && approxIndex < statuses.length - 1) { + err += statuses[approxIndex].offsetHeight + approxIndex++ + } + while (err > window.innerHeight + 100 && approxIndex > 0) { + approxIndex-- + err -= statuses[approxIndex].offsetHeight + } + + // this status is now the center point for virtual scrolling and visible + // statuses will be nearby statuses before and after it + this.virtualScrollIndex = approxIndex + }, scrollLoad() { // TODO simplify this logic const bodyBRect = document.body.getBoundingClientRect() @@ -195,6 +258,7 @@ const Timeline = { } }, handleScroll: throttle(function (e) { + this.determineVisibleStatuses() this.scrollLoad(e) }, 200), handleVisibilityChange() { diff --git a/src/components/timeline/timeline.vue b/src/components/timeline/timeline.vue index 7a0be1a50..905b94382 100644 --- a/src/components/timeline/timeline.vue +++ b/src/components/timeline/timeline.vue @@ -77,30 +77,19 @@ />
- - - + +