-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathuseVirtualList.ts
197 lines (177 loc) · 5.72 KB
/
useVirtualList.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { ref } from "vue"
import { TreeNode } from ".."
import { INonReactiveData, TreeNodeKeyType } from "../types"
import { VerticalPositionType, verticalPositionEnum } from "../constants"
import { TreeProps } from "../components/Tree.vue"
type IUseVirtualListProps = Required<Pick<TreeProps,
'renderNodeAmount' |
'nodeMinHeight' |
'bufferNodeAmount' |
'keyField'
>>
export const useVirtualList = (nonReactive: INonReactiveData, props: IUseVirtualListProps) => {
const scrollArea = ref()
const renderNodes = ref<TreeNode[]>([])
const blockLength = ref(0)
const blockAreaHeight = ref(0)
const topSpaceHeight = ref(0)
const bottomSpaceHeight = ref(0)
const renderAmount = ref(0)
const renderAmountCache = ref(0)
const renderStart = ref(0)
const renderStartCache = ref(0)
const debounceTimer = ref<number | undefined>(undefined)
/**
* 重置空白与滚动高度
*/
const resetSpaceHeights = (): void => {
topSpaceHeight.value = 0
bottomSpaceHeight.value = 0
if (scrollArea.value) scrollArea.value.scrollTop = 0
}
/**
* 计算需要渲染的节点的数量,只要容器高度(clientHeight)不变,这个数量一般就不会变
*/
const updateRenderAmount = (): void => {
const clientHeight = scrollArea.value.clientHeight
renderAmount.value = Math.max(
props.renderNodeAmount,
Math.ceil(clientHeight / props.nodeMinHeight) + props.bufferNodeAmount
)
}
/**
* 计算渲染的节点,基于 scrollTop 计算当前应该渲染哪些节点
*/
function updateRenderNodes(isScroll: boolean = false): void {
// 添加边界检查,防止无效更新
if (blockLength.value === 0) return;
if (blockLength.value > renderAmount.value) {
const scrollTop = scrollArea.value.scrollTop
const maxScrollTop = blockAreaHeight.value - scrollArea.value.clientHeight
// 添加滚动到底部的判断
if (scrollTop >= maxScrollTop) {
renderStart.value = Math.max(0, blockLength.value - renderAmount.value)
} else {
const scrollNodeAmount = Math.floor(scrollTop / props.nodeMinHeight)
renderStart.value = Math.floor(scrollNodeAmount / props.bufferNodeAmount) * props.bufferNodeAmount
}
} else {
renderStart.value = 0
}
// 避免不必要的更新
if (
isScroll &&
renderAmountCache.value === renderAmount.value &&
renderStartCache.value === renderStart.value
) {
return
}
renderNodes.value = nonReactive.blockNodes
.slice(renderStart.value, renderStart.value + renderAmount.value)
.map((blockNode: TreeNode) => {
return Object.assign({}, blockNode, {
_parent: null,
children: []
})
}) as TreeNode[]
topSpaceHeight.value = renderStart.value * props.nodeMinHeight
bottomSpaceHeight.value =
blockAreaHeight.value -
(topSpaceHeight.value + renderNodes.value.length * props.nodeMinHeight)
}
/**
* 计算渲染节点数量,并计算渲染节点
*/
const updateRender = (): void => {
updateRenderAmount()
updateRenderNodes()
}
/**
* 计算可见节点
*/
const updateBlockNodes = (): void => {
nonReactive.blockNodes = nonReactive.store.flatData.filter(
node => node.visible
)
updateBlockData()
updateRender()
}
/**
* 更新 block 数据相关信息
*/
const updateBlockData = (): void => {
blockLength.value = nonReactive.blockNodes.length
blockAreaHeight.value = props.nodeMinHeight * blockLength.value
}
//#endregion Calculate nodes
const isThrottled = ref(false)
function handleTreeScroll(): void {
if (debounceTimer.value) {
window.cancelAnimationFrame(debounceTimer.value)
}
// 添加节流
if (!isThrottled.value) {
isThrottled.value = true
renderAmountCache.value = renderAmount.value
renderStartCache.value = renderStart.value
debounceTimer.value = window.requestAnimationFrame(() => {
updateRenderNodes(true)
// 重置节流状态
setTimeout(() => {
isThrottled.value = false
}, 16) // 约60fps
})
}
}
/**
* 滚动到指定节点位置
* @param key 要滚动的节点
* @param verticalPosition 滚动的垂直位置,可选为 'top', 'center', 'bottom' 或距离容器可视顶部距离的数字,默认为 'top'
*/
const scrollTo = (
key: TreeNodeKeyType,
verticalPosition: VerticalPositionType | number = verticalPositionEnum.top
): void => {
const node = nonReactive.store.mapData[key]
if (!node || !node.visible) return
let index: number = -1
for (let i = 0; i < blockLength.value; i++) {
if (nonReactive.blockNodes[i][props.keyField] === key) {
index = i
break
}
}
if (index === -1) return
let scrollTop = index * props.nodeMinHeight
if (verticalPosition === verticalPositionEnum.center) {
const clientHeight = scrollArea.value.clientHeight
scrollTop = scrollTop - (clientHeight - props.nodeMinHeight) / 2
} else if (verticalPosition === verticalPositionEnum.bottom) {
const clientHeight = scrollArea.value.clientHeight
scrollTop = scrollTop - (clientHeight - props.nodeMinHeight)
} else if (typeof verticalPosition === 'number') {
scrollTop = scrollTop - verticalPosition
}
if (scrollArea.value) scrollArea.value.scrollTop = scrollTop
}
return {
scrollArea,
renderNodes,
blockLength,
blockAreaHeight,
topSpaceHeight,
bottomSpaceHeight,
renderAmount,
renderAmountCache,
renderStart,
renderStartCache,
resetSpaceHeights,
updateRenderAmount,
updateRenderNodes,
updateRender,
updateBlockNodes,
updateBlockData,
handleTreeScroll,
scrollTo,
}
}