-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch between text and document views without losing page position
- Loading branch information
Showing
7 changed files
with
124 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<!-- | ||
@component | ||
Text wraps a list of TextPage components for loading and management | ||
--> | ||
<script lang="ts"> | ||
import type { Writable } from "svelte/store"; | ||
import type { DocumentText } from "$lib/api/types"; | ||
import { getContext, onMount } from "svelte"; | ||
import Page from "./Page.svelte"; | ||
import TextPage from "./TextPage.svelte"; | ||
import { scrollToPage } from "$lib/utils/scroll"; | ||
export let text: Promise<DocumentText> | DocumentText; | ||
export let total: number = 0; | ||
export let zoom: number = 1; | ||
const currentPage: Writable<number> = getContext("currentPage"); | ||
onMount(async () => { | ||
await text; // wait until it loads | ||
if ($currentPage > 1) { | ||
scrollToPage($currentPage); | ||
} | ||
}); | ||
</script> | ||
|
||
<div class="textPages" style:--zoom={zoom}> | ||
{#await text} | ||
<!-- loading state--> | ||
{#each Array(total).fill(null) as p, n} | ||
<Page page_number={n + 1} mode="text"> | ||
<div class="placeholder"></div> | ||
</Page> | ||
{/each} | ||
{:then { pages }} | ||
{#each pages as { page, contents }} | ||
<TextPage {page} {contents} /> | ||
{/each} | ||
{/await} | ||
</div> | ||
|
||
<style> | ||
.textPages { | ||
max-width: 48rem; | ||
padding: 0 1rem; | ||
margin: 0 auto; | ||
width: 100%; | ||
} | ||
.placeholder { | ||
aspect-ratio: calc(11 / 8.5); | ||
box-shadow: var(--shadow); | ||
width: 66ch; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<script context="module" lang="ts"> | ||
import type { DocumentText } from "$lib/api/types"; | ||
import { Story } from "@storybook/addon-svelte-csf"; | ||
import Text from "../Text.svelte"; | ||
export const meta = { | ||
title: "Components / Documents / Text", | ||
component: Text, | ||
parameters: { layout: "centered" }, | ||
}; | ||
import txt from "$lib/api/fixtures/documents/document.txt.json"; | ||
const loading: Promise<DocumentText> = new Promise(() => {}); | ||
</script> | ||
|
||
<Story name="default"> | ||
<Text text={txt} /> | ||
</Story> | ||
|
||
<Story name="waiting to load"> | ||
<Text text={loading} total={txt.pages.length} /> | ||
</Story> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { pageHashUrl } from "../api/documents"; | ||
|
||
/** | ||
* Scroll to a page using a standard ID | ||
* | ||
* @param n page number | ||
*/ | ||
export function scrollToPage(n: number): void { | ||
const pageId = pageHashUrl(n).replace("#", ""); | ||
const heading = window.document.getElementById(pageId); | ||
|
||
if (!heading) return console.error(`Missing page ${n}`); | ||
heading.scrollIntoView(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters