auto-git:
[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
This commit is contained in:
49
src/commands/command-history.ts
Normal file
49
src/commands/command-history.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
19
src/commands/command.ts
Normal file
19
src/commands/command.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import type { EditorSelection } from "../core/selection";
|
||||
import type { ToolMode } from "../core/tool-mode";
|
||||
import type { SceneDocument } from "../document/scene-document";
|
||||
|
||||
export interface CommandContext {
|
||||
getDocument(): SceneDocument;
|
||||
setDocument(document: SceneDocument): void;
|
||||
getSelection(): EditorSelection;
|
||||
setSelection(selection: EditorSelection): void;
|
||||
getToolMode(): ToolMode;
|
||||
setToolMode(toolMode: ToolMode): void;
|
||||
}
|
||||
|
||||
export interface EditorCommand {
|
||||
id: string;
|
||||
label: string;
|
||||
execute(context: CommandContext): void;
|
||||
undo(context: CommandContext): void;
|
||||
}
|
||||
36
src/commands/set-scene-name-command.ts
Normal file
36
src/commands/set-scene-name-command.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { createOpaqueId } from "../core/ids";
|
||||
|
||||
import type { EditorCommand } from "./command";
|
||||
|
||||
export function createSetSceneNameCommand(nextName: string): EditorCommand {
|
||||
const normalizedName = nextName.trim() || "Untitled Scene";
|
||||
let previousName: string | null = null;
|
||||
|
||||
return {
|
||||
id: createOpaqueId("command"),
|
||||
label: `Rename scene to ${normalizedName}`,
|
||||
execute(context) {
|
||||
const currentDocument = context.getDocument();
|
||||
|
||||
if (previousName === null) {
|
||||
previousName = currentDocument.name;
|
||||
}
|
||||
|
||||
context.setDocument({
|
||||
...currentDocument,
|
||||
name: normalizedName
|
||||
});
|
||||
},
|
||||
undo(context) {
|
||||
if (previousName === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentDocument = context.getDocument();
|
||||
context.setDocument({
|
||||
...currentDocument,
|
||||
name: previousName
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user