Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. fabric.Canvas.prototype.historyInit = function () {
  2. this.historyUndo = [];
  3. this.historyNextState = this.historyNext();
  4.  
  5. this.on({
  6. "object:added": this.historySaveAction,
  7. "object:removed": this.historySaveAction,
  8. "object:modified": this.historySaveAction
  9. })
  10. }
  11.  
  12. fabric.Canvas.prototype.historyNext = function () {
  13. return JSON.stringify(this.toDatalessJSON(this.extraProps));
  14. }
  15.  
  16. fabric.Canvas.prototype.historySaveAction = function () {
  17. if (this.historyProcessing)
  18. return;
  19.  
  20. const json = this.historyNextState;
  21. this.historyUndo.push(json);
  22. this.historyNextState = this.historyNext();
  23. }
  24.  
  25. fabric.Canvas.prototype.undo = function () {
  26. // The undo process will render the new states of the objects
  27. // Therefore, object:added and object:modified events will triggered again
  28. // To ignore those events, we are setting a flag.
  29. this.historyProcessing = true;
  30.  
  31. const history = this.historyUndo.pop();
  32. if (history) {
  33. this.loadFromJSON(history).renderAll();
  34. }
  35.  
  36. this.historyProcessing = false;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement