Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix viewer search and consolidate search routing, too #862

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions src/lib/components/forms/Search.svelte
Original file line number Diff line number Diff line change
@@ -1,24 +1,57 @@
<script lang="ts">
import type { Maybe } from "$lib/api/types";

import { goto } from "$app/navigation";
import { page } from "$app/stores";

import { Search16, XCircleFill24 } from "svelte-octicons";
import { _ } from "svelte-i18n";
import type { Maybe, Nullable } from "$lib/api/types";

export let name: Nullable<string> = null;
export let name: string = "q";
export let query: string = "";
export let placeholder: string = $_("common.search");
export let action: Maybe<string> = undefined;

let input: HTMLInputElement;
let form: HTMLFormElement;

function clear() {
function clear(): Promise<void> {
query = "";
input.focus();

const url = new URL($page.url);
url.searchParams.delete(name);
return goto(url);
}

function search(e: Event): Promise<void> {
e.preventDefault();
const form = e.target as HTMLFormElement;
const fd = new FormData(form);
const q = fd.get(name) as string;
const url = new URL($page.url);

if (q) {
url.searchParams.set(name, q);
} else {
url.searchParams.delete(name);
}

return goto(url);
}
</script>

<form class="container" {action} on:submit on:reset bind:this={form}>
<label for="query" title="Search"><Search16 /></label>
<form
class="container"
{action}
on:submit={search}
on:reset={clear}
bind:this={form}
>
<label for="query" title="Search">
<Search16 />
<span class="sr-only">{$_("common.search")}</span>
</label>
<input
type="search"
id="query"
Expand All @@ -29,7 +62,6 @@
bind:this={input}
on:change
on:input
on:reset
/>
<button
title="Clear Search"
Expand Down
9 changes: 6 additions & 3 deletions src/lib/components/viewer/PDFPage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Selectable text can be rendered in one of two ways:
<script lang="ts">
import type { TextPosition } from "$lib/api/types";

import { page as pageStore } from "$app/stores";

import * as pdfjs from "pdfjs-dist/legacy/build/pdf.mjs";
import { _ } from "svelte-i18n";

Expand All @@ -28,21 +30,21 @@ Selectable text can be rendered in one of two ways:
getDocument,
getCurrentMode,
getPDF,
getQuery,
} from "$lib/components/viewer/ViewerContext.svelte";
import { fitPage, getNotes } from "@/lib/utils/viewer";
import { getQuery } from "$lib/utils/search";
import { fitPage, getNotes } from "$lib/utils/viewer";

export let page_number: number; // 1-indexed

export let scale: number | "width" | "height";
export let width: number;
export let height: number;
export let text: TextPosition[] = [];
export let query: string = getQuery($pageStore.url, "q");

const documentStore = getDocument();
const mode = getCurrentMode();
const pdf = getPDF();
const query = getQuery();

// make hidden things visible, for debugging
export let debug = false;
Expand All @@ -60,6 +62,7 @@ Selectable text can be rendered in one of two ways:
// visibility, for loading optimization
let visible: boolean = false;

$: query = getQuery($pageStore.url, "q");
$: document = $documentStore;
$: aspect = height / width;
$: orientation = height > width ? "vertical" : "horizontal";
Expand Down
8 changes: 5 additions & 3 deletions src/lib/components/viewer/ReadingToolbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Assumes it's a child of a ViewerContext
<script lang="ts">
import type { ReadMode, WriteMode } from "$lib/api/types";

import { page } from "$app/stores";

import { _ } from "svelte-i18n";
import {
Comment16,
Expand All @@ -29,18 +31,18 @@ Assumes it's a child of a ViewerContext

import { remToPx } from "$lib/utils/layout";
import { getViewerHref } from "$lib/utils/viewer";
import { getQuery } from "$lib/utils/search";
import {
getCurrentMode,
getDocument,
getPDFProgress,
getQuery,
isEmbedded,
} from "$lib/components/viewer/ViewerContext.svelte";

export let query = getQuery($page.url, "q");

let width: number;

const documentStore = getDocument();
const query = getQuery();
const mode = getCurrentMode();
const embed = isEmbedded();

Expand Down
14 changes: 6 additions & 8 deletions src/lib/components/viewer/Text.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
Assumes it's a child of a ViewerContext
-->
<script lang="ts">
import { page } from "$app/stores";
import { onMount } from "svelte";

import Page from "./Page.svelte";
import {
getText,
getCurrentPage,
getQuery,
getZoom,
} from "./ViewerContext.svelte";
import { getText, getCurrentPage, getZoom } from "./ViewerContext.svelte";
import { scrollToPage } from "$lib/utils/scroll";
import { highlight } from "$lib/utils/search";
import { getQuery } from "$lib/utils/search";

export let query = getQuery($page.url, "q");

const text = getText();
const query = getQuery();
const currentPage = getCurrentPage();
const text = getText();
const zoom = getZoom();

onMount(async () => {
Expand Down
6 changes: 0 additions & 6 deletions src/lib/components/viewer/ViewerContext.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ layouts, stories, and tests.
return getContext("asset_url");
}

export function getQuery(): string {
return getContext("query") ?? "";
}

export function isEmbedded(): boolean {
// are we embedded?
return getContext("embed") ?? false;
Expand Down Expand Up @@ -99,7 +95,6 @@ layouts, stories, and tests.
export let embed: boolean = false;
export let page: number = 1;
export let mode: ViewerMode = "document";
export let query: string = "";
export let zoom: Zoom = 1;
export let errors: Error[] = [];

Expand All @@ -123,7 +118,6 @@ layouts, stories, and tests.
setContext("text", text);
setContext("asset_url", asset_url);
setContext("embed", embed);
setContext("query", query);
setContext("newNote", writable(null));
setContext("currentNote", writable(note));
setContext("currentPage", writable(page));
Expand Down
24 changes: 21 additions & 3 deletions src/lib/components/viewer/stories/PDFPage.stories.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<script context="module" lang="ts">
import type { Document, Section } from "$lib/api/types";
import type { Document } from "$lib/api/types";

import { page } from "$app/stores";

import { writable } from "svelte/store";
import { Story } from "@storybook/addon-svelte-csf";
import PdfPage from "../PDFPage.svelte";

Expand All @@ -14,7 +18,6 @@
import textPositions from "@/test/fixtures/documents/examples/the-santa-anas-p1.position.json";
import pdfFile from "@/test/fixtures/documents/examples/the-santa-anas.pdf";
import ViewerContext from "../ViewerContext.svelte";
import { writable } from "svelte/store";

const document = { ...doc, edit_access: true } as Document;

Expand Down Expand Up @@ -58,8 +61,23 @@
</ViewerContext>
</Story>

<Story name="search results">
<Story
name="search results"
parameters={{
sveltekit_experimental: {
stores: {
page: {
url: new URL(
`https://www.dev.documentcloud.org/documents/20000040-the-santa-anas/?q=${query}`,
),
},
},
},
}}
>
<ViewerContext {document} pdf={writable(load(url))}>
<p>Query: {query}</p>
<p>URL: {$page.url}</p>
<PdfPage page_number={1} scale={1.5} {width} {height} />
</ViewerContext>
</Story>
Expand Down
7 changes: 5 additions & 2 deletions src/lib/utils/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export function pageNumber(page: string): number {
return number;
}

export function getQuery(url: URL, param: string = "q"): string {
return url.searchParams.get(param) ?? "";
export function getQuery(url?: Nullable<URL>, param: string = "q"): string {
if (!url) {
console.error("Missing URL");
}
return url?.searchParams?.get(param) ?? "";
}
1 change: 1 addition & 0 deletions src/routes/(app)/documents/[id]-[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
$: action = data.action;
$: addons = data.pinnedAddons;
$: hasDescription = Boolean(document.description?.trim().length);
$: query = data.query || "";
</script>

<svelte:head>
Expand Down
24 changes: 0 additions & 24 deletions src/routes/(app)/projects/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,6 @@
if (cursor) target.searchParams.set("cursor", cursor);
goto(target);
}

function search(e: Event) {
e.preventDefault();
const form = e.target as HTMLFormElement;
const fd = new FormData(form);
const q = fd.get("query") as string;
const url = new URL($page.url);

if (q) {
url.searchParams.set("query", q);
} else {
url.searchParams.delete("query");
}

return goto(url);
}

function reset() {
const url = new URL($page.url);
url.searchParams.delete("query");
return goto(url);
}
</script>

<svelte:head>
Expand Down Expand Up @@ -104,8 +82,6 @@
name="query"
placeholder={$_("projects.placeholder.projects")}
{query}
on:submit={search}
on:reset={reset}
/>
</PageToolbar>

Expand Down
2 changes: 1 addition & 1 deletion src/routes/embed/documents/[id]-[slug]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function load({ fetch, url, params, depends }) {

let settings: Partial<EmbedSettings> = getEmbedSettings(url.searchParams);

const query = getQuery(url);
const query = getQuery(url, "q");

return {
document,
Expand Down