[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
@@ -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