Skip to content

Commit

Permalink
Merge pull request #3754 from rushatgabhane/group-chat-keyboard-shortcut
Browse files Browse the repository at this point in the history
New keyboard shortcut for new Group chat
  • Loading branch information
ctkochan22 authored Jun 28, 2021
2 parents cfb5d1d + a3cb77d commit eff095b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 34 deletions.
79 changes: 49 additions & 30 deletions src/libs/KeyboardShortcut/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
return false;
}
if (modifier === 'control' && !event.ctrlKey) {
// Loop over all the callbacks
eventCallbacks.forEach((callback) => {
const pressedModifiers = _.all(callback.modifiers, (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;
});

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) {
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 === '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
Expand Down
23 changes: 19 additions & 4 deletions src/libs/Navigation/AppNavigator/AuthScreens.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit eff095b

Please sign in to comment.