Sidebar.Settings.Shortcuts.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { UIPanel, UIText, UIRow, UIInput } from './libs/ui.js';
  2. import { MultiCmdsCommand } from './commands/MultiCmdsCommand.js';
  3. import { RemoveObjectCommand } from './commands/RemoveObjectCommand.js';
  4. function SidebarSettingsShortcuts( editor ) {
  5. const strings = editor.strings;
  6. const IS_MAC = navigator.platform.toUpperCase().indexOf( 'MAC' ) >= 0;
  7. function isValidKeyBinding( key ) {
  8. return key.match( /^[A-Za-z0-9]$/i ); // Can't use z currently due to undo/redo
  9. }
  10. const config = editor.config;
  11. const signals = editor.signals;
  12. const container = new UIPanel();
  13. const headerRow = new UIRow();
  14. headerRow.add( new UIText( strings.getKey( 'sidebar/settings/shortcuts' ).toUpperCase() ) );
  15. container.add( headerRow );
  16. const shortcuts = [ 'translate', 'rotate', 'scale', 'undo', 'focus', 'perspective', 'orthographic' ];
  17. function createShortcutInput( name ) {
  18. const configName = 'settings/shortcuts/' + name;
  19. const shortcutRow = new UIRow();
  20. const shortcutInput = new UIInput().setWidth( '15px' ).setFontSize( '12px' );
  21. shortcutInput.setTextAlign( 'center' );
  22. shortcutInput.setTextTransform( 'lowercase' );
  23. shortcutInput.onChange( function () {
  24. const value = shortcutInput.getValue().toLowerCase();
  25. if ( isValidKeyBinding( value ) ) {
  26. config.setKey( configName, value );
  27. }
  28. } );
  29. // Automatically highlight when selecting an input field
  30. shortcutInput.dom.addEventListener( 'click', function () {
  31. shortcutInput.dom.select();
  32. } );
  33. // If the value of the input field is invalid, revert the input field
  34. // to contain the key binding stored in config
  35. shortcutInput.dom.addEventListener( 'blur', function () {
  36. if ( ! isValidKeyBinding( shortcutInput.getValue() ) ) {
  37. shortcutInput.setValue( config.getKey( configName ) );
  38. }
  39. } );
  40. // If a valid key binding character is entered, blur the input field
  41. shortcutInput.dom.addEventListener( 'keyup', function ( event ) {
  42. if ( isValidKeyBinding( event.key ) ) {
  43. shortcutInput.dom.blur();
  44. }
  45. } );
  46. if ( config.getKey( configName ) !== undefined ) {
  47. shortcutInput.setValue( config.getKey( configName ) );
  48. }
  49. shortcutInput.dom.maxLength = 1;
  50. shortcutRow.add( new UIText( strings.getKey( 'sidebar/settings/shortcuts/' + name ) ).setTextTransform( 'capitalize' ).setClass( 'Label' ) );
  51. shortcutRow.add( shortcutInput );
  52. container.add( shortcutRow );
  53. }
  54. for ( let i = 0; i < shortcuts.length; i ++ ) {
  55. createShortcutInput( shortcuts[ i ] );
  56. }
  57. document.addEventListener( 'keydown', function ( event ) {
  58. switch ( event.key.toLowerCase() ) {
  59. case 'backspace':
  60. event.preventDefault(); // prevent browser back
  61. // fall-through
  62. case 'delete':
  63. const object = editor.selected;
  64. if ( object === null || object.parent === null ) return;
  65. if ( object.isSpotLight || object.isDirectionalLight ) {
  66. editor.execute( new MultiCmdsCommand( editor, [
  67. new RemoveObjectCommand( editor, object ),
  68. new RemoveObjectCommand( editor, object.target )
  69. ] ) );
  70. } else {
  71. editor.execute( new RemoveObjectCommand( editor, object ) );
  72. }
  73. break;
  74. case config.getKey( 'settings/shortcuts/translate' ):
  75. signals.transformModeChanged.dispatch( 'translate' );
  76. break;
  77. case config.getKey( 'settings/shortcuts/rotate' ):
  78. signals.transformModeChanged.dispatch( 'rotate' );
  79. break;
  80. case config.getKey( 'settings/shortcuts/scale' ):
  81. signals.transformModeChanged.dispatch( 'scale' );
  82. break;
  83. case config.getKey( 'settings/shortcuts/undo' ):
  84. if ( IS_MAC ? event.metaKey : event.ctrlKey ) {
  85. event.preventDefault(); // Prevent browser specific hotkeys
  86. if ( event.shiftKey ) {
  87. editor.redo();
  88. } else {
  89. editor.undo();
  90. }
  91. }
  92. break;
  93. case config.getKey( 'settings/shortcuts/focus' ):
  94. if ( editor.selected !== null ) {
  95. editor.focus( editor.selected );
  96. }
  97. break;
  98. case config.getKey( 'settings/shortcuts/perspective' ):
  99. editor.setCameraType( 'perspective' );
  100. break;
  101. case config.getKey( 'settings/shortcuts/orthographic' ):
  102. editor.setCameraType( 'orthographic' );
  103. break;
  104. }
  105. } );
  106. return container;
  107. }
  108. export { SidebarSettingsShortcuts };
粤ICP备19079148号