47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
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
|
|
})
|
|
);
|
|
}
|
|
};
|
|
}
|