Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RichText: Fix undo after pattern #13917

Merged
merged 2 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
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
Next Next commit
Fix undo after pattern
  • Loading branch information
ellatrix committed Mar 5, 2019
commit a6f8f5d0d4d1a40d59196782e1d83eb698441be6
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