Skip to content

Commit

Permalink
First pass at adding 'Advanced Panels' to Options modal
Browse files Browse the repository at this point in the history
Allows the user to enable or disable 'Advanced Panels' (AKA Meta Boxes)
via the Options modal.

- The title and ID of each Meta Box is stored in the `core/edit-post`
  Redux store.
- The `MetaBoxTitles` HOC provides an easy way to list all of the
  registered Meta Boxes within Gutenberg.
- The `MetaBoxesVisibility` and `MetaBoxVisibility` components respond
  to the existing `isEditorPanelEnabled` selector and show/hide Meta Boxes
  using `el.style.display = 'none'`;
  • Loading branch information
noisysocks committed Oct 17, 2018
1 parent df71243 commit f864dc0
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/meta-box-partial-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ function the_gutenberg_metaboxes() {
*/
$wp_meta_boxes = apply_filters( 'filter_gutenberg_meta_boxes', $wp_meta_boxes );
$locations = array( 'side', 'normal', 'advanced' );
$priorities = array( 'high', 'sorted', 'core', 'default', 'low' );
$active_meta_box_locations = array();
// Render meta boxes.
?>
Expand Down Expand Up @@ -318,6 +319,16 @@ function the_gutenberg_metaboxes() {
<?php endforeach; ?>
<?php

$meta_box_titles = array();
foreach ( $locations as $location ) {
foreach ( $priorities as $priority ) {
$meta_boxes = (array) $wp_meta_boxes[ $current_screen->id ][ $location ][ $priority ];
foreach ( $meta_boxes as $meta_box ) {
$meta_box_titles[ $meta_box['id'] ] = $meta_box['title'];
}
}
}

/**
* Sadly we probably can not add this data directly into editor settings.
*
Expand All @@ -328,6 +339,7 @@ function the_gutenberg_metaboxes() {
*/
$script = 'window._wpLoadGutenbergEditor.then( function() {
wp.data.dispatch( \'core/edit-post\' ).setActiveMetaBoxLocations( ' . wp_json_encode( $active_meta_box_locations ) . ' );
wp.data.dispatch( \'core/edit-post\' ).setMetaBoxTitles( ' . wp_json_encode( $meta_box_titles ) . ' );
} );';

wp_add_inline_script( 'wp-edit-post', $script );
Expand Down
2 changes: 2 additions & 0 deletions packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Sidebar from '../sidebar';
import PluginPostPublishPanel from '../sidebar/plugin-post-publish-panel';
import PluginPrePublishPanel from '../sidebar/plugin-pre-publish-panel';
import FullscreenMode from '../fullscreen-mode';
import MetaBoxesVisibility from '../meta-boxes-visibility';

function Layout( {
mode,
Expand Down Expand Up @@ -91,6 +92,7 @@ function Layout( {
<div className="edit-post-layout__metaboxes">
<MetaBoxes location="advanced" />
</div>
<MetaBoxesVisibility />
</div>
{ publishSidebarOpened ? (
<PostPublishPanel
Expand Down
20 changes: 20 additions & 0 deletions packages/edit-post/src/components/meta-box-titles/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* External dependencies
*/
import { map } from 'lodash';

/**
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
import { withSelect } from '@wordpress/data';

function MetaBoxTitles( { titles, titleWrapper } ) {
return map( titles, ( title, id ) => (
<Fragment key={ id }>{ titleWrapper( title, id ) }</Fragment>
) );
}

export default withSelect( ( select ) => ( {
titles: select( 'core/edit-post' ).getMetaBoxTitles(),
} ) )( MetaBoxTitles );
17 changes: 17 additions & 0 deletions packages/edit-post/src/components/meta-boxes-visibility/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Internal dependencies
*/
import MetaBoxTitles from '../meta-box-titles';
import MetaBoxVisibility from './meta-box-visibility';

function MetaBoxesVisibility() {
return (
<MetaBoxTitles
titleWrapper={ ( title, id ) => (
<MetaBoxVisibility id={ id } />
) }
/>
);
}

export default MetaBoxesVisibility;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';

class MetaBoxVisibility extends Component {
componentDidMount() {
this.updateDOM();
}

componentDidUpdate( prevProps ) {
if ( this.props.isVisible !== prevProps.isVisible ) {
this.updateDOM();
}
}

updateDOM() {
const { id, isVisible } = this.props;
document.getElementById( id ).style.display = isVisible ? '' : 'none';
}

render() {
return null;
}
}

export default withSelect( ( select, { id } ) => ( {
isVisible: select( 'core/edit-post' ).isEditorPanelEnabled( `meta-box-${ id }` ),
} ) )( MetaBoxVisibility );
8 changes: 8 additions & 0 deletions packages/edit-post/src/components/options-modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { PostTaxonomies, PostExcerptCheck, PageAttributesCheck } from '@wordpres
*/
import Section from './section';
import { EnablePublishSidebarOption, EnableTipsOption, EnablePanelOption } from './options';
import MetaBoxTitles from '../meta-box-titles';

const MODAL_NAME = 'edit-post/options';

Expand Down Expand Up @@ -54,6 +55,13 @@ export function OptionsModal( { isModalActive, closeModal } ) {
<EnablePanelOption label={ __( 'Page Attributes' ) } panelName="page-attributes" />
</PageAttributesCheck>
</Section>
<Section title={ __( 'Advanced Panels' ) }>
<MetaBoxTitles
titleWrapper={ ( title, id ) => (
<EnablePanelOption label={ title } panelName={ `meta-box-${ id }` } />
) }
/>
</Section>
</Modal>
);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/edit-post/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ export function setActiveMetaBoxLocations( locations ) {
};
}

export function setMetaBoxTitles( titles ) {
return {
type: 'SET_META_BOX_TITLES',
titles,
};
}

/**
* Returns an action object used to request meta box update.
*
Expand Down
13 changes: 13 additions & 0 deletions packages/edit-post/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,25 @@ export function activeMetaBoxLocations( state = [], action ) {
return state;
}

export function metaBoxTitles( state = {}, action ) {
switch ( action.type ) {
case 'SET_META_BOX_TITLES':
return {
...state,
...action.titles,
};
}

return state;
}

export default combineReducers( {
preferences,
activeGeneralSidebar,
panel,
activeModal,
publishSidebarActive,
activeMetaBoxLocations,
metaBoxTitles,
isSavingMetaBoxes,
} );
4 changes: 4 additions & 0 deletions packages/edit-post/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ export function isMetaBoxLocationActive( state, location ) {
return getActiveMetaBoxLocations( state ).includes( location );
}

export function getMetaBoxTitles( state ) {
return state.metaBoxTitles;
}

/**
* Returns the state of legacy meta boxes.
*
Expand Down

0 comments on commit f864dc0

Please sign in to comment.