@wordpress/block-directory 3.0.1 → 3.0.5

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 (36) hide show
  1. package/README.md +1 -1
  2. package/build/plugins/get-install-missing/install-button.js +2 -2
  3. package/build/plugins/get-install-missing/install-button.js.map +1 -1
  4. package/build/store/actions.js +44 -37
  5. package/build/store/actions.js.map +1 -1
  6. package/build/store/index.js +3 -9
  7. package/build/store/index.js.map +1 -1
  8. package/build/store/{controls.js → load-assets.js} +25 -41
  9. package/build/store/load-assets.js.map +1 -0
  10. package/build/store/resolvers.js +19 -19
  11. package/build/store/resolvers.js.map +1 -1
  12. package/build/store/selectors.js +4 -8
  13. package/build/store/selectors.js.map +1 -1
  14. package/build-module/plugins/get-install-missing/install-button.js +2 -2
  15. package/build-module/plugins/get-install-missing/install-button.js.map +1 -1
  16. package/build-module/store/actions.js +39 -34
  17. package/build-module/store/actions.js.map +1 -1
  18. package/build-module/store/index.js +3 -7
  19. package/build-module/store/index.js.map +1 -1
  20. package/build-module/store/{controls.js → load-assets.js} +23 -37
  21. package/build-module/store/load-assets.js.map +1 -0
  22. package/build-module/store/resolvers.js +14 -17
  23. package/build-module/store/resolvers.js.map +1 -1
  24. package/build-module/store/selectors.js +4 -8
  25. package/build-module/store/selectors.js.map +1 -1
  26. package/package.json +20 -21
  27. package/src/plugins/get-install-missing/install-button.js +4 -3
  28. package/src/store/actions.js +38 -46
  29. package/src/store/index.js +2 -4
  30. package/src/store/{controls.js → load-assets.js} +25 -42
  31. package/src/store/resolvers.js +17 -19
  32. package/src/store/selectors.js +2 -14
  33. package/src/store/test/actions.js +236 -267
  34. package/src/store/test/{controls.js → load-assets.js} +1 -1
  35. package/build/store/controls.js.map +0 -1
  36. package/build-module/store/controls.js.map +0 -1
@@ -3,14 +3,13 @@
3
3
  */
4
4
  import { store as blocksStore } from '@wordpress/blocks';
5
5
  import { __, sprintf } from '@wordpress/i18n';
6
- import { controls } from '@wordpress/data';
7
- import { apiFetch } from '@wordpress/data-controls';
6
+ import apiFetch from '@wordpress/api-fetch';
8
7
  import { store as noticesStore } from '@wordpress/notices';
9
8
 
10
9
  /**
11
10
  * Internal dependencies
12
11
  */
13
- import { loadAssets } from './controls';
12
+ import { loadAssets } from './load-assets';
14
13
  import getPluginUrl from './utils/get-plugin-url';
15
14
 
16
15
  /**
@@ -49,56 +48,49 @@ export function receiveDownloadableBlocks( downloadableBlocks, filterValue ) {
49
48
  *
50
49
  * @return {boolean} Whether the block was successfully installed & loaded.
51
50
  */
52
- export function* installBlockType( block ) {
53
- const { id, assets } = block;
51
+ export const installBlockType = ( block ) => async ( {
52
+ registry,
53
+ dispatch,
54
+ } ) => {
55
+ const { id } = block;
54
56
  let success = false;
55
- yield clearErrorNotice( id );
57
+ dispatch.clearErrorNotice( id );
56
58
  try {
57
- yield setIsInstalling( block.id, true );
59
+ dispatch.setIsInstalling( id, true );
58
60
 
59
61
  // If we have a wp:plugin link, the plugin is installed but inactive.
60
62
  const url = getPluginUrl( block );
61
63
  let links = {};
62
64
  if ( url ) {
63
- yield apiFetch( {
64
- url,
65
- data: {
66
- status: 'active',
67
- },
65
+ await apiFetch( {
68
66
  method: 'PUT',
67
+ url,
68
+ data: { status: 'active' },
69
69
  } );
70
70
  } else {
71
- const response = yield apiFetch( {
72
- path: 'wp/v2/plugins',
73
- data: {
74
- slug: block.id,
75
- status: 'active',
76
- },
71
+ const response = await apiFetch( {
77
72
  method: 'POST',
73
+ path: 'wp/v2/plugins',
74
+ data: { slug: id, status: 'active' },
78
75
  } );
79
76
  // Add the `self` link for newly-installed blocks.
80
77
  links = response._links;
81
78
  }
82
79
 
83
- yield addInstalledBlockType( {
80
+ dispatch.addInstalledBlockType( {
84
81
  ...block,
85
82
  links: { ...block.links, ...links },
86
83
  } );
87
84
 
88
- yield loadAssets( assets );
89
- const registeredBlocks = yield controls.select(
90
- blocksStore.name,
91
- 'getBlockTypes'
92
- );
85
+ await loadAssets();
86
+ const registeredBlocks = registry.select( blocksStore ).getBlockTypes();
93
87
  if ( ! registeredBlocks.some( ( i ) => i.name === block.name ) ) {
94
88
  throw new Error(
95
89
  __( 'Error registering block. Try reloading the page.' )
96
90
  );
97
91
  }
98
92
 
99
- yield controls.dispatch(
100
- noticesStore,
101
- 'createInfoNotice',
93
+ registry.dispatch( noticesStore ).createInfoNotice(
102
94
  sprintf(
103
95
  // translators: %s is the block title.
104
96
  __( 'Block %s installed and added.' ),
@@ -131,43 +123,43 @@ export function* installBlockType( block ) {
131
123
  message = fatalAPIErrors[ error.code ];
132
124
  }
133
125
 
134
- yield setErrorNotice( id, message, isFatal );
135
- yield controls.dispatch( noticesStore, 'createErrorNotice', message, {
126
+ dispatch.setErrorNotice( id, message, isFatal );
127
+ registry.dispatch( noticesStore ).createErrorNotice( message, {
136
128
  speak: true,
137
129
  isDismissible: true,
138
130
  } );
139
131
  }
140
- yield setIsInstalling( block.id, false );
132
+ dispatch.setIsInstalling( id, false );
141
133
  return success;
142
- }
134
+ };
143
135
 
144
136
  /**
145
137
  * Action triggered to uninstall a block plugin.
146
138
  *
147
139
  * @param {Object} block The blockType object.
148
140
  */
149
- export function* uninstallBlockType( block ) {
141
+ export const uninstallBlockType = ( block ) => async ( {
142
+ registry,
143
+ dispatch,
144
+ } ) => {
150
145
  try {
151
- yield apiFetch( {
152
- url: getPluginUrl( block ),
153
- data: {
154
- status: 'inactive',
155
- },
146
+ const url = getPluginUrl( block );
147
+ await apiFetch( {
156
148
  method: 'PUT',
149
+ url,
150
+ data: { status: 'inactive' },
157
151
  } );
158
- yield apiFetch( {
159
- url: getPluginUrl( block ),
152
+ await apiFetch( {
160
153
  method: 'DELETE',
154
+ url,
161
155
  } );
162
- yield removeInstalledBlockType( block );
156
+ dispatch.removeInstalledBlockType( block );
163
157
  } catch ( error ) {
164
- yield controls.dispatch(
165
- noticesStore,
166
- 'createErrorNotice',
167
- error.message || __( 'An error occurred.' )
168
- );
158
+ registry
159
+ .dispatch( noticesStore )
160
+ .createErrorNotice( error.message || __( 'An error occurred.' ) );
169
161
  }
170
- }
162
+ };
171
163
 
172
164
  /**
173
165
  * Returns an action object used to add a block type to the "newly installed"
@@ -2,7 +2,6 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
  import { createReduxStore, register } from '@wordpress/data';
5
- import { controls as dataControls } from '@wordpress/data-controls';
6
5
 
7
6
  /**
8
7
  * Internal dependencies
@@ -10,8 +9,7 @@ import { controls as dataControls } from '@wordpress/data-controls';
10
9
  import reducer from './reducer';
11
10
  import * as selectors from './selectors';
12
11
  import * as actions from './actions';
13
- import resolvers from './resolvers';
14
- import controls from './controls';
12
+ import * as resolvers from './resolvers';
15
13
 
16
14
  /**
17
15
  * Module Constants
@@ -29,8 +27,8 @@ export const storeConfig = {
29
27
  reducer,
30
28
  selectors,
31
29
  actions,
32
- controls: { ...dataControls, ...controls },
33
30
  resolvers,
31
+ __experimentalUseThunks: true,
34
32
  };
35
33
 
36
34
  /**
@@ -49,50 +49,33 @@ export const loadAsset = ( el ) => {
49
49
 
50
50
  /**
51
51
  * Load the asset files for a block
52
- *
53
- * @param {Array} assets A collection of URLs for the assets.
54
- *
55
- * @return {Object} Control descriptor.
56
52
  */
57
- export function loadAssets( assets ) {
58
- return {
59
- type: 'LOAD_ASSETS',
60
- assets,
61
- };
62
- }
63
-
64
- const controls = {
65
- async LOAD_ASSETS() {
66
- /*
67
- * Fetch the current URL (post-new.php, or post.php?post=1&action=edit) and compare the
68
- * JavaScript and CSS assets loaded between the pages. This imports the required assets
69
- * for the block into the current page while not requiring that we know them up-front.
70
- * In the future this can be improved by reliance upon block.json and/or a script-loader
71
- * dependency API.
72
- */
73
- const response = await apiFetch( {
74
- url: document.location.href,
75
- parse: false,
76
- } );
53
+ export async function loadAssets() {
54
+ /*
55
+ * Fetch the current URL (post-new.php, or post.php?post=1&action=edit) and compare the
56
+ * JavaScript and CSS assets loaded between the pages. This imports the required assets
57
+ * for the block into the current page while not requiring that we know them up-front.
58
+ * In the future this can be improved by reliance upon block.json and/or a script-loader
59
+ * dependency API.
60
+ */
61
+ const response = await apiFetch( {
62
+ url: document.location.href,
63
+ parse: false,
64
+ } );
77
65
 
78
- const data = await response.text();
66
+ const data = await response.text();
79
67
 
80
- const doc = new window.DOMParser().parseFromString( data, 'text/html' );
68
+ const doc = new window.DOMParser().parseFromString( data, 'text/html' );
81
69
 
82
- const newAssets = Array.from(
83
- doc.querySelectorAll( 'link[rel="stylesheet"],script' )
84
- ).filter(
85
- ( asset ) => asset.id && ! document.getElementById( asset.id )
86
- );
70
+ const newAssets = Array.from(
71
+ doc.querySelectorAll( 'link[rel="stylesheet"],script' )
72
+ ).filter( ( asset ) => asset.id && ! document.getElementById( asset.id ) );
87
73
 
88
- /*
89
- * Load each asset in order, as they may depend upon an earlier loaded script.
90
- * Stylesheets and Inline Scripts will resolve immediately upon insertion.
91
- */
92
- for ( const newAsset of newAssets ) {
93
- await loadAsset( newAsset );
94
- }
95
- },
96
- };
97
-
98
- export default controls;
74
+ /*
75
+ * Load each asset in order, as they may depend upon an earlier loaded script.
76
+ * Stylesheets and Inline Scripts will resolve immediately upon insertion.
77
+ */
78
+ for ( const newAsset of newAssets ) {
79
+ await loadAsset( newAsset );
80
+ }
81
+ }
@@ -6,31 +6,29 @@ import { camelCase, mapKeys } from 'lodash';
6
6
  /**
7
7
  * WordPress dependencies
8
8
  */
9
- import { apiFetch } from '@wordpress/data-controls';
9
+ import apiFetch from '@wordpress/api-fetch';
10
10
 
11
11
  /**
12
12
  * Internal dependencies
13
13
  */
14
14
  import { fetchDownloadableBlocks, receiveDownloadableBlocks } from './actions';
15
15
 
16
- export default {
17
- *getDownloadableBlocks( filterValue ) {
18
- if ( ! filterValue ) {
19
- return;
20
- }
16
+ export const getDownloadableBlocks = ( filterValue ) => async ( {
17
+ dispatch,
18
+ } ) => {
19
+ if ( ! filterValue ) {
20
+ return;
21
+ }
21
22
 
22
- try {
23
- yield fetchDownloadableBlocks( filterValue );
24
- const results = yield apiFetch( {
25
- path: `wp/v2/block-directory/search?term=${ filterValue }`,
26
- } );
27
- const blocks = results.map( ( result ) =>
28
- mapKeys( result, ( value, key ) => {
29
- return camelCase( key );
30
- } )
31
- );
23
+ try {
24
+ dispatch( fetchDownloadableBlocks( filterValue ) );
25
+ const results = await apiFetch( {
26
+ path: `wp/v2/block-directory/search?term=${ filterValue }`,
27
+ } );
28
+ const blocks = results.map( ( result ) =>
29
+ mapKeys( result, ( value, key ) => camelCase( key ) )
30
+ );
32
31
 
33
- yield receiveDownloadableBlocks( blocks, filterValue );
34
- } catch ( error ) {}
35
- },
32
+ dispatch( receiveDownloadableBlocks( blocks, filterValue ) );
33
+ } catch {}
36
34
  };
@@ -18,13 +18,7 @@ import hasBlockType from './utils/has-block-type';
18
18
  * @return {boolean} Whether a request is in progress for the blocks list.
19
19
  */
20
20
  export function isRequestingDownloadableBlocks( state, filterValue ) {
21
- if (
22
- ! state.downloadableBlocks[ filterValue ] ||
23
- ! state.downloadableBlocks[ filterValue ].isRequesting
24
- ) {
25
- return false;
26
- }
27
- return state.downloadableBlocks[ filterValue ].isRequesting;
21
+ return state.downloadableBlocks[ filterValue ]?.isRequesting ?? false;
28
22
  }
29
23
 
30
24
  /**
@@ -36,13 +30,7 @@ export function isRequestingDownloadableBlocks( state, filterValue ) {
36
30
  * @return {Array} Downloadable blocks.
37
31
  */
38
32
  export function getDownloadableBlocks( state, filterValue ) {
39
- if (
40
- ! state.downloadableBlocks[ filterValue ] ||
41
- ! state.downloadableBlocks[ filterValue ].results
42
- ) {
43
- return [];
44
- }
45
- return state.downloadableBlocks[ filterValue ].results;
33
+ return state.downloadableBlocks[ filterValue ]?.results ?? [];
46
34
  }
47
35
 
48
36
  /**