Allow adding path points at specific indices within a path

This commit is contained in:
2026-05-13 05:09:31 +02:00
parent 2fc119fc65
commit d31f4ded8b

View File

@@ -5,6 +5,8 @@ import {
cloneScenePath, cloneScenePath,
cloneScenePathPoint, cloneScenePathPoint,
createAppendedScenePathPoint, createAppendedScenePathPoint,
createScenePathPointAfter,
getScenePathPointIndex,
type ScenePathPoint type ScenePathPoint
} from "../document/paths"; } from "../document/paths";
@@ -13,6 +15,7 @@ import type { EditorCommand } from "./command";
interface AddPathPointCommandOptions { interface AddPathPointCommandOptions {
pathId: string; pathId: string;
point?: ScenePathPoint; point?: ScenePathPoint;
insertAfterPointId?: string;
label?: string; label?: string;
} }
@@ -30,7 +33,7 @@ function setSelectedPathPointSelection(
export function createAddPathPointCommand( export function createAddPathPointCommand(
options: AddPathPointCommandOptions options: AddPathPointCommandOptions
): EditorCommand { ): EditorCommand {
let appendedPoint: ScenePathPoint | null = let addedPoint: ScenePathPoint | null =
options.point === undefined ? null : cloneScenePathPoint(options.point); options.point === undefined ? null : cloneScenePathPoint(options.point);
let previousSelection: EditorSelection | null = null; let previousSelection: EditorSelection | null = null;
let previousToolMode: ToolMode | null = null; let previousToolMode: ToolMode | null = null;
@@ -46,8 +49,11 @@ export function createAddPathPointCommand(
throw new Error(`Path ${options.pathId} does not exist.`); throw new Error(`Path ${options.pathId} does not exist.`);
} }
if (appendedPoint === null) { if (addedPoint === null) {
appendedPoint = createAppendedScenePathPoint(path); addedPoint =
options.insertAfterPointId === undefined
? createAppendedScenePathPoint(path)
: createScenePathPointAfter(path, options.insertAfterPointId);
} }
if (previousSelection === null) { if (previousSelection === null) {
@@ -58,23 +64,38 @@ export function createAddPathPointCommand(
previousToolMode = context.getToolMode(); previousToolMode = context.getToolMode();
} }
const insertionIndex =
options.insertAfterPointId === undefined
? path.points.length
: getScenePathPointIndex(path, options.insertAfterPointId) + 1;
if (insertionIndex <= 0) {
throw new Error(
`Path point ${options.insertAfterPointId} does not exist.`
);
}
context.setDocument({ context.setDocument({
...currentDocument, ...currentDocument,
paths: { paths: {
...currentDocument.paths, ...currentDocument.paths,
[path.id]: cloneScenePath({ [path.id]: cloneScenePath({
...path, ...path,
points: [...path.points, cloneScenePathPoint(appendedPoint)] points: [
...path.points.slice(0, insertionIndex),
cloneScenePathPoint(addedPoint),
...path.points.slice(insertionIndex)
]
}) })
} }
}); });
context.setSelection( context.setSelection(
setSelectedPathPointSelection(options.pathId, appendedPoint.id) setSelectedPathPointSelection(options.pathId, addedPoint.id)
); );
context.setToolMode("select"); context.setToolMode("select");
}, },
undo(context) { undo(context) {
if (appendedPoint === null) { if (addedPoint === null) {
return; return;
} }
@@ -91,7 +112,7 @@ export function createAddPathPointCommand(
...currentDocument.paths, ...currentDocument.paths,
[path.id]: cloneScenePath({ [path.id]: cloneScenePath({
...path, ...path,
points: path.points.filter((point) => point.id !== appendedPoint?.id) points: path.points.filter((point) => point.id !== addedPoint?.id)
}) })
} }
}); });