@uurtech/jdf 0.1.12 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -4
- package/dist/jdfjs.cjs +3 -2
- package/dist/jdfjs.cjs.map +1 -1
- package/dist/jdfjs.js +3 -2
- package/dist/jdfjs.js.map +1 -1
- package/package.json +4 -1
- package/src/jdfx.ts +58 -0
- package/src/viewer.ts +14 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uurtech/jdf",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"description": "Render JDF (JSON Document Format) documents in the browser. Drop-in replacement for embedding PDFs — point at a .jdf file and it appears on the page, fully styled, searchable, with a clickable TOC.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Ugur Kazdal <ugur@uurtech.com>",
|
|
@@ -57,5 +57,8 @@
|
|
|
57
57
|
},
|
|
58
58
|
"publishConfig": {
|
|
59
59
|
"access": "public"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"jszip": "^3.10.1"
|
|
60
63
|
}
|
|
61
64
|
}
|
package/src/jdfx.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import JSZip from "jszip";
|
|
2
|
+
import {
|
|
3
|
+
JDFX_DOCUMENT_PATH,
|
|
4
|
+
JDFX_MANIFEST_PATH,
|
|
5
|
+
type JdfDocument,
|
|
6
|
+
type JdfxManifest,
|
|
7
|
+
} from "@jdf/core";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Open a `.jdfx` zip bundle and return the embedded JDF document with all
|
|
11
|
+
* `image` element `resource` references rewritten to blob URLs that work as
|
|
12
|
+
* `<img src>`. The blob URLs are leaked intentionally — they live for the
|
|
13
|
+
* lifetime of the page; jdf.js does not (yet) support unmounting a viewer
|
|
14
|
+
* and reclaiming them.
|
|
15
|
+
*/
|
|
16
|
+
export async function unpackJdfxToDocument(bytes: ArrayBuffer | Uint8Array): Promise<JdfDocument> {
|
|
17
|
+
const zip = await JSZip.loadAsync(bytes as ArrayBuffer);
|
|
18
|
+
|
|
19
|
+
const docFile = zip.file(JDFX_DOCUMENT_PATH);
|
|
20
|
+
if (!docFile) throw new Error(`${JDFX_DOCUMENT_PATH} missing from .jdfx bundle`);
|
|
21
|
+
const doc = JSON.parse(await docFile.async("string")) as JdfDocument;
|
|
22
|
+
|
|
23
|
+
const manifestFile = zip.file(JDFX_MANIFEST_PATH);
|
|
24
|
+
let manifest: JdfxManifest | null = null;
|
|
25
|
+
if (manifestFile) {
|
|
26
|
+
try {
|
|
27
|
+
manifest = JSON.parse(await manifestFile.async("string")) as JdfxManifest;
|
|
28
|
+
} catch {
|
|
29
|
+
manifest = null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const idToBlobUrl = new Map<string, string>();
|
|
34
|
+
if (manifest?.assets) {
|
|
35
|
+
for (const entry of manifest.assets) {
|
|
36
|
+
const file = zip.file(entry.path);
|
|
37
|
+
if (!file) continue;
|
|
38
|
+
const data = await file.async("uint8array");
|
|
39
|
+
const blob = new Blob([data as BlobPart], { type: entry.mimeType });
|
|
40
|
+
idToBlobUrl.set(entry.id, URL.createObjectURL(blob));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function rebind(els: any[] | undefined) {
|
|
45
|
+
if (!els) return;
|
|
46
|
+
for (const el of els) {
|
|
47
|
+
if (el?.type === "image" && el.resource) {
|
|
48
|
+
const url = idToBlobUrl.get(el.resource);
|
|
49
|
+
if (url) el.src = url;
|
|
50
|
+
}
|
|
51
|
+
if (el?.elements) rebind(el.elements);
|
|
52
|
+
if (el?.children) rebind(el.children);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const page of doc.pages || []) rebind(page.elements as any[]);
|
|
56
|
+
|
|
57
|
+
return doc;
|
|
58
|
+
}
|
package/src/viewer.ts
CHANGED
|
@@ -62,9 +62,21 @@ export async function embed(
|
|
|
62
62
|
const el = resolveContainer(container);
|
|
63
63
|
el.classList.add("jdfjs-loading");
|
|
64
64
|
try {
|
|
65
|
-
const
|
|
65
|
+
const isJdfx = /\.jdfx(\?|#|$)/i.test(url);
|
|
66
|
+
const res = await fetch(url, {
|
|
67
|
+
headers: isJdfx
|
|
68
|
+
? { Accept: "application/jdf+zip,application/zip" }
|
|
69
|
+
: { Accept: "application/json,application/jdf+json" },
|
|
70
|
+
});
|
|
66
71
|
if (!res.ok) throw new Error(`Failed to fetch ${url} (${res.status})`);
|
|
67
|
-
|
|
72
|
+
|
|
73
|
+
let doc: JdfDocument;
|
|
74
|
+
if (isJdfx) {
|
|
75
|
+
const { unpackJdfxToDocument } = await import("./jdfx");
|
|
76
|
+
doc = await unpackJdfxToDocument(await res.arrayBuffer());
|
|
77
|
+
} else {
|
|
78
|
+
doc = (await res.json()) as JdfDocument;
|
|
79
|
+
}
|
|
68
80
|
if (!doc?.$jdf) throw new Error("Not a valid JDF document (missing $jdf field)");
|
|
69
81
|
el.classList.remove("jdfjs-loading");
|
|
70
82
|
return render(el, doc, options);
|