Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New keyboard shortcut for new Group chat #3754

Merged
merged 5 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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