History.js 5.9 KB

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