Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC custom step history #4128

Open
wants to merge 1 commit into
base: master-mysterious-egg
Choose a base branch
from
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
15 changes: 13 additions & 2 deletions addons/html_editor/static/src/core/history_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ export class HistoryPlugin extends Plugin {
* @param { Object } [params]
* @param { "consumed"|"undo"|"redo" } [params.stepState]
*/
addStep({ stepState } = {}) {
addStep({ stepState, customStep } = {}) {
// @todo @phoenix should we allow to pause the making of a step?
// if (!this.stepsActive) {
// return;
Expand All @@ -559,12 +559,15 @@ export class HistoryPlugin extends Plugin {
// executing the onChange callback.
// It is useful for external components if they execute shared.can[Undo|Redo]
const currentStep = this.currentStep;
if (customStep) {
currentStep.customStep = customStep;
}
if (stepState) {
this.stepsStates.set(currentStep.id, stepState);
}

this.handleObserverRecords();
if (!currentStep.mutations.length) {
if (!currentStep.mutations.length && !currentStep.customStep) {
return false;
}
const stepCommonAncestor = this.getMutationsRoot(currentStep.mutations) || this.editable;
Expand Down Expand Up @@ -618,6 +621,10 @@ export class HistoryPlugin extends Plugin {
this.stepsStates.set(this.steps[pos].id, "consumed");
this.revertMutations(this.steps[pos].mutations, { forNewStep: true });
this.setSerializedSelection(this.steps[pos].selection);
if (this.steps[pos].customStep) {
this.steps[pos].customStep.undo();
this.currentStep.customStep = this.steps[pos].customStep;
}
this.addStep({ stepState: "undo" });
// Consider the last position of the history as an undo.
}
Expand All @@ -639,6 +646,10 @@ export class HistoryPlugin extends Plugin {
this.stepsStates.set(this.steps[pos].id, "consumed");
this.revertMutations(this.steps[pos].mutations, { forNewStep: true });
this.setSerializedSelection(this.steps[pos].selection);
if (this.steps[pos].customStep) {
this.steps[pos].customStep.redo();
this.currentStep.customStep = this.steps[pos].customStep;
}
this.addStep({ stepState: "redo" });
}
this.dispatchTo("post_redo_handlers");
Expand Down
29 changes: 29 additions & 0 deletions addons/html_editor/static/tests/history.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,35 @@ describe("undo", () => {
redo(editor);
expect(getContent(el)).toBe(`<p>a[]c</p>`);
});

test("should trigger custom step", async () => {
const { el, editor } = await setupEditor(`<p>[]c</p>`);
const p = el.querySelector("p");
editor.shared.dom.insert("a");
editor.shared.history.addStep();
editor.shared.history.addStep({
customStep: {
undo: () => {
expect.step("custom undo");
},
redo: () => {
expect.step("custom redo");
},
},
});
p.prepend(document.createTextNode("b"));
undo(editor);
expect.verifySteps(["custom undo"]);
expect(getContent(el)).toBe(`<p>a[]c</p>`);
undo(editor);
expect(getContent(el)).toBe(`<p>[]c</p>`);
redo(editor);
expect.verifySteps([]);
expect(getContent(el)).toBe(`<p>a[]c</p>`);
redo(editor);
expect.verifySteps(["custom redo"]);
expect(getContent(el)).toBe(`<p>a[]c</p>`);
});
});

describe("redo", () => {
Expand Down