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

Add an API to add a plugin sidebar #4109

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 Dec 20, 2017
2864974
Merge branch 'master' into add/api-add-plugin-sidebar
Jan 3, 2018
b4fa154
Applied minor codestyle changes to sidebar.js
Jan 3, 2018
ba05cd8
Applied minor codestyle changes to plugins header
Jan 3, 2018
506fd15
Merge branch 'master' of https://github.com/WordPress/gutenberg into …
Jan 3, 2018
9caaf8e
Fixed broken dependency after merging wordpress/gutenberg
Jan 3, 2018
53579b3
Initial attempt at rendering plugins in a separate sidebar
Jan 5, 2018
9428aa9
Removed accidentally added IDE file
Jan 8, 2018
7241609
Initial changes
Jan 9, 2018
8870121
removed .idea
Jan 9, 2018
cc0b124
Updated actions, reducers and selectors
Jan 10, 2018
b3aa610
finished implementing new actions, reducers and selectors
Jan 10, 2018
c7b36b3
Fixed last build errors
Jan 10, 2018
e4b9df3
Merge branch 'master' into add/api-add-plugin-sidebar
xyfi Jan 11, 2018
693e5a8
Add renderSidebar function
IreneStr Jan 12, 2018
04d6e2b
Add renderSidebar function
IreneStr Jan 17, 2018
aa3c84b
Remove console.logs and unneeded argument
IreneStr Jan 18, 2018
ed6ea39
Alter class name to enable plugin sidebar-specific styling
IreneStr Jan 19, 2018
3af2240
Unexpose activateSidebar and registerSidebar
IreneStr Jan 22, 2018
10529c8
Fix console error syntax and remove unnecessary error
IreneStr Jan 22, 2018
fd057e8
Rename viewMode to viewportType
IreneStr Jan 22, 2018
af1cc84
Merge branch 'master' into add/api-add-plugin-sidebar
IreneStr Jan 22, 2018
c7daa63
Add SET_VIEWPORT_TYPE to reducers
IreneStr Jan 23, 2018
a5ffe6c
Fix unittests part 1
IreneStr Jan 23, 2018
ec870c3
Fix unittests part 2
IreneStr Jan 24, 2018
b34991d
Remove unneeded padding
IreneStr Jan 25, 2018
06950bd
Remove mobile middleware
IreneStr Jan 25, 2018
fce3ddd
Close sidebar when switching to mobile view
IreneStr Jan 25, 2018
02cc074
Unexpose activateSidebar
IreneStr Jan 25, 2018
0072e66
Fix codestyle
IreneStr Jan 25, 2018
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
4 changes: 4 additions & 0 deletions .idea/watcherTasks.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions editor/api/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isFunction } from 'lodash';

/* Internal dependencies */
import store from '../store';
import { setActivePanel } from '../store/actions';
import { setActivePlugin } from '../store/actions';
import { applyFilters } from '@wordpress/hooks';

const sidebars = {};
Expand Down Expand Up @@ -103,5 +103,5 @@ export function getSidebars() {
* @param {string} name The name of the sidebar to activate.
*/
export function activateSidebar( name ) {
store.dispatch( setActivePanel( name ) );
store.dispatch( setActivePlugin( name ) );
}
1 change: 1 addition & 0 deletions editor/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export { default as MetaBoxes } from './meta-boxes';
export { default as PageAttributesCheck } from './page-attributes/check';
export { default as PageAttributesOrder } from './page-attributes/order';
export { default as PageAttributesParent } from './page-attributes/parent';
export { default as PluginsPanel } from './plugins-panel';
export { default as PostAuthor } from './post-author';
export { default as PostAuthorCheck } from './post-author/check';
export { default as PostComments } from './post-comments';
Expand Down
71 changes: 71 additions & 0 deletions editor/components/plugins-panel/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { compose } from '@wordpress/element';
import { Panel, PanelBody, IconButton, withFocusReturn } from '@wordpress/components';
import { getActivePlugin } from '../../store/selectors';

/**
* Internal Dependencies
*/
import './style.scss';
import { getSidebar } from '../../api/sidebar';

/**
* 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.
*/
function getPluginSidebar( plugin ) {
const pluginSidebar = getSidebar( plugin );

if ( ! pluginSidebar ) {
return () => {
return <Panel>
<PanelBody>
{ sprintf( __( 'No matching plugin sidebar found for plugin "%s"' ), plugin ) }
Copy link
Member

Choose a reason for hiding this comment

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

Please add translator comment

</PanelBody>
</Panel>;
};
}

return pluginSidebar.render;
}

function PluginsPanel( { onClose, plugin } ) {
return (
<div
className="editor-sidebar"
role="region"
aria-label={ __( 'Editor plugins' ) }
tabIndex="-1">
<div className="components-panel__header editor-sidebar__panel-tabs">
<IconButton
onClick={ onClose }
icon="no-alt"
label={ __( 'Close settings' ) }
/>
</div>
<div className="editor-plugins-panel__content">
{ getPluginSidebar( plugin )() }
</div>
</div>
);
}

export default connect(
( state ) => {
return {
plugin: getActivePlugin( state ),
};
}
)( withFocusReturn( PluginsPanel ) );
49 changes: 49 additions & 0 deletions editor/components/plugins-panel/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.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;
align-content: space-between;
}
10 changes: 5 additions & 5 deletions editor/edit-post/header/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { MenuItemsGroup } from '@wordpress/components';
* Internal dependencies
*/
import { getSidebars, activateSidebar } from '../../../api/sidebar';
import { getActivePanel, hasOpenSidebar } from '../../../store/selectors';
import { getActivePanel, hasOpenSidebar } from '../../../store/selectors';
import { toggleSidebar } from '../../../store/actions';

/**
Expand All @@ -30,7 +30,7 @@ import { toggleSidebar } from '../../../store/actions';
*
* @returns {Object} The rendered list of menu items.
*/
function Plugins( { activePanel, onSwitch, isSidebarOpen, onToggleSidebar } ) {
function Plugins( { activePanel, onSwitch, isSidebarOpen, onTogglePluginsSidebar } ) {
const sidebars = getSidebars();

// This makes sure no check mark is before a plugin if the sidebar is closed.
Expand All @@ -47,7 +47,7 @@ function Plugins( { activePanel, onSwitch, isSidebarOpen, onToggleSidebar } ) {
onSwitch( panelToActivate );

if ( ! isSidebarOpen ) {
onToggleSidebar();
onTogglePluginsSidebar();
}
}

Expand Down Expand Up @@ -81,8 +81,8 @@ export default connect(
activateSidebar( value );
ownProps.onToggle( value );
},
onToggleSidebar: () => {
dispatch( toggleSidebar() );
onTogglePluginsSidebar: () => {
dispatch( toggleSidebar( 'plugins' ) );
},
};
}
Expand Down
5 changes: 5 additions & 0 deletions editor/edit-post/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
UnsavedChangesWarning,
EditorNotices,
PostPublishPanel,
PluginsPanel
} from '../../components';
import {
getEditorMode,
Expand All @@ -40,6 +41,7 @@ function Layout( {
layoutHasOpenSidebar,
isDefaultSidebarOpened,
isPublishSidebarOpened,
isPluginsSidebarOpened,
hasFixedToolbar,
onToggleSidebar,
} ) {
Expand All @@ -48,6 +50,7 @@ function Layout( {
'has-fixed-toolbar': hasFixedToolbar,
} );
const closePublishPanel = () => onToggleSidebar( 'publish', false );
const closePluginsPanel = () => onToggleSidebar( 'plugins', false );

return (
<div className={ className }>
Expand All @@ -71,6 +74,7 @@ function Layout( {
</div>
{ isDefaultSidebarOpened && <Sidebar /> }
{ isPublishSidebarOpened && <PostPublishPanel onClose={ closePublishPanel } /> }
{ isPluginsSidebarOpened && <PluginsPanel onClose={ closePluginsPanel } /> }
<Popover.Slot />
</div>
);
Expand All @@ -82,6 +86,7 @@ export default connect(
layoutHasOpenSidebar: hasOpenSidebar( state ),
isDefaultSidebarOpened: isSidebarOpened( state ),
isPublishSidebarOpened: isSidebarOpened( state, 'publish' ),
isPluginsSidebarOpened: isSidebarOpened( state, 'plugins' ),
hasFixedToolbar: isFeatureActive( state, 'fixedToolbar' ),
} ),
{ onToggleSidebar: toggleSidebar }
Expand Down
19 changes: 19 additions & 0 deletions editor/edit-post/layout/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,22 @@
.editor-layout .editor-post-publish-panel__header-publish-button {
margin-right: 52px; // This number is approximative to keep the publish button at the same position when opening the panel
}

.editor-layout .editor-plugins-panel {
position: fixed;
z-index: z-index( '.editor-layout .editor-post-publish-panel' );
top: $admin-bar-height-big;
bottom: 0;
right: 0;
left: 0;
overflow: auto;
position: fixed;

@include break-medium() {
top: $admin-bar-height;
left: auto;
width: $sidebar-width;
border-left: 1px solid $light-gray-500;
@include slide_in_right;
}
}
26 changes: 1 addition & 25 deletions editor/edit-post/sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,6 @@ import { getSidebar } from '../../api/sidebar';

import { getActivePanel } from '../../store/selectors';

/**
* Returns the sidebar that should be rendered in the sidebar registered by
* plugins.
*
* @param {string} panel The currently active panel.
*
* @returns {Object} The React element to render as a panel.
*/
function getPluginSidebar( panel ) {
const pluginSidebar = getSidebar( panel );

if ( ! pluginSidebar ) {
return () => {
return <Panel>
<PanelBody>
{ sprintf( __( 'No matching plugin sidebar found for panel "%s"' ), panel ) }
</PanelBody>
</Panel>;
};
}

return pluginSidebar.render;
}

/**
* Returns the panel that should be rendered in the sidebar.
*
Expand All @@ -60,7 +36,7 @@ function getPanel( panel ) {
return BlockInspectorPanel;

default:
return getPluginSidebar( panel );
return PostSettings;
}
}

Expand Down
15 changes: 14 additions & 1 deletion editor/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export function stopTyping() {
/**
* Returns an action object used in signalling that the user toggled the sidebar
*
* @param {String} sidebar Name of the sidebar to toggle (desktop, mobile or publish)
* @param {String} sidebar Name of the sidebar to toggle (desktop, mobile, plugins or publish)
* @param {Boolean?} force Force a sidebar state
* @return {Object} Action object
*/
Expand All @@ -359,6 +359,19 @@ export function setActivePanel( panel ) {
};
}

/**
* Returns an action object usid in signalling that the user switched the active plugin.
*
* @param {String} plugin The plugin name
* @return {Object} Action object
*/
export function setActivePlugin( plugin ) {
Copy link
Member

Choose a reason for hiding this comment

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

setActive(Plugin)Sidebar might be a better name, because a plugin can have multiple sidebars.

return {
type: 'SET_ACTIVE_PLUGIN',
plugin,
};
}

/**
* Returns an action object used in signalling that the user toggled a sidebar panel
*
Expand Down
1 change: 1 addition & 0 deletions editor/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const PREFERENCES_DEFAULTS = {
desktop: true,
mobile: false,
publish: false,
plugins: false,
},
panels: { 'post-status': true },
recentlyUsedBlocks: [],
Expand Down
10 changes: 10 additions & 0 deletions editor/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,15 @@ export function panel( state = 'document', action ) {
return state;
}

export function plugin( state = 'null', action ) {
switch( action.type ) {
case 'SET_ACTIVE_PLUGIN':
return action.plugin;
}

return state;
}

/**
* Reducer returning current network request state (whether a request to the WP
* REST API is in progress, successful, or failed).
Expand Down Expand Up @@ -786,6 +795,7 @@ export default optimist( combineReducers( {
hoveredBlock,
blocksMode,
blockInsertionPoint,
plugin,
preferences,
panel,
saving,
Expand Down
14 changes: 12 additions & 2 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ export function getActivePanel( state ) {
return state.panel;
}

/**
* Returns the current active plugin for the plugin sidebar.
Copy link
Member

Choose a reason for hiding this comment

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

See above: a plugin can have multiple sidebars

*
* @param {Object} state Global application state
* @return {String} Active plugin sidebar plugin
*/
export function getActivePlugin( state ) {
return state.plugin;
}

/**
* Returns the preferences (these preferences are persisted locally)
*
Expand Down Expand Up @@ -145,8 +155,8 @@ export function isSidebarOpened( state, sidebar ) {
export function hasOpenSidebar( state ) {
const sidebars = getPreference( state, 'sidebars' );
return isMobile( state ) ?
sidebars.mobile || sidebars.publish :
sidebars.desktop || sidebars.publish;
sidebars.mobile || sidebars.publish || sidebars.plugins :
sidebars.desktop || sidebars.publish || sidebars.plugins ;
}

/**
Expand Down