Skip to content

Commit

Permalink
Editable: Avoid unnecessary updateContent calls
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 1, 2017
1 parent d95c03c commit 473563a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions blocks/components/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ export default class Editable extends wp.element.Component {
return;
}

this.savedContent = this.getContent();
this.editor.save();
this.props.onChange( this.getContent() );
this.props.onChange( this.savedContent );
}

isStartOfEditor() {
Expand Down Expand Up @@ -216,7 +217,8 @@ export default class Editable extends wp.element.Component {

updateContent() {
const bookmark = this.editor.selection.getBookmark( 2, true );
this.setContent( this.props.value );
this.savedContent = this.props.value;
this.setContent( this.savedContent );
this.editor.selection.moveToBookmark( bookmark );
// Saving the editor on updates avoid unecessary onChanges calls
// These calls can make the focus jump
Expand Down Expand Up @@ -266,10 +268,13 @@ export default class Editable extends wp.element.Component {
this.focus();
}

// The savedContent var allows us to avoid updating the content right after an onChange call
if (
this.props.tagName === prevProps.tagName &&
this.props.value !== prevProps.value &&
! isEqual( this.props.value, prevProps.value )
this.props.value !== this.savedContent &&
! isEqual( this.props.value, prevProps.value ) &&
! isEqual( this.props.value, this.savedContent )
) {
this.updateContent();
}
Expand Down

0 comments on commit 473563a

Please sign in to comment.