@uurtech/jdf 0.1.24 → 0.1.26

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uurtech/jdf",
3
- "version": "0.1.24",
3
+ "version": "0.1.26",
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>",
package/src/jdfx.ts CHANGED
@@ -22,12 +22,22 @@ function isSafeAssetPath(p: string): boolean {
22
22
  return p.startsWith(`${JDFX_ASSET_DIR}/`);
23
23
  }
24
24
 
25
+ function uint8ToBase64(arr: Uint8Array): string {
26
+ let binary = "";
27
+ const CHUNK = 0x8000;
28
+ for (let i = 0; i < arr.length; i += CHUNK) {
29
+ binary += String.fromCharCode(...arr.subarray(i, i + CHUNK));
30
+ }
31
+ return btoa(binary);
32
+ }
33
+
25
34
  /**
26
- * Open a `.jdfx` zip bundle and return the embedded JDF document with all
27
- * `image` element `resource` references rewritten to blob URLs that work as
28
- * `<img src>`. The blob URLs are leaked intentionally — they live for the
29
- * lifetime of the page; jdf.js does not (yet) support unmounting a viewer
30
- * and reclaiming them.
35
+ * Open a `.jdfx` zip bundle and return the embedded JDF document with every
36
+ * zip asset bound into `resources.images[id].data` as base64. The image
37
+ * renderer resolves `resource → resources.images[id]`, so no element needs
38
+ * rewriting — and `viewer.exportJdf()` therefore emits a self-contained
39
+ * document. (An earlier version rewrote `el.src` to `blob:` object URLs;
40
+ * those rendered, but exports carried dead URLs and lost every image.)
31
41
  */
32
42
  export async function unpackJdfxToDocument(bytes: ArrayBuffer | Uint8Array): Promise<JdfDocument> {
33
43
  const zip = await JSZip.loadAsync(bytes as ArrayBuffer);
@@ -46,8 +56,9 @@ export async function unpackJdfxToDocument(bytes: ArrayBuffer | Uint8Array): Pro
46
56
  }
47
57
  }
48
58
 
49
- const idToBlobUrl = new Map<string, string>();
50
59
  if (manifest?.assets) {
60
+ if (!doc.resources) doc.resources = { images: {} };
61
+ if (!doc.resources.images) doc.resources.images = {};
51
62
  for (const entry of manifest.assets) {
52
63
  if (!isSafeAssetPath(entry.path)) {
53
64
  console.warn(`[jdfjs] dropping unsafe manifest asset path: ${entry.path}`);
@@ -56,23 +67,13 @@ export async function unpackJdfxToDocument(bytes: ArrayBuffer | Uint8Array): Pro
56
67
  const file = zip.file(entry.path);
57
68
  if (!file) continue;
58
69
  const data = await file.async("uint8array");
59
- const blob = new Blob([data as BlobPart], { type: entry.mimeType });
60
- idToBlobUrl.set(entry.id, URL.createObjectURL(blob));
61
- }
62
- }
63
-
64
- function rebind(els: any[] | undefined) {
65
- if (!els) return;
66
- for (const el of els) {
67
- if (el?.type === "image" && el.resource) {
68
- const url = idToBlobUrl.get(el.resource);
69
- if (url) el.src = url;
70
- }
71
- if (el?.elements) rebind(el.elements);
72
- if (el?.children) rebind(el.children);
70
+ doc.resources.images[entry.id] = {
71
+ src: "embedded",
72
+ mimeType: entry.mimeType || "image/png",
73
+ data: uint8ToBase64(data),
74
+ };
73
75
  }
74
76
  }
75
- for (const page of doc.pages || []) rebind(page.elements as any[]);
76
77
 
77
78
  return doc;
78
79
  }