hy-virtual-tree 1.1.23 → 1.1.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/index.js +29 -6
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -11,6 +11,7 @@ class VirtualScroll {
|
|
|
11
11
|
* @param {number} [options.maxHeight=26840000] Maximum height in pixels for the content wrapper
|
|
12
12
|
* @param {Function} [options.onRender] 节点刷新回调
|
|
13
13
|
*/
|
|
14
|
+
cacheNodeMap = new Map();
|
|
14
15
|
constructor(options) {
|
|
15
16
|
this.container = options.container;
|
|
16
17
|
this.items = options.items || [];
|
|
@@ -115,10 +116,13 @@ class VirtualScroll {
|
|
|
115
116
|
renderVisibleItems(startIndex, endIndex) {
|
|
116
117
|
this._onRender && this._onRender();
|
|
117
118
|
// Clear content container
|
|
118
|
-
this.contentContainer.innerHTML = ''
|
|
119
|
+
// this.contentContainer.innerHTML = ''
|
|
119
120
|
// Set position considering scaling factor
|
|
121
|
+
// const fragment = document.createDocumentFragment()
|
|
122
|
+
// const fragment = []
|
|
120
123
|
this.contentContainer.style.transform = `translateY(${startIndex * this.itemHeight * this.heightScale}px)`;
|
|
121
124
|
// Render visible items
|
|
125
|
+
const nodeMap = new Map();
|
|
122
126
|
for (let i = startIndex; i < endIndex; i++) {
|
|
123
127
|
const item = this.items[i];
|
|
124
128
|
if (this.customRenderItem) {
|
|
@@ -129,7 +133,17 @@ class VirtualScroll {
|
|
|
129
133
|
itemElement.style.height = `${this.itemHeight * this.heightScale}px`;
|
|
130
134
|
itemElement.style.boxSizing = 'border-box';
|
|
131
135
|
itemElement.style.width = '100%';
|
|
132
|
-
|
|
136
|
+
const innerHTML = itemElement.innerHTML;
|
|
137
|
+
if (this.cacheNodeMap.has(innerHTML)) {
|
|
138
|
+
const target = this.cacheNodeMap.get(innerHTML);
|
|
139
|
+
this.contentContainer.appendChild(target);
|
|
140
|
+
nodeMap.set(innerHTML, target);
|
|
141
|
+
this.cacheNodeMap.delete(innerHTML);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
this.contentContainer.appendChild(itemElement);
|
|
145
|
+
nodeMap.set(innerHTML, itemElement);
|
|
146
|
+
}
|
|
133
147
|
}
|
|
134
148
|
}
|
|
135
149
|
else {
|
|
@@ -146,6 +160,11 @@ class VirtualScroll {
|
|
|
146
160
|
this.contentContainer.appendChild(row);
|
|
147
161
|
}
|
|
148
162
|
}
|
|
163
|
+
for (const [key, value] of this.cacheNodeMap) {
|
|
164
|
+
this.contentContainer.removeChild(value);
|
|
165
|
+
}
|
|
166
|
+
this.cacheNodeMap.clear();
|
|
167
|
+
this.cacheNodeMap = nodeMap;
|
|
149
168
|
}
|
|
150
169
|
/**
|
|
151
170
|
* Update data items and re-render
|
|
@@ -480,7 +499,7 @@ const useConfig = (config) => {
|
|
|
480
499
|
config = {
|
|
481
500
|
itemHeight: 30,
|
|
482
501
|
itemGap: 10,
|
|
483
|
-
bufferSize:
|
|
502
|
+
bufferSize: 30,
|
|
484
503
|
color: '#fff',
|
|
485
504
|
placeholderColor: '#909399',
|
|
486
505
|
fontSize: 14,
|
|
@@ -1976,18 +1995,18 @@ class VirtualTree {
|
|
|
1976
1995
|
/** 渲染项 */
|
|
1977
1996
|
const generateItem = (item) => {
|
|
1978
1997
|
const { type, showSelect } = config.rowSelection;
|
|
1979
|
-
|
|
1998
|
+
let el = document.createElement('div');
|
|
1980
1999
|
el.classList.add('hy-tree-node');
|
|
1981
2000
|
if (config.itemGap) {
|
|
1982
2001
|
el.style.paddingBottom = `${config.itemGap}px`;
|
|
1983
2002
|
}
|
|
1984
|
-
|
|
2003
|
+
let nodeContainer = document.createElement('div');
|
|
1985
2004
|
nodeContainer.style.height = `${config.itemHeight}px`;
|
|
1986
2005
|
if (config.nodeBgColor) {
|
|
1987
2006
|
nodeContainer.style.backgroundColor = config.nodeBgColor;
|
|
1988
2007
|
}
|
|
1989
2008
|
setNodeClass(nodeContainer, item);
|
|
1990
|
-
|
|
2009
|
+
let content = document.createElement('div');
|
|
1991
2010
|
content.classList.add('hy-tree-node-content');
|
|
1992
2011
|
content.style.setProperty('padding-left', `${(item.level - 1) * (config.indent || 0)}px`);
|
|
1993
2012
|
// 设备离线 - 字体颜色
|
|
@@ -2132,6 +2151,10 @@ class VirtualTree {
|
|
|
2132
2151
|
}
|
|
2133
2152
|
});
|
|
2134
2153
|
el.appendChild(nodeContainer);
|
|
2154
|
+
this._destroyDomSet.add(() => {
|
|
2155
|
+
// @ts-ignore
|
|
2156
|
+
el = nodeContainer = content = null;
|
|
2157
|
+
});
|
|
2135
2158
|
return el;
|
|
2136
2159
|
};
|
|
2137
2160
|
return { renderItem: generateItem };
|