History.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * edited by dforrer on 20.07.15.
  4. */
  5. History = function ( editor ) {
  6. this.editor = editor;
  7. this.undos = [];
  8. this.redos = [];
  9. this.lastCmdTime = new Date();
  10. this.idCounter = 0;
  11. };
  12. History.prototype = {
  13. execute: function ( cmd ) {
  14. var lastCmd = this.undos[ this.undos.length - 1 ];
  15. var timeDifference = new Date().getTime() - this.lastCmdTime.getTime();
  16. if ( lastCmd != null &&
  17. lastCmd.updatable &&
  18. lastCmd.object === cmd.object &&
  19. lastCmd.type == cmd.type &&
  20. timeDifference < 500 ) {
  21. // command objects have the same type and are less than 0.5 second apart
  22. lastCmd.update( cmd );
  23. cmd = lastCmd;
  24. } else {
  25. this.undos.push( cmd );
  26. cmd.editor = this.editor;
  27. cmd.id = ++this.idCounter;
  28. }
  29. cmd.execute();
  30. this.lastCmdTime = new Date();
  31. // clearing all the redo-commands
  32. this.redos = [];
  33. this.editor.signals.historyChanged.dispatch( cmd );
  34. },
  35. undo: function () {
  36. var cmd = undefined;
  37. if ( this.undos.length > 0 ) {
  38. var cmd = this.undos.pop();
  39. if ( cmd.serialized ) {
  40. var json = cmd;
  41. cmd = new window[ json.type ](); // creates a new object of type "json.type"
  42. cmd.editor = this.editor;
  43. cmd.fromJSON( json );
  44. }
  45. }
  46. if ( cmd !== undefined ) {
  47. cmd.undo();
  48. console.log('Type: Undo ' + cmd.type );
  49. this.redos.push( cmd );
  50. this.editor.signals.historyChanged.dispatch( cmd );
  51. }
  52. return cmd;
  53. },
  54. redo: function () {
  55. var cmd = undefined;
  56. if ( this.redos.length > 0 ) {
  57. var cmd = this.redos.pop();
  58. if ( cmd.serialized ) {
  59. var json = cmd;
  60. cmd = new window[ json.type ](); // creates a new object of type "json.type"
  61. cmd.editor = this.editor;
  62. cmd.fromJSON( json );
  63. }
  64. }
  65. if ( cmd !== undefined ) {
  66. cmd.execute();
  67. console.log('Type: Redo ' + cmd.type );
  68. this.undos.push( cmd );
  69. this.editor.signals.historyChanged.dispatch( cmd );
  70. }
  71. return cmd;
  72. },
  73. toJSON: function () {
  74. var history = {};
  75. // Append Undos to History
  76. var undos = [];
  77. for ( var i = 0 ; i < this.undos.length; i++ ) {
  78. var cmd = this.undos[ i ];
  79. if ( cmd.serialized ) {
  80. undos.push( cmd ); // add without serializing
  81. } else {
  82. undos.push( cmd.toJSON() );
  83. }
  84. }
  85. history.undos = undos;
  86. // Append Redos to History
  87. var redos = [];
  88. for ( var i = 0 ; i < this.redos.length; i++ ) {
  89. var cmd = this.redos[ i ];
  90. if ( cmd.serialized ) {
  91. redos.push( cmd ); // add without serializing
  92. } else {
  93. redos.push( cmd.toJSON() );
  94. }
  95. }
  96. history.redos = redos;
  97. return history;
  98. },
  99. fromJSON: function ( json ) {
  100. if ( json === undefined ) return;
  101. for ( var i = 0; i < json.undos.length ; i++ ) {
  102. json.undos[ i ].serialized = true;
  103. this.undos.push( json.undos[ i ] );
  104. this.idCounter = json.undos[ i ].id > this.idCounter ? json.undos[ i ].id : this.idCounter; // set last used idCounter
  105. }
  106. for ( var i = 0; i < json.redos.length ; i++ ) {
  107. json.redos[ i ].serialized = true;
  108. this.redos.push( json.redos[ i ] );
  109. this.idCounter = json.redos[ i ].id > this.idCounter ? json.redos[ i ].id : this.idCounter; // set last used idCounter
  110. }
  111. this.editor.signals.historyChanged.dispatch();
  112. },
  113. clear: function () {
  114. this.undos = [];
  115. this.redos = [];
  116. this.idCounter = 0;
  117. this.editor.signals.historyChanged.dispatch();
  118. },
  119. goToState: function ( id ) {
  120. this.editor.signals.sceneGraphChanged.active = false;
  121. this.editor.signals.historyChanged.active = false;
  122. var cmd = this.undos.length > 0 ? this.undos[ this.undos.length - 1 ] : undefined; // next cmd to pop
  123. if ( cmd === undefined || id > cmd.id ) {
  124. cmd = this.redo();
  125. while ( id > cmd.id ) {
  126. cmd = this.redo();
  127. }
  128. } else {
  129. while ( true ) {
  130. cmd = this.undos[ this.undos.length - 1 ]; // next cmd to pop
  131. if ( cmd === undefined || id === cmd.id ) break;
  132. cmd = this.undo();
  133. }
  134. }
  135. this.editor.signals.sceneGraphChanged.active = true;
  136. this.editor.signals.historyChanged.active = true;
  137. this.editor.signals.sceneGraphChanged.dispatch();
  138. this.editor.signals.historyChanged.dispatch( cmd );
  139. }
  140. };
粤ICP备19079148号