From a61b8937d0308bbbe23031bbb934d28e87c7e606 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 9 Nov 2017 01:09:20 +0100 Subject: [PATCH 1/4] Start with keymap module --- src/keymap/index.js | 77 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/keymap/index.js diff --git a/src/keymap/index.js b/src/keymap/index.js new file mode 100644 index 000000000..16edc3e79 --- /dev/null +++ b/src/keymap/index.js @@ -0,0 +1,77 @@ +import { defaults } from 'underscore'; +const keymaster = require('keymaster').noConflict(); + +module.exports = () => { + let config; + const configDef = {}; + const keymaps = {}; + + return { + + name: 'Keymaps', + + + /** + * Get module configurations + * @return {Object} Configuration object + */ + getConfig() { + return config; + }, + + + /** + * Initialize module + * @param {Object} config Configurations + * @private + */ + init(opts = {}) { + config = opts; + defaults(config, configDef); + this.em = config.em; + return this; + }, + + /** + * Add new keymap + * @param {string} id Keymap id + * @param {string} keys Keymap keys, eg. '⌘+z, ctrl+z' + * @param {Function} handler Keymap handler + * @example + * keymaps.add('ns:my-keymap', '⌘+s, ctrl+s', () => { + * console.log('do stuff'); + * }); + */ + add(id, keys, handler) { + const em = this.em; + 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); + // emit + //keymaster.unbind(keys, id); + }, + + + /** + * Returns the keymap by id + * @param {string} + */ + get(id) { + return keymaps[id]; + }, + + + /** + * Return the global collection, containing all the assets + * @return {Collection} + */ + getAll() { + return keymaps; + }, + + + }; +}; From 97f2aadfeb479858bdc257995f1665377ba18958 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 9 Nov 2017 01:44:54 +0100 Subject: [PATCH 2/4] Update keymaster module --- src/editor/model/Editor.js | 6 +++++ src/keymap/index.js | 51 ++++++++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 8 deletions(-) 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; + } + }, + + }; }; From 41bf910efa418332691f36179eb0b2248381c404 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 9 Nov 2017 02:43:22 +0100 Subject: [PATCH 3/4] Fix how the keymap handler is resolved --- src/editor/index.js | 6 ++++++ src/editor/model/Editor.js | 1 + src/{keymap => keymaps}/index.js | 25 ++++++++++++++----------- 3 files changed, 21 insertions(+), 11 deletions(-) rename src/{keymap => keymaps}/index.js (78%) diff --git a/src/editor/index.js b/src/editor/index.js index a8635e8b2..619ab4781 100644 --- a/src/editor/index.js +++ b/src/editor/index.js @@ -172,6 +172,12 @@ module.exports = config => { */ Commands: em.get('Commands'), + /** + * @property {Keymaps} + * @private + */ + Keymaps: em.get('Keymaps'), + /** * @property {Modal} * @private diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index 938b6ed1b..d8433cb00 100644 --- a/src/editor/model/Editor.js +++ b/src/editor/model/Editor.js @@ -2,6 +2,7 @@ import { isUndefined, defaults } from 'underscore'; const deps = [ require('utils'), + require('keymaps'), require('storage_manager'), require('device_manager'), require('parser'), diff --git a/src/keymap/index.js b/src/keymaps/index.js similarity index 78% rename from src/keymap/index.js rename to src/keymaps/index.js index b58284d0f..062995641 100644 --- a/src/keymap/index.js +++ b/src/keymaps/index.js @@ -5,9 +5,12 @@ module.exports = () => { let config; const configDef = {}; const keymaps = {}; + console.log('called'); return { + keymaster, + name: 'Keymaps', @@ -48,20 +51,20 @@ module.exports = () => { add(id, keys, handler) { const em = this.em; const cmd = em.get('Commands'); - const keymap = { keys, handler }; + const editor = em.getEditor(); + const keymap = { id, keys, handler }; const pk = keymaps[id]; pk && this.remove(id); keymaps[id] = keymap; - keymaster(keys, id, () => { + keymaster(keys, (e, h) => { + // It's safer putting handlers resolution inside the callback 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); + typeof handler == 'object' ? handler.run(editor) : handler(editor); + const args = [id, h.shortcut, e]; + em.trigger('keymap:emit', ...args); + em.trigger(`keymap:emit:${id}`, ...args); }); - em.trigger('keymap:add', id, keymap); - // emit - //keymaster.unbind(keys, id); + em.trigger('keymap:add', keymap); }, @@ -101,8 +104,8 @@ module.exports = () => { if (keymap) { delete keymaps[id]; - keymaster.unbind(keymap.keys, id); - em && em.trigger('keymap:remove', id, keymap); + keymaster.unbind(keymap.keys); + em && em.trigger('keymap:remove', keymap); return keymap; } }, From 02804791b28a14b9c7def249ef249cc42cd454c7 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 9 Nov 2017 02:44:20 +0100 Subject: [PATCH 4/4] Remove logs --- src/keymaps/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/keymaps/index.js b/src/keymaps/index.js index 062995641..79f4a8431 100644 --- a/src/keymaps/index.js +++ b/src/keymaps/index.js @@ -1,16 +1,17 @@ import { defaults, isString } from 'underscore'; + const keymaster = require('keymaster').noConflict(); module.exports = () => { let config; const configDef = {}; const keymaps = {}; - console.log('called'); return { keymaster, + name: 'Keymaps',