@wordpress/block-directory 3.1.1 → 3.1.3

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.
@@ -15,6 +15,8 @@ exports.setErrorNotice = setErrorNotice;
15
15
  exports.setIsInstalling = setIsInstalling;
16
16
  exports.uninstallBlockType = void 0;
17
17
 
18
+ var _lodash = require("lodash");
19
+
18
20
  var _blocks = require("@wordpress/blocks");
19
21
 
20
22
  var _i18n = require("@wordpress/i18n");
@@ -23,10 +25,16 @@ var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
23
25
 
24
26
  var _notices = require("@wordpress/notices");
25
27
 
28
+ var _url = require("@wordpress/url");
29
+
26
30
  var _loadAssets = require("./load-assets");
27
31
 
28
32
  var _getPluginUrl = _interopRequireDefault(require("./utils/get-plugin-url"));
29
33
 
34
+ /**
35
+ * External dependencies
36
+ */
37
+
30
38
  /**
31
39
  * WordPress dependencies
32
40
  */
@@ -82,7 +90,8 @@ const installBlockType = block => async _ref => {
82
90
  dispatch
83
91
  } = _ref;
84
92
  const {
85
- id
93
+ id,
94
+ name
86
95
  } = block;
87
96
  let success = false;
88
97
  dispatch.clearErrorNotice(id);
@@ -118,11 +127,27 @@ const installBlockType = block => async _ref => {
118
127
  links: { ...block.links,
119
128
  ...links
120
129
  }
130
+ }); // Ensures that the block metadata is propagated to the editor when registered on the server.
131
+
132
+ const metadataFields = ['api_version', 'title', 'category', 'parent', 'icon', 'description', 'keywords', 'attributes', 'provides_context', 'uses_context', 'supports', 'styles', 'example', 'variations'];
133
+ await (0, _apiFetch.default)({
134
+ path: (0, _url.addQueryArgs)(`/wp/v2/block-types/${name}`, {
135
+ _fields: metadataFields
136
+ })
137
+ }) // Ignore when the block is not registered on the server.
138
+ .catch(() => {}).then(response => {
139
+ if (!response) {
140
+ return;
141
+ }
142
+
143
+ (0, _blocks.unstable__bootstrapServerSideBlockDefinitions)({
144
+ [name]: (0, _lodash.pick)(response, metadataFields)
145
+ });
121
146
  });
122
147
  await (0, _loadAssets.loadAssets)();
123
148
  const registeredBlocks = registry.select(_blocks.store).getBlockTypes();
124
149
 
125
- if (!registeredBlocks.some(i => i.name === block.name)) {
150
+ if (!registeredBlocks.some(i => i.name === name)) {
126
151
  throw new Error((0, _i18n.__)('Error registering block. Try reloading the page.'));
127
152
  }
128
153
 
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/block-directory/src/store/actions.js"],"names":["fetchDownloadableBlocks","filterValue","type","receiveDownloadableBlocks","downloadableBlocks","installBlockType","block","registry","dispatch","id","success","clearErrorNotice","setIsInstalling","url","links","method","data","status","response","path","slug","_links","addInstalledBlockType","registeredBlocks","select","blocksStore","getBlockTypes","some","i","name","Error","noticesStore","createInfoNotice","title","speak","error","message","isFatal","fatalAPIErrors","folder_exists","unable_to_connect_to_filesystem","code","setErrorNotice","createErrorNotice","isDismissible","uninstallBlockType","removeInstalledBlockType","item","blockId","isInstalling"],"mappings":";;;;;;;;;;;;;;;;;AAGA;;AACA;;AACA;;AACA;;AAKA;;AACA;;AAZA;AACA;AACA;;AAMA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,uBAAT,CAAkCC,WAAlC,EAAgD;AACtD,SAAO;AAAEC,IAAAA,IAAI,EAAE,2BAAR;AAAqCD,IAAAA;AAArC,GAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASE,yBAAT,CAAoCC,kBAApC,EAAwDH,WAAxD,EAAsE;AAC5E,SAAO;AACNC,IAAAA,IAAI,EAAE,6BADA;AAENE,IAAAA,kBAFM;AAGNH,IAAAA;AAHM,GAAP;AAKA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,MAAMI,gBAAgB,GAAKC,KAAF,IAAa,cAGtC;AAAA,MAH8C;AACpDC,IAAAA,QADoD;AAEpDC,IAAAA;AAFoD,GAG9C;AACN,QAAM;AAAEC,IAAAA;AAAF,MAASH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ACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAAS1B,gBAAT,CAA2BqC,OAA3B,EAAqC;AAC3C,SAAO;AACN9C,IAAAA,IAAI,EAAE,oBADA;AAEN8C,IAAAA;AAFM,GAAP;AAIA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { store as blocksStore } from '@wordpress/blocks';\nimport { __, sprintf } from '@wordpress/i18n';\nimport apiFetch from '@wordpress/api-fetch';\nimport { store as noticesStore } from '@wordpress/notices';\n\n/**\n * Internal dependencies\n */\nimport { loadAssets } from './load-assets';\nimport getPluginUrl from './utils/get-plugin-url';\n\n/**\n * Returns an action object used in signalling that the downloadable blocks\n * have been requested and are loading.\n *\n * @param {string} filterValue Search string.\n *\n * @return {Object} Action object.\n */\nexport function fetchDownloadableBlocks( filterValue ) {\n\treturn { type: 'FETCH_DOWNLOADABLE_BLOCKS', filterValue };\n}\n\n/**\n * Returns an action object used in signalling that the downloadable blocks\n * have been updated.\n *\n * @param {Array} downloadableBlocks Downloadable blocks.\n * @param {string} filterValue Search string.\n *\n * @return {Object} Action object.\n */\nexport function receiveDownloadableBlocks( downloadableBlocks, filterValue ) {\n\treturn {\n\t\ttype: 'RECEIVE_DOWNLOADABLE_BLOCKS',\n\t\tdownloadableBlocks,\n\t\tfilterValue,\n\t};\n}\n\n/**\n * Action triggered to install a block plugin.\n *\n * @param {Object} block The block item returned by search.\n *\n * @return {boolean} Whether the block was successfully installed & loaded.\n */\nexport const installBlockType = ( block ) => async ( {\n\tregistry,\n\tdispatch,\n} ) => {\n\tconst { id } = block;\n\tlet success = false;\n\tdispatch.clearErrorNotice( id );\n\ttry {\n\t\tdispatch.setIsInstalling( id, true );\n\n\t\t// If we have a wp:plugin link, the plugin is installed but inactive.\n\t\tconst url = getPluginUrl( block );\n\t\tlet links = {};\n\t\tif ( url ) {\n\t\t\tawait apiFetch( {\n\t\t\t\tmethod: 'PUT',\n\t\t\t\turl,\n\t\t\t\tdata: { status: 'active' },\n\t\t\t} );\n\t\t} else {\n\t\t\tconst response = await apiFetch( {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tpath: 'wp/v2/plugins',\n\t\t\t\tdata: { slug: id, status: 'active' },\n\t\t\t} );\n\t\t\t// Add the `self` link for newly-installed blocks.\n\t\t\tlinks = response._links;\n\t\t}\n\n\t\tdispatch.addInstalledBlockType( {\n\t\t\t...block,\n\t\t\tlinks: { ...block.links, ...links },\n\t\t} );\n\n\t\tawait loadAssets();\n\t\tconst registeredBlocks = registry.select( blocksStore ).getBlockTypes();\n\t\tif ( ! registeredBlocks.some( ( i ) => i.name === block.name ) ) {\n\t\t\tthrow new Error(\n\t\t\t\t__( 'Error registering block. Try reloading the page.' )\n\t\t\t);\n\t\t}\n\n\t\tregistry.dispatch( noticesStore ).createInfoNotice(\n\t\t\tsprintf(\n\t\t\t\t// translators: %s is the block title.\n\t\t\t\t__( 'Block %s installed and added.' ),\n\t\t\t\tblock.title\n\t\t\t),\n\t\t\t{\n\t\t\t\tspeak: true,\n\t\t\t\ttype: 'snackbar',\n\t\t\t}\n\t\t);\n\t\tsuccess = true;\n\t} catch ( error ) {\n\t\tlet message = error.message || __( 'An error occurred.' );\n\n\t\t// Errors we throw are fatal\n\t\tlet isFatal = error instanceof Error;\n\n\t\t// Specific API errors that are fatal\n\t\tconst fatalAPIErrors = {\n\t\t\tfolder_exists: __(\n\t\t\t\t'This block is already installed. Try reloading the page.'\n\t\t\t),\n\t\t\tunable_to_connect_to_filesystem: __(\n\t\t\t\t'Error installing block. You can reload the page and try again.'\n\t\t\t),\n\t\t};\n\n\t\tif ( fatalAPIErrors[ error.code ] ) {\n\t\t\tisFatal = true;\n\t\t\tmessage = fatalAPIErrors[ error.code ];\n\t\t}\n\n\t\tdispatch.setErrorNotice( id, message, isFatal );\n\t\tregistry.dispatch( noticesStore ).createErrorNotice( message, {\n\t\t\tspeak: true,\n\t\t\tisDismissible: true,\n\t\t} );\n\t}\n\tdispatch.setIsInstalling( id, false );\n\treturn success;\n};\n\n/**\n * Action triggered to uninstall a block plugin.\n *\n * @param {Object} block The blockType object.\n */\nexport const uninstallBlockType = ( block ) => async ( {\n\tregistry,\n\tdispatch,\n} ) => {\n\ttry {\n\t\tconst url = getPluginUrl( block );\n\t\tawait apiFetch( {\n\t\t\tmethod: 'PUT',\n\t\t\turl,\n\t\t\tdata: { status: 'inactive' },\n\t\t} );\n\t\tawait apiFetch( {\n\t\t\tmethod: 'DELETE',\n\t\t\turl,\n\t\t} );\n\t\tdispatch.removeInstalledBlockType( block );\n\t} catch ( error ) {\n\t\tregistry\n\t\t\t.dispatch( noticesStore )\n\t\t\t.createErrorNotice( error.message || __( 'An error occurred.' ) );\n\t}\n};\n\n/**\n * Returns an action object used to add a block type to the \"newly installed\"\n * tracking list.\n *\n * @param {Object} item The block item with the block id and name.\n *\n * @return {Object} Action object.\n */\nexport function addInstalledBlockType( item ) {\n\treturn {\n\t\ttype: 'ADD_INSTALLED_BLOCK_TYPE',\n\t\titem,\n\t};\n}\n\n/**\n * Returns an action object used to remove a block type from the \"newly installed\"\n * tracking list.\n *\n * @param {string} item The block item with the block id and name.\n *\n * @return {Object} Action object.\n */\nexport function removeInstalledBlockType( item ) {\n\treturn {\n\t\ttype: 'REMOVE_INSTALLED_BLOCK_TYPE',\n\t\titem,\n\t};\n}\n\n/**\n * Returns an action object used to indicate install in progress.\n *\n * @param {string} blockId\n * @param {boolean} isInstalling\n *\n * @return {Object} Action object.\n */\nexport function setIsInstalling( blockId, isInstalling ) {\n\treturn {\n\t\ttype: 'SET_INSTALLING_BLOCK',\n\t\tblockId,\n\t\tisInstalling,\n\t};\n}\n\n/**\n * Sets an error notice to be displayed to the user for a given block.\n *\n * @param {string} blockId The ID of the block plugin. eg: my-block\n * @param {string} message The message shown in the notice.\n * @param {boolean} isFatal Whether the user can recover from the error.\n *\n * @return {Object} Action object.\n */\nexport function setErrorNotice( blockId, message, isFatal = false ) {\n\treturn {\n\t\ttype: 'SET_ERROR_NOTICE',\n\t\tblockId,\n\t\tmessage,\n\t\tisFatal,\n\t};\n}\n\n/**\n * Sets the error notice to empty for specific block.\n *\n * @param {string} blockId The ID of the block plugin. eg: my-block\n *\n * @return {Object} Action object.\n */\nexport function clearErrorNotice( blockId ) {\n\treturn {\n\t\ttype: 'CLEAR_ERROR_NOTICE',\n\t\tblockId,\n\t};\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/block-directory/src/store/actions.js"],"names":["fetchDownloadableBlocks","filterValue","type","receiveDownloadableBlocks","downloadableBlocks","installBlockType","block","registry","dispatch","id","name","success","clearErrorNotice","setIsInstalling","url","links","method","data","status","response","path","slug","_links","addInstalledBlockType","metadataFields","_fields","catch","then","registeredBlocks","select","blocksStore","getBlockTypes","some","i","Error","noticesStore","createInfoNotice","title","speak","error","message","isFatal","fatalAPIErrors","folder_exists","unable_to_connect_to_filesystem","code","setErrorNotice","createErrorNotice","isDismissible","uninstallBlockType","removeInstalledBlockType","item","blockId","isInstalling"],"mappings":";;;;;;;;;;;;;;;;;AAGA;;AAKA;;AAIA;;AACA;;AACA;;AACA;;AAKA;;AACA;;AArBA;AACA;AACA;;AAGA;AACA;AACA;;AAUA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,uBAAT,CAAkCC,WAAlC,EAAgD;AACtD,SAAO;AAAEC,IAAAA,IAAI,EAAE,2BAAR;AAAqCD,IAAAA;AAArC,GAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASE,yBAAT,CAAoCC,kBAApC,EAAwDH,WAAxD,EAAsE;AAC5E,SAAO;AACNC,IAAAA,IAAI,EAAE,6BADA;AAENE,IAAAA,kBAFM;AAGNH,IAAAA;AAHM,GAAP;AAKA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,MAAMI,gBAAgB,GAAKC,KAAF,IAAa,cAGtC;AAAA,MAH8C;AACpDC,IAAAA,QADoD;AAEpDC,IAAAA;AAFoD,GAG9C;AACN,QAAM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,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASD,wBAAT,CAAmCC,IAAnC,EAA0C;AAChD,SAAO;AACNjD,IAAAA,IAAI,EAAE,6BADA;AAENiD,IAAAA;AAFM,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAAStC,eAAT,CAA0BuC,OAA1B,EAAmCC,YAAnC,EAAkD;AACxD,SAAO;AACNnD,IAAAA,IAAI,EAAE,sBADA;AAENkD,IAAAA,OAFM;AAGNC,IAAAA;AAHM,GAAP;AAKA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASP,cAAT,CAAyBM,OAAzB,EAAkCZ,OAAlC,EAA6D;AAAA,MAAlBC,OAAkB,uEAAR,KAAQ;AACnE,SAAO;AACNvC,IAAAA,IAAI,EAAE,kBADA;AAENkD,IAAAA,OAFM;AAGNZ,IAAAA,OAHM;AAINC,IAAAA;AAJM,GAAP;AAMA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAAS7B,gBAAT,CAA2BwC,OAA3B,EAAqC;AAC3C,SAAO;AACNlD,IAAAA,IAAI,EAAE,oBADA;AAENkD,IAAAA;AAFM,GAAP;AAIA","sourcesContent":["/**\n * External dependencies\n */\nimport { pick } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tstore as blocksStore,\n\tunstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase\n} from '@wordpress/blocks';\nimport { __, sprintf } from '@wordpress/i18n';\nimport apiFetch from '@wordpress/api-fetch';\nimport { store as noticesStore } from '@wordpress/notices';\nimport { addQueryArgs } from '@wordpress/url';\n\n/**\n * Internal dependencies\n */\nimport { loadAssets } from './load-assets';\nimport getPluginUrl from './utils/get-plugin-url';\n\n/**\n * Returns an action object used in signalling that the downloadable blocks\n * have been requested and are loading.\n *\n * @param {string} filterValue Search string.\n *\n * @return {Object} Action object.\n */\nexport function fetchDownloadableBlocks( filterValue ) {\n\treturn { type: 'FETCH_DOWNLOADABLE_BLOCKS', filterValue };\n}\n\n/**\n * Returns an action object used in signalling that the downloadable blocks\n * have been updated.\n *\n * @param {Array} downloadableBlocks Downloadable blocks.\n * @param {string} filterValue Search string.\n *\n * @return {Object} Action object.\n */\nexport function receiveDownloadableBlocks( downloadableBlocks, filterValue ) {\n\treturn {\n\t\ttype: 'RECEIVE_DOWNLOADABLE_BLOCKS',\n\t\tdownloadableBlocks,\n\t\tfilterValue,\n\t};\n}\n\n/**\n * Action triggered to install a block plugin.\n *\n * @param {Object} block The block item returned by search.\n *\n * @return {boolean} Whether the block was successfully installed & loaded.\n */\nexport const installBlockType = ( block ) => async ( {\n\tregistry,\n\tdispatch,\n} ) => {\n\tconst { id, name } = block;\n\tlet success = false;\n\tdispatch.clearErrorNotice( id );\n\ttry {\n\t\tdispatch.setIsInstalling( id, true );\n\n\t\t// If we have a wp:plugin link, the plugin is installed but inactive.\n\t\tconst url = getPluginUrl( block );\n\t\tlet links = {};\n\t\tif ( url ) {\n\t\t\tawait apiFetch( {\n\t\t\t\tmethod: 'PUT',\n\t\t\t\turl,\n\t\t\t\tdata: { status: 'active' },\n\t\t\t} );\n\t\t} else {\n\t\t\tconst response = await apiFetch( {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tpath: 'wp/v2/plugins',\n\t\t\t\tdata: { slug: id, status: 'active' },\n\t\t\t} );\n\t\t\t// Add the `self` link for newly-installed blocks.\n\t\t\tlinks = response._links;\n\t\t}\n\n\t\tdispatch.addInstalledBlockType( {\n\t\t\t...block,\n\t\t\tlinks: { ...block.links, ...links },\n\t\t} );\n\n\t\t// Ensures that the block metadata is propagated to the editor when registered on the server.\n\t\tconst metadataFields = [\n\t\t\t'api_version',\n\t\t\t'title',\n\t\t\t'category',\n\t\t\t'parent',\n\t\t\t'icon',\n\t\t\t'description',\n\t\t\t'keywords',\n\t\t\t'attributes',\n\t\t\t'provides_context',\n\t\t\t'uses_context',\n\t\t\t'supports',\n\t\t\t'styles',\n\t\t\t'example',\n\t\t\t'variations',\n\t\t];\n\t\tawait apiFetch( {\n\t\t\tpath: addQueryArgs( `/wp/v2/block-types/${ name }`, {\n\t\t\t\t_fields: metadataFields,\n\t\t\t} ),\n\t\t} )\n\t\t\t// Ignore when the block is not registered on the server.\n\t\t\t.catch( () => {} )\n\t\t\t.then( ( response ) => {\n\t\t\t\tif ( ! response ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tunstable__bootstrapServerSideBlockDefinitions( {\n\t\t\t\t\t[ name ]: pick( response, metadataFields ),\n\t\t\t\t} );\n\t\t\t} );\n\n\t\tawait loadAssets();\n\t\tconst registeredBlocks = registry.select( blocksStore ).getBlockTypes();\n\t\tif ( ! registeredBlocks.some( ( i ) => i.name === name ) ) {\n\t\t\tthrow new Error(\n\t\t\t\t__( 'Error registering block. Try reloading the page.' )\n\t\t\t);\n\t\t}\n\n\t\tregistry.dispatch( noticesStore ).createInfoNotice(\n\t\t\tsprintf(\n\t\t\t\t// translators: %s is the block title.\n\t\t\t\t__( 'Block %s installed and added.' ),\n\t\t\t\tblock.title\n\t\t\t),\n\t\t\t{\n\t\t\t\tspeak: true,\n\t\t\t\ttype: 'snackbar',\n\t\t\t}\n\t\t);\n\t\tsuccess = true;\n\t} catch ( error ) {\n\t\tlet message = error.message || __( 'An error occurred.' );\n\n\t\t// Errors we throw are fatal\n\t\tlet isFatal = error instanceof Error;\n\n\t\t// Specific API errors that are fatal\n\t\tconst fatalAPIErrors = {\n\t\t\tfolder_exists: __(\n\t\t\t\t'This block is already installed. Try reloading the page.'\n\t\t\t),\n\t\t\tunable_to_connect_to_filesystem: __(\n\t\t\t\t'Error installing block. You can reload the page and try again.'\n\t\t\t),\n\t\t};\n\n\t\tif ( fatalAPIErrors[ error.code ] ) {\n\t\t\tisFatal = true;\n\t\t\tmessage = fatalAPIErrors[ error.code ];\n\t\t}\n\n\t\tdispatch.setErrorNotice( id, message, isFatal );\n\t\tregistry.dispatch( noticesStore ).createErrorNotice( message, {\n\t\t\tspeak: true,\n\t\t\tisDismissible: true,\n\t\t} );\n\t}\n\tdispatch.setIsInstalling( id, false );\n\treturn success;\n};\n\n/**\n * Action triggered to uninstall a block plugin.\n *\n * @param {Object} block The blockType object.\n */\nexport const uninstallBlockType = ( block ) => async ( {\n\tregistry,\n\tdispatch,\n} ) => {\n\ttry {\n\t\tconst url = getPluginUrl( block );\n\t\tawait apiFetch( {\n\t\t\tmethod: 'PUT',\n\t\t\turl,\n\t\t\tdata: { status: 'inactive' },\n\t\t} );\n\t\tawait apiFetch( {\n\t\t\tmethod: 'DELETE',\n\t\t\turl,\n\t\t} );\n\t\tdispatch.removeInstalledBlockType( block );\n\t} catch ( error ) {\n\t\tregistry\n\t\t\t.dispatch( noticesStore )\n\t\t\t.createErrorNotice( error.message || __( 'An error occurred.' ) );\n\t}\n};\n\n/**\n * Returns an action object used to add a block type to the \"newly installed\"\n * tracking list.\n *\n * @param {Object} item The block item with the block id and name.\n *\n * @return {Object} Action object.\n */\nexport function addInstalledBlockType( item ) {\n\treturn {\n\t\ttype: 'ADD_INSTALLED_BLOCK_TYPE',\n\t\titem,\n\t};\n}\n\n/**\n * Returns an action object used to remove a block type from the \"newly installed\"\n * tracking list.\n *\n * @param {string} item The block item with the block id and name.\n *\n * @return {Object} Action object.\n */\nexport function removeInstalledBlockType( item ) {\n\treturn {\n\t\ttype: 'REMOVE_INSTALLED_BLOCK_TYPE',\n\t\titem,\n\t};\n}\n\n/**\n * Returns an action object used to indicate install in progress.\n *\n * @param {string} blockId\n * @param {boolean} isInstalling\n *\n * @return {Object} Action object.\n */\nexport function setIsInstalling( blockId, isInstalling ) {\n\treturn {\n\t\ttype: 'SET_INSTALLING_BLOCK',\n\t\tblockId,\n\t\tisInstalling,\n\t};\n}\n\n/**\n * Sets an error notice to be displayed to the user for a given block.\n *\n * @param {string} blockId The ID of the block plugin. eg: my-block\n * @param {string} message The message shown in the notice.\n * @param {boolean} isFatal Whether the user can recover from the error.\n *\n * @return {Object} Action object.\n */\nexport function setErrorNotice( blockId, message, isFatal = false ) {\n\treturn {\n\t\ttype: 'SET_ERROR_NOTICE',\n\t\tblockId,\n\t\tmessage,\n\t\tisFatal,\n\t};\n}\n\n/**\n * Sets the error notice to empty for specific block.\n *\n * @param {string} blockId The ID of the block plugin. eg: my-block\n *\n * @return {Object} Action object.\n */\nexport function clearErrorNotice( blockId ) {\n\treturn {\n\t\ttype: 'CLEAR_ERROR_NOTICE',\n\t\tblockId,\n\t};\n}\n"]}
@@ -1,10 +1,17 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { pick } from 'lodash';
1
5
  /**
2
6
  * WordPress dependencies
3
7
  */
4
- import { store as blocksStore } from '@wordpress/blocks';
8
+
9
+ import { store as blocksStore, unstable__bootstrapServerSideBlockDefinitions // eslint-disable-line camelcase
10
+ } from '@wordpress/blocks';
5
11
  import { __, sprintf } from '@wordpress/i18n';
6
12
  import apiFetch from '@wordpress/api-fetch';
7
13
  import { store as noticesStore } from '@wordpress/notices';
14
+ import { addQueryArgs } from '@wordpress/url';
8
15
  /**
9
16
  * Internal dependencies
10
17
  */
@@ -57,7 +64,8 @@ export const installBlockType = block => async _ref => {
57
64
  dispatch
58
65
  } = _ref;
59
66
  const {
60
- id
67
+ id,
68
+ name
61
69
  } = block;
62
70
  let success = false;
63
71
  dispatch.clearErrorNotice(id);
@@ -93,11 +101,27 @@ export const installBlockType = block => async _ref => {
93
101
  links: { ...block.links,
94
102
  ...links
95
103
  }
104
+ }); // Ensures that the block metadata is propagated to the editor when registered on the server.
105
+
106
+ const metadataFields = ['api_version', 'title', 'category', 'parent', 'icon', 'description', 'keywords', 'attributes', 'provides_context', 'uses_context', 'supports', 'styles', 'example', 'variations'];
107
+ await apiFetch({
108
+ path: addQueryArgs(`/wp/v2/block-types/${name}`, {
109
+ _fields: metadataFields
110
+ })
111
+ }) // Ignore when the block is not registered on the server.
112
+ .catch(() => {}).then(response => {
113
+ if (!response) {
114
+ return;
115
+ }
116
+
117
+ unstable__bootstrapServerSideBlockDefinitions({
118
+ [name]: pick(response, metadataFields)
119
+ });
96
120
  });
97
121
  await loadAssets();
98
122
  const registeredBlocks = registry.select(blocksStore).getBlockTypes();
99
123
 
100
- if (!registeredBlocks.some(i => i.name === block.name)) {
124
+ if (!registeredBlocks.some(i => i.name === name)) {
101
125
  throw new Error(__('Error registering block. Try reloading the page.'));
102
126
  }
103
127
 
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/block-directory/src/store/actions.js"],"names":["store","blocksStore","__","sprintf","apiFetch","noticesStore","loadAssets","getPluginUrl","fetchDownloadableBlocks","filterValue","type","receiveDownloadableBlocks","downloadableBlocks","installBlockType","block","registry","dispatch","id","success","clearErrorNotice","setIsInstalling","url","links","method","data","status","response","path","slug","_links","addInstalledBlockType","registeredBlocks","select","getBlockTypes","some","i","name","Error","createInfoNotice","title","speak","error","message","isFatal","fatalAPIErrors","folder_exists","unable_to_connect_to_filesystem","code","setErrorNotice","createErrorNotice","isDismissible","uninstallBlockType","removeInstalledBlockType","item","blockId","isInstalling"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,KAAK,IAAIC,WAAlB,QAAqC,mBAArC;AACA,SAASC,EAAT,EAAaC,OAAb,QAA4B,iBAA5B;AACA,OAAOC,QAAP,MAAqB,sBAArB;AACA,SAASJ,KAAK,IAAIK,YAAlB,QAAsC,oBAAtC;AAEA;AACA;AACA;;AACA,SAASC,UAAT,QAA2B,eAA3B;AACA,OAAOC,YAAP,MAAyB,wBAAzB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,uBAAT,CAAkCC,WAAlC,EAAgD;AACtD,SAAO;AAAEC,IAAAA,IAAI,EAAE,2BAAR;AAAqCD,IAAAA;AAArC,GAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASE,yBAAT,CAAoCC,kBAApC,EAAwDH,WAAx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A;AACA;;AACA,OAAO,SAASjC,eAAT,CAA0BkC,OAA1B,EAAmCC,YAAnC,EAAkD;AACxD,SAAO;AACN7C,IAAAA,IAAI,EAAE,sBADA;AAEN4C,IAAAA,OAFM;AAGNC,IAAAA;AAHM,GAAP;AAKA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASP,cAAT,CAAyBM,OAAzB,EAAkCZ,OAAlC,EAA6D;AAAA,MAAlBC,OAAkB,uEAAR,KAAQ;AACnE,SAAO;AACNjC,IAAAA,IAAI,EAAE,kBADA;AAEN4C,IAAAA,OAFM;AAGNZ,IAAAA,OAHM;AAINC,IAAAA;AAJM,GAAP;AAMA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASxB,gBAAT,CAA2BmC,OAA3B,EAAqC;AAC3C,SAAO;AACN5C,IAAAA,IAAI,EAAE,oBADA;AAEN4C,IAAAA;AAFM,GAAP;AAIA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { store as blocksStore } from '@wordpress/blocks';\nimport { __, sprintf } from '@wordpress/i18n';\nimport apiFetch from '@wordpress/api-fetch';\nimport { store as noticesStore } from '@wordpress/notices';\n\n/**\n * Internal dependencies\n */\nimport { loadAssets } from './load-assets';\nimport getPluginUrl from './utils/get-plugin-url';\n\n/**\n * Returns an action object used in signalling that the downloadable blocks\n * have been requested and are loading.\n *\n * @param {string} filterValue Search string.\n *\n * @return {Object} Action object.\n */\nexport function fetchDownloadableBlocks( filterValue ) {\n\treturn { type: 'FETCH_DOWNLOADABLE_BLOCKS', filterValue };\n}\n\n/**\n * Returns an action object used in signalling that the downloadable blocks\n * have been updated.\n *\n * @param {Array} downloadableBlocks Downloadable blocks.\n * @param {string} filterValue Search string.\n *\n * @return {Object} Action object.\n */\nexport function receiveDownloadableBlocks( downloadableBlocks, filterValue ) {\n\treturn {\n\t\ttype: 'RECEIVE_DOWNLOADABLE_BLOCKS',\n\t\tdownloadableBlocks,\n\t\tfilterValue,\n\t};\n}\n\n/**\n * Action triggered to install a block plugin.\n *\n * @param {Object} block The block item returned by search.\n *\n * @return {boolean} Whether the block was successfully installed & loaded.\n */\nexport const installBlockType = ( block ) => async ( {\n\tregistry,\n\tdispatch,\n} ) => {\n\tconst { id } = block;\n\tlet success = false;\n\tdispatch.clearErrorNotice( id );\n\ttry {\n\t\tdispatch.setIsInstalling( id, true );\n\n\t\t// If we have a wp:plugin link, the plugin is installed but inactive.\n\t\tconst url = getPluginUrl( block );\n\t\tlet links = {};\n\t\tif ( url ) {\n\t\t\tawait apiFetch( {\n\t\t\t\tmethod: 'PUT',\n\t\t\t\turl,\n\t\t\t\tdata: { status: 'active' },\n\t\t\t} );\n\t\t} else {\n\t\t\tconst response = await apiFetch( {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tpath: 'wp/v2/plugins',\n\t\t\t\tdata: { slug: id, status: 'active' },\n\t\t\t} );\n\t\t\t// Add the `self` link for newly-installed blocks.\n\t\t\tlinks = response._links;\n\t\t}\n\n\t\tdispatch.addInstalledBlockType( {\n\t\t\t...block,\n\t\t\tlinks: { ...block.links, ...links },\n\t\t} );\n\n\t\tawait loadAssets();\n\t\tconst registeredBlocks = registry.select( blocksStore ).getBlockTypes();\n\t\tif ( ! registeredBlocks.some( ( i ) => i.name === block.name ) ) {\n\t\t\tthrow new Error(\n\t\t\t\t__( 'Error registering block. Try reloading the page.' )\n\t\t\t);\n\t\t}\n\n\t\tregistry.dispatch( noticesStore ).createInfoNotice(\n\t\t\tsprintf(\n\t\t\t\t// translators: %s is the block title.\n\t\t\t\t__( 'Block %s installed and added.' ),\n\t\t\t\tblock.title\n\t\t\t),\n\t\t\t{\n\t\t\t\tspeak: true,\n\t\t\t\ttype: 'snackbar',\n\t\t\t}\n\t\t);\n\t\tsuccess = true;\n\t} catch ( error ) {\n\t\tlet message = error.message || __( 'An error occurred.' );\n\n\t\t// Errors we throw are fatal\n\t\tlet isFatal = error instanceof Error;\n\n\t\t// Specific API errors that are fatal\n\t\tconst fatalAPIErrors = {\n\t\t\tfolder_exists: __(\n\t\t\t\t'This block is already installed. Try reloading the page.'\n\t\t\t),\n\t\t\tunable_to_connect_to_filesystem: __(\n\t\t\t\t'Error installing block. You can reload the page and try again.'\n\t\t\t),\n\t\t};\n\n\t\tif ( fatalAPIErrors[ error.code ] ) {\n\t\t\tisFatal = true;\n\t\t\tmessage = fatalAPIErrors[ error.code ];\n\t\t}\n\n\t\tdispatch.setErrorNotice( id, message, isFatal );\n\t\tregistry.dispatch( noticesStore ).createErrorNotice( message, {\n\t\t\tspeak: true,\n\t\t\tisDismissible: true,\n\t\t} );\n\t}\n\tdispatch.setIsInstalling( id, false );\n\treturn success;\n};\n\n/**\n * Action triggered to uninstall a block plugin.\n *\n * @param {Object} block The blockType object.\n */\nexport const uninstallBlockType = ( block ) => async ( {\n\tregistry,\n\tdispatch,\n} ) => {\n\ttry {\n\t\tconst url = getPluginUrl( block );\n\t\tawait apiFetch( {\n\t\t\tmethod: 'PUT',\n\t\t\turl,\n\t\t\tdata: { status: 'inactive' },\n\t\t} );\n\t\tawait apiFetch( {\n\t\t\tmethod: 'DELETE',\n\t\t\turl,\n\t\t} );\n\t\tdispatch.removeInstalledBlockType( block );\n\t} catch ( error ) {\n\t\tregistry\n\t\t\t.dispatch( noticesStore )\n\t\t\t.createErrorNotice( error.message || __( 'An error occurred.' ) );\n\t}\n};\n\n/**\n * Returns an action object used to add a block type to the \"newly installed\"\n * tracking list.\n *\n * @param {Object} item The block item with the block id and name.\n *\n * @return {Object} Action object.\n */\nexport function addInstalledBlockType( item ) {\n\treturn {\n\t\ttype: 'ADD_INSTALLED_BLOCK_TYPE',\n\t\titem,\n\t};\n}\n\n/**\n * Returns an action object used to remove a block type from the \"newly installed\"\n * tracking list.\n *\n * @param {string} item The block item with the block id and name.\n *\n * @return {Object} Action object.\n */\nexport function removeInstalledBlockType( item ) {\n\treturn {\n\t\ttype: 'REMOVE_INSTALLED_BLOCK_TYPE',\n\t\titem,\n\t};\n}\n\n/**\n * Returns an action object used to indicate install in progress.\n *\n * @param {string} blockId\n * @param {boolean} isInstalling\n *\n * @return {Object} Action object.\n */\nexport function setIsInstalling( blockId, isInstalling ) {\n\treturn {\n\t\ttype: 'SET_INSTALLING_BLOCK',\n\t\tblockId,\n\t\tisInstalling,\n\t};\n}\n\n/**\n * Sets an error notice to be displayed to the user for a given block.\n *\n * @param {string} blockId The ID of the block plugin. eg: my-block\n * @param {string} message The message shown in the notice.\n * @param {boolean} isFatal Whether the user can recover from the error.\n *\n * @return {Object} Action object.\n */\nexport function setErrorNotice( blockId, message, isFatal = false ) {\n\treturn {\n\t\ttype: 'SET_ERROR_NOTICE',\n\t\tblockId,\n\t\tmessage,\n\t\tisFatal,\n\t};\n}\n\n/**\n * Sets the error notice to empty for specific block.\n *\n * @param {string} blockId The ID of the block plugin. eg: my-block\n *\n * @return {Object} Action object.\n */\nexport function clearErrorNotice( blockId ) {\n\treturn {\n\t\ttype: 'CLEAR_ERROR_NOTICE',\n\t\tblockId,\n\t};\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/block-directory/src/store/actions.js"],"names":["pick","store","blocksStore","unstable__bootstrapServerSideBlockDefinitions","__","sprintf","apiFetch","noticesStore","addQueryArgs","loadAssets","getPluginUrl","fetchDownloadableBlocks","filterValue","type","receiveDownloadableBlocks","downloadableBlocks","installBlockType","block","registry","dispatch","id","name","success","clearErrorNotice","setIsInstalling","url","links","method","data","status","response","path","slug","_links","addInstalledBlockType","metadataFields","_fields","catch","then","registeredBlocks","select","getBlockTypes","some","i","Error","createInfoNotice","title","speak","error","message","isFatal","fatalAPIErrors","folder_exists","unable_to_connect_to_filesystem","code","setErrorNotice","createErrorNotice","isDismissible","uninstallBlockType","removeInstalledBlockType","item","blockId","isInstalling"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,IAAT,QAAqB,QAArB;AAEA;AACA;AACA;;AACA,SACCC,KAAK,IAAIC,WADV,EAECC,6CAFD,CAEgD;AAFhD,O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EAAmCC,YAAnC,EAAkD;AACxD,SAAO;AACNjD,IAAAA,IAAI,EAAE,sBADA;AAENgD,IAAAA,OAFM;AAGNC,IAAAA;AAHM,GAAP;AAKA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASP,cAAT,CAAyBM,OAAzB,EAAkCZ,OAAlC,EAA6D;AAAA,MAAlBC,OAAkB,uEAAR,KAAQ;AACnE,SAAO;AACNrC,IAAAA,IAAI,EAAE,kBADA;AAENgD,IAAAA,OAFM;AAGNZ,IAAAA,OAHM;AAINC,IAAAA;AAJM,GAAP;AAMA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAAS3B,gBAAT,CAA2BsC,OAA3B,EAAqC;AAC3C,SAAO;AACNhD,IAAAA,IAAI,EAAE,oBADA;AAENgD,IAAAA;AAFM,GAAP;AAIA","sourcesContent":["/**\n * External dependencies\n */\nimport { pick } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tstore as blocksStore,\n\tunstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase\n} from '@wordpress/blocks';\nimport { __, sprintf } from '@wordpress/i18n';\nimport apiFetch from '@wordpress/api-fetch';\nimport { store as noticesStore } from '@wordpress/notices';\nimport { addQueryArgs } from '@wordpress/url';\n\n/**\n * Internal dependencies\n */\nimport { loadAssets } from './load-assets';\nimport getPluginUrl from './utils/get-plugin-url';\n\n/**\n * Returns an action object used in signalling that the downloadable blocks\n * have been requested and are loading.\n *\n * @param {string} filterValue Search string.\n *\n * @return {Object} Action object.\n */\nexport function fetchDownloadableBlocks( filterValue ) {\n\treturn { type: 'FETCH_DOWNLOADABLE_BLOCKS', filterValue };\n}\n\n/**\n * Returns an action object used in signalling that the downloadable blocks\n * have been updated.\n *\n * @param {Array} downloadableBlocks Downloadable blocks.\n * @param {string} filterValue Search string.\n *\n * @return {Object} Action object.\n */\nexport function receiveDownloadableBlocks( downloadableBlocks, filterValue ) {\n\treturn {\n\t\ttype: 'RECEIVE_DOWNLOADABLE_BLOCKS',\n\t\tdownloadableBlocks,\n\t\tfilterValue,\n\t};\n}\n\n/**\n * Action triggered to install a block plugin.\n *\n * @param {Object} block The block item returned by search.\n *\n * @return {boolean} Whether the block was successfully installed & loaded.\n */\nexport const installBlockType = ( block ) => async ( {\n\tregistry,\n\tdispatch,\n} ) => {\n\tconst { id, name } = block;\n\tlet success = false;\n\tdispatch.clearErrorNotice( id );\n\ttry {\n\t\tdispatch.setIsInstalling( id, true );\n\n\t\t// If we have a wp:plugin link, the plugin is installed but inactive.\n\t\tconst url = getPluginUrl( block );\n\t\tlet links = {};\n\t\tif ( url ) {\n\t\t\tawait apiFetch( {\n\t\t\t\tmethod: 'PUT',\n\t\t\t\turl,\n\t\t\t\tdata: { status: 'active' },\n\t\t\t} );\n\t\t} else {\n\t\t\tconst response = await apiFetch( {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tpath: 'wp/v2/plugins',\n\t\t\t\tdata: { slug: id, status: 'active' },\n\t\t\t} );\n\t\t\t// Add the `self` link for newly-installed blocks.\n\t\t\tlinks = response._links;\n\t\t}\n\n\t\tdispatch.addInstalledBlockType( {\n\t\t\t...block,\n\t\t\tlinks: { ...block.links, ...links },\n\t\t} );\n\n\t\t// Ensures that the block metadata is propagated to the editor when registered on the server.\n\t\tconst metadataFields = [\n\t\t\t'api_version',\n\t\t\t'title',\n\t\t\t'category',\n\t\t\t'parent',\n\t\t\t'icon',\n\t\t\t'description',\n\t\t\t'keywords',\n\t\t\t'attributes',\n\t\t\t'provides_context',\n\t\t\t'uses_context',\n\t\t\t'supports',\n\t\t\t'styles',\n\t\t\t'example',\n\t\t\t'variations',\n\t\t];\n\t\tawait apiFetch( {\n\t\t\tpath: addQueryArgs( `/wp/v2/block-types/${ name }`, {\n\t\t\t\t_fields: metadataFields,\n\t\t\t} ),\n\t\t} )\n\t\t\t// Ignore when the block is not registered on the server.\n\t\t\t.catch( () => {} )\n\t\t\t.then( ( response ) => {\n\t\t\t\tif ( ! response ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tunstable__bootstrapServerSideBlockDefinitions( {\n\t\t\t\t\t[ name ]: pick( response, metadataFields ),\n\t\t\t\t} );\n\t\t\t} );\n\n\t\tawait loadAssets();\n\t\tconst registeredBlocks = registry.select( blocksStore ).getBlockTypes();\n\t\tif ( ! registeredBlocks.some( ( i ) => i.name === name ) ) {\n\t\t\tthrow new Error(\n\t\t\t\t__( 'Error registering block. Try reloading the page.' )\n\t\t\t);\n\t\t}\n\n\t\tregistry.dispatch( noticesStore ).createInfoNotice(\n\t\t\tsprintf(\n\t\t\t\t// translators: %s is the block title.\n\t\t\t\t__( 'Block %s installed and added.' ),\n\t\t\t\tblock.title\n\t\t\t),\n\t\t\t{\n\t\t\t\tspeak: true,\n\t\t\t\ttype: 'snackbar',\n\t\t\t}\n\t\t);\n\t\tsuccess = true;\n\t} catch ( error ) {\n\t\tlet message = error.message || __( 'An error occurred.' );\n\n\t\t// Errors we throw are fatal\n\t\tlet isFatal = error instanceof Error;\n\n\t\t// Specific API errors that are fatal\n\t\tconst fatalAPIErrors = {\n\t\t\tfolder_exists: __(\n\t\t\t\t'This block is already installed. Try reloading the page.'\n\t\t\t),\n\t\t\tunable_to_connect_to_filesystem: __(\n\t\t\t\t'Error installing block. You can reload the page and try again.'\n\t\t\t),\n\t\t};\n\n\t\tif ( fatalAPIErrors[ error.code ] ) {\n\t\t\tisFatal = true;\n\t\t\tmessage = fatalAPIErrors[ error.code ];\n\t\t}\n\n\t\tdispatch.setErrorNotice( id, message, isFatal );\n\t\tregistry.dispatch( noticesStore ).createErrorNotice( message, {\n\t\t\tspeak: true,\n\t\t\tisDismissible: true,\n\t\t} );\n\t}\n\tdispatch.setIsInstalling( id, false );\n\treturn success;\n};\n\n/**\n * Action triggered to uninstall a block plugin.\n *\n * @param {Object} block The blockType object.\n */\nexport const uninstallBlockType = ( block ) => async ( {\n\tregistry,\n\tdispatch,\n} ) => {\n\ttry {\n\t\tconst url = getPluginUrl( block );\n\t\tawait apiFetch( {\n\t\t\tmethod: 'PUT',\n\t\t\turl,\n\t\t\tdata: { status: 'inactive' },\n\t\t} );\n\t\tawait apiFetch( {\n\t\t\tmethod: 'DELETE',\n\t\t\turl,\n\t\t} );\n\t\tdispatch.removeInstalledBlockType( block );\n\t} catch ( error ) {\n\t\tregistry\n\t\t\t.dispatch( noticesStore )\n\t\t\t.createErrorNotice( error.message || __( 'An error occurred.' ) );\n\t}\n};\n\n/**\n * Returns an action object used to add a block type to the \"newly installed\"\n * tracking list.\n *\n * @param {Object} item The block item with the block id and name.\n *\n * @return {Object} Action object.\n */\nexport function addInstalledBlockType( item ) {\n\treturn {\n\t\ttype: 'ADD_INSTALLED_BLOCK_TYPE',\n\t\titem,\n\t};\n}\n\n/**\n * Returns an action object used to remove a block type from the \"newly installed\"\n * tracking list.\n *\n * @param {string} item The block item with the block id and name.\n *\n * @return {Object} Action object.\n */\nexport function removeInstalledBlockType( item ) {\n\treturn {\n\t\ttype: 'REMOVE_INSTALLED_BLOCK_TYPE',\n\t\titem,\n\t};\n}\n\n/**\n * Returns an action object used to indicate install in progress.\n *\n * @param {string} blockId\n * @param {boolean} isInstalling\n *\n * @return {Object} Action object.\n */\nexport function setIsInstalling( blockId, isInstalling ) {\n\treturn {\n\t\ttype: 'SET_INSTALLING_BLOCK',\n\t\tblockId,\n\t\tisInstalling,\n\t};\n}\n\n/**\n * Sets an error notice to be displayed to the user for a given block.\n *\n * @param {string} blockId The ID of the block plugin. eg: my-block\n * @param {string} message The message shown in the notice.\n * @param {boolean} isFatal Whether the user can recover from the error.\n *\n * @return {Object} Action object.\n */\nexport function setErrorNotice( blockId, message, isFatal = false ) {\n\treturn {\n\t\ttype: 'SET_ERROR_NOTICE',\n\t\tblockId,\n\t\tmessage,\n\t\tisFatal,\n\t};\n}\n\n/**\n * Sets the error notice to empty for specific block.\n *\n * @param {string} blockId The ID of the block plugin. eg: my-block\n *\n * @return {Object} Action object.\n */\nexport function clearErrorNotice( blockId ) {\n\treturn {\n\t\ttype: 'CLEAR_ERROR_NOTICE',\n\t\tblockId,\n\t};\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/block-directory",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
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,25 +28,26 @@
28
28
  "@babel/runtime": "^7.16.0",
29
29
  "@wordpress/a11y": "^3.3.1",
30
30
  "@wordpress/api-fetch": "^6.0.1",
31
- "@wordpress/block-editor": "^8.1.1",
31
+ "@wordpress/block-editor": "^8.1.2",
32
32
  "@wordpress/blocks": "^11.2.1",
33
- "@wordpress/components": "^19.4.0",
33
+ "@wordpress/components": "^19.4.1",
34
34
  "@wordpress/compose": "^5.1.1",
35
35
  "@wordpress/core-data": "^4.1.1",
36
36
  "@wordpress/data": "^6.2.1",
37
- "@wordpress/edit-post": "^6.0.0",
38
- "@wordpress/editor": "^12.2.0",
37
+ "@wordpress/edit-post": "^6.0.1",
38
+ "@wordpress/editor": "^12.2.1",
39
39
  "@wordpress/element": "^4.1.1",
40
40
  "@wordpress/hooks": "^3.3.1",
41
41
  "@wordpress/html-entities": "^3.3.1",
42
42
  "@wordpress/i18n": "^4.3.1",
43
- "@wordpress/icons": "^6.3.0",
43
+ "@wordpress/icons": "^7.0.0",
44
44
  "@wordpress/notices": "^3.3.1",
45
- "@wordpress/plugins": "^4.1.1",
45
+ "@wordpress/plugins": "^4.1.2",
46
+ "@wordpress/url": "^3.4.1",
46
47
  "lodash": "^4.17.21"
47
48
  },
48
49
  "publishConfig": {
49
50
  "access": "public"
50
51
  },
51
- "gitHead": "2e4922861e49f5a090f9dc52056165092cfba163"
52
+ "gitHead": "0f838547f37472e46e6d5a9d2e84b6c61f4bd3f0"
52
53
  }
@@ -1,10 +1,19 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { pick } from 'lodash';
5
+
1
6
  /**
2
7
  * WordPress dependencies
3
8
  */
4
- import { store as blocksStore } from '@wordpress/blocks';
9
+ import {
10
+ store as blocksStore,
11
+ unstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase
12
+ } from '@wordpress/blocks';
5
13
  import { __, sprintf } from '@wordpress/i18n';
6
14
  import apiFetch from '@wordpress/api-fetch';
7
15
  import { store as noticesStore } from '@wordpress/notices';
16
+ import { addQueryArgs } from '@wordpress/url';
8
17
 
9
18
  /**
10
19
  * Internal dependencies
@@ -52,7 +61,7 @@ export const installBlockType = ( block ) => async ( {
52
61
  registry,
53
62
  dispatch,
54
63
  } ) => {
55
- const { id } = block;
64
+ const { id, name } = block;
56
65
  let success = false;
57
66
  dispatch.clearErrorNotice( id );
58
67
  try {
@@ -82,9 +91,42 @@ export const installBlockType = ( block ) => async ( {
82
91
  links: { ...block.links, ...links },
83
92
  } );
84
93
 
94
+ // Ensures that the block metadata is propagated to the editor when registered on the server.
95
+ const metadataFields = [
96
+ 'api_version',
97
+ 'title',
98
+ 'category',
99
+ 'parent',
100
+ 'icon',
101
+ 'description',
102
+ 'keywords',
103
+ 'attributes',
104
+ 'provides_context',
105
+ 'uses_context',
106
+ 'supports',
107
+ 'styles',
108
+ 'example',
109
+ 'variations',
110
+ ];
111
+ await apiFetch( {
112
+ path: addQueryArgs( `/wp/v2/block-types/${ name }`, {
113
+ _fields: metadataFields,
114
+ } ),
115
+ } )
116
+ // Ignore when the block is not registered on the server.
117
+ .catch( () => {} )
118
+ .then( ( response ) => {
119
+ if ( ! response ) {
120
+ return;
121
+ }
122
+ unstable__bootstrapServerSideBlockDefinitions( {
123
+ [ name ]: pick( response, metadataFields ),
124
+ } );
125
+ } );
126
+
85
127
  await loadAssets();
86
128
  const registeredBlocks = registry.select( blocksStore ).getBlockTypes();
87
- if ( ! registeredBlocks.some( ( i ) => i.name === block.name ) ) {
129
+ if ( ! registeredBlocks.some( ( i ) => i.name === name ) ) {
88
130
  throw new Error(
89
131
  __( 'Error registering block. Try reloading the page.' )
90
132
  );
@@ -75,6 +75,12 @@ describe( 'actions', () => {
75
75
  },
76
76
  };
77
77
 
78
+ const blockTypePath = '/wp/v2/block-types/block/block';
79
+ const blockTypeResponse = {
80
+ name: 'block/block',
81
+ title: 'Test Block',
82
+ };
83
+
78
84
  describe( 'installBlockType', () => {
79
85
  it( 'should install a block successfully', async () => {
80
86
  const registry = createRegistryWithStores();
@@ -84,6 +90,8 @@ describe( 'actions', () => {
84
90
  switch ( path ) {
85
91
  case 'wp/v2/plugins':
86
92
  return pluginResponse;
93
+ case blockTypePath:
94
+ return blockTypeResponse;
87
95
  default:
88
96
  throw new Error( `unexpected API endpoint: ${ path }` );
89
97
  }
@@ -121,14 +129,14 @@ describe( 'actions', () => {
121
129
  const registry = createRegistryWithStores();
122
130
 
123
131
  // mock the api-fetch and load-assets modules
124
- apiFetch.mockImplementation( async ( p ) => {
125
- const { url } = p;
126
- switch ( url ) {
127
- case pluginEndpoint:
128
- return pluginResponse;
129
- default:
130
- throw new Error( `unexpected API endpoint: ${ url }` );
132
+ apiFetch.mockImplementation( async ( { path, url } ) => {
133
+ if ( path === blockTypePath ) {
134
+ return blockTypeResponse;
135
+ }
136
+ if ( url === pluginEndpoint ) {
137
+ return pluginResponse;
131
138
  }
139
+ throw new Error( `unexpected API endpoint: ${ url }` );
132
140
  } );
133
141
 
134
142
  loadAssets.mockImplementation( loadAssetsMock( registry ) );