samclarke / SCEditor

A lightweight HTML and BBCode WYSIWYG editor
http://www.sceditor.com/
Other
657 stars 187 forks source link

How to clear undo/redo history #900

Open jagaxa opened 2 years ago

jagaxa commented 2 years ago

I'm currently using the undo plugin. Is there any way to clear the undo history after I empty the editor using .val("")?

If not, how can I implement something like

    function clear() {
        undoStates = []
    }
    sceditor.plugins.undo.clear = clear;
}(sceditor));
samclarke commented 2 years ago

This should really be part of the undo plugin so I'll leave this issue open.

For now, adding a method to the editor in the undo plugin init should work. Something like:

editor.clearUndo = function () {
  undoStates = [];
  redoPosition = 0;
  storeState();
};

Full file: https://gist.github.com/samclarke/4a53460692a0d8ecc55eadca861dbd57

Can then be called with:

instance.clearUndo();
huanacaraz commented 1 year ago

I suggest also to add support to undo/redo commands (the translations already contains those keywords even if not used..) I did it this way:

  1. modify the toolbar icons image - add undo and redo image (to the end...)
  2. modify the css - add icon definitions for the buttons: .sceditor-button-undo div{background-position:0 -659px} .sceditor-button-redo div{background-position:0 -675px}
  3. add this line to main plugin function: (just after "var base = this;") var utils = sceditor.utils;
  4. added this code to the plugin init method: ` var commands = this.commands;

        commands.undo = utils.extend(commands.undo || {}, {
            state: function () {
                return redoPosition < undoStates.length - 1 ? 0 : -1;
            },
            exec: function () {
                base.undo();
            },
            tooltip: 'Undo'
        });
    
        commands.redo = utils.extend(commands.redo || {}, {
            state: function () {
                return redoPosition > 0 ? 0 : -1;
            },
            exec: function () {
                base.redo();
            },
            tooltip: 'Redo'
        });
    
        editor.resetUndo = function() {
            undoStates = [];
            redoPosition = 0;
            storeState();
        }

    `