Merge pull request #1278 from matrix-org/luke/fix-rte-key-bindings

Mandate ctrl/meta ONLY for a subset of key bindings
This commit is contained in:
David Baker 2017-08-08 13:51:02 +01:00 committed by GitHub
commit 08fba3bb75

View file

@ -97,23 +97,39 @@ export default class MessageComposerInput extends React.Component {
onInputStateChanged: React.PropTypes.func, onInputStateChanged: React.PropTypes.func,
}; };
static getKeyBinding(e: SyntheticKeyboardEvent): string { static getKeyBinding(ev: SyntheticKeyboardEvent): string {
// C-m => Toggles between rich text and markdown modes const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
if (e.keyCode === KeyCode.KEY_M && KeyBindingUtil.isCtrlKeyCommand(e)) { let ctrlCmdOnly;
return 'toggle-mode'; if (isMac) {
ctrlCmdOnly = ev.metaKey && !ev.altKey && !ev.ctrlKey && !ev.shiftKey;
} else {
ctrlCmdOnly = ev.ctrlKey && !ev.altKey && !ev.metaKey && !ev.shiftKey;
} }
// Allow opening of dev tools. getDefaultKeyBinding would be 'italic' for KEY_I // Restrict a subset of key bindings to ONLY having ctrl/meta* pressed and
// Likewise protect bold and underline (in case some browsers use these as // importantly NOT having alt, shift, meta/ctrl* pressed. draft-js does not
// shortcuts for things). // handle this in `getDefaultKeyBinding` so we do it ourselves here.
if ([KeyCode.KEY_B, KeyCode.KEY_I, KeyCode.KEY_U].includes(e.keyCode) && //
e.shiftKey && e.ctrlKey // * if macOS, read second option
) { const ctrlCmdCommand = {
// When null is returned, draft-js will NOT preventDefault // C-m => Toggles between rich text and markdown modes
return null; [KeyCode.KEY_M]: 'toggle-mode',
[KeyCode.KEY_B]: 'bold',
[KeyCode.KEY_I]: 'italic',
[KeyCode.KEY_U]: 'underline',
[KeyCode.KEY_J]: 'code',
[KeyCode.KEY_O]: 'split-block',
}[ev.keyCode];
if (ctrlCmdCommand) {
if (!ctrlCmdOnly) {
return null;
}
return ctrlCmdCommand;
} }
return getDefaultKeyBinding(e); // Handle keys such as return, left and right arrows etc.
return getDefaultKeyBinding(ev);
} }
static getBlockStyle(block: ContentBlock): ?string { static getBlockStyle(block: ContentBlock): ?string {