in src/component/handlers/edit/editOnKeyDown.js [86:204]
function editOnKeyDown(
editor: DraftEditor,
e: SyntheticKeyboardEvent<HTMLElement>,
): void {
const keyCode = e.which;
const editorState = editor._latestEditorState;
function callDeprecatedHandler(
handlerName:
| 'onDownArrow'
| 'onEscape'
| 'onLeftArrow'
| 'onRightArrow'
| 'onTab'
| 'onUpArrow',
): boolean {
const deprecatedHandler = editor.props[handlerName];
if (deprecatedHandler) {
deprecatedHandler(e);
return true;
} else {
return false;
}
}
switch (keyCode) {
case Keys.RETURN:
e.preventDefault();
// The top-level component may manually handle newline insertion. If
// no special handling is performed, fall through to command handling.
if (
editor.props.handleReturn &&
isEventHandled(editor.props.handleReturn(e, editorState))
) {
return;
}
break;
case Keys.ESC:
e.preventDefault();
if (callDeprecatedHandler('onEscape')) {
return;
}
break;
case Keys.TAB:
if (callDeprecatedHandler('onTab')) {
return;
}
break;
case Keys.UP:
if (callDeprecatedHandler('onUpArrow')) {
return;
}
break;
case Keys.RIGHT:
if (callDeprecatedHandler('onRightArrow')) {
return;
}
break;
case Keys.DOWN:
if (callDeprecatedHandler('onDownArrow')) {
return;
}
break;
case Keys.LEFT:
if (callDeprecatedHandler('onLeftArrow')) {
return;
}
break;
case Keys.SPACE:
// Prevent Chrome on OSX behavior where option + space scrolls.
if (isChrome && isOptionKeyCommand(e)) {
e.preventDefault();
}
}
const command = editor.props.keyBindingFn(e);
// If no command is specified, allow keydown event to continue.
if (command == null || command === '') {
if (keyCode === Keys.SPACE && isChrome && isOptionKeyCommand(e)) {
// The default keydown event has already been prevented in order to stop
// Chrome from scrolling. Insert a nbsp into the editor as OSX would for
// other browsers.
const contentState = DraftModifier.replaceText(
editorState.getCurrentContent(),
editorState.getSelection(),
'\u00a0',
);
editor.update(
EditorState.push(editorState, contentState, 'insert-characters'),
);
}
return;
}
if (command === 'undo') {
// Since undo requires some special updating behavior to keep the editor
// in sync, handle it separately.
keyCommandUndo(e, editorState, editor.update);
return;
}
// At this point, we know that we're handling a command of some kind, so
// we don't want to insert a character following the keydown.
e.preventDefault();
// Allow components higher up the tree to handle the command first.
if (
editor.props.handleKeyCommand &&
isEventHandled(
editor.props.handleKeyCommand(command, editorState, e.timeStamp),
)
) {
return;
}
const newState = onKeyCommand(command, editorState, e);
if (newState !== editorState) {
editor.update(newState);
}
}