Skip to content

Commit

Permalink
Fix undo after pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Mar 5, 2019
1 parent 8c82d3e commit a6f8f5d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
21 changes: 14 additions & 7 deletions packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ export class RichText extends Component {
this.savedContent = value;
this.patterns = getPatterns( {
onReplace,
onCreateUndoLevel: this.onCreateUndoLevel,
valueToFormat: this.valueToFormat,
onChange: this.onChange,
} );
this.enterPatterns = getBlockTransforms( 'from' )
.filter( ( { type } ) => type === 'enter' );
Expand Down Expand Up @@ -395,10 +393,7 @@ export class RichText extends Component {
}

let { selectedFormat } = this.state;
const { formats, text, start, end } = this.patterns.reduce(
( accumlator, transform ) => transform( accumlator ),
this.createRecord()
);
const { formats, text, start, end } = this.createRecord();

if ( this.formatPlaceholder ) {
formats[ this.state.start ] = formats[ this.state.start ] || [];
Expand All @@ -420,10 +415,22 @@ export class RichText extends Component {
delete formats[ this.state.start ];
}

this.onChange( { formats, text, start, end, selectedFormat }, {
const change = { formats, text, start, end, selectedFormat };

this.onChange( change, {
withoutHistory: true,
} );

const transformed = this.patterns.reduce(
( accumlator, transform ) => transform( accumlator ),
change
);

if ( transformed !== change ) {
this.onCreateUndoLevel();
this.onChange( { ...transformed, selectedFormat } );
}

// Create an undo level when input stops for over a second.
this.props.clearTimeout( this.onInput.timeout );
this.onInput.timeout = this.props.setTimeout( this.onCreateUndoLevel, 1000 );
Expand Down
5 changes: 1 addition & 4 deletions packages/editor/src/components/rich-text/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
slice,
} from '@wordpress/rich-text';

export function getPatterns( { onReplace, valueToFormat, onCreateUndoLevel, onChange } ) {
export function getPatterns( { onReplace, valueToFormat } ) {
const prefixTransforms = getBlockTransforms( 'from' )
.filter( ( { type } ) => type === 'prefix' );

Expand Down Expand Up @@ -40,7 +40,6 @@ export function getPatterns( { onReplace, valueToFormat, onCreateUndoLevel, onCh
const content = valueToFormat( slice( record, start, text.length ) );
const block = transformation.transform( content );

onCreateUndoLevel();
onReplace( [ block ] );

return record;
Expand Down Expand Up @@ -70,8 +69,6 @@ export function getPatterns( { onReplace, valueToFormat, onCreateUndoLevel, onCh
return record;
}

onChange( record );

record = remove( record, startIndex, startIndex + 1 );
record = remove( record, endIndex, endIndex + 1 );
record = applyFormat( record, { type: 'code' }, startIndex, endIndex );
Expand Down

0 comments on commit a6f8f5d

Please sign in to comment.