@wordpress/keyboard-shortcuts 3.0.2 → 3.0.6

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 (40) hide show
  1. package/README.md +14 -1
  2. package/build/components/shortcut-provider.js +54 -0
  3. package/build/components/shortcut-provider.js.map +1 -0
  4. package/build/context.js +15 -0
  5. package/build/context.js.map +1 -0
  6. package/build/hooks/use-shortcut-event-match.js +5 -4
  7. package/build/hooks/use-shortcut-event-match.js.map +1 -1
  8. package/build/hooks/use-shortcut.js +34 -15
  9. package/build/hooks/use-shortcut.js.map +1 -1
  10. package/build/index.js +14 -6
  11. package/build/index.js.map +1 -1
  12. package/build/store/actions.js +8 -7
  13. package/build/store/actions.js.map +1 -1
  14. package/build/store/index.js +4 -2
  15. package/build/store/index.js.map +1 -1
  16. package/build/store/reducer.js +4 -1
  17. package/build/store/reducer.js.map +1 -1
  18. package/build/store/selectors.js +12 -5
  19. package/build/store/selectors.js.map +1 -1
  20. package/build-module/components/shortcut-provider.js +44 -0
  21. package/build-module/components/shortcut-provider.js.map +1 -0
  22. package/build-module/context.js +6 -0
  23. package/build-module/context.js.map +1 -0
  24. package/build-module/hooks/use-shortcut-event-match.js +5 -4
  25. package/build-module/hooks/use-shortcut-event-match.js.map +1 -1
  26. package/build-module/hooks/use-shortcut.js +31 -13
  27. package/build-module/hooks/use-shortcut.js.map +1 -1
  28. package/build-module/index.js +1 -0
  29. package/build-module/index.js.map +1 -1
  30. package/build-module/store/actions.js +8 -7
  31. package/build-module/store/actions.js.map +1 -1
  32. package/build-module/store/reducer.js +4 -1
  33. package/build-module/store/reducer.js.map +1 -1
  34. package/build-module/store/selectors.js +9 -2
  35. package/build-module/store/selectors.js.map +1 -1
  36. package/package.json +7 -7
  37. package/src/components/shortcut-provider.js +38 -0
  38. package/src/context.js +6 -0
  39. package/src/hooks/use-shortcut.js +28 -18
  40. package/src/index.js +1 -0
package/README.md CHANGED
@@ -10,12 +10,24 @@ Install the module
10
10
  npm install @wordpress/keyboard-shortcuts --save
11
11
  ```
12
12
 
13
- _This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for ES2015+ such as IE browsers then using [core-js](https://github.com/zloirock/core-js) will add polyfills for these methods._
13
+ _This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for such language features and APIs, you should include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._
14
14
 
15
15
  ## API
16
16
 
17
17
  <!-- START TOKEN(Autogenerated API docs) -->
18
18
 
19
+ ### ShortcutProvider
20
+
21
+ Handles callbacks added to context by `useShortcut`.
22
+
23
+ _Parameters_
24
+
25
+ - _props_ `Object`: Props to pass to `div`.
26
+
27
+ _Returns_
28
+
29
+ - `import('@wordpress/element').WPElement`: Component.
30
+
19
31
  ### store
20
32
 
21
33
  Store definition for the keyboard shortcuts namespace.
@@ -37,6 +49,7 @@ _Parameters_
37
49
  - _name_ `string`: Shortcut name.
38
50
  - _callback_ `Function`: Shortcut callback.
39
51
  - _options_ `Object`: Shortcut options.
52
+ - _options.isDisabled_ `boolean`: Whether to disable to shortut.
40
53
 
41
54
  <!-- END TOKEN(Autogenerated API docs) -->
42
55
 
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.ShortcutProvider = ShortcutProvider;
9
+
10
+ var _element = require("@wordpress/element");
11
+
12
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
+
14
+ var _context = require("../context");
15
+
16
+ /**
17
+ * WordPress dependencies
18
+ */
19
+
20
+ /**
21
+ * Internal dependencies
22
+ */
23
+ const {
24
+ Provider
25
+ } = _context.context;
26
+ /**
27
+ * Handles callbacks added to context by `useShortcut`.
28
+ *
29
+ * @param {Object} props Props to pass to `div`.
30
+ *
31
+ * @return {import('@wordpress/element').WPElement} Component.
32
+ */
33
+
34
+ function ShortcutProvider(props) {
35
+ const keyboardShortcuts = (0, _element.useRef)(new Set());
36
+
37
+ function onKeyDown(event) {
38
+ if (props.onKeyDown) props.onKeyDown(event);
39
+
40
+ for (const keyboardShortcut of keyboardShortcuts.current) {
41
+ keyboardShortcut(event);
42
+ }
43
+ }
44
+ /* eslint-disable jsx-a11y/no-static-element-interactions */
45
+
46
+
47
+ return (0, _element.createElement)(Provider, {
48
+ value: keyboardShortcuts
49
+ }, (0, _element.createElement)("div", (0, _extends2.default)({}, props, {
50
+ onKeyDown: onKeyDown
51
+ })));
52
+ /* eslint-enable jsx-a11y/no-static-element-interactions */
53
+ }
54
+ //# sourceMappingURL=shortcut-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/components/shortcut-provider.js"],"names":["Provider","context","ShortcutProvider","props","keyboardShortcuts","Set","onKeyDown","event","keyboardShortcut","current"],"mappings":";;;;;;;;;AAGA;;;;AAKA;;AARA;AACA;AACA;;AAGA;AACA;AACA;AAGA,MAAM;AAAEA,EAAAA;AAAF,IAAeC,gBAArB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACO,SAASC,gBAAT,CAA2BC,KAA3B,EAAmC;AACzC,QAAMC,iBAAiB,GAAG,qBAAQ,IAAIC,GAAJ,EAAR,CAA1B;;AAEA,WAASC,SAAT,CAAoBC,KAApB,EAA4B;AAC3B,QAAKJ,KAAK,CAACG,SAAX,EAAuBH,KAAK,CAACG,SAAN,CAAiBC,KAAjB;;AAEvB,SAAM,MAAMC,gBAAZ,IAAgCJ,iBAAiB,CAACK,OAAlD,EAA4D;AAC3DD,MAAAA,gBAAgB,CAAED,KAAF,CAAhB;AACA;AACD;AAED;;;AACA,SACC,4BAAC,QAAD;AAAU,IAAA,KAAK,EAAGH;AAAlB,KACC,8DAAUD,KAAV;AAAkB,IAAA,SAAS,EAAGG;AAA9B,KADD,CADD;AAKA;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { context } from '../context';\n\nconst { Provider } = context;\n\n/**\n * Handles callbacks added to context by `useShortcut`.\n *\n * @param {Object} props Props to pass to `div`.\n *\n * @return {import('@wordpress/element').WPElement} Component.\n */\nexport function ShortcutProvider( props ) {\n\tconst keyboardShortcuts = useRef( new Set() );\n\n\tfunction onKeyDown( event ) {\n\t\tif ( props.onKeyDown ) props.onKeyDown( event );\n\n\t\tfor ( const keyboardShortcut of keyboardShortcuts.current ) {\n\t\t\tkeyboardShortcut( event );\n\t\t}\n\t}\n\n\t/* eslint-disable jsx-a11y/no-static-element-interactions */\n\treturn (\n\t\t<Provider value={ keyboardShortcuts }>\n\t\t\t<div { ...props } onKeyDown={ onKeyDown } />\n\t\t</Provider>\n\t);\n\t/* eslint-enable jsx-a11y/no-static-element-interactions */\n}\n"]}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.context = void 0;
7
+
8
+ var _element = require("@wordpress/element");
9
+
10
+ /**
11
+ * WordPress dependencies
12
+ */
13
+ const context = (0, _element.createContext)();
14
+ exports.context = context;
15
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/context.js"],"names":["context"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;AAGO,MAAMA,OAAO,GAAG,6BAAhB","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createContext } from '@wordpress/element';\n\nexport const context = createContext();\n"]}
@@ -40,10 +40,11 @@ function useShortcutEventMatch() {
40
40
  */
41
41
 
42
42
  function isMatch(name, event) {
43
- return getAllShortcutKeyCombinations(name).some(({
44
- modifier,
45
- character
46
- }) => {
43
+ return getAllShortcutKeyCombinations(name).some(_ref => {
44
+ let {
45
+ modifier,
46
+ character
47
+ } = _ref;
47
48
  return _keycodes.isKeyboardEvent[modifier](event, character);
48
49
  });
49
50
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/hooks/use-shortcut-event-match.js"],"names":["useShortcutEventMatch","getAllShortcutKeyCombinations","keyboardShortcutsStore","isMatch","name","event","some","modifier","character","isKeyboardEvent"],"mappings":";;;;;;;AAGA;;AACA;;AAKA;;AATA;AACA;AACA;;AAIA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,qBAAT,GAAiC;AAC/C,QAAM;AAAEC,IAAAA;AAAF,MAAoC,qBACzCC,YADyC,CAA1C;AAIA;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACC,WAASC,OAAT,CAAkBC,IAAlB,EAAwBC,KAAxB,EAAgC;AAC/B,WAAOJ,6BAA6B,CAAEG,IAAF,CAA7B,CAAsCE,IAAtC,CACN,CAAE;AAAEC,MAAAA,QAAF;AAAYC,MAAAA;AAAZ,KAAF,KAA+B;AAC9B,aAAOC,0BAAiBF,QAAjB,EAA6BF,KAA7B,EAAoCG,SAApC,CAAP;AACA,KAHK,CAAP;AAKA;;AAED,SAAOL,OAAP;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { isKeyboardEvent } from '@wordpress/keycodes';\n\n/**\n * Internal dependencies\n */\nimport { store as keyboardShortcutsStore } from '../store';\n\n/**\n * Returns a function to check if a keyboard event matches a shortcut name.\n *\n * @return {Function} A function to to check if a keyboard event matches a\n * predefined shortcut combination.\n */\nexport default function useShortcutEventMatch() {\n\tconst { getAllShortcutKeyCombinations } = useSelect(\n\t\tkeyboardShortcutsStore\n\t);\n\n\t/**\n\t * A function to check if a keyboard event matches a predefined shortcut\n\t * combination.\n\t *\n\t * @param {string} name Shortcut name.\n\t * @param {KeyboardEvent} event Event to check.\n\t *\n\t * @return {boolean} True if the event matches any shortcuts, false if not.\n\t */\n\tfunction isMatch( name, event ) {\n\t\treturn getAllShortcutKeyCombinations( name ).some(\n\t\t\t( { modifier, character } ) => {\n\t\t\t\treturn isKeyboardEvent[ modifier ]( event, character );\n\t\t\t}\n\t\t);\n\t}\n\n\treturn isMatch;\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/hooks/use-shortcut-event-match.js"],"names":["useShortcutEventMatch","getAllShortcutKeyCombinations","keyboardShortcutsStore","isMatch","name","event","some","modifier","character","isKeyboardEvent"],"mappings":";;;;;;;AAGA;;AACA;;AAKA;;AATA;AACA;AACA;;AAIA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,qBAAT,GAAiC;AAC/C,QAAM;AAAEC,IAAAA;AAAF,MAAoC,qBACzCC,YADyC,CAA1C;AAIA;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACC,WAASC,OAAT,CAAkBC,IAAlB,EAAwBC,KAAxB,EAAgC;AAC/B,WAAOJ,6BAA6B,CAAEG,IAAF,CAA7B,CAAsCE,IAAtC,CACN,QAA+B;AAAA,UAA7B;AAAEC,QAAAA,QAAF;AAAYC,QAAAA;AAAZ,OAA6B;AAC9B,aAAOC,0BAAiBF,QAAjB,EAA6BF,KAA7B,EAAoCG,SAApC,CAAP;AACA,KAHK,CAAP;AAKA;;AAED,SAAOL,OAAP;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { isKeyboardEvent } from '@wordpress/keycodes';\n\n/**\n * Internal dependencies\n */\nimport { store as keyboardShortcutsStore } from '../store';\n\n/**\n * Returns a function to check if a keyboard event matches a shortcut name.\n *\n * @return {Function} A function to to check if a keyboard event matches a\n * predefined shortcut combination.\n */\nexport default function useShortcutEventMatch() {\n\tconst { getAllShortcutKeyCombinations } = useSelect(\n\t\tkeyboardShortcutsStore\n\t);\n\n\t/**\n\t * A function to check if a keyboard event matches a predefined shortcut\n\t * combination.\n\t *\n\t * @param {string} name Shortcut name.\n\t * @param {KeyboardEvent} event Event to check.\n\t *\n\t * @return {boolean} True if the event matches any shortcuts, false if not.\n\t */\n\tfunction isMatch( name, event ) {\n\t\treturn getAllShortcutKeyCombinations( name ).some(\n\t\t\t( { modifier, character } ) => {\n\t\t\t\treturn isKeyboardEvent[ modifier ]( event, character );\n\t\t\t}\n\t\t);\n\t}\n\n\treturn isMatch;\n}\n"]}
@@ -1,15 +1,17 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
6
- exports.default = void 0;
8
+ exports.default = useShortcut;
7
9
 
8
- var _data = require("@wordpress/data");
10
+ var _element = require("@wordpress/element");
9
11
 
10
- var _compose = require("@wordpress/compose");
12
+ var _useShortcutEventMatch = _interopRequireDefault(require("./use-shortcut-event-match"));
11
13
 
12
- var _store = require("../store");
14
+ var _context = require("../context");
13
15
 
14
16
  /**
15
17
  * WordPress dependencies
@@ -22,17 +24,34 @@ var _store = require("../store");
22
24
  /**
23
25
  * Attach a keyboard shortcut handler.
24
26
  *
25
- * @param {string} name Shortcut name.
26
- * @param {Function} callback Shortcut callback.
27
- * @param {Object} options Shortcut options.
27
+ * @param {string} name Shortcut name.
28
+ * @param {Function} callback Shortcut callback.
29
+ * @param {Object} options Shortcut options.
30
+ * @param {boolean} options.isDisabled Whether to disable to shortut.
28
31
  */
29
- function useShortcut(name, callback, options) {
30
- const shortcuts = (0, _data.useSelect)(select => {
31
- return select(_store.store).getAllShortcutRawKeyCombinations(name);
32
- }, [name]);
33
- (0, _compose.useKeyboardShortcut)(shortcuts, callback, options);
32
+ function useShortcut(name, callback) {
33
+ let {
34
+ isDisabled
35
+ } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
36
+ const shortcuts = (0, _element.useContext)(_context.context);
37
+ const isMatch = (0, _useShortcutEventMatch.default)();
38
+ const callbackRef = (0, _element.useRef)();
39
+ callbackRef.current = callback;
40
+ (0, _element.useEffect)(() => {
41
+ if (isDisabled) {
42
+ return;
43
+ }
44
+
45
+ function _callback(event) {
46
+ if (isMatch(name, event)) {
47
+ callbackRef.current(event);
48
+ }
49
+ }
50
+
51
+ shortcuts.current.add(_callback);
52
+ return () => {
53
+ shortcuts.current.delete(_callback);
54
+ };
55
+ }, [name, isDisabled]);
34
56
  }
35
-
36
- var _default = useShortcut;
37
- exports.default = _default;
38
57
  //# sourceMappingURL=use-shortcut.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/hooks/use-shortcut.js"],"names":["useShortcut","name","callback","options","shortcuts","select","keyboardShortcutsStore","getAllShortcutRawKeyCombinations"],"mappings":";;;;;;;AAGA;;AACA;;AAKA;;AATA;AACA;AACA;;AAIA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,WAAT,CAAsBC,IAAtB,EAA4BC,QAA5B,EAAsCC,OAAtC,EAAgD;AAC/C,QAAMC,SAAS,GAAG,qBACfC,MAAF,IAAc;AACb,WAAOA,MAAM,CACZC,YADY,CAAN,CAELC,gCAFK,CAE6BN,IAF7B,CAAP;AAGA,GALgB,EAMjB,CAAEA,IAAF,CANiB,CAAlB;AASA,oCAAqBG,SAArB,EAAgCF,QAAhC,EAA0CC,OAA1C;AACA;;eAEcH,W","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { useKeyboardShortcut } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport { store as keyboardShortcutsStore } from '../store';\n\n/**\n * Attach a keyboard shortcut handler.\n *\n * @param {string} name Shortcut name.\n * @param {Function} callback Shortcut callback.\n * @param {Object} options Shortcut options.\n */\nfunction useShortcut( name, callback, options ) {\n\tconst shortcuts = useSelect(\n\t\t( select ) => {\n\t\t\treturn select(\n\t\t\t\tkeyboardShortcutsStore\n\t\t\t).getAllShortcutRawKeyCombinations( name );\n\t\t},\n\t\t[ name ]\n\t);\n\n\tuseKeyboardShortcut( shortcuts, callback, options );\n}\n\nexport default useShortcut;\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/hooks/use-shortcut.js"],"names":["useShortcut","name","callback","isDisabled","shortcuts","context","isMatch","callbackRef","current","_callback","event","add","delete"],"mappings":";;;;;;;;;AAGA;;AAKA;;AACA;;AATA;AACA;AACA;;AAGA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,WAAT,CAAsBC,IAAtB,EAA4BC,QAA5B,EAA4D;AAAA,MAAtB;AAAEC,IAAAA;AAAF,GAAsB,uEAAL,EAAK;AAC1E,QAAMC,SAAS,GAAG,yBAAYC,gBAAZ,CAAlB;AACA,QAAMC,OAAO,GAAG,qCAAhB;AACA,QAAMC,WAAW,GAAG,sBAApB;AACAA,EAAAA,WAAW,CAACC,OAAZ,GAAsBN,QAAtB;AAEA,0BAAW,MAAM;AAChB,QAAKC,UAAL,EAAkB;AACjB;AACA;;AAED,aAASM,SAAT,CAAoBC,KAApB,EAA4B;AAC3B,UAAKJ,OAAO,CAAEL,IAAF,EAAQS,KAAR,CAAZ,EAA8B;AAC7BH,QAAAA,WAAW,CAACC,OAAZ,CAAqBE,KAArB;AACA;AACD;;AAEDN,IAAAA,SAAS,CAACI,OAAV,CAAkBG,GAAlB,CAAuBF,SAAvB;AACA,WAAO,MAAM;AACZL,MAAAA,SAAS,CAACI,OAAV,CAAkBI,MAAlB,CAA0BH,SAA1B;AACA,KAFD;AAGA,GAfD,EAeG,CAAER,IAAF,EAAQE,UAAR,CAfH;AAgBA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useContext, useEffect, useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport useShortcutEventMatch from './use-shortcut-event-match';\nimport { context } from '../context';\n\n/**\n * Attach a keyboard shortcut handler.\n *\n * @param {string} name Shortcut name.\n * @param {Function} callback Shortcut callback.\n * @param {Object} options Shortcut options.\n * @param {boolean} options.isDisabled Whether to disable to shortut.\n */\nexport default function useShortcut( name, callback, { isDisabled } = {} ) {\n\tconst shortcuts = useContext( context );\n\tconst isMatch = useShortcutEventMatch();\n\tconst callbackRef = useRef();\n\tcallbackRef.current = callback;\n\n\tuseEffect( () => {\n\t\tif ( isDisabled ) {\n\t\t\treturn;\n\t\t}\n\n\t\tfunction _callback( event ) {\n\t\t\tif ( isMatch( name, event ) ) {\n\t\t\t\tcallbackRef.current( event );\n\t\t\t}\n\t\t}\n\n\t\tshortcuts.current.add( _callback );\n\t\treturn () => {\n\t\t\tshortcuts.current.delete( _callback );\n\t\t};\n\t}, [ name, isDisabled ] );\n}\n"]}
package/build/index.js CHANGED
@@ -5,22 +5,28 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- Object.defineProperty(exports, "store", {
8
+ Object.defineProperty(exports, "ShortcutProvider", {
9
9
  enumerable: true,
10
10
  get: function () {
11
- return _store.store;
11
+ return _shortcutProvider.ShortcutProvider;
12
12
  }
13
13
  });
14
- Object.defineProperty(exports, "useShortcut", {
14
+ Object.defineProperty(exports, "__unstableUseShortcutEventMatch", {
15
15
  enumerable: true,
16
16
  get: function () {
17
- return _useShortcut.default;
17
+ return _useShortcutEventMatch.default;
18
18
  }
19
19
  });
20
- Object.defineProperty(exports, "__unstableUseShortcutEventMatch", {
20
+ Object.defineProperty(exports, "store", {
21
21
  enumerable: true,
22
22
  get: function () {
23
- return _useShortcutEventMatch.default;
23
+ return _store.store;
24
+ }
25
+ });
26
+ Object.defineProperty(exports, "useShortcut", {
27
+ enumerable: true,
28
+ get: function () {
29
+ return _useShortcut.default;
24
30
  }
25
31
  });
26
32
 
@@ -28,5 +34,7 @@ var _store = require("./store");
28
34
 
29
35
  var _useShortcut = _interopRequireDefault(require("./hooks/use-shortcut"));
30
36
 
37
+ var _shortcutProvider = require("./components/shortcut-provider");
38
+
31
39
  var _useShortcutEventMatch = _interopRequireDefault(require("./hooks/use-shortcut-event-match"));
32
40
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA","sourcesContent":["export { store } from './store';\nexport { default as useShortcut } from './hooks/use-shortcut';\nexport { default as __unstableUseShortcutEventMatch } from './hooks/use-shortcut-event-match';\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AACA","sourcesContent":["export { store } from './store';\nexport { default as useShortcut } from './hooks/use-shortcut';\nexport { ShortcutProvider } from './components/shortcut-provider';\nexport { default as __unstableUseShortcutEventMatch } from './hooks/use-shortcut-event-match';\n"]}
@@ -36,13 +36,14 @@ exports.unregisterShortcut = unregisterShortcut;
36
36
  *
37
37
  * @return {Object} action.
38
38
  */
39
- function registerShortcut({
40
- name,
41
- category,
42
- description,
43
- keyCombination,
44
- aliases
45
- }) {
39
+ function registerShortcut(_ref) {
40
+ let {
41
+ name,
42
+ category,
43
+ description,
44
+ keyCombination,
45
+ aliases
46
+ } = _ref;
46
47
  return {
47
48
  type: 'REGISTER_SHORTCUT',
48
49
  name,
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/actions.js"],"names":["registerShortcut","name","category","description","keyCombination","aliases","type","unregisterShortcut"],"mappings":";;;;;;;;AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,gBAAT,CAA2B;AACjCC,EAAAA,IADiC;AAEjCC,EAAAA,QAFiC;AAGjCC,EAAAA,WAHiC;AAIjCC,EAAAA,cAJiC;AAKjCC,EAAAA;AALiC,CAA3B,EAMH;AACH,SAAO;AACNC,IAAAA,IAAI,EAAE,mBADA;AAENL,IAAAA,IAFM;AAGNC,IAAAA,QAHM;AAINE,IAAAA,cAJM;AAKNC,IAAAA,OALM;AAMNF,IAAAA;AANM,GAAP;AAQA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASI,kBAAT,CAA6BN,IAA7B,EAAoC;AAC1C,SAAO;AACNK,IAAAA,IAAI,EAAE,qBADA;AAENL,IAAAA;AAFM,GAAP;AAIA","sourcesContent":["/** @typedef {import('@wordpress/keycodes').WPKeycodeModifier} WPKeycodeModifier */\n\n/**\n * Keyboard key combination.\n *\n * @typedef {Object} WPShortcutKeyCombination\n *\n * @property {string} character Character.\n * @property {WPKeycodeModifier|undefined} modifier Modifier.\n */\n\n/**\n * Configuration of a registered keyboard shortcut.\n *\n * @typedef {Object} WPShortcutConfig\n *\n * @property {string} name Shortcut name.\n * @property {string} category Shortcut category.\n * @property {string} description Shortcut description.\n * @property {WPShortcutKeyCombination} keyCombination Shortcut key combination.\n * @property {WPShortcutKeyCombination[]} [aliases] Shortcut aliases.\n */\n\n/**\n * Returns an action object used to register a new keyboard shortcut.\n *\n * @param {WPShortcutConfig} config Shortcut config.\n *\n * @return {Object} action.\n */\nexport function registerShortcut( {\n\tname,\n\tcategory,\n\tdescription,\n\tkeyCombination,\n\taliases,\n} ) {\n\treturn {\n\t\ttype: 'REGISTER_SHORTCUT',\n\t\tname,\n\t\tcategory,\n\t\tkeyCombination,\n\t\taliases,\n\t\tdescription,\n\t};\n}\n\n/**\n * Returns an action object used to unregister a keyboard shortcut.\n *\n * @param {string} name Shortcut name.\n *\n * @return {Object} action.\n */\nexport function unregisterShortcut( name ) {\n\treturn {\n\t\ttype: 'UNREGISTER_SHORTCUT',\n\t\tname,\n\t};\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/actions.js"],"names":["registerShortcut","name","category","description","keyCombination","aliases","type","unregisterShortcut"],"mappings":";;;;;;;;AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,gBAAT,OAMH;AAAA,MAN8B;AACjCC,IAAAA,IADiC;AAEjCC,IAAAA,QAFiC;AAGjCC,IAAAA,WAHiC;AAIjCC,IAAAA,cAJiC;AAKjCC,IAAAA;AALiC,GAM9B;AACH,SAAO;AACNC,IAAAA,IAAI,EAAE,mBADA;AAENL,IAAAA,IAFM;AAGNC,IAAAA,QAHM;AAINE,IAAAA,cAJM;AAKNC,IAAAA,OALM;AAMNF,IAAAA;AANM,GAAP;AAQA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASI,kBAAT,CAA6BN,IAA7B,EAAoC;AAC1C,SAAO;AACNK,IAAAA,IAAI,EAAE,qBADA;AAENL,IAAAA;AAFM,GAAP;AAIA","sourcesContent":["/** @typedef {import('@wordpress/keycodes').WPKeycodeModifier} WPKeycodeModifier */\n\n/**\n * Keyboard key combination.\n *\n * @typedef {Object} WPShortcutKeyCombination\n *\n * @property {string} character Character.\n * @property {WPKeycodeModifier|undefined} modifier Modifier.\n */\n\n/**\n * Configuration of a registered keyboard shortcut.\n *\n * @typedef {Object} WPShortcutConfig\n *\n * @property {string} name Shortcut name.\n * @property {string} category Shortcut category.\n * @property {string} description Shortcut description.\n * @property {WPShortcutKeyCombination} keyCombination Shortcut key combination.\n * @property {WPShortcutKeyCombination[]} [aliases] Shortcut aliases.\n */\n\n/**\n * Returns an action object used to register a new keyboard shortcut.\n *\n * @param {WPShortcutConfig} config Shortcut config.\n *\n * @return {Object} action.\n */\nexport function registerShortcut( {\n\tname,\n\tcategory,\n\tdescription,\n\tkeyCombination,\n\taliases,\n} ) {\n\treturn {\n\t\ttype: 'REGISTER_SHORTCUT',\n\t\tname,\n\t\tcategory,\n\t\tkeyCombination,\n\t\taliases,\n\t\tdescription,\n\t};\n}\n\n/**\n * Returns an action object used to unregister a keyboard shortcut.\n *\n * @param {string} name Shortcut name.\n *\n * @return {Object} action.\n */\nexport function unregisterShortcut( name ) {\n\treturn {\n\t\ttype: 'UNREGISTER_SHORTCUT',\n\t\tname,\n\t};\n}\n"]}
@@ -1,7 +1,5 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
-
5
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
4
 
7
5
  Object.defineProperty(exports, "__esModule", {
@@ -17,6 +15,10 @@ var actions = _interopRequireWildcard(require("./actions"));
17
15
 
18
16
  var selectors = _interopRequireWildcard(require("./selectors"));
19
17
 
18
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
19
+
20
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
+
20
22
  /**
21
23
  * WordPress dependencies
22
24
  */
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/index.js"],"names":["STORE_NAME","store","reducer","actions","selectors"],"mappings":";;;;;;;;;;;AAGA;;AAKA;;AACA;;AACA;;AAVA;AACA;AACA;;AAGA;AACA;AACA;AAKA,MAAMA,UAAU,GAAG,yBAAnB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACO,MAAMC,KAAK,GAAG,4BAAkBD,UAAlB,EAA8B;AAClDE,EAAAA,OAAO,EAAPA,gBADkD;AAElDC,EAAAA,OAFkD;AAGlDC,EAAAA;AAHkD,CAA9B,CAAd;;AAMP,oBAAUH,KAAV","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createReduxStore, register } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport reducer from './reducer';\nimport * as actions from './actions';\nimport * as selectors from './selectors';\n\nconst STORE_NAME = 'core/keyboard-shortcuts';\n\n/**\n * Store definition for the keyboard shortcuts namespace.\n *\n * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore\n *\n * @type {Object}\n */\nexport const store = createReduxStore( STORE_NAME, {\n\treducer,\n\tactions,\n\tselectors,\n} );\n\nregister( store );\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/index.js"],"names":["STORE_NAME","store","reducer","actions","selectors"],"mappings":";;;;;;;;;AAGA;;AAKA;;AACA;;AACA;;;;;;AAVA;AACA;AACA;;AAGA;AACA;AACA;AAKA,MAAMA,UAAU,GAAG,yBAAnB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACO,MAAMC,KAAK,GAAG,4BAAkBD,UAAlB,EAA8B;AAClDE,EAAAA,OAAO,EAAPA,gBADkD;AAElDC,EAAAA,OAFkD;AAGlDC,EAAAA;AAHkD,CAA9B,CAAd;;AAMP,oBAAUH,KAAV","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createReduxStore, register } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport reducer from './reducer';\nimport * as actions from './actions';\nimport * as selectors from './selectors';\n\nconst STORE_NAME = 'core/keyboard-shortcuts';\n\n/**\n * Store definition for the keyboard shortcuts namespace.\n *\n * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore\n *\n * @type {Object}\n */\nexport const store = createReduxStore( STORE_NAME, {\n\treducer,\n\tactions,\n\tselectors,\n} );\n\nregister( store );\n"]}
@@ -19,7 +19,10 @@ var _lodash = require("lodash");
19
19
  *
20
20
  * @return {Object} Updated state.
21
21
  */
22
- function reducer(state = {}, action) {
22
+ function reducer() {
23
+ let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
24
+ let action = arguments.length > 1 ? arguments[1] : undefined;
25
+
23
26
  switch (action.type) {
24
27
  case 'REGISTER_SHORTCUT':
25
28
  return { ...state,
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/reducer.js"],"names":["reducer","state","action","type","name","category","keyCombination","aliases","description"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,OAAT,CAAkBC,KAAK,GAAG,EAA1B,EAA8BC,MAA9B,EAAuC;AACtC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,mBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,IAAT,GAAiB;AAChBC,UAAAA,QAAQ,EAAEH,MAAM,CAACG,QADD;AAEhBC,UAAAA,cAAc,EAAEJ,MAAM,CAACI,cAFP;AAGhBC,UAAAA,OAAO,EAAEL,MAAM,CAACK,OAHA;AAIhBC,UAAAA,WAAW,EAAEN,MAAM,CAACM;AAJJ;AAFX,OAAP;;AASD,SAAK,qBAAL;AACC,aAAO,kBAAMP,KAAN,EAAaC,MAAM,CAACE,IAApB,CAAP;AAZF;;AAeA,SAAOH,KAAP;AACA;;eAEcD,O","sourcesContent":["/**\n * External dependencies\n */\nimport { omit } from 'lodash';\n\n/**\n * Reducer returning the registered shortcuts\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction reducer( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_SHORTCUT':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.name ]: {\n\t\t\t\t\tcategory: action.category,\n\t\t\t\t\tkeyCombination: action.keyCombination,\n\t\t\t\t\taliases: action.aliases,\n\t\t\t\t\tdescription: action.description,\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_SHORTCUT':\n\t\t\treturn omit( state, action.name );\n\t}\n\n\treturn state;\n}\n\nexport default reducer;\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/reducer.js"],"names":["reducer","state","action","type","name","category","keyCombination","aliases","description"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,OAAT,GAAuC;AAAA,MAArBC,KAAqB,uEAAb,EAAa;AAAA,MAATC,MAAS;;AACtC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,mBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,IAAT,GAAiB;AAChBC,UAAAA,QAAQ,EAAEH,MAAM,CAACG,QADD;AAEhBC,UAAAA,cAAc,EAAEJ,MAAM,CAACI,cAFP;AAGhBC,UAAAA,OAAO,EAAEL,MAAM,CAACK,OAHA;AAIhBC,UAAAA,WAAW,EAAEN,MAAM,CAACM;AAJJ;AAFX,OAAP;;AASD,SAAK,qBAAL;AACC,aAAO,kBAAMP,KAAN,EAAaC,MAAM,CAACE,IAApB,CAAP;AAZF;;AAeA,SAAOH,KAAP;AACA;;eAEcD,O","sourcesContent":["/**\n * External dependencies\n */\nimport { omit } from 'lodash';\n\n/**\n * Reducer returning the registered shortcuts\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction reducer( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_SHORTCUT':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.name ]: {\n\t\t\t\t\tcategory: action.category,\n\t\t\t\t\tkeyCombination: action.keyCombination,\n\t\t\t\t\taliases: action.aliases,\n\t\t\t\t\tdescription: action.description,\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_SHORTCUT':\n\t\t\treturn omit( state, action.name );\n\t}\n\n\treturn state;\n}\n\nexport default reducer;\n"]}
@@ -5,11 +5,11 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
+ exports.getCategoryShortcuts = exports.getAllShortcutRawKeyCombinations = exports.getAllShortcutKeyCombinations = void 0;
9
+ exports.getShortcutAliases = getShortcutAliases;
10
+ exports.getShortcutDescription = getShortcutDescription;
8
11
  exports.getShortcutKeyCombination = getShortcutKeyCombination;
9
12
  exports.getShortcutRepresentation = getShortcutRepresentation;
10
- exports.getShortcutDescription = getShortcutDescription;
11
- exports.getShortcutAliases = getShortcutAliases;
12
- exports.getCategoryShortcuts = exports.getAllShortcutRawKeyCombinations = exports.getAllShortcutKeyCombinations = void 0;
13
13
 
14
14
  var _rememo = _interopRequireDefault(require("rememo"));
15
15
 
@@ -91,7 +91,8 @@ function getShortcutKeyCombination(state, name) {
91
91
  */
92
92
 
93
93
 
94
- function getShortcutRepresentation(state, name, representation = 'display') {
94
+ function getShortcutRepresentation(state, name) {
95
+ let representation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'display';
95
96
  const shortcut = getShortcutKeyCombination(state, name);
96
97
  return getKeyCombinationRepresentation(shortcut, representation);
97
98
  }
@@ -149,7 +150,13 @@ const getAllShortcutRawKeyCombinations = (0, _rememo.default)((state, name) => {
149
150
 
150
151
  exports.getAllShortcutRawKeyCombinations = getAllShortcutRawKeyCombinations;
151
152
  const getCategoryShortcuts = (0, _rememo.default)((state, categoryName) => {
152
- return Object.entries(state).filter(([, shortcut]) => shortcut.category === categoryName).map(([name]) => name);
153
+ return Object.entries(state).filter(_ref => {
154
+ let [, shortcut] = _ref;
155
+ return shortcut.category === categoryName;
156
+ }).map(_ref2 => {
157
+ let [name] = _ref2;
158
+ return name;
159
+ });
153
160
  }, state => [state]);
154
161
  exports.getCategoryShortcuts = getCategoryShortcuts;
155
162
  //# sourceMappingURL=selectors.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/selectors.js"],"names":["EMPTY_ARRAY","FORMATTING_METHODS","display","displayShortcut","raw","rawShortcut","ariaLabel","shortcutAriaLabel","getKeyCombinationRepresentation","shortcut","representation","modifier","character","getShortcutKeyCombination","state","name","keyCombination","getShortcutRepresentation","getShortcutDescription","description","getShortcutAliases","aliases","getAllShortcutKeyCombinations","getAllShortcutRawKeyCombinations","map","combination","getCategoryShortcuts","categoryName","Object","entries","filter","category"],"mappings":"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","sourcesContent":["/**\n * External dependencies\n */\nimport createSelector from 'rememo';\nimport { compact } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tdisplayShortcut,\n\tshortcutAriaLabel,\n\trawShortcut,\n} from '@wordpress/keycodes';\n\n/** @typedef {import('./actions').WPShortcutKeyCombination} WPShortcutKeyCombination */\n\n/** @typedef {import('@wordpress/keycodes').WPKeycodeHandlerByModifier} WPKeycodeHandlerByModifier */\n\n/**\n * Shared reference to an empty array for cases where it is important to avoid\n * returning a new array reference on every invocation.\n *\n * @type {Array<any>}\n */\nconst EMPTY_ARRAY = [];\n\n/**\n * Shortcut formatting methods.\n *\n * @property {WPKeycodeHandlerByModifier} display Display formatting.\n * @property {WPKeycodeHandlerByModifier} rawShortcut Raw shortcut formatting.\n * @property {WPKeycodeHandlerByModifier} ariaLabel ARIA label formatting.\n */\nconst FORMATTING_METHODS = {\n\tdisplay: displayShortcut,\n\traw: rawShortcut,\n\tariaLabel: shortcutAriaLabel,\n};\n\n/**\n * Returns a string representing the key combination.\n *\n * @param {?WPShortcutKeyCombination} shortcut Key combination.\n * @param {keyof FORMATTING_METHODS} representation Type of representation\n * (display, raw, ariaLabel).\n *\n * @return {string?} Shortcut representation.\n */\nfunction getKeyCombinationRepresentation( shortcut, representation ) {\n\tif ( ! shortcut ) {\n\t\treturn null;\n\t}\n\n\treturn shortcut.modifier\n\t\t? FORMATTING_METHODS[ representation ][ shortcut.modifier ](\n\t\t\t\tshortcut.character\n\t\t )\n\t\t: shortcut.character;\n}\n\n/**\n * Returns the main key combination for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {WPShortcutKeyCombination?} Key combination.\n */\nexport function getShortcutKeyCombination( state, name ) {\n\treturn state[ name ] ? state[ name ].keyCombination : null;\n}\n\n/**\n * Returns a string representing the main key combination for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n * @param {keyof FORMATTING_METHODS} representation Type of representation\n * (display, raw, ariaLabel).\n *\n * @return {string?} Shortcut representation.\n */\nexport function getShortcutRepresentation(\n\tstate,\n\tname,\n\trepresentation = 'display'\n) {\n\tconst shortcut = getShortcutKeyCombination( state, name );\n\treturn getKeyCombinationRepresentation( shortcut, representation );\n}\n\n/**\n * Returns the shortcut description given its name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {string?} Shortcut description.\n */\nexport function getShortcutDescription( state, name ) {\n\treturn state[ name ] ? state[ name ].description : null;\n}\n\n/**\n * Returns the aliases for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {WPShortcutKeyCombination[]} Key combinations.\n */\nexport function getShortcutAliases( state, name ) {\n\treturn state[ name ] && state[ name ].aliases\n\t\t? state[ name ].aliases\n\t\t: EMPTY_ARRAY;\n}\n\nexport const getAllShortcutKeyCombinations = createSelector(\n\t( state, name ) => {\n\t\treturn compact( [\n\t\t\tgetShortcutKeyCombination( state, name ),\n\t\t\t...getShortcutAliases( state, name ),\n\t\t] );\n\t},\n\t( state, name ) => [ state[ name ] ]\n);\n\n/**\n * Returns the raw representation of all the keyboard combinations of a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {string[]} Shortcuts.\n */\nexport const getAllShortcutRawKeyCombinations = createSelector(\n\t( state, name ) => {\n\t\treturn getAllShortcutKeyCombinations(\n\t\t\tstate,\n\t\t\tname\n\t\t).map( ( combination ) =>\n\t\t\tgetKeyCombinationRepresentation( combination, 'raw' )\n\t\t);\n\t},\n\t( state, name ) => [ state[ name ] ]\n);\n\n/**\n * Returns the shortcut names list for a given category name.\n *\n * @param {Object} state Global state.\n * @param {string} name Category name.\n *\n * @return {string[]} Shortcut names.\n */\nexport const getCategoryShortcuts = createSelector(\n\t( state, categoryName ) => {\n\t\treturn Object.entries( state )\n\t\t\t.filter( ( [ , shortcut ] ) => shortcut.category === categoryName )\n\t\t\t.map( ( [ name ] ) => name );\n\t},\n\t( state ) => [ state ]\n);\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/selectors.js"],"names":["EMPTY_ARRAY","FORMATTING_METHODS","display","displayShortcut","raw","rawShortcut","ariaLabel","shortcutAriaLabel","getKeyCombinationRepresentation","shortcut","representation","modifier","character","getShortcutKeyCombination","state","name","keyCombination","getShortcutRepresentation","getShortcutDescription","description","getShortcutAliases","aliases","getAllShortcutKeyCombinations","getAllShortcutRawKeyCombinations","map","combination","getCategoryShortcuts","categoryName","Object","entries","filter","category"],"mappings":";;;;;;;;;;;;;AAGA;;AACA;;AAKA;;AATA;AACA;AACA;;AAIA;AACA;AACA;;AAOA;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,WAAW,GAAG,EAApB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,kBAAkB,GAAG;AAC1BC,EAAAA,OAAO,EAAEC,yBADiB;AAE1BC,EAAAA,GAAG,EAAEC,qBAFqB;AAG1BC,EAAAA,SAAS,EAAEC;AAHe,CAA3B;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,+BAAT,CAA0CC,QAA1C,EAAoDC,cAApD,EAAqE;AACpE,MAAK,CAAED,QAAP,EAAkB;AACjB,WAAO,IAAP;AACA;;AAED,SAAOA,QAAQ,CAACE,QAAT,GACJV,kBAAkB,CAAES,cAAF,CAAlB,CAAsCD,QAAQ,CAACE,QAA/C,EACAF,QAAQ,CAACG,SADT,CADI,GAIJH,QAAQ,CAACG,SAJZ;AAKA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASC,yBAAT,CAAoCC,KAApC,EAA2CC,IAA3C,EAAkD;AACxD,SAAOD,KAAK,CAAEC,IAAF,CAAL,GAAgBD,KAAK,CAAEC,IAAF,CAAL,CAAcC,cAA9B,GAA+C,IAAtD;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASC,yBAAT,CACNH,KADM,EAENC,IAFM,EAIL;AAAA,MADDL,cACC,uEADgB,SAChB;AACD,QAAMD,QAAQ,GAAGI,yBAAyB,CAAEC,KAAF,EAASC,IAAT,CAA1C;AACA,SAAOP,+BAA+B,CAAEC,QAAF,EAAYC,cAAZ,CAAtC;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASQ,sBAAT,CAAiCJ,KAAjC,EAAwCC,IAAxC,EAA+C;AACrD,SAAOD,KAAK,CAAEC,IAAF,CAAL,GAAgBD,KAAK,CAAEC,IAAF,CAAL,CAAcI,WAA9B,GAA4C,IAAnD;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASC,kBAAT,CAA6BN,KAA7B,EAAoCC,IAApC,EAA2C;AACjD,SAAOD,KAAK,CAAEC,IAAF,CAAL,IAAiBD,KAAK,CAAEC,IAAF,CAAL,CAAcM,OAA/B,GACJP,KAAK,CAAEC,IAAF,CAAL,CAAcM,OADV,GAEJrB,WAFH;AAGA;;AAEM,MAAMsB,6BAA6B,GAAG,qBAC5C,CAAER,KAAF,EAASC,IAAT,KAAmB;AAClB,SAAO,qBAAS,CACfF,yBAAyB,CAAEC,KAAF,EAASC,IAAT,CADV,EAEf,GAAGK,kBAAkB,CAAEN,KAAF,EAASC,IAAT,CAFN,CAAT,CAAP;AAIA,CAN2C,EAO5C,CAAED,KAAF,EAASC,IAAT,KAAmB,CAAED,KAAK,CAAEC,IAAF,CAAP,CAPyB,CAAtC;AAUP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,MAAMQ,gCAAgC,GAAG,qBAC/C,CAAET,KAAF,EAASC,IAAT,KAAmB;AAClB,SAAOO,6BAA6B,CACnCR,KADmC,EAEnCC,IAFmC,CAA7B,CAGLS,GAHK,CAGEC,WAAF,IACNjB,+BAA+B,CAAEiB,WAAF,EAAe,KAAf,CAJzB,CAAP;AAMA,CAR8C,EAS/C,CAAEX,KAAF,EAASC,IAAT,KAAmB,CAAED,KAAK,CAAEC,IAAF,CAAP,CAT4B,CAAzC;AAYP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,MAAMW,oBAAoB,GAAG,qBACnC,CAAEZ,KAAF,EAASa,YAAT,KAA2B;AAC1B,SAAOC,MAAM,CAACC,OAAP,CAAgBf,KAAhB,EACLgB,MADK,CACG;AAAA,QAAE,GAAIrB,QAAJ,CAAF;AAAA,WAAsBA,QAAQ,CAACsB,QAAT,KAAsBJ,YAA5C;AAAA,GADH,EAELH,GAFK,CAEA;AAAA,QAAE,CAAET,IAAF,CAAF;AAAA,WAAgBA,IAAhB;AAAA,GAFA,CAAP;AAGA,CALkC,EAMjCD,KAAF,IAAa,CAAEA,KAAF,CANsB,CAA7B","sourcesContent":["/**\n * External dependencies\n */\nimport createSelector from 'rememo';\nimport { compact } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tdisplayShortcut,\n\tshortcutAriaLabel,\n\trawShortcut,\n} from '@wordpress/keycodes';\n\n/** @typedef {import('./actions').WPShortcutKeyCombination} WPShortcutKeyCombination */\n\n/** @typedef {import('@wordpress/keycodes').WPKeycodeHandlerByModifier} WPKeycodeHandlerByModifier */\n\n/**\n * Shared reference to an empty array for cases where it is important to avoid\n * returning a new array reference on every invocation.\n *\n * @type {Array<any>}\n */\nconst EMPTY_ARRAY = [];\n\n/**\n * Shortcut formatting methods.\n *\n * @property {WPKeycodeHandlerByModifier} display Display formatting.\n * @property {WPKeycodeHandlerByModifier} rawShortcut Raw shortcut formatting.\n * @property {WPKeycodeHandlerByModifier} ariaLabel ARIA label formatting.\n */\nconst FORMATTING_METHODS = {\n\tdisplay: displayShortcut,\n\traw: rawShortcut,\n\tariaLabel: shortcutAriaLabel,\n};\n\n/**\n * Returns a string representing the key combination.\n *\n * @param {?WPShortcutKeyCombination} shortcut Key combination.\n * @param {keyof FORMATTING_METHODS} representation Type of representation\n * (display, raw, ariaLabel).\n *\n * @return {string?} Shortcut representation.\n */\nfunction getKeyCombinationRepresentation( shortcut, representation ) {\n\tif ( ! shortcut ) {\n\t\treturn null;\n\t}\n\n\treturn shortcut.modifier\n\t\t? FORMATTING_METHODS[ representation ][ shortcut.modifier ](\n\t\t\t\tshortcut.character\n\t\t )\n\t\t: shortcut.character;\n}\n\n/**\n * Returns the main key combination for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {WPShortcutKeyCombination?} Key combination.\n */\nexport function getShortcutKeyCombination( state, name ) {\n\treturn state[ name ] ? state[ name ].keyCombination : null;\n}\n\n/**\n * Returns a string representing the main key combination for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n * @param {keyof FORMATTING_METHODS} representation Type of representation\n * (display, raw, ariaLabel).\n *\n * @return {string?} Shortcut representation.\n */\nexport function getShortcutRepresentation(\n\tstate,\n\tname,\n\trepresentation = 'display'\n) {\n\tconst shortcut = getShortcutKeyCombination( state, name );\n\treturn getKeyCombinationRepresentation( shortcut, representation );\n}\n\n/**\n * Returns the shortcut description given its name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {string?} Shortcut description.\n */\nexport function getShortcutDescription( state, name ) {\n\treturn state[ name ] ? state[ name ].description : null;\n}\n\n/**\n * Returns the aliases for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {WPShortcutKeyCombination[]} Key combinations.\n */\nexport function getShortcutAliases( state, name ) {\n\treturn state[ name ] && state[ name ].aliases\n\t\t? state[ name ].aliases\n\t\t: EMPTY_ARRAY;\n}\n\nexport const getAllShortcutKeyCombinations = createSelector(\n\t( state, name ) => {\n\t\treturn compact( [\n\t\t\tgetShortcutKeyCombination( state, name ),\n\t\t\t...getShortcutAliases( state, name ),\n\t\t] );\n\t},\n\t( state, name ) => [ state[ name ] ]\n);\n\n/**\n * Returns the raw representation of all the keyboard combinations of a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {string[]} Shortcuts.\n */\nexport const getAllShortcutRawKeyCombinations = createSelector(\n\t( state, name ) => {\n\t\treturn getAllShortcutKeyCombinations(\n\t\t\tstate,\n\t\t\tname\n\t\t).map( ( combination ) =>\n\t\t\tgetKeyCombinationRepresentation( combination, 'raw' )\n\t\t);\n\t},\n\t( state, name ) => [ state[ name ] ]\n);\n\n/**\n * Returns the shortcut names list for a given category name.\n *\n * @param {Object} state Global state.\n * @param {string} name Category name.\n *\n * @return {string[]} Shortcut names.\n */\nexport const getCategoryShortcuts = createSelector(\n\t( state, categoryName ) => {\n\t\treturn Object.entries( state )\n\t\t\t.filter( ( [ , shortcut ] ) => shortcut.category === categoryName )\n\t\t\t.map( ( [ name ] ) => name );\n\t},\n\t( state ) => [ state ]\n);\n"]}
@@ -0,0 +1,44 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import { createElement } from "@wordpress/element";
3
+
4
+ /**
5
+ * WordPress dependencies
6
+ */
7
+ import { useRef } from '@wordpress/element';
8
+ /**
9
+ * Internal dependencies
10
+ */
11
+
12
+ import { context } from '../context';
13
+ const {
14
+ Provider
15
+ } = context;
16
+ /**
17
+ * Handles callbacks added to context by `useShortcut`.
18
+ *
19
+ * @param {Object} props Props to pass to `div`.
20
+ *
21
+ * @return {import('@wordpress/element').WPElement} Component.
22
+ */
23
+
24
+ export function ShortcutProvider(props) {
25
+ const keyboardShortcuts = useRef(new Set());
26
+
27
+ function onKeyDown(event) {
28
+ if (props.onKeyDown) props.onKeyDown(event);
29
+
30
+ for (const keyboardShortcut of keyboardShortcuts.current) {
31
+ keyboardShortcut(event);
32
+ }
33
+ }
34
+ /* eslint-disable jsx-a11y/no-static-element-interactions */
35
+
36
+
37
+ return createElement(Provider, {
38
+ value: keyboardShortcuts
39
+ }, createElement("div", _extends({}, props, {
40
+ onKeyDown: onKeyDown
41
+ })));
42
+ /* eslint-enable jsx-a11y/no-static-element-interactions */
43
+ }
44
+ //# sourceMappingURL=shortcut-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/components/shortcut-provider.js"],"names":["useRef","context","Provider","ShortcutProvider","props","keyboardShortcuts","Set","onKeyDown","event","keyboardShortcut","current"],"mappings":";;;AAAA;AACA;AACA;AACA,SAASA,MAAT,QAAuB,oBAAvB;AAEA;AACA;AACA;;AACA,SAASC,OAAT,QAAwB,YAAxB;AAEA,MAAM;AAAEC,EAAAA;AAAF,IAAeD,OAArB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASE,gBAAT,CAA2BC,KAA3B,EAAmC;AACzC,QAAMC,iBAAiB,GAAGL,MAAM,CAAE,IAAIM,GAAJ,EAAF,CAAhC;;AAEA,WAASC,SAAT,CAAoBC,KAApB,EAA4B;AAC3B,QAAKJ,KAAK,CAACG,SAAX,EAAuBH,KAAK,CAACG,SAAN,CAAiBC,KAAjB;;AAEvB,SAAM,MAAMC,gBAAZ,IAAgCJ,iBAAiB,CAACK,OAAlD,EAA4D;AAC3DD,MAAAA,gBAAgB,CAAED,KAAF,CAAhB;AACA;AACD;AAED;;;AACA,SACC,cAAC,QAAD;AAAU,IAAA,KAAK,EAAGH;AAAlB,KACC,kCAAUD,KAAV;AAAkB,IAAA,SAAS,EAAGG;AAA9B,KADD,CADD;AAKA;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { context } from '../context';\n\nconst { Provider } = context;\n\n/**\n * Handles callbacks added to context by `useShortcut`.\n *\n * @param {Object} props Props to pass to `div`.\n *\n * @return {import('@wordpress/element').WPElement} Component.\n */\nexport function ShortcutProvider( props ) {\n\tconst keyboardShortcuts = useRef( new Set() );\n\n\tfunction onKeyDown( event ) {\n\t\tif ( props.onKeyDown ) props.onKeyDown( event );\n\n\t\tfor ( const keyboardShortcut of keyboardShortcuts.current ) {\n\t\t\tkeyboardShortcut( event );\n\t\t}\n\t}\n\n\t/* eslint-disable jsx-a11y/no-static-element-interactions */\n\treturn (\n\t\t<Provider value={ keyboardShortcuts }>\n\t\t\t<div { ...props } onKeyDown={ onKeyDown } />\n\t\t</Provider>\n\t);\n\t/* eslint-enable jsx-a11y/no-static-element-interactions */\n}\n"]}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { createContext } from '@wordpress/element';
5
+ export const context = createContext();
6
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/context.js"],"names":["createContext","context"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,aAAT,QAA8B,oBAA9B;AAEA,OAAO,MAAMC,OAAO,GAAGD,aAAa,EAA7B","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createContext } from '@wordpress/element';\n\nexport const context = createContext();\n"]}
@@ -30,10 +30,11 @@ export default function useShortcutEventMatch() {
30
30
  */
31
31
 
32
32
  function isMatch(name, event) {
33
- return getAllShortcutKeyCombinations(name).some(({
34
- modifier,
35
- character
36
- }) => {
33
+ return getAllShortcutKeyCombinations(name).some(_ref => {
34
+ let {
35
+ modifier,
36
+ character
37
+ } = _ref;
37
38
  return isKeyboardEvent[modifier](event, character);
38
39
  });
39
40
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/hooks/use-shortcut-event-match.js"],"names":["useSelect","isKeyboardEvent","store","keyboardShortcutsStore","useShortcutEventMatch","getAllShortcutKeyCombinations","isMatch","name","event","some","modifier","character"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAT,QAA0B,iBAA1B;AACA,SAASC,eAAT,QAAgC,qBAAhC;AAEA;AACA;AACA;;AACA,SAASC,KAAK,IAAIC,sBAAlB,QAAgD,UAAhD;AAEA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,qBAAT,GAAiC;AAC/C,QAAM;AAAEC,IAAAA;AAAF,MAAoCL,SAAS,CAClDG,sBADkD,CAAnD;AAIA;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACC,WAASG,OAAT,CAAkBC,IAAlB,EAAwBC,KAAxB,EAAgC;AAC/B,WAAOH,6BAA6B,CAAEE,IAAF,CAA7B,CAAsCE,IAAtC,CACN,CAAE;AAAEC,MAAAA,QAAF;AAAYC,MAAAA;AAAZ,KAAF,KAA+B;AAC9B,aAAOV,eAAe,CAAES,QAAF,CAAf,CAA6BF,KAA7B,EAAoCG,SAApC,CAAP;AACA,KAHK,CAAP;AAKA;;AAED,SAAOL,OAAP;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { isKeyboardEvent } from '@wordpress/keycodes';\n\n/**\n * Internal dependencies\n */\nimport { store as keyboardShortcutsStore } from '../store';\n\n/**\n * Returns a function to check if a keyboard event matches a shortcut name.\n *\n * @return {Function} A function to to check if a keyboard event matches a\n * predefined shortcut combination.\n */\nexport default function useShortcutEventMatch() {\n\tconst { getAllShortcutKeyCombinations } = useSelect(\n\t\tkeyboardShortcutsStore\n\t);\n\n\t/**\n\t * A function to check if a keyboard event matches a predefined shortcut\n\t * combination.\n\t *\n\t * @param {string} name Shortcut name.\n\t * @param {KeyboardEvent} event Event to check.\n\t *\n\t * @return {boolean} True if the event matches any shortcuts, false if not.\n\t */\n\tfunction isMatch( name, event ) {\n\t\treturn getAllShortcutKeyCombinations( name ).some(\n\t\t\t( { modifier, character } ) => {\n\t\t\t\treturn isKeyboardEvent[ modifier ]( event, character );\n\t\t\t}\n\t\t);\n\t}\n\n\treturn isMatch;\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/hooks/use-shortcut-event-match.js"],"names":["useSelect","isKeyboardEvent","store","keyboardShortcutsStore","useShortcutEventMatch","getAllShortcutKeyCombinations","isMatch","name","event","some","modifier","character"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAT,QAA0B,iBAA1B;AACA,SAASC,eAAT,QAAgC,qBAAhC;AAEA;AACA;AACA;;AACA,SAASC,KAAK,IAAIC,sBAAlB,QAAgD,UAAhD;AAEA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,qBAAT,GAAiC;AAC/C,QAAM;AAAEC,IAAAA;AAAF,MAAoCL,SAAS,CAClDG,sBADkD,CAAnD;AAIA;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACC,WAASG,OAAT,CAAkBC,IAAlB,EAAwBC,KAAxB,EAAgC;AAC/B,WAAOH,6BAA6B,CAAEE,IAAF,CAA7B,CAAsCE,IAAtC,CACN,QAA+B;AAAA,UAA7B;AAAEC,QAAAA,QAAF;AAAYC,QAAAA;AAAZ,OAA6B;AAC9B,aAAOV,eAAe,CAAES,QAAF,CAAf,CAA6BF,KAA7B,EAAoCG,SAApC,CAAP;AACA,KAHK,CAAP;AAKA;;AAED,SAAOL,OAAP;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { isKeyboardEvent } from '@wordpress/keycodes';\n\n/**\n * Internal dependencies\n */\nimport { store as keyboardShortcutsStore } from '../store';\n\n/**\n * Returns a function to check if a keyboard event matches a shortcut name.\n *\n * @return {Function} A function to to check if a keyboard event matches a\n * predefined shortcut combination.\n */\nexport default function useShortcutEventMatch() {\n\tconst { getAllShortcutKeyCombinations } = useSelect(\n\t\tkeyboardShortcutsStore\n\t);\n\n\t/**\n\t * A function to check if a keyboard event matches a predefined shortcut\n\t * combination.\n\t *\n\t * @param {string} name Shortcut name.\n\t * @param {KeyboardEvent} event Event to check.\n\t *\n\t * @return {boolean} True if the event matches any shortcuts, false if not.\n\t */\n\tfunction isMatch( name, event ) {\n\t\treturn getAllShortcutKeyCombinations( name ).some(\n\t\t\t( { modifier, character } ) => {\n\t\t\t\treturn isKeyboardEvent[ modifier ]( event, character );\n\t\t\t}\n\t\t);\n\t}\n\n\treturn isMatch;\n}\n"]}
@@ -1,27 +1,45 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { useSelect } from '@wordpress/data';
5
- import { useKeyboardShortcut } from '@wordpress/compose';
4
+ import { useContext, useEffect, useRef } from '@wordpress/element';
6
5
  /**
7
6
  * Internal dependencies
8
7
  */
9
8
 
10
- import { store as keyboardShortcutsStore } from '../store';
9
+ import useShortcutEventMatch from './use-shortcut-event-match';
10
+ import { context } from '../context';
11
11
  /**
12
12
  * Attach a keyboard shortcut handler.
13
13
  *
14
- * @param {string} name Shortcut name.
15
- * @param {Function} callback Shortcut callback.
16
- * @param {Object} options Shortcut options.
14
+ * @param {string} name Shortcut name.
15
+ * @param {Function} callback Shortcut callback.
16
+ * @param {Object} options Shortcut options.
17
+ * @param {boolean} options.isDisabled Whether to disable to shortut.
17
18
  */
18
19
 
19
- function useShortcut(name, callback, options) {
20
- const shortcuts = useSelect(select => {
21
- return select(keyboardShortcutsStore).getAllShortcutRawKeyCombinations(name);
22
- }, [name]);
23
- useKeyboardShortcut(shortcuts, callback, options);
24
- }
20
+ export default function useShortcut(name, callback) {
21
+ let {
22
+ isDisabled
23
+ } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
24
+ const shortcuts = useContext(context);
25
+ const isMatch = useShortcutEventMatch();
26
+ const callbackRef = useRef();
27
+ callbackRef.current = callback;
28
+ useEffect(() => {
29
+ if (isDisabled) {
30
+ return;
31
+ }
32
+
33
+ function _callback(event) {
34
+ if (isMatch(name, event)) {
35
+ callbackRef.current(event);
36
+ }
37
+ }
25
38
 
26
- export default useShortcut;
39
+ shortcuts.current.add(_callback);
40
+ return () => {
41
+ shortcuts.current.delete(_callback);
42
+ };
43
+ }, [name, isDisabled]);
44
+ }
27
45
  //# sourceMappingURL=use-shortcut.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/hooks/use-shortcut.js"],"names":["useSelect","useKeyboardShortcut","store","keyboardShortcutsStore","useShortcut","name","callback","options","shortcuts","select","getAllShortcutRawKeyCombinations"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAT,QAA0B,iBAA1B;AACA,SAASC,mBAAT,QAAoC,oBAApC;AAEA;AACA;AACA;;AACA,SAASC,KAAK,IAAIC,sBAAlB,QAAgD,UAAhD;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,WAAT,CAAsBC,IAAtB,EAA4BC,QAA5B,EAAsCC,OAAtC,EAAgD;AAC/C,QAAMC,SAAS,GAAGR,SAAS,CACxBS,MAAF,IAAc;AACb,WAAOA,MAAM,CACZN,sBADY,CAAN,CAELO,gCAFK,CAE6BL,IAF7B,CAAP;AAGA,GALyB,EAM1B,CAAEA,IAAF,CAN0B,CAA3B;AASAJ,EAAAA,mBAAmB,CAAEO,SAAF,EAAaF,QAAb,EAAuBC,OAAvB,CAAnB;AACA;;AAED,eAAeH,WAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { useKeyboardShortcut } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport { store as keyboardShortcutsStore } from '../store';\n\n/**\n * Attach a keyboard shortcut handler.\n *\n * @param {string} name Shortcut name.\n * @param {Function} callback Shortcut callback.\n * @param {Object} options Shortcut options.\n */\nfunction useShortcut( name, callback, options ) {\n\tconst shortcuts = useSelect(\n\t\t( select ) => {\n\t\t\treturn select(\n\t\t\t\tkeyboardShortcutsStore\n\t\t\t).getAllShortcutRawKeyCombinations( name );\n\t\t},\n\t\t[ name ]\n\t);\n\n\tuseKeyboardShortcut( shortcuts, callback, options );\n}\n\nexport default useShortcut;\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/hooks/use-shortcut.js"],"names":["useContext","useEffect","useRef","useShortcutEventMatch","context","useShortcut","name","callback","isDisabled","shortcuts","isMatch","callbackRef","current","_callback","event","add","delete"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAT,EAAqBC,SAArB,EAAgCC,MAAhC,QAA8C,oBAA9C;AAEA;AACA;AACA;;AACA,OAAOC,qBAAP,MAAkC,4BAAlC;AACA,SAASC,OAAT,QAAwB,YAAxB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,WAAT,CAAsBC,IAAtB,EAA4BC,QAA5B,EAA4D;AAAA,MAAtB;AAAEC,IAAAA;AAAF,GAAsB,uEAAL,EAAK;AAC1E,QAAMC,SAAS,GAAGT,UAAU,CAAEI,OAAF,CAA5B;AACA,QAAMM,OAAO,GAAGP,qBAAqB,EAArC;AACA,QAAMQ,WAAW,GAAGT,MAAM,EAA1B;AACAS,EAAAA,WAAW,CAACC,OAAZ,GAAsBL,QAAtB;AAEAN,EAAAA,SAAS,CAAE,MAAM;AAChB,QAAKO,UAAL,EAAkB;AACjB;AACA;;AAED,aAASK,SAAT,CAAoBC,KAApB,EAA4B;AAC3B,UAAKJ,OAAO,CAAEJ,IAAF,EAAQQ,KAAR,CAAZ,EAA8B;AAC7BH,QAAAA,WAAW,CAACC,OAAZ,CAAqBE,KAArB;AACA;AACD;;AAEDL,IAAAA,SAAS,CAACG,OAAV,CAAkBG,GAAlB,CAAuBF,SAAvB;AACA,WAAO,MAAM;AACZJ,MAAAA,SAAS,CAACG,OAAV,CAAkBI,MAAlB,CAA0BH,SAA1B;AACA,KAFD;AAGA,GAfQ,EAeN,CAAEP,IAAF,EAAQE,UAAR,CAfM,CAAT;AAgBA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useContext, useEffect, useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport useShortcutEventMatch from './use-shortcut-event-match';\nimport { context } from '../context';\n\n/**\n * Attach a keyboard shortcut handler.\n *\n * @param {string} name Shortcut name.\n * @param {Function} callback Shortcut callback.\n * @param {Object} options Shortcut options.\n * @param {boolean} options.isDisabled Whether to disable to shortut.\n */\nexport default function useShortcut( name, callback, { isDisabled } = {} ) {\n\tconst shortcuts = useContext( context );\n\tconst isMatch = useShortcutEventMatch();\n\tconst callbackRef = useRef();\n\tcallbackRef.current = callback;\n\n\tuseEffect( () => {\n\t\tif ( isDisabled ) {\n\t\t\treturn;\n\t\t}\n\n\t\tfunction _callback( event ) {\n\t\t\tif ( isMatch( name, event ) ) {\n\t\t\t\tcallbackRef.current( event );\n\t\t\t}\n\t\t}\n\n\t\tshortcuts.current.add( _callback );\n\t\treturn () => {\n\t\t\tshortcuts.current.delete( _callback );\n\t\t};\n\t}, [ name, isDisabled ] );\n}\n"]}
@@ -1,4 +1,5 @@
1
1
  export { store } from './store';
2
2
  export { default as useShortcut } from './hooks/use-shortcut';
3
+ export { ShortcutProvider } from './components/shortcut-provider';
3
4
  export { default as __unstableUseShortcutEventMatch } from './hooks/use-shortcut-event-match';
4
5
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/index.js"],"names":["store","default","useShortcut","__unstableUseShortcutEventMatch"],"mappings":"AAAA,SAASA,KAAT,QAAsB,SAAtB;AACA,SAASC,OAAO,IAAIC,WAApB,QAAuC,sBAAvC;AACA,SAASD,OAAO,IAAIE,+BAApB,QAA2D,kCAA3D","sourcesContent":["export { store } from './store';\nexport { default as useShortcut } from './hooks/use-shortcut';\nexport { default as __unstableUseShortcutEventMatch } from './hooks/use-shortcut-event-match';\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/index.js"],"names":["store","default","useShortcut","ShortcutProvider","__unstableUseShortcutEventMatch"],"mappings":"AAAA,SAASA,KAAT,QAAsB,SAAtB;AACA,SAASC,OAAO,IAAIC,WAApB,QAAuC,sBAAvC;AACA,SAASC,gBAAT,QAAiC,gCAAjC;AACA,SAASF,OAAO,IAAIG,+BAApB,QAA2D,kCAA3D","sourcesContent":["export { store } from './store';\nexport { default as useShortcut } from './hooks/use-shortcut';\nexport { ShortcutProvider } from './components/shortcut-provider';\nexport { default as __unstableUseShortcutEventMatch } from './hooks/use-shortcut-event-match';\n"]}
@@ -28,13 +28,14 @@
28
28
  *
29
29
  * @return {Object} action.
30
30
  */
31
- export function registerShortcut({
32
- name,
33
- category,
34
- description,
35
- keyCombination,
36
- aliases
37
- }) {
31
+ export function registerShortcut(_ref) {
32
+ let {
33
+ name,
34
+ category,
35
+ description,
36
+ keyCombination,
37
+ aliases
38
+ } = _ref;
38
39
  return {
39
40
  type: 'REGISTER_SHORTCUT',
40
41
  name,
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/actions.js"],"names":["registerShortcut","name","category","description","keyCombination","aliases","type","unregisterShortcut"],"mappings":"AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,gBAAT,CAA2B;AACjCC,EAAAA,IADiC;AAEjCC,EAAAA,QAFiC;AAGjCC,EAAAA,WAHiC;AAIjCC,EAAAA,cAJiC;AAKjCC,EAAAA;AALiC,CAA3B,EAMH;AACH,SAAO;AACNC,IAAAA,IAAI,EAAE,mBADA;AAENL,IAAAA,IAFM;AAGNC,IAAAA,QAHM;AAINE,IAAAA,cAJM;AAKNC,IAAAA,OALM;AAMNF,IAAAA;AANM,GAAP;AAQA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASI,kBAAT,CAA6BN,IAA7B,EAAoC;AAC1C,SAAO;AACNK,IAAAA,IAAI,EAAE,qBADA;AAENL,IAAAA;AAFM,GAAP;AAIA","sourcesContent":["/** @typedef {import('@wordpress/keycodes').WPKeycodeModifier} WPKeycodeModifier */\n\n/**\n * Keyboard key combination.\n *\n * @typedef {Object} WPShortcutKeyCombination\n *\n * @property {string} character Character.\n * @property {WPKeycodeModifier|undefined} modifier Modifier.\n */\n\n/**\n * Configuration of a registered keyboard shortcut.\n *\n * @typedef {Object} WPShortcutConfig\n *\n * @property {string} name Shortcut name.\n * @property {string} category Shortcut category.\n * @property {string} description Shortcut description.\n * @property {WPShortcutKeyCombination} keyCombination Shortcut key combination.\n * @property {WPShortcutKeyCombination[]} [aliases] Shortcut aliases.\n */\n\n/**\n * Returns an action object used to register a new keyboard shortcut.\n *\n * @param {WPShortcutConfig} config Shortcut config.\n *\n * @return {Object} action.\n */\nexport function registerShortcut( {\n\tname,\n\tcategory,\n\tdescription,\n\tkeyCombination,\n\taliases,\n} ) {\n\treturn {\n\t\ttype: 'REGISTER_SHORTCUT',\n\t\tname,\n\t\tcategory,\n\t\tkeyCombination,\n\t\taliases,\n\t\tdescription,\n\t};\n}\n\n/**\n * Returns an action object used to unregister a keyboard shortcut.\n *\n * @param {string} name Shortcut name.\n *\n * @return {Object} action.\n */\nexport function unregisterShortcut( name ) {\n\treturn {\n\t\ttype: 'UNREGISTER_SHORTCUT',\n\t\tname,\n\t};\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/actions.js"],"names":["registerShortcut","name","category","description","keyCombination","aliases","type","unregisterShortcut"],"mappings":"AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,gBAAT,OAMH;AAAA,MAN8B;AACjCC,IAAAA,IADiC;AAEjCC,IAAAA,QAFiC;AAGjCC,IAAAA,WAHiC;AAIjCC,IAAAA,cAJiC;AAKjCC,IAAAA;AALiC,GAM9B;AACH,SAAO;AACNC,IAAAA,IAAI,EAAE,mBADA;AAENL,IAAAA,IAFM;AAGNC,IAAAA,QAHM;AAINE,IAAAA,cAJM;AAKNC,IAAAA,OALM;AAMNF,IAAAA;AANM,GAAP;AAQA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASI,kBAAT,CAA6BN,IAA7B,EAAoC;AAC1C,SAAO;AACNK,IAAAA,IAAI,EAAE,qBADA;AAENL,IAAAA;AAFM,GAAP;AAIA","sourcesContent":["/** @typedef {import('@wordpress/keycodes').WPKeycodeModifier} WPKeycodeModifier */\n\n/**\n * Keyboard key combination.\n *\n * @typedef {Object} WPShortcutKeyCombination\n *\n * @property {string} character Character.\n * @property {WPKeycodeModifier|undefined} modifier Modifier.\n */\n\n/**\n * Configuration of a registered keyboard shortcut.\n *\n * @typedef {Object} WPShortcutConfig\n *\n * @property {string} name Shortcut name.\n * @property {string} category Shortcut category.\n * @property {string} description Shortcut description.\n * @property {WPShortcutKeyCombination} keyCombination Shortcut key combination.\n * @property {WPShortcutKeyCombination[]} [aliases] Shortcut aliases.\n */\n\n/**\n * Returns an action object used to register a new keyboard shortcut.\n *\n * @param {WPShortcutConfig} config Shortcut config.\n *\n * @return {Object} action.\n */\nexport function registerShortcut( {\n\tname,\n\tcategory,\n\tdescription,\n\tkeyCombination,\n\taliases,\n} ) {\n\treturn {\n\t\ttype: 'REGISTER_SHORTCUT',\n\t\tname,\n\t\tcategory,\n\t\tkeyCombination,\n\t\taliases,\n\t\tdescription,\n\t};\n}\n\n/**\n * Returns an action object used to unregister a keyboard shortcut.\n *\n * @param {string} name Shortcut name.\n *\n * @return {Object} action.\n */\nexport function unregisterShortcut( name ) {\n\treturn {\n\t\ttype: 'UNREGISTER_SHORTCUT',\n\t\tname,\n\t};\n}\n"]}
@@ -11,7 +11,10 @@ import { omit } from 'lodash';
11
11
  * @return {Object} Updated state.
12
12
  */
13
13
 
14
- function reducer(state = {}, action) {
14
+ function reducer() {
15
+ let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
16
+ let action = arguments.length > 1 ? arguments[1] : undefined;
17
+
15
18
  switch (action.type) {
16
19
  case 'REGISTER_SHORTCUT':
17
20
  return { ...state,
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/reducer.js"],"names":["omit","reducer","state","action","type","name","category","keyCombination","aliases","description"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,IAAT,QAAqB,QAArB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,OAAT,CAAkBC,KAAK,GAAG,EAA1B,EAA8BC,MAA9B,EAAuC;AACtC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,mBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,IAAT,GAAiB;AAChBC,UAAAA,QAAQ,EAAEH,MAAM,CAACG,QADD;AAEhBC,UAAAA,cAAc,EAAEJ,MAAM,CAACI,cAFP;AAGhBC,UAAAA,OAAO,EAAEL,MAAM,CAACK,OAHA;AAIhBC,UAAAA,WAAW,EAAEN,MAAM,CAACM;AAJJ;AAFX,OAAP;;AASD,SAAK,qBAAL;AACC,aAAOT,IAAI,CAAEE,KAAF,EAASC,MAAM,CAACE,IAAhB,CAAX;AAZF;;AAeA,SAAOH,KAAP;AACA;;AAED,eAAeD,OAAf","sourcesContent":["/**\n * External dependencies\n */\nimport { omit } from 'lodash';\n\n/**\n * Reducer returning the registered shortcuts\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction reducer( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_SHORTCUT':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.name ]: {\n\t\t\t\t\tcategory: action.category,\n\t\t\t\t\tkeyCombination: action.keyCombination,\n\t\t\t\t\taliases: action.aliases,\n\t\t\t\t\tdescription: action.description,\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_SHORTCUT':\n\t\t\treturn omit( state, action.name );\n\t}\n\n\treturn state;\n}\n\nexport default reducer;\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/reducer.js"],"names":["omit","reducer","state","action","type","name","category","keyCombination","aliases","description"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,IAAT,QAAqB,QAArB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,OAAT,GAAuC;AAAA,MAArBC,KAAqB,uEAAb,EAAa;AAAA,MAATC,MAAS;;AACtC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,mBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,IAAT,GAAiB;AAChBC,UAAAA,QAAQ,EAAEH,MAAM,CAACG,QADD;AAEhBC,UAAAA,cAAc,EAAEJ,MAAM,CAACI,cAFP;AAGhBC,UAAAA,OAAO,EAAEL,MAAM,CAACK,OAHA;AAIhBC,UAAAA,WAAW,EAAEN,MAAM,CAACM;AAJJ;AAFX,OAAP;;AASD,SAAK,qBAAL;AACC,aAAOT,IAAI,CAAEE,KAAF,EAASC,MAAM,CAACE,IAAhB,CAAX;AAZF;;AAeA,SAAOH,KAAP;AACA;;AAED,eAAeD,OAAf","sourcesContent":["/**\n * External dependencies\n */\nimport { omit } from 'lodash';\n\n/**\n * Reducer returning the registered shortcuts\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction reducer( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_SHORTCUT':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.name ]: {\n\t\t\t\t\tcategory: action.category,\n\t\t\t\t\tkeyCombination: action.keyCombination,\n\t\t\t\t\taliases: action.aliases,\n\t\t\t\t\tdescription: action.description,\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_SHORTCUT':\n\t\t\treturn omit( state, action.name );\n\t}\n\n\treturn state;\n}\n\nexport default reducer;\n"]}
@@ -74,7 +74,8 @@ export function getShortcutKeyCombination(state, name) {
74
74
  * @return {string?} Shortcut representation.
75
75
  */
76
76
 
77
- export function getShortcutRepresentation(state, name, representation = 'display') {
77
+ export function getShortcutRepresentation(state, name) {
78
+ let representation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'display';
78
79
  const shortcut = getShortcutKeyCombination(state, name);
79
80
  return getKeyCombinationRepresentation(shortcut, representation);
80
81
  }
@@ -127,6 +128,12 @@ export const getAllShortcutRawKeyCombinations = createSelector((state, name) =>
127
128
  */
128
129
 
129
130
  export const getCategoryShortcuts = createSelector((state, categoryName) => {
130
- return Object.entries(state).filter(([, shortcut]) => shortcut.category === categoryName).map(([name]) => name);
131
+ return Object.entries(state).filter(_ref => {
132
+ let [, shortcut] = _ref;
133
+ return shortcut.category === categoryName;
134
+ }).map(_ref2 => {
135
+ let [name] = _ref2;
136
+ return name;
137
+ });
131
138
  }, state => [state]);
132
139
  //# sourceMappingURL=selectors.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/selectors.js"],"names":["createSelector","compact","displayShortcut","shortcutAriaLabel","rawShortcut","EMPTY_ARRAY","FORMATTING_METHODS","display","raw","ariaLabel","getKeyCombinationRepresentation","shortcut","representation","modifier","character","getShortcutKeyCombination","state","name","keyCombination","getShortcutRepresentation","getShortcutDescription","description","getShortcutAliases","aliases","getAllShortcutKeyCombinations","getAllShortcutRawKeyCombinations","map","combination","getCategoryShortcuts","categoryName","Object","entries","filter","category"],"mappings":"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","sourcesContent":["/**\n * External dependencies\n */\nimport createSelector from 'rememo';\nimport { compact } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tdisplayShortcut,\n\tshortcutAriaLabel,\n\trawShortcut,\n} from '@wordpress/keycodes';\n\n/** @typedef {import('./actions').WPShortcutKeyCombination} WPShortcutKeyCombination */\n\n/** @typedef {import('@wordpress/keycodes').WPKeycodeHandlerByModifier} WPKeycodeHandlerByModifier */\n\n/**\n * Shared reference to an empty array for cases where it is important to avoid\n * returning a new array reference on every invocation.\n *\n * @type {Array<any>}\n */\nconst EMPTY_ARRAY = [];\n\n/**\n * Shortcut formatting methods.\n *\n * @property {WPKeycodeHandlerByModifier} display Display formatting.\n * @property {WPKeycodeHandlerByModifier} rawShortcut Raw shortcut formatting.\n * @property {WPKeycodeHandlerByModifier} ariaLabel ARIA label formatting.\n */\nconst FORMATTING_METHODS = {\n\tdisplay: displayShortcut,\n\traw: rawShortcut,\n\tariaLabel: shortcutAriaLabel,\n};\n\n/**\n * Returns a string representing the key combination.\n *\n * @param {?WPShortcutKeyCombination} shortcut Key combination.\n * @param {keyof FORMATTING_METHODS} representation Type of representation\n * (display, raw, ariaLabel).\n *\n * @return {string?} Shortcut representation.\n */\nfunction getKeyCombinationRepresentation( shortcut, representation ) {\n\tif ( ! shortcut ) {\n\t\treturn null;\n\t}\n\n\treturn shortcut.modifier\n\t\t? FORMATTING_METHODS[ representation ][ shortcut.modifier ](\n\t\t\t\tshortcut.character\n\t\t )\n\t\t: shortcut.character;\n}\n\n/**\n * Returns the main key combination for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {WPShortcutKeyCombination?} Key combination.\n */\nexport function getShortcutKeyCombination( state, name ) {\n\treturn state[ name ] ? state[ name ].keyCombination : null;\n}\n\n/**\n * Returns a string representing the main key combination for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n * @param {keyof FORMATTING_METHODS} representation Type of representation\n * (display, raw, ariaLabel).\n *\n * @return {string?} Shortcut representation.\n */\nexport function getShortcutRepresentation(\n\tstate,\n\tname,\n\trepresentation = 'display'\n) {\n\tconst shortcut = getShortcutKeyCombination( state, name );\n\treturn getKeyCombinationRepresentation( shortcut, representation );\n}\n\n/**\n * Returns the shortcut description given its name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {string?} Shortcut description.\n */\nexport function getShortcutDescription( state, name ) {\n\treturn state[ name ] ? state[ name ].description : null;\n}\n\n/**\n * Returns the aliases for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {WPShortcutKeyCombination[]} Key combinations.\n */\nexport function getShortcutAliases( state, name ) {\n\treturn state[ name ] && state[ name ].aliases\n\t\t? state[ name ].aliases\n\t\t: EMPTY_ARRAY;\n}\n\nexport const getAllShortcutKeyCombinations = createSelector(\n\t( state, name ) => {\n\t\treturn compact( [\n\t\t\tgetShortcutKeyCombination( state, name ),\n\t\t\t...getShortcutAliases( state, name ),\n\t\t] );\n\t},\n\t( state, name ) => [ state[ name ] ]\n);\n\n/**\n * Returns the raw representation of all the keyboard combinations of a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {string[]} Shortcuts.\n */\nexport const getAllShortcutRawKeyCombinations = createSelector(\n\t( state, name ) => {\n\t\treturn getAllShortcutKeyCombinations(\n\t\t\tstate,\n\t\t\tname\n\t\t).map( ( combination ) =>\n\t\t\tgetKeyCombinationRepresentation( combination, 'raw' )\n\t\t);\n\t},\n\t( state, name ) => [ state[ name ] ]\n);\n\n/**\n * Returns the shortcut names list for a given category name.\n *\n * @param {Object} state Global state.\n * @param {string} name Category name.\n *\n * @return {string[]} Shortcut names.\n */\nexport const getCategoryShortcuts = createSelector(\n\t( state, categoryName ) => {\n\t\treturn Object.entries( state )\n\t\t\t.filter( ( [ , shortcut ] ) => shortcut.category === categoryName )\n\t\t\t.map( ( [ name ] ) => name );\n\t},\n\t( state ) => [ state ]\n);\n"]}
1
+ {"version":3,"sources":["@wordpress/keyboard-shortcuts/src/store/selectors.js"],"names":["createSelector","compact","displayShortcut","shortcutAriaLabel","rawShortcut","EMPTY_ARRAY","FORMATTING_METHODS","display","raw","ariaLabel","getKeyCombinationRepresentation","shortcut","representation","modifier","character","getShortcutKeyCombination","state","name","keyCombination","getShortcutRepresentation","getShortcutDescription","description","getShortcutAliases","aliases","getAllShortcutKeyCombinations","getAllShortcutRawKeyCombinations","map","combination","getCategoryShortcuts","categoryName","Object","entries","filter","category"],"mappings":"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","sourcesContent":["/**\n * External dependencies\n */\nimport createSelector from 'rememo';\nimport { compact } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tdisplayShortcut,\n\tshortcutAriaLabel,\n\trawShortcut,\n} from '@wordpress/keycodes';\n\n/** @typedef {import('./actions').WPShortcutKeyCombination} WPShortcutKeyCombination */\n\n/** @typedef {import('@wordpress/keycodes').WPKeycodeHandlerByModifier} WPKeycodeHandlerByModifier */\n\n/**\n * Shared reference to an empty array for cases where it is important to avoid\n * returning a new array reference on every invocation.\n *\n * @type {Array<any>}\n */\nconst EMPTY_ARRAY = [];\n\n/**\n * Shortcut formatting methods.\n *\n * @property {WPKeycodeHandlerByModifier} display Display formatting.\n * @property {WPKeycodeHandlerByModifier} rawShortcut Raw shortcut formatting.\n * @property {WPKeycodeHandlerByModifier} ariaLabel ARIA label formatting.\n */\nconst FORMATTING_METHODS = {\n\tdisplay: displayShortcut,\n\traw: rawShortcut,\n\tariaLabel: shortcutAriaLabel,\n};\n\n/**\n * Returns a string representing the key combination.\n *\n * @param {?WPShortcutKeyCombination} shortcut Key combination.\n * @param {keyof FORMATTING_METHODS} representation Type of representation\n * (display, raw, ariaLabel).\n *\n * @return {string?} Shortcut representation.\n */\nfunction getKeyCombinationRepresentation( shortcut, representation ) {\n\tif ( ! shortcut ) {\n\t\treturn null;\n\t}\n\n\treturn shortcut.modifier\n\t\t? FORMATTING_METHODS[ representation ][ shortcut.modifier ](\n\t\t\t\tshortcut.character\n\t\t )\n\t\t: shortcut.character;\n}\n\n/**\n * Returns the main key combination for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {WPShortcutKeyCombination?} Key combination.\n */\nexport function getShortcutKeyCombination( state, name ) {\n\treturn state[ name ] ? state[ name ].keyCombination : null;\n}\n\n/**\n * Returns a string representing the main key combination for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n * @param {keyof FORMATTING_METHODS} representation Type of representation\n * (display, raw, ariaLabel).\n *\n * @return {string?} Shortcut representation.\n */\nexport function getShortcutRepresentation(\n\tstate,\n\tname,\n\trepresentation = 'display'\n) {\n\tconst shortcut = getShortcutKeyCombination( state, name );\n\treturn getKeyCombinationRepresentation( shortcut, representation );\n}\n\n/**\n * Returns the shortcut description given its name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {string?} Shortcut description.\n */\nexport function getShortcutDescription( state, name ) {\n\treturn state[ name ] ? state[ name ].description : null;\n}\n\n/**\n * Returns the aliases for a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {WPShortcutKeyCombination[]} Key combinations.\n */\nexport function getShortcutAliases( state, name ) {\n\treturn state[ name ] && state[ name ].aliases\n\t\t? state[ name ].aliases\n\t\t: EMPTY_ARRAY;\n}\n\nexport const getAllShortcutKeyCombinations = createSelector(\n\t( state, name ) => {\n\t\treturn compact( [\n\t\t\tgetShortcutKeyCombination( state, name ),\n\t\t\t...getShortcutAliases( state, name ),\n\t\t] );\n\t},\n\t( state, name ) => [ state[ name ] ]\n);\n\n/**\n * Returns the raw representation of all the keyboard combinations of a given shortcut name.\n *\n * @param {Object} state Global state.\n * @param {string} name Shortcut name.\n *\n * @return {string[]} Shortcuts.\n */\nexport const getAllShortcutRawKeyCombinations = createSelector(\n\t( state, name ) => {\n\t\treturn getAllShortcutKeyCombinations(\n\t\t\tstate,\n\t\t\tname\n\t\t).map( ( combination ) =>\n\t\t\tgetKeyCombinationRepresentation( combination, 'raw' )\n\t\t);\n\t},\n\t( state, name ) => [ state[ name ] ]\n);\n\n/**\n * Returns the shortcut names list for a given category name.\n *\n * @param {Object} state Global state.\n * @param {string} name Category name.\n *\n * @return {string[]} Shortcut names.\n */\nexport const getCategoryShortcuts = createSelector(\n\t( state, categoryName ) => {\n\t\treturn Object.entries( state )\n\t\t\t.filter( ( [ , shortcut ] ) => shortcut.category === categoryName )\n\t\t\t.map( ( [ name ] ) => name );\n\t},\n\t( state ) => [ state ]\n);\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/keyboard-shortcuts",
3
- "version": "3.0.2",
3
+ "version": "3.0.6",
4
4
  "description": "Handling keyboard shortcuts.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -25,16 +25,16 @@
25
25
  "module": "build-module/index.js",
26
26
  "react-native": "src/index",
27
27
  "dependencies": {
28
- "@babel/runtime": "^7.13.10",
29
- "@wordpress/compose": "^5.0.2",
30
- "@wordpress/data": "^6.1.0",
31
- "@wordpress/element": "^4.0.1",
32
- "@wordpress/keycodes": "^3.2.2",
28
+ "@babel/runtime": "^7.16.0",
29
+ "@wordpress/compose": "^5.0.6",
30
+ "@wordpress/data": "^6.1.4",
31
+ "@wordpress/element": "^4.0.4",
32
+ "@wordpress/keycodes": "^3.2.4",
33
33
  "lodash": "^4.17.21",
34
34
  "rememo": "^3.0.0"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"
38
38
  },
39
- "gitHead": "98c42a7187f788fe3e023f04df7f5dcbdae4e4e7"
39
+ "gitHead": "9a1dd3474d937468e4cf9caf9886ad61ef0a8f50"
40
40
  }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useRef } from '@wordpress/element';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import { context } from '../context';
10
+
11
+ const { Provider } = context;
12
+
13
+ /**
14
+ * Handles callbacks added to context by `useShortcut`.
15
+ *
16
+ * @param {Object} props Props to pass to `div`.
17
+ *
18
+ * @return {import('@wordpress/element').WPElement} Component.
19
+ */
20
+ export function ShortcutProvider( props ) {
21
+ const keyboardShortcuts = useRef( new Set() );
22
+
23
+ function onKeyDown( event ) {
24
+ if ( props.onKeyDown ) props.onKeyDown( event );
25
+
26
+ for ( const keyboardShortcut of keyboardShortcuts.current ) {
27
+ keyboardShortcut( event );
28
+ }
29
+ }
30
+
31
+ /* eslint-disable jsx-a11y/no-static-element-interactions */
32
+ return (
33
+ <Provider value={ keyboardShortcuts }>
34
+ <div { ...props } onKeyDown={ onKeyDown } />
35
+ </Provider>
36
+ );
37
+ /* eslint-enable jsx-a11y/no-static-element-interactions */
38
+ }
package/src/context.js ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { createContext } from '@wordpress/element';
5
+
6
+ export const context = createContext();
@@ -1,32 +1,42 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { useSelect } from '@wordpress/data';
5
- import { useKeyboardShortcut } from '@wordpress/compose';
4
+ import { useContext, useEffect, useRef } from '@wordpress/element';
6
5
 
7
6
  /**
8
7
  * Internal dependencies
9
8
  */
10
- import { store as keyboardShortcutsStore } from '../store';
9
+ import useShortcutEventMatch from './use-shortcut-event-match';
10
+ import { context } from '../context';
11
11
 
12
12
  /**
13
13
  * Attach a keyboard shortcut handler.
14
14
  *
15
- * @param {string} name Shortcut name.
16
- * @param {Function} callback Shortcut callback.
17
- * @param {Object} options Shortcut options.
15
+ * @param {string} name Shortcut name.
16
+ * @param {Function} callback Shortcut callback.
17
+ * @param {Object} options Shortcut options.
18
+ * @param {boolean} options.isDisabled Whether to disable to shortut.
18
19
  */
19
- function useShortcut( name, callback, options ) {
20
- const shortcuts = useSelect(
21
- ( select ) => {
22
- return select(
23
- keyboardShortcutsStore
24
- ).getAllShortcutRawKeyCombinations( name );
25
- },
26
- [ name ]
27
- );
20
+ export default function useShortcut( name, callback, { isDisabled } = {} ) {
21
+ const shortcuts = useContext( context );
22
+ const isMatch = useShortcutEventMatch();
23
+ const callbackRef = useRef();
24
+ callbackRef.current = callback;
28
25
 
29
- useKeyboardShortcut( shortcuts, callback, options );
30
- }
26
+ useEffect( () => {
27
+ if ( isDisabled ) {
28
+ return;
29
+ }
30
+
31
+ function _callback( event ) {
32
+ if ( isMatch( name, event ) ) {
33
+ callbackRef.current( event );
34
+ }
35
+ }
31
36
 
32
- export default useShortcut;
37
+ shortcuts.current.add( _callback );
38
+ return () => {
39
+ shortcuts.current.delete( _callback );
40
+ };
41
+ }, [ name, isDisabled ] );
42
+ }
package/src/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { store } from './store';
2
2
  export { default as useShortcut } from './hooks/use-shortcut';
3
+ export { ShortcutProvider } from './components/shortcut-provider';
3
4
  export { default as __unstableUseShortcutEventMatch } from './hooks/use-shortcut-event-match';