Skip to content

Commit

Permalink
State: Override state on edits to same post property
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Mar 10, 2018
1 parent 4e95843 commit 871a36e
Show file tree
Hide file tree
Showing 2 changed files with 274 additions and 15 deletions.
75 changes: 60 additions & 15 deletions editor/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
keys,
isEqual,
includes,
overSome,
} from 'lodash';

/**
Expand Down Expand Up @@ -98,28 +99,72 @@ function getFlattenedBlocks( blocks ) {
}

/**
* Option for the history reducer. When the block ID and updated attirbute keys
* are the same as previously, the history reducer should overwrite its present
* state.
* Returns true if the two object arguments have the same keys, or false
* otherwise.
*
* @param {Object} action The currently dispatched action.
* @param {Object} previousAction The previously dispatched action.
* @param {Object} a First object.
* @param {Object} b Second object.
*
* @return {boolean} Whether or not to overwrite present state.
* @return {boolean} Whether the two objects have the same keys.
*/
function shouldOverwriteState( action, previousAction ) {
if (
previousAction &&
export function hasSameKeys( a, b ) {
return isEqual( keys( a ), keys( b ) );
}

/**
* Returns true if, given the currently dispatching action and the previously
* dispatched action, the two actions are updating the same block attribute, or
* false otherwise.
*
* @param {Object} action Currently dispatching action.
* @param {Object} previousAction Previously dispatched action.
*
* @return {boolean} Whether actions are updating the same block attribute.
*/
export function isUpdatingSameBlockAttribute( action, previousAction ) {
return (
action.type === 'UPDATE_BLOCK_ATTRIBUTES' &&
action.type === previousAction.type
) {
const attributes = keys( action.attributes );
const previousAttributes = keys( previousAction.attributes );
action.uid === previousAction.uid &&
hasSameKeys( action.attributes, previousAction.attributes )
);
}

return action.uid === previousAction.uid && isEqual( attributes, previousAttributes );
/**
* Returns true if, given the currently dispatching action and the previously
* dispatched action, the two actions are editing the same post property, or
* false otherwise.
*
* @param {Object} action Currently dispatching action.
* @param {Object} previousAction Previously dispatched action.
*
* @return {boolean} Whether actions are updating the same post property.
*/
export function isUpdatingSamePostProperty( action, previousAction ) {
return (
action.type === 'EDIT_POST' &&
hasSameKeys( action.edits, previousAction.edits )
);
}

/**
* Returns true if, given the currently dispatching action and the previously
* dispatched action, the two actions are modifying the same property such that
* undo history should be batched.
*
* @param {Object} action Currently dispatching action.
* @param {Object} previousAction Previously dispatched action.
*
* @return {boolean} Whether to overwrite present state.
*/
export function shouldOverwriteState( action, previousAction ) {
if ( ! previousAction || action.type !== previousAction.type ) {
return false;
}

return false;
return overSome( [
isUpdatingSameBlockAttribute,
isUpdatingSamePostProperty,
] )( action, previousAction );
}

/**
Expand Down
214 changes: 214 additions & 0 deletions editor/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import {
* Internal dependencies
*/
import {
hasSameKeys,
isUpdatingSameBlockAttribute,
isUpdatingSamePostProperty,
shouldOverwriteState,
getPostRawValue,
editor,
currentPost,
Expand All @@ -33,6 +37,216 @@ import {
} from '../reducer';

describe( 'state', () => {
describe( 'hasSameKeys()', () => {
it( 'returns false if two objects do not have the same keys', () => {
const a = { foo: 10 };
const b = { bar: 10 };

expect( hasSameKeys( a, b ) ).toBe( false );
} );

it( 'returns false if two objects have the same keys', () => {
const a = { foo: 10 };
const b = { foo: 20 };

expect( hasSameKeys( a, b ) ).toBe( true );
} );
} );

describe( 'isUpdatingSameBlockAttribute()', () => {
it( 'should return false if not updating block attributes', () => {
const action = {
type: 'EDIT_POST',
edits: {},
};
const previousAction = {
type: 'EDIT_POST',
edits: {},
};

expect( isUpdatingSameBlockAttribute( action, previousAction ) ).toBe( false );
} );

it( 'should return false if not updating the same block', () => {
const action = {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: '9db792c6-a25a-495d-adbd-97d56a4c4189',
attributes: {
foo: 10,
},
};
const previousAction = {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1',
attributes: {
foo: 20,
},
};

expect( isUpdatingSameBlockAttribute( action, previousAction ) ).toBe( false );
} );

it( 'should return false if not updating the same block attributes', () => {
const action = {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: '9db792c6-a25a-495d-adbd-97d56a4c4189',
attributes: {
foo: 10,
},
};
const previousAction = {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: '9db792c6-a25a-495d-adbd-97d56a4c4189',
attributes: {
bar: 20,
},
};

expect( isUpdatingSameBlockAttribute( action, previousAction ) ).toBe( false );
} );

it( 'should return true if updating the same block attributes', () => {
const action = {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: '9db792c6-a25a-495d-adbd-97d56a4c4189',
attributes: {
foo: 10,
},
};
const previousAction = {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: '9db792c6-a25a-495d-adbd-97d56a4c4189',
attributes: {
foo: 20,
},
};

expect( isUpdatingSameBlockAttribute( action, previousAction ) ).toBe( true );
} );
} );

describe( 'isUpdatingSamePostProperty()', () => {
it( 'should return false if not editing post', () => {
const action = {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1',
attributes: {
foo: 10,
},
};
const previousAction = {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1',
attributes: {
foo: 10,
},
};

expect( isUpdatingSamePostProperty( action, previousAction ) ).toBe( false );
} );

it( 'should return false if not editing the same post properties', () => {
const action = {
type: 'EDIT_POST',
edits: {
foo: 10,
},
};
const previousAction = {
type: 'EDIT_POST',
edits: {
bar: 20,
},
};

expect( isUpdatingSamePostProperty( action, previousAction ) ).toBe( false );
} );

it( 'should return true if updating the same post properties', () => {
const action = {
type: 'EDIT_POST',
edits: {
foo: 10,
},
};
const previousAction = {
type: 'EDIT_POST',
edits: {
foo: 20,
},
};

expect( isUpdatingSamePostProperty( action, previousAction ) ).toBe( true );
} );
} );

describe( 'shouldOverwriteState()', () => {
it( 'should return false if no previous action', () => {
const action = {
type: 'EDIT_POST',
edits: {
foo: 10,
},
};
const previousAction = undefined;

expect( shouldOverwriteState( action, previousAction ) ).toBe( false );
} );

it( 'should return false if the action types are different', () => {
const action = {
type: 'EDIT_POST',
edits: {
foo: 10,
},
};
const previousAction = {
type: 'EDIT_DIFFERENT_POST',
edits: {
foo: 20,
},
};

expect( shouldOverwriteState( action, previousAction ) ).toBe( false );
} );

it( 'should return true if updating same block attribute', () => {
const action = {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: '9db792c6-a25a-495d-adbd-97d56a4c4189',
attributes: {
foo: 10,
},
};
const previousAction = {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: '9db792c6-a25a-495d-adbd-97d56a4c4189',
attributes: {
foo: 20,
},
};

expect( shouldOverwriteState( action, previousAction ) ).toBe( true );
} );

it( 'should return true if updating same post property', () => {
const action = {
type: 'EDIT_POST',
edits: {
foo: 10,
},
};
const previousAction = {
type: 'EDIT_POST',
edits: {
foo: 20,
},
};

expect( shouldOverwriteState( action, previousAction ) ).toBe( true );
} );
} );

describe( 'getPostRawValue', () => {
it( 'returns original value for non-rendered content', () => {
const value = getPostRawValue( '' );
Expand Down

0 comments on commit 871a36e

Please sign in to comment.