[add] .prettierrc.json [add] eslint.config.js [add] index.html [add] package.json [add] playwright.config.ts [add] src/app/App.tsx [add] src/app/app.css [add] src/app/editor-store.ts [add] src/app/use-editor-store.ts [add] src/assets/.gitkeep [add] src/commands/command-history.ts [add] src/commands/command.ts [add] src/commands/set-scene-name-command.ts [add] src/core/ids.ts [add] src/core/selection.ts [add] src/core/tool-mode.ts [add] src/core/vector.ts [add] src/document/migrate-scene-document.ts [add] src/document/scene-document.ts [add] src/entities/.gitkeep [add] src/geometry/.gitkeep [add] src/main.tsx [add] src/materials/.gitkeep [add] src/runtime-three/.gitkeep [add] src/serialization/local-draft-storage.ts [add] src/serialization/scene-document-json.ts [add] src/shared-ui/Panel.tsx [add] src/viewport-three/ViewportCanvas.tsx [add] src/viewport-three/viewport-host.ts [add] src/vite-env.d.ts [add] tests/domain/create-empty-scene-document.test.ts [add] tests/domain/editor-store.test.ts [add] tests/e2e/app-smoke.e2e.ts [add] tests/serialization/scene-document-json.test.ts [add] tests/setup/vitest.setup.ts [add] tsconfig.json [add] vite.config.ts [add] vitest.config.ts
50 lines
1.0 KiB
TypeScript
50 lines
1.0 KiB
TypeScript
import type { CommandContext, EditorCommand } from "./command";
|
|
|
|
export class CommandHistory {
|
|
private readonly undoStack: EditorCommand[] = [];
|
|
private readonly redoStack: EditorCommand[] = [];
|
|
|
|
execute(command: EditorCommand, context: CommandContext) {
|
|
command.execute(context);
|
|
this.undoStack.push(command);
|
|
this.redoStack.length = 0;
|
|
}
|
|
|
|
undo(context: CommandContext): EditorCommand | null {
|
|
const command = this.undoStack.pop();
|
|
|
|
if (command === undefined) {
|
|
return null;
|
|
}
|
|
|
|
command.undo(context);
|
|
this.redoStack.push(command);
|
|
return command;
|
|
}
|
|
|
|
redo(context: CommandContext): EditorCommand | null {
|
|
const command = this.redoStack.pop();
|
|
|
|
if (command === undefined) {
|
|
return null;
|
|
}
|
|
|
|
command.execute(context);
|
|
this.undoStack.push(command);
|
|
return command;
|
|
}
|
|
|
|
clear() {
|
|
this.undoStack.length = 0;
|
|
this.redoStack.length = 0;
|
|
}
|
|
|
|
canUndo(): boolean {
|
|
return this.undoStack.length > 0;
|
|
}
|
|
|
|
canRedo(): boolean {
|
|
return this.redoStack.length > 0;
|
|
}
|
|
}
|