Files
FreeCADWasmDev/tools/fcweb/opfs.mjs
wangdequan 300c8efb01 docs(web): full UI surface inventory, illustrated manual, panel screenshots
Interface design document and manual now share one authoritative surface
inventory (333 surfaces across 9 categories, gap 0), and every chapter carries
real Playwright screenshots of the panels it documents.

- tools/fcweb: ui-surface-scan/ui-surface-inventory (deterministic --check),
  build-ui-doc renders panel catalog, workbench matrix and mapping tables;
  blank toolbar crops (<1 KB) are skipped and pruned instead of embedded
- web/tools/manual: generate-panel-index (ch16), audit-ui-gaps,
  generate-final-status, generate-gaps-doc; pager is per-chapter so the manual
  no longer overflows horizontally
- manual: 130 screenshot references / 128 PNGs, panel sections for ch02-ch13
- web/src: utility menus stop click propagation (menu reopened on item click);
  workbench shell gains an OPFS document picker so workbench.html can bind a
  real document instead of the fixed workbench-document id; Sketcher panel
  moves from the command column to the main stage

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-23 01:30:31 +08:00

99 lines
3.6 KiB
JavaScript

// OPFS contract shared by every fcweb consumer.
//
// The browser keeps exported geometry in real OPFS under the directory
// `freecad-exports` (web/src/storage.ts). Node scripts cannot reach
// navigator.storage, so this module mirrors the same directory layout on disk
// and addresses files by the bare export name, exactly like the UI does.
//
// Unlike the case-local opfs-store.mjs this root is configurable, so the
// toolkit does not depend on any teaching case directory. The default lives
// inside the toolkit's own out/, which keeps selftest hermetic.
import fs from "node:fs";
import path from "node:path";
import { createHash } from "node:crypto";
import { fileURLToPath } from "node:url";
const here = path.dirname(fileURLToPath(import.meta.url));
export const EXPORT_DIRECTORY = "freecad-exports";
export const DEFAULT_ROOT = path.join(here, "out", "opfs", EXPORT_DIRECTORY);
let root = DEFAULT_ROOT;
/** Point the store at another directory (absolute path). Returns the new root. */
export function configureOpfs({ root: next }) {
if (!next) throw new Error("configureOpfs requires { root }");
root = path.resolve(next);
return root;
}
export function storeRoot() {
return root;
}
/**
* A bare export name, no directory part, with an extension.
* Rejecting separators is what keeps `../x.brep` from escaping the store.
*/
export function safeName(name) {
if (typeof name !== "string") throw new Error(`invalid export name: ${String(name)}`);
if (name.includes("/") || name.includes("\\") || name.includes("\0")) throw new Error(`invalid export name: ${name}`);
const base = name.split(/[\\/]/).pop() || "";
if (base === "." || base === "..") throw new Error(`invalid export name: ${name}`);
if (!/^[A-Za-z0-9._-]+$/.test(base)) throw new Error(`invalid export name: ${name}`);
if (!/\.[A-Za-z0-9]+$/.test(base)) throw new Error(`invalid export name: ${name}`);
return base;
}
export function writeExportFile(name, bytes) {
const file = safeName(name);
fs.mkdirSync(root, { recursive: true });
const target = path.join(root, file);
fs.writeFileSync(target, Buffer.from(bytes));
return { name: file, path: target, bytes: fs.statSync(target).size };
}
export function readExportFile(name) {
const file = safeName(name);
const target = path.join(root, file);
if (!fs.existsSync(target)) throw new Error(`export missing from OPFS store: ${file}`);
return new Uint8Array(fs.readFileSync(target));
}
export function hasExportFile(name) {
return fs.existsSync(path.join(root, safeName(name)));
}
export function listExportFiles() {
if (!fs.existsSync(root)) return [];
return fs.readdirSync(root).sort();
}
export function sha256(bytes) {
return createHash("sha256").update(Buffer.from(bytes)).digest("hex");
}
/** Write bytes and report size + digest in one call. */
export function storeBytes(name, bytes) {
const stored = writeExportFile(name, bytes);
return { name: stored.name, path: stored.path, bytes: stored.bytes, sha256: sha256(bytes) };
}
/**
* Persist a JSON evidence record into the store and next to it on disk, so a
* report can be read without knowing the OPFS mirror location.
*/
export function evidence(name, value) {
const json = JSON.stringify(value, null, 2);
const stored = storeBytes(name, json);
const local = path.join(path.dirname(root), `evidence-${safeName(name)}`);
fs.mkdirSync(path.dirname(local), { recursive: true });
fs.writeFileSync(local, json);
return { ...stored, local };
}
/** Directory the OPFS mirror sits in, for evidence and trace side files. */
export function outRoot() {
return path.dirname(root);
}