Browse Source

Type keymap events

data-source-schema
Artur Arseniev 5 months ago
parent
commit
d37dd3bf78
  1. 33
      packages/core/src/keymaps/index.ts
  2. 28
      packages/core/src/keymaps/types.ts

33
packages/core/src/keymaps/index.ts

@ -15,22 +15,13 @@
* })
* ```
*
* Once the editor is instantiated you can use its API and listen to its events. Before using these methods, you should get the module from the instance.
* Once the editor is instantiated you can use its API. Before using these methods you should get the module from the instance.
*
* ```js
* // Listen to events
* editor.on('keymap:add', () => { ... });
*
* // Use the API
* const keymaps = editor.Keymaps;
* keymaps.add(...);
* ```
*
* ## Available Events
* * `keymap:add` - New keymap added. The new keyamp object is passed as an argument
* * `keymap:remove` - Keymap removed. The removed keyamp object is passed as an argument
* * `keymap:emit` - Some keymap emitted, in arguments you get keymapId, shortcutUsed, Event
* * `keymap:emit:{keymapId}` - `keymapId` emitted, in arguments you get keymapId, shortcutUsed, Event
* {REPLACE_EVENTS}
*
* ## Methods
* * [getConfig](#getconfig)
@ -44,19 +35,21 @@
*/
import { isFunction, isString } from 'underscore';
import { hasWin } from '../utils/mixins';
import keymaster from '../utils/keymaster';
import { Module } from '../abstract';
import EditorModel from '../editor/model/Editor';
import keymaster from '../utils/keymaster';
import { hasWin } from '../utils/mixins';
import defConfig, { Keymap, KeymapOptions, KeymapsConfig } from './config';
import { KeymapsEvents } from './types';
export type KeymapEvent = 'keymap:add' | 'keymap:remove' | 'keymap:emit' | `keymap:emit:${string}`;
export type KeymapEvent = `${KeymapsEvents}`;
hasWin() && keymaster.init(window);
export default class KeymapsModule extends Module<KeymapsConfig & { name?: string }> {
keymaster: any = keymaster;
keymaps: Record<string, Keymap>;
events = KeymapsEvents;
constructor(em: EditorModel) {
super(em, 'Keymaps', defConfig());
@ -106,7 +99,7 @@ export default class KeymapsModule extends Module<KeymapsConfig & { name?: strin
* })
*/
add(id: Keymap['id'], keys: Keymap['keys'], handler: Keymap['handler'], opts: KeymapOptions = {}) {
const { em } = this;
const { em, events } = this;
const cmd = em.Commands;
const editor = em.getEditor();
const canvas = em.Canvas;
@ -125,13 +118,13 @@ export default class KeymapsModule extends Module<KeymapsConfig & { name?: strin
opts.prevent && canvas.getCanvasView()?.preventDefault(e);
isFunction(handlerRes) ? handlerRes(editor, 0, opt) : cmd.runCommand(handlerRes, opt);
const args = [id, h.shortcut, e];
em.trigger('keymap:emit', ...args);
em.trigger(`keymap:emit:${id}`, ...args);
em.trigger(events.emit, ...args);
em.trigger(`${events.emitId}${id}`, ...args);
}
},
undefined,
);
em.trigger('keymap:add', keymap);
em.trigger(events.add, keymap);
return keymap;
}
@ -167,7 +160,7 @@ export default class KeymapsModule extends Module<KeymapsConfig & { name?: strin
* // -> {keys, handler};
*/
remove(id: string) {
const { em } = this;
const { em, events } = this;
const keymap = this.get(id);
if (keymap) {
@ -176,7 +169,7 @@ export default class KeymapsModule extends Module<KeymapsConfig & { name?: strin
// @ts-ignore
keymaster.unbind(k.trim());
});
em?.trigger('keymap:remove', keymap);
em?.trigger(events.remove, keymap);
return keymap;
}
}

28
packages/core/src/keymaps/types.ts

@ -0,0 +1,28 @@
/**{START_EVENTS}*/
export enum KeymapsEvents {
/**
* @event `keymap:add` New keymap added. The new keymap object is passed as an argument to the callback.
* @example
* editor.on('keymap:add', (keymap) => { ... });
*/
add = 'keymap:add',
/**
* @event `keymap:remove` Keymap removed. The removed keymap object is passed as an argument to the callback.
* @example
* editor.on('keymap:remove', (keymap) => { ... });
*/
remove = 'keymap:remove',
/**
* @event `keymap:emit` Some keymap emitted. The keymapId, shortcutUsed, and Event are passed as arguments to the callback.
* @example
* editor.on('keymap:emit', (keymapId, shortcutUsed, event) => { ... });
*/
emit = 'keymap:emit',
emitId = 'keymap:emit:',
}
/**{END_EVENTS}*/
// need this to avoid the TS documentation generator to break
export default KeymapsEvents;
Loading…
Cancel
Save