History.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var History = function ( editor ) {
  5. this.array = [];
  6. this.arrayLength = -1;
  7. this.current = -1;
  8. this.isRecording = true;
  9. //
  10. var scope = this;
  11. var signals = editor.signals;
  12. signals.objectAdded.add( function ( object ) {
  13. if ( scope.isRecording === false ) return;
  14. scope.add(
  15. function () {
  16. editor.removeObject( object );
  17. editor.select( null );
  18. },
  19. function () {
  20. editor.addObject( object );
  21. editor.select( object );
  22. }
  23. );
  24. } );
  25. };
  26. History.prototype = {
  27. add: function ( undo, redo ) {
  28. this.current ++;
  29. this.array[ this.current ] = { undo: undo, redo: redo };
  30. this.arrayLength = this.current;
  31. },
  32. undo: function () {
  33. if ( this.current < 0 ) return;
  34. this.isRecording = false;
  35. this.array[ this.current -- ].undo();
  36. this.isRecording = true;
  37. },
  38. redo: function () {
  39. if ( this.current === this.arrayLength ) return;
  40. this.isRecording = false;
  41. this.array[ ++ this.current ].redo();
  42. this.isRecording = true;
  43. },
  44. clear: function () {
  45. this.array = [];
  46. this.arrayLength = -1;
  47. this.current = -1;
  48. }
  49. };