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

History: Batch title and text editor edit undo history #4008

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 15 additions & 1 deletion editor/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,24 @@ export function clearBlockInsertionPoint() {
};
}

export function editPost( edits ) {
/**
* Returns an action object used in signalling that the post has been edited.
*
* @param {Object} edits Key-value pairs of edited post attributes
* @param {?Object} options Optional object of options, currently supporting
* batch flag (for grouping sequential edits)
* @return {Object} Action object
*/
export function editPost( edits, options ) {
let meta;
if ( options && options.batch ) {
meta = { batchHistory: true };
}

return {
type: 'EDIT_POST',
edits,
meta,
};
}

Expand Down
2 changes: 1 addition & 1 deletion editor/components/post-text-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default connect(
} ),
{
onChange( content ) {
return editPost( { content } );
return editPost( { content }, { batch: true } );
},
onPersist( content ) {
return resetBlocks( parse( content ) );
Expand Down
2 changes: 1 addition & 1 deletion editor/components/post-title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default connect(
return insertBlock( createBlock( getDefaultBlockName() ), 0 );
},
onUpdate( title ) {
return editPost( { title } );
return editPost( { title }, { batch: true } );
},
clearSelectedBlock,
}
Expand Down
20 changes: 18 additions & 2 deletions editor/utils/with-history/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { includes } from 'lodash';
import { includes, get } from 'lodash';

/**
* Reducer enhancer which transforms the result of the original reducer into an
Expand All @@ -19,6 +19,8 @@ export default function withHistory( reducer, options = {} ) {
future: [],
};

let isBatching = false;

return ( state = initialState, action ) => {
const { past, present, future } = state;

Expand Down Expand Up @@ -62,8 +64,22 @@ export default function withHistory( reducer, options = {} ) {
return state;
}

const nextPast = [ ...past, present ];

// If batching, prevent accumulating past until the next action which
// doesn't include the batch flag.
if ( get( action.meta, 'batchHistory' ) ) {
if ( isBatching ) {
nextPast.splice( -1, 1 );
}

isBatching = true;
} else {
isBatching = false;
}

return {
past: [ ...past, present ],
past: nextPast,
present: nextPresent,
future: [],
};
Expand Down
49 changes: 49 additions & 0 deletions editor/utils/with-history/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,53 @@ describe( 'withHistory', () => {

expect( state ).toBe( original );
} );

it( 'should allow history batching', () => {
const reducer = withHistory( counter );

let state;
state = reducer( undefined, {} );

state = reducer( state, { type: 'INCREMENT', meta: { batchHistory: true } } );
expect( state ).toEqual( {
past: [ 0 ],
present: 1,
future: [],
} );

state = reducer( state, { type: 'INCREMENT', meta: { batchHistory: true } } );
expect( state ).toEqual( {
past: [ 0 ],
present: 2,
future: [],
} );

state = reducer( state, { type: 'INCREMENT' } );
expect( state ).toEqual( {
past: [ 0, 2 ],
present: 3,
future: [],
} );

state = reducer( state, { type: 'INCREMENT', meta: { batchHistory: true } } );
expect( state ).toEqual( {
past: [ 0, 2, 3 ],
present: 4,
future: [],
} );

state = reducer( state, { type: 'INCREMENT', meta: { batchHistory: true } } );
expect( state ).toEqual( {
past: [ 0, 2, 3 ],
present: 5,
future: [],
} );

state = reducer( state, { type: 'UNDO' } );
expect( state ).toEqual( {
past: [ 0, 2 ],
present: 3,
future: [ 5 ],
} );
} );
} );