| 46 | hasWin() && keymaster.init(window); |
| 47 | |
| 48 | export default class KeymapsModule extends Module<KeymapsConfig & { name?: string }> { |
| 49 | keymaster: any = keymaster; |
| 50 | keymaps: Record<string, Keymap>; |
| 51 | events = KeymapsEvents; |
| 52 | |
| 53 | constructor(em: EditorModel) { |
| 54 | super(em, 'Keymaps', defConfig()); |
| 55 | this.keymaps = {}; |
| 56 | } |
| 57 | |
| 58 | onLoad() { |
| 59 | if (this.em.isHeadless) return; |
| 60 | const defKeys = this.config.defaults; |
| 61 | |
| 62 | for (let id in defKeys) { |
| 63 | const value = defKeys[id]; |
| 64 | this.add(id, value.keys, value.handler, value.opts || {}); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get configuration object |
| 70 | * @name getConfig |
| 71 | * @function |
| 72 | * @return {Object} |
| 73 | */ |
| 74 | |
| 75 | /** |
| 76 | * Add new keymap |
| 77 | * @param {string} id Keymap id |
| 78 | * @param {string} keys Keymap keys, eg. `ctrl+a`, `⌘+z, ctrl+z` |
| 79 | * @param {Function|string} handler Keymap handler, might be a function |
| 80 | * @param {Object} [opts={}] Options |
| 81 | * @param {Boolean} [opts.force=false] Force the handler to be executed. |
| 82 | * @param {Boolean} [opts.prevent=false] Prevent default of the original triggered event. |
| 83 | * @returns {Object} Added keymap |
| 84 | * @example |
| 85 | * // 'ns' is just a custom namespace |
| 86 | * keymaps.add('ns:my-keymap', '⌘+j, ⌘+u, ctrl+j, alt+u', editor => { |
| 87 | * console.log('do stuff'); |
| 88 | * }); |
| 89 | * // or |
| 90 | * keymaps.add('ns:my-keymap', '⌘+s, ctrl+s', 'some-gjs-command', { |
| 91 | * // Prevent the default browser action |
| 92 | * prevent: true, |
| 93 | * }); |
| 94 | * |
| 95 | * // listen to events |
| 96 | * editor.on('keymap:emit', (id, shortcut, event) => { |
| 97 | * // ... |
| 98 | * }) |
| 99 | */ |
| 100 | add(id: Keymap['id'], keys: Keymap['keys'], handler: Keymap['handler'], opts: KeymapOptions = {}) { |
| 101 | const { em, events } = this; |
| 102 | const cmd = em.Commands; |
| 103 | const editor = em.getEditor(); |
| 104 | const canvas = em.Canvas; |
| 105 | const keymap: Keymap = { id, keys, handler }; |
nothing calls this directly
no outgoing calls
no test coverage detected