Use Keyboard Key variables instead of hardcoded strings

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2019-10-09 19:59:11 +01:00
parent 84e71297ef
commit 58317b1608
9 changed files with 51 additions and 30 deletions

View file

@ -63,15 +63,29 @@ export const KeyCode = {
export const Key = { export const Key = {
HOME: "Home", HOME: "Home",
End: "End", END: "End",
PAGE_UP: "PageUp", PAGE_UP: "PageUp",
PAGE_DOWN: "PageDown", PAGE_DOWN: "PageDown",
BACKSPACE: "Backspace", BACKSPACE: "Backspace",
ARROW_UP: "ArrowUp",
ARROW_DOWN: "ArrowDown",
TAB: "Tab",
ESCAPE: "Escape",
ENTER: "Enter",
ALT: "Alt",
CONTROL: "Control",
META: "Meta",
SHIFT: "Shift",
LESS_THAN: "<",
GREATER_THAN: ">",
BACKTICK: "`", BACKTICK: "`",
SPACE: " ", SPACE: " ",
ENTER: "Enter", B: "b",
I: "i",
K: "k", K: "k",
Y: "y",
Z: "z",
}; };
export function isOnlyCtrlOrCmdKeyEvent(ev) { export function isOnlyCtrlOrCmdKeyEvent(ev) {

View file

@ -351,7 +351,7 @@ const LoggedInView = createReactClass({
let handled = false; let handled = false;
const ctrlCmdOnly = isOnlyCtrlOrCmdKeyEvent(ev); const ctrlCmdOnly = isOnlyCtrlOrCmdKeyEvent(ev);
const hasModifier = ev.altKey || ev.ctrlKey || ev.metaKey || ev.shiftKey || const hasModifier = ev.altKey || ev.ctrlKey || ev.metaKey || ev.shiftKey ||
ev.key === "Alt" || ev.key === "Control" || ev.key === "Meta" || ev.key === "Shift"; ev.key === Key.ALT || ev.key === Key.CONTROL || ev.key === Key.META || ev.key === Key.SHIFT;
switch (ev.key) { switch (ev.key) {
case Key.PAGE_UP: case Key.PAGE_UP:

View file

@ -22,6 +22,7 @@ import SdkConfig from '../../../SdkConfig';
import withValidation from '../elements/Validation'; import withValidation from '../elements/Validation';
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import MatrixClientPeg from '../../../MatrixClientPeg'; import MatrixClientPeg from '../../../MatrixClientPeg';
import {Key} from "../../../Keyboard";
export default createReactClass({ export default createReactClass({
displayName: 'CreateRoomDialog', displayName: 'CreateRoomDialog',
@ -74,7 +75,7 @@ export default createReactClass({
}, },
_onKeyDown: function(event) { _onKeyDown: function(event) {
if (event.key === "Enter") { if (event.key === Key.ENTER) {
this.onOk(); this.onOk();
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();

View file

@ -23,6 +23,7 @@ import Modal from '../../../../Modal';
import { MatrixClient } from 'matrix-js-sdk'; import { MatrixClient } from 'matrix-js-sdk';
import { _t } from '../../../../languageHandler'; import { _t } from '../../../../languageHandler';
import {Key} from "../../../../Keyboard";
const RESTORE_TYPE_PASSPHRASE = 0; const RESTORE_TYPE_PASSPHRASE = 0;
const RESTORE_TYPE_RECOVERYKEY = 1; const RESTORE_TYPE_RECOVERYKEY = 1;
@ -136,13 +137,13 @@ export default createReactClass({
}, },
_onPassPhraseKeyPress: function(e) { _onPassPhraseKeyPress: function(e) {
if (e.key === "Enter") { if (e.key === Key.ENTER) {
this._onPassPhraseNext(); this._onPassPhraseNext();
} }
}, },
_onRecoveryKeyKeyPress: function(e) { _onRecoveryKeyKeyPress: function(e) {
if (e.key === "Enter" && this.state.recoveryKeyValid) { if (e.key === Key.ENTER && this.state.recoveryKeyValid) {
this._onRecoveryKeyNext(); this._onRecoveryKeyNext();
} }
}, },

View file

@ -18,6 +18,7 @@ limitations under the License.
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import createReactClass from 'create-react-class'; import createReactClass from 'create-react-class';
import {Key} from "../../../Keyboard";
module.exports = createReactClass({ module.exports = createReactClass({
displayName: 'EditableText', displayName: 'EditableText',
@ -133,7 +134,7 @@ module.exports = createReactClass({
this.showPlaceholder(false); this.showPlaceholder(false);
} }
if (ev.key === "Enter") { if (ev.key === Key.ENTER) {
ev.stopPropagation(); ev.stopPropagation();
ev.preventDefault(); ev.preventDefault();
} }
@ -150,9 +151,9 @@ module.exports = createReactClass({
this.value = ev.target.textContent; this.value = ev.target.textContent;
} }
if (ev.key === "Enter") { if (ev.key === Key.ENTER) {
this.onFinish(ev); this.onFinish(ev);
} else if (ev.key === "Escape") { } else if (ev.key === Key.ESCAPE) {
this.cancelEdit(); this.cancelEdit();
} }
@ -184,7 +185,7 @@ module.exports = createReactClass({
onFinish: function(ev, shouldSubmit) { onFinish: function(ev, shouldSubmit) {
const self = this; const self = this;
const submit = (ev.key === "Enter") || shouldSubmit; const submit = (ev.key === Key.ENTER) || shouldSubmit;
this.setState({ this.setState({
phase: this.Phases.Display, phase: this.Phases.Display,
}, () => { }, () => {

View file

@ -20,6 +20,7 @@ import createReactClass from 'create-react-class';
import * as Roles from '../../../Roles'; import * as Roles from '../../../Roles';
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import Field from "./Field"; import Field from "./Field";
import {Key} from "../../../Keyboard";
module.exports = createReactClass({ module.exports = createReactClass({
displayName: 'PowerSelector', displayName: 'PowerSelector',
@ -113,7 +114,7 @@ module.exports = createReactClass({
}, },
onCustomKeyDown: function(event) { onCustomKeyDown: function(event) {
if (event.key === "Enter") { if (event.key === Key.ENTER) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();

View file

@ -38,6 +38,7 @@ import EMOJIBASE from 'emojibase-data/en/compact.json';
import SettingsStore from "../../../settings/SettingsStore"; import SettingsStore from "../../../settings/SettingsStore";
import EMOTICON_REGEX from 'emojibase-regex/emoticon'; import EMOTICON_REGEX from 'emojibase-regex/emoticon';
import sdk from '../../../index'; import sdk from '../../../index';
import {Key} from "../../../Keyboard";
const REGEX_EMOTICON_WHITESPACE = new RegExp('(?:^|\\s)(' + EMOTICON_REGEX.source + ')\\s$'); const REGEX_EMOTICON_WHITESPACE = new RegExp('(?:^|\\s)(' + EMOTICON_REGEX.source + ')\\s$');
@ -313,19 +314,19 @@ export default class BasicMessageEditor extends React.Component {
const modKey = IS_MAC ? event.metaKey : event.ctrlKey; const modKey = IS_MAC ? event.metaKey : event.ctrlKey;
let handled = false; let handled = false;
// format bold // format bold
if (modKey && event.key === "b") { if (modKey && event.key === Key.B) {
this._onFormatAction("bold"); this._onFormatAction("bold");
handled = true; handled = true;
// format italics // format italics
} else if (modKey && event.key === "i") { } else if (modKey && event.key === Key.I) {
this._onFormatAction("italics"); this._onFormatAction("italics");
handled = true; handled = true;
// format quote // format quote
} else if (modKey && event.key === ">") { } else if (modKey && event.key === Key.GREATER_THAN) {
this._onFormatAction("quote"); this._onFormatAction("quote");
handled = true; handled = true;
// undo // undo
} else if (modKey && event.key === "z") { } else if (modKey && event.key === Key.Z) {
if (this.historyManager.canUndo()) { if (this.historyManager.canUndo()) {
const {parts, caret} = this.historyManager.undo(this.props.model); const {parts, caret} = this.historyManager.undo(this.props.model);
// pass matching inputType so historyManager doesn't push echo // pass matching inputType so historyManager doesn't push echo
@ -334,7 +335,7 @@ export default class BasicMessageEditor extends React.Component {
} }
handled = true; handled = true;
// redo // redo
} else if (modKey && event.key === "y") { } else if (modKey && event.key === Key.Y) {
if (this.historyManager.canRedo()) { if (this.historyManager.canRedo()) {
const {parts, caret} = this.historyManager.redo(); const {parts, caret} = this.historyManager.redo();
// pass matching inputType so historyManager doesn't push echo // pass matching inputType so historyManager doesn't push echo
@ -343,7 +344,7 @@ export default class BasicMessageEditor extends React.Component {
} }
handled = true; handled = true;
// insert newline on Shift+Enter // insert newline on Shift+Enter
} else if (event.key === "Enter" && (event.shiftKey || (IS_MAC && event.altKey))) { } else if (event.key === Key.ENTER && (event.shiftKey || (IS_MAC && event.altKey))) {
this._insertText("\n"); this._insertText("\n");
handled = true; handled = true;
// autocomplete or enter to send below shouldn't have any modifier keys pressed. // autocomplete or enter to send below shouldn't have any modifier keys pressed.
@ -353,25 +354,25 @@ export default class BasicMessageEditor extends React.Component {
if (model.autoComplete && model.autoComplete.hasCompletions()) { if (model.autoComplete && model.autoComplete.hasCompletions()) {
const autoComplete = model.autoComplete; const autoComplete = model.autoComplete;
switch (event.key) { switch (event.key) {
case "ArrowUp": case Key.ARROW_UP:
if (!modifierPressed) { if (!modifierPressed) {
autoComplete.onUpArrow(event); autoComplete.onUpArrow(event);
handled = true; handled = true;
} }
break; break;
case "ArrowDown": case Key.ARROW_DOWN:
if (!modifierPressed) { if (!modifierPressed) {
autoComplete.onDownArrow(event); autoComplete.onDownArrow(event);
handled = true; handled = true;
} }
break; break;
case "Tab": case Key.TAB:
if (!metaOrAltPressed) { if (!metaOrAltPressed) {
autoComplete.onTab(event); autoComplete.onTab(event);
handled = true; handled = true;
} }
break; break;
case "Escape": case Key.ESCAPE:
if (!modifierPressed) { if (!modifierPressed) {
autoComplete.onEscape(event); autoComplete.onEscape(event);
handled = true; handled = true;
@ -380,7 +381,7 @@ export default class BasicMessageEditor extends React.Component {
default: default:
return; // don't preventDefault on anything else return; // don't preventDefault on anything else
} }
} else if (event.key === "Tab") { } else if (event.key === Key.TAB) {
this._tabCompleteName(); this._tabCompleteName();
handled = true; handled = true;
} }

View file

@ -30,6 +30,7 @@ import {MatrixClient} from 'matrix-js-sdk';
import classNames from 'classnames'; import classNames from 'classnames';
import {EventStatus} from 'matrix-js-sdk'; import {EventStatus} from 'matrix-js-sdk';
import BasicMessageComposer from "./BasicMessageComposer"; import BasicMessageComposer from "./BasicMessageComposer";
import {Key} from "../../../Keyboard";
function _isReply(mxEvent) { function _isReply(mxEvent) {
const relatesTo = mxEvent.getContent()["m.relates_to"]; const relatesTo = mxEvent.getContent()["m.relates_to"];
@ -134,12 +135,12 @@ export default class EditMessageComposer extends React.Component {
if (event.metaKey || event.altKey || event.shiftKey) { if (event.metaKey || event.altKey || event.shiftKey) {
return; return;
} }
if (event.key === "Enter") { if (event.key === Key.ENTER) {
this._sendEdit(); this._sendEdit();
event.preventDefault(); event.preventDefault();
} else if (event.key === "Escape") { } else if (event.key === Key.ESCAPE) {
this._cancelEdit(); this._cancelEdit();
} else if (event.key === "ArrowUp") { } else if (event.key === Key.ARROW_UP) {
if (this._editorRef.isModified() || !this._editorRef.isCaretAtStart()) { if (this._editorRef.isModified() || !this._editorRef.isCaretAtStart()) {
return; return;
} }
@ -148,7 +149,7 @@ export default class EditMessageComposer extends React.Component {
dis.dispatch({action: 'edit_event', event: previousEvent}); dis.dispatch({action: 'edit_event', event: previousEvent});
event.preventDefault(); event.preventDefault();
} }
} else if (event.key === "ArrowDown") { } else if (event.key === Key.ARROW_DOWN) {
if (this._editorRef.isModified() || !this._editorRef.isCaretAtEnd()) { if (this._editorRef.isModified() || !this._editorRef.isCaretAtEnd()) {
return; return;
} }

View file

@ -39,6 +39,7 @@ import sdk from '../../../index';
import Modal from '../../../Modal'; import Modal from '../../../Modal';
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import ContentMessages from '../../../ContentMessages'; import ContentMessages from '../../../ContentMessages';
import {Key} from "../../../Keyboard";
function addReplyToMessageContent(content, repliedToEvent, permalinkCreator) { function addReplyToMessageContent(content, repliedToEvent, permalinkCreator) {
const replyContent = ReplyThread.makeReplyMixIn(repliedToEvent); const replyContent = ReplyThread.makeReplyMixIn(repliedToEvent);
@ -109,12 +110,12 @@ export default class SendMessageComposer extends React.Component {
return; return;
} }
const hasModifier = event.altKey || event.ctrlKey || event.metaKey || event.shiftKey; const hasModifier = event.altKey || event.ctrlKey || event.metaKey || event.shiftKey;
if (event.key === "Enter" && !hasModifier) { if (event.key === Key.ENTER && !hasModifier) {
this._sendMessage(); this._sendMessage();
event.preventDefault(); event.preventDefault();
} else if (event.key === "ArrowUp") { } else if (event.key === Key.ARROW_UP) {
this.onVerticalArrow(event, true); this.onVerticalArrow(event, true);
} else if (event.key === "ArrowDown") { } else if (event.key === Key.ARROW_DOWN) {
this.onVerticalArrow(event, false); this.onVerticalArrow(event, false);
} }
} }