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

前端 未结 4 970
予麋鹿
予麋鹿 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:12

    If you're asking how to implement undo/redo functionality in general, it's fairly simple: you have an array of actions and a counter. You push new elements onto the array when actions occur and step backwards when people hit undo.

    Very basic implementation:

    var history = {
        stack   : [],
        counter : -1,
        add     : function(item){
            this.stack[++this.counter] = item;
            this.doSomethingWith(item);
    
            // delete anything forward of the counter
            this.stack.splice(this.counter+1);
        },
        undo : function(){
            this.doSomethingWith(this.stack[--this.counter]);
        },
        redo : function(){
            this.doSomethingWith(this.stack[++this.counter]);
        },
        doSomethingWith : function(item){
            // show item
        }
    };
    

    Note that there should be basic error checking to see that counter doesn't go beyond bounds and that you may want to pass 'undo' info into doSomethingWith in the case of an undo, but all that is app specific.

提交回复
热议问题