Files
feuerwerk/src/core/commands/Command.ts
T

26 lines
1022 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { Project } from '../model/Project';
/**
* An undoable edit of the project. Commands mutate the project in place and remember
* whatever they need to revert. They validate first: `execute` throws a `CommandError` on
* an invalid operation (e.g. placing a scene into a scene) before it changes anything.
*
* The same holds for `undo`: when its target is gone it throws a `CommandError` rather than
* reverting half of the edit. The command then stays on the undo stack (see `CommandStack`)
* and the editor shows the message. Validation belongs in `execute`/`undo`, never in the
* constructor – commands are built outside the editor's error handling.
*/
export interface Command {
readonly label: string;
execute(project: Project): void;
undo(project: Project): void;
}
/** A rejected edit with a message fit for the user; the editor shows it as a notice. */
export class CommandError extends Error {
constructor(message: string) {
super(message);
this.name = 'CommandError';
}
}