Feat: Update delete path command to manage junction connections
This commit is contained in:
@@ -2,6 +2,10 @@ import { createOpaqueId } from "../core/ids";
|
||||
import { cloneEditorSelection, type EditorSelection } from "../core/selection";
|
||||
import type { ToolMode } from "../core/tool-mode";
|
||||
import { cloneScenePath, type ScenePath } from "../document/paths";
|
||||
import {
|
||||
cloneSplineCorridorJunction,
|
||||
type SplineCorridorJunction
|
||||
} from "../document/spline-corridor-junctions";
|
||||
|
||||
import type { EditorCommand } from "./command";
|
||||
|
||||
@@ -15,6 +19,7 @@ function selectionIncludesPath(selection: EditorSelection, pathId: string): bool
|
||||
|
||||
export function createDeletePathCommand(pathId: string): EditorCommand {
|
||||
let previousPath: ScenePath | null = null;
|
||||
let previousJunctions: Record<string, SplineCorridorJunction> | null = null;
|
||||
let previousSelection: EditorSelection | null = null;
|
||||
let previousToolMode: ToolMode | null = null;
|
||||
|
||||
@@ -33,6 +38,21 @@ export function createDeletePathCommand(pathId: string): EditorCommand {
|
||||
previousPath = cloneScenePath(currentPath);
|
||||
}
|
||||
|
||||
if (previousJunctions === null) {
|
||||
previousJunctions = Object.fromEntries(
|
||||
Object.entries(currentDocument.splineCorridorJunctions)
|
||||
.filter(([, junction]) =>
|
||||
junction.connections.some(
|
||||
(connection) => connection.pathId === pathId
|
||||
)
|
||||
)
|
||||
.map(([junctionId, junction]) => [
|
||||
junctionId,
|
||||
cloneSplineCorridorJunction(junction)
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
if (previousSelection === null) {
|
||||
previousSelection = cloneEditorSelection(context.getSelection());
|
||||
}
|
||||
@@ -45,10 +65,24 @@ export function createDeletePathCommand(pathId: string): EditorCommand {
|
||||
...currentDocument.paths
|
||||
};
|
||||
delete nextPaths[pathId];
|
||||
const nextJunctions = Object.fromEntries(
|
||||
Object.entries(currentDocument.splineCorridorJunctions)
|
||||
.map(([junctionId, junction]) => [
|
||||
junctionId,
|
||||
cloneSplineCorridorJunction({
|
||||
...junction,
|
||||
connections: junction.connections.filter(
|
||||
(connection) => connection.pathId !== pathId
|
||||
)
|
||||
})
|
||||
])
|
||||
.filter(([, junction]) => junction.connections.length >= 2)
|
||||
);
|
||||
|
||||
context.setDocument({
|
||||
...currentDocument,
|
||||
paths: nextPaths
|
||||
paths: nextPaths,
|
||||
splineCorridorJunctions: nextJunctions
|
||||
});
|
||||
|
||||
if (selectionIncludesPath(context.getSelection(), pathId)) {
|
||||
@@ -71,6 +105,17 @@ export function createDeletePathCommand(pathId: string): EditorCommand {
|
||||
paths: {
|
||||
...currentDocument.paths,
|
||||
[previousPath.id]: cloneScenePath(previousPath)
|
||||
},
|
||||
splineCorridorJunctions: {
|
||||
...currentDocument.splineCorridorJunctions,
|
||||
...Object.fromEntries(
|
||||
Object.entries(previousJunctions ?? {}).map(
|
||||
([junctionId, junction]) => [
|
||||
junctionId,
|
||||
cloneSplineCorridorJunction(junction)
|
||||
]
|
||||
)
|
||||
)
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user