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

Store: Remove redundant handling of inserter position #4765

Merged
merged 2 commits into from
Jan 31, 2018
Merged
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
7 changes: 2 additions & 5 deletions editor/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ class Inserter extends Component {
}

onToggle( isOpen ) {
const {
insertIndex,
onToggle,
} = this.props;
const { onToggle } = this.props;

if ( isOpen ) {
this.props.showInsertionPoint( insertIndex );
this.props.showInsertionPoint();
} else {
this.props.hideInsertionPoint();
}
Expand Down
8 changes: 3 additions & 5 deletions editor/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,14 @@ export function insertBlocks( blocks, position ) {
}

/**
* Returns an action object showing the insertion point at a given index.
*
* @param {Number?} index Index of the insertion point.
* Returns an action object used in signalling that the insertion point should
* be shown.
*
* @returns {Object} Action object.
*/
export function showInsertionPoint( index ) {
export function showInsertionPoint() {
return {
type: 'SHOW_INSERTION_POINT',
index,
};
}

Expand Down
11 changes: 6 additions & 5 deletions editor/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,20 +497,21 @@ export function blocksMode( state = {}, action ) {
}

/**
* Reducer returning the block insertion point.
* Reducer returning the block insertion point visibility, a boolean value
* reflecting whether the insertion point should be shown.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @returns {Object} Updated state.
*/
export function blockInsertionPoint( state = {}, action ) {
export function isInsertionPointVisible( state = false, action ) {
switch ( action.type ) {
case 'SHOW_INSERTION_POINT':
return { ...state, visible: true, position: action.index };
return true;

case 'HIDE_INSERTION_POINT':
return { ...state, visible: false, position: null };
return false;
}

return state;
Expand Down Expand Up @@ -789,7 +790,7 @@ export default optimist( combineReducers( {
blockSelection,
hoveredBlock,
blocksMode,
blockInsertionPoint,
isInsertionPointVisible,
preferences,
saving,
notices,
Expand Down
24 changes: 1 addition & 23 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,11 +830,6 @@ export function isTyping( state ) {
* @returns {?String} Unique ID after which insertion will occur.
*/
export function getBlockInsertionPoint( state ) {
const position = getBlockSiblingInserterPosition( state );
if ( null !== position ) {
return position;
}

const lastMultiSelectedBlock = getLastMultiSelectedBlockUid( state );
if ( lastMultiSelectedBlock ) {
return getBlockIndex( state, lastMultiSelectedBlock ) + 1;
Expand All @@ -848,23 +843,6 @@ export function getBlockInsertionPoint( state ) {
return state.editor.present.blockOrder.length;
}

/**
* Returns the position at which the block inserter will insert a new adjacent
* sibling block, or null if the inserter is not actively visible.
*
* @param {Object} state Global application state.
*
* @returns {?Number} Whether the inserter is currently visible.
*/
export function getBlockSiblingInserterPosition( state ) {
const { position } = state.blockInsertionPoint;
if ( ! Number.isInteger( position ) ) {
return null;
}

return position;
}

/**
* Returns true if we should show the block insertion point.
*
Expand All @@ -873,7 +851,7 @@ export function getBlockSiblingInserterPosition( state ) {
* @returns {?Boolean} Whether the insertion point is visible or not.
*/
export function isBlockInsertionPointVisible( state ) {
return !! state.blockInsertionPoint.visible;
return state.isInsertionPointVisible;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions editor/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,8 @@ describe( 'actions', () => {

describe( 'showInsertionPoint', () => {
it( 'should return the SHOW_INSERTION_POINT action', () => {
expect( showInsertionPoint( 1 ) ).toEqual( {
expect( showInsertionPoint() ).toEqual( {
type: 'SHOW_INSERTION_POINT',
index: 1,
} );
} );
} );
Expand Down
24 changes: 10 additions & 14 deletions editor/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
saving,
notices,
blocksMode,
blockInsertionPoint,
isInsertionPointVisible,
isSavingMetaBoxes,
metaBoxes,
reusableBlocks,
Expand Down Expand Up @@ -721,31 +721,27 @@ describe( 'state', () => {
} );
} );

describe( 'blockInsertionPoint', () => {
it( 'should default to an empty object', () => {
const state = blockInsertionPoint( undefined, {} );
describe( 'isInsertionPointVisible', () => {
it( 'should default to false', () => {
const state = isInsertionPointVisible( undefined, {} );

expect( state ).toEqual( {} );
expect( state ).toBe( false );
} );

it( 'should set insertion point position', () => {
const state = blockInsertionPoint( undefined, {
it( 'should set insertion point visible', () => {
const state = isInsertionPointVisible( false, {
type: 'SHOW_INSERTION_POINT',
index: 5,
} );

expect( state ).toEqual( {
position: 5,
visible: true,
} );
expect( state ).toBe( true );
} );

it( 'should clear the insertion point', () => {
const state = blockInsertionPoint( deepFreeze( {} ), {
const state = isInsertionPointVisible( true, {
type: 'HIDE_INSERTION_POINT',
} );

expect( state ).toEqual( { visible: false, position: null } );
expect( state ).toBe( false );
} );
} );

Expand Down
48 changes: 4 additions & 44 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import {
getBlockMode,
isTyping,
getBlockInsertionPoint,
getBlockSiblingInserterPosition,
isBlockInsertionPointVisible,
isSavingPost,
didPostSaveRequestSucceed,
Expand Down Expand Up @@ -1585,24 +1584,7 @@ describe( 'selectors', () => {
edits: {},
},
},
blockInsertionPoint: {},
};

expect( getBlockInsertionPoint( state ) ).toBe( 2 );
} );

it( 'should return the assigned insertion point', () => {
const state = {
preferences: { mode: 'visual' },
blockSelection: {},
editor: {
present: {
blockOrder: [ 1, 2, 3 ],
},
},
blockInsertionPoint: {
position: 2,
},
isInsertionPointVisible: false,
};

expect( getBlockInsertionPoint( state ) ).toBe( 2 );
Expand All @@ -1620,7 +1602,7 @@ describe( 'selectors', () => {
blockOrder: [ 1, 2, 3 ],
},
},
blockInsertionPoint: {},
isInsertionPointVisible: false,
};

expect( getBlockInsertionPoint( state ) ).toBe( 2 );
Expand All @@ -1635,39 +1617,17 @@ describe( 'selectors', () => {
blockOrder: [ 1, 2, 3 ],
},
},
blockInsertionPoint: {},
isInsertionPointVisible: false,
};

expect( getBlockInsertionPoint( state ) ).toBe( 3 );
} );
} );

describe( 'getBlockSiblingInserterPosition', () => {
it( 'should return null if no sibling insertion point', () => {
const state = {
blockInsertionPoint: {},
};

expect( getBlockSiblingInserterPosition( state ) ).toBe( null );
} );

it( 'should return sibling insertion point', () => {
const state = {
blockInsertionPoint: {
position: 5,
},
};

expect( getBlockSiblingInserterPosition( state ) ).toBe( 5 );
} );
} );

describe( 'isBlockInsertionPointVisible', () => {
it( 'should return the value in state', () => {
const state = {
blockInsertionPoint: {
visible: true,
},
isInsertionPointVisible: true,
};

expect( isBlockInsertionPointVisible( state ) ).toBe( true );
Expand Down