-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add an API to add a plugin sidebar #4109
Closed
Closed
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
f3d3daf
Add an API to add a plugin sidebar
atimmer 2864974
Merge branch 'master' into add/api-add-plugin-sidebar
b4fa154
Applied minor codestyle changes to sidebar.js
ba05cd8
Applied minor codestyle changes to plugins header
506fd15
Merge branch 'master' of https://github.com/WordPress/gutenberg into …
9caaf8e
Fixed broken dependency after merging wordpress/gutenberg
53579b3
Initial attempt at rendering plugins in a separate sidebar
9428aa9
Removed accidentally added IDE file
7241609
Initial changes
8870121
removed .idea
cc0b124
Updated actions, reducers and selectors
b3aa610
finished implementing new actions, reducers and selectors
c7b36b3
Fixed last build errors
e4b9df3
Merge branch 'master' into add/api-add-plugin-sidebar
xyfi 693e5a8
Add renderSidebar function
IreneStr 04d6e2b
Add renderSidebar function
IreneStr aa3c84b
Remove console.logs and unneeded argument
IreneStr ed6ea39
Alter class name to enable plugin sidebar-specific styling
IreneStr 3af2240
Unexpose activateSidebar and registerSidebar
IreneStr 10529c8
Fix console error syntax and remove unnecessary error
IreneStr fd057e8
Rename viewMode to viewportType
IreneStr af1cc84
Merge branch 'master' into add/api-add-plugin-sidebar
IreneStr c7daa63
Add SET_VIEWPORT_TYPE to reducers
IreneStr a5ffe6c
Fix unittests part 1
IreneStr ec870c3
Fix unittests part 2
IreneStr b34991d
Remove unneeded padding
IreneStr 06950bd
Remove mobile middleware
IreneStr fce3ddd
Close sidebar when switching to mobile view
IreneStr 02cc074
Unexpose activateSidebar
IreneStr 0072e66
Fix codestyle
IreneStr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { | ||
renderSidebar, | ||
getSidebarSettings, | ||
} from './sidebar'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* eslint no-console: [ 'error', { allow: [ 'error' ] } ] */ | ||
|
||
/* External dependencies */ | ||
import { isFunction } from 'lodash'; | ||
|
||
/* Internal dependencies */ | ||
import store from '../store'; | ||
import { setGeneralSidebarActivePanel, openGeneralSidebar } from '../store/actions'; | ||
import { applyFilters } from '@wordpress/hooks'; | ||
|
||
const sidebars = {}; | ||
|
||
/** | ||
* Registers a sidebar to the editor. | ||
* | ||
* A button will be shown in the settings menu to open the sidebar. The sidebar | ||
* can be manually opened by calling the `activateSidebar` function. | ||
* | ||
* @param {string} name The name of the sidebar. Should be in | ||
* `[plugin]/[sidebar]` format. | ||
* @param {Object} settings The settings for this sidebar. | ||
* @param {string} settings.title The name to show in the settings menu. | ||
* @param {Function} settings.render The function that renders the sidebar. | ||
* | ||
* @returns {Object} The final sidebar settings object. | ||
*/ | ||
export function registerSidebar( name, settings ) { | ||
settings = { | ||
name, | ||
...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.' | ||
); | ||
return null; | ||
} | ||
if ( ! settings || ! isFunction( settings.render ) ) { | ||
console.error( | ||
'The "render" property must be specified and must be a valid function.' | ||
); | ||
return null; | ||
} | ||
if ( sidebars[ name ] ) { | ||
console.error( | ||
`Sidebar ${ name } is already registered.` | ||
); | ||
} | ||
|
||
if ( ! settings.title ) { | ||
console.error( | ||
`The sidebar ${ name } must have a title.` | ||
); | ||
return null; | ||
} | ||
if ( typeof settings.title !== 'string' ) { | ||
console.error( | ||
'Sidebar titles must be strings.' | ||
); | ||
return null; | ||
} | ||
|
||
settings = applyFilters( 'editor.registerSidebar', settings, name ); | ||
|
||
return sidebars[ name ] = settings; | ||
} | ||
|
||
/** | ||
* Retrieves the sidebar settings object. | ||
* | ||
* @param {string} name The name of the sidebar to retrieve the settings for. | ||
* | ||
* @returns {Object} The settings object of the sidebar. Or null if the | ||
* sidebar doesn't exist. | ||
*/ | ||
export function getSidebarSettings( name ) { | ||
if ( ! sidebars.hasOwnProperty( name ) ) { | ||
return null; | ||
} | ||
return sidebars[ name ]; | ||
} | ||
|
||
/** | ||
* Renders a plugin sidebar. | ||
* | ||
* @param {string} name The name of the plugin sidebar. | ||
* @param {Object} settings The settings for this sidebar. | ||
* @param {string} settings.title The name to show in the settings menu. | ||
* @param {Function} settings.render The function that renders the sidebar. | ||
* | ||
* @returns {void} | ||
*/ | ||
export function renderSidebar( name, settings ) { | ||
registerSidebar( name, settings ); | ||
activateSidebar( name ); | ||
} | ||
|
||
/** | ||
* Activates the given sidebar. | ||
* | ||
* @param {string} name The name of the sidebar to activate. | ||
* @return {void} | ||
*/ | ||
function activateSidebar( name ) { | ||
store.dispatch( openGeneralSidebar( 'plugins' ) ); | ||
store.dispatch( setGeneralSidebarActivePanel( 'plugins', name ) ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { Panel, PanelBody, IconButton, withFocusReturn } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal Dependencies | ||
*/ | ||
import './style.scss'; | ||
import { getSidebarSettings } from '../../api/sidebar'; | ||
import { getActivePlugin } from '../../store/selectors'; | ||
import { closeGeneralSidebar } from '../../store/actions'; | ||
|
||
/** | ||
* Returns the sidebar that should be rendered in the sidebar registered by | ||
* plugins. | ||
* | ||
* @param {string} plugin The currently active plugin. | ||
* | ||
* @returns {Object} The React element to render as a panel. | ||
*/ | ||
export function getPluginSidebar( plugin ) { | ||
const pluginSidebar = getSidebarSettings( plugin ); | ||
|
||
if ( ! pluginSidebar ) { | ||
return { | ||
title: __( 'Error: Unregistered plugin requested.' ), | ||
render: () => { | ||
return <Panel> | ||
<PanelBody> | ||
{ sprintf( __( 'No matching plugin sidebar found for plugin "%s"' ), plugin ) } | ||
</PanelBody> | ||
</Panel>; | ||
}, | ||
}; | ||
} | ||
return pluginSidebar; | ||
} | ||
|
||
function PluginsPanel( { onClose, plugin } ) { | ||
const { | ||
title, | ||
render, | ||
} = getPluginSidebar( plugin ); | ||
return ( | ||
<div | ||
className="editor-sidebar" | ||
role="region" | ||
aria-label={ __( 'Editor plugins' ) } | ||
tabIndex="-1"> | ||
<div className="editor-plugins-panel__header"> | ||
<h3>{ title }</h3> | ||
<IconButton | ||
onClick={ onClose } | ||
icon="no-alt" | ||
label={ __( 'Close settings' ) } | ||
/> | ||
</div> | ||
<div className="editor-plugins-panel__content"> | ||
{ render() } | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default connect( | ||
( state ) => { | ||
return { | ||
plugin: getActivePlugin( state ), | ||
}; | ||
}, { | ||
onClose: closeGeneralSidebar, | ||
} | ||
)( withFocusReturn( PluginsPanel ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
.editor-sidebar { | ||
.editor-plugins-panel { | ||
position: fixed; | ||
top: 0; | ||
right: 0; | ||
bottom: 0; | ||
width: $sidebar-width; | ||
border-left: 1px solid $light-gray-500; | ||
background: $light-gray-300; | ||
color: $dark-gray-500; | ||
height: 100vh; | ||
overflow: hidden; | ||
|
||
@include break-small() { | ||
top: $admin-bar-height-big + $header-height; | ||
z-index: auto; | ||
height: auto; | ||
overflow: auto; | ||
-webkit-overflow-scrolling: touch; | ||
} | ||
|
||
@include break-medium() { | ||
top: $admin-bar-height + $header-height; | ||
} | ||
|
||
> .components-panel .components-panel__header { | ||
position: fixed; | ||
z-index: z-index('.components-panel__header'); | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
height: $panel-header-height; | ||
|
||
@include break-small() { | ||
position: inherit; | ||
top: auto; | ||
left: auto; | ||
right: auto; | ||
} | ||
} | ||
} | ||
|
||
.editor-plugins-panel__header { | ||
padding-left: 16px; | ||
height: $header-height; | ||
border-bottom: 1px solid $light-gray-500; | ||
display: flex; | ||
align-items: center; | ||
|
||
.components-icon-button { | ||
margin-left: auto; | ||
} | ||
|
||
h3 { | ||
margin: 0; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL
@returns
and@return
are equally valid and equivalent JSDoc tags. Noting that we've conventionally used@return
elsewhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the WordPress JavaScript documentation standards we go with
@returns
.