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

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

    I found a better structure without counter, index shift or limit handling problems. Simply 2 stack for "done" and "reverted" action that are balancing.

    var history = function() {
        this.done = this.reverted = [];
        var self = this;
    
        this.add = function(item) {
            self.done.push(item);
    
            // delete anything forward
            self.reverted = [];
        };
    
        this.undo = function() {
            var item = self.done.pop();
    
            if (item) {
                self.reverted.push(item);
            }
    
            return item;
        };
    
        this.redo = function() {
            var item = self.reverted.pop();
    
            if (item) {
                self.done.push(item);
            }
    
            return item;
        };
    };
    

提交回复
热议问题