Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin sorting via priority property in registerPlugin #16384

Open
wants to merge 30 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
bef6dab
Adds priority to context
Jul 1, 2019
f5ea38a
Adds priority property to the settings object with a default of 10. S…
Jul 2, 2019
c807047
Adds better data validation and new unit tests
Jul 2, 2019
a240241
using compose to sort the plugins
Jul 2, 2019
14a7ae0
Adds priority to context
Jul 1, 2019
a35a9d3
Adds priority property to the settings object with a default of 10. S…
Jul 2, 2019
0b9b238
Adds better data validation and new unit tests
Jul 2, 2019
247a1a2
using compose to sort the plugins
Jul 2, 2019
5769764
Merge remote-tracking branch 'upstream/master'
Oct 29, 2019
f308333
Merge branch 'master' into try/slot-fill-priority
Oct 29, 2019
6af0bfb
Use Number.isInteger() instead to check the priority
Oct 29, 2019
ed23ed7
Adds 10 as the default priority
Oct 29, 2019
b3c8d2d
Remove priority from plugin-sidebar implementation
Oct 29, 2019
4eddcfe
Adds priority to context
Jul 1, 2019
0950f3c
Adds priority property to the settings object with a default of 10. S…
Jul 2, 2019
b83663e
Adds better data validation and new unit tests
Jul 2, 2019
e015128
using compose to sort the plugins
Jul 2, 2019
f9c78f1
Adds priority to context
Jul 1, 2019
17836a9
Adds priority property to the settings object with a default of 10. S…
Jul 2, 2019
2348f0a
Adds better data validation and new unit tests
Jul 2, 2019
3010b05
using compose to sort the plugins
Jul 2, 2019
0b90315
Use Number.isInteger() instead to check the priority
Oct 29, 2019
5f8e764
Adds 10 as the default priority
Oct 29, 2019
4beed6b
Remove priority from plugin-sidebar implementation
Oct 29, 2019
9978d72
Merge branch 'try/slot-fill-priority' of github.com:ryanwelcher/guten…
Jan 7, 2020
4e0e0b7
Merge branch 'master' into try/slot-fill-priority
adamsilverstein Jan 14, 2020
127aea5
Merge branch 'trunk' into try/slot-fill-priority
May 31, 2021
baef056
Adding missed test file
May 31, 2021
fd3c9fb
Add correct sorting.
May 31, 2021
56c536b
Update docs.
May 31, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export default compose(
return {
icon: ownProps.icon || context.icon,
sidebarName: `${ context.name }/${ ownProps.name }`,
priority: ownProps.priority || context.priority,
ryanwelcher marked this conversation as resolved.
Show resolved Hide resolved
};
} ),
withSelect( ( select, { sidebarName } ) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function Component() {
registerPlugin( 'plugin-name', {
icon: 'smiley',
render: Component,
priority: 5
} );
```

Expand Down Expand Up @@ -138,6 +139,7 @@ const Component = () => (
registerPlugin( 'plugin-name', {
icon: 'smiley',
render: Component,
priority: 5
} );
```

Expand All @@ -147,6 +149,7 @@ _Parameters_
- _settings_ `Object`: The settings for this plugin.
- _settings.icon_ `(string|WPElement|Function)`: An icon to be shown in the UI. It can be a slug of the Dashicon, or an element (or function returning an element) if you choose to render your own SVG.
- _settings.render_ `Function`: A component containing the UI elements to be rendered.
- _settings.priority_ `number`: Allows for controlling the display order of this plugin. Default is 10.

_Returns_

Expand Down
11 changes: 11 additions & 0 deletions packages/plugins/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const plugins = {};
* @param {string|WPElement|Function} settings.icon An icon to be shown in the UI. It can be a slug of the Dashicon,
* or an element (or function returning an element) if you choose to render your own SVG.
* @param {Function} settings.render A component containing the UI elements to be rendered.
* @param {number} settings.priority Allows for controlling the display order of this plugin. Default is 10.
ryanwelcher marked this conversation as resolved.
Show resolved Hide resolved
*
* @example <caption>ES5</caption>
* ```js
Expand Down Expand Up @@ -59,6 +60,7 @@ const plugins = {};
* registerPlugin( 'plugin-name', {
* icon: 'smiley',
* render: Component,
* priority: 5
* } );
* ```
*
Expand Down Expand Up @@ -87,6 +89,7 @@ const plugins = {};
* registerPlugin( 'plugin-name', {
* icon: 'smiley',
* render: Component,
* priority: 5
* } );
* ```
*
Expand All @@ -105,6 +108,13 @@ export function registerPlugin( name, settings ) {
);
return null;
}
const { priority = 10 } = settings;
if ( priority !== 0 && ( priority.length === 0 || typeof priority !== 'number' ) ) {
ryanwelcher marked this conversation as resolved.
Show resolved Hide resolved
console.error(
'The "priority" property must be a number'
);
return null;
}
if ( ! /^[a-z][a-z0-9-]*$/.test( name ) ) {
console.error(
'Plugin names must include only lowercase alphanumeric characters or dashes, and start with a letter. Example: "my-plugin".'
Expand All @@ -129,6 +139,7 @@ export function registerPlugin( name, settings ) {
plugins[ name ] = {
name,
icon: 'admin-plugins',
priority,
...settings,
};

Expand Down
25 changes: 25 additions & 0 deletions packages/plugins/src/api/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe( 'registerPlugin', () => {
name,
render: Component,
icon,
priority: 10,
} );
} );

Expand Down Expand Up @@ -65,4 +66,28 @@ describe( 'registerPlugin', () => {
} );
expect( console ).toHaveErroredWith( 'Plugin "plugin" is already registered.' );
} );

it( 'fails to register a plugin with a priority set to an empty string', () => {
registerPlugin( 'priority-as-string', {
render: () => {},
priority: '',
} );
expect( console ).toHaveErroredWith( 'The "priority" property must be a number' );
} );

it( 'fails to register a plugin with a priority set to boolean true', () => {
registerPlugin( 'priority-as-string', {
render: () => {},
priority: true,
} );
expect( console ).toHaveErroredWith( 'The "priority" property must be a number' );
} );

it( 'fails to register a plugin with a priority set to boolean false', () => {
registerPlugin( 'priority-as-string', {
render: () => {},
priority: false,
} );
expect( console ).toHaveErroredWith( 'The "priority" property must be a number' );
} );
} );
13 changes: 10 additions & 3 deletions packages/plugins/src/components/plugin-area/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* External dependencies
*/
import { map } from 'lodash';
import { map, sortBy } from 'lodash';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { addAction, removeAction } from '@wordpress/hooks';
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -58,16 +59,22 @@ class PluginArea extends Component {
}

getCurrentPluginsState() {
return {
plugins: map( getPlugins(), ( { icon, name, render } ) => {
const plugins = compose(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to have a test which validates whether components are rendered in the order enforced by priorities.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds great! I'll get those in place ASAP.

( list ) => map( list, ( { icon, name, render, priority } ) => {
return {
Plugin: render,
context: {
name,
icon,
priority,
},
};
} ),
( list ) => sortBy( list, [ 'priority' ] ),
)( getPlugins() );

return {
plugins,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/plugins/src/components/plugin-context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createHigherOrderComponent } from '@wordpress/compose';
const { Consumer, Provider } = createContext( {
name: null,
icon: null,
priority: null,
ryanwelcher marked this conversation as resolved.
Show resolved Hide resolved
} );

export { Provider as PluginContextProvider };
Expand Down