[K4][6][8] Einfügen, Gruppieren, Ungruppieren erzeugen Ops direkt (pasteElementClipboard, groupSelection, ungroup)
Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
6 files changed
+49
-45
No files matched your search
+3
-2
@@ -353,6 +353,7 @@ export default function App() {
|
||||
// Endlosschleife). Actions leben ebenfalls im Zustand und sind referenzstabil.
|
||||
const project = useStore((s) => s.project);
|
||||
const setProject = useStore((s) => s.setProject);
|
||||
const applyChange = useStore((s) => s.applyChange);
|
||||
const groupChanges = useStore((s) => s.groupChanges);
|
||||
const notify = useStore((s) => s.notify);
|
||||
const undo = useStore((s) => s.undo);
|
||||
@@ -1496,7 +1497,7 @@ export default function App() {
|
||||
};
|
||||
useGroupShortcut(
|
||||
project,
|
||||
setProject,
|
||||
applyChange,
|
||||
{
|
||||
wallIds: selectedWallIds,
|
||||
drawingIds: selectedDrawingIds,
|
||||
@@ -1873,7 +1874,7 @@ export default function App() {
|
||||
setElementClipboard,
|
||||
activeLevelId,
|
||||
project,
|
||||
setProject,
|
||||
applyChange,
|
||||
{
|
||||
wallIds: setSelectedWallIds,
|
||||
drawingIds: setSelectedDrawingIds,
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { collectElementClipboard, pasteElementClipboard, isClipboardEmpty } from "./elementClipboard";
|
||||
import { applyOps } from "../core/ops";
|
||||
import type { ElementSelectionIds } from "./elementClipboard";
|
||||
import type { Project, Wall, Opening, Ceiling } from "../model/types";
|
||||
|
||||
@@ -121,7 +122,8 @@ describe("pasteElementClipboard", () => {
|
||||
it("fügt Wand + Öffnung mit NEUEN, verknüpften IDs auf dem aktiven Geschoss ein", () => {
|
||||
const p = project();
|
||||
const clip = collectElementClipboard(p, { ...emptySel(), wallIds: ["w1"] })!;
|
||||
const { project: next, pastedIds } = pasteElementClipboard(p, clip, "og");
|
||||
const { ops, pastedIds } = pasteElementClipboard(clip, "og");
|
||||
const next = applyOps(p, ops);
|
||||
expect(next.walls.length).toBe(2);
|
||||
expect(next.openings!.length).toBe(2);
|
||||
const newWall = next.walls.find((w) => w.id === pastedIds.wallIds[0])!;
|
||||
@@ -137,7 +139,7 @@ describe("pasteElementClipboard", () => {
|
||||
it("Originale bleiben unverändert (immutabel)", () => {
|
||||
const p = project();
|
||||
const clip = collectElementClipboard(p, { ...emptySel(), wallIds: ["w1"] })!;
|
||||
pasteElementClipboard(p, clip, "og");
|
||||
pasteElementClipboard(clip, "og");
|
||||
expect(p.walls.length).toBe(1);
|
||||
expect(p.walls[0].id).toBe("w1");
|
||||
});
|
||||
|
||||
@@ -31,6 +31,8 @@ import type {
|
||||
Wall,
|
||||
} from "../model/types";
|
||||
import { uniqueId } from "../tools/types";
|
||||
import { addItems } from "../core";
|
||||
import type { CoreOp } from "../core";
|
||||
|
||||
/** Auswahl-IDs je Bauteil-Art, wie sie die 9 Auswahl-Kanäle des Stores liefern. */
|
||||
export interface ElementSelectionIds {
|
||||
@@ -145,13 +147,12 @@ export interface PastedIds {
|
||||
/**
|
||||
* Fügt eine Zwischenablage ins Projekt ein: Klone mit neuen IDs auf dem
|
||||
* AKTIVEN Geschoss (`activeLevelId`), an denselben Modell-Koordinaten. Liefert
|
||||
* das neue Projekt + die neuen IDs (für die Nachauswahl).
|
||||
* die Ops (Anhängen je Liste) + die neuen IDs (für die Nachauswahl).
|
||||
*/
|
||||
export function pasteElementClipboard(
|
||||
project: Project,
|
||||
clip: ElementClipboard,
|
||||
activeLevelId: string,
|
||||
): { project: Project; pastedIds: PastedIds } {
|
||||
): { ops: CoreOp[]; pastedIds: PastedIds } {
|
||||
const wallIdMap = new Map<string, string>();
|
||||
const newWalls: Wall[] = clip.walls.map((w) => {
|
||||
const id = uniqueId("W");
|
||||
@@ -204,21 +205,21 @@ export function pasteElementClipboard(
|
||||
floorId: activeLevelId,
|
||||
}));
|
||||
|
||||
const next: Project = {
|
||||
...project,
|
||||
walls: [...project.walls, ...newWalls],
|
||||
drawings2d: [...project.drawings2d, ...newDrawings],
|
||||
ceilings: [...(project.ceilings ?? []), ...newCeilings],
|
||||
roofs: [...(project.roofs ?? []), ...newRoofs],
|
||||
openings: [...(project.openings ?? []), ...newOpenings],
|
||||
stairs: [...(project.stairs ?? []), ...newStairs],
|
||||
rooms: [...(project.rooms ?? []), ...newRooms],
|
||||
extrudedSolids: [...(project.extrudedSolids ?? []), ...newExtrudedSolids],
|
||||
columns: [...(project.columns ?? []), ...newColumns],
|
||||
};
|
||||
// Wände vor Öffnungen (Wirtswand zuerst), sonst Reihenfolge der Projekt-Listen.
|
||||
const ops: CoreOp[] = [
|
||||
...addItems("walls", newWalls),
|
||||
...addItems("drawings2d", newDrawings),
|
||||
...addItems("ceilings", newCeilings),
|
||||
...addItems("roofs", newRoofs),
|
||||
...addItems("openings", newOpenings),
|
||||
...addItems("stairs", newStairs),
|
||||
...addItems("rooms", newRooms),
|
||||
...addItems("extrudedSolids", newExtrudedSolids),
|
||||
...addItems("columns", newColumns),
|
||||
];
|
||||
|
||||
return {
|
||||
project: next,
|
||||
ops,
|
||||
pastedIds: {
|
||||
wallIds: newWalls.map((w) => w.id),
|
||||
drawingIds: newDrawings.map((d) => d.id),
|
||||
|
||||
@@ -56,24 +56,24 @@ function project(groups: Group[]): Project {
|
||||
describe("groupSelection", () => {
|
||||
it("erzeugt eine Gruppe aus ≥2 gewählten Elementen", () => {
|
||||
const p = project([]);
|
||||
const res = groupSelection(p, { ...EMPTY_MEM, wallIds: ["w1", "w2"] }, []);
|
||||
const res = groupSelection({ ...EMPTY_MEM, wallIds: ["w1", "w2"] }, []);
|
||||
expect(res).not.toBeNull();
|
||||
expect(res!.project.groups).toHaveLength(1);
|
||||
expect(res!.project.groups![0]).toMatchObject({ wallIds: ["w1", "w2"], groupIds: [] });
|
||||
const out = applyOps(p, res!.ops);
|
||||
expect(out.groups).toHaveLength(1);
|
||||
expect(out.groups![0]).toMatchObject({ wallIds: ["w1", "w2"], groupIds: [] });
|
||||
});
|
||||
|
||||
it("weniger als 2 Elemente gruppiert nicht (Vectorworks-Konvention)", () => {
|
||||
const p = project([]);
|
||||
expect(groupSelection(p, { ...EMPTY_MEM, wallIds: ["w1"] }, [])).toBeNull();
|
||||
expect(groupSelection(p, EMPTY_MEM, [])).toBeNull();
|
||||
expect(groupSelection({ ...EMPTY_MEM, wallIds: ["w1"] }, [])).toBeNull();
|
||||
expect(groupSelection(EMPTY_MEM, [])).toBeNull();
|
||||
});
|
||||
|
||||
it("bereits gewählte Gruppen werden als Untergruppen verschachtelt, nicht vermischt", () => {
|
||||
const inner: Group = { id: "g1", wallIds: ["w1"], drawingIds: [], ceilingIds: [], roomIds: [], stairIds: [], columnIds: [], roofIds: [], openingIds: [], groupIds: [] };
|
||||
const p = project([inner]);
|
||||
const res = groupSelection(p, { ...EMPTY_MEM, wallIds: ["w2"] }, ["g1"]);
|
||||
const res = groupSelection({ ...EMPTY_MEM, wallIds: ["w2"] }, ["g1"]);
|
||||
expect(res).not.toBeNull();
|
||||
const outer = res!.project.groups!.find((g) => g.id === res!.groupId)!;
|
||||
const outer = applyOps(p, res!.ops).groups!.find((g) => g.id === res!.groupId)!;
|
||||
expect(outer.wallIds).toEqual(["w2"]);
|
||||
expect(outer.groupIds).toEqual(["g1"]);
|
||||
});
|
||||
@@ -297,7 +297,7 @@ describe("ungroup", () => {
|
||||
const p = project([inner, outer]);
|
||||
const res = ungroup(p, "g2");
|
||||
expect(res).not.toBeNull();
|
||||
expect(res!.project.groups!.map((g) => g.id)).toEqual(["g1"]); // g2 weg, g1 bleibt
|
||||
expect(applyOps(p, res!.ops).groups!.map((g) => g.id)).toEqual(["g1"]); // g2 weg, g1 bleibt
|
||||
expect(res!.members.wallIds).toEqual(["w2"]);
|
||||
expect(res!.subGroupIds).toEqual(["g1"]);
|
||||
});
|
||||
|
||||
@@ -11,7 +11,7 @@ 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 { addItems, itemsOf, patchItem, removeItems } from "../core";
|
||||
import type { EntityCollection } from "../core";
|
||||
|
||||
/** Mitglieds-IDs je Elementart — dieselbe Form wie `Group`, ohne `id`/`name`/`groupIds`. */
|
||||
@@ -353,10 +353,9 @@ export function activeGroupOps(before: Project, ops: readonly CoreOp[], groupPat
|
||||
* ihre Mitglieder platt zu vermischen). Leere Auswahl → `null` (nichts zu tun).
|
||||
*/
|
||||
export function groupSelection(
|
||||
project: Project,
|
||||
sel: GroupMembership,
|
||||
selectedGroupIds: string[],
|
||||
): { project: Project; groupId: string } | null {
|
||||
): { ops: CoreOp[]; groupId: string } | null {
|
||||
const totalCount =
|
||||
KEYS.reduce((n, k) => n + sel[k].length, 0) + selectedGroupIds.length;
|
||||
if (totalCount < 2) return null; // Gruppieren braucht ≥ 2 Elemente (Vectorworks-Konvention)
|
||||
@@ -373,7 +372,7 @@ export function groupSelection(
|
||||
openingIds: sel.openingIds,
|
||||
groupIds: [...selectedGroupIds],
|
||||
};
|
||||
return { project: { ...project, groups: [...(project.groups ?? []), g] }, groupId: id };
|
||||
return { ops: addItems("groups", [g]), groupId: id };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -386,11 +385,11 @@ export function groupSelection(
|
||||
export function ungroup(
|
||||
project: Project,
|
||||
groupId: string,
|
||||
): { project: Project; members: GroupMembership; subGroupIds: string[] } | null {
|
||||
): { ops: CoreOp[]; members: GroupMembership; subGroupIds: string[] } | null {
|
||||
const g = (project.groups ?? []).find((x) => x.id === groupId);
|
||||
if (!g) return null;
|
||||
return {
|
||||
project: { ...project, groups: (project.groups ?? []).filter((x) => x.id !== groupId) },
|
||||
ops: removeItems(project, "groups", [groupId]),
|
||||
members: {
|
||||
wallIds: g.wallIds,
|
||||
drawingIds: g.drawingIds,
|
||||
|
||||
@@ -18,6 +18,7 @@ import type { CopyMode, TransformOp } from "../tools/transform";
|
||||
import type { CommandEngine } from "../commands/engine";
|
||||
import type { CommandLineHandle } from "../ui/CommandLine";
|
||||
import type { Drawing2D, Project, Vec2 } from "../model/types";
|
||||
import type { CoreOp } from "../core";
|
||||
import { differenceDrawings, intersectionDrawings, unionDrawings } from "../editors/booleanOps";
|
||||
import type { BooleanResult } from "../editors/booleanOps";
|
||||
import { groupSelection, mergeMembership, ungroup, EMPTY_MEMBERSHIP } from "./groupActions";
|
||||
@@ -548,7 +549,7 @@ export function useElementClipboardShortcut(
|
||||
setElementClipboard: (c: ElementClipboard | null) => void,
|
||||
activeLevelId: string,
|
||||
project: Project,
|
||||
setProject: (next: Project | ((p: Project) => Project)) => void,
|
||||
applyChange: (ops: readonly CoreOp[]) => void,
|
||||
setSelected: {
|
||||
wallIds: (ids: string[]) => void;
|
||||
drawingIds: (ids: string[]) => void;
|
||||
@@ -587,8 +588,8 @@ export function useElementClipboardShortcut(
|
||||
e.preventDefault();
|
||||
const paste = (clip: ElementClipboard | null) => {
|
||||
if (!clip || isClipboardEmpty(clip)) return;
|
||||
const { project: next, pastedIds } = pasteElementClipboard(project, clip, activeLevelId);
|
||||
setProject(next);
|
||||
const { ops, pastedIds } = pasteElementClipboard(clip, activeLevelId);
|
||||
applyChange(ops);
|
||||
setSelected.wallIds(pastedIds.wallIds);
|
||||
setSelected.drawingIds(pastedIds.drawingIds);
|
||||
setSelected.ceilingIds(pastedIds.ceilingIds);
|
||||
@@ -764,7 +765,7 @@ export function useTransformShortcuts(
|
||||
*/
|
||||
export function useGroupShortcut(
|
||||
project: Project,
|
||||
setProject: (updater: (p: Project) => Project) => void,
|
||||
applyChange: (ops: readonly CoreOp[]) => void,
|
||||
currentSelection: GroupMembership,
|
||||
selectedGroupIds: string[],
|
||||
applySelection: (mem: GroupMembership, groupIds: string[]) => void,
|
||||
@@ -781,24 +782,24 @@ export function useGroupShortcut(
|
||||
// Ungruppieren: für jede gewählte Gruppe eine Hülle auflösen; ihre
|
||||
// direkten Mitglieder (+ Untergruppen) werden die neue Auswahl.
|
||||
if (selectedGroupIds.length === 0) return;
|
||||
let p = project;
|
||||
const ops: CoreOp[] = [];
|
||||
let members: GroupMembership = { ...EMPTY_MEMBERSHIP };
|
||||
const newGroupIds: string[] = [];
|
||||
for (const gid of selectedGroupIds) {
|
||||
const res = ungroup(p, gid);
|
||||
const res = ungroup(project, gid);
|
||||
if (!res) continue;
|
||||
p = res.project;
|
||||
ops.push(...res.ops);
|
||||
members = mergeMembership(members, res.members);
|
||||
newGroupIds.push(...res.subGroupIds);
|
||||
}
|
||||
setProject(() => p);
|
||||
applyChange(ops);
|
||||
applySelection(members, newGroupIds);
|
||||
return;
|
||||
}
|
||||
// Gruppieren: aktuelle Auswahl (+ bereits gewählte Gruppen als Untergruppen).
|
||||
const res = groupSelection(project, currentSelection, selectedGroupIds);
|
||||
const res = groupSelection(currentSelection, selectedGroupIds);
|
||||
if (!res) return;
|
||||
setProject(() => res.project);
|
||||
applyChange(res.ops);
|
||||
applySelection(currentSelection, [res.groupId]);
|
||||
};
|
||||
window.addEventListener("keydown", onKey);
|
||||
|
||||
Reference in new issue
Block a user