mirror of
https://github.com/codex-team/editor.js
synced 2024-05-06 16:36:47 +02:00
63eeec0f3b
Co-authored-by: George Berezhnoy <gohabereg@users.noreply.github.com> Co-authored-by: Georgy Berezhnoy <gohabereg@gmail.com> Co-authored-by: tasuku-s <tasuku@freemind.co.jp> Co-authored-by: Athul Anil Kumar <athul7744@outlook.com> Co-authored-by: Taly <vitalik7tv@yandex.ru> Co-authored-by: flaming-cl <51183663+flaming-cl@users.noreply.github.com> Co-authored-by: Nguyen Ngoc Son <sonnn.se@gmail.com> Co-authored-by: Sisir Das K <37764463+sis-dk@users.noreply.github.com> Co-authored-by: Sisir <sisir@hellosivi.com>
71 lines
1.4 KiB
TypeScript
71 lines
1.4 KiB
TypeScript
import Shortcut from '@codexteam/shortcuts';
|
|
|
|
/**
|
|
* Contains keyboard and mouse events binded on each Block by Block Manager
|
|
*/
|
|
import Module from '../__module';
|
|
|
|
/**
|
|
* ShortcutData interface
|
|
* Each shortcut must have name and handler
|
|
* `name` is a shortcut, like 'CMD+K', 'CMD+B' etc
|
|
* `handler` is a callback
|
|
*
|
|
* @interface ShortcutData
|
|
*/
|
|
export interface ShortcutData {
|
|
|
|
/**
|
|
* Shortcut name
|
|
* Ex. CMD+I, CMD+B ....
|
|
*/
|
|
name: string;
|
|
|
|
/**
|
|
* Shortcut handler
|
|
*/
|
|
handler(event): void;
|
|
}
|
|
|
|
/**
|
|
* @class Shortcut
|
|
* @classdesc Allows to register new shortcut
|
|
*
|
|
* Internal Shortcuts Module
|
|
*/
|
|
export default class Shortcuts extends Module {
|
|
/**
|
|
* All registered shortcuts
|
|
*
|
|
* @type {Shortcut[]}
|
|
*/
|
|
private registeredShortcuts: Shortcut[] = [];
|
|
|
|
/**
|
|
* Register shortcut
|
|
*
|
|
* @param {ShortcutData} shortcut - shortcut options
|
|
*/
|
|
public add(shortcut: ShortcutData): void {
|
|
const newShortcut = new Shortcut({
|
|
name: shortcut.name,
|
|
on: document, // UI.nodes.redactor
|
|
callback: shortcut.handler,
|
|
});
|
|
|
|
this.registeredShortcuts.push(newShortcut);
|
|
}
|
|
|
|
/**
|
|
* Remove shortcut
|
|
*
|
|
* @param {string} shortcut - shortcut name
|
|
*/
|
|
public remove(shortcut: string): void {
|
|
const index = this.registeredShortcuts.findIndex((shc) => shc.name === shortcut);
|
|
|
|
this.registeredShortcuts[index].remove();
|
|
this.registeredShortcuts.splice(index, 1);
|
|
}
|
|
}
|