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

Detect focus leaving wpbody-content to clear selected block #1266

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class VisualEditorBlock extends wp.element.Component {
this.mergeBlocks = this.mergeBlocks.bind( this );
this.onFocus = this.onFocus.bind( this );
this.onPointerDown = this.onPointerDown.bind( this );
this.stopClickPropagation = this.stopClickPropagation.bind( this );
this.previousOffset = null;
}

Expand Down Expand Up @@ -112,7 +113,6 @@ class VisualEditorBlock extends wp.element.Component {

bindBlockNode( node ) {
this.node = node;
this.props.blockRef( node );
}

setAttributes( attributes ) {
Expand Down Expand Up @@ -214,6 +214,12 @@ class VisualEditorBlock extends wp.element.Component {
this.props.onSelect();
}

stopClickPropagation( event ) {
// If parent visual editor receives a bubbled click event, it infers as
// click outside to clear selected block, so stop bubbling on click.
event.stopPropagation();
}

render() {
const { block, multiSelectedBlockUids } = this.props;
const blockType = wp.blocks.getBlockType( block.name );
Expand Down Expand Up @@ -255,6 +261,7 @@ class VisualEditorBlock extends wp.element.Component {
return (
<div
ref={ this.bindBlockNode }
onClick={ this.stopClickPropagation }
onKeyDown={ this.removeOrDeselect }
onFocus={ this.onFocus }
onMouseMove={ this.maybeHover }
Expand Down
35 changes: 20 additions & 15 deletions editor/modes/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { connect } from 'react-redux';
* WordPress dependencies
*/
import { __ } from 'i18n';
import { Component, findDOMNode } from 'element';
import { Component } from 'element';

/**
* Internal dependencies
Expand All @@ -21,23 +21,30 @@ import { clearSelectedBlock } from '../../actions';
class VisualEditor extends Component {
constructor() {
super( ...arguments );
this.bindContainer = this.bindContainer.bind( this );
this.bindBlocksContainer = this.bindBlocksContainer.bind( this );
this.onClick = this.onClick.bind( this );

this.clearSelectedOnFocusOut = this.clearSelectedOnFocusOut.bind( this );
}

bindContainer( ref ) {
this.container = ref;
componentDidMount() {
// If a click occurs outside editor layout while in visual mode, treat
// as intent to clear selected block. This preserves selection when
// focus moves from block to post settings, but clears when moving from
// block to the admin bar, sidebar, or below page content.
this.content = document.getElementById( 'wpbody-content' );
this.content.addEventListener( 'focusout', this.clearSelectedOnFocusOut );
}

bindBlocksContainer( ref ) {
this.blocksContainer = findDOMNode( ref );
componentWillUnmount() {
this.content.removeEventListener( 'focusout', this.clearSelectedOnFocusOut );
delete this.content;
}

onClick( event ) {
if ( event.target === this.container || event.target === this.blocksContainer ) {
this.props.clearSelectedBlock();
clearSelectedOnFocusOut( event ) {
if ( this.content.contains( event.relatedTarget ) ) {
return;
}

this.props.clearSelectedBlock();
}

render() {
Expand All @@ -48,12 +55,10 @@ class VisualEditor extends Component {
role="region"
aria-label={ __( 'Visual Editor' ) }
className="editor-visual-editor"
onMouseDown={ this.onClick }
onTouchStart={ this.onClick }
ref={ this.bindContainer }
onClick={ this.props.clearSelectedBlock }
>
<PostTitle />
<VisualEditorBlockList ref={ this.bindBlocksContainer } />
<VisualEditorBlockList />
<Inserter position="top right" />
</div>
);
Expand Down