History.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. var isScriptCmd = lastCmd &&
  17. lastCmd.updatable &&
  18. cmd.updatable &&
  19. lastCmd.script !== undefined &&
  20. lastCmd.object === cmd.object &&
  21. lastCmd.type === cmd.type &&
  22. lastCmd.script === cmd.script &&
  23. lastCmd.attributeName === cmd.attributeName;
  24. var isUpdatableCmd = lastCmd &&
  25. lastCmd.updatable &&
  26. cmd.updatable &&
  27. lastCmd.object === cmd.object &&
  28. lastCmd.type === cmd.type &&
  29. timeDifference < 500;
  30. if ( isScriptCmd || isUpdatableCmd ) {
  31. lastCmd.update( cmd );
  32. cmd = lastCmd;
  33. } else {
  34. this.undos.push( cmd );
  35. cmd.editor = this.editor;
  36. cmd.id = ++this.idCounter;
  37. }
  38. cmd.execute();
  39. this.lastCmdTime = new Date();
  40. // clearing all the redo-commands
  41. this.redos = [];
  42. this.editor.signals.historyChanged.dispatch( cmd );
  43. },
  44. undo: function () {
  45. var cmd = undefined;
  46. if ( this.undos.length > 0 ) {
  47. var cmd = this.undos.pop();
  48. if ( cmd.serialized ) {
  49. var json = cmd;
  50. cmd = new window[ json.type ](); // creates a new object of type "json.type"
  51. cmd.editor = this.editor;
  52. cmd.fromJSON( json );
  53. }
  54. }
  55. if ( cmd !== undefined ) {
  56. cmd.undo();
  57. console.log('Type: Undo ' + cmd.type );
  58. this.redos.push( cmd );
  59. this.editor.signals.historyChanged.dispatch( cmd );
  60. }
  61. return cmd;
  62. },
  63. redo: function () {
  64. var cmd = undefined;
  65. if ( this.redos.length > 0 ) {
  66. var cmd = this.redos.pop();
  67. if ( cmd.serialized ) {
  68. var json = cmd;
  69. cmd = new window[ json.type ](); // creates a new object of type "json.type"
  70. cmd.editor = this.editor;
  71. cmd.fromJSON( json );
  72. }
  73. }
  74. if ( cmd !== undefined ) {
  75. cmd.execute();
  76. console.log('Type: Redo ' + cmd.type );
  77. this.undos.push( cmd );
  78. this.editor.signals.historyChanged.dispatch( cmd );
  79. }
  80. return cmd;
  81. },
  82. toJSON: function () {
  83. var history = {};
  84. // Append Undos to History
  85. var undos = [];
  86. for ( var i = 0 ; i < this.undos.length; i++ ) {
  87. var cmd = this.undos[ i ];
  88. if ( cmd.serialized ) {
  89. undos.push( cmd ); // add without serializing
  90. } else {
  91. undos.push( cmd.toJSON() );
  92. }
  93. }
  94. history.undos = undos;
  95. // Append Redos to History
  96. var redos = [];
  97. for ( var i = 0 ; i < this.redos.length; i++ ) {
  98. var cmd = this.redos[ i ];
  99. if ( cmd.serialized ) {
  100. redos.push( cmd ); // add without serializing
  101. } else {
  102. redos.push( cmd.toJSON() );
  103. }
  104. }
  105. history.redos = redos;
  106. return history;
  107. },
  108. fromJSON: function ( json ) {
  109. if ( json === undefined ) return;
  110. for ( var i = 0; i < json.undos.length ; i++ ) {
  111. json.undos[ i ].serialized = true;
  112. this.undos.push( json.undos[ i ] );
  113. this.idCounter = json.undos[ i ].id > this.idCounter ? json.undos[ i ].id : this.idCounter; // set last used idCounter
  114. }
  115. for ( var i = 0; i < json.redos.length ; i++ ) {
  116. json.redos[ i ].serialized = true;
  117. this.redos.push( json.redos[ i ] );
  118. this.idCounter = json.redos[ i ].id > this.idCounter ? json.redos[ i ].id : this.idCounter; // set last used idCounter
  119. }
  120. this.editor.signals.historyChanged.dispatch();
  121. },
  122. clear: function () {
  123. this.undos = [];
  124. this.redos = [];
  125. this.idCounter = 0;
  126. this.editor.signals.historyChanged.dispatch();
  127. },
  128. goToState: function ( id ) {
  129. this.editor.signals.sceneGraphChanged.active = false;
  130. this.editor.signals.historyChanged.active = false;
  131. var cmd = this.undos.length > 0 ? this.undos[ this.undos.length - 1 ] : undefined; // next cmd to pop
  132. if ( cmd === undefined || id > cmd.id ) {
  133. cmd = this.redo();
  134. while ( id > cmd.id ) {
  135. cmd = this.redo();
  136. }
  137. } else {
  138. while ( true ) {
  139. cmd = this.undos[ this.undos.length - 1 ]; // next cmd to pop
  140. if ( cmd === undefined || id === cmd.id ) break;
  141. cmd = this.undo();
  142. }
  143. }
  144. this.editor.signals.sceneGraphChanged.active = true;
  145. this.editor.signals.historyChanged.active = true;
  146. this.editor.signals.sceneGraphChanged.dispatch();
  147. this.editor.signals.historyChanged.dispatch( cmd );
  148. }
  149. };