Skip to content

Commit

Permalink
Able to not lose content
Browse files Browse the repository at this point in the history
  • Loading branch information
hypest committed Apr 23, 2019
1 parent 60c75b0 commit c7aa381
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/block-editor/src/components/rich-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ export class RichText extends Component {
// value. This also provides an opportunity for the parent component to
// determine whether the before/after value has changed using a trivial
// strict equality operation.
if ( isEmpty( after ) ) {
if ( isEmpty( after ) && before.text.length == currentRecord.text.length ) {
before = currentRecord;
} else if ( isEmpty( before ) ) {
} else if ( isEmpty( before ) && after.text.length == currentRecord.text.length ) {
after = currentRecord;
}

Expand Down Expand Up @@ -215,7 +215,7 @@ export class RichText extends Component {
this.onFormatChange( record, true );
}

onFormatChange( record, doUpdateChild ) {
onFormatChange( record, doUpdateChild = true ) {
let newContent;
// valueToFormat might throw when converting the record to a tree structure
// let's ignore the event for now and force a render update so we're still in sync
Expand All @@ -230,7 +230,7 @@ export class RichText extends Component {
} );
if ( newContent && newContent !== this.props.value ) {
this.props.onChange( newContent );
if ( record.needsSelectionUpdate && record.start && record.end ) {
if ( record.needsSelectionUpdate && record.start && record.end && doUpdateChild ) {
this.forceSelectionUpdate( record.start, record.end );
}
} else {
Expand Down Expand Up @@ -303,12 +303,12 @@ export class RichText extends Component {
this.setState( {
needsSelectionUpdate: false,
} );
this.onSplit( ...split( currentRecord ).map( this.valueToFormat ) );
this.splitContent( currentRecord );
} else {
if ( ! event.nativeEvent.firedAfterTextChanged ) {
// if ( ! event.nativeEvent.firedAfterTextChanged ) {
const insertedLineSeparator = { needsSelectionUpdate: true, ...insertLineSeparator( currentRecord ) };
this.onFormatChangeForceChild( insertedLineSeparator );
}
this.onFormatChange( insertedLineSeparator, ! event.nativeEvent.firedAfterTextChanged );
// }
}
} else if ( event.shiftKey || ! this.onSplit ) {
const insertedLineBreak = { needsSelectionUpdate: true, ...insert( currentRecord, '\n' ) };
Expand Down Expand Up @@ -589,12 +589,16 @@ export class RichText extends Component {
}

shouldComponentUpdate( nextProps ) {
if ( nextProps.tagName !== this.props.tagName || nextProps.isSelected !== this.props.isSelected ) {
if ( nextProps.tagName !== this.props.tagName ) {
this.lastEventCount = undefined;
this.lastContent = undefined;
return true;
}

if ( nextProps.isSelected !== this.props.isSelected ) {
return true;
}

// TODO: Please re-introduce the check to avoid updating the content right after an `onChange` call.
// It was removed in https://github.com/WordPress/gutenberg/pull/12417 to fix undo/redo problem.

Expand All @@ -603,7 +607,7 @@ export class RichText extends Component {
if ( ( typeof nextProps.value !== 'undefined' ) &&
( typeof this.lastContent !== 'undefined' ) &&
nextProps.value !== this.lastContent ) {
this.lastEventCount = undefined; // force a refresh on the native side
// this.lastEventCount = undefined; // force a refresh on the native side
}

return true;
Expand Down

0 comments on commit c7aa381

Please sign in to comment.