Merge [K4][6][8] Restliche Aktionen erzeugen Ops direkt (Editier-Befehle, Einfuegen/Gruppen, Panels, Ressourcen, 3D-Griffe); Patch auf gleichen Wert erzeugt keinen Undo-Schritt
Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
59 files changed
+1311
-960
No files matched your search
+3
-3
@@ -25,10 +25,10 @@ export type { Project, DrawingLevel } from "../model/types";
|
||||
export { computeKennzahlen, checkRoomProgram, solidVolume } from "../analysis/kennzahlen";
|
||||
export type { Kennzahlen, ProgramCheck } from "../analysis/kennzahlen";
|
||||
export { compareRoomProgram, layoutRoomProgram, DEFAULT_PROGRAM_TOLERANCE } from "../analysis/roomProgram";
|
||||
export { buildProgramSheet, addProgramSheet, groupColor, defaultSheetLabels } from "../analysis/roomProgramSheet";
|
||||
export { buildProgramSheet, addProgramSheet, addProgramSheetOps, groupColor, defaultSheetLabels } from "../analysis/roomProgramSheet";
|
||||
export type { ProgramSheet, SheetLabels } from "../analysis/roomProgramSheet";
|
||||
export type { RoomProgramCompare, RoomProgramLayout, CompareRow, CompareSum, Light, LayoutItem, LayoutGroup } from "../analysis/roomProgram";
|
||||
export { readSheetFile, suggestMapping, mapRows, importedRows, applyRoomProgram, cellNumber, cellText, detectHeaderRow, parseKind, PROGRAM_FIELDS } from "../io/roomProgramImport";
|
||||
export { readSheetFile, suggestMapping, mapRows, importedRows, applyRoomProgram, roomProgramOps, cellNumber, cellText, detectHeaderRow, parseKind, PROGRAM_FIELDS } from "../io/roomProgramImport";
|
||||
export type { SheetData, SheetCell, Mapping, ColumnMap, ProgramField, ImportLine, ImportMode, SkipReason } from "../io/roomProgramImport";
|
||||
export { buildProgramReport } from "../export/programReport";
|
||||
export { computeAreaTable, computeIvhb } from "../analysis/areaTable";
|
||||
@@ -50,5 +50,5 @@ export { readPdfVectorPage, pdfPageToImport, pdfPageInfo, pdfMetersPerPt, isPdfF
|
||||
export type { PdfVectorPage, PdfVectorPath, PdfVectorText } from "../io/pdfVector";
|
||||
export { readMesh, meshObjectsToImportedMeshes, formatMeshReport, isMeshFile, MESH_UNITS, MESH_UNIT_SCALE } from "../io/meshImport";
|
||||
export type { MeshObject, MeshImportReport, MeshUnit, MeshUp, MeshPlacement } from "../io/meshImport";
|
||||
export { addItems, fieldOps, findItem, isOpsMutation, itemsOf, opsMutation, patchItem, patchItems, removeItems, replaceItem, setField, setFieldIfChanged } from "./opsBuild";
|
||||
export { addItems, fieldOps, findItem, isOpsMutation, itemsOf, opsMutation, patchItem, patchItems, removeItems, replaceItem, replaceWithMany, setField, setFieldIfChanged } from "./opsBuild";
|
||||
export type { EntityCollection, EntityOf, ProjectMutation } from "./opsBuild";
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
patchItem,
|
||||
patchItems,
|
||||
removeItems,
|
||||
replaceWithMany,
|
||||
setField,
|
||||
setFieldIfChanged,
|
||||
} from "./opsBuild";
|
||||
@@ -83,6 +84,27 @@ describe("Op-Bauhelfer", () => {
|
||||
expect(findItem(sampleProject, "walls", first)).toBe(sampleProject.walls[0]);
|
||||
});
|
||||
|
||||
it("replaceWithMany = flatMap über die Liste (Teile an der Stelle des Originals), Zufall", () => {
|
||||
for (let seed = 1; seed <= 300; seed++) {
|
||||
const p = project(seed);
|
||||
const r = rng(seed + 7);
|
||||
const map = new Map<string, Wall[]>();
|
||||
for (const w of p.walls) {
|
||||
const k = r();
|
||||
if (k < 0.25) map.set(w.id, []); // entfernen
|
||||
else if (k < 0.5) map.set(w.id, [{ ...w, id: `${w.id}a` }, { ...w, id: `${w.id}b`, height: 9 }]); // teilen
|
||||
else if (k < 0.6) map.set(w.id, [w]); // unverändert
|
||||
else if (k < 0.7) map.set(w.id, [{ ...w, height: 1 }]); // ersetzen
|
||||
}
|
||||
const expected = { ...p, walls: p.walls.flatMap((w) => map.get(w.id) ?? [w]) };
|
||||
const out = applyOps(p, replaceWithMany(p, "walls", map));
|
||||
expect(out.walls).toEqual(expected.walls);
|
||||
// Unveränderte Elemente behalten ihre Identität.
|
||||
for (const w of p.walls) if (!map.has(w.id) || map.get(w.id)![0] === w) expect(out.walls.includes(w)).toBe(true);
|
||||
}
|
||||
expect(replaceWithMany(project(1), "walls", new Map())).toEqual([]);
|
||||
});
|
||||
|
||||
it("setField / setFieldIfChanged", () => {
|
||||
expect(setField("name", "X")).toEqual({ kind: "setField", key: "name", value: "X" });
|
||||
expect(setField("geoAnchor", undefined)).toEqual({ kind: "setField", key: "geoAnchor" });
|
||||
@@ -113,3 +135,13 @@ describe("Op-Bauhelfer", () => {
|
||||
expect(m.toOps(sampleProject)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("patchItem ohne Änderung", () => {
|
||||
it("gleicher Wert ergibt keine Op (kein leerer Undo-Schritt)", () => {
|
||||
const p = sampleProject;
|
||||
const w = p.walls[0];
|
||||
expect(patchItem(p, "walls", w.id, { height: w.height })).toEqual([]);
|
||||
expect(patchItems(p, "walls", [w.id], (e) => ({ ...e }))).toEqual([]);
|
||||
expect(patchItem(p, "walls", w.id, { height: w.height + 1 })).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
+47
-2
@@ -46,6 +46,20 @@ export function replaceItem<K extends EntityCollection>(collection: K, entity: E
|
||||
* Ändert Element `id` in `collection`: `patch` als Teilobjekt (`{ ...e, ...patch }`) oder Funktion.
|
||||
* Leer, wenn das Element fehlt oder die Funktion dasselbe Objekt zurückgibt (keine Änderung).
|
||||
*/
|
||||
/** Gleich auf erster Ebene (gleiche Schlüssel, gleiche Werte per `===`): ein „Patch" auf denselben Wert
|
||||
* ergibt dann keine Op und damit keinen leeren Undo-Schritt. */
|
||||
function sameShallow(a: unknown, b: unknown): boolean {
|
||||
if (a === b) return true;
|
||||
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
|
||||
const ka = Object.keys(a);
|
||||
if (ka.length !== Object.keys(b).length) return false;
|
||||
for (const k of ka) {
|
||||
if (!Object.prototype.hasOwnProperty.call(b, k)) return false;
|
||||
if ((a as Record<string, unknown>)[k] !== (b as Record<string, unknown>)[k]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function patchItem<K extends EntityCollection>(
|
||||
p: Project,
|
||||
collection: K,
|
||||
@@ -55,7 +69,7 @@ export function patchItem<K extends EntityCollection>(
|
||||
const old = findItem(p, collection, id);
|
||||
if (old === undefined) return [];
|
||||
const next = typeof patch === "function" ? patch(old) : ({ ...old, ...patch } as EntityOf<K>);
|
||||
return next === old ? [] : [replaceItem(collection, next)];
|
||||
return sameShallow(next, old) ? [] : [replaceItem(collection, next)];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +87,7 @@ export function patchItems<K extends EntityCollection>(
|
||||
for (const e of itemsOf(p, collection)) {
|
||||
if (!hit(e)) continue;
|
||||
const next = fn(e);
|
||||
if (next !== e) ops.push(replaceItem(collection, next));
|
||||
if (!sameShallow(next, e)) ops.push(replaceItem(collection, next));
|
||||
}
|
||||
return ops;
|
||||
}
|
||||
@@ -105,6 +119,37 @@ export function removeItems<K extends EntityCollection>(
|
||||
return drop.length ? [{ kind: "remove", collection, ids: drop }] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Ersetzt Elemente durch je eine Liste neuer Elemente AN DERSELBEN STELLE (Zeichenreihenfolge bleibt;
|
||||
* leere Liste = Element entfernen). Ein Eintrag, der nur das Original selbst enthält (gleiche
|
||||
* Referenz), ändert nichts. Ein `remove` für alle ersetzten, danach je ein `add` an der Endposition
|
||||
* (aufsteigend), sodass `applyOps` die Liste so liefert, als wäre sie in einem Zug neu aufgebaut worden.
|
||||
*/
|
||||
export function replaceWithMany<K extends EntityCollection>(
|
||||
p: Project,
|
||||
collection: K,
|
||||
replacements: ReadonlyMap<string, readonly EntityOf<K>[]>,
|
||||
): CoreOp[] {
|
||||
const dropped: string[] = [];
|
||||
const inserts: { at: number; items: readonly EntityOf<K>[] }[] = [];
|
||||
let len = 0; // Länge der Liste nach dem Entfernen = Position im Ergebnis
|
||||
for (const e of itemsOf(p, collection)) {
|
||||
const id = (e as { id: string }).id;
|
||||
const items = replacements.get(id);
|
||||
if (items === undefined || (items.length === 1 && items[0] === e)) {
|
||||
len++;
|
||||
continue;
|
||||
}
|
||||
dropped.push(id);
|
||||
if (items.length > 0) inserts.push({ at: len, items });
|
||||
len += items.length;
|
||||
}
|
||||
if (dropped.length === 0) return [];
|
||||
const ops: CoreOp[] = [{ kind: "remove", collection, ids: dropped }];
|
||||
for (const ins of inserts) ops.push(...addItems(collection, ins.items, ins.at));
|
||||
return ops;
|
||||
}
|
||||
|
||||
/** Setzt ein Projekt-Feld (`undefined` ⇒ Feld entfernen). */
|
||||
export function setField<K extends keyof Project>(key: K, value: Project[K] | undefined): CoreOp {
|
||||
return value === undefined ? { kind: "setField", key } : { kind: "setField", key, value };
|
||||
|
||||
Reference in new issue
Block a user