From 2896b2e24a4d6a3dc8d110edef87823de0d7cc08 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Fri, 23 Mar 2018 15:08:01 +1100 Subject: [PATCH 1/3] Prevent changes from being lost while in Text Mode Store the value of the PostTextEditor text area in local state. This prevents text from being lost if the post is autosaved during editing. --- editor/components/post-text-editor/index.js | 42 +++++++++++---------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/editor/components/post-text-editor/index.js b/editor/components/post-text-editor/index.js index 5061421b308e9e..bf17d9b1729603 100644 --- a/editor/components/post-text-editor/index.js +++ b/editor/components/post-text-editor/index.js @@ -1,8 +1,8 @@ /** * External dependencies */ -import { connect } from 'react-redux'; import Textarea from 'react-autosize-textarea'; +import { connect } from 'react-redux'; /** * WordPress dependencies @@ -18,41 +18,45 @@ import { getEditedPostContent } from '../../store/selectors'; import { editPost, resetBlocks, checkTemplateValidity } from '../../store/actions'; class PostTextEditor extends Component { - constructor( props ) { + constructor() { super( ...arguments ); - this.onChange = this.onChange.bind( this ); - this.onPersist = this.onPersist.bind( this ); + this.handleFocus = this.handleFocus.bind( this ); + this.handleChange = this.handleChange.bind( this ); + this.handleBlur = this.handleBlur.bind( this ); this.state = { - initialValue: props.value, + value: null, + isDirty: false, }; } - onChange( event ) { - this.props.onChange( event.target.value ); + handleFocus() { + this.setState( { value: this.props.value } ); } - onPersist( event ) { - const { value } = event.target; - if ( value !== this.state.initialValue ) { - this.props.onPersist( value ); + handleChange( event ) { + const value = event.target.value; + this.props.onChange( value ); + this.setState( { value, isDirty: true } ); + } - this.setState( { - initialValue: value, - } ); + handleBlur() { + if ( this.state.isDirty ) { + this.props.onPersist( this.state.value ); } + + this.setState( { value: null, isDirty: false } ); } render() { - const { value } = this.props; - return (