From 2ff3f79f295d6bb1783fbc8e7d7ecb0c3ae3775b Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Fri, 3 Apr 2026 01:03:24 +0200 Subject: [PATCH] Add commands for setting entity and model instance names --- src/commands/set-entity-name-command.ts | 61 +++++++++++++++++++ .../set-model-instance-name-command.ts | 61 +++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 src/commands/set-entity-name-command.ts create mode 100644 src/commands/set-model-instance-name-command.ts diff --git a/src/commands/set-entity-name-command.ts b/src/commands/set-entity-name-command.ts new file mode 100644 index 00000000..9a6236e4 --- /dev/null +++ b/src/commands/set-entity-name-command.ts @@ -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 + }) + } + }); + } + }; +} diff --git a/src/commands/set-model-instance-name-command.ts b/src/commands/set-model-instance-name-command.ts new file mode 100644 index 00000000..b0685826 --- /dev/null +++ b/src/commands/set-model-instance-name-command.ts @@ -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 + }) + } + }); + } + }; +}