|
| 1 | +<script> |
| 2 | + import Virtual from "./vue/virtual" |
| 3 | + import Item from "./Item.svelte" |
| 4 | +
|
| 5 | + export let dataKey |
| 6 | + export let dataSources |
| 7 | + export let keeps = 30 |
| 8 | + export let estimateSize = 50 |
| 9 | + export let direction = "vertical" |
| 10 | + export let offset = 0 |
| 11 | + export let pageMode = false |
| 12 | +
|
| 13 | + let range = null |
| 14 | + let virtual = new Virtual({ |
| 15 | + slotHeaderSize: 0, |
| 16 | + slotFooterSize: 0, |
| 17 | + keeps: keeps, |
| 18 | + estimateSize: estimateSize, |
| 19 | + buffer: Math.round(keeps / 3), // recommend for a third of keeps |
| 20 | + uniqueIds: getUniqueIdFromDataSources(), |
| 21 | + }, onRangeChanged) |
| 22 | +
|
| 23 | + let root |
| 24 | + let isHorizontal = direction === "horizontal" |
| 25 | + let directionKey = isHorizontal ? "scrollLeft" : "scrollTop" |
| 26 | +
|
| 27 | + function getUniqueIdFromDataSources() { |
| 28 | + return dataSources.map((dataSource) => dataSource[dataKey]) |
| 29 | + } |
| 30 | +
|
| 31 | + function onItemResized({id, size}) { |
| 32 | + virtual.saveSize(id, size) |
| 33 | + } |
| 34 | +
|
| 35 | + function onRangeChanged(range_) { |
| 36 | + range = range_ |
| 37 | + } |
| 38 | +
|
| 39 | + function onScroll(evt) { |
| 40 | + const offset = getOffset() |
| 41 | + const clientSize = getClientSize() |
| 42 | + const scrollSize = getScrollSize() |
| 43 | +
|
| 44 | + // iOS scroll-spring-back behavior will make direction mistake |
| 45 | + if (offset < 0 || (offset + clientSize > scrollSize + 1) || !scrollSize) { |
| 46 | + return |
| 47 | + } |
| 48 | +
|
| 49 | + virtual.handleScroll(offset) |
| 50 | + } |
| 51 | +
|
| 52 | + function getOffset() { |
| 53 | + if (pageMode) { |
| 54 | + return document.documentElement[directionKey] || document.body[directionKey] |
| 55 | + } else { |
| 56 | + return root ? Math.ceil(root[directionKey]) : 0 |
| 57 | + } |
| 58 | + } |
| 59 | +
|
| 60 | + function getClientSize() { |
| 61 | + const key = isHorizontal ? "clientWidth" : "clientHeight" |
| 62 | + if (pageMode) { |
| 63 | + return document.documentElement[key] || document.body[key] |
| 64 | + } else { |
| 65 | + return root ? Math.ceil(root[key]) : 0 |
| 66 | + } |
| 67 | + } |
| 68 | +
|
| 69 | + function getScrollSize() { |
| 70 | + const key = isHorizontal ? "scrollWidth" : "scrollHeight" |
| 71 | + if (pageMode) { |
| 72 | + return document.documentElement[key] || document.body[key] |
| 73 | + } else { |
| 74 | + return root ? Math.ceil(root[key]) : 0 |
| 75 | + } |
| 76 | + } |
| 77 | +
|
| 78 | + $: paddingStyle = isHorizontal ? `0px ${range.padBehind}px 0px ${range.padFront}px` : `${range.padFront}px 0px ${range.padBehind}px` |
| 79 | +</script> |
| 80 | + |
| 81 | +<div bind:this={root} on:scroll={onScroll} style="overflow-y: auto; height: inherit"> |
| 82 | + <div style={paddingStyle}> |
| 83 | + {#each dataSources.slice(range.start, range.end + 1) as data} |
| 84 | + <Item on:resize={(e) => onItemResized(e.detail)} {...data}/> |
| 85 | + {/each} |
| 86 | + </div> |
| 87 | +</div> |
0 commit comments