From 56315f1067d1cd8b47f7fcfa7c28bef23eaaa997 Mon Sep 17 00:00:00 2001 From: iseulde Date: Tue, 1 May 2018 16:39:11 +0200 Subject: [PATCH] Display cite placeholder if inner block is selected --- core-blocks/quote/index.js | 4 +-- editor/components/block-list/block.js | 3 +++ editor/store/selectors.js | 19 +++++++++++++ editor/store/test/selectors.js | 39 +++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/core-blocks/quote/index.js b/core-blocks/quote/index.js index 7f96c802567d55..6a9a97b1f1993a 100644 --- a/core-blocks/quote/index.js +++ b/core-blocks/quote/index.js @@ -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 ) => () => { @@ -111,7 +111,7 @@ export const settings = { style={ { textAlign: align } } > - { ( ( citation && citation.length > 0 ) || isSelected ) && ( + { ( ( citation && citation.length > 0 ) || isSelected || hasSelectedBlock ) && ( ) } { isValid && mode === 'html' && ( @@ -617,6 +618,7 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => { getSelectedBlocksInitialCaretPosition, getBlockSelectionEnd, getBlockRootUID, + hasBlockSelectedBlock, } = select( 'core/editor' ); const isSelected = isBlockSelected( uid ); return { @@ -638,6 +640,7 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => { isSelected, rootUIDOfRoot: getBlockRootUID( rootUID ), orderOfRoot: getBlockIndex( rootUID, getBlockRootUID( rootUID ) ), + hasSelectedBlock: hasBlockSelectedBlock( uid ), }; } ); diff --git a/editor/store/selectors.js b/editor/store/selectors.js index 0d10c2106d3963..3364b01389cb0c 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -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. * diff --git a/editor/store/test/selectors.js b/editor/store/test/selectors.js index 53aa2a2bcfd594..e766aa548efe61 100644 --- a/editor/store/test/selectors.js +++ b/editor/store/test/selectors.js @@ -88,6 +88,7 @@ const { isPermalinkEditable, getPermalink, getPermalinkParts, + hasBlockSelectedBlock, } = selectors; describe( 'selectors', () => { @@ -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 = {