Apply Undo Redo on elements that can dragable (drag and drop)

前端 未结 4 961
予麋鹿
予麋鹿 2020-12-22 11:01

I want to implement functionality on the svgElements that can be dragged with javascript how could I do this...

I have implemented this with mouse up

When m

4条回答
  •  醉酒成梦
    2020-12-22 11:04

    There are some issues with the above codes. I attempted to use those and found out that I had to hit undo twice initially before it started going down the array.

    So say I have done[0] done[1] done[2]. done[2] was just saved into the array. If I hit undo, it returns that. You dont want that. It is replacing what is already there. But hitting undo again, THEN you get your previous code.

    Mine, as I have a drag and drop editor with different modes of edits. Drag and Drop Elements. Edit Elements HTML/Pictures. Sorting elements.

    $("#neoContentContainer") contains all editor html. And you can call editor_add on clicks, mousedowns ect... seeing it is a function you can easily call.

    function editor_add(){
        undo.push($("#neoContentContainer").html());
        update_buttons();
    }
    function editor_undo(){
        var item = undo.pop();
        // prevent undo/redo from undoing to the same HTML currently shown. 
        if(item == $("#neoContentContainer").html()){
            redo.push(item);
            item = undo.pop();
        }
        if(item){
            redo.push(item);
            $("#neoContentContainer").html(item);
        }
        update_buttons();
    }
    function editor_redo(){
        var item = redo.pop();
        if(item == $("#neoContentContainer").html()){
            undo.push(item);
            item = redo.pop();
        }
        if(item){
            undo.push(item);
            $("#neoContentContainer").html(item);
        }
        update_buttons();
    }
    function update_buttons(){
        if(undo.length == 0){
            $('button[data-id="undo"]').attr('disabled',true);
        } else {
            $('button[data-id="undo"]').attr('disabled',false);
        }
    
        if(redo.length == 0){
            $('button[data-id="redo"]').attr('disabled',true);
        } else {
            $('button[data-id="redo"]').attr('disabled',false);
        }
    }
    

    https://jsfiddle.net/s1L6vv5y/

    Not perfect, but get the jist of it. (Still wondering when we can get ONE LINE LINE BREAKS!!!! DEVS AT STACKOVERFLOW!! :)

    So when my page loads, I run: editor_add(); Because when they do something, it needs to undo to something!

    Now every time they drop something, sort things I run editor_add(); Took me all day, now realizing how simple it was this works very well for me.

    So with this one, which is good....

    var history = function() {
        this.done = this.reverted = [];
        var self = this;
    
        this.add = function(item) {
            self.done.push(item);
        };
    
        this.undo = function() {
            if(done.length >= 3){
                var undo_item = self.done.pop();
                self.reverted.push(undo_item);
            }
    
            var item = self.done.pop(); 
            if (item) {
                self.reverted.push(item);
            }
    
            return item;
        };
    
        this.redo = function() {
            if(reverted.length >= 3){
                var revert_item = self.reverted.pop();
                self.done.push(revert_item);
            }
            var item = self.reverted.pop();
            if (item) {
                self.done.push(item);
            }
    
            return item;
        };
    };
    

    You do not want to clear the redos until you ran through the array.

    (Guess logging in before editing helps!)

提交回复
热议问题