@wordpress/keycodes 4.27.1-next.46f643fa0.0 → 4.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,27 +19,48 @@ import { __ } from '@wordpress/i18n';
19
19
  */
20
20
  import { isAppleOS } from './platform';
21
21
 
22
- /** @typedef {typeof ALT | CTRL | COMMAND | SHIFT } WPModifierPart */
22
+ /**
23
+ * External dependencies
24
+ */
25
+ import type { KeyboardEvent as ReactKeyboardEvent } from 'react';
23
26
 
24
- /** @typedef {'primary' | 'primaryShift' | 'primaryAlt' | 'secondary' | 'access' | 'ctrl' | 'alt' | 'ctrlShift' | 'shift' | 'shiftAlt' | 'undefined'} WPKeycodeModifier */
27
+ export type WPModifierPart =
28
+ | typeof ALT
29
+ | typeof CTRL
30
+ | typeof COMMAND
31
+ | typeof SHIFT;
32
+
33
+ export type WPKeycodeModifier =
34
+ | 'primary'
35
+ | 'primaryShift'
36
+ | 'primaryAlt'
37
+ | 'secondary'
38
+ | 'access'
39
+ | 'ctrl'
40
+ | 'alt'
41
+ | 'ctrlShift'
42
+ | 'shift'
43
+ | 'shiftAlt'
44
+ | 'undefined';
25
45
 
26
46
  /**
27
47
  * An object of handler functions for each of the possible modifier
28
48
  * combinations. A handler will return a value for a given key.
29
- *
30
- * @template T
31
- *
32
- * @typedef {Record<WPKeycodeModifier, T>} WPModifierHandler
33
49
  */
50
+ export type WPModifierHandler< T > = Record< WPKeycodeModifier, T >;
34
51
 
35
- /**
36
- * @template T
37
- *
38
- * @typedef {(character: string, isApple?: () => boolean) => T} WPKeyHandler
39
- */
40
- /** @typedef {(event: import('react').KeyboardEvent<HTMLElement> | KeyboardEvent, character: string, isApple?: () => boolean) => boolean} WPEventKeyHandler */
52
+ export type WPKeyHandler< T > = (
53
+ character: string,
54
+ isApple?: () => boolean
55
+ ) => T;
56
+
57
+ export type WPEventKeyHandler = (
58
+ event: ReactKeyboardEvent< HTMLElement > | KeyboardEvent,
59
+ character: string,
60
+ isApple?: () => boolean
61
+ ) => boolean;
41
62
 
42
- /** @typedef {( isApple: () => boolean ) => WPModifierPart[]} WPModifier */
63
+ export type WPModifier = ( isApple: () => boolean ) => WPModifierPart[];
43
64
 
44
65
  /**
45
66
  * Keycode for BACKSPACE key.
@@ -145,10 +166,10 @@ export { isAppleOS };
145
166
 
146
167
  /**
147
168
  * Capitalise the first character of a string.
148
- * @param {string} string String to capitalise.
149
- * @return {string} Capitalised string.
169
+ * @param string String to capitalise.
170
+ * @return Capitalised string.
150
171
  */
151
- function capitaliseFirstCharacter( string ) {
172
+ function capitaliseFirstCharacter( string: string ): string {
152
173
  return string.length < 2
153
174
  ? string.toUpperCase()
154
175
  : string.charAt( 0 ).toUpperCase() + string.slice( 1 );
@@ -157,29 +178,30 @@ function capitaliseFirstCharacter( string ) {
157
178
  /**
158
179
  * Map the values of an object with a specified callback and return the result object.
159
180
  *
160
- * @template {{ [s: string]: any; } | ArrayLike<any>} T
161
- *
162
- * @param {T} object Object to map values of.
163
- * @param {( value: any ) => any} mapFn Mapping function
181
+ * @template T The object type
182
+ * @template R The return type of the mapping function
164
183
  *
165
- * @return {any} Active modifier constants.
166
- */
167
- function mapValues( object, mapFn ) {
184
+ * @param object Object to map values of.
185
+ * @param mapFn Mapping function to apply to each value.
186
+ * @return Object with the same keys and transformed values.
187
+ */
188
+ function mapValues< T extends Record< string, any >, R >(
189
+ object: T,
190
+ mapFn: ( value: T[ keyof T ] ) => R
191
+ ): Record< keyof T, R > {
168
192
  return Object.fromEntries(
169
193
  Object.entries( object ).map( ( [ key, value ] ) => [
170
194
  key,
171
195
  mapFn( value ),
172
196
  ] )
173
- );
197
+ ) as Record< keyof T, R >;
174
198
  }
175
199
 
176
200
  /**
177
201
  * Object that contains functions that return the available modifier
178
202
  * depending on platform.
179
- *
180
- * @type {WPModifierHandler< ( isApple: () => boolean ) => WPModifierPart[]>}
181
203
  */
182
- export const modifiers = {
204
+ export const modifiers: WPModifierHandler< WPModifier > = {
183
205
  primary: ( _isApple ) => ( _isApple() ? [ COMMAND ] : [ CTRL ] ),
184
206
  primaryShift: ( _isApple ) =>
185
207
  _isApple() ? [ SHIFT, COMMAND ] : [ CTRL, SHIFT ],
@@ -207,17 +229,11 @@ export const modifiers = {
207
229
  * rawShortcut.primary( 'm' )
208
230
  * // "meta+m""
209
231
  * ```
210
- *
211
- * @type {WPModifierHandler<WPKeyHandler<string>>} Keyed map of functions to raw
212
- * shortcuts.
213
232
  */
214
- export const rawShortcut =
233
+ export const rawShortcut: WPModifierHandler< WPKeyHandler< string > > =
215
234
  /* @__PURE__ */
216
- mapValues( modifiers, ( /** @type {WPModifier} */ modifier ) => {
217
- return /** @type {WPKeyHandler<string>} */ (
218
- character,
219
- _isApple = isAppleOS
220
- ) => {
235
+ mapValues( modifiers, ( modifier: WPModifier ) => {
236
+ return ( character: string, _isApple = isAppleOS ) => {
221
237
  return [ ...modifier( _isApple ), character.toLowerCase() ].join(
222
238
  '+'
223
239
  );
@@ -234,40 +250,44 @@ export const rawShortcut =
234
250
  * // [ "⌘", "M" ]
235
251
  * ```
236
252
  *
237
- * @type {WPModifierHandler<WPKeyHandler<string[]>>} Keyed map of functions to
238
- * shortcut sequences.
253
+ * Keyed map of functions to shortcut sequences.
239
254
  */
240
- export const displayShortcutList =
255
+ export const displayShortcutList: WPModifierHandler<
256
+ WPKeyHandler< string[] >
257
+ > =
241
258
  /* @__PURE__ */
242
- mapValues( modifiers, ( /** @type {WPModifier} */ modifier ) => {
243
- return /** @type {WPKeyHandler<string[]>} */ (
244
- character,
245
- _isApple = isAppleOS
246
- ) => {
247
- const isApple = _isApple();
248
- const replacementKeyMap = {
249
- [ ALT ]: isApple ? '' : 'Alt',
250
- [ CTRL ]: isApple ? '' : 'Ctrl', // Make sure ⌃ is the U+2303 UP ARROWHEAD unicode character and not the caret character.
251
- [ COMMAND ]: '',
252
- [ SHIFT ]: isApple ? '⇧' : 'Shift',
259
+ mapValues(
260
+ modifiers,
261
+ ( modifier: WPModifier ): WPKeyHandler< string[] > => {
262
+ return ( character: string, _isApple = isAppleOS ) => {
263
+ const isApple = _isApple();
264
+ const replacementKeyMap = {
265
+ [ ALT ]: isApple ? '⌥' : 'Alt',
266
+ [ CTRL ]: isApple ? '' : 'Ctrl', // Make sure ⌃ is the U+2303 UP ARROWHEAD unicode character and not the caret character.
267
+ [ COMMAND ]: '',
268
+ [ SHIFT ]: isApple ? '' : 'Shift',
269
+ };
270
+
271
+ const modifierKeys = modifier( _isApple ).reduce< string[] >(
272
+ ( accumulator, key ) => {
273
+ const replacementKey = replacementKeyMap[ key ] ?? key;
274
+ // If on the Mac, adhere to platform convention and don't show plus between keys.
275
+ if ( isApple ) {
276
+ return [ ...accumulator, replacementKey ];
277
+ }
278
+
279
+ return [ ...accumulator, replacementKey, '+' ];
280
+ },
281
+ []
282
+ );
283
+
284
+ return [
285
+ ...modifierKeys,
286
+ capitaliseFirstCharacter( character ),
287
+ ];
253
288
  };
254
-
255
- const modifierKeys = modifier( _isApple ).reduce(
256
- ( accumulator, key ) => {
257
- const replacementKey = replacementKeyMap[ key ] ?? key;
258
- // If on the Mac, adhere to platform convention and don't show plus between keys.
259
- if ( isApple ) {
260
- return [ ...accumulator, replacementKey ];
261
- }
262
-
263
- return [ ...accumulator, replacementKey, '+' ];
264
- },
265
- /** @type {string[]} */ ( [] )
266
- );
267
-
268
- return [ ...modifierKeys, capitaliseFirstCharacter( character ) ];
269
- };
270
- } );
289
+ }
290
+ );
271
291
 
272
292
  /**
273
293
  * An object that contains functions to display shortcuts.
@@ -279,18 +299,15 @@ export const displayShortcutList =
279
299
  * // "⌘M"
280
300
  * ```
281
301
  *
282
- * @type {WPModifierHandler<WPKeyHandler<string>>} Keyed map of functions to
283
- * display shortcuts.
302
+ * Keyed map of functions to display shortcuts.
284
303
  */
285
- export const displayShortcut =
304
+ export const displayShortcut: WPModifierHandler< WPKeyHandler< string > > =
286
305
  /* @__PURE__ */
287
306
  mapValues(
288
307
  displayShortcutList,
289
- ( /** @type {WPKeyHandler<string[]>} */ shortcutList ) => {
290
- return /** @type {WPKeyHandler<string>} */ (
291
- character,
292
- _isApple = isAppleOS
293
- ) => shortcutList( character, _isApple ).join( '' );
308
+ ( shortcutList: WPKeyHandler< string[] > ): WPKeyHandler< string > => {
309
+ return ( character: string, _isApple = isAppleOS ) =>
310
+ shortcutList( character, _isApple ).join( '' );
294
311
  }
295
312
  );
296
313
 
@@ -305,19 +322,14 @@ export const displayShortcut =
305
322
  * // "Command + Period"
306
323
  * ```
307
324
  *
308
- * @type {WPModifierHandler<WPKeyHandler<string>>} Keyed map of functions to
309
- * shortcut ARIA labels.
325
+ * Keyed map of functions to shortcut ARIA labels.
310
326
  */
311
- export const shortcutAriaLabel =
327
+ export const shortcutAriaLabel: WPModifierHandler< WPKeyHandler< string > > =
312
328
  /* @__PURE__ */
313
- mapValues( modifiers, ( /** @type {WPModifier} */ modifier ) => {
314
- return /** @type {WPKeyHandler<string>} */ (
315
- character,
316
- _isApple = isAppleOS
317
- ) => {
329
+ mapValues( modifiers, ( modifier: WPModifier ): WPKeyHandler< string > => {
330
+ return ( character: string, _isApple = isAppleOS ) => {
318
331
  const isApple = _isApple();
319
- /** @type {Record<string,string>} */
320
- const replacementKeyMap = {
332
+ const replacementKeyMap: Record< string, string > = {
321
333
  [ SHIFT ]: 'Shift',
322
334
  [ COMMAND ]: isApple ? 'Command' : 'Control',
323
335
  [ CTRL ]: 'Control',
@@ -344,22 +356,17 @@ export const shortcutAriaLabel =
344
356
  * From a given KeyboardEvent, returns an array of active modifier constants for
345
357
  * the event.
346
358
  *
347
- * @param {import('react').KeyboardEvent<HTMLElement> | KeyboardEvent} event Keyboard event.
359
+ * @param event Keyboard event.
348
360
  *
349
- * @return {Array<WPModifierPart>} Active modifier constants.
350
- */
351
- function getEventModifiers( event ) {
352
- return /** @type {WPModifierPart[]} */ ( [
353
- ALT,
354
- CTRL,
355
- COMMAND,
356
- SHIFT,
357
- ] ).filter(
361
+ * @return Active modifier constants.
362
+ */
363
+ function getEventModifiers(
364
+ event: ReactKeyboardEvent< HTMLElement > | KeyboardEvent
365
+ ): WPModifierPart[] {
366
+ return ( [ ALT, CTRL, COMMAND, SHIFT ] as const ).filter(
358
367
  ( key ) =>
359
- event[
360
- /** @type {'altKey' | 'ctrlKey' | 'metaKey' | 'shiftKey'} */ (
361
- `${ key }Key`
362
- )
368
+ ( event as KeyboardEvent )[
369
+ `${ key }Key` as 'altKey' | 'ctrlKey' | 'metaKey' | 'shiftKey'
363
370
  ]
364
371
  );
365
372
  }
@@ -375,21 +382,16 @@ function getEventModifiers( event ) {
375
382
  * // true
376
383
  * ```
377
384
  *
378
- * @type {WPModifierHandler<WPEventKeyHandler>} Keyed map of functions
379
- * to match events.
385
+ * Keyed map of functions to match events.
380
386
  */
381
- export const isKeyboardEvent =
387
+ export const isKeyboardEvent: WPModifierHandler< WPEventKeyHandler > =
382
388
  /* @__PURE__ */
383
- mapValues( modifiers, ( /** @type {WPModifier} */ getModifiers ) => {
384
- return /** @type {WPEventKeyHandler} */ (
385
- event,
386
- character,
387
- _isApple = isAppleOS
388
- ) => {
389
+ mapValues( modifiers, ( getModifiers: WPModifier ): WPEventKeyHandler => {
390
+ return ( event, character, _isApple = isAppleOS ) => {
389
391
  const mods = getModifiers( _isApple );
390
392
  const eventMods = getEventModifiers( event );
391
- /** @type {Record<string,string>} */
392
- const replacementWithShiftKeyMap = {
393
+
394
+ const replacementWithShiftKeyMap: Record< string, string > = {
393
395
  Comma: ',',
394
396
  Backslash: '\\',
395
397
  // Windows returns `\` for both IntlRo and IntlYen.
@@ -411,7 +413,7 @@ export const isKeyboardEvent =
411
413
  let key = event.key.toLowerCase();
412
414
 
413
415
  if ( ! character ) {
414
- return mods.includes( /** @type {WPModifierPart} */ ( key ) );
416
+ return mods.includes( key as WPModifierPart );
415
417
  }
416
418
 
417
419
  if ( event.altKey && character.length === 1 ) {
@@ -1,11 +1,11 @@
1
1
  /**
2
2
  * Return true if platform is MacOS.
3
3
  *
4
- * @param {Window?} _window window object by default; used for DI testing.
4
+ * @param _window window object by default; used for DI testing.
5
5
  *
6
- * @return {boolean} True if MacOS; false otherwise.
6
+ * @return True if MacOS; false otherwise.
7
7
  */
8
- export function isAppleOS( _window = null ) {
8
+ export function isAppleOS( _window?: Window ): boolean {
9
9
  if ( ! _window ) {
10
10
  if ( typeof window === 'undefined' ) {
11
11
  return false;