Sidebar.Settings.Shortcuts.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @author TyLindberg / https://github.com/TyLindberg
  3. */
  4. Sidebar.Settings.Shortcuts = function ( editor ) {
  5. const 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 UI.Div();
  12. container.add( new UI.Break() );
  13. var shortcuts = [ 'translate', 'rotate', 'scale', 'undo' ];
  14. for ( var i = 0; i < shortcuts.length; i ++ ) {
  15. let name = shortcuts[ i ];
  16. let configName = 'settings/shortcuts/' + name;
  17. let shortcutRow = new UI.Row();
  18. let shortcutInput = new UI.Input().setWidth( '150px' ).setFontSize( '12px' );
  19. shortcutInput.setTextTransform( 'lowercase' );
  20. shortcutInput.onChange( function () {
  21. var value = shortcutInput.getValue().toLowerCase();
  22. if ( isValidKeyBinding( value ) ) {
  23. config.setKey( configName, value );
  24. }
  25. } );
  26. // Automatically highlight when selecting an input field
  27. shortcutInput.dom.addEventListener( 'click', function () {
  28. shortcutInput.dom.select();
  29. } );
  30. // If the value of the input field is invalid, revert the input field
  31. // to contain the key binding stored in config
  32. shortcutInput.dom.addEventListener( 'blur', function () {
  33. if ( ! isValidKeyBinding( shortcutInput.getValue() ) ) {
  34. shortcutInput.setValue( config.getKey( configName ) );
  35. }
  36. } );
  37. // If a valid key binding character is entered, blur the input field
  38. shortcutInput.dom.addEventListener( 'keyup', function ( event ) {
  39. if ( isValidKeyBinding( event.key ) ) {
  40. shortcutInput.dom.blur();
  41. }
  42. } );
  43. if ( config.getKey( configName ) !== undefined ) {
  44. shortcutInput.setValue( config.getKey( configName ) );
  45. }
  46. shortcutInput.dom.maxLength = 1;
  47. shortcutRow.add( new UI.Text( name ).setTextTransform( 'capitalize' ).setWidth( '90px' ) );
  48. shortcutRow.add( shortcutInput );
  49. container.add( shortcutRow );
  50. }
  51. document.addEventListener( 'keydown', function ( event ) {
  52. switch ( event.key.toLowerCase() ) {
  53. case 'Backspace':
  54. event.preventDefault(); // prevent browser back
  55. break;
  56. case 'Delete':
  57. var object = editor.selected;
  58. if ( object === null ) return;
  59. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  60. var parent = object.parent;
  61. if ( parent !== null ) editor.execute( new RemoveObjectCommand( object ) );
  62. break;
  63. case config.getKey( 'settings/shortcuts/translate' ):
  64. signals.transformModeChanged.dispatch( 'translate' );
  65. break;
  66. case config.getKey( 'settings/shortcuts/rotate' ):
  67. signals.transformModeChanged.dispatch( 'rotate' );
  68. break;
  69. case config.getKey( 'settings/shortcuts/scale' ):
  70. signals.transformModeChanged.dispatch( 'scale' );
  71. break;
  72. case config.getKey( 'settings/shortcuts/undo' ):
  73. if ( IS_MAC ? event.metaKey : event.ctrlKey ) {
  74. event.preventDefault(); // Prevent browser specific hotkeys
  75. if ( event.shiftKey ) {
  76. editor.redo();
  77. } else {
  78. editor.undo();
  79. }
  80. }
  81. break;
  82. }
  83. }, false );
  84. return container;
  85. };