[K4][6][8] S3/S4: Öffnen/Neu laden im Kern, Importe als Ops, diffToOps gelöscht
Öffnen/Vorlage/Wiederherstellen: Store openProjectFile (Kern liest, migriert, lädt direkt ins Dokument, Verlauf leer); Neu/Musterhaus: loadProject (doc_load_json). Autosave sichert den Datei-Text des Kerns, Wiederherstellen lädt ihn wie eine Datei. CLI/MCP laden ins gemeinsame Abfrage-Dokument. DXF/PDF-Import als Ops in einer Gruppe (ein Undo-Schritt), Bilder und IFC-Bauteile als Ops, CLI-Importe über fieldOps/addItems. setProject/CommandResult.commit/ToolResult.commit nehmen nur noch ProjectMutation; diffToOps, isOpsMutation, addNewElementsToActiveGroup, project_parse gelöscht. documentFor folgt fremden Projekten feldweise (fieldOps). Tests: Laden/Speichern zeichengleich zu JSON.stringify, Store-Laden, e2e Öffnen leert Verlauf und DXF-Import = ein Schritt; Zufallsänderungen in Tests über projectDiff.testutil.
This commit is contained in:
37 files changed
+582
-485
No files matched your search
@@ -6,7 +6,8 @@ import "../engine/fileioWasm.setup";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { applyOps, diffToOps, validateProject } from "../core/ops";
|
||||
import { applyOps, validateProject } from "../core/ops";
|
||||
import { projectDiffOps } from "../core/projectDiff.testutil";
|
||||
import { exportIfcSpf } from "../export/exportIfc";
|
||||
import { emptyProject, sampleProject } from "../model/sampleProject";
|
||||
import type { Column, Opening, Project, Vec2, Wall } from "../model/types";
|
||||
@@ -143,7 +144,7 @@ describe("IFC-Import als Bauteile: Rundreise Demo-Haus", () => {
|
||||
|
||||
it("über Ops angewandt und gespeichert/geladen bleibt alles gleich", async () => {
|
||||
const { project: next } = await importInto(emptyProject, sampleProject);
|
||||
const applied = applyOps(emptyProject, diffToOps(emptyProject, next));
|
||||
const applied = applyOps(emptyProject, projectDiffOps(emptyProject, next));
|
||||
expect(applied.walls).toEqual(next.walls);
|
||||
expect(applied.openings).toEqual(next.openings);
|
||||
const back = parseProjectFile(serializeProject(applied));
|
||||
|
||||
+29
-23
@@ -5,10 +5,14 @@
|
||||
// konnte, kommt wie beim Mesh-Import als Kontext-Mesh (`ifcImport.ts`).
|
||||
//
|
||||
// Keine Geometrie-Rechnung hier: TS ordnet nur zu (Geschoss nach Höhe, Typ nach Schichten) und setzt
|
||||
// die Objekte zusammen. Das Projekt wird nicht verändert — der Aufrufer übernimmt das Ergebnis über
|
||||
// `setProject` (App) bzw. `diffToOps`/`applyOps` (CLI), also über Ops.
|
||||
// die Objekte zusammen. Das Projekt wird nicht verändert — das Ergebnis sind Ops ([K4] S3), die der
|
||||
// Aufrufer übernimmt (App: Store `applyChange`, ein Undo-Schritt; CLI: `applyOps`).
|
||||
|
||||
import { ensureContextCategories } from "../model/contextCategories";
|
||||
import { applyOps } from "../core/ops";
|
||||
import type { CoreOp } from "../core/ops";
|
||||
import { addItems, fieldOps } from "../core/opsBuild";
|
||||
import type { EntityOf } from "../core/opsBuild";
|
||||
import type {
|
||||
Ceiling,
|
||||
CeilingType,
|
||||
@@ -287,8 +291,8 @@ function flatCategories(list: LayerCategory[], out: LayerCategory[] = []): Layer
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt die erkannten Bauteile und die übrigen Kontext-Meshes ins Projekt (neues Objekt, das alte
|
||||
* bleibt unverändert). Geschosse werden nach Höhe wiederverwendet (±5 mm) oder angelegt, Wand- und
|
||||
* Setzt die erkannten Bauteile und die übrigen Kontext-Meshes ins Projekt: `ops` (für den Store),
|
||||
* `project` = `applyOps(project, ops)` (neues Objekt, das alte bleibt unverändert). Geschosse werden nach Höhe wiederverwendet (±5 mm) oder angelegt, Wand- und
|
||||
* Deckentypen nach Schichten (Material + Dicke) wiederverwendet oder angelegt, Materialien nach Namen.
|
||||
*/
|
||||
export function ifcElementsToProject(
|
||||
@@ -296,7 +300,7 @@ export function ifcElementsToProject(
|
||||
products: IfcProduct[],
|
||||
elements: IfcElementsData,
|
||||
opts: IfcElementsOptions,
|
||||
): { project: Project; summary: IfcElementsSummary } {
|
||||
): { ops: CoreOp[]; project: Project; summary: IfcElementsSummary } {
|
||||
const stamp = Date.now().toString(36);
|
||||
const newId = (kind: string) => `ifc-${kind}-${stamp}-${counter++}`;
|
||||
const summary: IfcElementsSummary = {
|
||||
@@ -559,25 +563,27 @@ export function ifcElementsToProject(
|
||||
withClass(elements.walls) + withClass(elements.openings.filter((o) => o.kind !== "hole")) + withClass(elements.slabs) + withClass(elements.columns) + withClass(elements.spaces) +
|
||||
products.filter((p) => p.classifications?.length).length;
|
||||
|
||||
const next: Project = {
|
||||
...project,
|
||||
drawingLevels: levels,
|
||||
components,
|
||||
wallTypes,
|
||||
...(ceilingTypes.length || project.ceilingTypes ? { ceilingTypes } : {}),
|
||||
walls: [...project.walls, ...walls],
|
||||
openings: [...(project.openings ?? []), ...openings],
|
||||
ceilings: [...(project.ceilings ?? []), ...ceilings],
|
||||
columns: [...(project.columns ?? []), ...columns],
|
||||
rooms: [...(project.rooms ?? []), ...rooms],
|
||||
// Fehlende Listen werden (wie früher beim Ersetzen des Projekts) leer angelegt.
|
||||
const list = <K extends "openings" | "ceilings" | "columns" | "rooms">(key: K, items: EntityOf<K>[]): CoreOp[] =>
|
||||
project[key] ? addItems(key, items) : fieldOps(project, key, items as Project[K]);
|
||||
const ops: CoreOp[] = [
|
||||
...fieldOps(project, "drawingLevels", levels),
|
||||
...fieldOps(project, "components", components),
|
||||
...fieldOps(project, "wallTypes", wallTypes),
|
||||
...(ceilingTypes.length || project.ceilingTypes ? fieldOps(project, "ceilingTypes", ceilingTypes) : []),
|
||||
...addItems("walls", walls),
|
||||
...list("openings", openings),
|
||||
...list("ceilings", ceilings),
|
||||
...list("columns", columns),
|
||||
...list("rooms", rooms),
|
||||
...(meshes.length
|
||||
? {
|
||||
context: [...(project.context ?? []), ...meshes],
|
||||
layers: ensureContextCategories(project.layers, meshes.flatMap((m) => (m.categoryCode ? [m.categoryCode] : []))),
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
return { project: next, summary };
|
||||
? [
|
||||
...(project.context ? addItems("context", meshes) : fieldOps(project, "context", meshes)),
|
||||
...fieldOps(project, "layers", ensureContextCategories(project.layers, meshes.flatMap((m) => (m.categoryCode ? [m.categoryCode] : [])))),
|
||||
]
|
||||
: []),
|
||||
];
|
||||
return { ops, project: applyOps(project, ops), summary };
|
||||
}
|
||||
|
||||
/** Meldung zum Bauteil-Import; `tr` übersetzt (Kern kennt kein i18n). */
|
||||
|
||||
@@ -71,3 +71,49 @@ describe("Migration 1 → 2: Legacy-Türen werden Öffnungen", () => {
|
||||
expect(r.project.openings!.some((o) => o.id === `${existing}-t` && o.kind === "door")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Laden und Speichern im Kern ([K4] S4)", () => {
|
||||
// Früher: TS `JSON.parse` → Prüfung/Migration → `JSON.stringify({ formatVersion, ...rest }, null, 2)`.
|
||||
// Heute liest, migriert und schreibt der Kern; die Datei muss Zeichen für Zeichen gleich bleiben
|
||||
// (Zahlen wie JS `String(n)`, Index-Schlüssel wie ein JS-Objekt vorn).
|
||||
const reference = (p: object): string => JSON.stringify({ formatVersion: PROJECT_FORMAT_VERSION, ...p }, null, 2);
|
||||
const TRICKY = [0.000005, 1.5e-7, 1e21, 2 ** 60, 2 ** 53 + 2, -0, 0.1 + 0.2, 1e16, 2.7755575615628914e-17, -1e-6];
|
||||
|
||||
it("Speichern nach Öffnen = JSON.stringify der Leseansicht (Beispiel, knifflige Zahlen, Index-Schlüssel)", () => {
|
||||
const odd = {
|
||||
...structuredClone(sampleProject),
|
||||
walls: sampleProject.walls.map((w, i) => ({ ...w, height: TRICKY[i % TRICKY.length] })),
|
||||
zukunft: { b: 1, "21": "x\u0001\n😀", "10": [1.0, 2.5e-7], "007": null, "4294967295": 0 },
|
||||
};
|
||||
const texts = [JSON.stringify(sampleProject), JSON.stringify(odd), JSON.stringify(odd, null, 2).replace(/: 2,/g, ": 2.0,")];
|
||||
for (const text of texts) {
|
||||
const r = parseProjectFile(text);
|
||||
expect(r.ok).toBe(true);
|
||||
if (!r.ok) continue;
|
||||
const saved = serializeProject(r.project);
|
||||
expect(saved).toBe(reference(r.project));
|
||||
const { formatVersion: _v, ...raw } = JSON.parse(text) as Record<string, unknown>;
|
||||
expect(saved).toBe(reference(raw));
|
||||
}
|
||||
});
|
||||
|
||||
it("Index-Schlüssel stehen vorn, aufsteigend (wie JSON.parse in JS)", () => {
|
||||
const text = `{"walls":[],"layers":[],"drawingLevels":[],"m":{"x":1,"10":2,"2":3,"01":4}}`;
|
||||
const r = parseProjectFile(text);
|
||||
expect(r.ok && Object.keys((r.project as unknown as { m: object }).m)).toEqual(["2", "10", "x", "01"]);
|
||||
expect(r.ok && serializeProject(r.project)).toBe(r.ok && reference(r.project));
|
||||
});
|
||||
|
||||
it("migrierte Datei wird im neuen Format gespeichert", () => {
|
||||
const old = { ...structuredClone(sampleProject), formatVersion: 1, doors: [{ id: "TX", hostWallId: "W1", position: 0.5, width: 0.9, height: 2.1 }] };
|
||||
const r = parseProjectFile(JSON.stringify(old));
|
||||
expect(r.ok).toBe(true);
|
||||
if (!r.ok) return;
|
||||
const saved = serializeProject(r.project);
|
||||
expect(saved).toBe(reference(r.project));
|
||||
expect(JSON.parse(saved).doors).toBeUndefined();
|
||||
const again = parseProjectFile(saved);
|
||||
expect(again.ok && again.migratedFrom).toBeNull();
|
||||
expect(again.ok && serializeProject(again.project)).toBe(saved);
|
||||
});
|
||||
});
|
||||
+12
-13
@@ -1,5 +1,7 @@
|
||||
// Projektdatei-Format (`.obp`/`.obpt`/Alt-`.json`): Versionierung, Migration und EIN Parser für alle
|
||||
// Ladestellen. Gerechnet wird im Rust-Kern (`src-tauri/core/src/project_format.rs`, [K4]).
|
||||
// Ladestellen. Gerechnet wird im Rust-Kern (`src-tauri/core/src/project_format.rs`, [K4] S4): der Kern
|
||||
// liest den Text, migriert und lädt das Ergebnis direkt in ein Kern-Dokument; Speichern schreibt der
|
||||
// Kern aus dem Dokument (Zeichen für Zeichen wie `JSON.stringify(…, null, 2)`).
|
||||
//
|
||||
// Regeln:
|
||||
// • Jede gespeicherte Datei trägt `formatVersion` (s. {@link PROJECT_FORMAT_VERSION}).
|
||||
@@ -9,26 +11,23 @@
|
||||
// • Tolerant lesen: fehlende Listen werden leer ergänzt, unbekannte Felder bleiben erhalten und
|
||||
// werden gemeldet (nicht gelöscht).
|
||||
// • Was kein Dossier-Projekt ist, ist ein Fehler mit Meldung — nie ein stilles `console.warn`.
|
||||
//
|
||||
// Im Store öffnet `openProjectFile` (projectSlice) die Datei im Store-Dokument (mit Verlauf); diese
|
||||
// Fassade lädt ins gemeinsame Abfrage-Dokument (CLI, MCP, Tests) — die Abfragen auf dem gelesenen
|
||||
// Projekt laufen danach ohne erneute Übergabe.
|
||||
|
||||
import type { Project } from "../model/types";
|
||||
import { core } from "../engine/coreWasm";
|
||||
import { documentFor } from "../core/document";
|
||||
import { documentFor, sharedDocument } from "../core/document";
|
||||
import type { LoadFileResult } from "../core/document";
|
||||
|
||||
/** Aktuelle Version des Dateiformats (muss `PROJECT_FORMAT_VERSION` im Kern entsprechen — Test). */
|
||||
export const PROJECT_FORMAT_VERSION = 2;
|
||||
|
||||
export type ParseProjectResult =
|
||||
| { ok: true; project: Project; warnings: string[]; migratedFrom: number | null }
|
||||
| { ok: false; error: string };
|
||||
export type ParseProjectResult = LoadFileResult;
|
||||
|
||||
/** Liest eine Projektdatei (Text) tolerant ein, migriert und prüft sie. */
|
||||
/** Liest eine Projektdatei (Text) tolerant ein, migriert und prüft sie (Kern, gemeinsames Dokument). */
|
||||
export function parseProjectFile(text: string): ParseProjectResult {
|
||||
return JSON.parse(core().project_parse(text, Date.now())) as ParseProjectResult;
|
||||
}
|
||||
|
||||
/** Wie {@link parseProjectFile}, für ein bereits geparstes Objekt. */
|
||||
export function parseProjectObject(raw: unknown): ParseProjectResult {
|
||||
return parseProjectFile(JSON.stringify(raw ?? null));
|
||||
return sharedDocument().loadFile(text);
|
||||
}
|
||||
|
||||
/** Serialisiert ein Projekt für die Datei (mit aktueller Formatversion vorne). */
|
||||
|
||||
Reference in new issue
Block a user