blob: b5cc725fd2ebc146188cdce076bfa09b2ad927c8 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
<script lang="ts" module>
import { Layout, type PageFragment } from '$gql/graphql';
import { getContext, setContext } from 'svelte';
export interface Chunk {
main: PageFragment;
secondary?: PageFragment;
index: number;
}
class ReaderContext {
visible = $state(false);
sidebar = $state(false);
pages: PageFragment[] = $state([]);
page = $state(0);
open = (page: number) => {
this.page = page;
this.visible = true;
};
}
export function initReaderContext() {
return setContext<ReaderContext>('reader', new ReaderContext());
}
export function getReaderContext() {
return getContext<ReaderContext>('reader');
}
export function partition(pages: PageFragment[], layout: Layout): [Chunk[], number[]] {
const single = layout === Layout.Single;
const offset = layout === Layout.DoubleOffset;
const chunks: Chunk[] = [];
const lookup: number[] = Array<number>(pages.length);
for (let chunkIndex = 0, pageIndex = 0; pageIndex < pages.length; chunkIndex++) {
const wide = () => pages[pageIndex].image.aspectRatio > 1;
const nextPage = () => {
lookup[pageIndex] = chunkIndex;
return pages[pageIndex++];
};
const offsetFirst = pageIndex === 0 && offset;
const full = single || wide() || offsetFirst;
const chunk: Chunk = { index: pageIndex, main: nextPage() };
if (!full && pageIndex < pages.length) {
if (!wide()) {
chunk.secondary = nextPage();
}
}
chunks.push(chunk);
}
return [chunks, lookup];
}
</script>
<script lang="ts">
import { trapFocus } from '$lib/Actions';
import { fadeDefault, slideXDefault } from '$lib/Transitions';
import type { Snippet } from 'svelte';
import { fade, slide } from 'svelte/transition';
import CloseReaderButton from './components/CloseReaderButton.svelte';
import PageIndicator from './components/PageIndicator.svelte';
import ReaderMenuButton from './components/ReaderMenuButton.svelte';
let { sidebar, children }: { sidebar?: Snippet; children?: Snippet } = $props();
const reader = getReaderContext();
</script>
{#if reader.visible}
<div
role="dialog"
class="fixed bottom-0 left-0 right-0 top-0 z-10 flex h-full w-full bg-black"
transition:fade={fadeDefault}
use:trapFocus
>
{#if sidebar && reader.sidebar}
<aside class="w-[36rem] shrink-0 bg-slate-800" transition:slide={slideXDefault}>
<div class="flex h-full min-w-[36rem] flex-col gap-4 overflow-auto p-4">
{@render sidebar?.()}
</div>
</aside>
{/if}
<main class="relative flex grow">
<div class="absolute flex w-full p-1 text-lg [&>*:last-child]:ml-auto">
{#if sidebar}
<ReaderMenuButton />
{/if}
<CloseReaderButton />
</div>
<div class="absolute bottom-0 right-0 z-10 flex p-1 text-lg">
<PageIndicator />
</div>
<div class="flex grow">
{@render children?.()}
</div>
</main>
</div>
{/if}
|