diff --git a/blocks/api/registration.js b/blocks/api/registration.js index 3bc071809f9c90..754a497c968d8e 100644 --- a/blocks/api/registration.js +++ b/blocks/api/registration.js @@ -9,6 +9,7 @@ import { get, isFunction, some } from 'lodash'; * WordPress dependencies */ import { applyFilters } from '@wordpress/hooks'; +import { validateNamespacedId } from '@wordpress/utils'; /** * Internal dependencies @@ -94,16 +95,7 @@ export function registerBlockType( name, settings ) { ...settings, }; - if ( typeof name !== 'string' ) { - console.error( - 'Block names must be strings.' - ); - return; - } - if ( ! /^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/.test( name ) ) { - console.error( - 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' - ); + if ( ! validateNamespacedId( name, 'Block names' ) ) { return; } if ( blocks[ name ] ) { diff --git a/blocks/api/test/registration.js b/blocks/api/test/registration.js index af9378ca61aa94..de0fd76bb309d5 100644 --- a/blocks/api/test/registration.js +++ b/blocks/api/test/registration.js @@ -51,31 +51,31 @@ describe( 'blocks', () => { it( 'should reject blocks without a namespace', () => { const block = registerBlockType( 'doing-it-wrong' ); - expect( console ).toHaveErroredWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' ); + expect( console ).toHaveErroredWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-id' ); expect( block ).toBeUndefined(); } ); it( 'should reject blocks with too many namespaces', () => { const block = registerBlockType( 'doing/it/wrong' ); - expect( console ).toHaveErroredWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' ); + expect( console ).toHaveErroredWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-id' ); expect( block ).toBeUndefined(); } ); it( 'should reject blocks with invalid characters', () => { const block = registerBlockType( 'still/_doing_it_wrong' ); - expect( console ).toHaveErroredWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' ); + expect( console ).toHaveErroredWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-id' ); expect( block ).toBeUndefined(); } ); it( 'should reject blocks with uppercase characters', () => { const block = registerBlockType( 'Core/Paragraph' ); - expect( console ).toHaveErroredWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' ); + expect( console ).toHaveErroredWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-id' ); expect( block ).toBeUndefined(); } ); it( 'should reject blocks not starting with a letter', () => { const block = registerBlockType( 'my-plugin/4-fancy-block', defaultBlockSettings ); - expect( console ).toHaveErroredWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' ); + expect( console ).toHaveErroredWith( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-id' ); expect( block ).toBeUndefined(); } ); diff --git a/edit-post/api/index.js b/edit-post/api/index.js index 1252f8010ec3f4..3015d9c3810229 100644 --- a/edit-post/api/index.js +++ b/edit-post/api/index.js @@ -1,3 +1,6 @@ +export { + registerMoreMenuItem as __experimentalRegisterMoreMenuItem, +} from './more-menu-item'; export { registerSidebar, activateSidebar, diff --git a/edit-post/api/more-menu-item.js b/edit-post/api/more-menu-item.js new file mode 100644 index 00000000000000..55af8f59e1b250 --- /dev/null +++ b/edit-post/api/more-menu-item.js @@ -0,0 +1,91 @@ +/* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */ + +/** + * Wordpress dependencies + */ +import { applyFilters } from '@wordpress/hooks'; +import { validateNamespacedId } from '@wordpress/utils'; + +/** + * Internal dependencies + */ +import { isString } from 'lodash'; +import { activateSidebar } from './sidebar'; + +const menuItems = {}; + +/** + * Registers a plugin under the more menu. + * + * @param {string} menuItemId The unique identifier of the plugin. Should be in + * `[namespace]/[name]` format. + * @param {Object} settings The settings for this menu item. + * @param {string} settings.title The name to show in the settings menu. + * @param {func} settings.target The registered plugin that should be activated. + * @param {ReactElement} [settings.icon] SVG React Element. + * + * @return {Object} The final sidebar settings object. + */ +export function registerMoreMenuItem( menuItemId, settings ) { + settings = { + menuItemId, + ...settings, + }; + + settings = applyFilters( 'editor.registerMoreMenuItem', settings, menuItemId ); + + if ( ! validateNamespacedId( menuItemId ) ) { + return null; + } + if ( menuItems[ menuItemId ] ) { + console.error( + `Menu item "${ menuItemId }" is already registered.` + ); + } + + if ( ! settings.title ) { + console.error( + `Menu item "${ menuItemId }" must have a title.` + ); + return null; + } + if ( typeof settings.title !== 'string' ) { + console.error( + 'Menu items title must be strings.' + ); + return null; + } + + if ( settings.icon && isString( settings.icon ) ) { + console.error( + 'Menu item icon must be a react component' + ); + return null; + } + + if ( ! settings.target ) { + console.error( + `Menu item "${ menuItemId }" must have a target.` + ); + return null; + } + if ( typeof settings.target !== 'string' ) { + console.error( + 'Menu items target must be strings.' + ); + return null; + } + + settings.callback = activateSidebar.bind( null, settings.target ); + + return menuItems[ menuItemId ] = settings; +} + +/** + * Retrieves all menu items that are registered. + * + * @return {Object} Registered menu items. + */ +export function getMoreMenuItems() { + return menuItems; +} diff --git a/edit-post/api/sidebar.js b/edit-post/api/sidebar.js index b2d52152d421e2..25c33d1ea01d02 100644 --- a/edit-post/api/sidebar.js +++ b/edit-post/api/sidebar.js @@ -1,12 +1,21 @@ /* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */ -/* External dependencies */ +/** + * External dependencies + */ import { isFunction } from 'lodash'; -/* Internal dependencies */ +/** + * Wordpress dependencies + */ +import { applyFilters } from '@wordpress/hooks'; +import { validateNamespacedId } from '@wordpress/utils'; + +/** + * Internal dependencies + */ import store from '../store'; import { setGeneralSidebarActivePanel, openGeneralSidebar } from '../store/actions'; -import { applyFilters } from '@wordpress/hooks'; const sidebars = {}; @@ -30,16 +39,7 @@ export function registerSidebar( name, settings ) { ...settings, }; - if ( typeof name !== 'string' ) { - console.error( - 'Sidebar names must be strings.' - ); - return null; - } - if ( ! /^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/.test( name ) ) { - console.error( - 'Sidebar names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-sidebar.' - ); + if ( ! validateNamespacedId( name ) ) { return null; } if ( ! settings || ! isFunction( settings.render ) ) { diff --git a/edit-post/api/test/more-menu-item.js b/edit-post/api/test/more-menu-item.js new file mode 100644 index 00000000000000..bdfec85dae4a54 --- /dev/null +++ b/edit-post/api/test/more-menu-item.js @@ -0,0 +1,43 @@ +let registerMoreMenuItem, getMoreMenuItems, registerSidebar; +function requireAll() { + jest.resetModules(); + const sidebar = require( '../sidebar' ); + registerSidebar = sidebar.registerSidebar; + const moreMenuItem = require( '../more-menu-item' ); + registerMoreMenuItem = moreMenuItem.registerMoreMenuItem; + getMoreMenuItems = moreMenuItem.getMoreMenuItems; +} + +requireAll(); + +describe( 'registerMoreMenuItem', () => { + beforeEach( requireAll ); + + it( 'successfully registers a more menu item', () => { + registerSidebar( 'plugins/sidebar', { + title: 'Plugin Title', + render: () => 'Component', + } ); + registerMoreMenuItem( 'gutenberg/plugin', { + title: 'Plugin', + target: 'gutenberg/plugin', + } ); + expect( getMoreMenuItems()[ 'gutenberg/plugin' ] ).toBeDefined(); + } ); + + it( 'throws an error when a more menu item with the same id already exists', () => { + registerSidebar( 'plugins/sidebar', { + title: 'Plugin Title', + render: () => 'Component', + } ); + registerMoreMenuItem( 'gutenberg/plugin', { + title: 'Plugin', + target: 'gutenberg/plugin', + } ); + registerMoreMenuItem( 'gutenberg/plugin', { + title: 'Plugin', + target: 'gutenberg/plugin', + } ); + expect( console ).toHaveErrored(); + } ); +} ); diff --git a/edit-post/components/header/more-menu/index.js b/edit-post/components/header/more-menu/index.js index 64a4d108f126f4..30cf99ce977050 100644 --- a/edit-post/components/header/more-menu/index.js +++ b/edit-post/components/header/more-menu/index.js @@ -10,6 +10,7 @@ import { IconButton, Dropdown, MenuItemsGroup } from '@wordpress/components'; import './style.scss'; import ModeSwitcher from '../mode-switcher'; import FixedToolbarToggle from '../fixed-toolbar-toggle'; +import Plugins from '../plugins'; const MoreMenu = () => ( (
+ + { map( ellipsisMenuItems, menuItem => { + const pluginActive = menuItem.target === props.activePlugin; + + let Icon = menuItem.icon ? ( + + { menuItem.icon } + + ) : null; + + if ( pluginActive ) { + Icon = 'yes'; + } + + const buttonClassName = classnames( + 'edit-post-plugins__button', + { + 'has-icon': Icon, + 'is-active': pluginActive, + } + ); + + return ( + onSelect( menuItem.menuItemId ) }> + { menuItem.title } + + ); + } ) } + + ); +} + +export default compose( [ + withSelect( select => { + const editPost = select( 'core/edit-post' ); + const openedSidebar = editPost.getOpenedGeneralSidebar(); + if ( openedSidebar !== 'plugin' ) { + return {}; + } + + return { + activePlugin: editPost.getActivePlugin(), + }; + } ), + withInstanceId, +] )( Plugins ); diff --git a/edit-post/components/header/plugins/style.scss b/edit-post/components/header/plugins/style.scss new file mode 100644 index 00000000000000..3b612f6640a0e1 --- /dev/null +++ b/edit-post/components/header/plugins/style.scss @@ -0,0 +1,32 @@ +.edit-post-plugins__button, +.edit-post-plugins__button.components-icon-button { + width: 100%; + padding: 8px 0; + padding-left: 25px; + text-align: left; + color: $dark-gray-500; + + .dashicon { + margin-right: 5px; + } + + &.has-icon { + padding-left: 0; + } + + &:hover { + color: $black; + } +} + +.edit-post-plugins__icon-container { + margin-right: 5px; + width: 20px; + height: 20px; + overflow: hidden; + + svg { + width: 100%; + height: 100%; + } +} diff --git a/utils/index.js b/utils/index.js index ea860814df14d0..b35f7cef4ea0a2 100644 --- a/utils/index.js +++ b/utils/index.js @@ -12,5 +12,6 @@ export * from './dom'; export * from './mediaupload'; export * from './terms'; export * from './deprecation'; +export * from './plugins'; export { viewPort }; diff --git a/utils/plugins.js b/utils/plugins.js new file mode 100644 index 00000000000000..e2fe91ab82e7df --- /dev/null +++ b/utils/plugins.js @@ -0,0 +1,35 @@ +/* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */ + +/** + * WordPress Dependencies + */ +import { sprintf } from '@wordpress/i18n'; + +/** + * Validates the namespaced id. + * + * A namespaced id should be formatted like this: "namespace/identifier". + * It can only contain lowercase alphanumeric characters and dashes. + * + * If a namespace id is invalid, it will print a console error, optionally using + * the name of the thing it identifies. + * + * @param {string} id Namespaced identifier. + * @param {string} [name] Plural name of what the id is for, defaults to "Namespaced identifiers". + * @return {boolean} Whether the id is valid. + */ +export function validateNamespacedId( id, name = 'Namespaced identifiers' ) { + if ( typeof id !== 'string' ) { + console.error( + sprintf( '%s must be strings.', name ) + ); + return false; + } + if ( ! /^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/.test( id ) ) { + console.error( + sprintf( '%s must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-id', name ) + ); + return false; + } + return true; +} diff --git a/utils/test/plugins.js b/utils/test/plugins.js new file mode 100644 index 00000000000000..a7d68d8b850fb8 --- /dev/null +++ b/utils/test/plugins.js @@ -0,0 +1,26 @@ +/** + * Internal dependencies + */ +import { validateNamespacedId } from '../plugins'; + +describe( 'validatePluginId', () => { + it( 'accepts a valid plugin id', () => { + expect( validateNamespacedId( 'gutenberg/plugin' ) ).toBe( true ); + } ); + + it( 'rejects a pluginId with special characters besides a single "/"', () => { + const valid = validateNamespacedId( 'gutenberg//plugin' ); + expect( console ).toHaveErrored(); + expect( valid ).toBe( false ); + } ); + + it( 'rejects a pluginId that\'s not a string but a number', () => { + expect( validateNamespacedId( 1 ) ).toBe( false ); + expect( console ).toHaveErrored(); + } ); + + it( 'rejects a pluginId that\'s not a string but an object', () => { + expect( validateNamespacedId( {} ) ).toBe( false ); + expect( console ).toHaveErrored(); + } ); +} );