From af1487efc137981a92324b4a9cd6f15de33a808b Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 14 Dec 2017 12:02:46 -0500 Subject: [PATCH 1/2] State: Enhance withHistory higher-order reducer to support batching --- editor/utils/with-history/index.js | 20 +++++++++- editor/utils/with-history/test/index.js | 49 +++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/editor/utils/with-history/index.js b/editor/utils/with-history/index.js index 7fa250d1319e7..e17b322798ca3 100644 --- a/editor/utils/with-history/index.js +++ b/editor/utils/with-history/index.js @@ -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 @@ -19,6 +19,8 @@ export default function withHistory( reducer, options = {} ) { future: [], }; + let isBatching = false; + return ( state = initialState, action ) => { const { past, present, future } = state; @@ -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: [], }; diff --git a/editor/utils/with-history/test/index.js b/editor/utils/with-history/test/index.js index d59dcfc39847f..8698e8407f899 100644 --- a/editor/utils/with-history/test/index.js +++ b/editor/utils/with-history/test/index.js @@ -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 ], + } ); + } ); } ); From 46eb5e23655d6d115df27621ee0dcdbe48cac416 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 14 Dec 2017 12:03:07 -0500 Subject: [PATCH 2/2] Edits: Batch title and text editor edits --- editor/actions.js | 16 +++++++++++++++- editor/components/post-text-editor/index.js | 2 +- editor/components/post-title/index.js | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/editor/actions.js b/editor/actions.js index 5a7733fc3cbf6..c6a25e39a7771 100644 --- a/editor/actions.js +++ b/editor/actions.js @@ -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, }; } diff --git a/editor/components/post-text-editor/index.js b/editor/components/post-text-editor/index.js index 5fa40bad6386f..3e080ee6c5d73 100644 --- a/editor/components/post-text-editor/index.js +++ b/editor/components/post-text-editor/index.js @@ -65,7 +65,7 @@ export default connect( } ), { onChange( content ) { - return editPost( { content } ); + return editPost( { content }, { batch: true } ); }, onPersist( content ) { return resetBlocks( parse( content ) ); diff --git a/editor/components/post-title/index.js b/editor/components/post-title/index.js index cd93582ec9a9a..b3d5fbb3c1e85 100644 --- a/editor/components/post-title/index.js +++ b/editor/components/post-title/index.js @@ -136,7 +136,7 @@ export default connect( return insertBlock( createBlock( getDefaultBlockName() ), 0 ); }, onUpdate( title ) { - return editPost( { title } ); + return editPost( { title }, { batch: true } ); }, clearSelectedBlock, }