Adds keyboard shortcut to open context menu on messages

This commit is contained in:
Josh Perez 2023-06-21 09:54:05 -07:00 committed by GitHub
parent 498205b964
commit 7247c2d674
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 114 additions and 14 deletions

View file

@ -234,6 +234,39 @@ export function useToggleReactionPicker(
);
}
export function useOpenContextMenu(
openContextMenu: () => unknown
): KeyboardShortcutHandlerType {
const hasOverlay = useHasAnyOverlay();
return useCallback(
ev => {
if (hasOverlay) {
return false;
}
const { shiftKey } = ev;
const key = KeyboardLayout.lookup(ev);
const isMacOS = get(window, 'platform') === 'darwin';
if (
(!isMacOS && shiftKey && key === 'F10') ||
(isMacOS && isCmdOrCtrl(ev) && key === 'F12')
) {
ev.preventDefault();
ev.stopPropagation();
openContextMenu();
return true;
}
return false;
},
[hasOverlay, openContextMenu]
);
}
export function useEditLastMessageSent(
maybeEditMessage: () => boolean
): KeyboardShortcutHandlerType {
@ -277,3 +310,23 @@ export function useKeyboardShortcuts(
};
}, [eventHandlers]);
}
export function useKeyboardShortcutsConditionally(
condition: boolean,
...eventHandlers: Array<KeyboardShortcutHandlerType>
): void {
useEffect(() => {
if (!condition) {
return;
}
function handleKeydown(ev: KeyboardEvent): void {
eventHandlers.some(eventHandler => eventHandler(ev));
}
document.addEventListener('keydown', handleKeydown);
return () => {
document.removeEventListener('keydown', handleKeydown);
};
}, [condition, eventHandlers]);
}