Add commands for setting entity and model instance names

This commit is contained in:
2026-04-03 01:03:24 +02:00
parent 1e82418c53
commit 2ff3f79f29
2 changed files with 122 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
import { createOpaqueId } from "../core/ids";
import { cloneEntityInstance, normalizeEntityName } from "../entities/entity-instances";
import type { EditorCommand } from "./command";
interface SetEntityNameCommandOptions {
entityId: string;
name: string | null;
}
export function createSetEntityNameCommand(options: SetEntityNameCommandOptions): EditorCommand {
const normalizedName = normalizeEntityName(options.name);
let previousName: string | undefined;
return {
id: createOpaqueId("command"),
label: normalizedName === undefined ? "Clear entity name" : `Rename entity to ${normalizedName}`,
execute(context) {
const currentDocument = context.getDocument();
const entity = currentDocument.entities[options.entityId];
if (entity === undefined) {
throw new Error(`Entity ${options.entityId} does not exist.`);
}
if (previousName === undefined) {
previousName = entity.name;
}
context.setDocument({
...currentDocument,
entities: {
...currentDocument.entities,
[entity.id]: cloneEntityInstance({
...entity,
name: normalizedName
})
}
});
},
undo(context) {
const currentDocument = context.getDocument();
const entity = currentDocument.entities[options.entityId];
if (entity === undefined) {
throw new Error(`Entity ${options.entityId} does not exist.`);
}
context.setDocument({
...currentDocument,
entities: {
...currentDocument.entities,
[entity.id]: cloneEntityInstance({
...entity,
name: previousName
})
}
});
}
};
}

View File

@@ -0,0 +1,61 @@
import { createOpaqueId } from "../core/ids";
import { cloneModelInstance, normalizeModelInstanceName } from "../assets/model-instances";
import type { EditorCommand } from "./command";
interface SetModelInstanceNameCommandOptions {
modelInstanceId: string;
name: string | null;
}
export function createSetModelInstanceNameCommand(options: SetModelInstanceNameCommandOptions): EditorCommand {
const normalizedName = normalizeModelInstanceName(options.name);
let previousName: string | undefined;
return {
id: createOpaqueId("command"),
label: normalizedName === undefined ? "Clear model instance name" : `Rename model instance to ${normalizedName}`,
execute(context) {
const currentDocument = context.getDocument();
const modelInstance = currentDocument.modelInstances[options.modelInstanceId];
if (modelInstance === undefined) {
throw new Error(`Model instance ${options.modelInstanceId} does not exist.`);
}
if (previousName === undefined) {
previousName = modelInstance.name;
}
context.setDocument({
...currentDocument,
modelInstances: {
...currentDocument.modelInstances,
[modelInstance.id]: cloneModelInstance({
...modelInstance,
name: normalizedName
})
}
});
},
undo(context) {
const currentDocument = context.getDocument();
const modelInstance = currentDocument.modelInstances[options.modelInstanceId];
if (modelInstance === undefined) {
throw new Error(`Model instance ${options.modelInstanceId} does not exist.`);
}
context.setDocument({
...currentDocument,
modelInstances: {
...currentDocument.modelInstances,
[modelInstance.id]: cloneModelInstance({
...modelInstance,
name: previousName
})
}
});
}
};
}