-
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 (new) #4777
Merged
Merged
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a69476e
Make it possible for plugins to render a sidebar
IreneStr f5586c9
Fix sidebar styling
IreneStr 26c088e
Remove unused mobile middleware
IreneStr d221bd9
Fix editor mode switching
IreneStr d31b21c
Fix editorMode switch test
IreneStr b69794b
Fix documentation
IreneStr 1db6a05
Merge branch 'master' into add/api-add-plugin-sidebar-2
IreneStr 3cdf86f
Change `@returns` into `@return`
atimmer fbf1225
Add storeKey to make the plugins panel connect to the correct store
IreneStr f30e950
Merge branch 'master' into add/api-add-plugin-sidebar-2
IreneStr 380a125
Rename 'plugins' to 'plugin' in selector
IreneStr cdfc5a6
Set editor as default activeGeneralSidebar
IreneStr 872ddcc
Unexpose getSidebarSettings
IreneStr 271db7c
Remove duplicate scss and remove renderSidebar function
IreneStr e116f96
Fix unittests
IreneStr 6bed5f5
Merge branch 'master' into add/api-add-plugin-sidebar-2
IreneStr 2388b60
Fix header height, font and close button
IreneStr c417b7e
Update index.js
gziolo 0570733
Edit Post: Remove no longer used getPluginSidebar
gziolo 29beb82
Edit Post: Remove no longer used imports from PluginsPanel
gziolo ab02dff
Edit Post: Update Layout to handle missing plugin properly
gziolo 85d6ff1
Edit Post: Remove obsolete blank line
gziolo 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 { | ||
registerSidebar, | ||
activateSidebar, | ||
} 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,98 @@ | ||
/* 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. | ||
* | ||
* @return {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. | ||
* | ||
* @return {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 ]; | ||
} | ||
/** | ||
* Activates the given sidebar. | ||
* | ||
* @param {string} name The name of the sidebar to activate. | ||
* @return {void} | ||
*/ | ||
export function activateSidebar( name ) { | ||
store.dispatch( openGeneralSidebar( 'plugin' ) ); | ||
store.dispatch( setGeneralSidebarActivePanel( 'plugin', 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,23 +29,36 @@ import VisualEditor from '../modes/visual-editor'; | |
import EditorModeKeyboardShortcuts from '../modes/keyboard-shortcuts'; | ||
import { | ||
getEditorMode, | ||
hasFixedToolbar, | ||
hasOpenSidebar, | ||
isSidebarOpened, | ||
isFeatureActive, | ||
getOpenedGeneralSidebar, | ||
isPublishSidebarOpened, | ||
} from '../../store/selectors'; | ||
import { toggleSidebar } from '../../store/actions'; | ||
import { closePublishSidebar } from '../../store/actions'; | ||
import PluginsPanel from '../../components/plugins-panel/index.js'; | ||
|
||
function GeneralSidebar( { openedGeneralSidebar } ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be inlined inside the component that is consuming it. If it is going to be reused in other places it should be moved to its own file. |
||
switch ( openedGeneralSidebar ) { | ||
case 'editor': | ||
return <Sidebar />; | ||
case 'plugin': | ||
return <PluginsPanel />; | ||
default: | ||
} | ||
return null; | ||
} | ||
|
||
function Layout( { | ||
mode, | ||
layoutHasOpenSidebar, | ||
isDefaultSidebarOpened, | ||
isPublishSidebarOpened, | ||
fixedToolbarActive, | ||
onClosePublishPanel, | ||
publishSidebarOpen, | ||
openedGeneralSidebar, | ||
hasFixedToolbar, | ||
onClosePublishSidebar, | ||
} ) { | ||
const className = classnames( 'edit-post-layout', { | ||
'is-sidebar-opened': layoutHasOpenSidebar, | ||
'has-fixed-toolbar': fixedToolbarActive, | ||
'has-fixed-toolbar': hasFixedToolbar, | ||
} ); | ||
|
||
return ( | ||
|
@@ -68,8 +81,11 @@ function Layout( { | |
<MetaBoxes location="advanced" /> | ||
</div> | ||
</div> | ||
{ isDefaultSidebarOpened && <Sidebar /> } | ||
{ isPublishSidebarOpened && <PostPublishPanel onClose={ onClosePublishPanel } /> } | ||
{ publishSidebarOpen && <PostPublishPanel onClose={ onClosePublishSidebar } /> } | ||
{ | ||
openedGeneralSidebar !== null && <GeneralSidebar | ||
openedGeneralSidebar={ openedGeneralSidebar } /> | ||
} | ||
<Popover.Slot /> | ||
</div> | ||
); | ||
|
@@ -79,12 +95,12 @@ export default connect( | |
( state ) => ( { | ||
mode: getEditorMode( state ), | ||
layoutHasOpenSidebar: hasOpenSidebar( state ), | ||
isDefaultSidebarOpened: isSidebarOpened( state ), | ||
isPublishSidebarOpened: isSidebarOpened( state, 'publish' ), | ||
fixedToolbarActive: hasFixedToolbar( state ), | ||
openedGeneralSidebar: getOpenedGeneralSidebar( state ), | ||
publishSidebarOpen: isPublishSidebarOpened( state ), | ||
hasFixedToolbar: isFeatureActive( state, 'fixedToolbar' ), | ||
} ), | ||
{ | ||
onClosePublishPanel: () => toggleSidebar( 'publish', false ), | ||
onClosePublishSidebar: closePublishSidebar, | ||
}, | ||
undefined, | ||
{ storeKey: 'edit-post' } | ||
|
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
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.
I think we have the same logic in
blocks.registerBlockType
. I'm afraid this needs to be further discussed. The thing is that it happens after validation process is done, so you can update all settings however you want and it won't be validated again. It seems like this should happen before the validation begins.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.
This will be addressed in a next iteration