Ghost/core/client/mixins/shortcuts-route.js
Matt Enlow e10c0f20cf Implement Shortcuts in Ember
Closes #2988, #2752
Ref #1463, #2984,
 # Shortcuts via Keymaster
- Added KeyMaster to bower dependencies. KeyMaster is a minimal keyboard
  shortcuts library.
- Added `ShortcutsRouteMixin` for routes that will use shortcuts.
  Currently, only routes can have shortcuts. See the extensive comment
  at the top of `core/client/mixins/shortcuts-route.js` for a
  description of how to implement shortcuts.

 ## Other Changes
- Injected popover service into ApplicationRoute
- Created `EditorRouteBase` mixin for the `editor.new` and
  `editor.edit` routes to mixin.
- `StyleBodyMixin` now calls `this._super()` on `activate` and
  `deactivate` to play nicely with other mixins.

 ## Shortcuts and Stubs implemented
 #### Application-Wide
- `'esc':'closePopups'` shortcut **stub** to close popovers,
 modals, and notifcations

 #### Editor Shortcuts
- `'ctrl+s, command+s': 'save'` note that `command` is the
  `meta` key.
- `'ctrl+alt+p': 'publish'`
- `'ctrl+alt+z': 'toggleZenMode'`
2014-06-19 15:07:42 -06:00

62 lines
1.8 KiB
JavaScript

/* global key, console */
//Configure KeyMaster to respond to all shortcuts,
//even inside of
//input, textarea, and select.
key.filter = function () {
return true;
};
/**
* 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
* (see [keymaster docs](https://github.com/madrobby/keymaster/blob/master/README.markdown)), and values are controller action names.
* ```javascript
* shortcuts: {
* 'ctrl+s, command+s': 'save',
* 'ctrl+alt+p': 'toggleZenMode'
* }
* ```
*/
var ShortcutsRoute = Ember.Mixin.create({
registerShortcuts: function () {
var self = this,
shortcuts = this.get('shortcuts');
Ember.keys(shortcuts).forEach(function (shortcut) {
key(shortcut, function (event) {
//stop things like ctrl+s from actually opening a save dialogue
event.preventDefault();
//map the shortcut to its action
self.send(shortcuts[shortcut], event);
});
});
},
removeShortcuts: function () {
var shortcuts = this.get('shortcuts');
Ember.keys(shortcuts).forEach(function (shortcut) {
key.unbind(shortcut);
});
},
activate: function () {
this._super();
if (!this.shortcuts) {
console.error('Shortcuts not found on route');
return;
}
this.registerShortcuts();
},
deactivate: function () {
this._super();
this.removeShortcuts();
}
});
export default ShortcutsRoute;