Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I interact with browser control+z/control+y for undo/redo?

Tags:

javascript

I'm creating my own editor, and I'm finally tackling the undo/redo issue that I've been dodging for weeks.

I have created the base framework for storing and traversing the history of custom actions, but I cannot seem to find good information on how to interact with the browsers history in a contentEditable area.

Looking at https://github.com/jzaefferer/undo/blob/master/undo.js, I still do not see how this is done.

I can undo/redo my custom actions, but I'm oblivious to how I can tap into the browsers default history.

Will I have to add all of the original functionality if I am to override the default control + ( z | y )?

Update: Where can I find more information about how the browser handles these undo/redo actions?

like image 275
Casey Dwayne Avatar asked Jun 16 '14 20:06

Casey Dwayne


People also ask

How do I undo CTRL Z undo?

To undo an action, press Ctrl + Z. To redo an undone action, press Ctrl + Y.

What is the control command for Undo?

To undo an action press Ctrl+Z. If you prefer your mouse, click Undo on the Quick Access Toolbar. You can press Undo (or CTRL+Z) repeatedly if you want to undo multiple steps. Note: For more information about the Quick Access Toolbar, see Customize the Quick Access Toolbar.

What is the Control key for redo?

Alternatively referred to as Control+Y and C-y, ^y, Ctrl+Y is a keyboard shortcut most often used to redo an action reversed using the undo command. For example, if you used the Ctrl+Z shortcut to undo what you thought was a mistake, but realize it wasn't, you could press Ctrl+Y to redo the previous action.

How do you implement undo and redo?

If “UNDO” string is encountered, pop the top element from Undo stack and push it to Redo stack. If “REDO” string is encountered, pop the top element of Redo stack and push it into the Undo stack. If “READ” string is encountered, print all the elements of the Undo stack in reverse order.


1 Answers

Check out the source of the contenteditable demo to figure out more about how he attached the library to the div.

$(function() {
    var stack = new Undo.Stack(),
        EditCommand = Undo.Command.extend({
            constructor: function(textarea, oldValue, newValue) {
                this.textarea = textarea;
                this.oldValue = oldValue;
                this.newValue = newValue;
            },
            execute: function() {
            },
            undo: function() {
                this.textarea.html(this.oldValue);
            },

            redo: function() {
                this.textarea.html(this.newValue);
            }
        });
    stack.changed = function() {
        stackUI();
    };

    var undo = $(".undo"),
        redo = $(".redo"),
        dirty = $(".dirty");
    function stackUI() {
        undo.attr("disabled", !stack.canUndo());
        redo.attr("disabled", !stack.canRedo());
        dirty.toggle(stack.dirty());
    }
    stackUI();

    $(document.body).delegate(".undo, .redo, .save", "click", function() {
        var what = $(this).attr("class");
        stack[what]();
        return false;
    });

    var text = $("#text"),
        startValue = text.html(),
        timer;
    $("#text").bind("keyup", function() {
        // a way too simple algorithm in place of single-character undo
        clearTimeout(timer);
        timer = setTimeout(function() {
            var newValue = text.html();
            // ignore meta key presses
            if (newValue != startValue) {
                // this could try and make a diff instead of storing snapshots
                stack.execute(new EditCommand(text, startValue, newValue));
                startValue = newValue;
            }
        }, 250);
    });

    $(".bold").click(function() {
        document.execCommand("bold", false);
        var newValue = text.html(); 
        stack.execute(new EditCommand(text, startValue, newValue));
        startValue = newValue;
    });

            // This is where he attaches the observer for undo / redo.
            // For more information: https://stackoverflow.com/questions/16006583/capturing-ctrlz-key-combination-in-javascript
    $(document).keydown(function(event) {
        if (!event.metaKey || event.keyCode != 90) {
            return;
        }
        event.preventDefault();
        if (event.shiftKey) {
            stack.canRedo() && stack.redo()
        } else {
            stack.canUndo() && stack.undo();
        }
    });
});

Capturing ctrl+z key combination in javascript

like image 96
Nijikokun Avatar answered Sep 25 '22 21:09

Nijikokun