big cmap refactoring

This commit is contained in:
2026-09-03 08:40:15 +02:00
parent f0562a06cc
commit 2c141b6d3f
32 changed files with 5616 additions and 4216 deletions
+48
View File
@@ -0,0 +1,48 @@
import test from "node:test";
import assert from "node:assert/strict";
import { CmapHistory } from "../static/cmap/controller/cmap-history.js";
function history(initial = "one") {
let value = initial;
const changes = [];
const instance = new CmapHistory({
snapshot: () => value,
restore: (snapshot) => { value = snapshot; },
onChange: (change) => changes.push(change),
limit: 2
});
return {
instance,
changes,
get value() { return value; },
set value(next) { value = next; }
};
}
test("CmapHistory commits, undoes and redoes serialized state", () => {
const state = history();
state.instance.reset();
state.value = "two";
assert.equal(state.instance.commit(), true);
state.value = "three";
assert.equal(state.instance.commit(), true);
assert.equal(state.instance.undo(), true);
assert.equal(state.value, "two");
assert.equal(state.instance.redo(), true);
assert.equal(state.value, "three");
assert.equal(state.instance.canUndo(), true);
assert.equal(state.instance.canRedo(), false);
});
test("CmapHistory replaces a document as one undo operation", () => {
const state = history();
state.instance.reset();
state.instance.replace(() => { state.value = "replacement"; });
assert.equal(state.value, "replacement");
assert.equal(state.instance.undo(), true);
assert.equal(state.value, "one");
assert.ok(state.changes.length > 0);
});