Files
OneCAD-Draft/web/ocs-parse-worker.js
T
Afonsop 61c32c7f74 web: XREFs, data links & extraction, ARCHIVE transmittals, worker-side saves
The last four big web-vs-desktop gaps.

XREFs: XATTACH reads the picked drawing's bytes and registers them
name-keyed (the image/PDF-underlay pattern), pre-parses on the web
worker, and the commit merge produces the same xref block flow as
native. Opened drawings resolve xrefs from the registry (trailing-name
match) before cache build; unresolved references produce ONE warning
listing the names and the fix — drop the referenced files on the page
(drops and picker opens now register their bytes) and reopen or
XRELOAD. XRELOAD works off the registry on unsaved drawings too.

Data Link & Extraction: spreadsheet links read picked xlsx/csv bytes
(calamine from slices / CSV from string) through a session registry,
so preview, refresh, and table-insert work on web; extraction takes
external drawings via a multi-file picker parsed on the worker, and
outputs CSV/TXT/.dxex via download_bytes. The disabled 'Add folder'
button explains why.

ARCHIVE/ETRANSMIT: assembles the drawing plus registered images, PDF
underlays, and session plot tables into a ZIP delivered as
{stem}_transmit.zip; skipped dependencies are reported by name.

Save/autosave off the UI thread: the parse worker gains a serialize
action (protocol v5) — the document is prepared once on the main
thread (clone, styles, dimension bake), then the DWG/DXF writer pass,
the dominant cost, runs on the worker; any worker failure falls back
to inline serialization with the identical shared implementation.
The save path also clears the dirty flag only when the tab didn't
change mid-write, and the runtime-fields sidecar now carries
graphic_data, fixing a pre-existing loss of raw DWG round-trip bytes
on web-opened documents.

(cherry picked from commit 2e100634)

Ported onto main (#1717 review): the new ocs_save crate builds on the
pinned codec; the hand-written ZIP writer is replaced by the zip crate
already in the tree; ARCHIVE packs registered xref bytes too and
serializes document_for_save() like a web Save; the web save prepares
document_for_save() (prepare_document_for_save now takes the copy by
value, so no second clone); the wasm resolve_path consults the xref
registry, so every reference lookup — not just resolve — finds dropped
files; XRELOAD keeps main's undo, unload and stale-baseline handling;
native data extraction streams the source drawings one at a time.
2026-10-12 12:02:04 +03:00

49 lines
1.7 KiB
JavaScript

import init, { parse_document, serialize_document, sha256_document } from "./worker_pkg/ocs_web_worker.js?v=5";
const ready = init(
new URL("./worker_pkg/ocs_web_worker_bg.wasm?v=5", import.meta.url),
);
self.onmessage = async ({ data }) => {
let stage = "initialize worker";
try {
await ready;
if (data.action === "hash") {
const digest = sha256_document(new Uint8Array(data.bytes));
self.postMessage({ ok: true, digest });
return;
}
if (data.action === "serialize") {
// SAVE/AUTOSAVE: decode the prepared document, run the DWG/DXF writer
// here, and hand the encoded result buffer back. Mirrors the parse
// branch's copy-before-transfer (wasm-bindgen returns a view into
// WebAssembly.Memory, which must not detach).
const encoded = serialize_document(new Uint8Array(data.document), (next) => {
stage = next;
});
const transferable = encoded.slice();
self.postMessage({ ok: true, data: transferable.buffer }, [transferable.buffer]);
return;
}
const encoded = parse_document(
data.name,
new Uint8Array(data.bytes),
data.recoveryMode === true,
data.initialError || "",
(next) => {
stage = next;
},
);
// wasm-bindgen returns a view into WebAssembly.Memory. Copy to a standalone
// ArrayBuffer before transferring it, otherwise the worker's wasm memory
// itself would be detached.
const transferable = encoded.slice();
self.postMessage({ ok: true, data: transferable.buffer }, [transferable.buffer]);
} catch (error) {
self.postMessage({
ok: false,
error: `${stage}: ${error instanceof Error ? error.message : String(error)}`,
});
}
};