@wordpress/keyboard-shortcuts 5.44.0 → 5.45.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.
Files changed (77) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +5 -9
  3. package/build/components/shortcut-provider.cjs +7 -3
  4. package/build/components/shortcut-provider.cjs.map +3 -3
  5. package/build/context.cjs +2 -1
  6. package/build/context.cjs.map +3 -3
  7. package/build/hooks/use-shortcut-event-match.cjs +16 -9
  8. package/build/hooks/use-shortcut-event-match.cjs.map +3 -3
  9. package/build/hooks/use-shortcut.cjs +4 -3
  10. package/build/hooks/use-shortcut.cjs.map +3 -3
  11. package/build/index.cjs +2 -1
  12. package/build/index.cjs.map +2 -2
  13. package/build/store/actions.cjs +2 -1
  14. package/build/store/actions.cjs.map +3 -3
  15. package/build/store/index.cjs +2 -1
  16. package/build/store/index.cjs.map +3 -3
  17. package/build/store/reducer.cjs +2 -1
  18. package/build/store/reducer.cjs.map +3 -3
  19. package/build/store/selectors.cjs +14 -2
  20. package/build/store/selectors.cjs.map +3 -3
  21. package/build/store/types.cjs +19 -0
  22. package/build/store/types.cjs.map +7 -0
  23. package/build-module/components/shortcut-provider.mjs +6 -3
  24. package/build-module/components/shortcut-provider.mjs.map +3 -3
  25. package/build-module/context.mjs +1 -1
  26. package/build-module/context.mjs.map +3 -3
  27. package/build-module/hooks/use-shortcut-event-match.mjs +15 -9
  28. package/build-module/hooks/use-shortcut-event-match.mjs.map +3 -3
  29. package/build-module/hooks/use-shortcut.mjs +3 -3
  30. package/build-module/hooks/use-shortcut.mjs.map +3 -3
  31. package/build-module/index.mjs +1 -1
  32. package/build-module/index.mjs.map +1 -1
  33. package/build-module/store/actions.mjs +1 -1
  34. package/build-module/store/actions.mjs.map +3 -3
  35. package/build-module/store/index.mjs +1 -1
  36. package/build-module/store/index.mjs.map +3 -3
  37. package/build-module/store/reducer.mjs +1 -1
  38. package/build-module/store/reducer.mjs.map +3 -3
  39. package/build-module/store/selectors.mjs +13 -2
  40. package/build-module/store/selectors.mjs.map +3 -3
  41. package/build-module/store/types.mjs +1 -0
  42. package/build-module/store/types.mjs.map +7 -0
  43. package/build-types/components/shortcut-provider.d.ts +15 -0
  44. package/build-types/components/shortcut-provider.d.ts.map +1 -0
  45. package/build-types/context.d.ts +8 -0
  46. package/build-types/context.d.ts.map +1 -0
  47. package/build-types/hooks/use-shortcut-event-match.d.ts +8 -0
  48. package/build-types/hooks/use-shortcut-event-match.d.ts.map +1 -0
  49. package/build-types/hooks/use-shortcut.d.ts +14 -0
  50. package/build-types/hooks/use-shortcut.d.ts.map +1 -0
  51. package/build-types/hooks/use-shortcut.native.d.ts +3 -0
  52. package/build-types/hooks/use-shortcut.native.d.ts.map +1 -0
  53. package/build-types/index.d.ts +5 -0
  54. package/build-types/index.d.ts.map +1 -0
  55. package/build-types/store/actions.d.ts +114 -0
  56. package/build-types/store/actions.d.ts.map +1 -0
  57. package/build-types/store/index.d.ts +9 -0
  58. package/build-types/store/index.d.ts.map +1 -0
  59. package/build-types/store/reducer.d.ts +16 -0
  60. package/build-types/store/reducer.d.ts.map +1 -0
  61. package/build-types/store/selectors.d.ts +321 -0
  62. package/build-types/store/selectors.d.ts.map +1 -0
  63. package/build-types/store/types.d.ts +9 -0
  64. package/build-types/store/types.d.ts.map +1 -0
  65. package/package.json +8 -6
  66. package/src/components/{shortcut-provider.js → shortcut-provider.tsx} +16 -6
  67. package/src/{context.js → context.ts} +12 -5
  68. package/src/hooks/use-shortcut-event-match.ts +51 -0
  69. package/src/hooks/{use-shortcut.js → use-shortcut.ts} +15 -11
  70. package/src/store/{actions.js → actions.ts} +21 -19
  71. package/src/store/{index.js → index.ts} +0 -2
  72. package/src/store/{reducer.js → reducer.ts} +13 -4
  73. package/src/store/{selectors.js → selectors.ts} +56 -28
  74. package/src/store/types.ts +10 -0
  75. package/src/hooks/use-shortcut-event-match.js +0 -41
  76. /package/src/hooks/{use-shortcut.native.js → use-shortcut.native.ts} +0 -0
  77. /package/src/{index.js → index.ts} +0 -0
@@ -8,41 +8,57 @@ import {
8
8
  rawShortcut,
9
9
  } from '@wordpress/keycodes';
10
10
 
11
- /** @typedef {import('./actions').WPShortcutKeyCombination} WPShortcutKeyCombination */
11
+ /**
12
+ * Internal dependencies
13
+ */
14
+ import type { ShortcutKeyCombination } from './actions';
15
+
16
+ interface ShortcutState {
17
+ category: string;
18
+ keyCombination: ShortcutKeyCombination;
19
+ aliases?: ShortcutKeyCombination[];
20
+ description: string;
21
+ }
12
22
 
13
- /** @typedef {import('@wordpress/keycodes').WPKeycodeHandlerByModifier} WPKeycodeHandlerByModifier */
23
+ type ShortcutsState = Record< string, ShortcutState >;
14
24
 
15
25
  /**
16
26
  * Shared reference to an empty array for cases where it is important to avoid
17
27
  * returning a new array reference on every invocation.
18
- *
19
- * @type {Array<any>}
20
28
  */
21
- const EMPTY_ARRAY = [];
29
+ const EMPTY_ARRAY: ShortcutKeyCombination[] = [];
22
30
 
23
31
  /**
24
32
  * Shortcut formatting methods.
25
- *
26
- * @property {WPKeycodeHandlerByModifier} display Display formatting.
27
- * @property {WPKeycodeHandlerByModifier} rawShortcut Raw shortcut formatting.
28
- * @property {WPKeycodeHandlerByModifier} ariaLabel ARIA label formatting.
29
33
  */
30
34
  const FORMATTING_METHODS = {
35
+ /**
36
+ * Display formatting.
37
+ */
31
38
  display: displayShortcut,
39
+ /**
40
+ * Raw shortcut formatting.
41
+ */
32
42
  raw: rawShortcut,
43
+ /**
44
+ * ARIA label formatting.
45
+ */
33
46
  ariaLabel: shortcutAriaLabel,
34
47
  };
35
48
 
36
49
  /**
37
50
  * Returns a string representing the key combination.
38
51
  *
39
- * @param {?WPShortcutKeyCombination} shortcut Key combination.
40
- * @param {keyof FORMATTING_METHODS} representation Type of representation
41
- * (display, raw, ariaLabel).
52
+ * @param shortcut Key combination.
53
+ * @param representation Type of representation
54
+ * (display, raw, ariaLabel).
42
55
  *
43
- * @return {?string} Shortcut representation.
56
+ * @return Shortcut representation.
44
57
  */
45
- function getKeyCombinationRepresentation( shortcut, representation ) {
58
+ function getKeyCombinationRepresentation(
59
+ shortcut: ShortcutKeyCombination | null,
60
+ representation: keyof typeof FORMATTING_METHODS
61
+ ): string | null {
46
62
  if ( ! shortcut ) {
47
63
  return null;
48
64
  }
@@ -93,9 +109,12 @@ function getKeyCombinationRepresentation( shortcut, representation ) {
93
109
  * };
94
110
  *```
95
111
  *
96
- * @return {WPShortcutKeyCombination?} Key combination.
112
+ * @return {ShortcutKeyCombination?} Key combination.
97
113
  */
98
- export function getShortcutKeyCombination( state, name ) {
114
+ export function getShortcutKeyCombination(
115
+ state: ShortcutsState,
116
+ name: string
117
+ ): ShortcutKeyCombination | null {
99
118
  return state[ name ] ? state[ name ].keyCombination : null;
100
119
  }
101
120
 
@@ -138,10 +157,10 @@ export function getShortcutKeyCombination( state, name ) {
138
157
  * @return {?string} Shortcut representation.
139
158
  */
140
159
  export function getShortcutRepresentation(
141
- state,
142
- name,
143
- representation = 'display'
144
- ) {
160
+ state: ShortcutsState,
161
+ name: string,
162
+ representation: keyof typeof FORMATTING_METHODS = 'display'
163
+ ): string | null {
145
164
  const shortcut = getShortcutKeyCombination( state, name );
146
165
  return getKeyCombinationRepresentation( shortcut, representation );
147
166
  }
@@ -174,7 +193,10 @@ export function getShortcutRepresentation(
174
193
  *```
175
194
  * @return {?string} Shortcut description.
176
195
  */
177
- export function getShortcutDescription( state, name ) {
196
+ export function getShortcutDescription(
197
+ state: ShortcutsState,
198
+ name: string
199
+ ): string | null {
178
200
  return state[ name ] ? state[ name ].description : null;
179
201
  }
180
202
 
@@ -222,9 +244,12 @@ export function getShortcutDescription( state, name ) {
222
244
  * };
223
245
  *```
224
246
  *
225
- * @return {WPShortcutKeyCombination[]} Key combinations.
247
+ * @return {ShortcutKeyCombination[]} Key combinations.
226
248
  */
227
- export function getShortcutAliases( state, name ) {
249
+ export function getShortcutAliases(
250
+ state: ShortcutsState,
251
+ name: string
252
+ ): ShortcutKeyCombination[] {
228
253
  return state[ name ] && state[ name ].aliases
229
254
  ? state[ name ].aliases
230
255
  : EMPTY_ARRAY;
@@ -277,14 +302,17 @@ export function getShortcutAliases( state, name ) {
277
302
  * };
278
303
  *```
279
304
  *
280
- * @return {WPShortcutKeyCombination[]} Key combinations.
305
+ * @return {ShortcutKeyCombination[]} Key combinations.
281
306
  */
282
307
  export const getAllShortcutKeyCombinations = createSelector(
283
308
  ( state, name ) => {
284
309
  return [
285
310
  getShortcutKeyCombination( state, name ),
286
311
  ...getShortcutAliases( state, name ),
287
- ].filter( Boolean );
312
+ ].filter(
313
+ ( combination ): combination is ShortcutKeyCombination =>
314
+ !! combination
315
+ );
288
316
  },
289
317
  ( state, name ) => [ state[ name ] ]
290
318
  );
@@ -382,10 +410,10 @@ export const getAllShortcutRawKeyCombinations = createSelector(
382
410
  * @return {string[]} Shortcut names.
383
411
  */
384
412
  export const getCategoryShortcuts = createSelector(
385
- ( state, categoryName ) => {
386
- return Object.entries( state )
413
+ ( state: ShortcutsState, categoryName: string ): string[] => {
414
+ return Object.entries< ShortcutState >( state )
387
415
  .filter( ( [ , shortcut ] ) => shortcut.category === categoryName )
388
416
  .map( ( [ name ] ) => name );
389
417
  },
390
- ( state ) => [ state ]
418
+ ( state: ShortcutsState ) => [ state ]
391
419
  );
@@ -0,0 +1,10 @@
1
+ import type { ShortcutKeyCombination } from './actions';
2
+
3
+ export interface ShortcutState {
4
+ category: string;
5
+ keyCombination: ShortcutKeyCombination;
6
+ aliases?: ShortcutKeyCombination[];
7
+ description: string;
8
+ }
9
+
10
+ export type ShortcutsState = Record< string, ShortcutState >;
@@ -1,41 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { useSelect } from '@wordpress/data';
5
- import { isKeyboardEvent } from '@wordpress/keycodes';
6
-
7
- /**
8
- * Internal dependencies
9
- */
10
- import { store as keyboardShortcutsStore } from '../store';
11
-
12
- /**
13
- * Returns a function to check if a keyboard event matches a shortcut name.
14
- *
15
- * @return {Function} A function to check if a keyboard event matches a
16
- * predefined shortcut combination.
17
- */
18
- export default function useShortcutEventMatch() {
19
- const { getAllShortcutKeyCombinations } = useSelect(
20
- keyboardShortcutsStore
21
- );
22
-
23
- /**
24
- * A function to check if a keyboard event matches a predefined shortcut
25
- * combination.
26
- *
27
- * @param {string} name Shortcut name.
28
- * @param {KeyboardEvent} event Event to check.
29
- *
30
- * @return {boolean} True if the event matches any shortcuts, false if not.
31
- */
32
- function isMatch( name, event ) {
33
- return getAllShortcutKeyCombinations( name ).some(
34
- ( { modifier, character } ) => {
35
- return isKeyboardEvent[ modifier ]( event, character );
36
- }
37
- );
38
- }
39
-
40
- return isMatch;
41
- }
File without changes