editor.js/src/components/modules/keyboard.js
2018-01-08 20:48:15 +03:00

105 lines
No EOL
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @class Keyboard
* @classdesc Сlass to handle the keystrokes
*
* @module Keyboard
*
* @author CodeX Team (team@ifmo.su)
* @copyright CodeX Team 2017
* @license The MIT License (MIT)
* @version 2.0.0
*/
/**
* @typedef {Keyboard} Keyboard
*/
export default class Keyboard extends Module {
/**
* @constructor
*/
constructor({config}) {
super({config});
}
/**
* Handler on Editor for keyboard keys at keydown event
*
* @param {KeyboardEvent} event
*/
keyboardListener(event) {
switch(event.keyCode) {
case _.keyCodes.BACKSPACE:
_.log('Backspace key pressed');
break;
case _.keyCodes.ENTER:
_.log('Enter key pressed');
this.enterPressed(event);
break;
case _.keyCodes.DOWN:
case _.keyCodes.RIGHT:
_.log('Right/Down key pressed');
this.arrowRightAndDownPressed();
break;
case _.keyCodes.UP:
case _.keyCodes.LEFT:
_.log('Left/Up key pressed');
this.arrowLeftAndUpPressed();
break;
default:
break;
}
}
/**
* Handle pressing enter key
*
* @param {KeyboardEvent} event
*/
enterPressed(event) {
/**
* @todo check settings of "allowLinebreaks" plugin
*/
event.preventDefault();
/**
* Insert new block with data below current block
*/
this.Editor.BlockManager.split();
}
/**
* Handle right and down keyboard keys
*/
arrowRightAndDownPressed() {
this.Editor.BlockManager.navigateNext();
}
/**
* Handle left and up keyboard keys
*/
arrowLeftAndUpPressed() {
this.Editor.BlockManager.navigatePrevious();
}
}