Add set box brush name command and update scene document migration
This commit is contained in:
46
src/commands/set-box-brush-name-command.ts
Normal file
46
src/commands/set-box-brush-name-command.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { createOpaqueId } from "../core/ids";
|
||||
import { normalizeBrushName } from "../document/brushes";
|
||||
|
||||
import { getBoxBrushOrThrow, replaceBrush } from "./brush-command-helpers";
|
||||
import type { EditorCommand } from "./command";
|
||||
|
||||
interface SetBoxBrushNameCommandOptions {
|
||||
brushId: string;
|
||||
name: string | null;
|
||||
}
|
||||
|
||||
export function createSetBoxBrushNameCommand(options: SetBoxBrushNameCommandOptions): EditorCommand {
|
||||
const normalizedName = normalizeBrushName(options.name);
|
||||
let previousName: string | undefined;
|
||||
|
||||
return {
|
||||
id: createOpaqueId("command"),
|
||||
label: normalizedName === undefined ? "Clear box brush name" : `Rename box brush to ${normalizedName}`,
|
||||
execute(context) {
|
||||
const currentDocument = context.getDocument();
|
||||
const brush = getBoxBrushOrThrow(currentDocument, options.brushId);
|
||||
|
||||
if (previousName === undefined) {
|
||||
previousName = brush.name;
|
||||
}
|
||||
|
||||
context.setDocument(
|
||||
replaceBrush(currentDocument, {
|
||||
...brush,
|
||||
name: normalizedName
|
||||
})
|
||||
);
|
||||
},
|
||||
undo(context) {
|
||||
const currentDocument = context.getDocument();
|
||||
const brush = getBoxBrushOrThrow(currentDocument, options.brushId);
|
||||
|
||||
context.setDocument(
|
||||
replaceBrush(currentDocument, {
|
||||
...brush,
|
||||
name: previousName
|
||||
})
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user