From d37dd3bf780153241660be07c59ff85330d58f1d Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 24 Oct 2025 12:36:23 +0400 Subject: [PATCH] Type keymap events --- packages/core/src/keymaps/index.ts | 33 ++++++++++++------------------ packages/core/src/keymaps/types.ts | 28 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 packages/core/src/keymaps/types.ts diff --git a/packages/core/src/keymaps/index.ts b/packages/core/src/keymaps/index.ts index 015462dda..7f35f444a 100644 --- a/packages/core/src/keymaps/index.ts +++ b/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 { keymaster: any = keymaster; keymaps: Record; + events = KeymapsEvents; constructor(em: EditorModel) { super(em, 'Keymaps', defConfig()); @@ -106,7 +99,7 @@ export default class KeymapsModule extends Module {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 { ... }); + */ + 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;