-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Sidebar.Controls to Sidebar.Settings.Shortcuts. Clean up.
- Loading branch information
Showing
6 changed files
with
162 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/** | ||
* @author TyLindberg / https://github.com/TyLindberg | ||
*/ | ||
|
||
Sidebar.Settings.Shortcuts = function ( editor ) { | ||
|
||
const 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' ]; | ||
|
||
for ( var i = 0; i < shortcuts.length; i ++ ) { | ||
|
||
let name = shortcuts[ i ]; | ||
|
||
let configName = 'settings/shortcuts/' + name; | ||
let shortcutRow = new UI.Row(); | ||
|
||
let 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( 'focus', 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( name ).setTextTransform( 'capitalize' ).setWidth( '90px' ) ); | ||
shortcutRow.add( shortcutInput ); | ||
|
||
container.add( shortcutRow ); | ||
|
||
} | ||
|
||
document.addEventListener( 'keydown', function ( event ) { | ||
|
||
switch ( event.key.toLowerCase() ) { | ||
|
||
case 'Backspace': | ||
|
||
event.preventDefault(); // prevent browser back | ||
|
||
break; | ||
|
||
case 'Delete': | ||
|
||
var object = editor.selected; | ||
|
||
if ( object === null ) return; | ||
if ( confirm( 'Delete ' + object.name + '?' ) === false ) return; | ||
|
||
var parent = object.parent; | ||
if ( parent !== null ) editor.execute( new RemoveObjectCommand( 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; | ||
|
||
} | ||
|
||
}, false ); | ||
|
||
return container; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters