Browse Source

Update keymaster module

pull/540/head
Artur Arseniev 9 years ago
parent
commit
97f2aadfeb
  1. 6
      src/editor/model/Editor.js
  2. 51
      src/keymap/index.js

6
src/editor/model/Editor.js

@ -167,6 +167,12 @@ module.exports = Backbone.Model.extend({
this.set('Editor', editor); this.set('Editor', editor);
}, },
getEditor() {
return this.get('Editor');
},
/** /**
* Listen for new rules * Listen for new rules
* @param {Object} collection * @param {Object} collection

51
src/keymap/index.js

@ -1,4 +1,4 @@
import { defaults } from 'underscore'; import { defaults, isString } from 'underscore';
const keymaster = require('keymaster').noConflict(); const keymaster = require('keymaster').noConflict();
module.exports = () => { module.exports = () => {
@ -36,28 +36,42 @@ module.exports = () => {
* Add new keymap * Add new keymap
* @param {string} id Keymap id * @param {string} id Keymap id
* @param {string} keys Keymap keys, eg. '⌘+z, ctrl+z' * @param {string} keys Keymap keys, eg. '⌘+z, ctrl+z'
* @param {Function} handler Keymap handler * @param {Function|string} handler Keymap handler, might be a function
* or just a command id as a string
* @example * @example
* keymaps.add('ns:my-keymap', '⌘+s, ctrl+s', () => { * keymaps.add('ns:my-keymap', '⌘+s, ctrl+s', () => {
* console.log('do stuff'); * console.log('do stuff');
* }); * });
* // or
* keymaps.add('ns:my-keymap', '⌘+s, ctrl+s', 'some-gjs-command');
*/ */
add(id, keys, handler) { add(id, keys, handler) {
const em = this.em; const em = this.em;
const cmd = em.get('Commands');
const keymap = { keys, handler }; const keymap = { keys, handler };
const pk = keymaps[id]; const pk = keymaps[id];
pk && this.remove(id); pk && this.remove(id);
keymaps[id] = keymap; keymaps[id] = keymap;
keymaster(keys, id, handler); keymaster(keys, id, () => {
em && em.trigger('keymap:add', id, keymap); handler = isString(handler) ? cmd.get(handler) : handler;
handler(em.getEditor());
em.trigger(`keymap:emit`, id, keymap);
em.trigger(`keymap:emit:${id}`, keymap);
console.log('executed', id);
});
em.trigger('keymap:add', id, keymap);
// emit // emit
//keymaster.unbind(keys, id); //keymaster.unbind(keys, id);
}, },
/** /**
* Returns the keymap by id * Get the keymap by id
* @param {string} * @param {string} id Keymap id
* @return {Object} Keymap object
* @example
* keymaps.get('ns:my-keymap');
* // -> {keys, handler};
*/ */
get(id) { get(id) {
return keymaps[id]; return keymaps[id];
@ -65,13 +79,34 @@ module.exports = () => {
/** /**
* Return the global collection, containing all the assets * Get all keymaps
* @return {Collection} * @return {Object}
*/ */
getAll() { getAll() {
return keymaps; return keymaps;
}, },
/**
* Remove the keymap by id
* @param {string} id Keymap id
* @return {Object} Removed keymap
* @example
* keymaps.remove('ns:my-keymap');
* // -> {keys, handler};
*/
remove(id) {
const em = this.em;
const keymap = this.get(id);
if (keymap) {
delete keymaps[id];
keymaster.unbind(keymap.keys, id);
em && em.trigger('keymap:remove', id, keymap);
return keymap;
}
},
}; };
}; };

Loading…
Cancel
Save