26 lines
1022 B
TypeScript
26 lines
1022 B
TypeScript
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';
|
||
}
|
||
}
|