From 692ecdafa7cb3e2b70d6836ed0f66ee73dee51de Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 21 Apr 2016 15:53:35 -0400 Subject: [PATCH] Editor: Set selection range after timeout Otherwise, IE11 sets selection to end of field --- client/post-editor/editor-title/index.jsx | 25 +++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/client/post-editor/editor-title/index.jsx b/client/post-editor/editor-title/index.jsx index 584d52a787a234..6b1330d663989c 100644 --- a/client/post-editor/editor-title/index.jsx +++ b/client/post-editor/editor-title/index.jsx @@ -56,12 +56,26 @@ const EditorTitle = React.createClass( { // If value updated via paste, restore caret location if ( this.selectionStart ) { - const input = ReactDom.findDOMNode( this.refs.titleInput ); - input.setSelectionRange( this.selectionStart, this.selectionStart ); - delete this.selectionStart; + this.setSelectionTimeout = setTimeout( this.setSelection, 0 ); } }, + componentWillUnmount() { + clearTimeout( this.setSelectionTimeout ); + delete this.setSelectionTimeout; + delete this.selectionStart; + }, + + setSelection() { + if ( ! this.selectionStart ) { + return; + } + + const input = ReactDom.findDOMNode( this.refs.titleInput ); + input.setSelectionRange( this.selectionStart, this.selectionStart ); + delete this.selectionStart; + }, + setTitle( title ) { // TODO: REDUX - remove flux actions when whole post-editor is reduxified PostActions.edit( { title } ); @@ -111,8 +125,11 @@ const EditorTitle = React.createClass( { valueChars.splice( selectionStart, selectionEnd - selectionStart, text ); const newTitle = valueChars.join( '' ); - this.setTitle( newTitle ); + // To preserve caret location, we track intended selection point to be + // restored after next render pass this.selectionStart = selectionStart + text.length; + + this.setTitle( newTitle ); }, preventEnterKeyPress( event ) {