@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
@@ -1,21 +1,60 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
+ import { createRegistry } from '@wordpress/data';
4
5
  import { store as blocksStore } from '@wordpress/blocks';
5
6
  import { store as noticesStore } from '@wordpress/notices';
7
+ import apiFetch from '@wordpress/api-fetch';
6
8
 
7
9
  /**
8
10
  * Internal dependencies
9
11
  */
10
- import { installBlockType, uninstallBlockType } from '../actions';
12
+ import { loadAssets } from '../load-assets';
13
+ import { store as blockDirectoryStore } from '..';
14
+
15
+ jest.mock( '@wordpress/api-fetch', () => ( {
16
+ __esModule: true,
17
+ default: jest.fn(),
18
+ } ) );
19
+
20
+ jest.mock( '../load-assets', () => ( {
21
+ loadAssets: jest.fn(),
22
+ } ) );
23
+
24
+ function createRegistryWithStores() {
25
+ // create a registry and register stores
26
+ const registry = createRegistry();
27
+
28
+ registry.register( blockDirectoryStore );
29
+ registry.register( noticesStore );
30
+ registry.register( blocksStore );
31
+
32
+ return registry;
33
+ }
34
+
35
+ // mock the `loadAssets` function. The real function would load the installed
36
+ // block's script assets, which in turn register the block. That registration
37
+ // call is the only thing we need to mock.
38
+ function loadAssetsMock( registry ) {
39
+ return async function () {
40
+ registry
41
+ .dispatch( blocksStore )
42
+ .addBlockTypes( [ { name: 'block/block' } ] );
43
+ };
44
+ }
45
+
46
+ function blockWithLinks( block, links ) {
47
+ return { ...block, links: { ...block.links, ...links } };
48
+ }
11
49
 
12
50
  describe( 'actions', () => {
13
51
  const pluginEndpoint =
14
- 'https://example.com/wp-json/wp/v2/plugins/block/block';
15
- const item = {
16
- id: 'block/block',
17
- name: 'Test Block',
18
- assets: [ 'script.js' ],
52
+ 'https://example.com/wp-json/wp/v2/plugins/block-block';
53
+
54
+ const block = {
55
+ id: 'block-block',
56
+ name: 'block/block',
57
+ title: 'Test Block',
19
58
  links: {
20
59
  'wp:install-plugin': [
21
60
  {
@@ -25,288 +64,218 @@ describe( 'actions', () => {
25
64
  ],
26
65
  },
27
66
  };
28
- const plugin = {
67
+
68
+ const pluginResponse = {
29
69
  plugin: 'block/block.php',
30
70
  status: 'active',
31
71
  name: 'Test Block',
32
72
  version: '1.0.0',
33
73
  _links: {
34
- self: [
35
- {
36
- href: pluginEndpoint,
37
- },
38
- ],
74
+ self: [ { href: pluginEndpoint } ],
39
75
  },
40
76
  };
41
77
 
42
78
  describe( 'installBlockType', () => {
43
- const block = item;
44
- it( 'should install a block successfully', () => {
45
- const generator = installBlockType( block );
46
-
47
- expect( generator.next().value ).toEqual( {
48
- type: 'CLEAR_ERROR_NOTICE',
49
- blockId: block.id,
50
- } );
51
-
52
- expect( generator.next().value ).toEqual( {
53
- type: 'SET_INSTALLING_BLOCK',
54
- blockId: block.id,
55
- isInstalling: true,
56
- } );
57
-
58
- expect( generator.next().value ).toMatchObject( {
59
- type: 'API_FETCH',
60
- request: {
61
- path: 'wp/v2/plugins',
62
- method: 'POST',
63
- },
64
- } );
65
-
66
- expect( generator.next( plugin ).value ).toEqual( {
67
- type: 'ADD_INSTALLED_BLOCK_TYPE',
68
- item: {
69
- ...block,
70
- links: {
71
- ...block.links,
72
- self: [
73
- {
74
- href: pluginEndpoint,
75
- },
76
- ],
77
- },
78
- },
79
- } );
80
-
81
- expect( generator.next().value ).toEqual( {
82
- type: 'LOAD_ASSETS',
83
- assets: block.assets,
84
- } );
85
-
86
- expect( generator.next().value ).toEqual( {
87
- args: [],
88
- selectorName: 'getBlockTypes',
89
- storeKey: blocksStore.name,
90
- type: '@@data/SELECT',
91
- } );
92
-
93
- expect( generator.next( [ block ] ).value ).toMatchObject( {
94
- type: '@@data/DISPATCH',
95
- actionName: 'createInfoNotice',
96
- storeKey: noticesStore,
97
- } );
98
-
99
- expect( generator.next().value ).toEqual( {
100
- type: 'SET_INSTALLING_BLOCK',
101
- blockId: block.id,
102
- isInstalling: false,
103
- } );
104
-
105
- expect( generator.next() ).toEqual( {
106
- value: true,
107
- done: true,
108
- } );
79
+ it( 'should install a block successfully', async () => {
80
+ const registry = createRegistryWithStores();
81
+
82
+ // mock the api-fetch and load-assets modules
83
+ apiFetch.mockImplementation( async ( { path } ) => {
84
+ switch ( path ) {
85
+ case 'wp/v2/plugins':
86
+ return pluginResponse;
87
+ default:
88
+ throw new Error( `unexpected API endpoint: ${ path }` );
89
+ }
90
+ } );
91
+
92
+ loadAssets.mockImplementation( loadAssetsMock( registry ) );
93
+
94
+ // install the block
95
+ await registry
96
+ .dispatch( blockDirectoryStore )
97
+ .installBlockType( block );
98
+
99
+ // check that blocks store contains the new block
100
+ const registeredBlock = registry
101
+ .select( blocksStore )
102
+ .getBlockType( 'block/block' );
103
+ expect( registeredBlock ).toBeTruthy();
104
+
105
+ // check that the block-directory store contains the new block, too
106
+ const installedBlockTypes = registry
107
+ .select( blockDirectoryStore )
108
+ .getInstalledBlockTypes();
109
+ expect( installedBlockTypes ).toMatchObject( [
110
+ { name: 'block/block' },
111
+ ] );
112
+
113
+ // check that notice was displayed
114
+ const notices = registry.select( noticesStore ).getNotices();
115
+ expect( notices ).toMatchObject( [
116
+ { content: 'Block Test Block installed and added.' },
117
+ ] );
109
118
  } );
110
119
 
111
- it( 'should activate an inactive block plugin successfully', () => {
112
- const inactiveBlock = {
113
- ...block,
114
- links: {
115
- ...block.links,
116
- 'wp:plugin': [
117
- {
118
- href: pluginEndpoint,
119
- },
120
- ],
121
- },
122
- };
123
- const generator = installBlockType( inactiveBlock );
124
-
125
- expect( generator.next().value ).toEqual( {
126
- type: 'CLEAR_ERROR_NOTICE',
127
- blockId: inactiveBlock.id,
128
- } );
129
-
130
- expect( generator.next().value ).toEqual( {
131
- type: 'SET_INSTALLING_BLOCK',
132
- blockId: inactiveBlock.id,
133
- isInstalling: true,
134
- } );
135
-
136
- expect( generator.next().value ).toMatchObject( {
137
- type: 'API_FETCH',
138
- request: {
139
- url: pluginEndpoint,
140
- method: 'PUT',
141
- },
142
- } );
143
-
144
- expect( generator.next( plugin ).value ).toEqual( {
145
- type: 'ADD_INSTALLED_BLOCK_TYPE',
146
- item: inactiveBlock,
147
- } );
148
-
149
- expect( generator.next().value ).toEqual( {
150
- type: 'LOAD_ASSETS',
151
- assets: inactiveBlock.assets,
152
- } );
153
-
154
- expect( generator.next().value ).toEqual( {
155
- args: [],
156
- selectorName: 'getBlockTypes',
157
- storeKey: blocksStore.name,
158
- type: '@@data/SELECT',
159
- } );
160
-
161
- expect( generator.next( [ inactiveBlock ] ).value ).toMatchObject( {
162
- type: '@@data/DISPATCH',
163
- actionName: 'createInfoNotice',
164
- storeKey: noticesStore,
165
- } );
166
-
167
- expect( generator.next().value ).toEqual( {
168
- type: 'SET_INSTALLING_BLOCK',
169
- blockId: inactiveBlock.id,
170
- isInstalling: false,
171
- } );
172
-
173
- expect( generator.next() ).toEqual( {
174
- value: true,
175
- done: true,
176
- } );
120
+ it( 'should activate an inactive block plugin successfully', async () => {
121
+ const registry = createRegistryWithStores();
122
+
123
+ // 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 }` );
131
+ }
132
+ } );
133
+
134
+ loadAssets.mockImplementation( loadAssetsMock( registry ) );
135
+
136
+ // install the block
137
+ await registry.dispatch( blockDirectoryStore ).installBlockType(
138
+ blockWithLinks( block, {
139
+ 'wp:plugin': [ { href: pluginEndpoint } ],
140
+ } )
141
+ );
142
+
143
+ // check that blocks store contains the new block
144
+ const registeredBlock = registry
145
+ .select( blocksStore )
146
+ .getBlockType( 'block/block' );
147
+ expect( registeredBlock ).toBeTruthy();
148
+
149
+ // check that notice was displayed
150
+ const notices = registry.select( noticesStore ).getNotices();
151
+ expect( notices ).toMatchObject( [
152
+ { content: 'Block Test Block installed and added.' },
153
+ ] );
177
154
  } );
178
155
 
179
- it( "should set an error if the plugin can't install", () => {
180
- const generator = installBlockType( block );
181
-
182
- expect( generator.next().value ).toEqual( {
183
- type: 'CLEAR_ERROR_NOTICE',
184
- blockId: block.id,
185
- } );
186
-
187
- expect( generator.next().value ).toEqual( {
188
- type: 'SET_INSTALLING_BLOCK',
189
- blockId: block.id,
190
- isInstalling: true,
191
- } );
192
-
193
- expect( generator.next().value ).toMatchObject( {
194
- type: 'API_FETCH',
195
- request: {
196
- path: 'wp/v2/plugins',
197
- method: 'POST',
198
- },
199
- } );
200
-
201
- const apiError = {
202
- code: 'plugins_api_failed',
203
- message: 'Plugin not found.',
204
- data: null,
205
- };
206
- expect( generator.throw( apiError ).value ).toMatchObject( {
207
- type: 'SET_ERROR_NOTICE',
208
- blockId: block.id,
209
- } );
210
-
211
- expect( generator.next().value ).toMatchObject( {
212
- type: '@@data/DISPATCH',
213
- actionName: 'createErrorNotice',
214
- storeKey: noticesStore,
215
- } );
216
-
217
- expect( generator.next().value ).toEqual( {
218
- type: 'SET_INSTALLING_BLOCK',
219
- blockId: block.id,
220
- isInstalling: false,
221
- } );
222
-
223
- expect( generator.next() ).toEqual( {
224
- value: false,
225
- done: true,
226
- } );
156
+ it( "should set an error if the plugin can't install", async () => {
157
+ const registry = createRegistryWithStores();
158
+
159
+ // mock the api-fetch and load-assets modules
160
+ apiFetch.mockImplementation( async ( { path } ) => {
161
+ switch ( path ) {
162
+ case 'wp/v2/plugins':
163
+ throw {
164
+ code: 'plugins_api_failed',
165
+ message: 'Plugin not found.',
166
+ data: null,
167
+ };
168
+ default:
169
+ throw new Error( `unexpected API endpoint: ${ path }` );
170
+ }
171
+ } );
172
+
173
+ loadAssets.mockImplementation( loadAssetsMock( registry ) );
174
+
175
+ // install the block
176
+ await registry
177
+ .dispatch( blockDirectoryStore )
178
+ .installBlockType( block );
179
+
180
+ // check that blocks store doesn't contain the new block
181
+ const registeredBlock = registry
182
+ .select( blocksStore )
183
+ .getBlockType( 'block/block' );
184
+ expect( registeredBlock ).toBeUndefined();
185
+
186
+ // check that error notice was displayed
187
+ const notices = registry.select( noticesStore ).getNotices();
188
+ expect( notices ).toMatchObject( [
189
+ { content: 'Plugin not found.' },
190
+ ] );
227
191
  } );
228
192
  } );
229
193
 
230
194
  describe( 'uninstallBlockType', () => {
231
- const block = {
232
- ...item,
233
- links: {
234
- ...item.links,
235
- self: [
236
- {
237
- href: pluginEndpoint,
238
- },
239
- ],
240
- },
241
- };
242
-
243
- it( 'should uninstall a block successfully', () => {
244
- const generator = uninstallBlockType( block );
245
-
246
- // First the deactivation step
247
- expect( generator.next().value ).toMatchObject( {
248
- type: 'API_FETCH',
249
- request: {
250
- url: pluginEndpoint,
251
- method: 'PUT',
252
- },
253
- } );
254
-
255
- // Then the deletion step
256
- expect( generator.next().value ).toMatchObject( {
257
- type: 'API_FETCH',
258
- request: {
259
- url: pluginEndpoint,
260
- method: 'DELETE',
261
- },
262
- } );
263
-
264
- expect( generator.next().value ).toEqual( {
265
- type: 'REMOVE_INSTALLED_BLOCK_TYPE',
266
- item: block,
267
- } );
268
-
269
- expect( generator.next() ).toEqual( {
270
- value: undefined,
271
- done: true,
272
- } );
195
+ const installedBlock = blockWithLinks( block, {
196
+ self: [ { href: pluginEndpoint } ],
273
197
  } );
274
198
 
275
- it( "should set a global notice if the plugin can't be deleted", () => {
276
- const generator = uninstallBlockType( block );
277
-
278
- expect( generator.next().value ).toMatchObject( {
279
- type: 'API_FETCH',
280
- request: {
281
- url: pluginEndpoint,
282
- method: 'PUT',
283
- },
284
- } );
199
+ it( 'should uninstall a block successfully', async () => {
200
+ const registry = createRegistryWithStores();
201
+
202
+ apiFetch.mockImplementation( async ( { url, method } ) => {
203
+ switch ( url ) {
204
+ case pluginEndpoint:
205
+ switch ( method ) {
206
+ case 'PUT':
207
+ case 'DELETE':
208
+ return;
209
+ default:
210
+ throw new Error(
211
+ `unexpected API endpoint method: ${ method }`
212
+ );
213
+ }
214
+ default:
215
+ throw new Error( `unexpected API endpoint: ${ url }` );
216
+ }
217
+ } );
218
+
219
+ // add installed block type that we're going to uninstall
220
+ registry
221
+ .dispatch( blockDirectoryStore )
222
+ .addInstalledBlockType( installedBlock );
223
+
224
+ // uninstall the block
225
+ await registry
226
+ .dispatch( blockDirectoryStore )
227
+ .uninstallBlockType( installedBlock );
228
+
229
+ // check that no error notice was displayed
230
+ const notices = registry.select( noticesStore ).getNotices();
231
+ expect( notices ).toEqual( [] );
232
+
233
+ // verify that the block was uninstalled
234
+ const installedBlockTypes = registry
235
+ .select( blockDirectoryStore )
236
+ .getInstalledBlockTypes();
237
+ expect( installedBlockTypes ).toEqual( [] );
238
+ } );
285
239
 
286
- expect( generator.next().value ).toMatchObject( {
287
- type: 'API_FETCH',
288
- request: {
289
- url: pluginEndpoint,
290
- method: 'DELETE',
240
+ it( "should set a global notice if the plugin can't be deleted", async () => {
241
+ const registry = createRegistryWithStores();
242
+
243
+ apiFetch.mockImplementation( async ( { url, method } ) => {
244
+ switch ( url ) {
245
+ case pluginEndpoint:
246
+ switch ( method ) {
247
+ case 'PUT':
248
+ return;
249
+ case 'DELETE':
250
+ throw {
251
+ code: 'rest_cannot_delete_active_plugin',
252
+ message:
253
+ 'Cannot delete an active plugin. Please deactivate it first.',
254
+ data: null,
255
+ };
256
+ default:
257
+ throw new Error(
258
+ `unexpected API endpoint method: ${ method }`
259
+ );
260
+ }
261
+ default:
262
+ throw new Error( `unexpected API endpoint: ${ url }` );
263
+ }
264
+ } );
265
+
266
+ // uninstall the block
267
+ await registry
268
+ .dispatch( blockDirectoryStore )
269
+ .uninstallBlockType( installedBlock );
270
+
271
+ // check that error notice was displayed
272
+ const notices = registry.select( noticesStore ).getNotices();
273
+ expect( notices ).toMatchObject( [
274
+ {
275
+ content:
276
+ 'Cannot delete an active plugin. Please deactivate it first.',
291
277
  },
292
- } );
293
-
294
- const apiError = {
295
- code: 'rest_cannot_delete_active_plugin',
296
- message:
297
- 'Cannot delete an active plugin. Please deactivate it first.',
298
- data: null,
299
- };
300
- expect( generator.throw( apiError ).value ).toMatchObject( {
301
- type: '@@data/DISPATCH',
302
- actionName: 'createErrorNotice',
303
- storeKey: noticesStore,
304
- } );
305
-
306
- expect( generator.next() ).toEqual( {
307
- value: undefined,
308
- done: true,
309
- } );
278
+ ] );
310
279
  } );
311
280
  } );
312
281
  } );
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
- import { loadAsset } from '../controls';
4
+ import { loadAsset } from '../load-assets';
5
5
 
6
6
  describe( 'controls', () => {
7
7
  describe( 'loadAsset', () => {
@@ -1 +0,0 @@
1
- {"version":3,"sources":["@wordpress/block-directory/src/store/controls.js"],"names":["loadAsset","el","Promise","resolve","reject","newNode","document","createElement","nodeName","forEach","attr","innerHTML","appendChild","createTextNode","onload","onerror","Error","body","toLowerCase","src","loadAssets","assets","type","controls","LOAD_ASSETS","response","url","location","href","parse","data","text","doc","window","DOMParser","parseFromString","newAssets","Array","from","querySelectorAll","filter","asset","id","getElementById","newAsset"],"mappings":";;;;;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,SAAS,GAAKC,EAAF,IAAU;AAClC,SAAO,IAAIC,OAAJ,CAAa,CAAEC,OAAF,EAAWC,MAAX,KAAuB;AAC1C;AACF;AACA;AACA;AACE,UAAMC,OAAO,GAAGC,QAAQ,CAACC,aAAT,CAAwBN,EAAE,CAACO,QAA3B,CAAhB;AAEA,KAAE,IAAF,EAAQ,KAAR,EAAe,KAAf,EAAsB,MAAtB,EAA8B,MAA9B,EAAuCC,OAAvC,CAAkDC,IAAF,IAAY;AAC3D,UAAKT,EAAE,CAAES,IAAF,CAAP,EAAkB;AACjBL,QAAAA,OAAO,CAAEK,IAAF,CAAP,GAAkBT,EAAE,CAAES,IAAF,CAApB;AACA;AACD,KAJD,EAP0C,CAa1C;;AACA,QAAKT,EAAE,CAACU,SAAR,EAAoB;AACnBN,MAAAA,OAAO,CAACO,WAAR,CAAqBN,QAAQ,CAACO,cAAT,CAAyBZ,EAAE,CAACU,SAA5B,CAArB;AACA;;AAEDN,IAAAA,OAAO,CAACS,MAAR,GAAiB,MAAMX,OAAO,CAAE,IAAF,CAA9B;;AACAE,IAAAA,OAAO,CAACU,OAAR,GAAkB,MAAMX,MAAM,CAAE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AjB;eAkCerB,Q","sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Load an asset for a block.\n *\n * This function returns a Promise that will resolve once the asset is loaded,\n * or in the case of Stylesheets and Inline JavaScript, will resolve immediately.\n *\n * @param {HTMLElement} el A HTML Element asset to inject.\n *\n * @return {Promise} Promise which will resolve when the asset is loaded.\n */\nexport const loadAsset = ( el ) => {\n\treturn new Promise( ( resolve, reject ) => {\n\t\t/*\n\t\t * Reconstruct the passed element, this is required as inserting the Node directly\n\t\t * won't always fire the required onload events, even if the asset wasn't already loaded.\n\t\t */\n\t\tconst newNode = document.createElement( el.nodeName );\n\n\t\t[ 'id', 'rel', 'src', 'href', 'type' ].forEach( ( attr ) => {\n\t\t\tif ( el[ attr ] ) {\n\t\t\t\tnewNode[ attr ] = el[ attr ];\n\t\t\t}\n\t\t} );\n\n\t\t// Append inline <script> contents.\n\t\tif ( el.innerHTML ) {\n\t\t\tnewNode.appendChild( document.createTextNode( el.innerHTML ) );\n\t\t}\n\n\t\tnewNode.onload = () => resolve( true );\n\t\tnewNode.onerror = () => reject( new Error( 'Error loading asset.' ) );\n\n\t\tdocument.body.appendChild( newNode );\n\n\t\t// Resolve Stylesheets and Inline JavaScript immediately.\n\t\tif (\n\t\t\t'link' === newNode.nodeName.toLowerCase() ||\n\t\t\t( 'script' === newNode.nodeName.toLowerCase() && ! newNode.src )\n\t\t) {\n\t\t\tresolve();\n\t\t}\n\t} );\n};\n\n/**\n * Load the asset files for a block\n *\n * @param {Array} assets A collection of URLs for the assets.\n *\n * @return {Object} Control descriptor.\n */\nexport function loadAssets( assets ) {\n\treturn {\n\t\ttype: 'LOAD_ASSETS',\n\t\tassets,\n\t};\n}\n\nconst controls = {\n\tasync LOAD_ASSETS() {\n\t\t/*\n\t\t * Fetch the current URL (post-new.php, or post.php?post=1&action=edit) and compare the\n\t\t * JavaScript and CSS assets loaded between the pages. This imports the required assets\n\t\t * for the block into the current page while not requiring that we know them up-front.\n\t\t * In the future this can be improved by reliance upon block.json and/or a script-loader\n\t\t * dependency API.\n\t\t */\n\t\tconst response = await apiFetch( {\n\t\t\turl: document.location.href,\n\t\t\tparse: false,\n\t\t} );\n\n\t\tconst data = await response.text();\n\n\t\tconst doc = new window.DOMParser().parseFromString( data, 'text/html' );\n\n\t\tconst newAssets = Array.from(\n\t\t\tdoc.querySelectorAll( 'link[rel=\"stylesheet\"],script' )\n\t\t).filter(\n\t\t\t( asset ) => asset.id && ! document.getElementById( asset.id )\n\t\t);\n\n\t\t/*\n\t\t * Load each asset in order, as they may depend upon an earlier loaded script.\n\t\t * Stylesheets and Inline Scripts will resolve immediately upon insertion.\n\t\t */\n\t\tfor ( const newAsset of newAssets ) {\n\t\t\tawait loadAsset( newAsset );\n\t\t}\n\t},\n};\n\nexport default controls;\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["@wordpress/block-directory/src/store/controls.js"],"names":["apiFetch","loadAsset","el","Promise","resolve","reject","newNode","document","createElement","nodeName","forEach","attr","innerHTML","appendChild","createTextNode","onload","onerror","Error","body","toLowerCase","src","loadAssets","assets","type","controls","LOAD_ASSETS","response","url","location","href","parse","data","text","doc","window","DOMParser","parseFromString","newAssets","Array","from","querySelectorAll","filter","asset","id","getElementById","newAsset"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,QAAP,MAAqB,sBAArB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,SAAS,GAAKC,EAAF,IAAU;AAClC,SAAO,IAAIC,OAAJ,CAAa,CAAEC,OAAF,EAAWC,MAAX,KAAuB;AAC1C;AACF;AACA;AACA;AACE,UAAMC,OAAO,GAAGC,QAAQ,CAACC,aAAT,CAAwBN,EAAE,CAACO,QAA3B,CAAhB;AAEA,KAAE,IAAF,EAAQ,KAAR,EAAe,KAAf,EAAsB,MAAtB,EAA8B,MAA9B,EAAuCC,OAAvC,CAAkDC,IAAF,IAAY;AAC3D,U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,CAACC,SAAX,GAAuBC,eAAvB,CAAwCL,IAAxC,EAA8C,WAA9C,CAAZ;AAEA,UAAMM,SAAS,GAAGC,KAAK,CAACC,IAAN,CACjBN,GAAG,CAACO,gBAAJ,CAAsB,+BAAtB,CADiB,EAEhBC,MAFgB,CAGfC,KAAF,IAAaA,KAAK,CAACC,EAAN,IAAY,CAAEpC,QAAQ,CAACqC,cAAT,CAAyBF,KAAK,CAACC,EAA/B,CAHV,CAAlB;AAMA;AACF;AACA;AACA;;AACE,SAAM,MAAME,QAAZ,IAAwBR,SAAxB,EAAoC;AACnC,YAAMpC,SAAS,CAAE4C,QAAF,CAAf;AACA;AACD;;AA/Be,CAAjB;AAkCA,eAAerB,QAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Load an asset for a block.\n *\n * This function returns a Promise that will resolve once the asset is loaded,\n * or in the case of Stylesheets and Inline JavaScript, will resolve immediately.\n *\n * @param {HTMLElement} el A HTML Element asset to inject.\n *\n * @return {Promise} Promise which will resolve when the asset is loaded.\n */\nexport const loadAsset = ( el ) => {\n\treturn new Promise( ( resolve, reject ) => {\n\t\t/*\n\t\t * Reconstruct the passed element, this is required as inserting the Node directly\n\t\t * won't always fire the required onload events, even if the asset wasn't already loaded.\n\t\t */\n\t\tconst newNode = document.createElement( el.nodeName );\n\n\t\t[ 'id', 'rel', 'src', 'href', 'type' ].forEach( ( attr ) => {\n\t\t\tif ( el[ attr ] ) {\n\t\t\t\tnewNode[ attr ] = el[ attr ];\n\t\t\t}\n\t\t} );\n\n\t\t// Append inline <script> contents.\n\t\tif ( el.innerHTML ) {\n\t\t\tnewNode.appendChild( document.createTextNode( el.innerHTML ) );\n\t\t}\n\n\t\tnewNode.onload = () => resolve( true );\n\t\tnewNode.onerror = () => reject( new Error( 'Error loading asset.' ) );\n\n\t\tdocument.body.appendChild( newNode );\n\n\t\t// Resolve Stylesheets and Inline JavaScript immediately.\n\t\tif (\n\t\t\t'link' === newNode.nodeName.toLowerCase() ||\n\t\t\t( 'script' === newNode.nodeName.toLowerCase() && ! newNode.src )\n\t\t) {\n\t\t\tresolve();\n\t\t}\n\t} );\n};\n\n/**\n * Load the asset files for a block\n *\n * @param {Array} assets A collection of URLs for the assets.\n *\n * @return {Object} Control descriptor.\n */\nexport function loadAssets( assets ) {\n\treturn {\n\t\ttype: 'LOAD_ASSETS',\n\t\tassets,\n\t};\n}\n\nconst controls = {\n\tasync LOAD_ASSETS() {\n\t\t/*\n\t\t * Fetch the current URL (post-new.php, or post.php?post=1&action=edit) and compare the\n\t\t * JavaScript and CSS assets loaded between the pages. This imports the required assets\n\t\t * for the block into the current page while not requiring that we know them up-front.\n\t\t * In the future this can be improved by reliance upon block.json and/or a script-loader\n\t\t * dependency API.\n\t\t */\n\t\tconst response = await apiFetch( {\n\t\t\turl: document.location.href,\n\t\t\tparse: false,\n\t\t} );\n\n\t\tconst data = await response.text();\n\n\t\tconst doc = new window.DOMParser().parseFromString( data, 'text/html' );\n\n\t\tconst newAssets = Array.from(\n\t\t\tdoc.querySelectorAll( 'link[rel=\"stylesheet\"],script' )\n\t\t).filter(\n\t\t\t( asset ) => asset.id && ! document.getElementById( asset.id )\n\t\t);\n\n\t\t/*\n\t\t * Load each asset in order, as they may depend upon an earlier loaded script.\n\t\t * Stylesheets and Inline Scripts will resolve immediately upon insertion.\n\t\t */\n\t\tfor ( const newAsset of newAssets ) {\n\t\t\tawait loadAsset( newAsset );\n\t\t}\n\t},\n};\n\nexport default controls;\n"]}