UNPKG

@z-cloud/virtual-mini

Version:

提供Taro,uni-app的虚拟列表公共类

107 lines (106 loc) 3.55 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { BasicVirtualizer, debounce } from "@z-cloud/virtual-vanilla"; class MiniVirtualizer extends BasicVirtualizer { constructor(options) { super(); __publicField(this, "scrollToIndexTimeoutId", null); __publicField(this, "resetScrolling", debounce(() => { this.scrolling = false; this.notify(); }, 160)); this.setOptions(options); } init() { this.scrollToOffset( typeof this.options.initialOffset === "function" ? this.options.initialOffset() : this.options.initialOffset ); this.notify(); } onElementSizeChange(index, rect) { const item = this.items[index]; const size = rect[this.options.horizontal ? "width" : "height"]; if (!item) { return; } const delta = size - item.size; if (Math.abs(delta) >= 1) { const offset = this.getScrollOffset(); if (item.start < offset) { this.scrollTo(offset + delta); } this.dynamicSizeCache.set(index, size); this.pendingDynamicSizeIndexes.push(index); this.scrolling = false; this.clearFnMemo(["geItemsWithtMemo", "notifyWithMemo"]); this.notify(); } } onScroll(res) { if (!res) { return; } const offset = res[this.options.horizontal ? "scrollLeft" : "scrollTop"] ?? 0; if (typeof this.scrollOffset !== "undefined" && Math.abs(this.scrollOffset - offset) < 5) { this.scrollOffset = offset; return; } this.scrollOffset = offset; this.scrolling = true; this.resetScrolling({ clearTimeout, setTimeout }); this.notify(); } scrollTo(_offset, _behavior = "instant") { console.warn("scrollTo 需要针对 uni-app 或者 taro 各自实现"); } scrollToOffset(offset, behavior) { this.cancelScrollToIndex(); this.scrollTo(offset, behavior); } cancelScrollToIndex() { if (this.scrollToIndexTimeoutId !== null) { clearTimeout(this.scrollToIndexTimeoutId); this.scrollToIndexTimeoutId = null; } } scrollToIndex(index, { align, behavior } = {}) { this.cancelScrollToIndex(); const safeIndex = Math.max(0, Math.min(index, this.options.count - 1)); const offset = this.getOffsetForIndex(safeIndex, align); if (!offset) { return; } this.scrollTo(offset, this.dynamicMode() ? "instant" : behavior); if (this.dynamicMode()) { this.scrollToIndexTimeoutId = setTimeout(() => { this.scrollToIndexTimeoutId = null; if (this.dynamicSizeCache.has(safeIndex)) { const calcOffset = this.getOffsetForIndex(safeIndex, align); if (!calcOffset) { return; } const currentScrollOffset = this.getScrollOffset(); if (Math.abs(calcOffset - currentScrollOffset) > 1) { this.scrollToIndex(safeIndex, { align, behavior: "instant" }); } } else { this.scrollToIndex(safeIndex, { align, behavior: "instant" }); } }); } } clean() { this.unsubscribes = this.unsubscribes.filter((unsub) => { if (typeof unsub === "function") { unsub(); } return false; }); this.dynamicSizeCache.clear(); this.pendingDynamicSizeIndexes = []; } } export { MiniVirtualizer };