Skip to content

Commit

Permalink
Document Outline: Refactor to use the data module (#5871)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored and mtias committed Apr 2, 2018
1 parent 7f406fb commit 0c4bdf6
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions editor/components/document-outline/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';
import { countBy, filter, get } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { compose } from '@wordpress/element';
import { withAPIData } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';

/**
* Internal dependencies
*/
import './style.scss';
import DocumentOutlineItem from './item';
import { getBlocks, getCurrentPostType, getEditedPostAttribute } from '../../store/selectors';
import { selectBlock } from '../../store/actions';

/**
* Module constants
Expand Down Expand Up @@ -61,7 +58,7 @@ const getHeadingLevel = heading => {

const isEmptyHeading = heading => ! heading.attributes.content || heading.attributes.content.length === 0;

export const DocumentOutline = ( { blocks = [], title, onSelect, postType } ) => {
export const DocumentOutline = ( { blocks = [], title, onSelect, isTitleSupported } ) => {
const headings = filter( blocks, ( block ) => block.name === 'core/heading' );

if ( headings.length < 1 ) {
Expand All @@ -81,7 +78,6 @@ export const DocumentOutline = ( { blocks = [], title, onSelect, postType } ) =>
}
};

const isTitleSupported = get( postType, [ 'data', 'supports', 'title' ], false );
const hasTitle = isTitleSupported && title;
const items = headings.map( ( heading ) => ( {
...heading,
Expand Down Expand Up @@ -136,23 +132,21 @@ export const DocumentOutline = ( { blocks = [], title, onSelect, postType } ) =>
};

export default compose(
connect(
( state ) => {
return {
title: getEditedPostAttribute( state, 'title' ),
blocks: getBlocks( state ),
postTypeName: getCurrentPostType( state ),
};
},
{
onSelect( uid ) {
return selectBlock( uid );
},
},
),
withAPIData( ( { postTypeName } ) => {
withSelect( ( select ) => {
const { getEditedPostAttribute, getBlocks } = select( 'core/editor' );
const { getPostType } = select( 'core' );
const postType = getPostType( getEditedPostAttribute( 'type' ) );

return {
title: getEditedPostAttribute( 'title' ),
blocks: getBlocks(),
isTitleSupported: get( postType, [ 'supports', 'title' ], false ),
};
} ),
withDispatch( ( dispatch ) => {
const { selectBlock } = dispatch( 'core/editor' );
return {
postType: postTypeName ? `/wp/v2/types/${ postTypeName }?context=edit` : undefined,
onSelect: selectBlock,
};
} )
)( DocumentOutline );

0 comments on commit 0c4bdf6

Please sign in to comment.