@wordpress/block-directory 3.0.2 → 3.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/build/plugins/get-install-missing/install-button.js +2 -2
- package/build/plugins/get-install-missing/install-button.js.map +1 -1
- package/build/store/actions.js +44 -37
- package/build/store/actions.js.map +1 -1
- package/build/store/index.js +3 -9
- package/build/store/index.js.map +1 -1
- package/build/store/{controls.js → load-assets.js} +25 -41
- package/build/store/load-assets.js.map +1 -0
- package/build/store/resolvers.js +19 -19
- package/build/store/resolvers.js.map +1 -1
- package/build/store/selectors.js +4 -8
- package/build/store/selectors.js.map +1 -1
- package/build-module/plugins/get-install-missing/install-button.js +2 -2
- package/build-module/plugins/get-install-missing/install-button.js.map +1 -1
- package/build-module/store/actions.js +39 -34
- package/build-module/store/actions.js.map +1 -1
- package/build-module/store/index.js +3 -7
- package/build-module/store/index.js.map +1 -1
- package/build-module/store/{controls.js → load-assets.js} +23 -37
- package/build-module/store/load-assets.js.map +1 -0
- package/build-module/store/resolvers.js +14 -17
- package/build-module/store/resolvers.js.map +1 -1
- package/build-module/store/selectors.js +4 -8
- package/build-module/store/selectors.js.map +1 -1
- package/package.json +20 -21
- package/src/plugins/get-install-missing/install-button.js +4 -3
- package/src/store/actions.js +38 -46
- package/src/store/index.js +2 -4
- package/src/store/{controls.js → load-assets.js} +25 -42
- package/src/store/resolvers.js +17 -19
- package/src/store/selectors.js +2 -14
- package/src/store/test/actions.js +236 -267
- package/src/store/test/{controls.js → load-assets.js} +1 -1
- package/build/store/controls.js.map +0 -1
- package/build-module/store/controls.js.map +0 -1
package/src/store/actions.js
CHANGED
|
@@ -3,14 +3,13 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { store as blocksStore } from '@wordpress/blocks';
|
|
5
5
|
import { __, sprintf } from '@wordpress/i18n';
|
|
6
|
-
import
|
|
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 './
|
|
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
|
|
53
|
-
|
|
51
|
+
export const installBlockType = ( block ) => async ( {
|
|
52
|
+
registry,
|
|
53
|
+
dispatch,
|
|
54
|
+
} ) => {
|
|
55
|
+
const { id } = block;
|
|
54
56
|
let success = false;
|
|
55
|
-
|
|
57
|
+
dispatch.clearErrorNotice( id );
|
|
56
58
|
try {
|
|
57
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
-
|
|
80
|
+
dispatch.addInstalledBlockType( {
|
|
84
81
|
...block,
|
|
85
82
|
links: { ...block.links, ...links },
|
|
86
83
|
} );
|
|
87
84
|
|
|
88
|
-
|
|
89
|
-
const registeredBlocks =
|
|
90
|
-
blocksStore,
|
|
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
|
-
|
|
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
|
-
|
|
135
|
-
|
|
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
|
-
|
|
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
|
|
141
|
+
export const uninstallBlockType = ( block ) => async ( {
|
|
142
|
+
registry,
|
|
143
|
+
dispatch,
|
|
144
|
+
} ) => {
|
|
150
145
|
try {
|
|
151
|
-
|
|
152
|
-
|
|
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
|
-
|
|
159
|
-
url: getPluginUrl( block ),
|
|
152
|
+
await apiFetch( {
|
|
160
153
|
method: 'DELETE',
|
|
154
|
+
url,
|
|
161
155
|
} );
|
|
162
|
-
|
|
156
|
+
dispatch.removeInstalledBlockType( block );
|
|
163
157
|
} catch ( error ) {
|
|
164
|
-
|
|
165
|
-
noticesStore
|
|
166
|
-
|
|
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"
|
package/src/store/index.js
CHANGED
|
@@ -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(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
66
|
+
const data = await response.text();
|
|
79
67
|
|
|
80
|
-
|
|
68
|
+
const doc = new window.DOMParser().parseFromString( data, 'text/html' );
|
|
81
69
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
+
}
|
package/src/store/resolvers.js
CHANGED
|
@@ -6,31 +6,29 @@ import { camelCase, mapKeys } from 'lodash';
|
|
|
6
6
|
/**
|
|
7
7
|
* WordPress dependencies
|
|
8
8
|
*/
|
|
9
|
-
import
|
|
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
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
export const getDownloadableBlocks = ( filterValue ) => async ( {
|
|
17
|
+
dispatch,
|
|
18
|
+
} ) => {
|
|
19
|
+
if ( ! filterValue ) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
},
|
|
32
|
+
dispatch( receiveDownloadableBlocks( blocks, filterValue ) );
|
|
33
|
+
} catch {}
|
|
36
34
|
};
|
package/src/store/selectors.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
/**
|