[K4][6][8] Aktionen erzeugen Ops direkt statt Projekt → Projekt + diffToOps
- Store `applyChange(ops | (p) => ops, coalesceKey?)` geht ohne Differenz an den Kern; `setProject` erkennt `opsMutation`-Funktionen und nimmt deren Ops. - Umgestellt: Griffe (moveGripOf/moveElementByOf/moveEdgeOf, Decke/Dach/Treppe/Raum), Objekt-Info (updateWall/-Ceiling/-Opening/-Roof/-Stair/-Room/-Drawing2D, Farbe, Füllung, Vorder-/Hintergrund, Resize, Wand-/Deckendicke, Polylinien-Punkte), Auswahl verschieben/ kopieren/drehen (`transformOps`), Löschen der Auswahl (+ Schnittlinien, eine Gruppe), Ebenen/Sichtbarkeit/Kategorien, Text-Tippen, Panel-Zeichnungs-Eigenschaften und die Zeichenbefehle (Wand, Decke, Raum, Treppe, Öffnung, Dach, Stütze, Extrusion, alle 2D-Zeichenbefehle, move/copy/mirror/rotate/scale/array). Gruppen-Zuordnung beim Zeichnen in einer Gruppe über `activeGroupOps`. - Griff-Zug mit mitgeführten Ecken: je Mausschritt eine Gruppe mit Schlüssel → ein Undo-Schritt für den ganzen Zug; aufgeschobene Commits beim Loslassen als Gruppe. - Tests: Store-Zufallsfolgen (Leseansicht = Kern, Aktion = applyOps(vorher, ihre Ops), Undo bis Anfang/Redo bis Ende exakt), activeGroupOps = addNewElementsToActiveGroup; beim Umbau zusätzlich 2400 Zufallsschritte gegen die alte setProject-Fassung und 400 Transformationen gegen das alte commitTransform geprüft (gleiches Projekt bis auf erzeugte ids und fehlend/leere Listen). - Messung (vitest, 3000 Wände): Griff ziehen 3,3 → 0,2–0,3 ms/Schritt, Auswahl (300 Wände) verschieben 30 → 6–7 ms, Eigenschaft tippen 3 → 0,03 ms. Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
49 files changed
+1001
-750
No files matched your search
@@ -0,0 +1,57 @@
|
||||
// [K4] S3 Messung: typische Hochfrequenz-Aktionen auf einem grossen Projekt (3000 Wände) über den
|
||||
// Store — Griff ziehen (100 Schritte, ein Undo-Schritt), Auswahl verschieben (300 Wände, 20-mal),
|
||||
// Eigenschaft tippen (100 Zeichen, ein Undo-Schritt). Werte gehen nach stderr (TODO [K4]); die
|
||||
// Grenzen sind grosszügig (Container unter Last), geprüft wird vor allem das Ergebnis.
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getState } from "./appStore";
|
||||
import { sampleProject } from "../model/sampleProject";
|
||||
import type { Project, Wall } from "../model/types";
|
||||
|
||||
const st = () => getState();
|
||||
|
||||
function bigProject(n: number): Project {
|
||||
const base = sampleProject.walls;
|
||||
const walls: Wall[] = Array.from({ length: n }, (_, i) => ({
|
||||
...base[i % base.length],
|
||||
id: `w${i}`,
|
||||
start: { x: i, y: 0 },
|
||||
end: { x: i + 1, y: 0 },
|
||||
}));
|
||||
return { ...structuredClone(sampleProject), walls };
|
||||
}
|
||||
|
||||
describe("Messung Store-Aktionen, 3000 Wände", () => {
|
||||
it("Griff ziehen, Auswahl verschieben, Eigenschaft tippen", () => {
|
||||
st().setProject(bigProject(3000));
|
||||
st().clearHistory();
|
||||
const depth0 = st().undoDepth;
|
||||
|
||||
// Griff ziehen: 100 Mausschritte, ein Undo-Schritt.
|
||||
let t = performance.now();
|
||||
for (let i = 0; i < 100; i++) st().moveGripOf(null, "w1500", 0, { x: 1500 + i * 0.01, y: 0.5 });
|
||||
const gripMs = (performance.now() - t) / 100;
|
||||
expect(st().project.walls[1500].start).toEqual({ x: 1500 + 99 * 0.01, y: 0.5 });
|
||||
expect(st().undoDepth).toBe(depth0 + 1);
|
||||
|
||||
// Auswahl verschieben: 300 Wände, 20 Verschiebungen.
|
||||
const ids = Array.from({ length: 300 }, (_, i) => `w${i * 10}`);
|
||||
t = performance.now();
|
||||
for (let i = 0; i < 20; i++) {
|
||||
st().commitTransformOn({ wallIds: ids, drawingId: null }, "move", [{ x: 0, y: 0 }, { x: 0, y: 0.1 }], "move", 1);
|
||||
}
|
||||
const moveMs = (performance.now() - t) / 20;
|
||||
expect(st().project.walls[10].start.y).toBeCloseTo(2, 9);
|
||||
expect(st().project.walls[11].start.y).toBe(0);
|
||||
|
||||
// Eigenschaft tippen: 100 Tastenschritte mit coalesceKey.
|
||||
t = performance.now();
|
||||
for (let i = 0; i < 100; i++) st().updateWall("w42", { height: 2.5 + i / 1000 }, "typing:w42");
|
||||
const typeMs = (performance.now() - t) / 100;
|
||||
expect(st().project.walls[42].height).toBeCloseTo(2.599, 9);
|
||||
|
||||
process.stderr.write(
|
||||
`3000 Wände (Store): Griff ziehen ${gripMs.toFixed(2)} ms/Schritt, Auswahl verschieben (300) ${moveMs.toFixed(2)} ms, Eigenschaft tippen ${typeMs.toFixed(2)} ms\n`,
|
||||
);
|
||||
expect(gripMs + moveMs + typeMs).toBeLessThan(200);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,177 @@
|
||||
// [K4] S3 zweite Etappe: Store-Aktionen erzeugen Ops direkt (`applyChange`, `opsMutation`) statt
|
||||
// Projekt → Projekt + `diffToOps`. Geprüft: Ops gehen unverändert an den Kern (Verlauf, Prüfung),
|
||||
// Leseansicht und Kern-Dokument bleiben gleich — auch über Zufallsfolgen der umgestellten
|
||||
// Aktionen mit Undo bis zum Anfang und Redo bis zum Ende.
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { getState, setState } from "./appStore";
|
||||
import { storeDocument } from "./historySlice";
|
||||
import { sampleProject } from "../model/sampleProject";
|
||||
import type { Drawing2D, Project, Vec2 } from "../model/types";
|
||||
import { applyOps } from "../core/ops";
|
||||
import { opsMutation, patchItem, removeItems } from "../core/opsBuild";
|
||||
|
||||
const st = () => getState();
|
||||
|
||||
function rng(seed: number) {
|
||||
let x = seed >>> 0;
|
||||
return () => ((x = (x * 1664525 + 1013904223) % 4294967296) / 4294967296);
|
||||
}
|
||||
|
||||
function drawings(r: () => number): Drawing2D[] {
|
||||
const v = (): Vec2 => ({ x: Math.round(r() * 100) / 10, y: Math.round(r() * 100) / 10 });
|
||||
const base = { type: "drawing2d" as const, levelId: "eg", categoryCode: "20" };
|
||||
const geoms = [
|
||||
{ shape: "line", a: v(), b: v() },
|
||||
{ shape: "polyline", pts: [v(), v(), v(), v()], closed: true, bulge: [0, 0.3, 0, 0] },
|
||||
{ shape: "polyline", pts: [v(), v(), v(), v()], closed: false },
|
||||
{ shape: "rect", min: { x: 0, y: 0 }, max: { x: 2, y: 1 } },
|
||||
{ shape: "circle", center: v(), r: 1.5 },
|
||||
{ shape: "arc", center: v(), r: 1, a0: 0.2, a1: 2 },
|
||||
{ shape: "text", at: v(), text: "A", height: 0.3, angle: 0 },
|
||||
{ shape: "dimension", pts: [v(), v(), v()], offset: 0.5 },
|
||||
] as Drawing2D["geom"][];
|
||||
return geoms.map((geom, i) => ({ ...base, id: `d${i}`, geom }) as Drawing2D);
|
||||
}
|
||||
|
||||
/** Kern-Dokument == TS-Leseansicht (JSON). */
|
||||
function expectMirror(): void {
|
||||
const p = st().project;
|
||||
expect(JSON.parse(storeDocument(p).json())).toEqual(JSON.parse(JSON.stringify(p)));
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
st().setProject(structuredClone(sampleProject));
|
||||
st().clearHistory();
|
||||
});
|
||||
|
||||
describe("applyChange", () => {
|
||||
it("Ops gehen ohne Differenz an den Kern: ein Undo-Schritt, Protokoll = genau diese Ops", () => {
|
||||
const before = st().project;
|
||||
const w = before.walls[0];
|
||||
const ops = patchItem(before, "walls", w.id, { height: 3.3 });
|
||||
st().applyChange(ops);
|
||||
expect(st().project.walls[0].height).toBe(3.3);
|
||||
// Unveränderte Elemente und Listen behalten ihre Identität.
|
||||
expect(st().project.walls[1]).toBe(before.walls[1]);
|
||||
expect(st().project.drawingLevels).toBe(before.drawingLevels);
|
||||
expect(st().recentOps().at(-1)).toEqual(ops);
|
||||
expect(st().undoDepth).toBe(1);
|
||||
st().undo();
|
||||
expect(st().project.walls[0].height).toBe(w.height);
|
||||
expectMirror();
|
||||
});
|
||||
|
||||
it("leere Ops: kein Schritt; NaN: abgelehnt, Projekt unverändert", () => {
|
||||
const before = st().project;
|
||||
st().applyChange([]);
|
||||
st().applyChange((p) => patchItem(p, "walls", "gibt-es-nicht", { height: 1 }));
|
||||
expect(st().project).toBe(before);
|
||||
expect(st().undoDepth).toBe(0);
|
||||
st().applyChange((p) => patchItem(p, "walls", p.walls[0].id, { height: NaN }));
|
||||
expect(st().project).toBe(before);
|
||||
expect(st().undoDepth).toBe(0);
|
||||
});
|
||||
|
||||
it("coalesceKey: Serie = ein Schritt; setProject(opsMutation) nimmt die Ops direkt", () => {
|
||||
const id = st().project.walls[0].id;
|
||||
for (let i = 0; i < 50; i++) st().applyChange((p) => patchItem(p, "walls", id, { height: 2 + i / 100 }), "drag");
|
||||
expect(st().undoDepth).toBe(1);
|
||||
const m = opsMutation((p) => removeItems(p, "walls", [id]));
|
||||
st().setProject(m);
|
||||
expect(st().recentOps().at(-1)).toEqual([{ kind: "remove", collection: "walls", ids: [id] }]);
|
||||
expect(st().undoDepth).toBe(2);
|
||||
st().undo();
|
||||
st().undo();
|
||||
expect(st().project.walls[0].height).toBe(sampleProject.walls[0].height);
|
||||
expectMirror();
|
||||
});
|
||||
|
||||
it("groupChanges: mehrere Aktionen = ein Undo-Schritt", () => {
|
||||
const p0 = st().project;
|
||||
st().groupChanges(() => {
|
||||
st().patchLevel(p0.drawingLevels[0].id, { linePoints: undefined, name: "X" });
|
||||
st().setProject(opsMutation((p) => removeItems(p, "walls", [p.walls[0].id, p.walls[1].id])));
|
||||
});
|
||||
expect(st().undoDepth).toBe(1);
|
||||
st().undo();
|
||||
expect(st().project).toEqual(p0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("groupChanges mit Schlüssel", () => {
|
||||
it("Griff-Zug mit mitgeführten Ecken: alle Mausschritte = ein Undo-Schritt", () => {
|
||||
const p0 = st().project;
|
||||
const [a, b] = p0.walls;
|
||||
for (let i = 0; i < 30; i++) {
|
||||
st().groupChanges(() => {
|
||||
st().moveElementByOf(null, a.id, { x: 0.01, y: 0 });
|
||||
st().moveGripOf(null, b.id, 0, { x: b.start.x + (i + 1) * 0.01, y: b.start.y });
|
||||
}, `bodyDrag::${a.id}`);
|
||||
}
|
||||
expect(st().undoDepth).toBe(1);
|
||||
st().undo();
|
||||
expect(st().project).toEqual(p0);
|
||||
expectMirror();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Zufallsfolgen der umgestellten Aktionen", () => {
|
||||
it("Leseansicht = Kern, jede Aktion = applyOps(vorher, ihre Ops), Undo/Redo exakt", () => {
|
||||
for (let seed = 1; seed <= 12; seed++) {
|
||||
const r = rng(seed);
|
||||
const start: Project = { ...structuredClone(sampleProject), drawings2d: drawings(r) };
|
||||
setState({ project: start });
|
||||
st().clearHistory();
|
||||
const v = (): Vec2 => ({ x: r() * 10, y: r() * 10 });
|
||||
let committed = 0;
|
||||
for (let k = 0; k < 60; k++) {
|
||||
const s = st();
|
||||
const p = s.project;
|
||||
const wid = p.walls[Math.floor(r() * p.walls.length)]?.id ?? "x";
|
||||
const did = p.drawings2d[Math.floor(r() * p.drawings2d.length)]?.id ?? "x";
|
||||
const lid = p.drawingLevels[Math.floor(r() * p.drawingLevels.length)]?.id ?? "x";
|
||||
const cid = p.ceilings?.[0]?.id ?? "x";
|
||||
const rmid = p.rooms?.[0]?.id ?? "x";
|
||||
const idx = Math.floor(r() * 4);
|
||||
const n = Math.floor(r() * 14);
|
||||
const depth = s.undoDepth;
|
||||
try {
|
||||
switch (n) {
|
||||
case 0: s.moveGripOf(null, wid, idx % 2, v()); break;
|
||||
case 1: s.moveGripOf(did, null, idx % 2, v()); break;
|
||||
case 2: s.moveElementByOf(did, null, v()); break;
|
||||
case 3: s.moveEdgeOf(null, wid, 0, 1, v()); break;
|
||||
case 4: s.updateWall(wid, { height: 2 + r() }); break;
|
||||
case 5: s.setElementColor("drawing2d", did, "#abcdef"); break;
|
||||
case 6: s.commitTransformOn({ wallIds: [wid], drawingId: did }, idx % 2 ? "move" : "rotate", [v(), v(), v()], idx < 2 ? "move" : "copy", 1); break;
|
||||
case 7: s.moveCeilingBy(cid, v()); break;
|
||||
case 8: s.moveRoomGrip(rmid, idx, v()); break;
|
||||
case 9: s.toggleLevelVisible(lid); break;
|
||||
case 10: s.patchLevel(lid, { floorHeight: 2.5 + r() }); break;
|
||||
case 11: s.setWallThickness(wid, 0.1 + r() * 0.3); break;
|
||||
case 12: s.resizeElement("drawing2d", did, 1 + r(), 1 + r(), { fx: 0, fy: 0 }); break;
|
||||
case 13: s.setProject(opsMutation((q) => removeItems(q, "drawings2d", [did]))); break;
|
||||
}
|
||||
} catch {
|
||||
continue; // ungültige Griff-Indizes (z. B. Kante einer kurzen Polylinie) — wie bisher
|
||||
}
|
||||
if (st().project !== p) {
|
||||
committed++;
|
||||
// Ein Schritt mehr — oder gleich viele, wenn dieselbe Griff-Serie verschmolzen wurde.
|
||||
expect(st().undoDepth - depth).toBeGreaterThanOrEqual(0);
|
||||
expect(st().undoDepth - depth).toBeLessThanOrEqual(1);
|
||||
expect(st().project).toEqual(applyOps(p, st().recentOps().at(-1)!));
|
||||
}
|
||||
}
|
||||
expect(committed).toBeGreaterThan(30);
|
||||
expectMirror();
|
||||
const end = JSON.stringify(st().project);
|
||||
while (st().canUndo) st().undo();
|
||||
expect(JSON.stringify(st().project)).toBe(JSON.stringify(start));
|
||||
expectMirror();
|
||||
while (st().canRedo) st().redo();
|
||||
expect(JSON.stringify(st().project)).toBe(end);
|
||||
expectMirror();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
activeGroupOps,
|
||||
addNewElementsToActiveGroup,
|
||||
applyGroupDimming,
|
||||
applyGroupDimmingToPlan,
|
||||
@@ -21,6 +22,9 @@ import {
|
||||
import type { Primitive } from "../plan/generatePlan/shared";
|
||||
import { materializeUnderlay, type Plan } from "../plan/generatePlan";
|
||||
import type { Drawing2D, Group, Project } from "../model/types";
|
||||
import { applyOps } from "../core/ops";
|
||||
import type { CoreOp } from "../core/ops";
|
||||
import { addItems, replaceItem } from "../core/opsBuild";
|
||||
|
||||
const EMPTY_MEM = {
|
||||
wallIds: [] as string[],
|
||||
@@ -261,6 +265,31 @@ describe("addNewElementsToActiveGroup", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("activeGroupOps ([K4] S3: Ops statt Differenz)", () => {
|
||||
const mk = (id: string): Group => ({ id, wallIds: ["w1"], drawingIds: [], ceilingIds: [], roomIds: [], stairIds: [], columnIds: [], roofIds: [], openingIds: [], groupIds: [] });
|
||||
const line = (id: string) =>
|
||||
({ id, type: "drawing2d", levelId: "eg", categoryCode: "20", geom: { shape: "line", a: { x: 0, y: 0 }, b: { x: 1, y: 0 } } }) as Project["drawings2d"][number];
|
||||
const wall = (id: string) => ({ id, start: { x: 0, y: 0 }, end: { x: 1, y: 0 } }) as unknown as Project["walls"][number];
|
||||
|
||||
it("gleiches Ergebnis wie addNewElementsToActiveGroup, Zufall", () => {
|
||||
let x = 11;
|
||||
const r = () => ((x = (x * 1664525 + 1013904223) % 4294967296) / 4294967296);
|
||||
for (let n = 0; n < 200; n++) {
|
||||
const before: Project = { ...project([mk("g1"), mk("g2")]), drawings2d: [line("d0")], walls: [wall("w0")] };
|
||||
const ops: CoreOp[] = [];
|
||||
const k = Math.floor(r() * 4);
|
||||
if (k >= 1) ops.push(...addItems("drawings2d", [line(`a${n}`), line(`b${n}`)], r() < 0.5 ? 0 : undefined));
|
||||
if (k >= 2) ops.push(...addItems("walls", [wall(`w${n}x`)]));
|
||||
if (k >= 3) ops.push(replaceItem("drawings2d", { ...line("d0"), color: "#f00" }));
|
||||
const path = r() < 0.2 ? [] : r() < 0.5 ? ["g1"] : ["g2", "g1", "gx"];
|
||||
const after = applyOps(before, ops);
|
||||
const legacy = addNewElementsToActiveGroup(before, after, path);
|
||||
const viaOps = applyOps(before, [...ops, ...activeGroupOps(before, ops, path)]);
|
||||
expect(viaOps).toEqual(legacy);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("ungroup", () => {
|
||||
it("löst genau EIN Level auf; Untergruppen bleiben als eigene Gruppen bestehen", () => {
|
||||
const inner: Group = { id: "g1", wallIds: ["w1"], drawingIds: [], ceilingIds: [], roomIds: [], stairIds: [], columnIds: [], roofIds: [], openingIds: [], groupIds: [] };
|
||||
|
||||
@@ -9,6 +9,10 @@ import type { Drawing2D, Group, Project } from "../model/types";
|
||||
import type { Plan, UnderlayGroup } from "../core";
|
||||
import { uniqueId } from "../tools/types";
|
||||
import type { Primitive } from "../core";
|
||||
import { applyOps } from "../core";
|
||||
import type { CoreOp } from "../core";
|
||||
import { itemsOf, patchItem } from "../core";
|
||||
import type { EntityCollection } from "../core";
|
||||
|
||||
/** Mitglieds-IDs je Elementart — dieselbe Form wie `Group`, ohne `id`/`name`/`groupIds`. */
|
||||
export interface GroupMembership {
|
||||
@@ -292,6 +296,56 @@ export function addNewElementsToActiveGroup(
|
||||
};
|
||||
}
|
||||
|
||||
/** Liste je Mitgliedsart. */
|
||||
const COLLECTION_OF: Record<MemberKind, EntityCollection> = {
|
||||
wallIds: "walls",
|
||||
drawingIds: "drawings2d",
|
||||
ceilingIds: "ceilings",
|
||||
roomIds: "rooms",
|
||||
stairIds: "stairs",
|
||||
columnIds: "columns",
|
||||
roofIds: "roofs",
|
||||
openingIds: "openings",
|
||||
};
|
||||
|
||||
/**
|
||||
* Wie {@link addNewElementsToActiveGroup}, für eine Änderung als Ops ([K4] S3): die zusätzlichen
|
||||
* Ops, die die neu eingefügten Elemente (`add`-Ops) der aktiven Gruppe zuschlagen — ohne Differenz
|
||||
* über alle Listen. Leer ohne aktive Gruppe oder ohne neue Elemente.
|
||||
*/
|
||||
export function activeGroupOps(before: Project, ops: readonly CoreOp[], groupPath: string[]): CoreOp[] {
|
||||
if (groupPath.length === 0) return [];
|
||||
const addedIds = new Set<string>();
|
||||
const touched = new Set<string>();
|
||||
for (const op of ops) {
|
||||
if (op.kind !== "add") continue;
|
||||
touched.add(op.collection);
|
||||
for (const e of op.items) addedIds.add(e.id);
|
||||
}
|
||||
if (addedIds.size === 0) return [];
|
||||
const after = applyOps(before, ops);
|
||||
const activeId = groupPath[groupPath.length - 1];
|
||||
const g = (after.groups ?? []).find((x) => x.id === activeId);
|
||||
if (!g) return [];
|
||||
const added = { ...EMPTY_MEMBERSHIP };
|
||||
for (const k of KEYS) {
|
||||
const coll = COLLECTION_OF[k];
|
||||
if (!touched.has(coll)) continue;
|
||||
// Reihenfolge wie in der Liste (wie `newIds`); vorher vorhandene ids sind nie neu.
|
||||
const existed = new Set<string>();
|
||||
for (const e of itemsOf(before, coll)) {
|
||||
const id = (e as { id: string }).id;
|
||||
if (addedIds.has(id)) existed.add(id);
|
||||
}
|
||||
added[k] = itemsOf(after, coll)
|
||||
.map((e) => (e as { id: string }).id)
|
||||
.filter((id) => addedIds.has(id) && !existed.has(id));
|
||||
}
|
||||
if (KEYS.every((k) => added[k].length === 0)) return [];
|
||||
const merged: Group = { ...g, ...mergeMembership(g, added) };
|
||||
return patchItem(after, "groups", activeId, () => merged);
|
||||
}
|
||||
|
||||
/**
|
||||
* Erzeugt eine neue Gruppe aus einer Mengen-Auswahl (+ optional bereits
|
||||
* gewählte Gruppen, die dabei als Untergruppen verschachtelt werden — „zwei
|
||||
|
||||
+222
-355
File diff suppressed because it is too large.
Load diff
@@ -92,6 +92,11 @@ export function useGripEditing(opts: {
|
||||
moveRoomBy: (id: string, delta: Vec2) => void;
|
||||
moveCeilingBy: (id: string, delta: Vec2) => void;
|
||||
moveElementByOf: (drawingId: string | null, wallId: string | null, delta: Vec2) => void;
|
||||
/**
|
||||
* Fasst mehrere Änderungen zu EINEM Undo-Schritt zusammen ([K4] S3, Store `groupChanges`); mit
|
||||
* Schlüssel schliessen die Mausschritte desselben Zugs an denselben Schritt an.
|
||||
*/
|
||||
groupChanges?: (fn: () => void, coalesceKey?: string) => void;
|
||||
}) {
|
||||
const {
|
||||
project,
|
||||
@@ -129,6 +134,7 @@ export function useGripEditing(opts: {
|
||||
moveCeilingBy,
|
||||
moveElementByOf,
|
||||
} = opts;
|
||||
const groupChanges = opts.groupChanges ?? ((fn: () => void) => fn());
|
||||
|
||||
// Bewegpunkt (Move-Handle) einer Treppe: liegt auf der Treppe (Mitte des
|
||||
// ersten Laufs bei gerade/L, Zentrum bei Wendel) und verschiebt beim Ziehen
|
||||
@@ -1000,7 +1006,8 @@ export function useGripEditing(opts: {
|
||||
};
|
||||
|
||||
/** Ausstehenden Zug jetzt in das Projekt schreiben (EINE Mutation). */
|
||||
const commitPendingGrip = () => {
|
||||
const commitPendingGrip = () => groupChanges(commitPendingGripChanges);
|
||||
const commitPendingGripChanges = () => {
|
||||
const ph = pendingSheetHeightRef.current;
|
||||
pendingSheetHeightRef.current = null;
|
||||
if (ph) {
|
||||
@@ -1053,7 +1060,8 @@ export function useGripEditing(opts: {
|
||||
};
|
||||
|
||||
/** Aufgeschobenes Körper-Verschieben jetzt ins Projekt schreiben. */
|
||||
const commitPendingBody = () => {
|
||||
const commitPendingBody = () => groupChanges(commitPendingBodyChanges);
|
||||
const commitPendingBodyChanges = () => {
|
||||
const pwm = pendingWallMoveRef.current;
|
||||
pendingWallMoveRef.current = null;
|
||||
sheetDuRef.current = null;
|
||||
@@ -1436,16 +1444,21 @@ export function useGripEditing(opts: {
|
||||
setDraft({ preview: bodyPreviewShapes(), vertices: [], snap: null });
|
||||
return;
|
||||
}
|
||||
moveElementByOf(moveDrawingId, moveWallId, delta);
|
||||
// Beim ersten Move-Event die Koinzidenz-Gruppe einmalig bestimmen (Snapshot
|
||||
// VOR der ersten Mutation der Siblings). Danach inkrementell mitführen.
|
||||
if (moveCoincidentRef.current === null) {
|
||||
moveCoincidentRef.current = findMoveCoincident2D(moveDrawingId, moveWallId);
|
||||
}
|
||||
for (const c of moveCoincidentRef.current) {
|
||||
c.pos = { x: c.pos.x + delta.x, y: c.pos.y + delta.y };
|
||||
moveGripOf(c.drawingId, c.wallId, c.index, c.pos, scaleDenominator);
|
||||
}
|
||||
const coincident = moveCoincidentRef.current;
|
||||
// Element + mitgeführte Ecken: je Mausschritt eine Gruppe, die Schritte desselben Zugs
|
||||
// schliessen an denselben Undo-Schritt an.
|
||||
groupChanges(() => {
|
||||
moveElementByOf(moveDrawingId, moveWallId, delta);
|
||||
for (const c of coincident) {
|
||||
c.pos = { x: c.pos.x + delta.x, y: c.pos.y + delta.y };
|
||||
moveGripOf(c.drawingId, c.wallId, c.index, c.pos, scaleDenominator);
|
||||
}
|
||||
}, `bodyDrag:${moveDrawingId ?? ""}:${moveWallId ?? ""}`);
|
||||
},
|
||||
// Kanten-Griff ziehen. Zwei Verhalten (Zielpunkt jeweils ABSOLUT relativ
|
||||
// zum ORIGINALEN Anker `edge.mid`, angewandt über applyEdgeToward — s. dort):
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
//
|
||||
// Extrahiert aus App.tsx. Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
|
||||
|
||||
import { opsMutation, patchItem, removeItems } from "../core";
|
||||
import type { Project, Vec2 } from "../model/types";
|
||||
import type { CategoryDisplayResolver } from "../core";
|
||||
import type { DisplayResolver } from "../viewport/Viewport3D";
|
||||
@@ -168,14 +169,11 @@ export function useMouseSelectionHandlers(opts: {
|
||||
const id = editTextId;
|
||||
if (!id) return;
|
||||
setProject(
|
||||
(p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) =>
|
||||
d.id === id && d.geom.shape === "text"
|
||||
? { ...d, geom: { ...d.geom, doc, text: plainText(doc) } }
|
||||
: d,
|
||||
opsMutation((p) =>
|
||||
patchItem(p, "drawings2d", id, (d) =>
|
||||
d.geom.shape === "text" ? { ...d, geom: { ...d.geom, doc, text: plainText(doc) } } : d,
|
||||
),
|
||||
}),
|
||||
),
|
||||
`commitTextEdit:${id}`,
|
||||
);
|
||||
};
|
||||
@@ -186,15 +184,14 @@ export function useMouseSelectionHandlers(opts: {
|
||||
const closeTextEdit = () => {
|
||||
const id = editTextId;
|
||||
if (id) {
|
||||
setProject((p) => {
|
||||
const d = p.drawings2d.find((x) => x.id === id);
|
||||
// `text` ist immer der Reintext-Spiegel von `doc` (s. commitTextEdit) —
|
||||
// reicht daher allein als Leer-Test, egal ob `doc` je gesetzt wurde.
|
||||
if (d && d.geom.shape === "text" && d.geom.text.trim() === "") {
|
||||
return { ...p, drawings2d: p.drawings2d.filter((x) => x.id !== id) };
|
||||
}
|
||||
return p;
|
||||
});
|
||||
setProject(
|
||||
opsMutation((p) => {
|
||||
const d = p.drawings2d.find((x) => x.id === id);
|
||||
// `text` ist immer der Reintext-Spiegel von `doc` (s. commitTextEdit) —
|
||||
// reicht daher allein als Leer-Test, egal ob `doc` je gesetzt wurde.
|
||||
return d && d.geom.shape === "text" && d.geom.text.trim() === "" ? removeItems(p, "drawings2d", [id]) : [];
|
||||
}),
|
||||
);
|
||||
}
|
||||
setEditTextId(null);
|
||||
setEditTextMinHeightM(null);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
// Extrahiert aus App.tsx. Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
|
||||
|
||||
import { applyFillMode } from "./fillMode";
|
||||
import { opsMutation, patchItem } from "../core";
|
||||
import { useMemo, type RefObject } from "react";
|
||||
import type { DrawingLevel, Layout, Project } from "../model/types";
|
||||
import { resolvePenColor } from "../model/penSets";
|
||||
@@ -474,21 +475,19 @@ export function usePanelHostBase(opts: {
|
||||
},
|
||||
onSetSelectionFillMode: (mode) => {
|
||||
if (selection?.kind !== "drawing2d") return;
|
||||
setProject((p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) => (d.id === selection.id ? applyFillMode(d, p.hatches, mode) : d)),
|
||||
}));
|
||||
const id = selection.id;
|
||||
setProject(opsMutation((p) => patchItem(p, "drawings2d", id, (d) => applyFillMode(d, p.hatches, mode))));
|
||||
},
|
||||
onSetSelectionFillOpacity: (opacity) => {
|
||||
if (selection?.kind === "drawing2d")
|
||||
setProject((p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) => {
|
||||
if (d.id !== selection.id) return d;
|
||||
const { fillOpacity: _old, ...rest } = d;
|
||||
return opacity >= 1 ? rest : { ...rest, fillOpacity: Math.max(0, opacity) };
|
||||
}),
|
||||
}));
|
||||
setProject(
|
||||
opsMutation((p) =>
|
||||
patchItem(p, "drawings2d", selection.id, (d) => {
|
||||
const { fillOpacity: _old, ...rest } = d;
|
||||
return opacity >= 1 ? rest : { ...rest, fillOpacity: Math.max(0, opacity) };
|
||||
}),
|
||||
),
|
||||
);
|
||||
},
|
||||
onSetSelectionOpacity: (opacity) => {
|
||||
// Deckkraft eines 2D-Elements. Bei BILDERN bleibt es die Deckkraft der
|
||||
@@ -497,15 +496,13 @@ export function usePanelHostBase(opts: {
|
||||
// Füllungen wirkt (`Drawing2D.opacity`). Nur EIN Bedienfeld — welches
|
||||
// Feld gesetzt wird, entscheidet die Form.
|
||||
if (selection?.kind === "drawing2d")
|
||||
setProject((p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) => {
|
||||
if (d.id !== selection.id) return d;
|
||||
return d.geom.shape === "image"
|
||||
? { ...d, geom: { ...d.geom, opacity } }
|
||||
: { ...d, opacity };
|
||||
}),
|
||||
}));
|
||||
setProject(
|
||||
opsMutation((p) =>
|
||||
patchItem(p, "drawings2d", selection.id, (d) =>
|
||||
d.geom.shape === "image" ? { ...d, geom: { ...d.geom, opacity } } : { ...d, opacity },
|
||||
),
|
||||
),
|
||||
);
|
||||
else if (selection?.kind === "layoutAnnotation" && activeLayout)
|
||||
patchOpenLayout(activeLayout.id, (l) =>
|
||||
patchAnnotationInLayout(l, selection.id, { opacity }),
|
||||
@@ -571,44 +568,39 @@ export function usePanelHostBase(opts: {
|
||||
},
|
||||
onAddColorToCustomPalette: (paletteId, hex) => addColorToCustomPalette(paletteId, hex),
|
||||
onSetDashScale: (drawingId, scale) =>
|
||||
setProject((p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) => (d.id === drawingId ? { ...d, dashScale: scale } : d)),
|
||||
})),
|
||||
setProject(opsMutation((p) => patchItem(p, "drawings2d", drawingId, { dashScale: scale }))),
|
||||
onSetDetailRefLabel: (drawingId, label) =>
|
||||
setProject((p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) =>
|
||||
d.id === drawingId && d.geom.shape === "detailref"
|
||||
? { ...d, geom: { ...d.geom, label } }
|
||||
: d,
|
||||
setProject(
|
||||
opsMutation((p) =>
|
||||
patchItem(p, "drawings2d", drawingId, (d) =>
|
||||
d.geom.shape === "detailref" ? { ...d, geom: { ...d.geom, label } } : d,
|
||||
),
|
||||
),
|
||||
})),
|
||||
),
|
||||
onSetDetailRefSheetLayout: (drawingId, sheetLayoutId) =>
|
||||
setProject((p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) => {
|
||||
if (d.id !== drawingId || d.geom.shape !== "detailref") return d;
|
||||
const geom = { ...d.geom };
|
||||
if (sheetLayoutId) geom.sheetLayoutId = sheetLayoutId;
|
||||
else delete geom.sheetLayoutId;
|
||||
return { ...d, geom };
|
||||
}),
|
||||
})),
|
||||
onSetDetailRefSheetLabel: (drawingId, sheetLabel) =>
|
||||
setProject((p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) =>
|
||||
d.id === drawingId && d.geom.shape === "detailref"
|
||||
? { ...d, geom: { ...d.geom, sheetLabel } }
|
||||
: d,
|
||||
setProject(
|
||||
opsMutation((p) =>
|
||||
patchItem(p, "drawings2d", drawingId, (d) => {
|
||||
if (d.geom.shape !== "detailref") return d;
|
||||
const geom = { ...d.geom };
|
||||
if (sheetLayoutId) geom.sheetLayoutId = sheetLayoutId;
|
||||
else delete geom.sheetLayoutId;
|
||||
return { ...d, geom };
|
||||
}),
|
||||
),
|
||||
})),
|
||||
),
|
||||
onSetDetailRefSheetLabel: (drawingId, sheetLabel) =>
|
||||
setProject(
|
||||
opsMutation((p) =>
|
||||
patchItem(p, "drawings2d", drawingId, (d) =>
|
||||
d.geom.shape === "detailref" ? { ...d, geom: { ...d.geom, sheetLabel } } : d,
|
||||
),
|
||||
),
|
||||
),
|
||||
onSetHatchPlacement: (drawingId, patch) =>
|
||||
setProject((p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) => {
|
||||
if (d.id !== drawingId) return d;
|
||||
setProject(
|
||||
opsMutation((p) =>
|
||||
patchItem(p, "drawings2d", drawingId, (d) => {
|
||||
const next = { ...d };
|
||||
// `null` = zurück auf die Ressource (Feld entfernen).
|
||||
if ("angleDeg" in patch)
|
||||
@@ -618,24 +610,23 @@ export function usePanelHostBase(opts: {
|
||||
if ("origin" in patch)
|
||||
patch.origin === null ? delete next.hatchOrigin : (next.hatchOrigin = patch.origin);
|
||||
return next;
|
||||
}),
|
||||
})),
|
||||
}),
|
||||
),
|
||||
),
|
||||
onSetEndMarks: (drawingId, patch) =>
|
||||
setProject((p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) =>
|
||||
d.id === drawingId ? { ...d, endMarks: { ...d.endMarks, ...patch } } : d,
|
||||
setProject(
|
||||
opsMutation((p) =>
|
||||
patchItem(p, "drawings2d", drawingId, (d) => ({ ...d, endMarks: { ...d.endMarks, ...patch } })),
|
||||
),
|
||||
})),
|
||||
),
|
||||
onSetDimensionStyle: (drawingId, patch) =>
|
||||
setProject((p) => ({
|
||||
...p,
|
||||
drawings2d: p.drawings2d.map((d) =>
|
||||
d.id === drawingId && d.geom.shape === "dimension"
|
||||
? { ...d, geom: { ...d.geom, ...patch } }
|
||||
: d,
|
||||
setProject(
|
||||
opsMutation((p) =>
|
||||
patchItem(p, "drawings2d", drawingId, (d) =>
|
||||
d.geom.shape === "dimension" ? { ...d, geom: { ...d.geom, ...patch } } : d,
|
||||
),
|
||||
),
|
||||
})),
|
||||
),
|
||||
onSetSelectionBackgroundPen: (penId) => {
|
||||
if (!selection) return;
|
||||
const background = penId ? resolvePenColor(project, penId) : undefined;
|
||||
|
||||
Reference in new issue
Block a user