Files
DOSSIER/src/commands/cmds/copy.ts
T
Claude a24cfbb3d7 [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
2026-10-28 22:02:40 +00:00

162 lines
5.0 KiB
TypeScript

// Copy — wie Move, aber jeder Zielpunkt fügt eine KOPIE der Auswahl (neue IDs)
// an der Ziellage hinzu; das Original bleibt (Rhino-Copy, docs §2.10). Schritte:
// 1) „Basispunkt:" → Punkt
// 2) „Zielpunkt:" → Punkt (Tab length/angle) → Kopie + BLEIBT AKTIV
// → weitere Zielpunkte = weitere Kopien (Basispunkt bleibt), bis Esc/Enter.
//
// Die Auswahl kommt vorab über ctx.selection. Leere Auswahl → No-op + Hinweis.
// Der Commit nutzt `transformMutation` (op:"move", mode:"copy") — eine Kopie der
// Auswahl an die Endlage, Original unverändert; neue IDs via uniqueId (dort).
import {
transformMutation,
hasTransformSelection,
toTransformSel,
transformPreview,
} from "../../tools/transform";
import type {
Command,
CommandField,
CommandResult,
CommandSelection,
CommandState,
Project,
ToolDraft,
Vec2,
} from "../types";
const DEG = Math.PI / 180;
const segLen = (a: Vec2, b: Vec2): number => Math.hypot(b.x - a.x, b.y - a.y);
const segAngleDeg = (a: Vec2, b: Vec2): number =>
(Math.atan2(b.y - a.y, b.x - a.x) * 180) / Math.PI;
/** Tab-Felder des Zielschritts: Länge + Winkel des Kopier-Versatzes (§2.7). */
const COPY_FIELDS: CommandField[] = [
{ id: "length", labelKey: "cmd.field.length" },
{ id: "angle", labelKey: "cmd.field.angle" },
];
function targetFromFields(base: Vec2, locks: Record<string, number>, cursor: Vec2 | null): Vec2 {
const ref = cursor ?? base;
const length = "length" in locks ? locks.length : segLen(base, ref);
const angleDeg = "angle" in locks ? locks.angle : segAngleDeg(base, ref);
const ang = angleDeg * DEG;
return { x: base.x + Math.cos(ang) * length, y: base.y + Math.sin(ang) * length };
}
interface CopyBase extends CommandState {
phase: "base";
}
interface CopyTarget extends CommandState {
phase: "target";
sel: CommandSelection;
base: Vec2;
cursor: Vec2 | null;
}
interface CopyDone extends CommandState {
phase: "done";
}
type CopyState = CopyBase | CopyTarget | CopyDone;
/** Vorschau EINER Kopie der Auswahl an der Cursor-Lage. */
function copyDraft(
project: Project,
sel: CommandSelection,
base: Vec2,
target: Vec2,
): ToolDraft {
const preview = transformPreview(
project,
toTransformSel(sel),
"move",
[base, target],
"copy",
1,
);
return {
preview,
vertices: [base],
hud: {
at: target,
text: `${segLen(base, target).toFixed(2)} m · ${Math.abs(
(segAngleDeg(base, target) + 360) % 360,
).toFixed(0)}°`,
},
};
}
const finish = (): [CommandState, CommandResult] => [
{ phase: "done", lastPoint: null } as CopyDone,
{ draft: null, done: true },
];
export const copyCommand: Command = {
name: "copy",
labelKey: "cmd.copy.label",
prompt: (s) => {
const cs = s as CopyState;
if (cs.phase === "target") return "cmd.copy.target";
if (cs.phase === "done") return "cmd.copy.empty";
return "cmd.copy.base";
},
accepts: () => ["point"],
options: () => [],
init: (): CopyBase => ({ phase: "base", lastPoint: null }),
onInput: (state, input, ctx): [CommandState, CommandResult] => {
const s = state as CopyState;
if (s.phase === "done") return finish();
if (!hasTransformSelection(ctx.selection)) {
return [{ phase: "done", lastPoint: null } as CopyDone, { draft: null, done: true }];
}
if (input.kind !== "point") {
if (s.phase === "target") {
return [s, { draft: s.cursor ? copyDraft(ctx.project, s.sel, s.base, s.cursor) : null }];
}
return [s, { draft: null }];
}
const pt = input.point;
if (s.phase !== "target") {
const next: CopyTarget = {
phase: "target",
sel: ctx.selection,
base: pt,
cursor: pt,
lastPoint: pt,
};
return [next, { draft: copyDraft(ctx.project, next.sel, pt, pt) }];
}
// Zielpunkt → eine Kopie committen, ABER aktiv bleiben (wiederholend): der
// Befehl kehrt NICHT in den Ruhezustand zurück (kein done), Basispunkt
// bleibt; jeder weitere Zielpunkt = weitere Kopie, bis Esc/Enter.
const base = s.base;
const sel = s.sel;
const next: CopyTarget = { ...s, cursor: pt, lastPoint: pt };
return [
next,
{
draft: copyDraft(ctx.project, sel, base, pt),
commit: transformMutation(toTransformSel(sel), "move", [base, pt], "copy", 1),
},
];
},
onMove: (state, point, _snap, ctx): [CommandState, CommandResult] => {
const s = state as CopyState;
if (s.phase !== "target") return [s, { draft: null }];
const ns: CopyTarget = { ...s, cursor: point };
return [ns, { draft: copyDraft(ctx.project, s.sel, s.base, point) }];
},
// Enter/Space/Rechtsklick: Copy-Serie beenden (kein Commit hier).
onConfirm: (): [CommandState, CommandResult] => finish(),
onCancel: (): [CommandState, CommandResult] => finish(),
fields: (state) => ((state as CopyState).phase === "target" ? COPY_FIELDS : []),
pointFromFields: (state, locks, cursor) => {
const s = state as CopyState;
if (s.phase !== "target") return null;
return targetFromFields(s.base, locks, cursor);
},
};