2015-12-14 15:52:53 +03:00
|
|
|
/* global key */
|
2017-08-22 10:53:26 +03:00
|
|
|
import Mixin from '@ember/object/mixin';
|
|
|
|
import {run} from '@ember/runloop';
|
|
|
|
import {typeOf} from '@ember/utils';
|
2015-12-14 15:52:53 +03:00
|
|
|
|
2022-09-23 20:17:37 +03:00
|
|
|
import * as shortcutsCache from '../utils/shortcuts';
|
2022-08-03 13:14:12 +03:00
|
|
|
|
2015-12-14 15:52:53 +03:00
|
|
|
// Configure KeyMaster to respond to all shortcuts,
|
|
|
|
// even inside of
|
|
|
|
// input, textarea, and select.
|
|
|
|
key.filter = function () {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
key.setScope('default');
|
|
|
|
/**
|
|
|
|
* Only routes can implement shortcuts.
|
|
|
|
* If you need to trigger actions on the controller,
|
|
|
|
* simply call them with `this.get('controller').send('action')`.
|
|
|
|
*
|
|
|
|
* To implement shortcuts, add this mixin to your `extend()`,
|
|
|
|
* and implement a `shortcuts` hash.
|
|
|
|
* In this hash, keys are shortcut combinations and values are route action names.
|
|
|
|
* (see [keymaster docs](https://github.com/madrobby/keymaster/blob/master/README.markdown)),
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* shortcuts: {
|
2016-07-31 22:35:19 +03:00
|
|
|
* 'ctrl+s, command+s': 'save'
|
2015-12-14 15:52:53 +03:00
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
* For more complex actions, shortcuts can instead have their value
|
|
|
|
* be an object like {action, options}
|
|
|
|
* ```javascript
|
|
|
|
* shortcuts: {
|
|
|
|
* 'ctrl+k': {action: 'markdownShortcut', options: 'createLink'}
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
* You can set the scope of your shortcut by passing a scope property.
|
|
|
|
* ```javascript
|
|
|
|
* shortcuts : {
|
|
|
|
* 'enter': {action : 'confirmModal', scope: 'modal'}
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
* If you don't specify a scope, we use a default scope called "default".
|
|
|
|
* To have all your shortcut work in all scopes, give it the scope "all".
|
|
|
|
* Find out more at the keymaster docs
|
|
|
|
*/
|
|
|
|
export default Mixin.create({
|
2018-03-19 21:39:17 +03:00
|
|
|
|
2015-12-14 15:52:53 +03:00
|
|
|
registerShortcuts() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let shortcuts = this.shortcuts;
|
2015-12-14 15:52:53 +03:00
|
|
|
|
2018-04-09 15:41:23 +03:00
|
|
|
Object.keys(shortcuts).forEach((shortcut) => {
|
|
|
|
let scope = shortcuts[shortcut].scope || 'default';
|
|
|
|
let action = shortcuts[shortcut];
|
|
|
|
let options;
|
2015-12-14 15:52:53 +03:00
|
|
|
|
2018-04-09 15:41:23 +03:00
|
|
|
if (typeOf(action) !== 'string') {
|
|
|
|
options = action.options;
|
|
|
|
action = action.action;
|
|
|
|
}
|
2015-12-14 15:52:53 +03:00
|
|
|
|
2022-08-03 13:14:12 +03:00
|
|
|
shortcutsCache.register(shortcut);
|
|
|
|
|
2018-04-09 15:41:23 +03:00
|
|
|
key(shortcut, scope, (event) => {
|
2021-06-09 18:28:19 +03:00
|
|
|
// stop things like ctrl+s from actually opening a save dialog
|
2018-04-09 15:41:23 +03:00
|
|
|
event.preventDefault();
|
|
|
|
run(this, function () {
|
|
|
|
this.send(action, options);
|
2015-12-14 15:52:53 +03:00
|
|
|
});
|
|
|
|
});
|
2018-04-09 15:41:23 +03:00
|
|
|
});
|
2015-12-14 15:52:53 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
removeShortcuts() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let shortcuts = this.shortcuts;
|
2015-12-14 15:52:53 +03:00
|
|
|
|
|
|
|
Object.keys(shortcuts).forEach((shortcut) => {
|
|
|
|
let scope = shortcuts[shortcut].scope || 'default';
|
2022-08-03 13:14:12 +03:00
|
|
|
shortcutsCache.unregister(shortcut);
|
2015-12-14 15:52:53 +03:00
|
|
|
key.unbind(shortcut, scope);
|
|
|
|
});
|
2018-03-19 21:39:17 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
willDestroy() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.removeShortcuts();
|
2015-12-14 15:52:53 +03:00
|
|
|
}
|
|
|
|
});
|