Skip to content

Commit

Permalink
Trigger typing stopped when mouse moves anywhere on page
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jun 13, 2017
1 parent 0d28bfc commit 6d4f092
Showing 1 changed file with 51 additions and 31 deletions.
82 changes: 51 additions & 31 deletions editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ class VisualEditorBlock extends wp.element.Component {
this.setAttributes = this.setAttributes.bind( this );
this.maybeHover = this.maybeHover.bind( this );
this.maybeStartTyping = this.maybeStartTyping.bind( this );
this.stopTyping = this.stopTyping.bind( this );
this.removeOrDeselect = this.removeOrDeselect.bind( this );
this.mergeBlocks = this.mergeBlocks.bind( this );
this.onFocus = this.onFocus.bind( this );
this.onPointerDown = this.onPointerDown.bind( this );
this.previousOffset = null;
}

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

componentWillReceiveProps( newProps ) {
Expand All @@ -74,6 +76,45 @@ class VisualEditorBlock extends wp.element.Component {
}
}

componentDidUpdate( prevProps ) {
// Preserve scroll prosition when block rearranged
if ( this.previousOffset ) {
window.scrollTo(
window.scrollX,
window.scrollY + this.node.getBoundingClientRect().top - this.previousOffset
);
this.previousOffset = null;
}

// Focus node when focus state is programmatically transferred.
if ( this.props.focus && ! prevProps.focus ) {
this.node.focus();
}

// Bind or unbind mousemove from page when user starts or stops typing
const { isTyping } = this.props;
if ( isTyping !== prevProps.isTyping ) {
if ( isTyping ) {
document.addEventListener( 'mousemove', this.stopTyping );
} else {
this.removeStopTypingListener();
}
}
}

componentWillUnmount() {
this.removeStopTypingListener();
}

removeStopTypingListener() {
document.removeEventListener( 'mousemove', this.stopTyping );
}

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

setAttributes( attributes ) {
const { block, onChange } = this.props;
onChange( block.uid, {
Expand All @@ -85,17 +126,13 @@ class VisualEditorBlock extends wp.element.Component {
}

maybeHover() {
const { isHovered, isSelected, isTyping, isMultiSelected, onStopTyping, onHover } = this.props;
const { isHovered, isSelected, isMultiSelected, onHover } = this.props;

if ( isMultiSelected ) {
if ( isHovered || isSelected || isMultiSelected ) {
return;
}

if ( isSelected && isTyping ) {
onStopTyping();
} else if ( ! isSelected && ! isHovered ) {
onHover();
}
onHover();
}

maybeStartTyping() {
Expand All @@ -110,6 +147,10 @@ class VisualEditorBlock extends wp.element.Component {
}
}

stopTyping() {
this.props.onStopTyping();
}

removeOrDeselect( { keyCode, target } ) {
const {
uid,
Expand Down Expand Up @@ -159,27 +200,6 @@ class VisualEditorBlock extends wp.element.Component {
}
}

componentDidUpdate( prevProps ) {
if ( this.previousOffset ) {
window.scrollTo(
window.scrollX,
window.scrollY + this.node.getBoundingClientRect().top - this.previousOffset
);
this.previousOffset = null;
}

// Focus node when focus state is programmatically transferred.
if ( this.props.focus && ! prevProps.focus ) {
this.node.focus();
}
}

componentDidMount() {
if ( this.props.focus ) {
this.node.focus();
}
}

onFocus( event ) {
if ( event.target === this.node ) {
this.props.onSelect();
Expand Down

0 comments on commit 6d4f092

Please sign in to comment.