Skip to content

Commit

Permalink
Move text into document route, add an api method
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast committed Apr 28, 2024
1 parent abd8d2f commit 4970200
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/common/Dropdown2.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
position: relative;
}
.dropdownContainer.open {
z-index: var(--menuActive);
z-index: var(--menuActive, 16);
}
.title {
display: block;
Expand All @@ -143,7 +143,7 @@
border: 1px solid rgba(0, 0, 0, 0.1);
}
.title:hover {
background: var(--light-primary);
background: var(--light-primary, #eff7ff);
}
.title.open {
background: var(--primary);
Expand Down Expand Up @@ -187,7 +187,7 @@
}
.overlay {
z-index: var(--menuShim);
z-index: var(--menuShim, 14);
position: fixed;
top: 0;
left: 0;
Expand Down
33 changes: 32 additions & 1 deletion src/lib/api/documents.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { Document, DocumentUpload, Pending, Sizes } from "./types";
import type {
Document,
DocumentText,
DocumentUpload,
Pending,
Sizes,
} from "./types";

import { vi, test as base, describe, expect, afterEach } from "vitest";
import { APP_URL, DC_BASE, IMAGE_WIDTHS_ENTRIES } from "@/config/config.js";
Expand Down Expand Up @@ -31,11 +37,36 @@ const test = base.extend({

await use(pending);
},

text: async ({}, use: Use<DocumentText>) => {
const { default: text } = await import(
"./fixtures/documents/document.txt.json"
);

await use(text);
},
});

describe("document fetching", () => {
test.todo("documents.get");
test.todo("documents.search");

test("documents.text", async ({ document, text }) => {
const mockFetch = vi.fn().mockImplementation(async () => ({
ok: true,
async json() {
return text;
},
}));

const endpoint = documents.jsonUrl(document);

documents.text(document, mockFetch).then((t) => {
expect(t).toMatchObject(text);
});

expect(mockFetch).toHaveBeenCalledWith(endpoint);
});
});

describe("document uploads and processing", () => {
Expand Down
15 changes: 15 additions & 0 deletions src/lib/api/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import type {
Document,
DocumentText,
DocumentUpload,
DocumentResults,
Pending,
Expand Down Expand Up @@ -86,6 +87,20 @@ export async function get(
return resp.json();
}

export async function text(
document: Document,
fetch = globalThis.fetch,
): Promise<DocumentText> {
const url = jsonUrl(document);
const resp = await fetch(url).catch(console.error);

if (!resp || isErrorCode(resp.status)) {
return { updated: 0, pages: [] };
}

return resp.json();
}

/**
* Create new documents in a batch (or a batch of one).
*
Expand Down
30 changes: 30 additions & 0 deletions src/routes/documents/[id]-[slug]/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script lang="ts">
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 = [
["document", "Document"],
["text", "Text"],
["thumbnails", "Thumbnails"],
["notes", "Notes"],
];
</script>

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

<PageToolbar slot="footer">
<select name="mode" slot="left">
{#each modes as [value, name]}
<option {value}>{name}</option>
{/each}
</select>
</PageToolbar>
</ContentLayout>
11 changes: 11 additions & 0 deletions src/routes/documents/[id]-[slug]/+page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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();

return {
mode,
text: documents.text(document, fetch),
};
}
23 changes: 0 additions & 23 deletions src/routes/documents/[id]-[slug]/text/+page.svelte

This file was deleted.

26 changes: 0 additions & 26 deletions src/routes/documents/[id]-[slug]/text/+page.ts

This file was deleted.

0 comments on commit 4970200

Please sign in to comment.