editor.js/src/components/modules/shortcuts.ts
Murod Khaydarov 8d6ac74b57
Issue-616: Handle backspace, cmd+X, cmd+C and enter on RectangleSelection (#617)
* cut-n-copy multiple selection with rectangle

* use pop instead of shift

* enter and backspace behavior with rectangeSelection

* bump version and minify bundle

* optimize code
2019-02-25 17:19:30 +03:00

68 lines
1.4 KiB
TypeScript

import Shortcut from '@codexteam/shortcuts';
/**
* ShortcutData interface
* Each shortcut must have name and handler
* `name` is a shortcut, like 'CMD+K', 'CMD+B' etc
* `handler` is a callback
*/
export interface ShortcutData {
/**
* Shortcut name
* Ex. CMD+I, CMD+B ....
*/
name: string;
/**
* Shortcut handler
*/
handler(event): void;
}
/**
* Contains keyboard and mouse events binded on each Block by Block Manager
*/
import Module from '../__module';
/**
* @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
*/
public add(shortcut: ShortcutData): void {
const { UI } = this.Editor;
const newShortcut = new Shortcut({
name: shortcut.name,
on: document, // UI.nodes.redactor
callback: shortcut.handler,
});
this.registeredShortcuts.push(newShortcut);
}
/**
* Remove shortcut
* @param {ShortcutData} shortcut
*/
public remove(shortcut: string): void {
const index = this.registeredShortcuts.findIndex((shc) => shc.name === shortcut);
this.registeredShortcuts[index].remove();
this.registeredShortcuts.splice(index, 1);
}
}