History.js 4.3 KB

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