Files
chatwoot/app/javascript/shared/helpers/KeyboardHelpers.js
Shivam Mishra 47f8b2cd0c refactor: handling keyboard shortcuts (#9242)
* fix: Resolve and go next keyboard shortcuts doesn't work

* refactor: use buildHotKeys instead of  hasPressedCommandPlusAltAndEKey

* feat: install tinykeys

* refactor: use tinykeys

* test: update buildKeyEvents

* fix: remove stray import

* feat: handle action list globally

* feat: allow configuring `allowOnFocusedInput`

* chore: Navigate chat list item

* chore: Navigate dashboard

* feat: Navigate editor top panel

* feat: Toggle file upload

* chore: More keyboard shortcuts

* chore: Update mention selection mixin

* chore: Phone input

* chore: Clean up

* chore: Clean up

* chore: Dropdown and editor

* chore: Enter key to send and clean up

* chore: Rename mixin

* chore: Review fixes

* chore: Removed unused shortcut from modal

* fix: Specs

---------

Co-authored-by: iamsivin <iamsivin@gmail.com>
Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
2024-04-26 15:41:02 +05:30

51 lines
1.2 KiB
JavaScript

export const isEnter = e => {
return e.key === 'Enter';
};
export const isEscape = e => {
return e.key === 'Escape';
};
export const hasPressedShift = e => {
return e.shiftKey;
};
export const hasPressedCommand = e => {
return e.metaKey;
};
export const hasPressedEnterAndNotCmdOrShift = e => {
return isEnter(e) && !hasPressedCommand(e) && !hasPressedShift(e);
};
export const hasPressedCommandAndEnter = e => {
return hasPressedCommand(e) && isEnter(e);
};
/**
* Determines whether the active element is typeable.
*
* @param {KeyboardEvent} e - The keyboard event object.
* @returns {boolean} `true` if the active element is typeable, `false` otherwise.
*
* @example
* document.addEventListener('keydown', e => {
* if (isActiveElementTypeable(e)) {
* handleTypeableElement(e);
* }
* });
*/
export const isActiveElementTypeable = e => {
/** @type {HTMLElement | null} */
// @ts-ignore
const activeElement = e.target || document.activeElement;
return !!(
activeElement?.tagName === 'INPUT' ||
activeElement?.tagName === 'NINJA-KEYS' ||
activeElement?.tagName === 'TEXTAREA' ||
activeElement?.contentEditable === 'true' ||
activeElement?.className?.includes('ProseMirror')
);
};