Sidebar.History.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. Sidebar.History = function ( editor ) {
  6. var signals = editor.signals;
  7. var config = editor.config;
  8. var history = editor.history;
  9. var container = new UI.Panel();
  10. container.add( new UI.Text( 'HISTORY' ) );
  11. //
  12. var persistent = new UI.THREE.Boolean( config.getKey( 'settings/history' ), 'persistent' );
  13. persistent.setPosition( 'absolute' ).setRight( '8px' );
  14. persistent.onChange( function () {
  15. var value = this.getValue();
  16. config.setKey( 'settings/history', value );
  17. if ( value ) {
  18. alert( 'The history will be preserved across sessions.\nThis can have an impact on performance when working with textures.' );
  19. var lastUndoCmd = history.undos[ history.undos.length - 1 ];
  20. var lastUndoId = ( lastUndoCmd !== undefined ) ? lastUndoCmd.id : 0;
  21. editor.history.enableSerialization( lastUndoId );
  22. } else {
  23. signals.historyChanged.dispatch();
  24. }
  25. } );
  26. container.add( persistent );
  27. container.add( new UI.Break(), new UI.Break() );
  28. var ignoreObjectSelectedSignal = false;
  29. var outliner = new UI.Outliner( editor );
  30. outliner.onChange( function () {
  31. ignoreObjectSelectedSignal = true;
  32. editor.history.goToState( parseInt( outliner.getValue() ) );
  33. ignoreObjectSelectedSignal = false;
  34. } );
  35. container.add( outliner );
  36. //
  37. var refreshUI = function () {
  38. var options = [];
  39. var enumerator = 1;
  40. ( function addObjects( objects, pad ) {
  41. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  42. var object = objects[ i ];
  43. var html = pad + "<span style='color: #0000cc '>" + enumerator ++ + ". Undo: " + object.name + "</span>";
  44. options.push( { value: object.id, html: html } );
  45. }
  46. } )( history.undos, '&nbsp;' );
  47. ( function addObjects( objects, pad ) {
  48. for ( var i = objects.length - 1; i >= 0; i -- ) {
  49. var object = objects[ i ];
  50. var html = pad + "<span style='color: #71544e'>" + enumerator ++ + ". Redo: " + object.name + "</span>";
  51. options.push( { value: object.id, html: html } );
  52. }
  53. } )( history.redos, '&nbsp;' );
  54. outliner.setOptions( options );
  55. };
  56. refreshUI();
  57. // events
  58. signals.editorCleared.add( refreshUI );
  59. signals.historyChanged.add( refreshUI );
  60. signals.historyChanged.add( function ( cmd ) {
  61. outliner.setValue( cmd !== undefined ? cmd.id : null );
  62. } );
  63. return container;
  64. };