Skip to content

Commit

Permalink
Components: Extract Reusable DocumentOutlineCheck component
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 24, 2017
1 parent e51fbe3 commit b65f8a0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
26 changes: 26 additions & 0 deletions editor/components/document-outline/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';
import { filter } from 'lodash';

/**
* Internal dependencies
*/
import { getBlocks } from '../../selectors';

function DocumentOutlineCheck( { blocks, children } ) {
const headings = filter( blocks, ( block ) => block.name === 'core/heading' );

if ( headings.length <= 1 ) {
return null;
}

return children;
}

export default connect(
( state ) => ( {
blocks: getBlocks( state ),
} )
)( DocumentOutlineCheck );
1 change: 1 addition & 0 deletions editor/components/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Post Related Components
export { default as AutosaveMonitor } from './autosave-monitor';
export { default as DocumentOutline } from './document-outline';
export { default as DocumentOutlineCheck } from './document-outline/check';
export { default as EditorGlobalKeyboardShortcuts } from './editor-global-keyboard-shortcuts';
export { default as EditorHistoryRedo } from './editor-history/redo';
export { default as EditorHistoryUndo } from './editor-history/undo';
Expand Down
24 changes: 9 additions & 15 deletions editor/edit-post/sidebar/document-outline-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { connect } from 'react-redux';
import { filter } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -13,33 +12,28 @@ import { PanelBody } from '@wordpress/components';
/**
* Internal dependencies
*/
import { DocumentOutline } from '../../../components';
import { getBlocks, isEditorSidebarPanelOpened } from '../../../selectors';
import { DocumentOutline, DocumentOutlineCheck } from '../../../components';
import { isEditorSidebarPanelOpened } from '../../../selectors';
import { toggleSidebarPanel } from '../../../actions';

/**
* Module constants
*/
const PANEL_NAME = 'table-of-contents';

const DocumentOutlinePanel = ( { blocks, isOpened, onTogglePanel } ) => {
const headings = filter( blocks, ( block ) => block.name === 'core/heading' );

if ( headings.length <= 1 ) {
return null;
}

function DocumentOutlinePanel( { isOpened, onTogglePanel } ) {
return (
<PanelBody title={ __( 'Document Outline' ) } opened={ isOpened } onToggle={ onTogglePanel }>
<DocumentOutline />
</PanelBody>
<DocumentOutlineCheck>
<PanelBody title={ __( 'Document Outline' ) } opened={ isOpened } onToggle={ onTogglePanel }>
<DocumentOutline />
</PanelBody>
</DocumentOutlineCheck>
);
};
}

export default connect(
( state ) => {
return {
blocks: getBlocks( state ),
isOpened: isEditorSidebarPanelOpened( state, PANEL_NAME ),
};
},
Expand Down

0 comments on commit b65f8a0

Please sign in to comment.