History.js 6.1 KB

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