-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[组件-下载视频] feat: 引入了虚拟列表来改善列表太长时操作卡顿的问题 #4455
Open
Asukaaaaaa
wants to merge
2
commits into
the1812:preview-features
Choose a base branch
from
Asukaaaaaa:plugin-video-download-scroll
base: preview-features
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
registry/lib/components/video/download/inputs/VirtualList.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<script setup lang="ts"> | ||
import { ref, computed, watch, onMounted } from 'vue' | ||
|
||
type PropsType = { | ||
items: any[] | ||
itemHeight: number | ||
stageItemCount?: number // 容器容纳的列表项数量 | ||
bufferItemCount?: number // 边缘预留缓冲的列表项数量 | ||
updateRequiredItemCount?: number // 缓冲列表项低于此数时触发更新 | ||
focuseItemIndex?: number // 初始聚焦的列表项 | ||
} | ||
|
||
const props = withDefaults(defineProps<PropsType>(), { | ||
stageItemCount: 50, | ||
bufferItemCount: 5, | ||
updateRequiredItemCount: 5, | ||
focuseItemIndex: 0, | ||
}) | ||
|
||
const container = ref<HTMLDivElement>() | ||
const stageHeight = computed(() => props.stageItemCount * props.itemHeight) | ||
const clientHeight = computed(() => container.value?.clientHeight ?? 0) | ||
// const scrollHeight = computed(() => container.value?.scrollHeight ?? 0) | ||
|
||
const ptr = ref(0) | ||
const appendPaddingTop = computed(() => ptr.value * props.itemHeight) | ||
const appendPaddingBottom = computed(() => { | ||
return (props.items.length - ptr.value - props.stageItemCount) * props.itemHeight | ||
}) | ||
const activatedItems = computed(() => { | ||
return props.items.slice(ptr.value, ptr.value + props.stageItemCount) | ||
}) | ||
|
||
const forceScrolling = ref(false) | ||
const lastScrollTop = ref(0) | ||
|
||
/** | ||
* 在 stage 之内移动 viewport,不引起数据变化 | ||
* @param top float | ||
*/ | ||
const moveViewport = (top: number) => { | ||
if (top < 0) { | ||
top = 0 | ||
} else if (top > stageHeight.value - clientHeight.value) { | ||
top = stageHeight.value - clientHeight.value | ||
} | ||
// 滚动列表 | ||
forceScrolling.value = true | ||
container.value?.scrollTo({ | ||
top: appendPaddingTop.value + top, | ||
behavior: 'instant', | ||
}) | ||
|
||
return top | ||
} | ||
|
||
/** | ||
* 在虚拟列表之内移动 stage,引起数据变化 | ||
* @param index int | ||
*/ | ||
const moveStage = (index: number) => { | ||
if (index < 0) { | ||
index = 0 | ||
} else if (index > props.items.length - props.stageItemCount) { | ||
index = props.items.length - props.stageItemCount | ||
} | ||
// 触发内容变化并推动列表 | ||
return (ptr.value = index) | ||
} | ||
|
||
/** | ||
* 将 viewport 对齐到指定元素 | ||
* @param index | ||
* @param alignment 决定用顶部还是底部对齐 | ||
*/ | ||
const focus = (index: number, alignment: 'top' | 'bottom' = 'top') => { | ||
if (alignment === 'top') { | ||
const offset = index - moveStage(index - props.bufferItemCount) | ||
moveViewport(offset * props.itemHeight) | ||
} else if (alignment === 'bottom') { | ||
const offset = index - moveStage(index + props.bufferItemCount - props.stageItemCount) | ||
moveViewport(offset * props.itemHeight) | ||
} | ||
} | ||
|
||
onMounted(() => { | ||
setTimeout(() => focus(props.focuseItemIndex)) | ||
}) | ||
|
||
watch( | ||
() => props.focuseItemIndex, | ||
newv => { | ||
focus(newv) | ||
}, | ||
) | ||
|
||
const onScroll = lodash.throttle((event: Event) => { | ||
if (forceScrolling.value) { | ||
// 手动触发滚动时不做处理 | ||
forceScrolling.value = false | ||
return | ||
} | ||
|
||
const tar = event.currentTarget as HTMLDivElement | ||
const step = tar.scrollTop - lastScrollTop.value | ||
// console.log(tar.scrollHeight, tar.scrollTop, step) | ||
lastScrollTop.value = tar.scrollTop | ||
if (step > 0) { | ||
// 向下滚动 | ||
const actualBottomHeight = tar.scrollTop + tar.clientHeight | ||
const contentBottomHeight = appendPaddingTop.value + stageHeight.value | ||
const buffer = contentBottomHeight - actualBottomHeight | ||
const min = props.itemHeight * props.updateRequiredItemCount | ||
if (buffer < min) { | ||
const offset = props.bufferItemCount - Math.round(buffer / props.itemHeight) | ||
moveStage(ptr.value + offset) | ||
} | ||
} else if (step < 0) { | ||
// 向上滚动 | ||
const actualTopHeight = tar.scrollHeight - tar.scrollTop | ||
const contentTopHeight = tar.scrollHeight - appendPaddingTop.value | ||
const buffer = contentTopHeight - actualTopHeight | ||
const min = props.itemHeight * props.updateRequiredItemCount | ||
if (buffer < min) { | ||
const offset = props.bufferItemCount - Math.round(buffer / props.itemHeight) | ||
moveStage(ptr.value - offset) | ||
} | ||
} | ||
}, 33) | ||
</script> | ||
|
||
<template> | ||
<div class="virtual-list" ref="container" @scroll.self="onScroll"> | ||
<ul class="scroll-box"> | ||
<li class="list-item" v-for="(item, index) in activatedItems" :key="index"> | ||
<slot name="item" :item="item" :index="index"></slot> | ||
</li> | ||
</ul> | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
.virtual-list { | ||
overflow-x: hidden; | ||
overflow-y: auto; | ||
|
||
> .scroll-box { | ||
list-style: none; | ||
padding-top: v-bind('`${appendPaddingTop}px`'); | ||
padding-bottom: v-bind('`${appendPaddingBottom}px`'); | ||
|
||
> .list-item { | ||
} | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
限制为 1 行的话, 建议加一个 title, 鼠标停留时可以看到完整标题