Files
DOSSIER/src/menus/AppContextMenu.tsx
T

202 lines
9.5 KiB
TypeScript

// Kontextmenü der App ([163], aus App.tsx herausgelöst): verdrahtet das offene Menü (`MenuState`,
// genau eines: Ebene, Zeichnungsebene, Grundriss, 3D) mit den Projekt-Aktionen und den Befehlen auf
// der Auswahl. Die Einträge selbst baut `buildMenuItems` (contextMenu.tsx). Gerendert von `App.tsx`,
// solange ein Menü offen ist. Das Projekt abonniert das Menü selbst ([121], App liest es nicht mehr);
// die Anzeige-Werte zur Auswahl rechnet `selectionMenuState`.
import type { RefObject } from "react";
import { getState, useStore } from "../state/appStore";
import { ContextMenu } from "../ui/ContextMenu";
import type { Vec2 } from "../model/types";
import type { PlanViewHandle } from "../views/plan/PlanView";
import { countClosedAreas, differenceDrawings, intersectionDrawings, unionDrawings } from "../editors/booleanOps";
import { categoryNames } from "../state/selectionLayers";
import type { useProjectActions } from "../state/useProjectActions";
import { hideLayerCodes, isolateLayerCodes, showAllLayerCodes } from "../state/layerIsolate";
import { selectionMenuState, type useSelectionCommands } from "../state/useSelectionCommands";
import type { useSelectionState } from "../state/useSelectionState";
import { buildMenuItems, menuTitle, type MenuState } from "./contextMenu";
import type { useContextMenuState } from "./useContextMenuState";
type Menu = ReturnType<typeof useContextMenuState>;
type Sel = ReturnType<typeof useSelectionState>;
export function AppContextMenu(props: {
menu: NonNullable<MenuState>;
selection: Sel["selection"];
setSelection: Sel["setSelection"];
selectedWallId: string | null;
layerClipboard: Menu["layerClipboard"];
setLayerClipboard: Menu["setLayerClipboard"];
drawingStyleClipboard: Menu["drawingStyleClipboard"];
setMenu: Menu["setMenu"];
setEditor: Menu["setEditor"];
openLayerSettings: (code: string, x: number, y: number) => void;
planViewRef: RefObject<PlanViewHandle | null>;
pa: ReturnType<typeof useProjectActions>;
selCmds: ReturnType<typeof useSelectionCommands>;
}) {
const {
menu,
selection,
setSelection,
selectedWallId,
layerClipboard,
setLayerClipboard,
drawingStyleClipboard,
setMenu,
setEditor,
openLayerSettings,
planViewRef,
} = props;
const {
addSubCategory,
assignWallCategory,
duplicateCategory,
deleteCategory,
patchCategory,
duplicateLevel,
deleteLevel,
reorderDrawings,
insertDrawingPoint,
insertSectionBend,
deleteSectionBend,
insertRingVertex,
deleteRingVertex,
deleteDrawingPoint,
setPolylineVertexType,
reorderDrawingLevels,
} = props.pa;
const {
applyBoolean,
copyDrawingStyle,
pasteDrawingStyle,
selectionLockIds,
toggleSelectionLock,
hideSelectionLayers,
isolateSelectionLayers,
toggleSelectionNoPrint,
detachDimensions,
resetDimensionTexts,
} = props.selCmds;
const project = useStore((s) => s.project);
const deleteMeshPoint = useStore((s) => s.deleteMeshPoint);
const setMeshHeightPrompt = useStore((s) => s.setMeshHeightPrompt);
const { selectionAllLocked, selectionLayerCodes, selectionAllNoPrint, attachedDimensionIds, movedDimTextIds } = selectionMenuState(
project,
selection,
true,
);
return (
<ContextMenu
x={menu.x}
y={menu.y}
title={menuTitle(menu, project)}
items={buildMenuItems(menu, {
project,
selectedWallId,
layerClipboard,
openLayerEditor: (code) => openLayerSettings(code, menu.x, menu.y),
openLevelEditor: (id) =>
setEditor({ kind: "level", id, x: menu.x, y: menu.y }),
addSubCategory,
assignWallCategory,
duplicateCategory,
deleteCategory,
setLayerClipboard,
patchCategory,
duplicateLevel,
deleteLevel,
clearSelection: () => setSelection({ walls: [] }),
fit: () => planViewRef.current?.fit(),
fitSelection: () => planViewRef.current?.fitSelection(),
closedSelectionCount: countClosedAreas(
project.drawings2d.filter((d) =>
selection.drawings.includes(d.id),
),
),
union: () => applyBoolean(unionDrawings),
difference: () => applyBoolean(differenceDrawings),
intersect: () => applyBoolean(intersectionDrawings),
drawingSelectionCount: selection.drawings.length,
hasDrawingStyleClipboard: drawingStyleClipboard !== null,
copyDrawingStyle,
pasteDrawingStyle,
lockableSelectionCount: selectionLockIds.length,
selectionAllLocked,
toggleSelectionLock,
selectionLayerNames: categoryNames(project.layers, selectionLayerCodes),
hideSelectionLayers,
isolateSelectionLayers,
isolateLayer: (code) => isolateLayerCodes([code]),
hideLayer: (code) => hideLayerCodes([code]),
showAllLayers: showAllLayerCodes,
selectionAllNoPrint: !!selectionAllNoPrint,
toggleSelectionNoPrint,
attachedDimensionCount: attachedDimensionIds.length,
detachDimensions,
movedDimensionTextCount: movedDimTextIds.length,
resetDimensionTexts,
bringToFront: () => reorderDrawings(selection.drawings, "front"),
bringForward: () => reorderDrawings(selection.drawings, "forward"),
sendBackward: () => reorderDrawings(selection.drawings, "backward"),
sendToBack: () => reorderDrawings(selection.drawings, "back"),
// Stützpunkt-Einträge nur bei GENAU EINER selektierten Polylinie
// (mehrere gleichzeitig hätten keinen eindeutigen Klick-Bezug).
selectedPolyline: (() => {
if (selection.drawings.length !== 1) return null;
const d = project.drawings2d.find((x) => x.id === selection.drawings[0]);
return d && d.geom.shape === "polyline"
? { id: d.id, pts: d.geom.pts, closed: d.geom.closed, bulge: d.geom.bulge }
: null;
})(),
// [176] Geknickte Schnittlinie: Knick einfügen/löschen am gewählten Schnitt.
selectedSectionLine: (() => {
if (selection.sectionLines.length !== 1) return null;
const l = project.drawingLevels.find((x) => x.id === selection.sectionLines[0] && x.linePoints);
return l?.linePoints ? { id: l.id, pts: [l.linePoints[0], ...(l.bendPoints ?? []), l.linePoints[1]] } : null;
})(),
insertSectionBend,
deleteSectionBend,
// [26] Umriss-Ecken: genau ein Element mit Umriss gewählt (sonst kein Bezug für den Klick).
selectedRing: (() => {
const total = Object.values(selection).reduce((n, v) => n + (Array.isArray(v) ? v.length : 0), 0);
if (total !== 1) return null;
const roof = selection.roofs.length === 1 ? (project.roofs ?? []).find((x) => x.id === selection.roofs[0]) : undefined;
if (roof && !roof.locked) {
const bulges = [roof.outlineBulge, ...(roof.holes ?? []).map((_, k) => roof.holesBulge?.[k])];
return { kind: "roof" as const, id: roof.id, rings: [roof.outline, ...(roof.holes ?? [])].reduce<{ pts: Vec2[]; off: number; bulge?: number[] }[]>(
(acc, pts, k) => [...acc, { pts, off: acc.reduce((n, r) => n + r.pts.length, 0), bulge: bulges[k] }], []) };
}
const ceiling = selection.ceilings.length === 1 ? (project.ceilings ?? []).find((x) => x.id === selection.ceilings[0]) : undefined;
if (ceiling && !ceiling.locked) return { kind: "ceiling" as const, id: ceiling.id, rings: [{ pts: ceiling.outline, off: 0, bulge: ceiling.outlineBulge }] };
const room = selection.rooms.length === 1 ? (project.rooms ?? []).find((x) => x.id === selection.rooms[0]) : undefined;
if (room && !room.locked) return { kind: "room" as const, id: room.id, rings: [{ pts: room.boundary, off: 0, bulge: room.boundaryBulge }] };
const ctx = selection.contextObjects.length === 1 ? (project.context ?? []).find((x) => x.id === selection.contextObjects[0]) : undefined;
if (ctx && ctx.type === "excavation") return { kind: "excavation" as const, id: ctx.id, rings: [{ pts: ctx.outline, off: 0, bulge: ctx.outlineBulge }] };
// [193] Höhenlinien-Satz: je Linie ein Ring (flacher Index über alle Linien); offene Linien haben kein Schlusssegment.
if (ctx && ctx.type === "contourSet") {
return { kind: "contours" as const, id: ctx.id, rings: ctx.contours.reduce<{ pts: Vec2[]; off: number; closed: boolean }[]>(
(acc, c) => (c.pts.length >= 2 ? [...acc, { pts: c.pts, off: ctx.contours.slice(0, ctx.contours.indexOf(c)).reduce((n, x) => n + x.pts.length, 0), closed: !!c.closed }] : acc), []) };
}
return null;
})(),
insertRingVertex,
deleteRingVertex,
selectedMesh: (() => {
const ctx = selection.contextObjects.length === 1 ? (project.context ?? []).find((x) => x.id === selection.contextObjects[0]) : undefined;
return ctx && (ctx.type === "terrainMesh" || ctx.type === "importedMesh") ? { id: ctx.id, positions: ctx.positions } : null;
})(),
askMeshPointHeight: (id, index) => setMeshHeightPrompt({ id, index }),
deleteMeshPoint: (id, index) => { deleteMeshPoint(id, index); },
setRingArc: (kind, id, seg, pt) => getState().setRingArc(kind, id, seg, pt),
insertPolylinePoint: insertDrawingPoint,
deletePolylinePoint: deleteDrawingPoint,
setPolylineVertexType,
reorderLevel: (id, op) => reorderDrawingLevels([id], op),
})}
onClose={() => setMenu(null)}
/>
);
}