Skip to content
Open
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: 7 additions & 3 deletions packages/history/src/stack/history-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ export class HistoryStack {
}
return;
}
for (;;) {
const last = this._past.findLast((entry) => entry.undoable);
if (!last || last.id <= entryId) return;
/** Positions, not ids: restored and recorded steps are numbered out of stack order. */
const target =
entryId === SAVED_AT_START ? -1 : this._past.findIndex((entry) => entry.id === entryId);
if (target < 0 && entryId !== SAVED_AT_START) return;
while (this._past.findLastIndex((entry) => entry.undoable) > target) {
if (!(await this._undoOne())) return;
}
});
Expand All @@ -137,6 +139,8 @@ export class HistoryStack {
this._future = [];
this._savedAt = null;
}
/** The state at load (savepoint 0) is now the one after the restored steps. */
if (this._savedAt === SAVED_AT_START && restored.length) this._savedAt = restored.at(-1)!.id;
this._past = [...restored, ...this._past];
this._trim();
this._changed();
Expand Down
1 change: 1 addition & 0 deletions packages/history/src/stack/history-stack.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CommandOrigin, HistoryCommand } from '../command/history-command.type';

export interface HistoryEntry {
/** Unique in its stack, but not in stack order (restored and recorded steps): compare positions. */
readonly id: number;
readonly command: HistoryCommand;
readonly origin: CommandOrigin;
Expand Down
62 changes: 61 additions & 1 deletion packages/history/test/stack/history-stack.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from 'vitest';

import { type HistoryCommand, compositeCommand } from '../../src';
import { type HistoryCommand, type HistoryEntry, compositeCommand } from '../../src';
import { cell, setup } from '../fixtures/history';

describe('HistoryStack', () => {
Expand Down Expand Up @@ -152,6 +152,66 @@ describe('HistoryStack', () => {
expect(stack.state.dirty).toBe(false);
});

describe('goTo', () => {
const labels = (entries: readonly HistoryEntry[]) =>
entries.map((entry) => entry.command.label);

it('reaches steps restored under the steps made while loading', async () => {
const { history, stack } = setup();
const { set } = cell(0);
const restored = (value: number) => ({
command: set(value),
origin: history.localOrigin,
time: 0,
});
await stack.push(set(3));
await stack.restore([restored(1), restored(2)]);
await stack.goTo(stack.state.past[0]!.id);
expect(labels(stack.state.past)).toEqual(['set 1']);
});

it('keeps an id read before a queued restore pointing at its step', async () => {
const { history, stack } = setup();
const { set } = cell(0);
await stack.push(set(3));
const id = stack.state.past[0]!.id;
await stack.push(set(4));
void stack.restore([{ command: set(1), origin: history.localOrigin, time: 0 }]);
await stack.goTo(id);
expect(labels(stack.state.past)).toEqual(['set 1', 'set 3']);
});

it('reaches a change of another origin recorded before a redo', async () => {
const { stack } = setup();
const { set } = cell(0);
await stack.push(set(1));
await stack.push(set(2));
await stack.undo();
await stack.record({ ...set(9), origin: { kind: 'remote', id: 'peer' } });
await stack.redo();
await stack.goTo(stack.state.past[1]!.id);
expect(labels(stack.state.past)).toEqual(['set 1', 'set 9']);
});
});

it('keeps the state at load as the savepoint when steps are restored under new ones', async () => {
const { history, stack } = setup();
const { set } = cell(0);
const restored = (value: number) => ({
command: set(value),
origin: history.localOrigin,
time: 0,
});
await stack.push(set(3));
await stack.restore([restored(1), restored(2)]);
expect(stack.state.dirty).toBe(true);
await stack.undo();
expect(stack.state.dirty).toBe(false);
await stack.undo();
await stack.undo();
expect(stack.state.dirty).toBe(true);
});

it('drops the history when a command is no longer valid', async () => {
const { history, stack } = setup();
const invalidated = vi.fn();
Expand Down
Loading