From 666280400b678cb124252e0bab93459ad11afbc4 Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Wed, 13 May 2026 13:14:16 +0200 Subject: [PATCH] Feat: Add command to delete spline corridor junctions --- ...delete-spline-corridor-junction-command.ts | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/commands/delete-spline-corridor-junction-command.ts diff --git a/src/commands/delete-spline-corridor-junction-command.ts b/src/commands/delete-spline-corridor-junction-command.ts new file mode 100644 index 00000000..97fe62f1 --- /dev/null +++ b/src/commands/delete-spline-corridor-junction-command.ts @@ -0,0 +1,77 @@ +import { createOpaqueId } from "../core/ids"; +import { cloneEditorSelection, type EditorSelection } from "../core/selection"; +import type { ToolMode } from "../core/tool-mode"; +import { + cloneSplineCorridorJunction, + type SplineCorridorJunction +} from "../document/spline-corridor-junctions"; + +import type { EditorCommand } from "./command"; + +export function createDeleteSplineCorridorJunctionCommand( + junctionId: string +): EditorCommand { + let previousJunction: SplineCorridorJunction | null = null; + let previousSelection: EditorSelection | null = null; + let previousToolMode: ToolMode | null = null; + + return { + id: createOpaqueId("command"), + label: "Delete spline corridor junction", + execute(context) { + const currentDocument = context.getDocument(); + const junction = currentDocument.splineCorridorJunctions[junctionId]; + + if (junction === undefined) { + throw new Error(`Spline corridor junction ${junctionId} does not exist.`); + } + + if (previousJunction === null) { + previousJunction = cloneSplineCorridorJunction(junction); + } + + if (previousSelection === null) { + previousSelection = cloneEditorSelection(context.getSelection()); + } + + if (previousToolMode === null) { + previousToolMode = context.getToolMode(); + } + + const nextJunctions = { + ...currentDocument.splineCorridorJunctions + }; + delete nextJunctions[junctionId]; + + context.setDocument({ + ...currentDocument, + splineCorridorJunctions: nextJunctions + }); + context.setToolMode("select"); + }, + undo(context) { + if (previousJunction === null) { + return; + } + + const currentDocument = context.getDocument(); + + context.setDocument({ + ...currentDocument, + splineCorridorJunctions: { + ...currentDocument.splineCorridorJunctions, + [previousJunction.id]: cloneSplineCorridorJunction(previousJunction) + } + }); + + if (previousSelection !== null) { + context.setSelection(previousSelection); + } + + if (previousToolMode !== null) { + context.setToolMode(previousToolMode); + } + } + }; +} +