Skip to content

Commit

Permalink
Display cite placeholder if inner block is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed May 1, 2018
1 parent 01df9e7 commit 56315f1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core-blocks/quote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const settings = {

edit: withState( {
editable: 'content',
} )( ( { attributes, setAttributes, isSelected, className, editable, setState } ) => {
} )( ( { attributes, setAttributes, isSelected, className, editable, setState, hasSelectedBlock } ) => {
const { align, citation, style } = attributes;
const containerClassname = classnames( className, style === 2 ? 'is-large' : '' );
const onSetActiveEditable = ( newEditable ) => () => {
Expand Down Expand Up @@ -111,7 +111,7 @@ export const settings = {
style={ { textAlign: align } }
>
<InnerBlocks />
{ ( ( citation && citation.length > 0 ) || isSelected ) && (
{ ( ( citation && citation.length > 0 ) || isSelected || hasSelectedBlock ) && (
<RichText
tagName="cite"
value={ citation }
Expand Down
3 changes: 3 additions & 0 deletions editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ export class BlockListBlock extends Component {
id={ uid }
isSelectionEnabled={ this.props.isSelectionEnabled }
toggleSelection={ this.props.toggleSelection }
hasSelectedBlock={ this.props.hasSelectedBlock }
/>
) }
{ isValid && mode === 'html' && (
Expand Down Expand Up @@ -617,6 +618,7 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => {
getSelectedBlocksInitialCaretPosition,
getBlockSelectionEnd,
getBlockRootUID,
hasBlockSelectedBlock,
} = select( 'core/editor' );
const isSelected = isBlockSelected( uid );
return {
Expand All @@ -638,6 +640,7 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => {
isSelected,
rootUIDOfRoot: getBlockRootUID( rootUID ),
orderOfRoot: getBlockIndex( rootUID, getBlockRootUID( rootUID ) ),
hasSelectedBlock: hasBlockSelectedBlock( uid ),
};
} );

Expand Down
19 changes: 19 additions & 0 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,25 @@ export function hasSelectedBlock( state ) {
return !! start && start === end;
}

/**
* Returns true if there is a selected block inside a given block, or false
* otherwise.
*
* @param {Object} state Editor state.
* @param {string} uid Block in which to find a selected block.
*
* @return {boolean} Whether a the block contains a selected block.
*/
export function hasBlockSelectedBlock( state, uid ) {
const { start, end } = state.blockSelection;

if ( ! start || start !== end ) {
return false;
}

return getBlockRootUID( state, start ) === uid;
}

/**
* Returns the currently selected block, or null if there is no selected block.
*
Expand Down
39 changes: 39 additions & 0 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const {
isPermalinkEditable,
getPermalink,
getPermalinkParts,
hasBlockSelectedBlock,
} = selectors;

describe( 'selectors', () => {
Expand Down Expand Up @@ -1491,6 +1492,44 @@ describe( 'selectors', () => {
} );
} );

describe( 'hasBlockSelectedBlock', () => {
it( 'should return true if the block has selected blocks', () => {
const state = {
editor: {
present: {
blockOrder: {
1: [ '2' ],
},
},
},
blockSelection: {
start: '2',
end: '2',
},
};

expect( hasBlockSelectedBlock( state, '1' ) ).toBe( true );
} );

it( 'should return false if the block does not have selected blocks', () => {
const state = {
editor: {
present: {
blockOrder: {
1: [ '2' ],
},
},
},
blockSelection: {
start: '1',
end: '1',
},
};

expect( hasBlockSelectedBlock( state, '1' ) ).toBe( false );
} );
} );

describe( 'getGlobalBlockCount', () => {
it( 'should return the global number of top-level blocks in the post', () => {
const state = {
Expand Down

0 comments on commit 56315f1

Please sign in to comment.