Sidebar.Settings.Shortcuts.js 3.5 KB

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