fix: reader blank viewport when scrolling into estimated spacer regions
Spacers are estimated from average paragraph height, so fast scrollbar drags could land the viewport inside the blank bottom spacer where the old append-at-document-bottom logic never reached. Now every scroll event maps scrollTop back to a paragraph index; if it falls outside the rendered window the window re-renders around it and re-anchors the scroll position (self-healing), with edge-batching loops for large jumps.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
// 分块渲染容器(§4):维护已渲染区间 [start, end),随滚动增删段落。
|
||||
// 上下占位符记录未渲染段落的估算高度(按当前窗口实测平均段高 × 段数),
|
||||
// 窗口移动时同步补偿 scrollTop,保证视口锚定的段落不动。
|
||||
// 上下占位符记录未渲染段落的估算高度(按当前窗口实测平均段高 × 段数)。
|
||||
// 关键点:占位符是估算值,快速拖动滚动条会跳进占位符空白区,
|
||||
// 因此每次滚动都按 scrollTop/平均段高 反算目标段落,落在窗口外时
|
||||
// 整体重渲染窗口并重新锚定滚动位置(自愈),而不是只在文档底部追加。
|
||||
import { onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
import { paragraphIndexForOffset } from '../core/offset'
|
||||
|
||||
@@ -17,7 +19,8 @@ const emit = defineEmits<{
|
||||
|
||||
const BATCH = 30
|
||||
const WINDOW = 240 // 窗口内最多保留的段落数
|
||||
const EDGE = 600 // 距边缘多少像素时触发增删
|
||||
const EDGE = 600 // 距窗口边缘多少像素时触发增删
|
||||
const TOLERANCE = 5 // 目标段落偏离窗口多大范围算"仍在窗口内"
|
||||
|
||||
const root = ref<HTMLElement | null>(null)
|
||||
const topSpacer = ref<HTMLElement | null>(null)
|
||||
@@ -27,7 +30,7 @@ let start = 0
|
||||
let end = 0
|
||||
let mounted = false
|
||||
let restoring = false
|
||||
// 窗口内实测平均段高(含 margin),用于估算占位符
|
||||
// 窗口内实测平均段高(含 margin),用于估算占位符与反算目标段落
|
||||
let avgHeight = 40
|
||||
|
||||
function isChapter(text: string): boolean {
|
||||
@@ -77,7 +80,7 @@ function measureAvg() {
|
||||
if (nodes.length === 0) return
|
||||
let sum = 0
|
||||
for (const n of nodes) sum += n.offsetHeight
|
||||
avgHeight = sum / nodes.length
|
||||
avgHeight = Math.max(1, sum / nodes.length)
|
||||
}
|
||||
|
||||
// 占位符高度 = 段数 × 平均段高;返回顶部占位符相对变化量
|
||||
@@ -97,7 +100,6 @@ function appendBatch() {
|
||||
end = Math.min(total, end + BATCH)
|
||||
measureAvg()
|
||||
trimAbove()
|
||||
root.value!.scrollTop += applySpacers()
|
||||
}
|
||||
|
||||
function prependBatch() {
|
||||
@@ -110,7 +112,6 @@ function prependBatch() {
|
||||
start = a
|
||||
measureAvg()
|
||||
trimBelow()
|
||||
root.value!.scrollTop += applySpacers()
|
||||
}
|
||||
|
||||
function trimAbove() {
|
||||
@@ -128,6 +129,21 @@ function trimBelow() {
|
||||
end = limit
|
||||
}
|
||||
|
||||
// 整体重渲染以 idx 为中心的窗口,并把 idx 段锚定到视口顶部附近
|
||||
function jumpTo(idx: number) {
|
||||
const el = root.value!
|
||||
removeRange(start, end)
|
||||
start = Math.max(0, idx - 10)
|
||||
end = Math.min(props.paragraphs.length, idx + BATCH * 2)
|
||||
insertRange(start, end)
|
||||
measureAvg()
|
||||
applySpacers()
|
||||
// offsetTop 已包含顶部占位符,直接锚定即可(不可再叠加 start*avg)
|
||||
const target = el.querySelector<HTMLElement>(`[data-idx="${idx}"]`)
|
||||
el.scrollTop = Math.max(0, (target ? target.offsetTop : idx * avgHeight) - 8)
|
||||
emit('offset', props.offsets[idx])
|
||||
}
|
||||
|
||||
// 当前视口顶部所在段落的字符偏移量
|
||||
function currentOffset(): number {
|
||||
const nodes = root.value!.querySelectorAll<HTMLElement>('[data-idx]')
|
||||
@@ -145,32 +161,33 @@ function currentOffset(): number {
|
||||
function onScroll() {
|
||||
if (!mounted || restoring) return
|
||||
const el = root.value!
|
||||
if (el.scrollHeight - el.scrollTop - el.clientHeight < EDGE) appendBatch()
|
||||
if (el.scrollTop < EDGE && start > 0) prependBatch()
|
||||
const total = props.paragraphs.length
|
||||
// 全局位置 → 段落下标(占位符按平均段高线性铺开)
|
||||
const targetIdx = Math.max(0, Math.min(total - 1, Math.round(el.scrollTop / avgHeight)))
|
||||
if (targetIdx < start - TOLERANCE || targetIdx >= end + TOLERANCE) {
|
||||
// 跳进了占位符空白区:整体重渲染并重新锚定
|
||||
jumpTo(targetIdx)
|
||||
return
|
||||
}
|
||||
// 距窗口边缘不足 EDGE 时增量扩展,循环直到视口不再贴边(应对大幅跳跃)
|
||||
let guard = 0
|
||||
while (el.scrollHeight - el.scrollTop - el.clientHeight < EDGE && end < total && guard++ < total) {
|
||||
appendBatch()
|
||||
}
|
||||
guard = 0
|
||||
while (el.scrollTop < EDGE && start > 0 && guard++ < total) {
|
||||
prependBatch()
|
||||
}
|
||||
root.value!.scrollTop += applySpacers()
|
||||
emit('offset', currentOffset())
|
||||
}
|
||||
|
||||
async function restoreTo(offset: number) {
|
||||
if (offset <= 0 || props.offsets.length === 0) return
|
||||
const idx = paragraphIndexForOffset(props.offsets, offset)
|
||||
start = Math.max(0, idx - 10)
|
||||
end = Math.min(props.paragraphs.length, idx + BATCH * 2)
|
||||
insertRange(start, end)
|
||||
measureAvg()
|
||||
// 目标段落在窗口内的位置 + 顶部占位符估算高度 = 绝对滚动位置
|
||||
const target = root.value!.querySelector<HTMLElement>(`[data-idx="${idx}"]`)
|
||||
const innerTop = target ? target.offsetTop : 0
|
||||
applySpacers()
|
||||
root.value!.scrollTop = Math.max(0, innerTop + start * avgHeight - 8)
|
||||
emit('offset', props.offsets[idx])
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
restoring = true
|
||||
mounted = true
|
||||
await restoreTo(props.restoreOffset)
|
||||
if (end === 0) {
|
||||
// 无进度:渲染首屏
|
||||
if (props.restoreOffset > 0 && props.offsets.length > 0) {
|
||||
jumpTo(paragraphIndexForOffset(props.offsets, props.restoreOffset))
|
||||
} else {
|
||||
end = Math.min(props.paragraphs.length, BATCH * 2)
|
||||
insertRange(start, end)
|
||||
measureAvg()
|
||||
|
||||
Reference in New Issue
Block a user