UNPKG

mui-tiptap

Version:

A Material-UI (MUI) styled WYSIWYG rich text editor, using Tiptap

52 lines (51 loc) 2.07 kB
// We'll cache the result of isMac() and isTouchDevice(), since they shouldn't // change during a session. That way repeated calls don't require any logic and // are rapid. let isMacResult; let isTouchDeviceResult; /** * Return true if the user is using a Mac (as opposed to Windows, etc.) device. */ export function isMac() { if (isMacResult === undefined) { // eslint-disable-next-line @typescript-eslint/no-deprecated isMacResult = navigator.platform.includes("Mac"); } return isMacResult; } /** * Return a human-readable version of which modifier key should be used for * keyboard shortcuts depending on Mac vs non-Mac platforms. Useful for visually * indicating which key to press. */ export function getModShortcutKey() { return isMac() ? "⌘" : "Ctrl"; } /** * Format an array of shortcut keys (e.g. ["mod", "Shift", "B"]) into a * human-readable string (e.g. "Ctrl + Shift + B" or "⌘ + Shift + B"), suitable * for use as an aria-description. Returns undefined if the array is empty or * undefined. */ export function getShortcutKeysDescription(shortcutKeys) { return (shortcutKeys === null || shortcutKeys === void 0 ? void 0 : shortcutKeys.length) ? shortcutKeys .map((key) => (key === "mod" ? getModShortcutKey() : key)) .join(" + ") : undefined; } /** Return true if the user is using a touch-based device. */ export function isTouchDevice() { if (isTouchDeviceResult === undefined) { // This technique is taken from // https://hacks.mozilla.org/2013/04/detecting-touch-its-the-why-not-the-how/ // (and https://stackoverflow.com/a/4819886/4543977) isTouchDeviceResult = // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition (window && "ontouchstart" in window) || navigator.maxTouchPoints > 0 || // @ts-expect-error: msMaxTouchPoints is IE-specific, so needs to be ignored navigator.msMaxTouchPoints > 0; } return isTouchDeviceResult; }