Sidebar.Settings.History.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { UIButton, UIPanel, UIBreak, UIText } from './libs/ui.js';
  2. import { UIBoolean, UIOutliner } from './libs/ui.three.js';
  3. function SidebarSettingsHistory( editor ) {
  4. const strings = editor.strings;
  5. const signals = editor.signals;
  6. const config = editor.config;
  7. const history = editor.history;
  8. const container = new UIPanel();
  9. container.add( new UIText( strings.getKey( 'sidebar/history' ).toUpperCase() ) );
  10. //
  11. const persistent = new UIBoolean( config.getKey( 'settings/history' ), strings.getKey( 'sidebar/history/persistent' ) );
  12. persistent.setPosition( 'absolute' ).setRight( '8px' );
  13. persistent.onChange( function () {
  14. const value = this.getValue();
  15. config.setKey( 'settings/history', value );
  16. if ( value ) {
  17. alert( strings.getKey( 'prompt/history/preserve' ) );
  18. const lastUndoCmd = history.undos[ history.undos.length - 1 ];
  19. const lastUndoId = ( lastUndoCmd !== undefined ) ? lastUndoCmd.id : 0;
  20. editor.history.enableSerialization( lastUndoId );
  21. } else {
  22. signals.historyChanged.dispatch();
  23. }
  24. } );
  25. container.add( persistent );
  26. container.add( new UIBreak(), new UIBreak() );
  27. let ignoreObjectSelectedSignal = false;
  28. const outliner = new UIOutliner( editor );
  29. outliner.onChange( function () {
  30. ignoreObjectSelectedSignal = true;
  31. editor.history.goToState( parseInt( outliner.getValue() ) );
  32. ignoreObjectSelectedSignal = false;
  33. } );
  34. container.add( outliner );
  35. container.add( new UIBreak() );
  36. // Clear History
  37. const option = new UIButton( strings.getKey( 'sidebar/history/clear' ) );
  38. option.onClick( function () {
  39. if ( confirm( strings.getKey( 'prompt/history/clear' ) ) ) {
  40. editor.history.clear();
  41. }
  42. } );
  43. container.add( option );
  44. //
  45. const refreshUI = function () {
  46. const options = [];
  47. function buildOption( object ) {
  48. const option = document.createElement( 'div' );
  49. option.value = object.id;
  50. return option;
  51. }
  52. ( function addObjects( objects ) {
  53. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  54. const object = objects[ i ];
  55. const option = buildOption( object );
  56. option.innerHTML = '&nbsp;' + object.name;
  57. options.push( option );
  58. }
  59. } )( history.undos );
  60. ( function addObjects( objects ) {
  61. for ( let i = objects.length - 1; i >= 0; i -- ) {
  62. const object = objects[ i ];
  63. const option = buildOption( object );
  64. option.innerHTML = '&nbsp;' + object.name;
  65. option.style.opacity = 0.3;
  66. options.push( option );
  67. }
  68. } )( history.redos );
  69. outliner.setOptions( options );
  70. };
  71. refreshUI();
  72. // events
  73. signals.editorCleared.add( refreshUI );
  74. signals.historyChanged.add( refreshUI );
  75. signals.historyChanged.add( function ( cmd ) {
  76. if ( ignoreObjectSelectedSignal === true ) return;
  77. outliner.setValue( cmd !== undefined ? cmd.id : null );
  78. } );
  79. return container;
  80. }
  81. export { SidebarSettingsHistory };