kollapsminoriteten/wp-includes/js/dist/keycodes.js

466 lines
14 KiB
JavaScript
Raw Normal View History

2024-04-17 11:32:24 +02:00
/******/ (() => { // webpackBootstrap
2022-06-16 14:03:35 +02:00
/******/ "use strict";
/******/ // The require scope
/******/ var __webpack_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
2024-04-17 11:32:24 +02:00
/******/ (() => {
2022-06-16 14:03:35 +02:00
/******/ // define getter functions for harmony exports
2024-04-17 11:32:24 +02:00
/******/ __webpack_require__.d = (exports, definition) => {
2022-06-16 14:03:35 +02:00
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
2024-04-17 11:32:24 +02:00
/******/ })();
2022-06-16 14:03:35 +02:00
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
2024-04-17 11:32:24 +02:00
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
2022-06-16 14:03:35 +02:00
/******/
/******/ /* webpack/runtime/make namespace object */
2024-04-17 11:32:24 +02:00
/******/ (() => {
2022-06-16 14:03:35 +02:00
/******/ // define __esModule on exports
2024-04-17 11:32:24 +02:00
/******/ __webpack_require__.r = (exports) => {
2022-06-16 14:03:35 +02:00
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
2019-11-02 10:38:58 +01:00
/******/ };
2024-04-17 11:32:24 +02:00
/******/ })();
2022-06-16 14:03:35 +02:00
/******/
2019-11-02 10:38:58 +01:00
/************************************************************************/
2022-06-16 14:03:35 +02:00
var __webpack_exports__ = {};
2020-09-15 14:29:22 +02:00
// ESM COMPAT FLAG
2019-11-02 10:38:58 +01:00
__webpack_require__.r(__webpack_exports__);
2020-09-15 14:29:22 +02:00
// EXPORTS
2022-06-16 14:03:35 +02:00
__webpack_require__.d(__webpack_exports__, {
2024-04-17 11:32:24 +02:00
ALT: () => (/* binding */ ALT),
BACKSPACE: () => (/* binding */ BACKSPACE),
COMMAND: () => (/* binding */ COMMAND),
CTRL: () => (/* binding */ CTRL),
DELETE: () => (/* binding */ DELETE),
DOWN: () => (/* binding */ DOWN),
END: () => (/* binding */ END),
ENTER: () => (/* binding */ ENTER),
ESCAPE: () => (/* binding */ ESCAPE),
F10: () => (/* binding */ F10),
HOME: () => (/* binding */ HOME),
LEFT: () => (/* binding */ LEFT),
PAGEDOWN: () => (/* binding */ PAGEDOWN),
PAGEUP: () => (/* binding */ PAGEUP),
RIGHT: () => (/* binding */ RIGHT),
SHIFT: () => (/* binding */ SHIFT),
SPACE: () => (/* binding */ SPACE),
TAB: () => (/* binding */ TAB),
UP: () => (/* binding */ UP),
ZERO: () => (/* binding */ ZERO),
displayShortcut: () => (/* binding */ displayShortcut),
displayShortcutList: () => (/* binding */ displayShortcutList),
isAppleOS: () => (/* reexport */ isAppleOS),
isKeyboardEvent: () => (/* binding */ isKeyboardEvent),
modifiers: () => (/* binding */ modifiers),
rawShortcut: () => (/* binding */ rawShortcut),
shortcutAriaLabel: () => (/* binding */ shortcutAriaLabel)
2022-06-16 14:03:35 +02:00
});
;// CONCATENATED MODULE: external ["wp","i18n"]
2024-04-17 11:32:24 +02:00
const external_wp_i18n_namespaceObject = window["wp"]["i18n"];
2022-06-16 14:03:35 +02:00
;// CONCATENATED MODULE: ./node_modules/@wordpress/keycodes/build-module/platform.js
2019-11-02 10:38:58 +01:00
/**
* Return true if platform is MacOS.
*
2021-07-23 11:58:50 +02:00
* @param {Window?} _window window object by default; used for DI testing.
2019-11-02 10:38:58 +01:00
*
2021-04-27 08:32:47 +02:00
* @return {boolean} True if MacOS; false otherwise.
2019-11-02 10:38:58 +01:00
*/
2023-09-26 10:33:34 +02:00
function isAppleOS(_window = null) {
2021-07-23 11:58:50 +02:00
if (!_window) {
if (typeof window === 'undefined') {
return false;
}
_window = window;
}
const {
platform
} = _window.navigator;
2023-04-26 17:39:43 +02:00
return platform.indexOf('Mac') !== -1 || ['iPad', 'iPhone'].includes(platform);
2019-11-02 10:38:58 +01:00
}
2022-06-16 14:03:35 +02:00
;// CONCATENATED MODULE: ./node_modules/@wordpress/keycodes/build-module/index.js
2019-11-02 10:38:58 +01:00
/**
* Note: The order of the modifier keys in many of the [foo]Shortcut()
* functions in this file are intentional and should not be changed. They're
* designed to fit with the standard menu keyboard shortcuts shown in the
* user's platform.
*
* For example, on MacOS menu shortcuts will place Shift before Command, but
* on Windows Control will usually come first. So don't provide your own
* shortcut combos directly to keyboardShortcut().
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
2021-04-27 08:32:47 +02:00
/** @typedef {typeof ALT | CTRL | COMMAND | SHIFT } WPModifierPart */
2022-04-02 10:26:41 +02:00
/** @typedef {'primary' | 'primaryShift' | 'primaryAlt' | 'secondary' | 'access' | 'ctrl' | 'alt' | 'ctrlShift' | 'shift' | 'shiftAlt' | 'undefined'} WPKeycodeModifier */
2020-05-06 17:23:38 +02:00
/**
* An object of handler functions for each of the possible modifier
* combinations. A handler will return a value for a given key.
*
2021-04-27 08:32:47 +02:00
* @template T
*
* @typedef {Record<WPKeycodeModifier, T>} WPModifierHandler
*/
/**
* @template T
*
* @typedef {(character: string, isApple?: () => boolean) => T} WPKeyHandler
2020-05-06 17:23:38 +02:00
*/
2023-09-26 10:33:34 +02:00
/** @typedef {(event: import('react').KeyboardEvent<HTMLElement> | KeyboardEvent, character: string, isApple?: () => boolean) => boolean} WPEventKeyHandler */
2021-04-27 08:32:47 +02:00
2023-04-26 17:39:43 +02:00
/** @typedef {( isApple: () => boolean ) => WPModifierPart[]} WPModifier */
2019-11-02 10:38:58 +01:00
/**
* Keycode for BACKSPACE key.
*/
2021-07-23 11:58:50 +02:00
const BACKSPACE = 8;
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for TAB key.
*/
2021-07-23 11:58:50 +02:00
const TAB = 9;
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for ENTER key.
*/
2021-07-23 11:58:50 +02:00
const ENTER = 13;
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for ESCAPE key.
*/
2021-07-23 11:58:50 +02:00
const ESCAPE = 27;
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for SPACE key.
*/
2021-07-23 11:58:50 +02:00
const SPACE = 32;
2023-12-07 09:44:11 +01:00
2022-04-02 10:26:41 +02:00
/**
* Keycode for PAGEUP key.
*/
const PAGEUP = 33;
2023-12-07 09:44:11 +01:00
2022-04-02 10:26:41 +02:00
/**
* Keycode for PAGEDOWN key.
*/
const PAGEDOWN = 34;
2023-12-07 09:44:11 +01:00
2022-04-02 10:26:41 +02:00
/**
* Keycode for END key.
*/
const END = 35;
2023-12-07 09:44:11 +01:00
2022-04-02 10:26:41 +02:00
/**
* Keycode for HOME key.
*/
const HOME = 36;
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for LEFT key.
*/
2021-07-23 11:58:50 +02:00
const LEFT = 37;
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for UP key.
*/
2021-07-23 11:58:50 +02:00
const UP = 38;
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for RIGHT key.
*/
2021-07-23 11:58:50 +02:00
const RIGHT = 39;
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for DOWN key.
*/
2021-07-23 11:58:50 +02:00
const DOWN = 40;
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for DELETE key.
*/
2021-07-23 11:58:50 +02:00
const DELETE = 46;
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for F10 key.
*/
2021-07-23 11:58:50 +02:00
const F10 = 121;
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for ALT key.
*/
2021-07-23 11:58:50 +02:00
const ALT = 'alt';
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for CTRL key.
*/
2021-07-23 11:58:50 +02:00
const CTRL = 'ctrl';
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for COMMAND/META key.
*/
2021-07-23 11:58:50 +02:00
const COMMAND = 'meta';
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Keycode for SHIFT key.
*/
2021-07-23 11:58:50 +02:00
const SHIFT = 'shift';
2023-12-07 09:44:11 +01:00
2020-09-15 14:29:22 +02:00
/**
* Keycode for ZERO key.
*/
2021-07-23 11:58:50 +02:00
const ZERO = 48;
2022-12-15 17:47:31 +01:00
2023-12-07 09:44:11 +01:00
2024-04-17 11:32:24 +02:00
/**
* Capitalise the first character of a string.
* @param {string} string String to capitalise.
* @return {string} Capitalised string.
*/
function capitaliseFirstCharacter(string) {
return string.length < 2 ? string.toUpperCase() : string.charAt(0).toUpperCase() + string.slice(1);
}
2023-04-26 17:39:43 +02:00
/**
* Map the values of an object with a specified callback and return the result object.
*
2023-09-26 10:33:34 +02:00
* @template {{ [s: string]: any; } | ArrayLike<any>} T
2023-04-26 17:39:43 +02:00
*
* @param {T} object Object to map values of.
* @param {( value: any ) => any} mapFn Mapping function
*
* @return {any} Active modifier constants.
*/
function mapValues(object, mapFn) {
2023-09-26 10:33:34 +02:00
return Object.fromEntries(Object.entries(object).map(([key, value]) => [key, mapFn(value)]));
2023-04-26 17:39:43 +02:00
}
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* Object that contains functions that return the available modifier
* depending on platform.
*
2021-04-27 08:32:47 +02:00
* @type {WPModifierHandler< ( isApple: () => boolean ) => WPModifierPart[]>}
2019-11-02 10:38:58 +01:00
*/
2021-07-23 11:58:50 +02:00
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],
2022-04-02 10:26:41 +02:00
shiftAlt: () => [SHIFT, ALT],
undefined: () => []
2019-11-02 10:38:58 +01:00
};
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* An object that contains functions to get raw shortcuts.
*
2021-04-27 08:32:47 +02:00
* These are intended for user with the KeyboardShortcuts.
*
* @example
* ```js
* // Assuming macOS:
* rawShortcut.primary( 'm' )
* // "meta+m""
* ```
*
* @type {WPModifierHandler<WPKeyHandler<string>>} Keyed map of functions to raw
* shortcuts.
2019-11-02 10:38:58 +01:00
*/
2023-12-07 09:44:11 +01:00
const rawShortcut = mapValues(modifiers, ( /** @type {WPModifier} */modifier) => {
2024-04-17 11:32:24 +02:00
return /** @type {WPKeyHandler<string>} */(character, _isApple = isAppleOS) => {
return [...modifier(_isApple), character.toLowerCase()].join('+');
};
2019-11-02 10:38:58 +01:00
});
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
2021-04-27 08:32:47 +02:00
* Return an array of the parts of a keyboard shortcut chord for display.
2019-11-02 10:38:58 +01:00
*
2021-04-27 08:32:47 +02:00
* @example
* ```js
* // Assuming macOS:
* displayShortcutList.primary( 'm' );
* // [ "⌘", "M" ]
* ```
*
* @type {WPModifierHandler<WPKeyHandler<string[]>>} Keyed map of functions to
* shortcut sequences.
2019-11-02 10:38:58 +01:00
*/
2023-12-07 09:44:11 +01:00
const displayShortcutList = mapValues(modifiers, ( /** @type {WPModifier} */modifier) => {
2024-04-17 11:32:24 +02:00
return /** @type {WPKeyHandler<string[]>} */(character, _isApple = isAppleOS) => {
const isApple = _isApple();
const replacementKeyMap = {
[ALT]: isApple ? '⌥' : 'Alt',
[CTRL]: isApple ? '⌃' : 'Ctrl',
// Make sure ⌃ is the U+2303 UP ARROWHEAD unicode character and not the caret character.
[COMMAND]: '⌘',
[SHIFT]: isApple ? '⇧' : 'Shift'
};
const modifierKeys = modifier(_isApple).reduce((accumulator, key) => {
var _replacementKeyMap$ke;
const replacementKey = (_replacementKeyMap$ke = replacementKeyMap[key]) !== null && _replacementKeyMap$ke !== void 0 ? _replacementKeyMap$ke : key;
// If on the Mac, adhere to platform convention and don't show plus between keys.
if (isApple) {
return [...accumulator, replacementKey];
}
return [...accumulator, replacementKey, '+'];
}, /** @type {string[]} */[]);
return [...modifierKeys, capitaliseFirstCharacter(character)];
};
2019-11-02 10:38:58 +01:00
});
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* An object that contains functions to display shortcuts.
*
2021-04-27 08:32:47 +02:00
* @example
* ```js
* // Assuming macOS:
* displayShortcut.primary( 'm' );
* // "⌘M"
* ```
*
* @type {WPModifierHandler<WPKeyHandler<string>>} Keyed map of functions to
* display shortcuts.
2019-11-02 10:38:58 +01:00
*/
2023-12-07 09:44:11 +01:00
const displayShortcut = mapValues(displayShortcutList, ( /** @type {WPKeyHandler<string[]>} */shortcutList) => {
2024-04-17 11:32:24 +02:00
return /** @type {WPKeyHandler<string>} */(character, _isApple = isAppleOS) => shortcutList(character, _isApple).join('');
2019-11-02 10:38:58 +01:00
});
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
2021-04-27 08:32:47 +02:00
* An object that contains functions to return an aria label for a keyboard
* shortcut.
*
* @example
* ```js
* // Assuming macOS:
* shortcutAriaLabel.primary( '.' );
* // "Command + Period"
* ```
2020-05-06 17:23:38 +02:00
*
2021-04-27 08:32:47 +02:00
* @type {WPModifierHandler<WPKeyHandler<string>>} Keyed map of functions to
* shortcut ARIA labels.
2019-11-02 10:38:58 +01:00
*/
2023-12-07 09:44:11 +01:00
const shortcutAriaLabel = mapValues(modifiers, ( /** @type {WPModifier} */modifier) => {
2024-04-17 11:32:24 +02:00
return /** @type {WPKeyHandler<string>} */(character, _isApple = isAppleOS) => {
const isApple = _isApple();
/** @type {Record<string,string>} */
const replacementKeyMap = {
[SHIFT]: 'Shift',
[COMMAND]: isApple ? 'Command' : 'Control',
[CTRL]: 'Control',
[ALT]: isApple ? 'Option' : 'Alt',
/* translators: comma as in the character ',' */
',': (0,external_wp_i18n_namespaceObject.__)('Comma'),
/* translators: period as in the character '.' */
'.': (0,external_wp_i18n_namespaceObject.__)('Period'),
/* translators: backtick as in the character '`' */
'`': (0,external_wp_i18n_namespaceObject.__)('Backtick'),
/* translators: tilde as in the character '~' */
'~': (0,external_wp_i18n_namespaceObject.__)('Tilde')
};
return [...modifier(_isApple), character].map(key => {
var _replacementKeyMap$ke2;
return capitaliseFirstCharacter((_replacementKeyMap$ke2 = replacementKeyMap[key]) !== null && _replacementKeyMap$ke2 !== void 0 ? _replacementKeyMap$ke2 : key);
}).join(isApple ? ' ' : ' + ');
};
2019-11-02 10:38:58 +01:00
});
2023-12-07 09:44:11 +01:00
2020-09-15 14:29:22 +02:00
/**
* From a given KeyboardEvent, returns an array of active modifier constants for
* the event.
*
2023-09-26 10:33:34 +02:00
* @param {import('react').KeyboardEvent<HTMLElement> | KeyboardEvent} event Keyboard event.
2020-09-15 14:29:22 +02:00
*
2021-04-27 08:32:47 +02:00
* @return {Array<WPModifierPart>} Active modifier constants.
2020-09-15 14:29:22 +02:00
*/
function getEventModifiers(event) {
2024-04-17 11:32:24 +02:00
return /** @type {WPModifierPart[]} */[ALT, CTRL, COMMAND, SHIFT].filter(key => event[( /** @type {'altKey' | 'ctrlKey' | 'metaKey' | 'shiftKey'} */
`${key}Key`)]);
2020-09-15 14:29:22 +02:00
}
2023-12-07 09:44:11 +01:00
2019-11-02 10:38:58 +01:00
/**
* An object that contains functions to check if a keyboard event matches a
* predefined shortcut combination.
*
2021-04-27 08:32:47 +02:00
* @example
* ```js
* // Assuming an event for ⌘M key press:
* isKeyboardEvent.primary( event, 'm' );
* // true
* ```
*
* @type {WPModifierHandler<WPEventKeyHandler>} Keyed map of functions
* to match events.
2019-11-02 10:38:58 +01:00
*/
2023-12-07 09:44:11 +01:00
const isKeyboardEvent = mapValues(modifiers, ( /** @type {WPModifier} */getModifiers) => {
2024-04-17 11:32:24 +02:00
return /** @type {WPEventKeyHandler} */(event, character, _isApple = isAppleOS) => {
const mods = getModifiers(_isApple);
const eventMods = getEventModifiers(event);
/** @type {Record<string,string>} */
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( /** @type {WPModifierPart} */key);
}
if (event.altKey && character.length === 1) {
key = String.fromCharCode(event.keyCode).toLowerCase();
}
2023-12-07 09:44:11 +01:00
2024-04-17 11:32:24 +02:00
// `event.key` returns the value of the key pressed, taking into the state of
// modifier keys such as `Shift`. If the shift key is pressed, a different
// value may be returned depending on the keyboard layout. It is necessary to
// convert to the physical key value that don't take into account keyboard
// layout or modifier key state.
if (event.shiftKey && character.length === 1 && replacementWithShiftKeyMap[event.code]) {
key = replacementWithShiftKeyMap[event.code];
}
2022-04-02 10:26:41 +02:00
2024-04-17 11:32:24 +02:00
// For backwards compatibility.
if (character === 'del') {
character = 'delete';
2021-04-27 08:32:47 +02:00
}
2024-04-17 11:32:24 +02:00
return key === character.toLowerCase();
};
2019-11-02 10:38:58 +01:00
});
2022-06-16 14:03:35 +02:00
(window.wp = window.wp || {}).keycodes = __webpack_exports__;
/******/ })()
;