From a7f2b9782aa89332b9507f9084887989168720bb Mon Sep 17 00:00:00 2001 From: James Johnson Date: Wed, 14 Jun 2017 10:35:33 +1000 Subject: [PATCH] Update freeform block to apply focus/blur fix from 43b85f3cb2ed0ddd17781f54202b0fca78e51d27 --- blocks/library/freeform/freeform-block.js | 30 ++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/blocks/library/freeform/freeform-block.js b/blocks/library/freeform/freeform-block.js index b65725b2bc32c..d97de33c37d5a 100644 --- a/blocks/library/freeform/freeform-block.js +++ b/blocks/library/freeform/freeform-block.js @@ -92,6 +92,7 @@ export default class FreeformBlock extends wp.element.Component { this.onSelectionChange = this.onSelectionChange.bind( this ); this.onChange = this.onChange.bind( this ); this.onFocus = this.onFocus.bind( this ); + this.isEndOfEditor = this.isEndOfEditor.bind( this ); this.updateFocus = this.updateFocus.bind( this ); this.updateContent = this.updateContent.bind( this ); this.setContent = this.setContent.bind( this ); @@ -200,16 +201,39 @@ export default class FreeformBlock extends wp.element.Component { } } + isEndOfEditor() { + const range = this.editor.selection.getRng(); + if ( range.endOffset !== range.endContainer.textContent.length || ! range.collapsed ) { + return false; + } + const start = range.endContainer; + const body = this.editor.getBody(); + let element = start; + while ( element !== body ) { + const child = element; + element = element.parentNode; + if ( element.lastChild !== child ) { + return false; + } + } + return true; + } + updateFocus() { const { focus } = this.props; + const isActive = this.isActive(); + if ( focus ) { - this.editor.focus(); + if ( ! isActive ) { + this.editor.focus(); + } + // Offset = -1 means we should focus the end of the editable - if ( focus.offset === -1 ) { + if ( focus.offset === -1 && ! this.isEndOfEditor() ) { this.editor.selection.select( this.editor.getBody(), true ); this.editor.selection.collapse( false ); } - } else { + } else if ( isActive ) { this.editor.getBody().blur(); } }