@wordpress/commands 0.4.0 → 0.6.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 (66) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +28 -0
  3. package/build/components/command-menu.js +78 -72
  4. package/build/components/command-menu.js.map +1 -1
  5. package/build/hooks/use-command-context.js +47 -0
  6. package/build/hooks/use-command-context.js.map +1 -0
  7. package/build/hooks/use-command-loader.js +6 -11
  8. package/build/hooks/use-command-loader.js.map +1 -1
  9. package/build/hooks/use-command.js +4 -3
  10. package/build/hooks/use-command.js.map +1 -1
  11. package/build/index.js +26 -0
  12. package/build/index.js.map +1 -1
  13. package/build/lock-unlock.js +19 -0
  14. package/build/lock-unlock.js.map +1 -0
  15. package/build/private-apis.js +5 -21
  16. package/build/private-apis.js.map +1 -1
  17. package/build/store/actions.js +19 -40
  18. package/build/store/actions.js.map +1 -1
  19. package/build/store/index.js +5 -0
  20. package/build/store/index.js.map +1 -1
  21. package/build/store/private-actions.js +21 -0
  22. package/build/store/private-actions.js.map +1 -0
  23. package/build/store/reducer.js +38 -34
  24. package/build/store/reducer.js.map +1 -1
  25. package/build/store/selectors.js +14 -17
  26. package/build/store/selectors.js.map +1 -1
  27. package/build-module/components/command-menu.js +78 -72
  28. package/build-module/components/command-menu.js.map +1 -1
  29. package/build-module/hooks/use-command-context.js +36 -0
  30. package/build-module/hooks/use-command-context.js.map +1 -0
  31. package/build-module/hooks/use-command-loader.js +6 -11
  32. package/build-module/hooks/use-command-loader.js.map +1 -1
  33. package/build-module/hooks/use-command.js +4 -3
  34. package/build-module/hooks/use-command.js.map +1 -1
  35. package/build-module/index.js +3 -0
  36. package/build-module/index.js.map +1 -1
  37. package/build-module/lock-unlock.js +9 -0
  38. package/build-module/lock-unlock.js.map +1 -0
  39. package/build-module/private-apis.js +3 -15
  40. package/build-module/private-apis.js.map +1 -1
  41. package/build-module/store/actions.js +19 -40
  42. package/build-module/store/actions.js.map +1 -1
  43. package/build-module/store/index.js +3 -0
  44. package/build-module/store/index.js.map +1 -1
  45. package/build-module/store/private-actions.js +14 -0
  46. package/build-module/store/private-actions.js.map +1 -0
  47. package/build-module/store/reducer.js +38 -34
  48. package/build-module/store/reducer.js.map +1 -1
  49. package/build-module/store/selectors.js +11 -16
  50. package/build-module/store/selectors.js.map +1 -1
  51. package/build-style/style-rtl.css +6 -1
  52. package/build-style/style.css +6 -1
  53. package/package.json +9 -9
  54. package/src/components/command-menu.js +38 -35
  55. package/src/components/style.scss +8 -1
  56. package/src/hooks/use-command-context.js +33 -0
  57. package/src/hooks/use-command-loader.js +12 -6
  58. package/src/hooks/use-command.js +5 -3
  59. package/src/index.js +3 -0
  60. package/src/lock-unlock.js +10 -0
  61. package/src/private-apis.js +3 -17
  62. package/src/store/actions.js +17 -26
  63. package/src/store/index.js +3 -0
  64. package/src/store/private-actions.js +13 -0
  65. package/src/store/reducer.js +33 -27
  66. package/src/store/selectors.js +18 -20
@@ -17,11 +17,12 @@ exports.unregisterCommandLoader = unregisterCommandLoader;
17
17
  *
18
18
  * @typedef {Object} WPCommandConfig
19
19
  *
20
- * @property {string} name Command name.
21
- * @property {string} label Command label.
22
- * @property {string=} group Command group.
23
- * @property {JSX.Element} icon Command icon.
24
- * @property {Function} callback Command callback.
20
+ * @property {string} name Command name.
21
+ * @property {string} label Command label.
22
+ * @property {string=} searchLabel Command search label.
23
+ * @property {string=} context Command context.
24
+ * @property {JSX.Element} icon Command icon.
25
+ * @property {Function} callback Command callback.
25
26
  */
26
27
 
27
28
  /**
@@ -33,9 +34,9 @@ exports.unregisterCommandLoader = unregisterCommandLoader;
33
34
  *
34
35
  * @typedef {Object} WPCommandLoaderConfig
35
36
  *
36
- * @property {string} name Command loader name.
37
- * @property {string=} group Command loader group.
38
- * @property {WPCommandLoaderHook} hook Command loader hook.
37
+ * @property {string} name Command loader name.
38
+ * @property {string=} context Command loader context.
39
+ * @property {WPCommandLoaderHook} hook Command loader hook.
39
40
  */
40
41
 
41
42
  /**
@@ -45,38 +46,25 @@ exports.unregisterCommandLoader = unregisterCommandLoader;
45
46
  *
46
47
  * @return {Object} action.
47
48
  */
48
- function registerCommand(_ref) {
49
- let {
50
- name,
51
- label,
52
- icon,
53
- callback,
54
- group = ''
55
- } = _ref;
49
+ function registerCommand(config) {
56
50
  return {
57
51
  type: 'REGISTER_COMMAND',
58
- name,
59
- label,
60
- icon,
61
- callback,
62
- group
52
+ ...config
63
53
  };
64
54
  }
65
55
  /**
66
56
  * Returns an action object used to unregister a command.
67
57
  *
68
- * @param {string} name Command name.
69
- * @param {string} group Command group.
58
+ * @param {string} name Command name.
70
59
  *
71
60
  * @return {Object} action.
72
61
  */
73
62
 
74
63
 
75
- function unregisterCommand(name, group) {
64
+ function unregisterCommand(name) {
76
65
  return {
77
66
  type: 'UNREGISTER_COMMAND',
78
- name,
79
- group
67
+ name
80
68
  };
81
69
  }
82
70
  /**
@@ -88,34 +76,25 @@ function unregisterCommand(name, group) {
88
76
  */
89
77
 
90
78
 
91
- function registerCommandLoader(_ref2) {
92
- let {
93
- name,
94
- group = '',
95
- hook
96
- } = _ref2;
79
+ function registerCommandLoader(config) {
97
80
  return {
98
81
  type: 'REGISTER_COMMAND_LOADER',
99
- name,
100
- group,
101
- hook
82
+ ...config
102
83
  };
103
84
  }
104
85
  /**
105
86
  * Unregister command loader hook.
106
87
  *
107
- * @param {string} name Command loader name.
108
- * @param {string} group Command loader group.
88
+ * @param {string} name Command loader name.
109
89
  *
110
90
  * @return {Object} action.
111
91
  */
112
92
 
113
93
 
114
- function unregisterCommandLoader(name, group) {
94
+ function unregisterCommandLoader(name) {
115
95
  return {
116
96
  type: 'UNREGISTER_COMMAND_LOADER',
117
- name,
118
- group
97
+ name
119
98
  };
120
99
  }
121
100
  /**
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/commands/src/store/actions.js"],"names":["registerCommand","name","label","icon","callback","group","type","unregisterCommand","registerCommandLoader","hook","unregisterCommandLoader","open","close"],"mappings":";;;;;;;;;;;;AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,eAAT,OAAwE;AAAA,MAA9C;AAAEC,IAAAA,IAAF;AAAQC,IAAAA,KAAR;AAAeC,IAAAA,IAAf;AAAqBC,IAAAA,QAArB;AAA+BC,IAAAA,KAAK,GAAG;AAAvC,GAA8C;AAC9E,SAAO;AACNC,IAAAA,IAAI,EAAE,kBADA;AAENL,IAAAA,IAFM;AAGNC,IAAAA,KAHM;AAINC,IAAAA,IAJM;AAKNC,IAAAA,QALM;AAMNC,IAAAA;AANM,GAAP;AAQA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASE,iBAAT,CAA4BN,IAA5B,EAAkCI,KAAlC,EAA0C;AAChD,SAAO;AACNC,IAAAA,IAAI,EAAE,oBADA;AAENL,IAAAA,IAFM;AAGNI,IAAAA;AAHM,GAAP;AAKA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASG,qBAAT,QAA6D;AAAA,MAA7B;AAAEP,IAAAA,IAAF;AAAQI,IAAAA,KAAK,GAAG,EAAhB;AAAoBI,IAAAA;AAApB,GAA6B;AACnE,SAAO;AACNH,IAAAA,IAAI,EAAE,yBADA;AAENL,IAAAA,IAFM;AAGNI,IAAAA,KAHM;AAINI,IAAAA;AAJM,GAAP;AAMA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASC,uBAAT,CAAkCT,IAAlC,EAAwCI,KAAxC,EAAgD;AACtD,SAAO;AACNC,IAAAA,IAAI,EAAE,2BADA;AAENL,IAAAA,IAFM;AAGNI,IAAAA;AAHM,GAAP;AAKA;AAED;AACA;AACA;AACA;AACA;;;AACO,SAASM,IAAT,GAAgB;AACtB,SAAO;AACNL,IAAAA,IAAI,EAAE;AADA,GAAP;AAGA;AAED;AACA;AACA;AACA;AACA;;;AACO,SAASM,KAAT,GAAiB;AACvB,SAAO;AACNN,IAAAA,IAAI,EAAE;AADA,GAAP;AAGA","sourcesContent":["/** @typedef {import('@wordpress/keycodes').WPKeycodeModifier} WPKeycodeModifier */\n\n/**\n * Configuration of a registered keyboard shortcut.\n *\n * @typedef {Object} WPCommandConfig\n *\n * @property {string} name Command name.\n * @property {string} label Command label.\n * @property {string=} group Command group.\n * @property {JSX.Element} icon Command icon.\n * @property {Function} callback Command callback.\n */\n\n/**\n * @typedef {(search: string) => WPCommandConfig[]} WPCommandLoaderHook hoo\n */\n\n/**\n * Command loader config.\n *\n * @typedef {Object} WPCommandLoaderConfig\n *\n * @property {string} name Command loader name.\n * @property {string=} group Command loader group.\n * @property {WPCommandLoaderHook} hook Command loader hook.\n */\n\n/**\n * Returns an action object used to register a new command.\n *\n * @param {WPCommandConfig} config Command config.\n *\n * @return {Object} action.\n */\nexport function registerCommand( { name, label, icon, callback, group = '' } ) {\n\treturn {\n\t\ttype: 'REGISTER_COMMAND',\n\t\tname,\n\t\tlabel,\n\t\ticon,\n\t\tcallback,\n\t\tgroup,\n\t};\n}\n\n/**\n * Returns an action object used to unregister a command.\n *\n * @param {string} name Command name.\n * @param {string} group Command group.\n *\n * @return {Object} action.\n */\nexport function unregisterCommand( name, group ) {\n\treturn {\n\t\ttype: 'UNREGISTER_COMMAND',\n\t\tname,\n\t\tgroup,\n\t};\n}\n\n/**\n * Register command loader.\n *\n * @param {WPCommandLoaderConfig} config Command loader config.\n *\n * @return {Object} action.\n */\nexport function registerCommandLoader( { name, group = '', hook } ) {\n\treturn {\n\t\ttype: 'REGISTER_COMMAND_LOADER',\n\t\tname,\n\t\tgroup,\n\t\thook,\n\t};\n}\n\n/**\n * Unregister command loader hook.\n *\n * @param {string} name Command loader name.\n * @param {string} group Command loader group.\n *\n * @return {Object} action.\n */\nexport function unregisterCommandLoader( name, group ) {\n\treturn {\n\t\ttype: 'UNREGISTER_COMMAND_LOADER',\n\t\tname,\n\t\tgroup,\n\t};\n}\n\n/**\n * Opens the command center.\n *\n * @return {Object} action.\n */\nexport function open() {\n\treturn {\n\t\ttype: 'OPEN',\n\t};\n}\n\n/**\n * Closes the command center.\n *\n * @return {Object} action.\n */\nexport function close() {\n\treturn {\n\t\ttype: 'CLOSE',\n\t};\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/commands/src/store/actions.js"],"names":["registerCommand","config","type","unregisterCommand","name","registerCommandLoader","unregisterCommandLoader","open","close"],"mappings":";;;;;;;;;;;;AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,eAAT,CAA0BC,MAA1B,EAAmC;AACzC,SAAO;AACNC,IAAAA,IAAI,EAAE,kBADA;AAEN,OAAGD;AAFG,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASE,iBAAT,CAA4BC,IAA5B,EAAmC;AACzC,SAAO;AACNF,IAAAA,IAAI,EAAE,oBADA;AAENE,IAAAA;AAFM,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASC,qBAAT,CAAgCJ,MAAhC,EAAyC;AAC/C,SAAO;AACNC,IAAAA,IAAI,EAAE,yBADA;AAEN,OAAGD;AAFG,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASK,uBAAT,CAAkCF,IAAlC,EAAyC;AAC/C,SAAO;AACNF,IAAAA,IAAI,EAAE,2BADA;AAENE,IAAAA;AAFM,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;;;AACO,SAASG,IAAT,GAAgB;AACtB,SAAO;AACNL,IAAAA,IAAI,EAAE;AADA,GAAP;AAGA;AAED;AACA;AACA;AACA;AACA;;;AACO,SAASM,KAAT,GAAiB;AACvB,SAAO;AACNN,IAAAA,IAAI,EAAE;AADA,GAAP;AAGA","sourcesContent":["/** @typedef {import('@wordpress/keycodes').WPKeycodeModifier} WPKeycodeModifier */\n\n/**\n * Configuration of a registered keyboard shortcut.\n *\n * @typedef {Object} WPCommandConfig\n *\n * @property {string} name Command name.\n * @property {string} label Command label.\n * @property {string=} searchLabel Command search label.\n * @property {string=} context Command context.\n * @property {JSX.Element} icon Command icon.\n * @property {Function} callback Command callback.\n */\n\n/**\n * @typedef {(search: string) => WPCommandConfig[]} WPCommandLoaderHook hoo\n */\n\n/**\n * Command loader config.\n *\n * @typedef {Object} WPCommandLoaderConfig\n *\n * @property {string} name Command loader name.\n * @property {string=} context Command loader context.\n * @property {WPCommandLoaderHook} hook Command loader hook.\n */\n\n/**\n * Returns an action object used to register a new command.\n *\n * @param {WPCommandConfig} config Command config.\n *\n * @return {Object} action.\n */\nexport function registerCommand( config ) {\n\treturn {\n\t\ttype: 'REGISTER_COMMAND',\n\t\t...config,\n\t};\n}\n\n/**\n * Returns an action object used to unregister a command.\n *\n * @param {string} name Command name.\n *\n * @return {Object} action.\n */\nexport function unregisterCommand( name ) {\n\treturn {\n\t\ttype: 'UNREGISTER_COMMAND',\n\t\tname,\n\t};\n}\n\n/**\n * Register command loader.\n *\n * @param {WPCommandLoaderConfig} config Command loader config.\n *\n * @return {Object} action.\n */\nexport function registerCommandLoader( config ) {\n\treturn {\n\t\ttype: 'REGISTER_COMMAND_LOADER',\n\t\t...config,\n\t};\n}\n\n/**\n * Unregister command loader hook.\n *\n * @param {string} name Command loader name.\n *\n * @return {Object} action.\n */\nexport function unregisterCommandLoader( name ) {\n\treturn {\n\t\ttype: 'UNREGISTER_COMMAND_LOADER',\n\t\tname,\n\t};\n}\n\n/**\n * Opens the command center.\n *\n * @return {Object} action.\n */\nexport function open() {\n\treturn {\n\t\ttype: 'OPEN',\n\t};\n}\n\n/**\n * Closes the command center.\n *\n * @return {Object} action.\n */\nexport function close() {\n\treturn {\n\t\ttype: 'CLOSE',\n\t};\n}\n"]}
@@ -15,6 +15,10 @@ var actions = _interopRequireWildcard(require("./actions"));
15
15
 
16
16
  var selectors = _interopRequireWildcard(require("./selectors"));
17
17
 
18
+ var privateActions = _interopRequireWildcard(require("./private-actions"));
19
+
20
+ var _lockUnlock = require("../lock-unlock");
21
+
18
22
  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
23
 
20
24
  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; }
@@ -42,4 +46,5 @@ const store = (0, _data.createReduxStore)(STORE_NAME, {
42
46
  });
43
47
  exports.store = store;
44
48
  (0, _data.register)(store);
49
+ (0, _lockUnlock.unlock)(store).registerPrivateActions(privateActions);
45
50
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/commands/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,eAAnB;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/commands';\n\n/**\n * Store definition for the commands 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/commands/src/store/index.js"],"names":["STORE_NAME","store","reducer","actions","selectors","registerPrivateActions","privateActions"],"mappings":";;;;;;;;;AAGA;;AAKA;;AACA;;AACA;;AACA;;AACA;;;;;;AAZA;AACA;AACA;;AAGA;AACA;AACA;AAOA,MAAMA,UAAU,GAAG,eAAnB;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;AACA,wBAAQA,KAAR,EAAgBI,sBAAhB,CAAwCC,cAAxC","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';\nimport * as privateActions from './private-actions';\nimport { unlock } from '../lock-unlock';\n\nconst STORE_NAME = 'core/commands';\n\n/**\n * Store definition for the commands 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 );\nunlock( store ).registerPrivateActions( privateActions );\n"]}
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.setContext = setContext;
7
+
8
+ /**
9
+ * Sets the active context.
10
+ *
11
+ * @param {string} context Context.
12
+ *
13
+ * @return {Object} action.
14
+ */
15
+ function setContext(context) {
16
+ return {
17
+ type: 'SET_CONTEXT',
18
+ context
19
+ };
20
+ }
21
+ //# sourceMappingURL=private-actions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/commands/src/store/private-actions.js"],"names":["setContext","context","type"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,UAAT,CAAqBC,OAArB,EAA+B;AACrC,SAAO;AACNC,IAAAA,IAAI,EAAE,aADA;AAEND,IAAAA;AAFM,GAAP;AAIA","sourcesContent":["/**\n * Sets the active context.\n *\n * @param {string} context Context.\n *\n * @return {Object} action.\n */\nexport function setContext( context ) {\n\treturn {\n\t\ttype: 'SET_CONTEXT',\n\t\tcontext,\n\t};\n}\n"]}
@@ -19,21 +19,17 @@ var _data = require("@wordpress/data");
19
19
  *
20
20
  * @return {Object} Updated state.
21
21
  */
22
- function commands() {
23
- let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
24
- let action = arguments.length > 1 ? arguments[1] : undefined;
25
-
22
+ function commands(state = {}, action) {
26
23
  switch (action.type) {
27
24
  case 'REGISTER_COMMAND':
28
25
  return { ...state,
29
- [action.group]: { ...state[action.group],
30
- [action.name]: {
31
- name: action.name,
32
- label: action.label,
33
- group: action.group,
34
- callback: action.callback,
35
- icon: action.icon
36
- }
26
+ [action.name]: {
27
+ name: action.name,
28
+ label: action.label,
29
+ searchLabel: action.searchLabel,
30
+ context: action.context,
31
+ callback: action.callback,
32
+ icon: action.icon
37
33
  }
38
34
  };
39
35
 
@@ -42,10 +38,8 @@ function commands() {
42
38
  const {
43
39
  [action.name]: _,
44
40
  ...remainingState
45
- } = state === null || state === void 0 ? void 0 : state[action.group];
46
- return { ...state,
47
- [action.group]: remainingState
48
- };
41
+ } = state;
42
+ return remainingState;
49
43
  }
50
44
  }
51
45
 
@@ -61,18 +55,14 @@ function commands() {
61
55
  */
62
56
 
63
57
 
64
- function commandLoaders() {
65
- let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
66
- let action = arguments.length > 1 ? arguments[1] : undefined;
67
-
58
+ function commandLoaders(state = {}, action) {
68
59
  switch (action.type) {
69
60
  case 'REGISTER_COMMAND_LOADER':
70
61
  return { ...state,
71
- [action.group]: { ...state[action.group],
72
- [action.name]: {
73
- name: action.name,
74
- hook: action.hook
75
- }
62
+ [action.name]: {
63
+ name: action.name,
64
+ context: action.context,
65
+ hook: action.hook
76
66
  }
77
67
  };
78
68
 
@@ -81,10 +71,8 @@ function commandLoaders() {
81
71
  const {
82
72
  [action.name]: _,
83
73
  ...remainingState
84
- } = state === null || state === void 0 ? void 0 : state[action.group];
85
- return { ...state,
86
- [action.group]: remainingState
87
- };
74
+ } = state;
75
+ return remainingState;
88
76
  }
89
77
  }
90
78
 
@@ -100,10 +88,7 @@ function commandLoaders() {
100
88
  */
101
89
 
102
90
 
103
- function isOpen() {
104
- let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
105
- let action = arguments.length > 1 ? arguments[1] : undefined;
106
-
91
+ function isOpen(state = false, action) {
107
92
  switch (action.type) {
108
93
  case 'OPEN':
109
94
  return true;
@@ -114,11 +99,30 @@ function isOpen() {
114
99
 
115
100
  return state;
116
101
  }
102
+ /**
103
+ * Reducer returning the command center's active context.
104
+ *
105
+ * @param {Object} state Current state.
106
+ * @param {Object} action Dispatched action.
107
+ *
108
+ * @return {boolean} Updated state.
109
+ */
110
+
111
+
112
+ function context(state = 'root', action) {
113
+ switch (action.type) {
114
+ case 'SET_CONTEXT':
115
+ return action.context;
116
+ }
117
+
118
+ return state;
119
+ }
117
120
 
118
121
  const reducer = (0, _data.combineReducers)({
119
122
  commands,
120
123
  commandLoaders,
121
- isOpen
124
+ isOpen,
125
+ context
122
126
  });
123
127
  var _default = reducer;
124
128
  exports.default = _default;
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/commands/src/store/reducer.js"],"names":["commands","state","action","type","group","name","label","callback","icon","_","remainingState","commandLoaders","hook","isOpen","reducer"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,QAAT,GAAwC;AAAA,MAArBC,KAAqB,uEAAb,EAAa;AAAA,MAATC,MAAS;;AACvC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,kBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,KAAT,GAAkB,EACjB,GAAGH,KAAK,CAAEC,MAAM,CAACE,KAAT,CADS;AAEjB,WAAEF,MAAM,CAACG,IAAT,GAAiB;AAChBA,YAAAA,IAAI,EAAEH,MAAM,CAACG,IADG;AAEhBC,YAAAA,KAAK,EAAEJ,MAAM,CAACI,KAFE;AAGhBF,YAAAA,KAAK,EAAEF,MAAM,CAACE,KAHE;AAIhBG,YAAAA,QAAQ,EAAEL,MAAM,CAACK,QAJD;AAKhBC,YAAAA,IAAI,EAAEN,MAAM,CAACM;AALG;AAFA;AAFZ,OAAP;;AAaD,SAAK,oBAAL;AAA2B;AAC1B,cAAM;AAAE,WAAEN,MAAM,CAACG,IAAT,GAAiBI,CAAnB;AAAsB,aAAGC;AAAzB,YACLT,KADK,aACLA,KADK,uBACLA,KAAK,CAAIC,MAAM,CAACE,KAAX,CADN;AAEA,eAAO,EACN,GAAGH,KADG;AAEN,WAAEC,MAAM,CAACE,KAAT,GAAkBM;AAFZ,SAAP;AAIA;AAtBF;;AAyBA,SAAOT,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASU,cAAT,GAA8C;AAAA,MAArBV,KAAqB,uEAAb,EAAa;AAAA,MAATC,MAAS;;AAC7C,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,yBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,KAAT,GAAkB,EACjB,GAAGH,KAAK,CAAEC,MAAM,CAACE,KAAT,CADS;AAEjB,WAAEF,MAAM,CAACG,IAAT,GAAiB;AAChBA,YAAAA,IAAI,EAAEH,MAAM,CAACG,IADG;AAEhBO,YAAAA,IAAI,EAAEV,MAAM,CAACU;AAFG;AAFA;AAFZ,OAAP;;AAUD,SAAK,2BAAL;AAAkC;AACjC,cAAM;AAAE,WAAEV,MAAM,CAACG,IAAT,GAAiBI,CAAnB;AAAsB,aAAGC;AAAzB,YACLT,KADK,aACLA,KADK,uBACLA,KAAK,CAAIC,MAAM,CAACE,KAAX,CADN;AAEA,eAAO,EACN,GAAGH,KADG;AAEN,WAAEC,MAAM,CAACE,KAAT,GAAkBM;AAFZ,SAAP;AAIA;AAnBF;;AAsBA,SAAOT,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASY,MAAT,GAAyC;AAAA,MAAxBZ,KAAwB,uEAAhB,KAAgB;AAAA,MAATC,MAAS;;AACxC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,MAAL;AACC,aAAO,IAAP;;AACD,SAAK,OAAL;AACC,aAAO,KAAP;AAJF;;AAOA,SAAOF,KAAP;AACA;;AAED,MAAMa,OAAO,GAAG,2BAAiB;AAChCd,EAAAA,QADgC;AAEhCW,EAAAA,cAFgC;AAGhCE,EAAAA;AAHgC,CAAjB,CAAhB;eAMeC,O","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { combineReducers } from '@wordpress/data';\n\n/**\n * Reducer returning the registered commands\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction commands( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_COMMAND':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.group ]: {\n\t\t\t\t\t...state[ action.group ],\n\t\t\t\t\t[ action.name ]: {\n\t\t\t\t\t\tname: action.name,\n\t\t\t\t\t\tlabel: action.label,\n\t\t\t\t\t\tgroup: action.group,\n\t\t\t\t\t\tcallback: action.callback,\n\t\t\t\t\t\ticon: action.icon,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_COMMAND': {\n\t\t\tconst { [ action.name ]: _, ...remainingState } =\n\t\t\t\tstate?.[ action.group ];\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.group ]: remainingState,\n\t\t\t};\n\t\t}\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command loaders\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction commandLoaders( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_COMMAND_LOADER':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.group ]: {\n\t\t\t\t\t...state[ action.group ],\n\t\t\t\t\t[ action.name ]: {\n\t\t\t\t\t\tname: action.name,\n\t\t\t\t\t\thook: action.hook,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_COMMAND_LOADER': {\n\t\t\tconst { [ action.name ]: _, ...remainingState } =\n\t\t\t\tstate?.[ action.group ];\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.group ]: remainingState,\n\t\t\t};\n\t\t}\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command center open state.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {boolean} Updated state.\n */\nfunction isOpen( state = false, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'OPEN':\n\t\t\treturn true;\n\t\tcase 'CLOSE':\n\t\t\treturn false;\n\t}\n\n\treturn state;\n}\n\nconst reducer = combineReducers( {\n\tcommands,\n\tcommandLoaders,\n\tisOpen,\n} );\n\nexport default reducer;\n"]}
1
+ {"version":3,"sources":["@wordpress/commands/src/store/reducer.js"],"names":["commands","state","action","type","name","label","searchLabel","context","callback","icon","_","remainingState","commandLoaders","hook","isOpen","reducer"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,QAAT,CAAmBC,KAAK,GAAG,EAA3B,EAA+BC,MAA/B,EAAwC;AACvC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,kBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,IAAT,GAAiB;AAChBA,UAAAA,IAAI,EAAEF,MAAM,CAACE,IADG;AAEhBC,UAAAA,KAAK,EAAEH,MAAM,CAACG,KAFE;AAGhBC,UAAAA,WAAW,EAAEJ,MAAM,CAACI,WAHJ;AAIhBC,UAAAA,OAAO,EAAEL,MAAM,CAACK,OAJA;AAKhBC,UAAAA,QAAQ,EAAEN,MAAM,CAACM,QALD;AAMhBC,UAAAA,IAAI,EAAEP,MAAM,CAACO;AANG;AAFX,OAAP;;AAWD,SAAK,oBAAL;AAA2B;AAC1B,cAAM;AAAE,WAAEP,MAAM,CAACE,IAAT,GAAiBM,CAAnB;AAAsB,aAAGC;AAAzB,YAA4CV,KAAlD;AACA,eAAOU,cAAP;AACA;AAhBF;;AAmBA,SAAOV,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASW,cAAT,CAAyBX,KAAK,GAAG,EAAjC,EAAqCC,MAArC,EAA8C;AAC7C,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,yBAAL;AACC,aAAO,EACN,GAAGF,KADG;AAEN,SAAEC,MAAM,CAACE,IAAT,GAAiB;AAChBA,UAAAA,IAAI,EAAEF,MAAM,CAACE,IADG;AAEhBG,UAAAA,OAAO,EAAEL,MAAM,CAACK,OAFA;AAGhBM,UAAAA,IAAI,EAAEX,MAAM,CAACW;AAHG;AAFX,OAAP;;AAQD,SAAK,2BAAL;AAAkC;AACjC,cAAM;AAAE,WAAEX,MAAM,CAACE,IAAT,GAAiBM,CAAnB;AAAsB,aAAGC;AAAzB,YAA4CV,KAAlD;AACA,eAAOU,cAAP;AACA;AAbF;;AAgBA,SAAOV,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASa,MAAT,CAAiBb,KAAK,GAAG,KAAzB,EAAgCC,MAAhC,EAAyC;AACxC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,MAAL;AACC,aAAO,IAAP;;AACD,SAAK,OAAL;AACC,aAAO,KAAP;AAJF;;AAOA,SAAOF,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASM,OAAT,CAAkBN,KAAK,GAAG,MAA1B,EAAkCC,MAAlC,EAA2C;AAC1C,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,aAAL;AACC,aAAOD,MAAM,CAACK,OAAd;AAFF;;AAKA,SAAON,KAAP;AACA;;AAED,MAAMc,OAAO,GAAG,2BAAiB;AAChCf,EAAAA,QADgC;AAEhCY,EAAAA,cAFgC;AAGhCE,EAAAA,MAHgC;AAIhCP,EAAAA;AAJgC,CAAjB,CAAhB;eAOeQ,O","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { combineReducers } from '@wordpress/data';\n\n/**\n * Reducer returning the registered commands\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction commands( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_COMMAND':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.name ]: {\n\t\t\t\t\tname: action.name,\n\t\t\t\t\tlabel: action.label,\n\t\t\t\t\tsearchLabel: action.searchLabel,\n\t\t\t\t\tcontext: action.context,\n\t\t\t\t\tcallback: action.callback,\n\t\t\t\t\ticon: action.icon,\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_COMMAND': {\n\t\t\tconst { [ action.name ]: _, ...remainingState } = state;\n\t\t\treturn remainingState;\n\t\t}\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command loaders\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction commandLoaders( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_COMMAND_LOADER':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.name ]: {\n\t\t\t\t\tname: action.name,\n\t\t\t\t\tcontext: action.context,\n\t\t\t\t\thook: action.hook,\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_COMMAND_LOADER': {\n\t\t\tconst { [ action.name ]: _, ...remainingState } = state;\n\t\t\treturn remainingState;\n\t\t}\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command center open state.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {boolean} Updated state.\n */\nfunction isOpen( state = false, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'OPEN':\n\t\t\treturn true;\n\t\tcase 'CLOSE':\n\t\t\treturn false;\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command center's active context.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {boolean} Updated state.\n */\nfunction context( state = 'root', action ) {\n\tswitch ( action.type ) {\n\t\tcase 'SET_CONTEXT':\n\t\t\treturn action.context;\n\t}\n\n\treturn state;\n}\n\nconst reducer = combineReducers( {\n\tcommands,\n\tcommandLoaders,\n\tisOpen,\n\tcontext,\n} );\n\nexport default reducer;\n"]}
@@ -5,7 +5,8 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.getGroups = exports.getCommands = exports.getCommandLoaders = void 0;
8
+ exports.getCommands = exports.getCommandLoaders = void 0;
9
+ exports.getContext = getContext;
9
10
  exports.isOpen = isOpen;
10
11
 
11
12
  var _rememo = _interopRequireDefault(require("rememo"));
@@ -13,26 +14,22 @@ var _rememo = _interopRequireDefault(require("rememo"));
13
14
  /**
14
15
  * External dependencies
15
16
  */
16
- function unique(array) {
17
- return array.filter((value, index, current) => current.indexOf(value) === index);
18
- }
19
-
20
- const getGroups = (0, _rememo.default)(state => unique(Object.keys(state.commands).concat(Object.keys(state.commandLoaders))), state => [state.commands, state.commandLoaders]);
21
- exports.getGroups = getGroups;
22
- const getCommands = (0, _rememo.default)((state, group) => {
23
- var _state$commands$group;
24
-
25
- return Object.values((_state$commands$group = state.commands[group]) !== null && _state$commands$group !== void 0 ? _state$commands$group : {});
26
- }, (state, group) => [state.commands[group]]);
17
+ const getCommands = (0, _rememo.default)((state, contextual = false) => Object.values(state.commands).filter(command => {
18
+ const isContextual = command.context && command.context === state.context;
19
+ return contextual ? isContextual : !isContextual;
20
+ }), state => [state.commands, state.context]);
27
21
  exports.getCommands = getCommands;
28
- const getCommandLoaders = (0, _rememo.default)((state, group) => {
29
- var _state$commandLoaders;
30
-
31
- return Object.values((_state$commandLoaders = state.commandLoaders[group]) !== null && _state$commandLoaders !== void 0 ? _state$commandLoaders : {});
32
- }, (state, group) => [state.commandLoaders[group]]);
22
+ const getCommandLoaders = (0, _rememo.default)((state, contextual = false) => Object.values(state.commandLoaders).filter(loader => {
23
+ const isContextual = loader.context && loader.context === state.context;
24
+ return contextual ? isContextual : !isContextual;
25
+ }), state => [state.commandLoaders, state.context]);
33
26
  exports.getCommandLoaders = getCommandLoaders;
34
27
 
35
28
  function isOpen(state) {
36
29
  return state.isOpen;
37
30
  }
31
+
32
+ function getContext(state) {
33
+ return state.context;
34
+ }
38
35
  //# sourceMappingURL=selectors.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/commands/src/store/selectors.js"],"names":["unique","array","filter","value","index","current","indexOf","getGroups","state","Object","keys","commands","concat","commandLoaders","getCommands","group","values","getCommandLoaders","isOpen"],"mappings":";;;;;;;;;;AAGA;;AAHA;AACA;AACA;AAGA,SAASA,MAAT,CAAiBC,KAAjB,EAAyB;AACxB,SAAOA,KAAK,CAACC,MAAN,CACN,CAAEC,KAAF,EAASC,KAAT,EAAgBC,OAAhB,KAA6BA,OAAO,CAACC,OAAR,CAAiBH,KAAjB,MAA6BC,KADpD,CAAP;AAGA;;AAEM,MAAMG,SAAS,GAAG,qBACtBC,KAAF,IACCR,MAAM,CACLS,MAAM,CAACC,IAAP,CAAaF,KAAK,CAACG,QAAnB,EAA8BC,MAA9B,CACCH,MAAM,CAACC,IAAP,CAAaF,KAAK,CAACK,cAAnB,CADD,CADK,CAFiB,EAOtBL,KAAF,IAAa,CAAEA,KAAK,CAACG,QAAR,EAAkBH,KAAK,CAACK,cAAxB,CAPW,CAAlB;;AAUA,MAAMC,WAAW,GAAG,qBAC1B,CAAEN,KAAF,EAASO,KAAT;AAAA;;AAAA,SAAoBN,MAAM,CAACO,MAAP,0BAAeR,KAAK,CAACG,QAAN,CAAgBI,KAAhB,CAAf,yEAA0C,EAA1C,CAApB;AAAA,CAD0B,EAE1B,CAAEP,KAAF,EAASO,KAAT,KAAoB,CAAEP,KAAK,CAACG,QAAN,CAAgBI,KAAhB,CAAF,CAFM,CAApB;;AAKA,MAAME,iBAAiB,GAAG,qBAChC,CAAET,KAAF,EAASO,KAAT;AAAA;;AAAA,SAAoBN,MAAM,CAACO,MAAP,0BAAeR,KAAK,CAACK,cAAN,CAAsBE,KAAtB,CAAf,yEAAgD,EAAhD,CAApB;AAAA,CADgC,EAEhC,CAAEP,KAAF,EAASO,KAAT,KAAoB,CAAEP,KAAK,CAACK,cAAN,CAAsBE,KAAtB,CAAF,CAFY,CAA1B;;;AAKA,SAASG,MAAT,CAAiBV,KAAjB,EAAyB;AAC/B,SAAOA,KAAK,CAACU,MAAb;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport createSelector from 'rememo';\n\nfunction unique( array ) {\n\treturn array.filter(\n\t\t( value, index, current ) => current.indexOf( value ) === index\n\t);\n}\n\nexport const getGroups = createSelector(\n\t( state ) =>\n\t\tunique(\n\t\t\tObject.keys( state.commands ).concat(\n\t\t\t\tObject.keys( state.commandLoaders )\n\t\t\t)\n\t\t),\n\t( state ) => [ state.commands, state.commandLoaders ]\n);\n\nexport const getCommands = createSelector(\n\t( state, group ) => Object.values( state.commands[ group ] ?? {} ),\n\t( state, group ) => [ state.commands[ group ] ]\n);\n\nexport const getCommandLoaders = createSelector(\n\t( state, group ) => Object.values( state.commandLoaders[ group ] ?? {} ),\n\t( state, group ) => [ state.commandLoaders[ group ] ]\n);\n\nexport function isOpen( state ) {\n\treturn state.isOpen;\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/commands/src/store/selectors.js"],"names":["getCommands","state","contextual","Object","values","commands","filter","command","isContextual","context","getCommandLoaders","commandLoaders","loader","isOpen","getContext"],"mappings":";;;;;;;;;;;AAGA;;AAHA;AACA;AACA;AAGO,MAAMA,WAAW,GAAG,qBAC1B,CAAEC,KAAF,EAASC,UAAU,GAAG,KAAtB,KACCC,MAAM,CAACC,MAAP,CAAeH,KAAK,CAACI,QAArB,EAAgCC,MAAhC,CAA0CC,OAAF,IAAe;AACtD,QAAMC,YAAY,GACjBD,OAAO,CAACE,OAAR,IAAmBF,OAAO,CAACE,OAAR,KAAoBR,KAAK,CAACQ,OAD9C;AAEA,SAAOP,UAAU,GAAGM,YAAH,GAAkB,CAAEA,YAArC;AACA,CAJD,CAFyB,EAOxBP,KAAF,IAAa,CAAEA,KAAK,CAACI,QAAR,EAAkBJ,KAAK,CAACQ,OAAxB,CAPa,CAApB;;AAUA,MAAMC,iBAAiB,GAAG,qBAChC,CAAET,KAAF,EAASC,UAAU,GAAG,KAAtB,KACCC,MAAM,CAACC,MAAP,CAAeH,KAAK,CAACU,cAArB,EAAsCL,MAAtC,CAAgDM,MAAF,IAAc;AAC3D,QAAMJ,YAAY,GACjBI,MAAM,CAACH,OAAP,IAAkBG,MAAM,CAACH,OAAP,KAAmBR,KAAK,CAACQ,OAD5C;AAEA,SAAOP,UAAU,GAAGM,YAAH,GAAkB,CAAEA,YAArC;AACA,CAJD,CAF+B,EAO9BP,KAAF,IAAa,CAAEA,KAAK,CAACU,cAAR,EAAwBV,KAAK,CAACQ,OAA9B,CAPmB,CAA1B;;;AAUA,SAASI,MAAT,CAAiBZ,KAAjB,EAAyB;AAC/B,SAAOA,KAAK,CAACY,MAAb;AACA;;AAEM,SAASC,UAAT,CAAqBb,KAArB,EAA6B;AACnC,SAAOA,KAAK,CAACQ,OAAb;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport createSelector from 'rememo';\n\nexport const getCommands = createSelector(\n\t( state, contextual = false ) =>\n\t\tObject.values( state.commands ).filter( ( command ) => {\n\t\t\tconst isContextual =\n\t\t\t\tcommand.context && command.context === state.context;\n\t\t\treturn contextual ? isContextual : ! isContextual;\n\t\t} ),\n\t( state ) => [ state.commands, state.context ]\n);\n\nexport const getCommandLoaders = createSelector(\n\t( state, contextual = false ) =>\n\t\tObject.values( state.commandLoaders ).filter( ( loader ) => {\n\t\t\tconst isContextual =\n\t\t\t\tloader.context && loader.context === state.context;\n\t\t\treturn contextual ? isContextual : ! isContextual;\n\t\t} ),\n\t( state ) => [ state.commandLoaders, state.context ]\n);\n\nexport function isOpen( state ) {\n\treturn state.isOpen;\n}\n\nexport function getContext( state ) {\n\treturn state.context;\n}\n"]}
@@ -20,16 +20,15 @@ import { Icon } from '@wordpress/icons';
20
20
 
21
21
  import { store as commandsStore } from '../store';
22
22
 
23
- function CommandMenuLoader(_ref) {
23
+ function CommandMenuLoader({
24
+ name,
25
+ search,
26
+ hook,
27
+ setLoader,
28
+ close
29
+ }) {
24
30
  var _hook;
25
31
 
26
- let {
27
- name,
28
- search,
29
- hook,
30
- setLoader,
31
- close
32
- } = _ref;
33
32
  const {
34
33
  isLoading,
35
34
  commands = []
@@ -39,30 +38,38 @@ function CommandMenuLoader(_ref) {
39
38
  useEffect(() => {
40
39
  setLoader(name, isLoading);
41
40
  }, [setLoader, name, isLoading]);
42
- return createElement(Fragment, null, createElement(Command.List, null, isLoading && createElement(Command.Loading, null, __('Searching…')), commands.map(command => createElement(Command.Item, {
43
- key: command.name,
44
- value: command.name,
45
- onSelect: () => command.callback({
46
- close
47
- })
48
- }, createElement(HStack, {
49
- alignment: "left",
50
- className: "commands-command-menu__item"
51
- }, createElement(Icon, {
52
- icon: command.icon
53
- }), createElement("span", null, createElement(TextHighlight, {
54
- text: command.label,
55
- highlight: search
56
- })))))));
41
+
42
+ if (!commands.length) {
43
+ return null;
44
+ }
45
+
46
+ return createElement(Fragment, null, createElement(Command.List, null, commands.map(command => {
47
+ var _command$searchLabel;
48
+
49
+ return createElement(Command.Item, {
50
+ key: command.name,
51
+ value: (_command$searchLabel = command.searchLabel) !== null && _command$searchLabel !== void 0 ? _command$searchLabel : command.label,
52
+ onSelect: () => command.callback({
53
+ close
54
+ })
55
+ }, createElement(HStack, {
56
+ alignment: "left",
57
+ className: "commands-command-menu__item"
58
+ }, createElement(Icon, {
59
+ icon: command.icon
60
+ }), createElement("span", null, createElement(TextHighlight, {
61
+ text: command.label,
62
+ highlight: search
63
+ }))));
64
+ })));
57
65
  }
58
66
 
59
- export function CommandMenuLoaderWrapper(_ref2) {
60
- let {
61
- hook,
62
- search,
63
- setLoader,
64
- close
65
- } = _ref2;
67
+ export function CommandMenuLoaderWrapper({
68
+ hook,
69
+ search,
70
+ setLoader,
71
+ close
72
+ }) {
66
73
  // The "hook" prop is actually a custom React hook
67
74
  // so to avoid breaking the rules of hooks
68
75
  // the CommandMenuLoaderWrapper component need to be
@@ -84,13 +91,12 @@ export function CommandMenuLoaderWrapper(_ref2) {
84
91
  close: close
85
92
  });
86
93
  }
87
- export function CommandMenuGroup(_ref3) {
88
- let {
89
- group,
90
- search,
91
- setLoader,
92
- close
93
- } = _ref3;
94
+ export function CommandMenuGroup({
95
+ isContextual,
96
+ search,
97
+ setLoader,
98
+ close
99
+ }) {
94
100
  const {
95
101
  commands,
96
102
  loaders
@@ -100,25 +106,34 @@ export function CommandMenuGroup(_ref3) {
100
106
  getCommandLoaders
101
107
  } = select(commandsStore);
102
108
  return {
103
- commands: getCommands(group),
104
- loaders: getCommandLoaders(group)
109
+ commands: getCommands(isContextual),
110
+ loaders: getCommandLoaders(isContextual)
105
111
  };
106
- }, [group]);
107
- return createElement(Command.Group, null, commands.map(command => createElement(Command.Item, {
108
- key: command.name,
109
- value: command.name,
110
- onSelect: () => command.callback({
111
- close
112
- })
113
- }, createElement(HStack, {
114
- alignment: "left",
115
- className: "commands-command-menu__item"
116
- }, createElement(Icon, {
117
- icon: command.icon
118
- }), createElement("span", null, createElement(TextHighlight, {
119
- text: command.label,
120
- highlight: search
121
- }))))), loaders.map(loader => createElement(CommandMenuLoaderWrapper, {
112
+ }, [isContextual]);
113
+
114
+ if (!commands.length && !loaders.length) {
115
+ return null;
116
+ }
117
+
118
+ return createElement(Command.Group, null, commands.map(command => {
119
+ var _command$searchLabel2;
120
+
121
+ return createElement(Command.Item, {
122
+ key: command.name,
123
+ value: (_command$searchLabel2 = command.searchLabel) !== null && _command$searchLabel2 !== void 0 ? _command$searchLabel2 : command.label,
124
+ onSelect: () => command.callback({
125
+ close
126
+ })
127
+ }, createElement(HStack, {
128
+ alignment: "left",
129
+ className: "commands-command-menu__item"
130
+ }, createElement(Icon, {
131
+ icon: command.icon
132
+ }), createElement("span", null, createElement(TextHighlight, {
133
+ text: command.label,
134
+ highlight: search
135
+ }))));
136
+ }), loaders.map(loader => createElement(CommandMenuLoaderWrapper, {
122
137
  key: loader.name,
123
138
  hook: loader.hook,
124
139
  search: search,
@@ -131,19 +146,7 @@ export function CommandMenu() {
131
146
  registerShortcut
132
147
  } = useDispatch(keyboardShortcutsStore);
133
148
  const [search, setSearch] = useState('');
134
- const {
135
- groups,
136
- isOpen
137
- } = useSelect(select => {
138
- const {
139
- getGroups,
140
- isOpen: _isOpen
141
- } = select(commandsStore);
142
- return {
143
- groups: getGroups(),
144
- isOpen: _isOpen()
145
- };
146
- }, []);
149
+ const isOpen = useSelect(select => select(commandsStore).isOpen(), []);
147
150
  const {
148
151
  open,
149
152
  close
@@ -209,12 +212,15 @@ export function CommandMenu() {
209
212
  value: search,
210
213
  onValueChange: setSearch,
211
214
  placeholder: __('Type a command or search')
212
- })), search && createElement(Command.List, null, !isLoading && createElement(Command.Empty, null, __('No results found.')), groups.map(group => createElement(CommandMenuGroup, {
213
- key: group,
214
- group: group,
215
+ })), createElement(Command.List, null, search && !isLoading && createElement(Command.Empty, null, __('No results found.')), createElement(CommandMenuGroup, {
216
+ search: search,
217
+ setLoader: setLoader,
218
+ close: closeAndReset,
219
+ isContextual: true
220
+ }), search && createElement(CommandMenuGroup, {
215
221
  search: search,
216
222
  setLoader: setLoader,
217
223
  close: closeAndReset
218
- }))))));
224
+ })))));
219
225
  }
220
226
  //# sourceMappingURL=command-menu.js.map