2019-08-01 09:25:04 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2019-08-01 09:25:04 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-08-01 09:25:04 +00:00
|
|
|
*/
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import HistoryManager, { IHistory, MAX_STEP_LENGTH } from "../../../src/editor/history";
|
|
|
|
import EditorModel from "../../../src/editor/model";
|
|
|
|
import DocumentPosition from "../../../src/editor/position";
|
2019-08-01 09:25:04 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("editor/history", function () {
|
|
|
|
it("push, then undo", function () {
|
2019-08-01 09:25:04 +00:00
|
|
|
const history = new HistoryManager();
|
|
|
|
const parts = ["hello"];
|
2022-04-06 11:40:13 +00:00
|
|
|
const model = { serializeParts: () => parts.slice() } as unknown as EditorModel;
|
|
|
|
const caret1 = new DocumentPosition(0, 0);
|
2022-12-12 11:24:14 +00:00
|
|
|
const result1 = history.tryPush(model, caret1, "insertText", {});
|
2019-08-01 09:25:04 +00:00
|
|
|
expect(result1).toEqual(true);
|
|
|
|
parts[0] = "hello world";
|
2022-12-12 11:24:14 +00:00
|
|
|
history.tryPush(model, new DocumentPosition(0, 0), "insertText", {});
|
2019-08-01 09:25:04 +00:00
|
|
|
expect(history.canUndo()).toEqual(true);
|
2023-03-23 13:35:55 +00:00
|
|
|
const undoState = history.undo(model) as IHistory;
|
2022-04-06 11:40:13 +00:00
|
|
|
expect(undoState.caret).toBe(caret1);
|
2019-08-01 09:25:04 +00:00
|
|
|
expect(undoState.parts).toEqual(["hello"]);
|
|
|
|
expect(history.canUndo()).toEqual(false);
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("push, undo, then redo", function () {
|
2019-08-01 09:25:04 +00:00
|
|
|
const history = new HistoryManager();
|
|
|
|
const parts = ["hello"];
|
2022-04-06 11:40:13 +00:00
|
|
|
const model = { serializeParts: () => parts.slice() } as unknown as EditorModel;
|
2022-12-12 11:24:14 +00:00
|
|
|
history.tryPush(model, new DocumentPosition(0, 0), "insertText", {});
|
2019-08-01 09:25:04 +00:00
|
|
|
parts[0] = "hello world";
|
2022-04-06 11:40:13 +00:00
|
|
|
const caret2 = new DocumentPosition(0, 0);
|
2022-12-12 11:24:14 +00:00
|
|
|
history.tryPush(model, caret2, "insertText", {});
|
2019-08-01 09:25:04 +00:00
|
|
|
history.undo(model);
|
|
|
|
expect(history.canRedo()).toEqual(true);
|
2023-03-23 13:35:55 +00:00
|
|
|
const redoState = history.redo() as IHistory;
|
2022-04-06 11:40:13 +00:00
|
|
|
expect(redoState.caret).toBe(caret2);
|
2019-08-01 09:25:04 +00:00
|
|
|
expect(redoState.parts).toEqual(["hello world"]);
|
|
|
|
expect(history.canRedo()).toEqual(false);
|
|
|
|
expect(history.canUndo()).toEqual(true);
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("push, undo, push, ensure you can`t redo", function () {
|
2019-08-01 09:25:04 +00:00
|
|
|
const history = new HistoryManager();
|
|
|
|
const parts = ["hello"];
|
2022-04-06 11:40:13 +00:00
|
|
|
const model = { serializeParts: () => parts.slice() } as unknown as EditorModel;
|
|
|
|
history.tryPush(model, new DocumentPosition(0, 0), "insertText", {});
|
2019-08-01 09:25:04 +00:00
|
|
|
parts[0] = "hello world";
|
2022-04-06 11:40:13 +00:00
|
|
|
history.tryPush(model, new DocumentPosition(0, 0), "insertText", {});
|
2019-08-01 09:25:04 +00:00
|
|
|
history.undo(model);
|
|
|
|
parts[0] = "hello world!!";
|
2022-04-06 11:40:13 +00:00
|
|
|
history.tryPush(model, new DocumentPosition(0, 0), "insertText", {});
|
2019-08-01 09:25:04 +00:00
|
|
|
expect(history.canRedo()).toEqual(false);
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("not every keystroke stores a history step", function () {
|
2019-08-01 09:25:04 +00:00
|
|
|
const history = new HistoryManager();
|
|
|
|
const parts = ["hello"];
|
2022-04-06 11:40:13 +00:00
|
|
|
const model = { serializeParts: () => parts.slice() } as unknown as EditorModel;
|
|
|
|
const firstCaret = new DocumentPosition(0, 0);
|
|
|
|
history.tryPush(model, firstCaret, "insertText", {});
|
2021-06-29 12:11:58 +00:00
|
|
|
const diff = { added: "o" };
|
2019-08-01 09:25:04 +00:00
|
|
|
let keystrokeCount = 0;
|
|
|
|
do {
|
|
|
|
parts[0] = parts[0] + diff.added;
|
|
|
|
keystrokeCount += 1;
|
2022-04-06 11:40:13 +00:00
|
|
|
} while (!history.tryPush(model, new DocumentPosition(0, 0), "insertText", diff));
|
2023-03-23 13:35:55 +00:00
|
|
|
const undoState = history.undo(model) as IHistory;
|
2022-04-06 11:40:13 +00:00
|
|
|
expect(undoState.caret).toBe(firstCaret);
|
2019-08-01 09:25:04 +00:00
|
|
|
expect(undoState.parts).toEqual(["hello"]);
|
|
|
|
expect(history.canUndo()).toEqual(false);
|
2019-08-01 14:10:21 +00:00
|
|
|
expect(keystrokeCount).toEqual(MAX_STEP_LENGTH + 1); // +1 before we type before checking
|
2019-08-01 09:25:04 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("history step is added at word boundary", function () {
|
2019-08-02 09:31:01 +00:00
|
|
|
const history = new HistoryManager();
|
2022-04-06 11:40:13 +00:00
|
|
|
const model = { serializeParts: () => parts.slice() } as unknown as EditorModel;
|
2019-08-02 09:31:01 +00:00
|
|
|
const parts = ["h"];
|
2021-06-29 12:11:58 +00:00
|
|
|
let diff = { added: "h" };
|
2022-04-06 11:40:13 +00:00
|
|
|
const blankCaret = new DocumentPosition(0, 0);
|
|
|
|
expect(history.tryPush(model, blankCaret, "insertText", diff)).toEqual(false);
|
2021-06-29 12:11:58 +00:00
|
|
|
diff = { added: "i" };
|
2019-08-02 09:31:01 +00:00
|
|
|
parts[0] = "hi";
|
2022-04-06 11:40:13 +00:00
|
|
|
expect(history.tryPush(model, blankCaret, "insertText", diff)).toEqual(false);
|
2021-06-29 12:11:58 +00:00
|
|
|
diff = { added: " " };
|
2019-08-02 09:31:01 +00:00
|
|
|
parts[0] = "hi ";
|
2022-04-06 11:40:13 +00:00
|
|
|
const spaceCaret = new DocumentPosition(1, 1);
|
2019-08-02 09:31:01 +00:00
|
|
|
expect(history.tryPush(model, spaceCaret, "insertText", diff)).toEqual(true);
|
2021-06-29 12:11:58 +00:00
|
|
|
diff = { added: "y" };
|
2019-08-02 09:31:01 +00:00
|
|
|
parts[0] = "hi y";
|
2022-04-06 11:40:13 +00:00
|
|
|
expect(history.tryPush(model, blankCaret, "insertText", diff)).toEqual(false);
|
2021-06-29 12:11:58 +00:00
|
|
|
diff = { added: "o" };
|
2019-08-02 09:31:01 +00:00
|
|
|
parts[0] = "hi yo";
|
2022-04-06 11:40:13 +00:00
|
|
|
expect(history.tryPush(model, blankCaret, "insertText", diff)).toEqual(false);
|
2021-06-29 12:11:58 +00:00
|
|
|
diff = { added: "u" };
|
2019-08-02 09:31:01 +00:00
|
|
|
parts[0] = "hi you";
|
|
|
|
|
|
|
|
expect(history.canUndo()).toEqual(true);
|
2023-03-23 13:35:55 +00:00
|
|
|
const undoResult = history.undo(model) as IHistory;
|
2019-08-02 09:31:01 +00:00
|
|
|
expect(undoResult.caret).toEqual(spaceCaret);
|
|
|
|
expect(undoResult.parts).toEqual(["hi "]);
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("keystroke that didn't add a step can undo", function () {
|
2019-08-01 09:25:04 +00:00
|
|
|
const history = new HistoryManager();
|
|
|
|
const parts = ["hello"];
|
2022-04-06 11:40:13 +00:00
|
|
|
const model = { serializeParts: () => parts.slice() } as unknown as EditorModel;
|
|
|
|
const firstCaret = new DocumentPosition(0, 0);
|
|
|
|
history.tryPush(model, firstCaret, "insertText", {});
|
2019-08-01 09:25:04 +00:00
|
|
|
parts[0] = "helloo";
|
2022-04-06 11:40:13 +00:00
|
|
|
const result = history.tryPush(model, new DocumentPosition(0, 0), "insertText", { added: "o" });
|
2019-08-01 09:25:04 +00:00
|
|
|
expect(result).toEqual(false);
|
|
|
|
expect(history.canUndo()).toEqual(true);
|
2023-03-23 13:35:55 +00:00
|
|
|
const undoState = history.undo(model) as IHistory;
|
2019-08-01 09:25:04 +00:00
|
|
|
expect(undoState.caret).toEqual(firstCaret);
|
|
|
|
expect(undoState.parts).toEqual(["hello"]);
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("undo after keystroke that didn't add a step is able to redo", function () {
|
2019-08-01 09:25:04 +00:00
|
|
|
const history = new HistoryManager();
|
|
|
|
const parts = ["hello"];
|
2022-04-06 11:40:13 +00:00
|
|
|
const model = { serializeParts: () => parts.slice() } as unknown as EditorModel;
|
|
|
|
history.tryPush(model, new DocumentPosition(0, 0), "insertText", {});
|
2019-08-01 09:25:04 +00:00
|
|
|
parts[0] = "helloo";
|
2022-04-06 11:40:13 +00:00
|
|
|
const caret = new DocumentPosition(1, 1);
|
2021-06-29 12:11:58 +00:00
|
|
|
history.tryPush(model, caret, "insertText", { added: "o" });
|
2019-08-01 09:25:04 +00:00
|
|
|
history.undo(model);
|
|
|
|
expect(history.canRedo()).toEqual(true);
|
2023-03-23 13:35:55 +00:00
|
|
|
const redoState = history.redo() as IHistory;
|
2022-04-06 11:40:13 +00:00
|
|
|
expect(redoState.caret).toBe(caret);
|
2019-08-01 09:25:04 +00:00
|
|
|
expect(redoState.parts).toEqual(["helloo"]);
|
|
|
|
});
|
2022-04-06 11:40:13 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("overwriting text always stores a step", function () {
|
2019-08-01 09:25:04 +00:00
|
|
|
const history = new HistoryManager();
|
|
|
|
const parts = ["hello"];
|
2022-04-06 11:40:13 +00:00
|
|
|
const model = { serializeParts: () => parts.slice() } as unknown as EditorModel;
|
|
|
|
const firstCaret = new DocumentPosition(0, 0);
|
2022-12-12 11:24:14 +00:00
|
|
|
history.tryPush(model, firstCaret, "insertText", {});
|
2021-06-29 12:11:58 +00:00
|
|
|
const diff = { at: 1, added: "a", removed: "e" };
|
2022-04-06 11:40:13 +00:00
|
|
|
const secondCaret = new DocumentPosition(1, 1);
|
|
|
|
const result = history.tryPush(model, secondCaret, "insertText", diff);
|
2019-08-01 09:25:04 +00:00
|
|
|
expect(result).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|