@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.
- package/CHANGELOG.md +2 -0
- package/README.md +6 -18
- package/build/index.js +29 -54
- package/build/index.js.map +1 -1
- package/build/platform.js +3 -3
- package/build/platform.js.map +1 -1
- package/build-module/index.js +29 -54
- package/build-module/index.js.map +1 -1
- package/build-module/platform.js +3 -3
- package/build-module/platform.js.map +1 -1
- package/build-types/index.d.ts +53 -62
- package/build-types/index.d.ts.map +1 -1
- package/build-types/platform.d.ts +3 -3
- package/build-types/platform.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/{index.js → index.ts} +113 -111
- package/src/{platform.js → platform.ts} +3 -3
- package/src/test/{index.js → index.ts} +143 -100
- package/src/test/platform.ts +56 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/src/test/platform.js +0 -54
|
@@ -19,27 +19,48 @@ import { __ } from '@wordpress/i18n';
|
|
|
19
19
|
*/
|
|
20
20
|
import { isAppleOS } from './platform';
|
|
21
21
|
|
|
22
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* External dependencies
|
|
24
|
+
*/
|
|
25
|
+
import type { KeyboardEvent as ReactKeyboardEvent } from 'react';
|
|
23
26
|
|
|
24
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
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
|
|
149
|
-
* @return
|
|
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
|
|
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
|
-
* @
|
|
166
|
-
|
|
167
|
-
|
|
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, (
|
|
217
|
-
return
|
|
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
|
-
*
|
|
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(
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
_isApple = isAppleOS
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
-
(
|
|
290
|
-
return
|
|
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
|
-
*
|
|
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, (
|
|
314
|
-
return
|
|
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
|
-
|
|
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
|
|
359
|
+
* @param event Keyboard event.
|
|
348
360
|
*
|
|
349
|
-
* @return
|
|
350
|
-
*/
|
|
351
|
-
function getEventModifiers(
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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, (
|
|
384
|
-
return
|
|
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
|
-
|
|
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(
|
|
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
|
|
4
|
+
* @param _window window object by default; used for DI testing.
|
|
5
5
|
*
|
|
6
|
-
* @return
|
|
6
|
+
* @return True if MacOS; false otherwise.
|
|
7
7
|
*/
|
|
8
|
-
export function isAppleOS( _window
|
|
8
|
+
export function isAppleOS( _window?: Window ): boolean {
|
|
9
9
|
if ( ! _window ) {
|
|
10
10
|
if ( typeof window === 'undefined' ) {
|
|
11
11
|
return false;
|