@wordpress/block-directory 5.19.0 → 5.19.1

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.
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
3
  Object.defineProperty(exports, "__esModule", {
5
4
  value: true
6
5
  });
@@ -13,14 +12,11 @@ exports.isInstalling = isInstalling;
13
12
  exports.isRequestingDownloadableBlocks = isRequestingDownloadableBlocks;
14
13
  var _data = require("@wordpress/data");
15
14
  var _blockEditor = require("@wordpress/block-editor");
16
- var _hasBlockType = _interopRequireDefault(require("./utils/has-block-type"));
17
15
  /**
18
16
  * WordPress dependencies
19
17
  */
20
18
 
21
- /**
22
- * Internal dependencies
23
- */
19
+ const EMPTY_ARRAY = [];
24
20
 
25
21
  /**
26
22
  * Returns true if application is requesting for downloadable blocks.
@@ -45,7 +41,7 @@ function isRequestingDownloadableBlocks(state, filterValue) {
45
41
  */
46
42
  function getDownloadableBlocks(state, filterValue) {
47
43
  var _state$downloadableBl2;
48
- return (_state$downloadableBl2 = state.downloadableBlocks[filterValue]?.results) !== null && _state$downloadableBl2 !== void 0 ? _state$downloadableBl2 : [];
44
+ return (_state$downloadableBl2 = state.downloadableBlocks[filterValue]?.results) !== null && _state$downloadableBl2 !== void 0 ? _state$downloadableBl2 : EMPTY_ARRAY;
49
45
  }
50
46
 
51
47
  /**
@@ -69,10 +65,22 @@ function getInstalledBlockTypes(state) {
69
65
  * @return {Array} Block type items.
70
66
  */
71
67
  const getNewBlockTypes = exports.getNewBlockTypes = (0, _data.createRegistrySelector)(select => (0, _data.createSelector)(state => {
72
- const usedBlockTree = select(_blockEditor.store).getBlocks();
73
68
  const installedBlockTypes = getInstalledBlockTypes(state);
74
- return installedBlockTypes.filter(blockType => (0, _hasBlockType.default)(blockType, usedBlockTree));
75
- }, state => [getInstalledBlockTypes(state), select(_blockEditor.store).getBlocks()]));
69
+ if (!installedBlockTypes.length) {
70
+ return EMPTY_ARRAY;
71
+ }
72
+ const {
73
+ getBlockName,
74
+ getClientIdsWithDescendants
75
+ } = select(_blockEditor.store);
76
+ const installedBlockNames = installedBlockTypes.map(blockType => blockType.name);
77
+ const foundBlockNames = getClientIdsWithDescendants().flatMap(clientId => {
78
+ const blockName = getBlockName(clientId);
79
+ return installedBlockNames.includes(blockName) ? blockName : [];
80
+ });
81
+ const newBlockTypes = installedBlockTypes.filter(blockType => foundBlockNames.includes(blockType.name));
82
+ return newBlockTypes.length > 0 ? newBlockTypes : EMPTY_ARRAY;
83
+ }, state => [getInstalledBlockTypes(state), select(_blockEditor.store).getClientIdsWithDescendants()]));
76
84
 
77
85
  /**
78
86
  * Returns the block types that have been installed on the server but are not
@@ -83,10 +91,22 @@ const getNewBlockTypes = exports.getNewBlockTypes = (0, _data.createRegistrySele
83
91
  * @return {Array} Block type items.
84
92
  */
85
93
  const getUnusedBlockTypes = exports.getUnusedBlockTypes = (0, _data.createRegistrySelector)(select => (0, _data.createSelector)(state => {
86
- const usedBlockTree = select(_blockEditor.store).getBlocks();
87
94
  const installedBlockTypes = getInstalledBlockTypes(state);
88
- return installedBlockTypes.filter(blockType => !(0, _hasBlockType.default)(blockType, usedBlockTree));
89
- }, state => [getInstalledBlockTypes(state), select(_blockEditor.store).getBlocks()]));
95
+ if (!installedBlockTypes.length) {
96
+ return EMPTY_ARRAY;
97
+ }
98
+ const {
99
+ getBlockName,
100
+ getClientIdsWithDescendants
101
+ } = select(_blockEditor.store);
102
+ const installedBlockNames = installedBlockTypes.map(blockType => blockType.name);
103
+ const foundBlockNames = getClientIdsWithDescendants().flatMap(clientId => {
104
+ const blockName = getBlockName(clientId);
105
+ return installedBlockNames.includes(blockName) ? blockName : [];
106
+ });
107
+ const unusedBlockTypes = installedBlockTypes.filter(blockType => !foundBlockNames.includes(blockType.name));
108
+ return unusedBlockTypes.length > 0 ? unusedBlockTypes : EMPTY_ARRAY;
109
+ }, state => [getInstalledBlockTypes(state), select(_blockEditor.store).getClientIdsWithDescendants()]));
90
110
 
91
111
  /**
92
112
  * Returns true if a block plugin install is in progress.
@@ -1 +1 @@
1
- {"version":3,"names":["_data","require","_blockEditor","_hasBlockType","_interopRequireDefault","isRequestingDownloadableBlocks","state","filterValue","_state$downloadableBl","downloadableBlocks","isRequesting","getDownloadableBlocks","_state$downloadableBl2","results","getInstalledBlockTypes","blockManagement","installedBlockTypes","getNewBlockTypes","exports","createRegistrySelector","select","createSelector","usedBlockTree","blockEditorStore","getBlocks","filter","blockType","hasBlockType","getUnusedBlockTypes","isInstalling","blockId","getErrorNotices","errorNotices","getErrorNoticeForBlock"],"sources":["@wordpress/block-directory/src/store/selectors.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createSelector, createRegistrySelector } from '@wordpress/data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport hasBlockType from './utils/has-block-type';\n\n/**\n * Returns true if application is requesting for downloadable blocks.\n *\n * @param {Object} state Global application state.\n * @param {string} filterValue Search string.\n *\n * @return {boolean} Whether a request is in progress for the blocks list.\n */\nexport function isRequestingDownloadableBlocks( state, filterValue ) {\n\treturn state.downloadableBlocks[ filterValue ]?.isRequesting ?? false;\n}\n\n/**\n * Returns the available uninstalled blocks.\n *\n * @param {Object} state Global application state.\n * @param {string} filterValue Search string.\n *\n * @return {Array} Downloadable blocks.\n */\nexport function getDownloadableBlocks( state, filterValue ) {\n\treturn state.downloadableBlocks[ filterValue ]?.results ?? [];\n}\n\n/**\n * Returns the block types that have been installed on the server in this\n * session.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items\n */\nexport function getInstalledBlockTypes( state ) {\n\treturn state.blockManagement.installedBlockTypes;\n}\n\n/**\n * Returns block types that have been installed on the server and used in the\n * current post.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items.\n */\nexport const getNewBlockTypes = createRegistrySelector( ( select ) =>\n\tcreateSelector(\n\t\t( state ) => {\n\t\t\tconst usedBlockTree = select( blockEditorStore ).getBlocks();\n\t\t\tconst installedBlockTypes = getInstalledBlockTypes( state );\n\n\t\t\treturn installedBlockTypes.filter( ( blockType ) =>\n\t\t\t\thasBlockType( blockType, usedBlockTree )\n\t\t\t);\n\t\t},\n\t\t( state ) => [\n\t\t\tgetInstalledBlockTypes( state ),\n\t\t\tselect( blockEditorStore ).getBlocks(),\n\t\t]\n\t)\n);\n\n/**\n * Returns the block types that have been installed on the server but are not\n * used in the current post.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items.\n */\nexport const getUnusedBlockTypes = createRegistrySelector( ( select ) =>\n\tcreateSelector(\n\t\t( state ) => {\n\t\t\tconst usedBlockTree = select( blockEditorStore ).getBlocks();\n\t\t\tconst installedBlockTypes = getInstalledBlockTypes( state );\n\n\t\t\treturn installedBlockTypes.filter(\n\t\t\t\t( blockType ) => ! hasBlockType( blockType, usedBlockTree )\n\t\t\t);\n\t\t},\n\t\t( state ) => [\n\t\t\tgetInstalledBlockTypes( state ),\n\t\t\tselect( blockEditorStore ).getBlocks(),\n\t\t]\n\t)\n);\n\n/**\n * Returns true if a block plugin install is in progress.\n *\n * @param {Object} state Global application state.\n * @param {string} blockId Id of the block.\n *\n * @return {boolean} Whether this block is currently being installed.\n */\nexport function isInstalling( state, blockId ) {\n\treturn state.blockManagement.isInstalling[ blockId ] || false;\n}\n\n/**\n * Returns all block error notices.\n *\n * @param {Object} state Global application state.\n *\n * @return {Object} Object with error notices.\n */\nexport function getErrorNotices( state ) {\n\treturn state.errorNotices;\n}\n\n/**\n * Returns the error notice for a given block.\n *\n * @param {Object} state Global application state.\n * @param {string} blockId The ID of the block plugin. eg: my-block\n *\n * @return {string|boolean} The error text, or false if no error.\n */\nexport function getErrorNoticeForBlock( state, blockId ) {\n\treturn state.errorNotices[ blockId ];\n}\n"],"mappings":";;;;;;;;;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,YAAA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AC,CAAC,CAExC,CACD,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMI,mBAAmB,GAAAV,OAAA,CAAAU,mBAAA,GAAG,IAAAT,4BAAsB,EAAIC,MAAM,IAClE,IAAAC,oBAAc,EACXf,KAAK,IAAM;EACZ,MAAMgB,aAAa,GAAGF,MAAM,CAAEG,kBAAiB,CAAC,CAACC,SAAS,CAAC,CAAC;EAC5D,MAAMR,mBAAmB,GAAGF,sBAAsB,CAAER,KAAM,CAAC;EAE3D,OAAOU,mBAAmB,CAACS,MAAM,CAC9BC,SAAS,IAAM,CAAE,IAAAC,qBAAY,EAAED,SAAS,EAAEJ,aAAc,CAC3D,CAAC;AACF,CAAC,EACChB,KAAK,IAAM,CACZQ,sBAAsB,CAAER,KAAM,CAAC,EAC/Bc,MAAM,CAAEG,kBAAiB,CAAC,CAACC,SAAS,CAAC,CAAC,CAExC,CACD,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASK,YAAYA,CAAEvB,KAAK,EAAEwB,OAAO,EAAG;EAC9C,OAAOxB,KAAK,CAACS,eAAe,CAACc,YAAY,CAAEC,OAAO,CAAE,IAAI,KAAK;AAC9D;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,eAAeA,CAAEzB,KAAK,EAAG;EACxC,OAAOA,KAAK,CAAC0B,YAAY;AAC1B;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,sBAAsBA,CAAE3B,KAAK,EAAEwB,OAAO,EAAG;EACxD,OAAOxB,KAAK,CAAC0B,YAAY,CAAEF,OAAO,CAAE;AACrC","ignoreList":[]}
1
+ {"version":3,"names":["_data","require","_blockEditor","EMPTY_ARRAY","isRequestingDownloadableBlocks","state","filterValue","_state$downloadableBl","downloadableBlocks","isRequesting","getDownloadableBlocks","_state$downloadableBl2","results","getInstalledBlockTypes","blockManagement","installedBlockTypes","getNewBlockTypes","exports","createRegistrySelector","select","createSelector","length","getBlockName","getClientIdsWithDescendants","blockEditorStore","installedBlockNames","map","blockType","name","foundBlockNames","flatMap","clientId","blockName","includes","newBlockTypes","filter","getUnusedBlockTypes","unusedBlockTypes","isInstalling","blockId","getErrorNotices","errorNotices","getErrorNoticeForBlock"],"sources":["@wordpress/block-directory/src/store/selectors.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createSelector, createRegistrySelector } from '@wordpress/data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Returns true if application is requesting for downloadable blocks.\n *\n * @param {Object} state Global application state.\n * @param {string} filterValue Search string.\n *\n * @return {boolean} Whether a request is in progress for the blocks list.\n */\nexport function isRequestingDownloadableBlocks( state, filterValue ) {\n\treturn state.downloadableBlocks[ filterValue ]?.isRequesting ?? false;\n}\n\n/**\n * Returns the available uninstalled blocks.\n *\n * @param {Object} state Global application state.\n * @param {string} filterValue Search string.\n *\n * @return {Array} Downloadable blocks.\n */\nexport function getDownloadableBlocks( state, filterValue ) {\n\treturn state.downloadableBlocks[ filterValue ]?.results ?? EMPTY_ARRAY;\n}\n\n/**\n * Returns the block types that have been installed on the server in this\n * session.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items\n */\nexport function getInstalledBlockTypes( state ) {\n\treturn state.blockManagement.installedBlockTypes;\n}\n\n/**\n * Returns block types that have been installed on the server and used in the\n * current post.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items.\n */\nexport const getNewBlockTypes = createRegistrySelector( ( select ) =>\n\tcreateSelector(\n\t\t( state ) => {\n\t\t\tconst installedBlockTypes = getInstalledBlockTypes( state );\n\t\t\tif ( ! installedBlockTypes.length ) {\n\t\t\t\treturn EMPTY_ARRAY;\n\t\t\t}\n\n\t\t\tconst { getBlockName, getClientIdsWithDescendants } =\n\t\t\t\tselect( blockEditorStore );\n\t\t\tconst installedBlockNames = installedBlockTypes.map(\n\t\t\t\t( blockType ) => blockType.name\n\t\t\t);\n\t\t\tconst foundBlockNames = getClientIdsWithDescendants().flatMap(\n\t\t\t\t( clientId ) => {\n\t\t\t\t\tconst blockName = getBlockName( clientId );\n\t\t\t\t\treturn installedBlockNames.includes( blockName )\n\t\t\t\t\t\t? blockName\n\t\t\t\t\t\t: [];\n\t\t\t\t}\n\t\t\t);\n\t\t\tconst newBlockTypes = installedBlockTypes.filter( ( blockType ) =>\n\t\t\t\tfoundBlockNames.includes( blockType.name )\n\t\t\t);\n\n\t\t\treturn newBlockTypes.length > 0 ? newBlockTypes : EMPTY_ARRAY;\n\t\t},\n\t\t( state ) => [\n\t\t\tgetInstalledBlockTypes( state ),\n\t\t\tselect( blockEditorStore ).getClientIdsWithDescendants(),\n\t\t]\n\t)\n);\n\n/**\n * Returns the block types that have been installed on the server but are not\n * used in the current post.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items.\n */\nexport const getUnusedBlockTypes = createRegistrySelector( ( select ) =>\n\tcreateSelector(\n\t\t( state ) => {\n\t\t\tconst installedBlockTypes = getInstalledBlockTypes( state );\n\t\t\tif ( ! installedBlockTypes.length ) {\n\t\t\t\treturn EMPTY_ARRAY;\n\t\t\t}\n\n\t\t\tconst { getBlockName, getClientIdsWithDescendants } =\n\t\t\t\tselect( blockEditorStore );\n\t\t\tconst installedBlockNames = installedBlockTypes.map(\n\t\t\t\t( blockType ) => blockType.name\n\t\t\t);\n\t\t\tconst foundBlockNames = getClientIdsWithDescendants().flatMap(\n\t\t\t\t( clientId ) => {\n\t\t\t\t\tconst blockName = getBlockName( clientId );\n\t\t\t\t\treturn installedBlockNames.includes( blockName )\n\t\t\t\t\t\t? blockName\n\t\t\t\t\t\t: [];\n\t\t\t\t}\n\t\t\t);\n\t\t\tconst unusedBlockTypes = installedBlockTypes.filter(\n\t\t\t\t( blockType ) => ! foundBlockNames.includes( blockType.name )\n\t\t\t);\n\n\t\t\treturn unusedBlockTypes.length > 0 ? unusedBlockTypes : EMPTY_ARRAY;\n\t\t},\n\t\t( state ) => [\n\t\t\tgetInstalledBlockTypes( state ),\n\t\t\tselect( blockEditorStore ).getClientIdsWithDescendants(),\n\t\t]\n\t)\n);\n\n/**\n * Returns true if a block plugin install is in progress.\n *\n * @param {Object} state Global application state.\n * @param {string} blockId Id of the block.\n *\n * @return {boolean} Whether this block is currently being installed.\n */\nexport function isInstalling( state, blockId ) {\n\treturn state.blockManagement.isInstalling[ blockId ] || false;\n}\n\n/**\n * Returns all block error notices.\n *\n * @param {Object} state Global application state.\n *\n * @return {Object} Object with error notices.\n */\nexport function getErrorNotices( state ) {\n\treturn state.errorNotices;\n}\n\n/**\n * Returns the error notice for a given block.\n *\n * @param {Object} state Global application state.\n * @param {string} blockId The ID of the block plugin. eg: my-block\n *\n * @return {string|boolean} The error text, or false if no error.\n */\nexport function getErrorNoticeForBlock( state, blockId ) {\n\treturn state.errorNotices[ blockId ];\n}\n"],"mappings":";;;;;;;;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AAJA;AACA;AACA;;AAIA,MAAME,WAAW,GAAG,EAAE;;AAEtB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,8BAA8BA,CAAEC,KAAK,EAAEC,WAAW,EAAG;EAAA,IAAAC,qBAAA;EACpE,QAAAA,qBAAA,GAAOF,KAAK,CAACG,kBAAkB,CAAEF,WAAW,CAAE,EAAEG,YAAY,cAAAF,qBAAA,cAAAA,qBAAA,GAAI,KAAK;AACtE;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASG,qBAAqBA,CAAEL,KAAK,EAAEC,WAAW,EAAG;EAAA,IAAAK,sBAAA;EAC3D,QAAAA,sBAAA,GAAON,KAAK,CAACG,kBAAkB,CAAEF,WAAW,CAAE,EAAEM,OAAO,cAAAD,sBAAA,cAAAA,sBAAA,GAAIR,WAAW;AACvE;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASU,sBAA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EA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,sBAAsBA,CAAErC,KAAK,EAAEkC,OAAO,EAAG;EACxD,OAAOlC,KAAK,CAACoC,YAAY,CAAEF,OAAO,CAAE;AACrC","ignoreList":[]}
@@ -3,11 +3,7 @@
3
3
  */
4
4
  import { createSelector, createRegistrySelector } from '@wordpress/data';
5
5
  import { store as blockEditorStore } from '@wordpress/block-editor';
6
-
7
- /**
8
- * Internal dependencies
9
- */
10
- import hasBlockType from './utils/has-block-type';
6
+ const EMPTY_ARRAY = [];
11
7
 
12
8
  /**
13
9
  * Returns true if application is requesting for downloadable blocks.
@@ -32,7 +28,7 @@ export function isRequestingDownloadableBlocks(state, filterValue) {
32
28
  */
33
29
  export function getDownloadableBlocks(state, filterValue) {
34
30
  var _state$downloadableBl2;
35
- return (_state$downloadableBl2 = state.downloadableBlocks[filterValue]?.results) !== null && _state$downloadableBl2 !== void 0 ? _state$downloadableBl2 : [];
31
+ return (_state$downloadableBl2 = state.downloadableBlocks[filterValue]?.results) !== null && _state$downloadableBl2 !== void 0 ? _state$downloadableBl2 : EMPTY_ARRAY;
36
32
  }
37
33
 
38
34
  /**
@@ -56,10 +52,22 @@ export function getInstalledBlockTypes(state) {
56
52
  * @return {Array} Block type items.
57
53
  */
58
54
  export const getNewBlockTypes = createRegistrySelector(select => createSelector(state => {
59
- const usedBlockTree = select(blockEditorStore).getBlocks();
60
55
  const installedBlockTypes = getInstalledBlockTypes(state);
61
- return installedBlockTypes.filter(blockType => hasBlockType(blockType, usedBlockTree));
62
- }, state => [getInstalledBlockTypes(state), select(blockEditorStore).getBlocks()]));
56
+ if (!installedBlockTypes.length) {
57
+ return EMPTY_ARRAY;
58
+ }
59
+ const {
60
+ getBlockName,
61
+ getClientIdsWithDescendants
62
+ } = select(blockEditorStore);
63
+ const installedBlockNames = installedBlockTypes.map(blockType => blockType.name);
64
+ const foundBlockNames = getClientIdsWithDescendants().flatMap(clientId => {
65
+ const blockName = getBlockName(clientId);
66
+ return installedBlockNames.includes(blockName) ? blockName : [];
67
+ });
68
+ const newBlockTypes = installedBlockTypes.filter(blockType => foundBlockNames.includes(blockType.name));
69
+ return newBlockTypes.length > 0 ? newBlockTypes : EMPTY_ARRAY;
70
+ }, state => [getInstalledBlockTypes(state), select(blockEditorStore).getClientIdsWithDescendants()]));
63
71
 
64
72
  /**
65
73
  * Returns the block types that have been installed on the server but are not
@@ -70,10 +78,22 @@ export const getNewBlockTypes = createRegistrySelector(select => createSelector(
70
78
  * @return {Array} Block type items.
71
79
  */
72
80
  export const getUnusedBlockTypes = createRegistrySelector(select => createSelector(state => {
73
- const usedBlockTree = select(blockEditorStore).getBlocks();
74
81
  const installedBlockTypes = getInstalledBlockTypes(state);
75
- return installedBlockTypes.filter(blockType => !hasBlockType(blockType, usedBlockTree));
76
- }, state => [getInstalledBlockTypes(state), select(blockEditorStore).getBlocks()]));
82
+ if (!installedBlockTypes.length) {
83
+ return EMPTY_ARRAY;
84
+ }
85
+ const {
86
+ getBlockName,
87
+ getClientIdsWithDescendants
88
+ } = select(blockEditorStore);
89
+ const installedBlockNames = installedBlockTypes.map(blockType => blockType.name);
90
+ const foundBlockNames = getClientIdsWithDescendants().flatMap(clientId => {
91
+ const blockName = getBlockName(clientId);
92
+ return installedBlockNames.includes(blockName) ? blockName : [];
93
+ });
94
+ const unusedBlockTypes = installedBlockTypes.filter(blockType => !foundBlockNames.includes(blockType.name));
95
+ return unusedBlockTypes.length > 0 ? unusedBlockTypes : EMPTY_ARRAY;
96
+ }, state => [getInstalledBlockTypes(state), select(blockEditorStore).getClientIdsWithDescendants()]));
77
97
 
78
98
  /**
79
99
  * Returns true if a block plugin install is in progress.
@@ -1 +1 @@
1
- {"version":3,"names":["createSelector","createRegistrySelector","store","blockEditorStore","hasBlockType","isRequestingDownloadableBlocks","state","filterValue","_state$downloadableBl","downloadableBlocks","isRequesting","getDownloadableBlocks","_state$downloadableBl2","results","getInstalledBlockTypes","blockManagement","installedBlockTypes","getNewBlockTypes","select","usedBlockTree","getBlocks","filter","blockType","getUnusedBlockTypes","isInstalling","blockId","getErrorNotices","errorNotices","getErrorNoticeForBlock"],"sources":["@wordpress/block-directory/src/store/selectors.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createSelector, createRegistrySelector } from '@wordpress/data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport hasBlockType from './utils/has-block-type';\n\n/**\n * Returns true if application is requesting for downloadable blocks.\n *\n * @param {Object} state Global application state.\n * @param {string} filterValue Search string.\n *\n * @return {boolean} Whether a request is in progress for the blocks list.\n */\nexport function isRequestingDownloadableBlocks( state, filterValue ) {\n\treturn state.downloadableBlocks[ filterValue ]?.isRequesting ?? false;\n}\n\n/**\n * Returns the available uninstalled blocks.\n *\n * @param {Object} state Global application state.\n * @param {string} filterValue Search string.\n *\n * @return {Array} Downloadable blocks.\n */\nexport function getDownloadableBlocks( state, filterValue ) {\n\treturn state.downloadableBlocks[ filterValue ]?.results ?? [];\n}\n\n/**\n * Returns the block types that have been installed on the server in this\n * session.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items\n */\nexport function getInstalledBlockTypes( state ) {\n\treturn state.blockManagement.installedBlockTypes;\n}\n\n/**\n * Returns block types that have been installed on the server and used in the\n * current post.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items.\n */\nexport const getNewBlockTypes = createRegistrySelector( ( select ) =>\n\tcreateSelector(\n\t\t( state ) => {\n\t\t\tconst usedBlockTree = select( blockEditorStore ).getBlocks();\n\t\t\tconst installedBlockTypes = getInstalledBlockTypes( state );\n\n\t\t\treturn installedBlockTypes.filter( ( blockType ) =>\n\t\t\t\thasBlockType( blockType, usedBlockTree )\n\t\t\t);\n\t\t},\n\t\t( state ) => [\n\t\t\tgetInstalledBlockTypes( state ),\n\t\t\tselect( blockEditorStore ).getBlocks(),\n\t\t]\n\t)\n);\n\n/**\n * Returns the block types that have been installed on the server but are not\n * used in the current post.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items.\n */\nexport const getUnusedBlockTypes = createRegistrySelector( ( select ) =>\n\tcreateSelector(\n\t\t( state ) => {\n\t\t\tconst usedBlockTree = select( blockEditorStore ).getBlocks();\n\t\t\tconst installedBlockTypes = getInstalledBlockTypes( state );\n\n\t\t\treturn installedBlockTypes.filter(\n\t\t\t\t( blockType ) => ! hasBlockType( blockType, usedBlockTree )\n\t\t\t);\n\t\t},\n\t\t( state ) => [\n\t\t\tgetInstalledBlockTypes( state ),\n\t\t\tselect( blockEditorStore ).getBlocks(),\n\t\t]\n\t)\n);\n\n/**\n * Returns true if a block plugin install is in progress.\n *\n * @param {Object} state Global application state.\n * @param {string} blockId Id of the block.\n *\n * @return {boolean} Whether this block is currently being installed.\n */\nexport function isInstalling( state, blockId ) {\n\treturn state.blockManagement.isInstalling[ blockId ] || false;\n}\n\n/**\n * Returns all block error notices.\n *\n * @param {Object} state Global application state.\n *\n * @return {Object} Object with error notices.\n */\nexport function getErrorNotices( state ) {\n\treturn state.errorNotices;\n}\n\n/**\n * Returns the error notice for a given block.\n *\n * @param {Object} state Global application state.\n * @param {string} blockId The ID of the block plugin. eg: my-block\n *\n * @return {string|boolean} The error text, or false if no error.\n */\nexport function getErrorNoticeForBlock( state, blockId ) {\n\treturn state.errorNotices[ blockId ];\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,cAAc,EAAEC,sBAAsB,QAAQ,iBAAiB;AACxE,SAASC,KAAK,IAAIC,gBAAgB,QAAQ,yBAAyB;;AAEnE;AACA;AACA;AACA,OAAOC,YAAY,MAAM,wBAAwB;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,8BAA8BA,CAAEC,KAAK,EAAEC,WAAW,EAAG;EAAA,IAAAC,qBAAA;EACpE,QAAAA,qBAAA,GAAOF,KAAK,CAACG,kBAAkB,CAAEF,WAAW,CAAE,EAAEG,YAAY,cAAAF,qBAAA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AM,CAC9BC,SAAS,IAAM,CAAElB,YAAY,CAAEkB,SAAS,EAAEH,aAAc,CAC3D,CAAC;AACF,CAAC,EACCb,KAAK,IAAM,CACZQ,sBAAsB,CAAER,KAAM,CAAC,EAC/BY,MAAM,CAAEf,gBAAiB,CAAC,CAACiB,SAAS,CAAC,CAAC,CAExC,CACD,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASI,YAAYA,CAAElB,KAAK,EAAEmB,OAAO,EAAG;EAC9C,OAAOnB,KAAK,CAACS,eAAe,CAACS,YAAY,CAAEC,OAAO,CAAE,IAAI,KAAK;AAC9D;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAAEpB,KAAK,EAAG;EACxC,OAAOA,KAAK,CAACqB,YAAY;AAC1B;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,sBAAsBA,CAAEtB,KAAK,EAAEmB,OAAO,EAAG;EACxD,OAAOnB,KAAK,CAACqB,YAAY,CAAEF,OAAO,CAAE;AACrC","ignoreList":[]}
1
+ {"version":3,"names":["createSelector","createRegistrySelector","store","blockEditorStore","EMPTY_ARRAY","isRequestingDownloadableBlocks","state","filterValue","_state$downloadableBl","downloadableBlocks","isRequesting","getDownloadableBlocks","_state$downloadableBl2","results","getInstalledBlockTypes","blockManagement","installedBlockTypes","getNewBlockTypes","select","length","getBlockName","getClientIdsWithDescendants","installedBlockNames","map","blockType","name","foundBlockNames","flatMap","clientId","blockName","includes","newBlockTypes","filter","getUnusedBlockTypes","unusedBlockTypes","isInstalling","blockId","getErrorNotices","errorNotices","getErrorNoticeForBlock"],"sources":["@wordpress/block-directory/src/store/selectors.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createSelector, createRegistrySelector } from '@wordpress/data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Returns true if application is requesting for downloadable blocks.\n *\n * @param {Object} state Global application state.\n * @param {string} filterValue Search string.\n *\n * @return {boolean} Whether a request is in progress for the blocks list.\n */\nexport function isRequestingDownloadableBlocks( state, filterValue ) {\n\treturn state.downloadableBlocks[ filterValue ]?.isRequesting ?? false;\n}\n\n/**\n * Returns the available uninstalled blocks.\n *\n * @param {Object} state Global application state.\n * @param {string} filterValue Search string.\n *\n * @return {Array} Downloadable blocks.\n */\nexport function getDownloadableBlocks( state, filterValue ) {\n\treturn state.downloadableBlocks[ filterValue ]?.results ?? EMPTY_ARRAY;\n}\n\n/**\n * Returns the block types that have been installed on the server in this\n * session.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items\n */\nexport function getInstalledBlockTypes( state ) {\n\treturn state.blockManagement.installedBlockTypes;\n}\n\n/**\n * Returns block types that have been installed on the server and used in the\n * current post.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items.\n */\nexport const getNewBlockTypes = createRegistrySelector( ( select ) =>\n\tcreateSelector(\n\t\t( state ) => {\n\t\t\tconst installedBlockTypes = getInstalledBlockTypes( state );\n\t\t\tif ( ! installedBlockTypes.length ) {\n\t\t\t\treturn EMPTY_ARRAY;\n\t\t\t}\n\n\t\t\tconst { getBlockName, getClientIdsWithDescendants } =\n\t\t\t\tselect( blockEditorStore );\n\t\t\tconst installedBlockNames = installedBlockTypes.map(\n\t\t\t\t( blockType ) => blockType.name\n\t\t\t);\n\t\t\tconst foundBlockNames = getClientIdsWithDescendants().flatMap(\n\t\t\t\t( clientId ) => {\n\t\t\t\t\tconst blockName = getBlockName( clientId );\n\t\t\t\t\treturn installedBlockNames.includes( blockName )\n\t\t\t\t\t\t? blockName\n\t\t\t\t\t\t: [];\n\t\t\t\t}\n\t\t\t);\n\t\t\tconst newBlockTypes = installedBlockTypes.filter( ( blockType ) =>\n\t\t\t\tfoundBlockNames.includes( blockType.name )\n\t\t\t);\n\n\t\t\treturn newBlockTypes.length > 0 ? newBlockTypes : EMPTY_ARRAY;\n\t\t},\n\t\t( state ) => [\n\t\t\tgetInstalledBlockTypes( state ),\n\t\t\tselect( blockEditorStore ).getClientIdsWithDescendants(),\n\t\t]\n\t)\n);\n\n/**\n * Returns the block types that have been installed on the server but are not\n * used in the current post.\n *\n * @param {Object} state Global application state.\n *\n * @return {Array} Block type items.\n */\nexport const getUnusedBlockTypes = createRegistrySelector( ( select ) =>\n\tcreateSelector(\n\t\t( state ) => {\n\t\t\tconst installedBlockTypes = getInstalledBlockTypes( state );\n\t\t\tif ( ! installedBlockTypes.length ) {\n\t\t\t\treturn EMPTY_ARRAY;\n\t\t\t}\n\n\t\t\tconst { getBlockName, getClientIdsWithDescendants } =\n\t\t\t\tselect( blockEditorStore );\n\t\t\tconst installedBlockNames = installedBlockTypes.map(\n\t\t\t\t( blockType ) => blockType.name\n\t\t\t);\n\t\t\tconst foundBlockNames = getClientIdsWithDescendants().flatMap(\n\t\t\t\t( clientId ) => {\n\t\t\t\t\tconst blockName = getBlockName( clientId );\n\t\t\t\t\treturn installedBlockNames.includes( blockName )\n\t\t\t\t\t\t? blockName\n\t\t\t\t\t\t: [];\n\t\t\t\t}\n\t\t\t);\n\t\t\tconst unusedBlockTypes = installedBlockTypes.filter(\n\t\t\t\t( blockType ) => ! foundBlockNames.includes( blockType.name )\n\t\t\t);\n\n\t\t\treturn unusedBlockTypes.length > 0 ? unusedBlockTypes : EMPTY_ARRAY;\n\t\t},\n\t\t( state ) => [\n\t\t\tgetInstalledBlockTypes( state ),\n\t\t\tselect( blockEditorStore ).getClientIdsWithDescendants(),\n\t\t]\n\t)\n);\n\n/**\n * Returns true if a block plugin install is in progress.\n *\n * @param {Object} state Global application state.\n * @param {string} blockId Id of the block.\n *\n * @return {boolean} Whether this block is currently being installed.\n */\nexport function isInstalling( state, blockId ) {\n\treturn state.blockManagement.isInstalling[ blockId ] || false;\n}\n\n/**\n * Returns all block error notices.\n *\n * @param {Object} state Global application state.\n *\n * @return {Object} Object with error notices.\n */\nexport function getErrorNotices( state ) {\n\treturn state.errorNotices;\n}\n\n/**\n * Returns the error notice for a given block.\n *\n * @param {Object} state Global application state.\n * @param {string} blockId The ID of the block plugin. eg: my-block\n *\n * @return {string|boolean} The error text, or false if no error.\n */\nexport function getErrorNoticeForBlock( state, blockId ) {\n\treturn state.errorNotices[ blockId ];\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,cAAc,EAAEC,sBAAsB,QAAQ,iBAAiB;AACxE,SAASC,KAAK,IAAIC,gBAAgB,QAAQ,yBAAyB;AAEnE,MAAMC,WAAW,GAAG,EAAE;;AAEtB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,8BAA8BA,CAAEC,KAAK,EAAEC,WAAW,EAAG;EAAA,IAAAC,qBAAA;EACpE,QAAAA,qBAAA,GAAOF,KAAK,CAACG,kBAAkB,CAAEF,WAAW,CAAE,EAAEG,YAAY,cAAAF,qBAAA,cAAAA,qBAAA,GAAI,KAAK;AACtE;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASG,qBAAqBA,CAAEL,KAAK,EAAEC,WAAW,EAAG;EAAA,IAAAK,sBAAA;EAC3D,QAAAA,sBAAA,GAAON,KAAK,CAACG,kBAAkB,CAAEF,WAAW,CAAE,EAAEM,OAAO,cAAAD,sBAAA,cAAAA,sBAAA,GAAIR,WAAW;AACvE;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASU,sBAAsBA,CAAER,KAAK,EAAG;EAC/C,OAAOA,KAAK,CAACS,eAAe,CAACC,mBAAmB;AACjD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,gBAAgB,GAAGhB,sBAAsB,CAAIiB,MAAM,IAC/DlB,cAAc,CACXM,KAAK,IAAM;EACZ,MAAMU,mBAAmB,GAAGF,sBAAsB,CAAER,KAAM,CAAC;EAC3D,IAAK,CAAEU,mBAAmB,CAACG,MAAM,EAAG;IACnC,OAAOf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AAC,CAAC,CAACM,OAAO,CAC1DC,QAAQ,IAAM;IACf,MAAMC,SAAS,GAAGT,YAAY,CAAEQ,QAAS,CAAC;IAC1C,OAAON,mBAAmB,CAACQ,QAAQ,CAAED,SAAU,CAAC,GAC7CA,SAAS,GACT,EAAE;EACN,CACD,CAAC;EACD,MAAMK,gBAAgB,GAAGlB,mBAAmB,CAACgB,MAAM,CAChDR,SAAS,IAAM,CAAEE,eAAe,CAACI,QAAQ,CAAEN,SAAS,CAACC,IAAK,CAC7D,CAAC;EAED,OAAOS,gBAAgB,CAACf,MAAM,GAAG,CAAC,GAAGe,gBAAgB,GAAG9B,WAAW;AACpE,CAAC,EACCE,KAAK,IAAM,CACZQ,sBAAsB,CAAER,KAAM,CAAC,EAC/BY,MAAM,CAAEf,gBAAiB,CAAC,CAACkB,2BAA2B,CAAC,CAAC,CAE1D,CACD,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASc,YAAYA,CAAE7B,KAAK,EAAE8B,OAAO,EAAG;EAC9C,OAAO9B,KAAK,CAACS,eAAe,CAACoB,YAAY,CAAEC,OAAO,CAAE,IAAI,KAAK;AAC9D;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAAE/B,KAAK,EAAG;EACxC,OAAOA,KAAK,CAACgC,YAAY;AAC1B;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,sBAAsBA,CAAEjC,KAAK,EAAE8B,OAAO,EAAG;EACxD,OAAO9B,KAAK,CAACgC,YAAY,CAAEF,OAAO,CAAE;AACrC","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/block-directory",
3
- "version": "5.19.0",
3
+ "version": "5.19.1",
4
4
  "description": "Extend editor with block directory features to search, download and install blocks.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -28,24 +28,24 @@
28
28
  "wpScript": true,
29
29
  "dependencies": {
30
30
  "@babel/runtime": "7.25.7",
31
- "@wordpress/a11y": "^4.19.0",
32
- "@wordpress/api-fetch": "^7.19.0",
33
- "@wordpress/block-editor": "^14.14.0",
34
- "@wordpress/blocks": "^14.8.0",
35
- "@wordpress/components": "^29.5.0",
36
- "@wordpress/compose": "^7.19.0",
37
- "@wordpress/core-data": "^7.19.0",
38
- "@wordpress/data": "^10.19.0",
39
- "@wordpress/editor": "^14.19.0",
40
- "@wordpress/element": "^6.19.0",
41
- "@wordpress/hooks": "^4.19.0",
42
- "@wordpress/html-entities": "^4.19.0",
43
- "@wordpress/i18n": "^5.19.0",
44
- "@wordpress/icons": "^10.19.0",
45
- "@wordpress/notices": "^5.19.0",
46
- "@wordpress/plugins": "^7.19.0",
47
- "@wordpress/private-apis": "^1.19.0",
48
- "@wordpress/url": "^4.19.0",
31
+ "@wordpress/a11y": "^4.19.1",
32
+ "@wordpress/api-fetch": "^7.19.1",
33
+ "@wordpress/block-editor": "^14.14.1",
34
+ "@wordpress/blocks": "^14.8.1",
35
+ "@wordpress/components": "^29.5.1",
36
+ "@wordpress/compose": "^7.19.1",
37
+ "@wordpress/core-data": "^7.19.1",
38
+ "@wordpress/data": "^10.19.1",
39
+ "@wordpress/editor": "^14.19.1",
40
+ "@wordpress/element": "^6.19.1",
41
+ "@wordpress/hooks": "^4.19.1",
42
+ "@wordpress/html-entities": "^4.19.1",
43
+ "@wordpress/i18n": "^5.19.1",
44
+ "@wordpress/icons": "^10.19.1",
45
+ "@wordpress/notices": "^5.19.1",
46
+ "@wordpress/plugins": "^7.19.1",
47
+ "@wordpress/private-apis": "^1.19.1",
48
+ "@wordpress/url": "^4.19.1",
49
49
  "change-case": "^4.1.2",
50
50
  "clsx": "^2.1.1"
51
51
  },
@@ -56,5 +56,5 @@
56
56
  "publishConfig": {
57
57
  "access": "public"
58
58
  },
59
- "gitHead": "d6b0b20fa927b110140dc7fdd906a7e0bf662004"
59
+ "gitHead": "6f49fee89f840761f7fedf662713cbd4a71723e9"
60
60
  }
@@ -4,10 +4,7 @@
4
4
  import { createSelector, createRegistrySelector } from '@wordpress/data';
5
5
  import { store as blockEditorStore } from '@wordpress/block-editor';
6
6
 
7
- /**
8
- * Internal dependencies
9
- */
10
- import hasBlockType from './utils/has-block-type';
7
+ const EMPTY_ARRAY = [];
11
8
 
12
9
  /**
13
10
  * Returns true if application is requesting for downloadable blocks.
@@ -30,7 +27,7 @@ export function isRequestingDownloadableBlocks( state, filterValue ) {
30
27
  * @return {Array} Downloadable blocks.
31
28
  */
32
29
  export function getDownloadableBlocks( state, filterValue ) {
33
- return state.downloadableBlocks[ filterValue ]?.results ?? [];
30
+ return state.downloadableBlocks[ filterValue ]?.results ?? EMPTY_ARRAY;
34
31
  }
35
32
 
36
33
  /**
@@ -56,16 +53,33 @@ export function getInstalledBlockTypes( state ) {
56
53
  export const getNewBlockTypes = createRegistrySelector( ( select ) =>
57
54
  createSelector(
58
55
  ( state ) => {
59
- const usedBlockTree = select( blockEditorStore ).getBlocks();
60
56
  const installedBlockTypes = getInstalledBlockTypes( state );
57
+ if ( ! installedBlockTypes.length ) {
58
+ return EMPTY_ARRAY;
59
+ }
61
60
 
62
- return installedBlockTypes.filter( ( blockType ) =>
63
- hasBlockType( blockType, usedBlockTree )
61
+ const { getBlockName, getClientIdsWithDescendants } =
62
+ select( blockEditorStore );
63
+ const installedBlockNames = installedBlockTypes.map(
64
+ ( blockType ) => blockType.name
65
+ );
66
+ const foundBlockNames = getClientIdsWithDescendants().flatMap(
67
+ ( clientId ) => {
68
+ const blockName = getBlockName( clientId );
69
+ return installedBlockNames.includes( blockName )
70
+ ? blockName
71
+ : [];
72
+ }
73
+ );
74
+ const newBlockTypes = installedBlockTypes.filter( ( blockType ) =>
75
+ foundBlockNames.includes( blockType.name )
64
76
  );
77
+
78
+ return newBlockTypes.length > 0 ? newBlockTypes : EMPTY_ARRAY;
65
79
  },
66
80
  ( state ) => [
67
81
  getInstalledBlockTypes( state ),
68
- select( blockEditorStore ).getBlocks(),
82
+ select( blockEditorStore ).getClientIdsWithDescendants(),
69
83
  ]
70
84
  )
71
85
  );
@@ -81,16 +95,33 @@ export const getNewBlockTypes = createRegistrySelector( ( select ) =>
81
95
  export const getUnusedBlockTypes = createRegistrySelector( ( select ) =>
82
96
  createSelector(
83
97
  ( state ) => {
84
- const usedBlockTree = select( blockEditorStore ).getBlocks();
85
98
  const installedBlockTypes = getInstalledBlockTypes( state );
99
+ if ( ! installedBlockTypes.length ) {
100
+ return EMPTY_ARRAY;
101
+ }
86
102
 
87
- return installedBlockTypes.filter(
88
- ( blockType ) => ! hasBlockType( blockType, usedBlockTree )
103
+ const { getBlockName, getClientIdsWithDescendants } =
104
+ select( blockEditorStore );
105
+ const installedBlockNames = installedBlockTypes.map(
106
+ ( blockType ) => blockType.name
107
+ );
108
+ const foundBlockNames = getClientIdsWithDescendants().flatMap(
109
+ ( clientId ) => {
110
+ const blockName = getBlockName( clientId );
111
+ return installedBlockNames.includes( blockName )
112
+ ? blockName
113
+ : [];
114
+ }
89
115
  );
116
+ const unusedBlockTypes = installedBlockTypes.filter(
117
+ ( blockType ) => ! foundBlockNames.includes( blockType.name )
118
+ );
119
+
120
+ return unusedBlockTypes.length > 0 ? unusedBlockTypes : EMPTY_ARRAY;
90
121
  },
91
122
  ( state ) => [
92
123
  getInstalledBlockTypes( state ),
93
- select( blockEditorStore ).getBlocks(),
124
+ select( blockEditorStore ).getClientIdsWithDescendants(),
94
125
  ]
95
126
  )
96
127
  );
@@ -48,3 +48,9 @@ export const blockList = [
48
48
  innerBlocks: [],
49
49
  },
50
50
  ];
51
+
52
+ export const blockListIds = blockList.map( ( block ) => block.clientId );
53
+ export const blockListNameMap = blockList.reduce( ( acc, block ) => {
54
+ acc[ block.clientId ] = block.name;
55
+ return acc;
56
+ }, {} );
@@ -2,7 +2,8 @@
2
2
  * Internal dependencies
3
3
  */
4
4
  import {
5
- blockList,
5
+ blockListIds,
6
+ blockListNameMap,
6
7
  blockTypeInstalled,
7
8
  blockTypeUnused,
8
9
  downloadableBlock,
@@ -90,7 +91,10 @@ describe( 'selectors', () => {
90
91
  describe( 'getNewBlockTypes', () => {
91
92
  it( 'should retrieve the block types that are installed and in the post content', () => {
92
93
  getNewBlockTypes.registry = {
93
- select: jest.fn( () => ( { getBlocks: () => blockList } ) ),
94
+ select: jest.fn( () => ( {
95
+ getBlockName: ( clientId ) => blockListNameMap[ clientId ],
96
+ getClientIdsWithDescendants: () => blockListIds,
97
+ } ) ),
94
98
  };
95
99
  const state = {
96
100
  blockManagement: {
@@ -107,7 +111,10 @@ describe( 'selectors', () => {
107
111
 
108
112
  it( 'should return an empty array if no blocks are used', () => {
109
113
  getNewBlockTypes.registry = {
110
- select: jest.fn( () => ( { getBlocks: () => [] } ) ),
114
+ select: jest.fn( () => ( {
115
+ getBlockName: ( clientId ) => blockListNameMap[ clientId ],
116
+ getClientIdsWithDescendants: () => [],
117
+ } ) ),
111
118
  };
112
119
  const state = {
113
120
  blockManagement: {
@@ -125,7 +132,10 @@ describe( 'selectors', () => {
125
132
  describe( 'getUnusedBlockTypes', () => {
126
133
  it( 'should retrieve the block types that are installed but not used', () => {
127
134
  getUnusedBlockTypes.registry = {
128
- select: jest.fn( () => ( { getBlocks: () => blockList } ) ),
135
+ select: jest.fn( () => ( {
136
+ getBlockName: ( clientId ) => blockListNameMap[ clientId ],
137
+ getClientIdsWithDescendants: () => blockListIds,
138
+ } ) ),
129
139
  };
130
140
  const state = {
131
141
  blockManagement: {
@@ -142,7 +152,10 @@ describe( 'selectors', () => {
142
152
 
143
153
  it( 'should return all block types if no blocks are used', () => {
144
154
  getUnusedBlockTypes.registry = {
145
- select: jest.fn( () => ( { getBlocks: () => [] } ) ),
155
+ select: jest.fn( () => ( {
156
+ getBlockName: ( clientId ) => blockListNameMap[ clientId ],
157
+ getClientIdsWithDescendants: () => [],
158
+ } ) ),
146
159
  };
147
160
  const state = {
148
161
  blockManagement: {
@@ -1,32 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = hasBlockType;
7
- /**
8
- * Check if a block list contains a specific block type. Recursively searches
9
- * through `innerBlocks` if they exist.
10
- *
11
- * @param {Object} blockType A block object to search for.
12
- * @param {Object[]} blocks The list of blocks to look through.
13
- *
14
- * @return {boolean} Whether the blockType is found.
15
- */
16
- function hasBlockType(blockType, blocks = []) {
17
- if (!blocks.length) {
18
- return false;
19
- }
20
- if (blocks.some(({
21
- name
22
- }) => name === blockType.name)) {
23
- return true;
24
- }
25
- for (let i = 0; i < blocks.length; i++) {
26
- if (hasBlockType(blockType, blocks[i].innerBlocks)) {
27
- return true;
28
- }
29
- }
30
- return false;
31
- }
32
- //# sourceMappingURL=has-block-type.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["hasBlockType","blockType","blocks","length","some","name","i","innerBlocks"],"sources":["@wordpress/block-directory/src/store/utils/has-block-type.js"],"sourcesContent":["/**\n * Check if a block list contains a specific block type. Recursively searches\n * through `innerBlocks` if they exist.\n *\n * @param {Object} blockType A block object to search for.\n * @param {Object[]} blocks The list of blocks to look through.\n *\n * @return {boolean} Whether the blockType is found.\n */\nexport default function hasBlockType( blockType, blocks = [] ) {\n\tif ( ! blocks.length ) {\n\t\treturn false;\n\t}\n\tif ( blocks.some( ( { name } ) => name === blockType.name ) ) {\n\t\treturn true;\n\t}\n\tfor ( let i = 0; i < blocks.length; i++ ) {\n\t\tif ( hasBlockType( blockType, blocks[ i ].innerBlocks ) ) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,YAAYA,CAAEC,SAAS,EAAEC,MAAM,GAAG,EAAE,EAAG;EAC9D,IAAK,CAAEA,MAAM,CAACC,MAAM,EAAG;IACtB,OAAO,KAAK;EACb;EACA,IAAKD,MAAM,CAACE,IAAI,CAAE,CAAE;IAAEC;EAAK,CAAC,KAAMA,IAAI,KAAKJ,SAAS,CAACI,IAAK,CAAC,EAAG;IAC7D,OAAO,IAAI;EACZ;EACA,KAAM,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGJ,MAAM,CAACC,MAAM,EAAEG,CAAC,EAAE,EAAG;IACzC,IAAKN,YAAY,CAAEC,SAAS,EAAEC,MAAM,CAAEI,CAAC,CAAE,CAACC,WAAY,CAAC,EAAG;MACzD,OAAO,IAAI;IACZ;EACD;EAEA,OAAO,KAAK;AACb","ignoreList":[]}
@@ -1,26 +0,0 @@
1
- /**
2
- * Check if a block list contains a specific block type. Recursively searches
3
- * through `innerBlocks` if they exist.
4
- *
5
- * @param {Object} blockType A block object to search for.
6
- * @param {Object[]} blocks The list of blocks to look through.
7
- *
8
- * @return {boolean} Whether the blockType is found.
9
- */
10
- export default function hasBlockType(blockType, blocks = []) {
11
- if (!blocks.length) {
12
- return false;
13
- }
14
- if (blocks.some(({
15
- name
16
- }) => name === blockType.name)) {
17
- return true;
18
- }
19
- for (let i = 0; i < blocks.length; i++) {
20
- if (hasBlockType(blockType, blocks[i].innerBlocks)) {
21
- return true;
22
- }
23
- }
24
- return false;
25
- }
26
- //# sourceMappingURL=has-block-type.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["hasBlockType","blockType","blocks","length","some","name","i","innerBlocks"],"sources":["@wordpress/block-directory/src/store/utils/has-block-type.js"],"sourcesContent":["/**\n * Check if a block list contains a specific block type. Recursively searches\n * through `innerBlocks` if they exist.\n *\n * @param {Object} blockType A block object to search for.\n * @param {Object[]} blocks The list of blocks to look through.\n *\n * @return {boolean} Whether the blockType is found.\n */\nexport default function hasBlockType( blockType, blocks = [] ) {\n\tif ( ! blocks.length ) {\n\t\treturn false;\n\t}\n\tif ( blocks.some( ( { name } ) => name === blockType.name ) ) {\n\t\treturn true;\n\t}\n\tfor ( let i = 0; i < blocks.length; i++ ) {\n\t\tif ( hasBlockType( blockType, blocks[ i ].innerBlocks ) ) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASA,YAAYA,CAAEC,SAAS,EAAEC,MAAM,GAAG,EAAE,EAAG;EAC9D,IAAK,CAAEA,MAAM,CAACC,MAAM,EAAG;IACtB,OAAO,KAAK;EACb;EACA,IAAKD,MAAM,CAACE,IAAI,CAAE,CAAE;IAAEC;EAAK,CAAC,KAAMA,IAAI,KAAKJ,SAAS,CAACI,IAAK,CAAC,EAAG;IAC7D,OAAO,IAAI;EACZ;EACA,KAAM,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGJ,MAAM,CAACC,MAAM,EAAEG,CAAC,EAAE,EAAG;IACzC,IAAKN,YAAY,CAAEC,SAAS,EAAEC,MAAM,CAAEI,CAAC,CAAE,CAACC,WAAY,CAAC,EAAG;MACzD,OAAO,IAAI;IACZ;EACD;EAEA,OAAO,KAAK;AACb","ignoreList":[]}
@@ -1,24 +0,0 @@
1
- /**
2
- * Check if a block list contains a specific block type. Recursively searches
3
- * through `innerBlocks` if they exist.
4
- *
5
- * @param {Object} blockType A block object to search for.
6
- * @param {Object[]} blocks The list of blocks to look through.
7
- *
8
- * @return {boolean} Whether the blockType is found.
9
- */
10
- export default function hasBlockType( blockType, blocks = [] ) {
11
- if ( ! blocks.length ) {
12
- return false;
13
- }
14
- if ( blocks.some( ( { name } ) => name === blockType.name ) ) {
15
- return true;
16
- }
17
- for ( let i = 0; i < blocks.length; i++ ) {
18
- if ( hasBlockType( blockType, blocks[ i ].innerBlocks ) ) {
19
- return true;
20
- }
21
- }
22
-
23
- return false;
24
- }
@@ -1,42 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import {
5
- blockList,
6
- blockTypeInstalled,
7
- blockTypeUnused,
8
- } from '../../test/fixtures';
9
- import hasBlockType from '../has-block-type';
10
-
11
- describe( 'hasBlockType', () => {
12
- it( 'should find the block', () => {
13
- const found = hasBlockType( blockTypeInstalled, blockList );
14
- expect( found ).toBe( true );
15
- } );
16
-
17
- it( 'should not find the unused block', () => {
18
- const found = hasBlockType( blockTypeUnused, blockList );
19
- expect( found ).toBe( false );
20
- } );
21
-
22
- it( 'should find the block in innerBlocks', () => {
23
- const innerBlockList = [
24
- ...blockList,
25
- {
26
- clientId: 4,
27
- name: 'core/cover',
28
- attributes: {},
29
- innerBlocks: [
30
- {
31
- clientId: 5,
32
- name: blockTypeUnused.name,
33
- attributes: {},
34
- innerBlocks: [],
35
- },
36
- ],
37
- },
38
- ];
39
- const found = hasBlockType( blockTypeUnused, innerBlockList );
40
- expect( found ).toBe( true );
41
- } );
42
- } );