Sidebar.History.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 { UIPanel, UIBreak, UIText } from './libs/ui.js';
  6. import { UIBoolean, UIOutliner } from './libs/ui.three.js';
  7. var SidebarHistory = function ( editor ) {
  8. var strings = editor.strings;
  9. var signals = editor.signals;
  10. var config = editor.config;
  11. var history = editor.history;
  12. var container = new UIPanel();
  13. container.add( new UIText( strings.getKey( 'sidebar/history' ).toUpperCase() ) );
  14. //
  15. var persistent = new UIBoolean( config.getKey( 'settings/history' ), strings.getKey( 'sidebar/history/persistent' ) );
  16. persistent.setPosition( 'absolute' ).setRight( '8px' );
  17. persistent.onChange( function () {
  18. var value = this.getValue();
  19. config.setKey( 'settings/history', value );
  20. if ( value ) {
  21. alert( 'The history will be preserved across sessions.\nThis can have an impact on performance when working with textures.' );
  22. var lastUndoCmd = history.undos[ history.undos.length - 1 ];
  23. var lastUndoId = ( lastUndoCmd !== undefined ) ? lastUndoCmd.id : 0;
  24. editor.history.enableSerialization( lastUndoId );
  25. } else {
  26. signals.historyChanged.dispatch();
  27. }
  28. } );
  29. container.add( persistent );
  30. container.add( new UIBreak(), new UIBreak() );
  31. var ignoreObjectSelectedSignal = false;
  32. var outliner = new UIOutliner( editor );
  33. outliner.onChange( function () {
  34. ignoreObjectSelectedSignal = true;
  35. editor.history.goToState( parseInt( outliner.getValue() ) );
  36. ignoreObjectSelectedSignal = false;
  37. } );
  38. container.add( outliner );
  39. //
  40. var refreshUI = function () {
  41. var options = [];
  42. function buildOption( object ) {
  43. var option = document.createElement( 'div' );
  44. option.value = object.id;
  45. return option;
  46. }
  47. ( function addObjects( objects ) {
  48. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  49. var object = objects[ i ];
  50. var option = buildOption( object );
  51. option.innerHTML = '&nbsp;' + object.name;
  52. options.push( option );
  53. }
  54. } )( history.undos );
  55. ( function addObjects( objects ) {
  56. for ( var i = objects.length - 1; i >= 0; i -- ) {
  57. var object = objects[ i ];
  58. var option = buildOption( object );
  59. option.innerHTML = '&nbsp;' + object.name;
  60. option.style.opacity = 0.3;
  61. options.push( option );
  62. }
  63. } )( history.redos );
  64. outliner.setOptions( options );
  65. };
  66. refreshUI();
  67. // events
  68. signals.editorCleared.add( refreshUI );
  69. signals.historyChanged.add( refreshUI );
  70. signals.historyChanged.add( function ( cmd ) {
  71. if ( ignoreObjectSelectedSignal === true ) return;
  72. outliner.setValue( cmd !== undefined ? cmd.id : null );
  73. } );
  74. return container;
  75. };
  76. export { SidebarHistory };