Skip to content

Commit 4acc92d

Browse files
author
Sebastian Flick
committed
finish loading contant by scrolling
1 parent 219eb8f commit 4acc92d

File tree

2 files changed

+74
-22
lines changed

2 files changed

+74
-22
lines changed

src/routes/fassungen/[thirties]/+page.svelte

+64-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,74 @@
11
<script>
22
import { base } from '$app/paths';
3-
import { SvelteMap } from 'svelte/reactivity';
43
import FassungenContent from './FassungenContent.svelte';
54
65
/** @type {{data: import('./$types').PageData}} */
76
let { data } = $props();
7+
class localPageClass {
8+
/**
9+
* @type {[[number, string][],[number, string][],[number, string][],[number, string][]]}
10+
*/
11+
pages = $state([[], [], [], []]);
12+
/**
13+
* @type {string[]}
14+
*/
15+
thirties = [];
816
9-
let localPages = $state([new SvelteMap(), new SvelteMap(), new SvelteMap(), new SvelteMap()]);
17+
fetchPage = async (/** @type {string} */ page) => {
18+
// console.log('fetching', page);
19+
// console.log(Number(page), !this.thirties.length);
20+
if (!this.thirties.length) {
21+
let [d, m, G, T] = await fetch(`${base}/fassungen/data/${Number(page) - 1}`).then((r) =>
22+
r.json()
23+
);
24+
this.pages[0].push([Number(page) - 1, d]);
25+
this.pages[1].push([Number(page) - 1, m]);
26+
this.pages[2].push([Number(page) - 1, G]);
27+
this.pages[3].push([Number(page) - 1, T]);
28+
this.thirties.push(String(Number(page) - 1));
29+
[d, m, G, T] = await fetch(`${base}/fassungen/data/${page}`).then((r) => r.json());
30+
this.pages[0].push([Number(page), d]);
31+
this.pages[1].push([Number(page), m]);
32+
this.pages[2].push([Number(page), G]);
33+
this.pages[3].push([Number(page), T]);
34+
this.thirties.push(page);
35+
[d, m, G, T] = await fetch(`${base}/fassungen/data/${Number(page) + 1}`).then((r) =>
36+
r.json()
37+
);
38+
this.pages[0].push([Number(page) + 1, d]);
39+
this.pages[1].push([Number(page) + 1, m]);
40+
this.pages[2].push([Number(page) + 1, G]);
41+
this.pages[3].push([Number(page) + 1, T]);
42+
this.thirties.push(String(Number(page) + 1));
43+
} else {
44+
if (Number(page) < Number(this.thirties[0])) {
45+
// console.log('fetching before');
46+
let [d, m, G, T] = await fetch(`${base}/fassungen/data/${page}`).then((r) => r.json());
47+
this.pages[0].unshift([Number(page), d]);
48+
this.pages[1].unshift([Number(page), m]);
49+
this.pages[2].unshift([Number(page), G]);
50+
this.pages[3].unshift([Number(page), T]);
51+
this.thirties.unshift(page);
52+
} else if (Number(page) > Number(this.thirties[this.thirties.length - 1])) {
53+
let [d, m, G, T] = await fetch(`${base}/fassungen/data/${page}`).then((r) => r.json());
54+
this.pages[0].push([Number(page), d]);
55+
this.pages[1].push([Number(page), m]);
56+
this.pages[2].push([Number(page), G]);
57+
this.pages[3].push([Number(page), T]);
58+
this.thirties.push(page);
59+
} else if (Number(page) === Number(this.thirties[0])) {
60+
this.fetchPage(String(Number(page) - 1));
61+
} else if (Number(page) === Number(this.thirties[this.thirties.length - 1])) {
62+
this.fetchPage(String(Number(page) + 1));
63+
}
64+
}
65+
};
66+
}
1067
11-
const checkAndFetch = async (/** @type {string} */ page) => {
12-
if (!localPages[0].has(page)) {
13-
let [d, m, G, T] = await fetch(`${base}/fassungen/data/${page}`).then((r) => r.json());
14-
localPages[0].set(page, d);
15-
localPages[1].set(page, m);
16-
localPages[2].set(page, G);
17-
localPages[3].set(page, T);
18-
}
19-
};
68+
let localPages = new localPageClass();
2069
21-
$effect(async () => {
22-
// need to await the fetches because of race condition: random order of thirties
23-
await checkAndFetch(String(Number(data.thirties) - 1));
24-
await checkAndFetch(data.thirties);
25-
await checkAndFetch(String(Number(data.thirties) + 1));
70+
$effect(() => {
71+
localPages.fetchPage(data.thirties);
2672
});
2773
</script>
2874

@@ -34,8 +80,8 @@
3480
<div
3581
class="grid grid-cols-[repeat(auto-fit,minmax(400px,1fr))] gap-4 bg-surface-active-token my-4 py-4 px-8 rounded-xl"
3682
>
37-
{#each localPages as pages}
38-
{#if pages.size >= 3}
83+
{#each localPages.pages as pages}
84+
{#if pages.length >= 3}
3985
<!-- when at least 3 pages are loaded, the one for the currect thirties should be loaded aswell -->
4086
<FassungenContent {pages} />
4187
{/if}

src/routes/fassungen/[thirties]/FassungenContent.svelte

+10-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
(entries) => {
1818
entries.forEach((entry) => {
1919
if (entry.isIntersecting) {
20-
const direction = entry.boundingClientRect.y < 0 ? -1 : 1;
21-
goto(`${base}/fassungen/${Number($page.data.thirties) + direction}`);
20+
console.log('intersecting', entry.target);
21+
let verse = entry.target
22+
.querySelector(`[data-verse]`)
23+
?.attributes['data-verse']?.value.split('.')[0];
24+
if (entry.target && verse) {
25+
goto(`${base}/fassungen/${verse}`, { noScroll: true, keepFocus: true });
26+
}
2227
}
2328
});
2429
},
@@ -30,6 +35,7 @@
3035
);
3136
const verse = scrollContainer?.querySelector(`[data-verse="${$page.data.thirties}.01"]`);
3237
if (verse) {
38+
console.log('scrolling to', verse);
3339
scrollContainer?.scrollTo({
3440
top:
3541
scrollContainer?.scrollTop +
@@ -53,9 +59,9 @@
5359
</script>
5460
5561
<div class="max-h-[70vh] overflow-y-auto" bind:this={scrollContainer}>
56-
{#each pages as page}
62+
{#each pages as page (page[0])}
5763
<div class="thirty" use:addToObserver>
58-
{@html page}
64+
{@html page[1]}
5965
</div>
6066
<hr class="!border-t-4 !border-primary-500" />
6167
{/each}

0 commit comments

Comments
 (0)