Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/core/edit-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,16 @@ export class Edit {
}
});
}
/** True when there is a command to undo (the history pointer is not before the first command). */
public get canUndo(): boolean {
return this.commandIndex >= 0;
}

/** True when there is a command ahead to redo (the history pointer is not at the latest command). */
public get canRedo(): boolean {
return this.commandIndex < this.commandHistory.length - 1;
}

/** @internal */
public setUpdatedClip(clip: Player, initialClipConfig: ResolvedClip | null = null, finalClipConfig: ResolvedClip | null = null): void {
// Find track and clip indices
Expand Down
55 changes: 55 additions & 0 deletions tests/edit-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,61 @@ describe("Edit Command History", () => {
});
});

describe("canUndo / canRedo getters", () => {
it("are both false on a freshly loaded edit (load is not a command)", () => {
expect(getCommandState(edit).index).toBe(-1);
expect(edit.canUndo).toBe(false);
expect(edit.canRedo).toBe(false);
});

it("canUndo turns true after a command; canRedo stays false", async () => {
await edit.executeEditCommand(new TestCommand());
expect(edit.canUndo).toBe(true);
expect(edit.canRedo).toBe(false);
});

it("undo turns canRedo on and, back at the start, canUndo off", async () => {
await edit.executeEditCommand(new TestCommand());
await edit.undo();
expect(edit.canUndo).toBe(false);
expect(edit.canRedo).toBe(true);
});

it("redo turns canUndo back on and, at the end, canRedo off", async () => {
await edit.executeEditCommand(new TestCommand());
await edit.undo();
await edit.redo();
expect(edit.canUndo).toBe(true);
expect(edit.canRedo).toBe(false);
});

it("both are true mid-stack", async () => {
await edit.executeEditCommand(new TestCommand());
await edit.executeEditCommand(new TestCommand());
await edit.undo();
expect(edit.canUndo).toBe(true);
expect(edit.canRedo).toBe(true);
});

it("a new command after undo truncates redo (canRedo false)", async () => {
await edit.executeEditCommand(new TestCommand());
await edit.executeEditCommand(new TestCommand());
await edit.undo();
await edit.executeEditCommand(new TestCommand());
expect(edit.canUndo).toBe(true);
expect(edit.canRedo).toBe(false);
});

it("track the underlying commandIndex bounds", async () => {
await edit.executeEditCommand(new TestCommand());
await edit.executeEditCommand(new TestCommand());
await edit.undo();
const { history, index } = getCommandState(edit);
expect(edit.canUndo).toBe(index >= 0);
expect(edit.canRedo).toBe(index < history.length - 1);
});
});

describe("state restoration", () => {
it("undo restores previous state via command.undo()", async () => {
// Use a command that tracks state changes
Expand Down
Loading