mirror of https://github.com/artf/grapesjs.git
3 changed files with 129 additions and 0 deletions
@ -0,0 +1,116 @@ |
|||
import { defaults, isString } from 'underscore'; |
|||
|
|||
const keymaster = require('keymaster').noConflict(); |
|||
|
|||
module.exports = () => { |
|||
let config; |
|||
const configDef = {}; |
|||
const keymaps = {}; |
|||
|
|||
return { |
|||
|
|||
keymaster, |
|||
|
|||
|
|||
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|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 editor = em.getEditor(); |
|||
const keymap = { id, keys, handler }; |
|||
const pk = keymaps[id]; |
|||
pk && this.remove(id); |
|||
keymaps[id] = keymap; |
|||
keymaster(keys, (e, h) => { |
|||
// It's safer putting handlers resolution inside the callback
|
|||
handler = isString(handler) ? cmd.get(handler) : handler; |
|||
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', keymap); |
|||
}, |
|||
|
|||
|
|||
/** |
|||
* 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]; |
|||
}, |
|||
|
|||
|
|||
/** |
|||
* 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); |
|||
em && em.trigger('keymap:remove', keymap); |
|||
return keymap; |
|||
} |
|||
}, |
|||
|
|||
|
|||
}; |
|||
}; |
|||
Loading…
Reference in new issue