diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index 225704b90..938b6ed1b 100644 --- a/src/editor/model/Editor.js +++ b/src/editor/model/Editor.js @@ -167,6 +167,12 @@ module.exports = Backbone.Model.extend({ this.set('Editor', editor); }, + + getEditor() { + return this.get('Editor'); + }, + + /** * Listen for new rules * @param {Object} collection diff --git a/src/keymap/index.js b/src/keymap/index.js index 16edc3e79..b58284d0f 100644 --- a/src/keymap/index.js +++ b/src/keymap/index.js @@ -1,4 +1,4 @@ -import { defaults } from 'underscore'; +import { defaults, isString } from 'underscore'; const keymaster = require('keymaster').noConflict(); module.exports = () => { @@ -36,28 +36,42 @@ module.exports = () => { * Add new keymap * @param {string} id Keymap id * @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 * keymaps.add('ns:my-keymap', '⌘+s, ctrl+s', () => { * console.log('do stuff'); * }); + * // or + * keymaps.add('ns:my-keymap', '⌘+s, ctrl+s', 'some-gjs-command'); */ add(id, keys, handler) { const em = this.em; + const cmd = em.get('Commands'); const keymap = { keys, handler }; const pk = keymaps[id]; pk && this.remove(id); keymaps[id] = keymap; - keymaster(keys, id, handler); - em && em.trigger('keymap:add', id, keymap); + keymaster(keys, id, () => { + 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 //keymaster.unbind(keys, id); }, /** - * Returns the keymap by id - * @param {string} + * Get the keymap by id + * @param {string} id Keymap id + * @return {Object} Keymap object + * @example + * keymaps.get('ns:my-keymap'); + * // -> {keys, handler}; */ get(id) { return keymaps[id]; @@ -65,13 +79,34 @@ module.exports = () => { /** - * Return the global collection, containing all the assets - * @return {Collection} + * Get all keymaps + * @return {Object} */ getAll() { 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; + } + }, + + }; };