@wordpress/keycodes
Version:
Keycodes utilities for WordPress. Used to check for keyboard events across browsers/operating systems.
232 lines (231 loc) • 6.91 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var index_exports = {};
__export(index_exports, {
ALT: () => ALT,
BACKSPACE: () => BACKSPACE,
COMMAND: () => COMMAND,
CTRL: () => CTRL,
DELETE: () => DELETE,
DOWN: () => DOWN,
END: () => END,
ENTER: () => ENTER,
ESCAPE: () => ESCAPE,
F10: () => F10,
HOME: () => HOME,
LEFT: () => LEFT,
PAGEDOWN: () => PAGEDOWN,
PAGEUP: () => PAGEUP,
RIGHT: () => RIGHT,
SHIFT: () => SHIFT,
SPACE: () => SPACE,
TAB: () => TAB,
UP: () => UP,
ZERO: () => ZERO,
displayShortcut: () => displayShortcut,
displayShortcutList: () => displayShortcutList,
isAppleOS: () => import_platform.isAppleOS,
isKeyboardEvent: () => isKeyboardEvent,
modifiers: () => modifiers,
rawShortcut: () => rawShortcut,
shortcutAriaLabel: () => shortcutAriaLabel
});
module.exports = __toCommonJS(index_exports);
var import_i18n = require("@wordpress/i18n");
var import_platform = require("./platform");
const BACKSPACE = 8;
const TAB = 9;
const ENTER = 13;
const ESCAPE = 27;
const SPACE = 32;
const PAGEUP = 33;
const PAGEDOWN = 34;
const END = 35;
const HOME = 36;
const LEFT = 37;
const UP = 38;
const RIGHT = 39;
const DOWN = 40;
const DELETE = 46;
const F10 = 121;
const ALT = "alt";
const CTRL = "ctrl";
const COMMAND = "meta";
const SHIFT = "shift";
const ZERO = 48;
function capitaliseFirstCharacter(string) {
return string.length < 2 ? string.toUpperCase() : string.charAt(0).toUpperCase() + string.slice(1);
}
function mapValues(object, mapFn) {
return Object.fromEntries(
Object.entries(object).map(([key, value]) => [
key,
mapFn(value)
])
);
}
const modifiers = {
primary: (_isApple) => _isApple() ? [COMMAND] : [CTRL],
primaryShift: (_isApple) => _isApple() ? [SHIFT, COMMAND] : [CTRL, SHIFT],
primaryAlt: (_isApple) => _isApple() ? [ALT, COMMAND] : [CTRL, ALT],
secondary: (_isApple) => _isApple() ? [SHIFT, ALT, COMMAND] : [CTRL, SHIFT, ALT],
access: (_isApple) => _isApple() ? [CTRL, ALT] : [SHIFT, ALT],
ctrl: () => [CTRL],
alt: () => [ALT],
ctrlShift: () => [CTRL, SHIFT],
shift: () => [SHIFT],
shiftAlt: () => [SHIFT, ALT],
undefined: () => []
};
const rawShortcut = /* @__PURE__ */ mapValues(modifiers, (modifier) => {
return (character, _isApple = import_platform.isAppleOS) => {
return [...modifier(_isApple), character.toLowerCase()].join(
"+"
);
};
});
const displayShortcutList = /* @__PURE__ */ mapValues(
modifiers,
(modifier) => {
return (character, _isApple = import_platform.isAppleOS) => {
const isApple = _isApple();
const replacementKeyMap = {
[ALT]: isApple ? "\u2325" : "Alt",
[CTRL]: isApple ? "\u2303" : "Ctrl",
// Make sure ⌃ is the U+2303 UP ARROWHEAD unicode character and not the caret character.
[COMMAND]: "\u2318",
[SHIFT]: isApple ? "\u21E7" : "Shift"
};
const modifierKeys = modifier(_isApple).reduce(
(accumulator, key) => {
const replacementKey = replacementKeyMap[key] ?? key;
if (isApple) {
return [...accumulator, replacementKey];
}
return [...accumulator, replacementKey, "+"];
},
[]
);
return [
...modifierKeys,
capitaliseFirstCharacter(character)
];
};
}
);
const displayShortcut = /* @__PURE__ */ mapValues(
displayShortcutList,
(shortcutList) => {
return (character, _isApple = import_platform.isAppleOS) => shortcutList(character, _isApple).join("");
}
);
const shortcutAriaLabel = /* @__PURE__ */ mapValues(modifiers, (modifier) => {
return (character, _isApple = import_platform.isAppleOS) => {
const isApple = _isApple();
const replacementKeyMap = {
[SHIFT]: "Shift",
[COMMAND]: isApple ? "Command" : "Control",
[CTRL]: "Control",
[ALT]: isApple ? "Option" : "Alt",
/* translators: comma as in the character ',' */
",": (0, import_i18n.__)("Comma"),
/* translators: period as in the character '.' */
".": (0, import_i18n.__)("Period"),
/* translators: backtick as in the character '`' */
"`": (0, import_i18n.__)("Backtick"),
/* translators: tilde as in the character '~' */
"~": (0, import_i18n.__)("Tilde")
};
return [...modifier(_isApple), character].map(
(key) => capitaliseFirstCharacter(replacementKeyMap[key] ?? key)
).join(isApple ? " " : " + ");
};
});
function getEventModifiers(event) {
return [ALT, CTRL, COMMAND, SHIFT].filter(
(key) => event[`${key}Key`]
);
}
const isKeyboardEvent = /* @__PURE__ */ mapValues(modifiers, (getModifiers) => {
return (event, character, _isApple = import_platform.isAppleOS) => {
const mods = getModifiers(_isApple);
const eventMods = getEventModifiers(event);
const replacementWithShiftKeyMap = {
Comma: ",",
Backslash: "\\",
// Windows returns `\` for both IntlRo and IntlYen.
IntlRo: "\\",
IntlYen: "\\"
};
const modsDiff = mods.filter(
(mod) => !eventMods.includes(mod)
);
const eventModsDiff = eventMods.filter(
(mod) => !mods.includes(mod)
);
if (modsDiff.length > 0 || eventModsDiff.length > 0) {
return false;
}
let key = event.key.toLowerCase();
if (!character) {
return mods.includes(key);
}
if (event.altKey && character.length === 1) {
key = String.fromCharCode(event.keyCode).toLowerCase();
}
if (event.shiftKey && character.length === 1 && replacementWithShiftKeyMap[event.code]) {
key = replacementWithShiftKeyMap[event.code];
}
if (character === "del") {
character = "delete";
}
return key === character.toLowerCase();
};
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ALT,
BACKSPACE,
COMMAND,
CTRL,
DELETE,
DOWN,
END,
ENTER,
ESCAPE,
F10,
HOME,
LEFT,
PAGEDOWN,
PAGEUP,
RIGHT,
SHIFT,
SPACE,
TAB,
UP,
ZERO,
displayShortcut,
displayShortcutList,
isAppleOS,
isKeyboardEvent,
modifiers,
rawShortcut,
shortcutAriaLabel
});
//# sourceMappingURL=index.js.map