Add commands for setting entity and model instance names
This commit is contained in:
61
src/commands/set-entity-name-command.ts
Normal file
61
src/commands/set-entity-name-command.ts
Normal 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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
61
src/commands/set-model-instance-name-command.ts
Normal file
61
src/commands/set-model-instance-name-command.ts
Normal 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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user