From c08c0b588b10e4de1864afed08ef39bc95d54480 Mon Sep 17 00:00:00 2001 From: iseulde Date: Sat, 30 Mar 2019 18:42:55 +0100 Subject: [PATCH] Clean up --- .../src/components/rich-text/index.js | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/index.js b/packages/block-editor/src/components/rich-text/index.js index 74f9e9358043cd..26e906f0ae91ad 100644 --- a/packages/block-editor/src/components/rich-text/index.js +++ b/packages/block-editor/src/components/rich-text/index.js @@ -928,30 +928,23 @@ export class RichText extends Component { const { tagName, value, isSelected } = this.props; const record = this.getRecord(); - if ( + // Check if the content changed. + let shouldReapply = ( tagName === prevProps.tagName && value !== prevProps.value && value !== this.savedContent - ) { - if ( ! isSelected ) { - delete record.start; - delete record.end; - } - - this.applyRecord( record ); - } else if ( isSelected && ! prevProps.isSelected && ( - this.savedSelectionStart !== record.start || - this.savedSelectionEnd !== record.end - ) ) { - this.applyRecord( record ); - } + ); - this.savedContent = value; - this.savedSelectionStart = record.start; - this.savedSelectionEnd = record.end; + // Check if the selection changed. + shouldReapply = shouldReapply || ( + isSelected && ! prevProps.isSelected && ( + this.savedSelectionStart !== record.start || + this.savedSelectionEnd !== record.end + ) + ); - // If any format props update, reapply value. - const shouldReapply = Object.keys( this.props ).some( ( name ) => { + // Check if any format props changed. + shouldReapply = shouldReapply || Object.keys( this.props ).some( ( name ) => { if ( name.indexOf( 'format_' ) !== 0 ) { return false; } @@ -974,6 +967,10 @@ export class RichText extends Component { this.applyRecord( record ); } + + this.savedContent = value; + this.savedSelectionStart = record.start; + this.savedSelectionEnd = record.end; } /**