From 5192c699dc2b4ff91d39d715ad3acf5202136f35 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Mon, 5 Mar 2018 16:26:06 -0500 Subject: [PATCH 1/5] Chrome: Refactor PostTitle for easier select, deselect --- editor/components/post-title/index.js | 74 ++++++------------------- editor/components/post-title/style.scss | 38 ++++--------- 2 files changed, 29 insertions(+), 83 deletions(-) diff --git a/editor/components/post-title/index.js b/editor/components/post-title/index.js index aad6417c3f2158..31e1362f7d44ae 100644 --- a/editor/components/post-title/index.js +++ b/editor/components/post-title/index.js @@ -12,7 +12,7 @@ import { __ } from '@wordpress/i18n'; import { Component, compose } from '@wordpress/element'; import { keycodes } from '@wordpress/utils'; import { createBlock, getDefaultBlockName } from '@wordpress/blocks'; -import { withContext } from '@wordpress/components'; +import { withContext, withFocusOutside } from '@wordpress/components'; /** * Internal dependencies @@ -32,47 +32,18 @@ class PostTitle extends Component { constructor() { super( ...arguments ); - this.bindContainer = this.bindNode.bind( this, 'container' ); - this.bindTextarea = this.bindNode.bind( this, 'textarea' ); this.onChange = this.onChange.bind( this ); this.onSelect = this.onSelect.bind( this ); this.onUnselect = this.onUnselect.bind( this ); - this.onSelectionChange = this.onSelectionChange.bind( this ); this.onKeyDown = this.onKeyDown.bind( this ); - this.blurIfOutside = this.blurIfOutside.bind( this ); - - this.nodes = {}; this.state = { isSelected: false, }; } - componentDidMount() { - document.addEventListener( 'selectionchange', this.onSelectionChange ); - } - - componentWillUnmount() { - document.removeEventListener( 'selectionchange', this.onSelectionChange ); - } - - bindNode( name, node ) { - this.nodes[ name ] = node; - } - - onSelectionChange() { - const textarea = this.nodes.textarea.textarea; - if ( - document.activeElement === textarea && - textarea.selectionStart !== textarea.selectionEnd - ) { - this.onSelect(); - } - } - - onChange( event ) { - const newTitle = event.target.value.replace( REGEXP_NEWLINES, ' ' ); - this.props.onUpdate( newTitle ); + handleFocusOutside() { + this.onUnselect(); } onSelect() { @@ -84,10 +55,9 @@ class PostTitle extends Component { this.setState( { isSelected: false } ); } - blurIfOutside( event ) { - if ( ! this.nodes.container.contains( event.relatedTarget ) ) { - this.onUnselect(); - } + onChange( event ) { + const newTitle = event.target.value.replace( REGEXP_NEWLINES, ' ' ); + this.props.onUpdate( newTitle ); } onKeyDown( event ) { @@ -103,26 +73,17 @@ class PostTitle extends Component { const className = classnames( 'editor-post-title', { 'is-selected': isSelected } ); return ( -
+
{ isSelected && } -
-