Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix don't show hover effect if the ancestor of a block is multi-selected #7613

Merged
merged 1 commit into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ export class BlockListBlock extends Component {
* @see https://developer.mozilla.org/en-US/docs/Web/Events/mouseenter
*/
maybeHover() {
const { isMultiSelected, isSelected } = this.props;
const { isPartOfMultiSelection, isSelected } = this.props;
const { isHovered } = this.state;

if ( isHovered || isMultiSelected || isSelected ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we can drop this.props.isMultiSelecting entirely and replace it with isPartOfMultiSelection. Is there a use-case where we need both?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isMultiSelecting condition is required to avoid the hover effect when dragging the mouse to multi-select. There is a moment when adding a new block to a multi-selection where the block is not yet multi-selected and we are with the mouse over it without thus the hover would appear. The isMultiSelecting flag may be true while isPartOfMultiSelection maybe false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's true because isPartOfMultiSelection = isMultiSelected || somethingElse I was not taking about isMultiSelecting

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you mean dropping isMultiSelected entirely. If that's case in function maybeHover we are already doing it and we don't use isMultiSelected. But for other functions e.g: related to the block focus we should continue to use isMultiSelected because they want to check if the specific block is selected and not if the block or one ancestor is selected.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at the other tests and I believe isPartOfMultiSelection probably makes more sense there as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for watching the use cases in detail. I applied your suggestion and I made some tests and the result looks great :)

if ( isHovered || isPartOfMultiSelection || isSelected ||
this.props.isMultiSelecting || this.hadTouchStart ) {
return;
}
Expand Down Expand Up @@ -258,7 +258,7 @@ export class BlockListBlock extends Component {
* @return {void}
*/
onFocus() {
if ( ! this.props.isSelected && ! this.props.isMultiSelected ) {
if ( ! this.props.isSelected && ! this.props.isPartOfMultiSelection ) {
this.props.onSelect();
}
}
Expand Down Expand Up @@ -302,7 +302,7 @@ export class BlockListBlock extends Component {
// onFocus excludes blocks involved in a multiselection, as
// focus can be incurred by starting a multiselection (focus
// moved to first block's multi-controls).
if ( this.props.isMultiSelected ) {
if ( this.props.isPartOfMultiSelection ) {
this.props.onSelect();
}
}
Expand Down Expand Up @@ -378,7 +378,7 @@ export class BlockListBlock extends Component {
rootUID,
layout,
isSelected,
isMultiSelected,
isPartOfMultiSelection,
isFirstMultiSelected,
isTypingWithinBlock,
isMultiSelecting,
Expand All @@ -403,8 +403,8 @@ export class BlockListBlock extends Component {
const showSideInserter = ( isSelected || isHovered ) && isEmptyDefaultBlock;
const shouldAppearSelected = ! showSideInserter && ( isSelected || hasSelectedInnerBlock ) && ! isTypingWithinBlock;
// We render block movers and block settings to keep them tabbale even if hidden
const shouldRenderMovers = ( isSelected || hoverArea === 'left' ) && ! showEmptyBlockSideInserter && ! isMultiSelecting && ! isMultiSelected && ! isTypingWithinBlock;
const shouldRenderBlockSettings = ( isSelected || hoverArea === 'right' ) && ! isMultiSelecting && ! isMultiSelected && ! isTypingWithinBlock;
const shouldRenderMovers = ( isSelected || hoverArea === 'left' ) && ! showEmptyBlockSideInserter && ! isMultiSelecting && ! isPartOfMultiSelection && ! isTypingWithinBlock;
const shouldRenderBlockSettings = ( isSelected || hoverArea === 'right' ) && ! isMultiSelecting && ! isPartOfMultiSelection && ! isTypingWithinBlock;
const shouldShowBreadcrumb = isHovered && ! isEmptyDefaultBlock;
const shouldShowContextualToolbar = ! showSideInserter && isSelected && ! isTypingWithinBlock && isValid && ( ! hasFixedToolbar || ! isLargeViewport );
const shouldShowMobileToolbar = shouldAppearSelected;
Expand All @@ -413,14 +413,14 @@ export class BlockListBlock extends Component {
// Insertion point can only be made visible when the side inserter is
// not present, and either the block is at the extent of a selection or
// is the first block in the top-level list rendering.
const shouldShowInsertionPoint = ( isMultiSelected && isFirst ) || ! isMultiSelected;
const shouldShowInsertionPoint = ( isPartOfMultiSelection && isFirst ) || ! isPartOfMultiSelection;
const canShowInBetweenInserter = ! isEmptyDefaultBlock && ! isPreviousBlockADefaultEmptyBlock;

// Generate the wrapper class names handling the different states of the block.
const wrapperClassName = classnames( 'editor-block-list__block', {
'has-warning': ! isValid || !! error,
'is-selected': shouldAppearSelected,
'is-multi-selected': isMultiSelected,
'is-multi-selected': isPartOfMultiSelection,
'is-hovered': isHovered && ! isEmptyDefaultBlock,
'is-shared': isSharedBlock( blockType ),
'is-hidden': dragging,
Expand Down Expand Up @@ -469,7 +469,7 @@ export class BlockListBlock extends Component {
] }
{ ...wrapperProps }
>
{ ! isMultiSelected && isMovable && (
{ ! isPartOfMultiSelection && isMovable && (
<BlockDraggable
rootUID={ rootUID }
index={ order }
Expand Down Expand Up @@ -584,6 +584,7 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => {
getPreviousBlockUid,
getNextBlockUid,
getBlock,
isAncestorMultiSelected,
isBlockMultiSelected,
isFirstMultiSelectedBlock,
isMultiSelecting,
Expand All @@ -602,10 +603,9 @@ const applyWithSelect = withSelect( ( select, { uid, rootUID } ) => {
const block = getBlock( uid );
const previousBlockUid = getPreviousBlockUid( uid );
const previousBlock = getBlock( previousBlockUid );

return {
nextBlockUid: getNextBlockUid( uid ),
isMultiSelected: isBlockMultiSelected( uid ),
isPartOfMultiSelection: isBlockMultiSelected( uid ) || isAncestorMultiSelected( uid ),
isFirstMultiSelected: isFirstMultiSelectedBlock( uid ),
isMultiSelecting: isMultiSelecting(),
hasSelectedInnerBlock: isParentOfSelectedBlock,
Expand Down
24 changes: 24 additions & 0 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,30 @@ export function isBlockMultiSelected( state, uid ) {
return getMultiSelectedBlockUids( state ).indexOf( uid ) !== -1;
}

/**
* Returns true if an ancestor of the block is multi-selected and false otherwise.
*
* @param {Object} state Global application state.
* @param {string} uid Block unique ID.
*
* @return {boolean} Whether an ancestor of the block is in multi-selection set.
*/
export const isAncestorMultiSelected = createSelector(
( state, uid ) => {
let ancestorUid = uid;
let isMultiSelected = false;
while ( ancestorUid && ! isMultiSelected ) {
ancestorUid = getBlockRootUID( state, ancestorUid );
isMultiSelected = isBlockMultiSelected( state, ancestorUid );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we need some recursivity here in case we have more than one ancestor?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @youknowriad, yes we need to iterate over all the ancestors. We are doing it with the while ( ancestorUid && ! isMultiSelected ). The while cycle will only stop when we have no more ancestors or when we found a multi-selected ancestor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh gotcha missed it, I guess I was expecting a call to isAncestorMultiSelected

}
return isMultiSelected;
},
( state ) => [
state.editor.present.blockOrder,
state.blockSelection.start,
state.blockSelection.end,
],
);
/**
* Returns the unique ID of the block which begins the multi-selection set, or
* null if there is no multi-selection.
Expand Down