From c569eed5dd1674677110270a3d26e3e3ab538e1e Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Fri, 25 Jun 2021 14:23:02 +0300 Subject: [PATCH 1/5] os specific shortcuts, add new group shortcut --- .../Navigation/AppNavigator/AuthScreens.js | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 1d5e0a30376a..ffd10df535c5 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -31,6 +31,7 @@ import {getPolicySummaries, getPolicyList} from '../../actions/Policy'; import modalCardStyleInterpolator from './modalCardStyleInterpolator'; import createCustomModalStackNavigator from './createCustomModalStackNavigator'; import Permissions from '../../Permissions'; +import getOperatingSystem from '../../getOperatingSystem'; // Main drawer navigator import MainDrawerNavigator from './MainDrawerNavigator'; @@ -155,10 +156,24 @@ class AuthScreens extends React.Component { Timing.end(CONST.TIMING.HOMEPAGE_INITIAL_RENDER); - // Listen for the Command+K key being pressed so the focus can be given to the chat switcher - KeyboardShortcut.subscribe('K', () => { - Navigation.navigate(ROUTES.SEARCH); - }, ['meta'], true); + // Listen for the key K being pressed so that focus can be given to + // the chat switcher, or new group chat + // based on the key modifiers pressed and the operating system + if (getOperatingSystem() === CONST.OS.MAC_OS) { + KeyboardShortcut.subscribe('K', () => { + Navigation.navigate(ROUTES.SEARCH); + }, ['meta'], true); + KeyboardShortcut.subscribe('K', () => { + Navigation.navigate(ROUTES.NEW_GROUP); + }, ['meta', 'shift'], true); + } else { + KeyboardShortcut.subscribe('K', () => { + Navigation.navigate(ROUTES.SEARCH); + }, ['control'], true); + KeyboardShortcut.subscribe('K', () => { + Navigation.navigate(ROUTES.NEW_GROUP); + }, ['control', 'shift'], true); + } } shouldComponentUpdate(nextProps) { From 755bf7e7744c9830f1ca8cc86e1a5bdc49aa0722 Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Fri, 25 Jun 2021 14:33:12 +0300 Subject: [PATCH 2/5] run callbacks only if exact modifiers are pressed --- src/libs/KeyboardShortcut/index.js | 79 ++++++++++++++++++------------ 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/src/libs/KeyboardShortcut/index.js b/src/libs/KeyboardShortcut/index.js index 250fa9c9c3d4..2c7d3624977f 100644 --- a/src/libs/KeyboardShortcut/index.js +++ b/src/libs/KeyboardShortcut/index.js @@ -11,43 +11,62 @@ function bindHandlerToKeyupEvent(event) { return; } - // The active callback is the last element in the array const eventCallbacks = events[event.keyCode]; - const callback = eventCallbacks[eventCallbacks.length - 1]; - const pressedModifiers = _.all(callback.modifiers, (modifier) => { - if (modifier === 'shift' && !event.shiftKey) { + // Loop over all the callbacks + eventCallbacks.forEach((callback) => { + let extraModifiers = ['shift', 'control', 'alt', 'meta']; + const pressedModifiers = _.all(callback.modifiers, (modifier) => { + extraModifiers = _.without(extraModifiers, modifier); + if (modifier === 'shift' && !event.shiftKey) { + return false; + } + if (modifier === 'control' && !event.ctrlKey) { + return false; + } + if (modifier === 'alt' && !event.altKey) { + return false; + } + if (modifier === 'meta' && !event.metaKey) { + return false; + } + return true; + }); + + // returns true if extra modifiers are pressed + const pressedExtraModifiers = _.some(extraModifiers, (extraModifier) => { + if (extraModifier === 'shift' && event.shiftKey) { + return true; + } + if (extraModifier === 'control' && event.ctrlKey) { + return true; + } + if (extraModifier === 'alt' && event.altKey) { + return true; + } + if (extraModifier === 'meta' && event.metaKey) { + return true; + } return false; + }); + if (!pressedModifiers || pressedExtraModifiers) { + return; } - if (modifier === 'control' && !event.ctrlKey) { - return false; - } - if (modifier === 'alt' && !event.altKey) { - return false; + + // If configured to do so, prevent input text control to trigger this event + if (!callback.captureOnInputs && ( + event.target.nodeName === 'INPUT' + || event.target.nodeName === 'TEXTAREA' + || event.target.contentEditable === 'true' + )) { + return; } - if (modifier === 'meta' && !event.metaKey) { - return false; + + if (_.isFunction(callback.callback)) { + callback.callback(event); } - return true; + event.preventDefault(); }); - - if (!pressedModifiers) { - return; - } - - // If configured to do so, prevent input text control to trigger this event - if (!callback.captureOnInputs && ( - event.target.nodeName === 'INPUT' - || event.target.nodeName === 'TEXTAREA' - || event.target.contentEditable === 'true' - )) { - return; - } - - if (_.isFunction(callback.callback)) { - callback.callback(event); - } - event.preventDefault(); } // Make sure we don't add multiple listeners From 0a92a89b32ac001dcb01a5c03979ab16bf331b9f Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Fri, 25 Jun 2021 14:39:02 +0300 Subject: [PATCH 3/5] eslint --fix --- src/libs/KeyboardShortcut/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/KeyboardShortcut/index.js b/src/libs/KeyboardShortcut/index.js index 2c7d3624977f..044694e729e8 100644 --- a/src/libs/KeyboardShortcut/index.js +++ b/src/libs/KeyboardShortcut/index.js @@ -32,7 +32,7 @@ function bindHandlerToKeyupEvent(event) { } return true; }); - + // returns true if extra modifiers are pressed const pressedExtraModifiers = _.some(extraModifiers, (extraModifier) => { if (extraModifier === 'shift' && event.shiftKey) { @@ -52,7 +52,7 @@ function bindHandlerToKeyupEvent(event) { if (!pressedModifiers || pressedExtraModifiers) { return; } - + // If configured to do so, prevent input text control to trigger this event if (!callback.captureOnInputs && ( event.target.nodeName === 'INPUT' @@ -61,7 +61,7 @@ function bindHandlerToKeyupEvent(event) { )) { return; } - + if (_.isFunction(callback.callback)) { callback.callback(event); } From 094b81282a30daedbbb38c088d947067674477cf Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Fri, 25 Jun 2021 15:33:19 +0300 Subject: [PATCH 4/5] cleaner way to get extraModifiers --- src/libs/KeyboardShortcut/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libs/KeyboardShortcut/index.js b/src/libs/KeyboardShortcut/index.js index 044694e729e8..1f3e0131b1f7 100644 --- a/src/libs/KeyboardShortcut/index.js +++ b/src/libs/KeyboardShortcut/index.js @@ -15,9 +15,8 @@ function bindHandlerToKeyupEvent(event) { // Loop over all the callbacks eventCallbacks.forEach((callback) => { - let extraModifiers = ['shift', 'control', 'alt', 'meta']; + const extraModifiers = _.difference(['shift', 'control', 'alt', 'meta'], callback.modifiers); const pressedModifiers = _.all(callback.modifiers, (modifier) => { - extraModifiers = _.without(extraModifiers, modifier); if (modifier === 'shift' && !event.shiftKey) { return false; } From a3cb77d5096d0b8630c7093576fa9b1fcc67c367 Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Fri, 25 Jun 2021 17:51:09 +0300 Subject: [PATCH 5/5] refactor --- src/libs/KeyboardShortcut/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/KeyboardShortcut/index.js b/src/libs/KeyboardShortcut/index.js index 1f3e0131b1f7..3cc302801e99 100644 --- a/src/libs/KeyboardShortcut/index.js +++ b/src/libs/KeyboardShortcut/index.js @@ -15,7 +15,6 @@ function bindHandlerToKeyupEvent(event) { // Loop over all the callbacks eventCallbacks.forEach((callback) => { - const extraModifiers = _.difference(['shift', 'control', 'alt', 'meta'], callback.modifiers); const pressedModifiers = _.all(callback.modifiers, (modifier) => { if (modifier === 'shift' && !event.shiftKey) { return false; @@ -32,6 +31,8 @@ function bindHandlerToKeyupEvent(event) { return true; }); + const extraModifiers = _.difference(['shift', 'control', 'alt', 'meta'], callback.modifiers); + // returns true if extra modifiers are pressed const pressedExtraModifiers = _.some(extraModifiers, (extraModifier) => { if (extraModifier === 'shift' && event.shiftKey) {