Skip to content

Commit 9b25162

Browse files
committed
Init
0 parents  commit 9b25162

File tree

17 files changed

+1959
-0
lines changed

17 files changed

+1959
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules/
2+
/public/build/
3+
dist/
4+
.idea/
5+
.DS_Store

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Svelte virtual scroll list
2+
3+
Svelte implementation of vue library [vue-virtual-scroll-list](https://github.com/tangbc/vue-virtual-scroll-list)
4+
5+
## Doesn't fork for now

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "svelte-virtual-scroll-list",
3+
"version": "1.0.0",
4+
"private": true,
5+
"scripts": {
6+
"build": "rollup -c rollup/rollup.config.dev.js",
7+
"dev": "rollup -c rollup/rollup.config.dev.js -w",
8+
"start": "sirv public --no-clear"
9+
},
10+
"devDependencies": {
11+
"@rollup/plugin-commonjs": "^17.0.0",
12+
"@rollup/plugin-node-resolve": "^11.0.0",
13+
"rollup": "^2.3.4",
14+
"rollup-plugin-css-only": "^3.1.0",
15+
"rollup-plugin-livereload": "^2.0.0",
16+
"rollup-plugin-svelte": "^7.0.0",
17+
"rollup-plugin-terser": "^7.0.0",
18+
"sirv-cli": "^1.0.0",
19+
"svelte": "^3.0.0"
20+
}
21+
}

public/favicon.png

3.05 KB
Loading

public/global.css

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
html, body {
2+
position: relative;
3+
width: 100%;
4+
height: 100%;
5+
}
6+
7+
body {
8+
color: #333;
9+
margin: 0;
10+
padding: 8px;
11+
box-sizing: border-box;
12+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
13+
}
14+
15+
a {
16+
color: rgb(0,100,200);
17+
text-decoration: none;
18+
}
19+
20+
a:hover {
21+
text-decoration: underline;
22+
}
23+
24+
a:visited {
25+
color: rgb(0,80,160);
26+
}
27+
28+
label {
29+
display: block;
30+
}
31+
32+
input, button, select, textarea {
33+
font-family: inherit;
34+
font-size: inherit;
35+
-webkit-padding: 0.4em 0;
36+
padding: 0.4em;
37+
margin: 0 0 0.5em 0;
38+
box-sizing: border-box;
39+
border: 1px solid #ccc;
40+
border-radius: 2px;
41+
}
42+
43+
input:disabled {
44+
color: #ccc;
45+
}
46+
47+
button {
48+
color: #333;
49+
background-color: #f4f4f4;
50+
outline: none;
51+
}
52+
53+
button:disabled {
54+
color: #999;
55+
}
56+
57+
button:not(:disabled):active {
58+
background-color: #ddd;
59+
}
60+
61+
button:focus {
62+
border-color: #666;
63+
}

public/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset='utf-8'>
5+
<meta name='viewport' content='width=device-width,initial-scale=1'>
6+
7+
<title>Svelte app</title>
8+
9+
<link rel='icon' type='image/png' href='/favicon.png'>
10+
<link rel='stylesheet' href='/global.css'>
11+
<link rel='stylesheet' href='/build/bundle.css'>
12+
13+
<script defer src='/build/bundle.js' type="module"></script>
14+
</head>
15+
16+
<body>
17+
</body>
18+
</html>

rollup/rollup.config.dev.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import svelte from "rollup-plugin-svelte"
2+
import commonjs from "@rollup/plugin-commonjs"
3+
import resolve from "@rollup/plugin-node-resolve"
4+
import livereload from "rollup-plugin-livereload"
5+
import css from "rollup-plugin-css-only"
6+
7+
const production = !process.env.ROLLUP_WATCH
8+
9+
function serve() {
10+
let server
11+
12+
function toExit() {
13+
if (server) server.kill(0)
14+
}
15+
16+
return {
17+
writeBundle() {
18+
if (server) return
19+
server = require("child_process").spawn("npm", ["run", "start", "--", "--dev"], {
20+
stdio: ["ignore", "inherit", "inherit"],
21+
shell: true,
22+
})
23+
24+
process.on("SIGTERM", toExit)
25+
process.on("exit", toExit)
26+
},
27+
}
28+
}
29+
30+
export default {
31+
input: "test/main.js",
32+
output: {
33+
sourcemap: true,
34+
format: "es",
35+
name: "app",
36+
file: "public/build/bundle.js",
37+
},
38+
plugins: [
39+
svelte({
40+
compilerOptions: {
41+
// enable run-time checks when not in production
42+
dev: !production,
43+
},
44+
}),
45+
// we'll extract any component CSS out into
46+
// a separate file - better for performance
47+
css({output: "bundle.css"}),
48+
49+
// If you have external dependencies installed from
50+
// npm, you'll most likely need these plugins. In
51+
// some cases you'll need additional configuration -
52+
// consult the documentation for details:
53+
// https://github.com/rollup/plugins/tree/master/packages/commonjs
54+
resolve({
55+
browser: true,
56+
dedupe: ["svelte"],
57+
}),
58+
commonjs(),
59+
60+
// In dev mode, call `npm run start` once
61+
// the bundle has been generated
62+
!production && serve(),
63+
64+
// Watch the `public` directory and refresh the
65+
// browser on changes when not in production
66+
!production && livereload("public"),
67+
],
68+
watch: {
69+
clearScreen: false,
70+
},
71+
}

src/Item.svelte

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<script>
2+
import {afterUpdate, createEventDispatcher, onDestroy, onMount} from "svelte"
3+
4+
export let horizontal = false
5+
export let height
6+
export let uniqueKey
7+
8+
let resizeObserver
9+
let itemDiv
10+
11+
const dispatch = createEventDispatcher()
12+
const shapeKey = horizontal ? "offsetWidth" : "offsetHeight"
13+
14+
onMount(() => {
15+
if (typeof ResizeObserver !== "undefined") {
16+
resizeObserver = new ResizeObserver(() => {
17+
dispatchSizeChange()
18+
})
19+
resizeObserver.observe(itemDiv)
20+
}
21+
})
22+
afterUpdate(dispatchSizeChange)
23+
onDestroy(() => {
24+
if (resizeObserver) {
25+
resizeObserver.disconnect()
26+
resizeObserver = null
27+
}
28+
})
29+
30+
function getCurrentSize() {
31+
return itemDiv ? itemDiv[shapeKey] : 0
32+
}
33+
34+
// tell parent current size identify by unqiue key
35+
function dispatchSizeChange() {
36+
dispatch("resize", {id: uniqueKey, size: getCurrentSize()})
37+
}
38+
</script>
39+
40+
<div bind:this={itemDiv} style="height: {height}px">
41+
{uniqueKey} Item ({height}px)
42+
</div>
43+
<style>
44+
div {
45+
border: 1px solid gray;
46+
}
47+
</style>

src/VirtualScroll.svelte

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)