Make PDF Viewer a native feature

- Move the PDF Viewer plugin into src/lib/features/pdf/ with pdfjs-dist 6.4.299 (minified builds).
- A static pdf document type marks PDF files and opens them as tabs. The first PDF tab loads the module.
- The worker, the character maps, the fonts, and the decoders come from the app. One worker for each tab.
- The toolbar uses IconButton and the shared tooltip, with the new zoom-in, zoom-out, fit-width, and rotate icons.
- Add THIRD_PARTY_NOTICES.md for PDF.js and xterm.
- Update the tests, the menu inventory, the docs, and UI-059.
This commit is contained in:
Knotty Wyvern
2026-10-29 19:41:17 -07:00
parent 833f793244
commit e6deecc092
35 changed files with 2101 additions and 49 deletions
+9 -9
View File
@@ -33,9 +33,9 @@ test("switches keep known booleans only, and missing keys take the default", ()
assert.deepEqual(storedSwitches({ git: false, terminal: "no", csv: true, other: "yes" }), { git: false });
assert.deepEqual(storedSwitches(null), {});
assert.deepEqual(storedSwitches(["git"]), {});
assert.deepEqual(switchesFrom(undefined), { git: true, background: true, terminal: true });
assert.deepEqual(switchesFrom({ git: false, background: false, terminal: false }), { git: false, background: false, terminal: false });
assert.deepEqual(switchesFrom({ git: "false" }), { git: true, background: true, terminal: true });
assert.deepEqual(switchesFrom(undefined), { git: true, background: true, terminal: true, pdf: true });
assert.deepEqual(switchesFrom({ git: false, background: false, terminal: false, pdf: false }), { git: false, background: false, terminal: false, pdf: false });
assert.deepEqual(switchesFrom({ git: "false" }), { git: true, background: true, terminal: true, pdf: true });
});
test("an installed package gives its state, and without a package the default applies", () => {
@@ -71,10 +71,10 @@ test("the migration asks only for features without a switch, and keeps other key
write: async (value: unknown) => { writes.push(value); },
legacy: async (ids: string[]) => { asked.push(ids); return packages; },
});
assert.deepEqual(await migrateFeatures(host({ future: 1, background: false, terminal: true }, [{ id: "runestone.git", installed: true, enabled: false, settings: null }]), presentBackground(), presentTerminal()), ["git"]);
assert.deepEqual(await migrateFeatures(host({ future: 1, background: false, terminal: true, pdf: true }, [{ id: "runestone.git", installed: true, enabled: false, settings: null }]), presentBackground(), presentTerminal()), ["git"]);
assert.deepEqual(asked, [["runestone.git"]]);
assert.deepEqual(writes, [{ future: 1, background: false, terminal: true, git: false }]);
assert.deepEqual(await migrateFeatures(host({ git: true, background: true, terminal: true }, []), presentBackground(), presentTerminal()), []);
assert.deepEqual(writes, [{ future: 1, background: false, terminal: true, pdf: true, git: false }]);
assert.deepEqual(await migrateFeatures(host({ git: true, background: true, terminal: true, pdf: true }, []), presentBackground(), presentTerminal()), []);
assert.equal(asked.length, 1, "A stored switch needs no package read.");
assert.equal(writes.length, 1);
});
@@ -84,7 +84,7 @@ test("a missing background section asks for the slideshow package and makes the
const switches: unknown[] = [];
const sections: BackgroundSettings[] = [];
const background: BackgroundMigrationHost = { missing: () => true, hasImage: () => true, migrateFolder: async () => true, write: async value => { sections.push(value); } };
const host = { read: () => ({ git: true, background: false, terminal: true }), write: async (value: unknown) => { switches.push(value); }, legacy: async (ids: string[]) => { asked.push(ids); return []; } };
const host = { read: () => ({ git: true, background: false, terminal: true, pdf: true }), write: async (value: unknown) => { switches.push(value); }, legacy: async (ids: string[]) => { asked.push(ids); return []; } };
assert.deepEqual(await migrateFeatures(host, background, presentTerminal()), []);
assert.deepEqual(asked, [["runestone.bg-slideshow"]]);
assert.deepEqual(switches, [], "Stored switches stay.");
@@ -93,8 +93,8 @@ test("a missing background section asks for the slideshow package and makes the
// Both migrations together ask once for both packages.
asked.length = 0;
await migrateFeatures({ ...host, read: () => undefined }, background, presentTerminal());
assert.deepEqual(asked, [["runestone.git", "runestone.bg-slideshow", "runestone.terminal"]]);
assert.deepEqual(switches, [{ git: true, background: true, terminal: true }]);
assert.deepEqual(asked, [["runestone.git", "runestone.bg-slideshow", "runestone.terminal", "runestone.pdf-viewer"]]);
assert.deepEqual(switches, [{ git: true, background: true, terminal: true, pdf: true }]);
});
test("a failed package read writes nothing, so the next start tries again", async () => {
+104
View File
@@ -0,0 +1,104 @@
// The PDF Viewer feature: the page and zoom limits, the document identification of the catalog, and the document
// mount of a feature session. The model tests come from the PDF Viewer plugin (tests/model.test.ts).
import { test } from "node:test";
import assert from "node:assert/strict";
import { featureEntry, featureOfOwner } from "../../../src/lib/features/catalog.ts";
import { identifyPdf } from "../../../src/lib/features/pdfDocument.ts";
import { nextRotation, outputScale, pageNumber, zoomLevel } from "../../../src/lib/features/pdf/model.ts";
import { FeatureSession } from "../../../src/lib/features/session.ts";
import type { FeatureContext, FeatureInstance } from "../../../src/lib/features/types.ts";
import type { PluginDocumentContext } from "../../../src/lib/plugins/sdk.ts";
test("PDF identification handles uppercase extensions and distinct paths", () => {
assert.deepEqual(identifyPdf("notes\\Report.PDF"), { id: "notes/Report.PDF" });
assert.notDeepEqual(identifyPdf("a/report.pdf"), identifyPdf("b/report.pdf"));
for (const path of ["report.pdf.md", "pdf", "report.png"]) assert.equal(identifyPdf(path), null);
});
test("page and zoom inputs stay within supported bounds", () => {
assert.equal(pageNumber(NaN, 20), 1);
assert.equal(pageNumber(-3, 20), 1);
assert.equal(pageNumber(99, 20), 20);
assert.equal(pageNumber(2.8, 20), 2);
assert.equal(zoomLevel(Infinity), 1);
assert.equal(zoomLevel(0), .25);
assert.equal(zoomLevel(10), 4);
assert.deepEqual([0, 90, 180, 270].map(nextRotation), [90, 180, 270, 0]);
});
test("large PDF pages respect canvas dimension and memory limits", () => {
for (const [width, height] of [[612, 792], [20000, 500], [20000, 20000]]) {
const scale = outputScale(width, height, 3);
assert.ok(width * scale <= 8192);
assert.ok(height * scale <= 8192);
assert.ok(width * height * scale * scale <= 16_000_001);
}
});
test("the catalog gives PDF Viewer the old package ID and one binary document type", () => {
const pdf = featureEntry("pdf");
assert.equal(pdf.owner, "runestone.pdf-viewer");
assert.equal(featureOfOwner("runestone.pdf-viewer")?.id, "pdf");
assert.equal(pdf.defaultOn, true);
assert.equal(pdf.followsPackage, true);
assert.deepEqual(pdf.views, []);
assert.deepEqual(pdf.commands, []);
assert.deepEqual(pdf.documentTypes.map(type => [type.id, type.icon, type.content, [...type.extensions], type.title()]), [["pdf", "file", "binary", ["pdf"], "PDF document"]]);
assert.deepEqual(featureEntry("git").documentTypes, []);
});
function fakeContext(): FeatureContext {
return { openView: () => {}, openFile: async () => {}, onWorkspaceChange: () => () => {} };
}
test("a feature session identifies PDF files without loading the feature", () => {
let loads = 0;
const session = new FeatureSession(featureEntry("pdf"), fakeContext(), async () => { loads++; throw new Error("not loaded"); });
const type = session.documentTypes.get("pdf");
assert.ok(type);
assert.equal(type.title, "PDF document");
assert.equal(type.content, "binary");
assert.deepEqual(type.extensions, ["pdf"]);
assert.deepEqual(type.identify("docs/Guide.PDF", ""), { id: "docs/Guide.PDF" });
assert.equal(type.identify("docs/guide.md", ""), null);
assert.equal(loads, 0, "Identification does not load the feature.");
assert.equal(session.started, false);
});
test("the first document mount starts one instance, each tab gets the document, and stop disposes it", async () => {
const events: string[] = [];
let starts = 0;
const instance: FeatureInstance = {
mount: () => { throw new Error("no views"); },
mountDocument: (type, element, document) => {
events.push(`mount ${type} ${document.path}`);
element.textContent = document.id;
return () => { events.push(`unmount ${document.path}`); };
},
dispose: () => { events.push("dispose"); },
};
const session = new FeatureSession(featureEntry("pdf"), fakeContext(), async () => ({ start: () => { starts++; return instance; } }));
const element = () => ({ textContent: "", replaceChildren(this: { textContent: string }) { this.textContent = ""; } }) as unknown as HTMLElement;
const first = element(), second = element();
const a: PluginDocumentContext = { id: "a.pdf", path: "a.pdf" };
const b: PluginDocumentContext = { id: "b/B.PDF", path: "b/B.PDF" };
const closeA = await session.mount("pdf", first, a);
const closeB = await session.mount("pdf", second, b);
assert.equal(starts, 1, "Two tabs share one instance.");
assert.equal(first.textContent, "a.pdf");
await closeA();
assert.equal(first.textContent, "", "The host empties the element after the dispose.");
await assert.rejects(session.mount("missing", element(), a), /This view is unavailable/);
await assert.rejects(session.mount("pdf", element()), /This view is unavailable/, "Without a document, the ID names a view.");
await closeB();
await session.stop();
assert.deepEqual(events, ["mount pdf a.pdf", "mount pdf b/B.PDF", "unmount a.pdf", "unmount b/B.PDF", "dispose"]);
await assert.rejects(session.mount("pdf", element(), a), /This feature is off/);
});
test("a feature without a document mount refuses a document", async () => {
const instance: FeatureInstance = { mount: () => () => {}, dispose() {} };
const session = new FeatureSession(featureEntry("pdf"), fakeContext(), async () => ({ start: () => instance }));
await assert.rejects(session.mount("pdf", { replaceChildren() {} } as unknown as HTMLElement, { id: "a.pdf", path: "a.pdf" }), /This view is unavailable/);
await session.stop();
});
+2 -2
View File
@@ -261,9 +261,9 @@ test("the feature migration copies the terminal settings also when the package r
test("the terminal switch follows the enabled state of an installed Terminal plugin", async () => {
const writes: unknown[] = [];
const host = { read: () => ({ git: true, background: true }), write: async (value: unknown) => { writes.push(value); },
const host = { read: () => ({ git: true, background: true, pdf: true }), write: async (value: unknown) => { writes.push(value); },
legacy: async (ids: string[]) => ids.map(id => ({ id, installed: true, enabled: false, settings: null })) };
const background = { missing: () => false, hasImage: () => false, migrateFolder: async () => false, write: async () => {} };
assert.deepEqual(await migrateFeatures(host, background, terminalHost(false, null)), ["terminal"]);
assert.deepEqual(writes, [{ git: true, background: true, terminal: false }]);
assert.deepEqual(writes, [{ git: true, background: true, pdf: true, terminal: false }]);
});