Sidebar.History.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. */
  4. Sidebar.History = function ( editor ) {
  5. var signals = editor.signals;
  6. var history = editor.history;
  7. var container = new UI.CollapsiblePanel();
  8. container.setCollapsed( editor.config.getKey( 'ui/sidebar/history/collapsed' ) );
  9. container.onCollapsedChange( function ( boolean ) {
  10. editor.config.setKey( 'ui/sidebar/history/collapsed', boolean );
  11. } );
  12. container.addStatic( new UI.Text( 'HISTORY' ) );
  13. // Actions
  14. var objectActions = new UI.Select().setPosition( 'absolute' ).setRight( '8px' ).setFontSize( '11px' );
  15. objectActions.setOptions( {
  16. 'Actions': 'Actions',
  17. 'Serialization': 'Serialize History?'
  18. } );
  19. objectActions.onClick( function ( event ) {
  20. event.stopPropagation(); // Avoid panel collapsing
  21. } );
  22. objectActions.onChange( function ( event ) {
  23. var currentValue = history.serializationEnabled ? 'yes' : 'no';
  24. var response;
  25. if ( ( response = prompt( 'Should the history be preserved across a browser refresh? (yes or no)', currentValue ) ) === null ) {
  26. this.setValue( 'Actions' );
  27. return;
  28. }
  29. if ( response.toLowerCase() === 'yes' ) {
  30. alert( 'The history will be preserved across a browser refresh.' );
  31. var lastUndoCmd = history.undos[ history.undos.length - 1 ];
  32. var lastUndoId = ( lastUndoCmd !== undefined ) ? lastUndoCmd.id : 0;
  33. editor.history.enableSerialization( lastUndoId );
  34. } else {
  35. alert( 'The history will NOT be preserved across a browser refresh.' );
  36. editor.history.disableSerialization();
  37. }
  38. this.setValue( 'Actions' );
  39. } );
  40. container.addStatic( objectActions );
  41. container.add( new UI.Break() );
  42. var ignoreObjectSelectedSignal = false;
  43. var outliner = new UI.Outliner( editor );
  44. outliner.onChange( function () {
  45. ignoreObjectSelectedSignal = true;
  46. editor.history.goToState( parseInt( outliner.getValue() ) );
  47. ignoreObjectSelectedSignal = false;
  48. } );
  49. outliner.onDblClick( function () {
  50. //editor.focusById( parseInt( outliner.getValue() ) );
  51. } );
  52. container.add( outliner );
  53. //
  54. var refreshUI = function () {
  55. var options = [];
  56. var enumerator = 1;
  57. ( function addObjects( objects, pad ) {
  58. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  59. var object = objects[ i ];
  60. var html = pad + "<span style='color: #0000cc '>" + enumerator ++ + ". Undo: " + object.name + "</span>";
  61. options.push( { value: object.id, html: html } );
  62. }
  63. } )( history.undos, '&nbsp;' );
  64. ( function addObjects( objects, pad ) {
  65. for ( var i = objects.length - 1; i >= 0; i -- ) {
  66. var object = objects[ i ];
  67. var html = pad + "<span style='color: #71544e'>" + enumerator ++ + ". Redo: " + object.name + "</span>";
  68. options.push( { value: object.id, html: html } );
  69. }
  70. } )( history.redos, '&nbsp;' );
  71. outliner.setOptions( options );
  72. };
  73. refreshUI();
  74. // events
  75. signals.editorCleared.add( refreshUI );
  76. signals.historyChanged.add( refreshUI );
  77. signals.historyChanged.add( function ( cmd ) {
  78. outliner.setValue( cmd !== undefined ? cmd.id : null );
  79. } );
  80. return container;
  81. };