From f43273cd84b27c172560f031707347173e2cab6b Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Wed, 15 May 2024 17:19:22 -0400 Subject: [PATCH] Switch between text and document views without losing page position --- src/lib/components/documents/Text.svelte | 59 +++++++++++++++++++ .../documents/stories/Text.stories.svelte | 23 ++++++++ .../documents/stories/TextPage.stories.svelte | 3 +- src/lib/utils/scroll.ts | 14 +++++ src/routes/documents/[id]-[slug]/+layout.ts | 15 +---- src/routes/documents/[id]-[slug]/+page.svelte | 39 +++--------- src/routes/documents/[id]-[slug]/+page.ts | 18 +++++- 7 files changed, 124 insertions(+), 47 deletions(-) create mode 100644 src/lib/components/documents/Text.svelte create mode 100644 src/lib/components/documents/stories/Text.stories.svelte create mode 100644 src/lib/utils/scroll.ts diff --git a/src/lib/components/documents/Text.svelte b/src/lib/components/documents/Text.svelte new file mode 100644 index 000000000..6abf7587a --- /dev/null +++ b/src/lib/components/documents/Text.svelte @@ -0,0 +1,59 @@ + + + +
+ {#await text} + + {#each Array(total).fill(null) as p, n} + +
+
+ {/each} + {:then { pages }} + {#each pages as { page, contents }} + + {/each} + {/await} +
+ + diff --git a/src/lib/components/documents/stories/Text.stories.svelte b/src/lib/components/documents/stories/Text.stories.svelte new file mode 100644 index 000000000..d70b3fd18 --- /dev/null +++ b/src/lib/components/documents/stories/Text.stories.svelte @@ -0,0 +1,23 @@ + + + + + + + + + diff --git a/src/lib/components/documents/stories/TextPage.stories.svelte b/src/lib/components/documents/stories/TextPage.stories.svelte index c15474c3c..b5d8ebbe9 100644 --- a/src/lib/components/documents/stories/TextPage.stories.svelte +++ b/src/lib/components/documents/stories/TextPage.stories.svelte @@ -3,9 +3,8 @@ import TextPage from "../TextPage.svelte"; export const meta = { - title: "Components / Documents / Text", + title: "Components / Documents / Text page", component: TextPage, - tags: ["autodocs"], parameters: { layout: "centered" }, }; diff --git a/src/lib/utils/scroll.ts b/src/lib/utils/scroll.ts new file mode 100644 index 000000000..4bc0f4fbf --- /dev/null +++ b/src/lib/utils/scroll.ts @@ -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(); +} diff --git a/src/routes/documents/[id]-[slug]/+layout.ts b/src/routes/documents/[id]-[slug]/+layout.ts index 61a2bb7c3..bfed40715 100644 --- a/src/routes/documents/[id]-[slug]/+layout.ts +++ b/src/routes/documents/[id]-[slug]/+layout.ts @@ -7,10 +7,9 @@ import type { Document } from "@/lib/api/types"; import { redirect, error } from "@sveltejs/kit"; -import { DC_BASE } from "@/config/config.js"; import * as documents from "$lib/api/documents"; import { getPinnedAddons } from "$lib/api/addons"; -import { breadcrumbTrail, getPrivateAsset } from "$lib/utils/index"; +import { breadcrumbTrail } from "$lib/utils/index"; function documentPath(document: Document) { return `/documents/${document.id}-${document.slug}/`; @@ -34,23 +33,11 @@ export async function load({ fetch, params, parent }) { { href: documentPath(document), title: document.title }, ]); - let asset_url = documents.pdfUrl(document); - - // assets still processing are in private storage until finished - if (document.access !== "public" || String(asset_url).startsWith(DC_BASE)) { - asset_url = await getPrivateAsset(asset_url, fetch).catch((e) => { - console.error(e); - console.error(asset_url.href); - return asset_url; - }); - } - // stream this const pinnedAddons = getPinnedAddons(fetch); return { document, - asset_url, pinnedAddons, breadcrumbs, }; diff --git a/src/routes/documents/[id]-[slug]/+page.svelte b/src/routes/documents/[id]-[slug]/+page.svelte index 712554ed6..fa303ee63 100644 --- a/src/routes/documents/[id]-[slug]/+page.svelte +++ b/src/routes/documents/[id]-[slug]/+page.svelte @@ -1,7 +1,6 @@