From 2ca9d8680284f546df2358e297b7c3af766e6791 Mon Sep 17 00:00:00 2001 From: Dave Hughes Date: Wed, 19 Jul 2017 22:37:07 -0700 Subject: [PATCH] Allow for custom History actions --- src/js/History.js | 12 ++++-- test/test_history_actions.html | 78 ++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 test/test_history_actions.html diff --git a/src/js/History.js b/src/js/History.js index 1a8b0bf66..66b234a9b 100644 --- a/src/js/History.js +++ b/src/js/History.js @@ -150,9 +150,13 @@ History.prototype.onChange = function () {}; /** * Add a new action to the history - * @param {String} action The executed action. Available actions: "editField", + * @param {String or Object} action + * The executed action. Built-in actions can be + * referenced by name: "editField", * "editValue", "changeType", "appendNode", - * "removeNode", "duplicateNode", "moveNode" + * "removeNode", "duplicateNode", "moveNode". + * Alternatively, an action Object can be provided + * with undo and redo functions. * @param {Object} params Object containing parameters describing the change. * The parameters in params depend on the action (for * example for "editValue" the Node, old value, and new @@ -210,7 +214,7 @@ History.prototype.undo = function () { if (this.canUndo()) { var obj = this.history[this.index]; if (obj) { - var action = this.actions[obj.action]; + var action = obj.action instanceof Object ? obj.action : this.actions[obj.action]; if (action && action.undo) { action.undo(obj.params); if (obj.params.oldSelection) { @@ -237,7 +241,7 @@ History.prototype.redo = function () { var obj = this.history[this.index]; if (obj) { - var action = this.actions[obj.action]; + var action = obj.action instanceof Object ? obj.action : this.actions[obj.action]; if (action && action.redo) { action.redo(obj.params); if (obj.params.newSelection) { diff --git a/test/test_history_actions.html b/test/test_history_actions.html new file mode 100644 index 000000000..a4d846113 --- /dev/null +++ b/test/test_history_actions.html @@ -0,0 +1,78 @@ + + + + + + + + + + + + +

+ Switch editor mode using the mode box. + Note that the mode can be changed programmatically as well using the method + editor.setMode(mode), try it in the console of your browser. +

+ +
+ + + +