History.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. var History = function ( editor ) {
  6. this.editor = editor;
  7. this.undos = [];
  8. this.redos = [];
  9. this.lastCmdTime = new Date();
  10. this.idCounter = 0;
  11. this.historyDisabled = false;
  12. this.config = editor.config;
  13. // signals
  14. var scope = this;
  15. this.editor.signals.startPlayer.add( function () {
  16. scope.historyDisabled = true;
  17. } );
  18. this.editor.signals.stopPlayer.add( function () {
  19. scope.historyDisabled = false;
  20. } );
  21. };
  22. History.prototype = {
  23. execute: function ( cmd, optionalName ) {
  24. var lastCmd = this.undos[ this.undos.length - 1 ];
  25. var timeDifference = new Date().getTime() - this.lastCmdTime.getTime();
  26. var isUpdatableCmd = lastCmd &&
  27. lastCmd.updatable &&
  28. cmd.updatable &&
  29. lastCmd.object === cmd.object &&
  30. lastCmd.type === cmd.type &&
  31. lastCmd.script === cmd.script &&
  32. lastCmd.attributeName === cmd.attributeName;
  33. if ( isUpdatableCmd && cmd.type === "SetScriptValueCommand" ) {
  34. // When the cmd.type is "SetScriptValueCommand" the timeDifference is ignored
  35. lastCmd.update( cmd );
  36. cmd = lastCmd;
  37. } else if ( isUpdatableCmd && timeDifference < 500 ) {
  38. lastCmd.update( cmd );
  39. cmd = lastCmd;
  40. } else {
  41. // the command is not updatable and is added as a new part of the history
  42. this.undos.push( cmd );
  43. cmd.id = ++ this.idCounter;
  44. }
  45. cmd.name = ( optionalName !== undefined ) ? optionalName : cmd.name;
  46. cmd.execute();
  47. cmd.inMemory = true;
  48. if ( this.config.getKey( 'settings/history' ) ) {
  49. cmd.json = cmd.toJSON(); // serialize the cmd immediately after execution and append the json to the cmd
  50. }
  51. this.lastCmdTime = new Date();
  52. // clearing all the redo-commands
  53. this.redos = [];
  54. this.editor.signals.historyChanged.dispatch( cmd );
  55. },
  56. undo: function () {
  57. if ( this.historyDisabled ) {
  58. alert( "Undo/Redo disabled while scene is playing." );
  59. return;
  60. }
  61. var cmd = undefined;
  62. if ( this.undos.length > 0 ) {
  63. cmd = this.undos.pop();
  64. if ( cmd.inMemory === false ) {
  65. cmd.fromJSON( cmd.json );
  66. }
  67. }
  68. if ( cmd !== undefined ) {
  69. cmd.undo();
  70. this.redos.push( cmd );
  71. this.editor.signals.historyChanged.dispatch( cmd );
  72. }
  73. return cmd;
  74. },
  75. redo: function () {
  76. if ( this.historyDisabled ) {
  77. alert( "Undo/Redo disabled while scene is playing." );
  78. return;
  79. }
  80. var cmd = undefined;
  81. if ( this.redos.length > 0 ) {
  82. cmd = this.redos.pop();
  83. if ( cmd.inMemory === false ) {
  84. cmd.fromJSON( cmd.json );
  85. }
  86. }
  87. if ( cmd !== undefined ) {
  88. cmd.execute();
  89. this.undos.push( cmd );
  90. this.editor.signals.historyChanged.dispatch( cmd );
  91. }
  92. return cmd;
  93. },
  94. toJSON: function () {
  95. var history = {};
  96. history.undos = [];
  97. history.redos = [];
  98. if ( ! this.config.getKey( 'settings/history' ) ) {
  99. return history;
  100. }
  101. // Append Undos to History
  102. for ( var i = 0; i < this.undos.length; i ++ ) {
  103. if ( this.undos[ i ].hasOwnProperty( "json" ) ) {
  104. history.undos.push( this.undos[ i ].json );
  105. }
  106. }
  107. // Append Redos to History
  108. for ( var i = 0; i < this.redos.length; i ++ ) {
  109. if ( this.redos[ i ].hasOwnProperty( "json" ) ) {
  110. history.redos.push( this.redos[ i ].json );
  111. }
  112. }
  113. return history;
  114. },
  115. fromJSON: function ( json ) {
  116. if ( json === undefined ) return;
  117. for ( var i = 0; i < json.undos.length; i ++ ) {
  118. var cmdJSON = json.undos[ i ];
  119. var cmd = new window[ cmdJSON.type ](); // creates a new object of type "json.type"
  120. cmd.json = cmdJSON;
  121. cmd.id = cmdJSON.id;
  122. cmd.name = cmdJSON.name;
  123. this.undos.push( cmd );
  124. this.idCounter = ( cmdJSON.id > this.idCounter ) ? cmdJSON.id : this.idCounter; // set last used idCounter
  125. }
  126. for ( var i = 0; i < json.redos.length; i ++ ) {
  127. var cmdJSON = json.redos[ i ];
  128. var cmd = new window[ cmdJSON.type ](); // creates a new object of type "json.type"
  129. cmd.json = cmdJSON;
  130. cmd.id = cmdJSON.id;
  131. cmd.name = cmdJSON.name;
  132. this.redos.push( cmd );
  133. this.idCounter = ( cmdJSON.id > this.idCounter ) ? cmdJSON.id : this.idCounter; // set last used idCounter
  134. }
  135. // Select the last executed undo-command
  136. this.editor.signals.historyChanged.dispatch( this.undos[ this.undos.length - 1 ] );
  137. },
  138. clear: function () {
  139. this.undos = [];
  140. this.redos = [];
  141. this.idCounter = 0;
  142. this.editor.signals.historyChanged.dispatch();
  143. },
  144. goToState: function ( id ) {
  145. if ( this.historyDisabled ) {
  146. alert( "Undo/Redo disabled while scene is playing." );
  147. return;
  148. }
  149. this.editor.signals.sceneGraphChanged.active = false;
  150. this.editor.signals.historyChanged.active = false;
  151. var cmd = this.undos.length > 0 ? this.undos[ this.undos.length - 1 ] : undefined; // next cmd to pop
  152. if ( cmd === undefined || id > cmd.id ) {
  153. cmd = this.redo();
  154. while ( cmd !== undefined && id > cmd.id ) {
  155. cmd = this.redo();
  156. }
  157. } else {
  158. while ( true ) {
  159. cmd = this.undos[ this.undos.length - 1 ]; // next cmd to pop
  160. if ( cmd === undefined || id === cmd.id ) break;
  161. this.undo();
  162. }
  163. }
  164. this.editor.signals.sceneGraphChanged.active = true;
  165. this.editor.signals.historyChanged.active = true;
  166. this.editor.signals.sceneGraphChanged.dispatch();
  167. this.editor.signals.historyChanged.dispatch( cmd );
  168. },
  169. enableSerialization: function ( id ) {
  170. /**
  171. * because there might be commands in this.undos and this.redos
  172. * which have not been serialized with .toJSON() we go back
  173. * to the oldest command and redo one command after the other
  174. * while also calling .toJSON() on them.
  175. */
  176. this.goToState( - 1 );
  177. this.editor.signals.sceneGraphChanged.active = false;
  178. this.editor.signals.historyChanged.active = false;
  179. var cmd = this.redo();
  180. while ( cmd !== undefined ) {
  181. if ( ! cmd.hasOwnProperty( "json" ) ) {
  182. cmd.json = cmd.toJSON();
  183. }
  184. cmd = this.redo();
  185. }
  186. this.editor.signals.sceneGraphChanged.active = true;
  187. this.editor.signals.historyChanged.active = true;
  188. this.goToState( id );
  189. }
  190. };
  191. export { History };