-
-
Notifications
You must be signed in to change notification settings - Fork 629
Feat virtual scroll #1895
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
Open
william-xue
wants to merge
38
commits into
varletjs:dev
Choose a base branch
from
william-xue:feat-virtual-scroll
base: dev
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
Feat virtual scroll #1895
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
b2e9340
feat: add name
william-xue dda43c3
feat: add function signature
william-xue 960ad7b
fix: adjust css
william-xue 46e4e7b
fix: adjust css
william-xue ca79f54
feat: signature finish
william-xue f4cf2a7
Merge branch 'varletjs:dev' into dev
william-xue cf7df45
feat: add test cases
william-xue b15d038
feat: add test cases
william-xue 0796c74
feat: test cases
william-xue e8bf4ca
feat: resolve
william-xue 600a11c
feat: fix
william-xue 375c477
Merge branch 'varletjs:dev' into dev
william-xue bb63493
feat: test case
william-xue 62895d4
feat: test case
william-xue bd7fe46
feat: expose empty
william-xue 5b746cd
Merge branch 'varletjs:dev' into dev
william-xue 8857fb8
feat: resolve
william-xue 34158dc
refactor: optimize event handling with useEventListener hook
william-xue 5cfc324
refactor: remove duplicate onMounted hook and clean up unnecessary code
william-xue f228328
refactor: add
william-xue faf5c08
Merge branch 'varletjs:dev' into dev
william-xue fd9a16c
refactor: add
william-xue f8ab947
refactor: add
william-xue c5ad7cd
feat: resolve comment
william-xue 75682a1
feat: resolve comment
william-xue 1268d01
feat: resolve comment
william-xue a52f477
feat: cleanup
haoziqaq 01f61e7
feat: test cases
william-xue 4e61cfb
feat: docs finish
william-xue d6811dd
feat: resolve comment
william-xue e71f0f3
feat: "resolve comment"
william-xue 3e7c88f
refactor: cleanup
haoziqaq f50640e
types: fix types
haoziqaq 8a6c042
types: fix types
haoziqaq 05a17bd
Merge branch 'varletjs:dev' into dev
william-xue d150b65
Merge branch 'varletjs:dev' into dev
william-xue f71fde3
feat(virtual-list): 添加虚拟滚动列表组件及相关文档和示例
4c0ce3e
refactor(virtual-list): update comments and improve code readability
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 hidden or 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,263 @@ | ||
| <template> | ||
| <div ref="containerRef" :class="n()" :style="containerStyle" @scroll.passive="handleScroll"> | ||
| <!-- Phantom layer for scroll area --> | ||
| <div :class="n('phantom')" :style="{ height: `${phantomHeight}px` }" /> | ||
| <!-- Content layer for actual rendering --> | ||
| <div ref="contentRef" :class="n('content')" :style="{ transform: getTransform() }"> | ||
| <div v-for="(item, index) in visibleData" :key="index" :class="n('item')"> | ||
| <slot :item="item" :index="start + index" /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </template> | ||
|
|
||
| <script lang="ts"> | ||
| import { computed, defineComponent, onMounted, reactive, ref, toRefs, watch } from 'vue' | ||
| import { call, toNumber } from '@varlet/shared' | ||
| import { createNamespace } from '../utils/components' | ||
| import { props } from './props' | ||
|
|
||
| const { name, n, classes } = createNamespace('virtual-list') | ||
|
|
||
| interface CachedPosition { | ||
| index: number | ||
| top: number | ||
| bottom: number | ||
| height: number | ||
| dValue: number | ||
| } | ||
|
|
||
| enum CompareResult { | ||
| eq = 1, | ||
| lt, | ||
| gt, | ||
| } | ||
|
|
||
| // Binary search for fast index lookup | ||
| function binarySearch<T, VT>(list: T[], value: VT, compareFunc: (current: T, value: VT) => CompareResult): number { | ||
| let start = 0 | ||
| let end = list.length - 1 | ||
| let tempIndex = 0 | ||
|
|
||
| while (start <= end) { | ||
| tempIndex = Math.floor((start + end) / 2) | ||
| const midValue = list[tempIndex] | ||
| const compareRes = compareFunc(midValue, value) | ||
| if (compareRes === CompareResult.eq) { | ||
| return tempIndex | ||
| } | ||
| if (compareRes === CompareResult.lt) { | ||
| start = tempIndex + 1 | ||
| } else { | ||
| end = tempIndex - 1 | ||
| } | ||
| } | ||
|
|
||
| return tempIndex | ||
| } | ||
|
|
||
| export default defineComponent({ | ||
| name, | ||
| props, | ||
| setup(props) { | ||
| // DOM refs | ||
| const containerRef = ref<HTMLElement | null>(null) | ||
| const contentRef = ref<HTMLElement | null>(null) | ||
|
|
||
| // Virtual scroll state | ||
| const state = reactive({ | ||
| start: 0, | ||
| originStartIndex: 0, | ||
| scrollTop: 0, | ||
| cachePositions: [] as CachedPosition[], | ||
| phantomHeight: 0, | ||
| }) | ||
|
|
||
| // Computed container style | ||
| const containerStyle = computed(() => { | ||
| if (props.containerHeight) { | ||
| return { | ||
| height: typeof props.containerHeight === 'number' ? `${props.containerHeight}px` : props.containerHeight, | ||
| } | ||
| } | ||
| return {} | ||
| }) | ||
|
|
||
| // Number of visible items | ||
| const visibleCount = computed(() => { | ||
| if (!containerRef.value) { | ||
| return 0 | ||
| } | ||
| return Math.ceil(containerRef.value.clientHeight / toNumber(props.itemHeight)) | ||
| }) | ||
|
|
||
| // End index (start + visible + buffer) | ||
| const end = computed(() => { | ||
| return Math.min(state.originStartIndex + visibleCount.value + props.bufferSize, props.data.length) | ||
| }) | ||
|
|
||
| // Visible data | ||
| const visibleData = computed(() => { | ||
| return props.data.slice(state.start, end.value) | ||
| }) | ||
|
|
||
| // Get transform style for content layer | ||
| const getTransform = () => { | ||
| return `translate3d(0, ${state.start >= 1 ? state.cachePositions[state.start - 1].bottom : 0}px, 0)` | ||
| } | ||
|
|
||
| // Initialize cached positions | ||
| function initCachedPosition() { | ||
| state.cachePositions = [] | ||
| const itemHeight = toNumber(props.itemHeight) | ||
| for (let i = 0; i < props.data.length; ++i) { | ||
| state.cachePositions[i] = { | ||
| index: i, | ||
| height: itemHeight, | ||
| top: i * itemHeight, | ||
| bottom: (i + 1) * itemHeight, | ||
| dValue: 0, | ||
| } | ||
| } | ||
| state.phantomHeight = props.data.length * itemHeight | ||
| } | ||
|
|
||
| // Update cached positions based on actual DOM height | ||
| function updateCachedPosition() { | ||
| if (!contentRef.value) { | ||
| return | ||
| } | ||
| const nodes = Array.from(contentRef.value.childNodes).filter((node: Node) => node.nodeType === 1) as HTMLElement[] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. node.nodeType === Node.ELEMENT_NODE |
||
| nodes.forEach((node, index) => { | ||
| const height = node.offsetHeight | ||
| const pos = state.cachePositions[index + state.start] | ||
| const dValue = pos.height - height | ||
| if (dValue) { | ||
| pos.bottom -= dValue | ||
| pos.height = height | ||
| pos.dValue = dValue | ||
| } | ||
| }) | ||
| // Update following positions | ||
| const startIndex = state.start | ||
| const len = state.cachePositions.length | ||
| let cumulativeDiffHeight = state.cachePositions[startIndex].dValue | ||
| state.cachePositions[startIndex].dValue = 0 | ||
| for (let i = startIndex + 1; i < len; ++i) { | ||
| const prev = state.cachePositions[i - 1] | ||
| const curr = state.cachePositions[i] | ||
| curr.top = prev.bottom | ||
| curr.bottom = curr.bottom - cumulativeDiffHeight | ||
| if (curr.dValue !== 0) { | ||
| cumulativeDiffHeight += curr.dValue | ||
| curr.dValue = 0 | ||
| } | ||
| } | ||
| // Update phantom height | ||
| state.phantomHeight = state.cachePositions[len - 1]?.bottom || 0 | ||
| } | ||
|
|
||
| // Get start index for given scrollTop | ||
| function getStartIndex(scrollTop = 0) { | ||
| let idx = binarySearch<CachedPosition, number>(state.cachePositions, scrollTop, (current, target) => { | ||
| const val = current.bottom | ||
| if (val === target) { | ||
| return CompareResult.eq | ||
| } | ||
| if (val < target) { | ||
| return CompareResult.lt | ||
| } | ||
| return CompareResult.gt | ||
| }) | ||
| const targetItem = state.cachePositions[idx] | ||
| if (targetItem && targetItem.bottom < scrollTop) { | ||
| idx += 1 | ||
| } | ||
| return idx | ||
| } | ||
|
|
||
| // Reset all virtual scroll params | ||
| function resetAllVirtualParam() { | ||
| state.originStartIndex = 0 | ||
| state.start = 0 | ||
| state.scrollTop = 0 | ||
| if (containerRef.value) { | ||
| containerRef.value.scrollTop = 0 | ||
| } | ||
| initCachedPosition() | ||
| } | ||
|
|
||
| // Handle scroll event | ||
| function handleScroll(e: Event) { | ||
| if (!containerRef.value) { | ||
| return | ||
| } | ||
| const scrollTop = containerRef.value.scrollTop | ||
| const { originStartIndex } = state | ||
| const currentIndex = getStartIndex(scrollTop) | ||
| if (currentIndex !== originStartIndex) { | ||
| state.originStartIndex = currentIndex | ||
| state.start = Math.max(state.originStartIndex - props.bufferSize, 0) | ||
| } | ||
| state.scrollTop = scrollTop | ||
| call(props.onScroll, e) | ||
| } | ||
|
|
||
| // Scroll to specified index | ||
| function scrollTo(index: number) { | ||
| if (!containerRef.value || index < 0 || index >= props.data.length) { | ||
| return | ||
| } | ||
| if (state.cachePositions[index]) { | ||
| containerRef.value.scrollTop = state.cachePositions[index].top | ||
| } else { | ||
| const itemHeight = toNumber(props.itemHeight) | ||
| containerRef.value.scrollTop = index * itemHeight | ||
| } | ||
| } | ||
|
|
||
| // Watch data changes, reset virtual params | ||
| watch( | ||
| () => props.data, | ||
| (newVal, oldVal) => { | ||
| if (newVal.length !== oldVal.length) { | ||
| resetAllVirtualParam() | ||
| } | ||
| }, | ||
| { deep: true }, | ||
| ) | ||
|
|
||
| // Watch start index, update cached positions | ||
| watch( | ||
| () => state.start, | ||
| () => { | ||
| if (contentRef.value && props.data.length > 0) { | ||
| updateCachedPosition() | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| // Init cached positions on mount | ||
| onMounted(() => { | ||
| initCachedPosition() | ||
| }) | ||
|
|
||
| return { | ||
| n, | ||
| classes, | ||
| containerRef, | ||
| contentRef, | ||
| containerStyle, | ||
| scrollTo, | ||
| handleScroll, | ||
| visibleData, | ||
| getTransform, | ||
| ...toRefs(state), | ||
| } | ||
| }, | ||
| }) | ||
| </script> | ||
|
|
||
| <style lang="less"> | ||
| @import './virtualList'; | ||
| </style> | ||
This file contains hidden or 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,8 @@ | ||
| import { createApp } from 'vue' | ||
| import { expect, test } from 'vitest' | ||
| import VirtualList from '..' | ||
|
|
||
| test('virtual-list plugin', () => { | ||
| const app = createApp({}).use(VirtualList) | ||
| expect(app.component(VirtualList.name)).toBeTruthy() | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
使用 toPxNum
import { toPxNum } from '../utils/elements'