Skip to content

Commit f59a73b

Browse files
committed
fix: editor restore point should create new edit history and tests
1 parent 5d6eb0f commit f59a73b

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/editor/Editor.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,10 @@ define(function (require, exports, module) {
13441344
if(history.done && history.done.length) {
13451345
history.done[history.done.length -1].restorePointName = restorePointName;
13461346
}
1347+
// the current history event should be ‘closed’, meaning it can't be combined with further changes
1348+
// (rapid typing or deleting events are typically combined) as we need to effectively snapshot this history
1349+
// point at this time.
1350+
this._codeMirror.changeGeneration(true);
13471351
};
13481352

13491353
Editor.prototype.restoreHistoryPoint = function (restorePointName) {

test/spec/Editor-test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,5 +2467,59 @@ define(function (require, exports, module) {
24672467
expect(mark.length).toBe(0);
24682468
});
24692469
});
2470+
describe("Editor History undo redo", function () {
2471+
2472+
beforeEach(function () {
2473+
let jsContent = "const x=10;\n" +
2474+
"function print(val){\n" +
2475+
" console.log(val);\n" +
2476+
"}\n" +
2477+
"print(x);\n" +
2478+
"let str=`multi\n" +
2479+
"line`;";
2480+
createTestEditor(jsContent, langNames.javascript.mode);
2481+
});
2482+
2483+
it("should be able to get history", function () {
2484+
expect(myEditor.getHistory().done.length).toBe(1);
2485+
expect(myEditor.getHistory().undone.length).toBe(0);
2486+
myEditor.setSelection({line: 0, ch: 0}, {line: 0, ch: 5});
2487+
myEditor.replaceSelection("hello", "around");
2488+
expect(myEditor.getHistory().done.length).toBe(4);
2489+
expect(myEditor.getHistory().undone.length).toBe(0);
2490+
});
2491+
2492+
it("should be able to create a history restore point and restore to that point", function () {
2493+
expect(myEditor.getHistory().done.length).toBe(1);
2494+
expect(myEditor.getHistory().undone.length).toBe(0);
2495+
myEditor.createHistoryRestorePoint("restore1");
2496+
myEditor.setSelection({line: 0, ch: 0}, {line: 0, ch: 5});
2497+
myEditor.replaceSelection("hello", "around");
2498+
expect(myEditor.getHistory().done.length).toBe(4);
2499+
expect(myEditor.getHistory().undone.length).toBe(0);
2500+
myEditor.restoreHistoryPoint("restore1");
2501+
expect(myEditor.getHistory().done.length).toBe(1);
2502+
expect(myEditor.getHistory().undone.length).toBe(4);
2503+
});
2504+
2505+
it("should be able to set and restore multiple history points", function () {
2506+
expect(myEditor.getHistory().done.length).toBe(1);
2507+
expect(myEditor.getHistory().undone.length).toBe(0);
2508+
myEditor.setSelection({line: 0, ch: 0}, {line: 0, ch: 5});
2509+
myEditor.createHistoryRestorePoint("selectionRestore");
2510+
myEditor.replaceSelection("hello", "around");
2511+
expect(myEditor.getHistory().done.length).toBe(4);
2512+
myEditor.createHistoryRestorePoint("editRestore");
2513+
myEditor.replaceSelection("anotherEdit", "around");
2514+
expect(myEditor.getHistory().done.length).toBe(6);
2515+
expect(myEditor.getHistory().undone.length).toBe(0);
2516+
expect(myEditor.getSelectedText()).toBe("anotherEdit");
2517+
// now restore one by one
2518+
myEditor.restoreHistoryPoint("editRestore");
2519+
expect(myEditor.getSelectedText()).toBe("hello");
2520+
myEditor.restoreHistoryPoint("selectionRestore");
2521+
expect(myEditor.getSelectedText()).toBe("const");
2522+
});
2523+
});
24702524
});
24712525
});

0 commit comments

Comments
 (0)