Skip to content

Commit

Permalink
Move Search.svelte into forms because it's a form. Implement viewer m…
Browse files Browse the repository at this point in the history
…ode switching.
  • Loading branch information
eyeseast committed Apr 29, 2024
1 parent ca286a1 commit 7ed92ef
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/lib/api/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { isOrg } from "@/api/types/orgAndUser";
import { APP_URL, BASE_API_URL, CSRF_HEADER_NAME } from "@/config/config.js";
import { isErrorCode } from "../utils";

export const MODES = new Set(["document", "text", "thumbnails", "notes"]);

/**
* Search documents
* https://www.documentcloud.org/help/search/
Expand Down
4 changes: 3 additions & 1 deletion src/lib/api/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ export type Access = "public" | "private" | "organization"; // https://www.docum

export type Data = Record<string, string[]>;

export type Highlights = Record<string, string[]>;

export type Status = "success" | "readable" | "pending" | "error" | "nofile"; // https://www.documentcloud.org/help/api#statuses

export type Sizes = "thumbnail" | "small" | "normal" | "large" | "xlarge";

export type Highlights = Record<string, string[]>;
export type ViewerMode = "document" | "text" | "thumbnails" | "notes";

export interface NoteHighlight {
title: string[];
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import ResultsList from "../../documents/ResultsList.svelte";
import ContentLayout from "../ContentLayout.svelte";
import PageToolbar from "../../common/PageToolbar.svelte";
import Search from "../../inputs/Search.svelte";
import Search from "../../forms/Search.svelte";
import { addons } from "@/test/handlers/addons";
Expand Down
2 changes: 1 addition & 1 deletion src/routes/app/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import MainLayout from "$lib/components/layouts/MainLayout.svelte";
import ContentLayout from "$lib/components/layouts/ContentLayout.svelte";
import PageToolbar from "$lib/components/common/PageToolbar.svelte";
import Search from "$lib/components/inputs/Search.svelte";
import Search from "@/lib/components/forms/Search.svelte";
import Empty from "$lib/components/common/Empty.svelte";
import Button from "$lib/components/common/Button.svelte";
import SignedIn from "$lib/components/common/SignedIn.svelte";
Expand Down
2 changes: 1 addition & 1 deletion src/routes/app/add-ons/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { Hourglass24, Plug24 } from "svelte-octicons";
import Paginator from "@/common/Paginator.svelte";
import Search from "@/lib/components/inputs/Search.svelte";
import Search from "@/lib/components/forms/Search.svelte";
import Pin from "@/common/icons/Pin.svelte";
import Star from "@/common/icons/Star.svelte";
import ContentLayout from "$lib/components/layouts/ContentLayout.svelte";
Expand Down
2 changes: 1 addition & 1 deletion src/routes/app/projects/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import PageToolbar from "@/lib/components/common/PageToolbar.svelte";
import MainLayout from "@/lib/components/layouts/MainLayout.svelte";
import ContentLayout from "@/lib/components/layouts/ContentLayout.svelte";
import Search from "@/lib/components/inputs/Search.svelte";
import Search from "@/lib/components/forms/Search.svelte";
import SidebarItem from "@/lib/components/sidebar/SidebarItem.svelte";
import ProjectListItem from "@/lib/components/projects/ProjectListItem.svelte";
Expand Down
2 changes: 1 addition & 1 deletion src/routes/app/projects/[id]-[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import Empty from "@/lib/components/common/Empty.svelte";
import Error from "@/lib/components/common/Error.svelte";
import { projectSearchUrl } from "@/lib/utils/search";
import Search from "@/lib/components/inputs/Search.svelte";
import Search from "@/lib/components/forms/Search.svelte";
import ResultsList, {
selected,
total,
Expand Down
35 changes: 26 additions & 9 deletions src/routes/documents/[id]-[slug]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
<script lang="ts">
import { goto } from "$app/navigation";
import { page } from "$app/stores";
import ContentLayout from "$lib/components/layouts/ContentLayout.svelte";
import PageToolbar from "$lib/components/common/PageToolbar.svelte";
import TextPage from "$lib/components/documents/TextPage.svelte";
export let data;
const modes = [
const modes = new Map([
["document", "Document"],
["text", "Text"],
["thumbnails", "Thumbnails"],
["notes", "Notes"],
];
]);
$: mode = modes.has(data.mode) ? data.mode : "document";
$: text = data.text;
function setMode(e) {
const mode = e.target.value;
const u = new URL($page.url);
u.searchParams.set("mode", mode);
goto(u);
}
</script>

<ContentLayout>
{#await data.text then { pages }}
{#each pages as { page, contents }}
<TextPage {page} {contents} />
{/each}
{/await}
{#if mode === "text"}
{#await text then { pages }}
{#each pages as { page, contents }}
<TextPage {page} {contents} />
{/each}
{/await}
{/if}

<PageToolbar slot="footer">
<select name="mode" slot="left">
{#each modes as [value, name]}
<select name="mode" slot="left" value={mode} on:change={setMode}>
{#each modes.entries() as [value, name]}
<option {value}>{name}</option>
{/each}
</select>
Expand Down
22 changes: 19 additions & 3 deletions src/routes/documents/[id]-[slug]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import type { DocumentText, ViewerMode } from "$lib/api/types.js";
import * as documents from "$lib/api/documents";

export async function load({ fetch, parent, url }) {
const mode = url.searchParams.get("mode") ?? "document";
const { document } = await parent();
let mode: ViewerMode =
(url.searchParams.get("mode") as ViewerMode) ?? "document";

if (!documents.MODES.has(mode)) {
mode = "document";
}

// only load text in text mode
let text: Promise<DocumentText> = Promise.resolve({
updated: 0,
pages: [],
});

if (mode === "text") {
const { document } = await parent();
text = documents.text(document, fetch);
}

return {
mode,
text: documents.text(document, fetch),
text,
};
}

0 comments on commit 7ed92ef

Please sign in to comment.