Skip to content

Commit 3f47cbc

Browse files
authored
Block Directory: Add developer docs to readme (#25591)
1 parent 78dde89 commit 3f47cbc

File tree

3 files changed

+261
-19
lines changed

3 files changed

+261
-19
lines changed

packages/block-directory/README.md

+240
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,244 @@ npm install @wordpress/block-directory --save
1414

1515
_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using [core-js](https://github.com/zloirock/core-js) or [@babel/polyfill](https://babeljs.io/docs/en/next/babel-polyfill) will add support for these methods. Learn more about it in [Babel docs](https://babeljs.io/docs/en/next/caveats)._
1616

17+
## Usage
18+
19+
This package builds a standalone JS file. When loaded on a page with the block editor, it extends the block inserter to search for blocks from WordPress.org.
20+
21+
To do this, it uses the `__experimentalInserterMenuExtension`, a slot-fill area hooked into the block types list. When the user runs a search and there are no results currently installed, it fires off a request to WordPress.org for matching blocks. These are listed for the user to install with a one-click process that [installs, activates, and injects the block into the post.](./src/store/actions.js#L49) When the post is saved, if the block was not used, it will be [silently uninstalled](./src/store/actions.js#L129) to avoid clutter.
22+
23+
See also the API endpoints for searching WordPress.org: `/wp/v2/block-directory/search`, and installing & activating plugins: `/wp/v2/plugins/`.
24+
25+
## Actions
26+
27+
The following set of dispatching action creators are available on the object returned by `wp.data.dispatch( 'core/block-directory' )`:
28+
29+
<!-- START TOKEN(Autogenerated actions|src/store/actions.js) -->
30+
31+
<a name="addInstalledBlockType" href="#addInstalledBlockType">#</a> **addInstalledBlockType**
32+
33+
Returns an action object used to add a block type to the "newly installed"
34+
tracking list.
35+
36+
_Parameters_
37+
38+
- _item_ `Object`: The block item with the block id and name.
39+
40+
_Returns_
41+
42+
- `Object`: Action object.
43+
44+
<a name="clearErrorNotice" href="#clearErrorNotice">#</a> **clearErrorNotice**
45+
46+
Sets the error notice to empty for specific block.
47+
48+
_Parameters_
49+
50+
- _blockId_ `string`: The ID of the block plugin. eg: my-block
51+
52+
_Returns_
53+
54+
- `Object`: Action object.
55+
56+
<a name="fetchDownloadableBlocks" href="#fetchDownloadableBlocks">#</a> **fetchDownloadableBlocks**
57+
58+
Returns an action object used in signalling that the downloadable blocks
59+
have been requested and are loading.
60+
61+
_Parameters_
62+
63+
- _filterValue_ `string`: Search string.
64+
65+
_Returns_
66+
67+
- `Object`: Action object.
68+
69+
<a name="installBlockType" href="#installBlockType">#</a> **installBlockType**
70+
71+
Action triggered to install a block plugin.
72+
73+
_Parameters_
74+
75+
- _block_ `Object`: The block item returned by search.
76+
77+
_Returns_
78+
79+
- `boolean`: Whether the block was successfully installed & loaded.
80+
81+
<a name="receiveDownloadableBlocks" href="#receiveDownloadableBlocks">#</a> **receiveDownloadableBlocks**
82+
83+
Returns an action object used in signalling that the downloadable blocks
84+
have been updated.
85+
86+
_Parameters_
87+
88+
- _downloadableBlocks_ `Array`: Downloadable blocks.
89+
- _filterValue_ `string`: Search string.
90+
91+
_Returns_
92+
93+
- `Object`: Action object.
94+
95+
<a name="removeInstalledBlockType" href="#removeInstalledBlockType">#</a> **removeInstalledBlockType**
96+
97+
Returns an action object used to remove a block type from the "newly installed"
98+
tracking list.
99+
100+
_Parameters_
101+
102+
- _item_ `string`: The block item with the block id and name.
103+
104+
_Returns_
105+
106+
- `Object`: Action object.
107+
108+
<a name="setErrorNotice" href="#setErrorNotice">#</a> **setErrorNotice**
109+
110+
Sets an error notice to be displayed to the user for a given block.
111+
112+
_Parameters_
113+
114+
- _blockId_ `string`: The ID of the block plugin. eg: my-block
115+
- _message_ `string`: The message shown in the notice.
116+
- _isFatal_ `boolean`: Whether the user can recover from the error.
117+
118+
_Returns_
119+
120+
- `Object`: Action object.
121+
122+
<a name="setIsInstalling" href="#setIsInstalling">#</a> **setIsInstalling**
123+
124+
Returns an action object used to indicate install in progress.
125+
126+
_Parameters_
127+
128+
- _blockId_ `string`:
129+
- _isInstalling_ `boolean`:
130+
131+
_Returns_
132+
133+
- `Object`: Action object.
134+
135+
<a name="uninstallBlockType" href="#uninstallBlockType">#</a> **uninstallBlockType**
136+
137+
Action triggered to uninstall a block plugin.
138+
139+
_Parameters_
140+
141+
- _block_ `Object`: The blockType object.
142+
143+
<!-- END TOKEN(Autogenerated actions|src/store/actions.js) -->
144+
145+
## Selectors
146+
147+
The following selectors are available on the object returned by `wp.data.select( 'core/block-directory' )`:
148+
149+
<!-- START TOKEN(Autogenerated selectors|src/store/selectors.js) -->
150+
151+
<a name="getDownloadableBlocks" href="#getDownloadableBlocks">#</a> **getDownloadableBlocks**
152+
153+
Returns the available uninstalled blocks.
154+
155+
_Parameters_
156+
157+
- _state_ `Object`: Global application state.
158+
- _filterValue_ `string`: Search string.
159+
160+
_Returns_
161+
162+
- `Array`: Downloadable blocks.
163+
164+
<a name="getErrorNoticeForBlock" href="#getErrorNoticeForBlock">#</a> **getErrorNoticeForBlock**
165+
166+
Returns the error notice for a given block.
167+
168+
_Parameters_
169+
170+
- _state_ `Object`: Global application state.
171+
- _blockId_ `string`: The ID of the block plugin. eg: my-block
172+
173+
_Returns_
174+
175+
- `(string|boolean)`: The error text, or false if no error.
176+
177+
<a name="getErrorNotices" href="#getErrorNotices">#</a> **getErrorNotices**
178+
179+
Returns all block error notices.
180+
181+
_Parameters_
182+
183+
- _state_ `Object`: Global application state.
184+
185+
_Returns_
186+
187+
- `Object`: Object with error notices.
188+
189+
<a name="getInstalledBlockTypes" href="#getInstalledBlockTypes">#</a> **getInstalledBlockTypes**
190+
191+
Returns the block types that have been installed on the server in this
192+
session.
193+
194+
_Parameters_
195+
196+
- _state_ `Object`: Global application state.
197+
198+
_Returns_
199+
200+
- `Array`: Block type items
201+
202+
<a name="getNewBlockTypes" href="#getNewBlockTypes">#</a> **getNewBlockTypes**
203+
204+
Returns block types that have been installed on the server and used in the
205+
current post.
206+
207+
_Parameters_
208+
209+
- _state_ `Object`: Global application state.
210+
211+
_Returns_
212+
213+
- `Array`: Block type items.
214+
215+
<a name="getUnusedBlockTypes" href="#getUnusedBlockTypes">#</a> **getUnusedBlockTypes**
216+
217+
Returns the block types that have been installed on the server but are not
218+
used in the current post.
219+
220+
_Parameters_
221+
222+
- _state_ `Object`: Global application state.
223+
224+
_Returns_
225+
226+
- `Array`: Block type items.
227+
228+
<a name="isInstalling" href="#isInstalling">#</a> **isInstalling**
229+
230+
Returns true if a block plugin install is in progress.
231+
232+
_Parameters_
233+
234+
- _state_ `Object`: Global application state.
235+
- _blockId_ `string`: Id of the block.
236+
237+
_Returns_
238+
239+
- `boolean`: Whether this block is currently being installed.
240+
241+
<a name="isRequestingDownloadableBlocks" href="#isRequestingDownloadableBlocks">#</a> **isRequestingDownloadableBlocks**
242+
243+
Returns true if application is requesting for downloadable blocks.
244+
245+
_Parameters_
246+
247+
- _state_ `Object`: Global application state.
248+
- _filterValue_ `string`: Search string.
249+
250+
_Returns_
251+
252+
- `boolean`: Whether a request is in progress for the blocks list.
253+
254+
255+
<!-- END TOKEN(Autogenerated selectors|src/store/selectors.js) -->
256+
17257
<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>

packages/block-directory/src/store/actions.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import getPluginUrl from './utils/get-plugin-url';
1212

1313
/**
1414
* Returns an action object used in signalling that the downloadable blocks
15-
* have been requested and is loading.
15+
* have been requested and are loading.
1616
*
1717
* @param {string} filterValue Search string.
1818
*
@@ -150,7 +150,8 @@ export function* uninstallBlockType( block ) {
150150
}
151151

152152
/**
153-
* Returns an action object used to add a newly installed block type.
153+
* Returns an action object used to add a block type to the "newly installed"
154+
* tracking list.
154155
*
155156
* @param {Object} item The block item with the block id and name.
156157
*
@@ -164,7 +165,8 @@ export function addInstalledBlockType( item ) {
164165
}
165166

166167
/**
167-
* Returns an action object used to remove a newly installed block type.
168+
* Returns an action object used to remove a block type from the "newly installed"
169+
* tracking list.
168170
*
169171
* @param {string} item The block item with the block id and name.
170172
*
@@ -178,7 +180,7 @@ export function removeInstalledBlockType( item ) {
178180
}
179181

180182
/**
181-
* Returns an action object used to indicate install in progress
183+
* Returns an action object used to indicate install in progress.
182184
*
183185
* @param {string} blockId
184186
* @param {boolean} isInstalling
@@ -194,11 +196,11 @@ export function setIsInstalling( blockId, isInstalling ) {
194196
}
195197

196198
/**
197-
* Sets an error notice string to be displayed to the user
199+
* Sets an error notice to be displayed to the user for a given block.
198200
*
199-
* @param {string} blockId The ID of the block plugin. eg: my-block
201+
* @param {string} blockId The ID of the block plugin. eg: my-block
200202
* @param {string} message The message shown in the notice.
201-
* @param {boolean} isFatal Whether the user can recover from the error
203+
* @param {boolean} isFatal Whether the user can recover from the error.
202204
*
203205
* @return {Object} Action object.
204206
*/
@@ -212,7 +214,7 @@ export function setErrorNotice( blockId, message, isFatal = false ) {
212214
}
213215

214216
/**
215-
* Sets the error notice to empty for specific block
217+
* Sets the error notice to empty for specific block.
216218
*
217219
* @param {string} blockId The ID of the block plugin. eg: my-block
218220
*

packages/block-directory/src/store/selectors.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import hasBlockType from './utils/has-block-type';
1111
/**
1212
* Returns true if application is requesting for downloadable blocks.
1313
*
14-
* @param {Object} state Global application state.
14+
* @param {Object} state Global application state.
1515
* @param {string} filterValue Search string.
1616
*
17-
*
18-
* @return {Array} Downloadable blocks
17+
* @return {boolean} Whether a request is in progress for the blocks list.
1918
*/
2019
export function isRequestingDownloadableBlocks( state, filterValue ) {
2120
if (
@@ -28,12 +27,12 @@ export function isRequestingDownloadableBlocks( state, filterValue ) {
2827
}
2928

3029
/**
31-
* Returns the available uninstalled blocks
30+
* Returns the available uninstalled blocks.
3231
*
3332
* @param {Object} state Global application state.
3433
* @param {string} filterValue Search string.
3534
*
36-
* @return {Array} Downloadable blocks
35+
* @return {Array} Downloadable blocks.
3736
*/
3837
export function getDownloadableBlocks( state, filterValue ) {
3938
if (
@@ -46,11 +45,12 @@ export function getDownloadableBlocks( state, filterValue ) {
4645
}
4746

4847
/**
49-
* Returns the block types that have been installed on the server.
48+
* Returns the block types that have been installed on the server in this
49+
* session.
5050
*
5151
* @param {Object} state Global application state.
5252
*
53-
* @return {Array} Block type items.
53+
* @return {Array} Block type items
5454
*/
5555
export function getInstalledBlockTypes( state ) {
5656
return state.blockManagement.installedBlockTypes;
@@ -95,19 +95,19 @@ export const getUnusedBlockTypes = createRegistrySelector(
9595
);
9696

9797
/**
98-
* Returns true if application is calling install endpoint.
98+
* Returns true if a block plugin install is in progress.
9999
*
100-
* @param {Object} state Global application state.
100+
* @param {Object} state Global application state.
101101
* @param {string} blockId Id of the block.
102102
*
103-
* @return {boolean} Whether its currently installing
103+
* @return {boolean} Whether this block is currently being installed.
104104
*/
105105
export function isInstalling( state, blockId ) {
106106
return state.blockManagement.isInstalling[ blockId ] || false;
107107
}
108108

109109
/**
110-
* Returns the error notices
110+
* Returns all block error notices.
111111
*
112112
* @param {Object} state Global application state.
113113
*

0 commit comments

Comments
 (0)