1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/**
* @author TyLindberg / https://github.com/TyLindberg
*/
Sidebar.Settings.Shortcuts = function ( editor ) {
var strings = editor.strings;
var IS_MAC = navigator.platform.toUpperCase().indexOf( 'MAC' ) >= 0;
function isValidKeyBinding( key ) {
return key.match( /^[A-Za-z0-9]$/i ); // Can't use z currently due to undo/redo
}
var config = editor.config;
var signals = editor.signals;
var container = new UI.Div();
container.add( new UI.Break() );
var shortcuts = [ 'translate', 'rotate', 'scale', 'undo', 'focus' ];
function createShortcutInput( name ) {
var configName = 'settings/shortcuts/' + name;
var shortcutRow = new UI.Row();
var shortcutInput = new UI.Input().setWidth( '150px' ).setFontSize( '12px' );
shortcutInput.setTextTransform( 'lowercase' );
shortcutInput.onChange( function () {
var value = shortcutInput.getValue().toLowerCase();
if ( isValidKeyBinding( value ) ) {
config.setKey( configName, value );
}
} );
// Automatically highlight when selecting an input field
shortcutInput.dom.addEventListener( 'click', function () {
shortcutInput.dom.select();
} );
// If the value of the input field is invalid, revert the input field
// to contain the key binding stored in config
shortcutInput.dom.addEventListener( 'blur', function () {
if ( ! isValidKeyBinding( shortcutInput.getValue() ) ) {
shortcutInput.setValue( config.getKey( configName ) );
}
} );
// If a valid key binding character is entered, blur the input field
shortcutInput.dom.addEventListener( 'keyup', function ( event ) {
if ( isValidKeyBinding( event.key ) ) {
shortcutInput.dom.blur();
}
} );
if ( config.getKey( configName ) !== undefined ) {
shortcutInput.setValue( config.getKey( configName ) );
}
shortcutInput.dom.maxLength = 1;
shortcutRow.add( new UI.Text( strings.getKey( 'sidebar/settings/shortcuts/' + name ) ).setTextTransform( 'capitalize' ).setWidth( '90px' ) );
shortcutRow.add( shortcutInput );
container.add( shortcutRow );
}
for ( var i = 0; i < shortcuts.length; i ++ ) {
createShortcutInput( shortcuts[ i ] );
}
document.addEventListener( 'keydown', function ( event ) {
switch ( event.key.toLowerCase() ) {
case 'backspace':
event.preventDefault(); // prevent browser back
// fall-through
case 'delete':
var object = editor.selected;
if ( object === null ) return;
var parent = object.parent;
if ( parent !== null ) editor.execute( new RemoveObjectCommand( editor, object ) );
break;
case config.getKey( 'settings/shortcuts/translate' ):
signals.transformModeChanged.dispatch( 'translate' );
break;
case config.getKey( 'settings/shortcuts/rotate' ):
signals.transformModeChanged.dispatch( 'rotate' );
break;
case config.getKey( 'settings/shortcuts/scale' ):
signals.transformModeChanged.dispatch( 'scale' );
break;
case config.getKey( 'settings/shortcuts/undo' ):
if ( IS_MAC ? event.metaKey : event.ctrlKey ) {
event.preventDefault(); // Prevent browser specific hotkeys
if ( event.shiftKey ) {
editor.redo();
} else {
editor.undo();
}
}
break;
case config.getKey( 'settings/shortcuts/focus' ):
if ( editor.selected !== null ) {
editor.focus( editor.selected );
}
break;
}
}, false );
return container;
};
|