Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Editable: Avoid unnecessary updateContent calls
  • Loading branch information
youknowriad committed May 1, 2017
commit 473563af5185c741c68e962b36994db66f8970a3
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow this. Why does the value of this.props.value differ from prevProps.value after the onChange? And if it does, why do we not want this to be reflected in the editable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you make a change on the editable. Say the old value is a and the new one is ab, we don't want to trigger the updateContent even if the old value prop and the new one are different. That's what I'm trying to achieve here by caching the savedContent.

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