From 7315f2516f382a391d01817b5cf0728ac8efa319 Mon Sep 17 00:00:00 2001 From: iseulde Date: Fri, 5 Jul 2019 13:15:42 +0200 Subject: [PATCH] Move selection within blocks reducer --- packages/block-editor/src/store/reducer.js | 278 +++++++++--------- packages/block-editor/src/store/selectors.js | 48 +-- .../block-editor/src/store/test/effects.js | 36 ++- .../block-editor/src/store/test/reducer.js | 35 +++ .../block-editor/src/store/test/selectors.js | 155 +++++----- 5 files changed, 303 insertions(+), 249 deletions(-) diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index d9d0ecbdd28b64..fffd0fe1a5aa0a 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -191,6 +191,15 @@ function withPersistentBlockChange( reducer ) { return ( state, action ) => { let nextState = reducer( state, action ); + const selectionActions = new Set( [ 'SELECTION_CHANGE', 'TOGGLE_SELECTION', 'SELECT_BLOCK', 'MULTI_SELECT', 'START_MULTI_SELECT', 'STOP_MULTI_SELECT', 'CLEAR_SELECTED_BLOCK' ] ); + + if ( selectionActions.has( action.type ) ) { + return { + ...nextState, + isPersistentChange: false, + }; + } + const isExplicitPersistentChange = action.type === 'MARK_LAST_CHANGE_AS_PERSISTENT'; // Defer to previous state value (or default) unless changing or @@ -381,6 +390,139 @@ const withSaveReusableBlock = ( reducer ) => ( state, action ) => { return reducer( state, action ); }; +const BLOCK_SELECTION_EMPTY_OBJECT = {}; +const BLOCK_SELECTION_INITIAL_STATE = { + start: BLOCK_SELECTION_EMPTY_OBJECT, + end: BLOCK_SELECTION_EMPTY_OBJECT, + isMultiSelecting: false, + isEnabled: true, + initialPosition: null, +}; + +/** + * Reducer returning the block selection's state. + * + * @param {Object} state Current state. + * @param {Object} action Dispatched action. + * + * @return {Object} Updated state. + */ +export function blockSelection( state = BLOCK_SELECTION_INITIAL_STATE, action ) { + switch ( action.type ) { + case 'CLEAR_SELECTED_BLOCK': + return BLOCK_SELECTION_INITIAL_STATE; + case 'START_MULTI_SELECT': + if ( state.isMultiSelecting ) { + return state; + } + + return { + ...state, + isMultiSelecting: true, + initialPosition: null, + }; + case 'STOP_MULTI_SELECT': + if ( ! state.isMultiSelecting ) { + return state; + } + + return { + ...state, + isMultiSelecting: false, + initialPosition: null, + }; + case 'MULTI_SELECT': + return { + ...BLOCK_SELECTION_INITIAL_STATE, + isMultiSelecting: state.isMultiSelecting, + start: { clientId: action.start }, + end: { clientId: action.end }, + }; + case 'SELECT_BLOCK': + if ( + action.clientId === state.start.clientId && + action.clientId === state.end.clientId + ) { + return state; + } + + return { + ...BLOCK_SELECTION_INITIAL_STATE, + initialPosition: action.initialPosition, + start: { clientId: action.clientId }, + end: { clientId: action.clientId }, + }; + case 'REPLACE_INNER_BLOCKS': // REPLACE_INNER_BLOCKS and INSERT_BLOCKS should follow the same logic. + case 'INSERT_BLOCKS': { + if ( action.updateSelection ) { + return { + ...BLOCK_SELECTION_INITIAL_STATE, + start: { clientId: action.blocks[ 0 ].clientId }, + end: { clientId: action.blocks[ 0 ].clientId }, + }; + } + + return state; + } + case 'REMOVE_BLOCKS': + if ( + ! action.clientIds || + ! action.clientIds.length || + action.clientIds.indexOf( state.start.clientId ) === -1 + ) { + return state; + } + + return BLOCK_SELECTION_INITIAL_STATE; + case 'REPLACE_BLOCKS': { + if ( action.clientIds.indexOf( state.start.clientId ) === -1 ) { + return state; + } + + const indexToSelect = action.indexToSelect || action.blocks.length - 1; + const blockToSelect = action.blocks[ indexToSelect ]; + + if ( ! blockToSelect ) { + return BLOCK_SELECTION_INITIAL_STATE; + } + + if ( + blockToSelect.clientId === state.start.clientId && + blockToSelect.clientId === state.end.clientId + ) { + return state; + } + + return { + ...BLOCK_SELECTION_INITIAL_STATE, + start: { clientId: blockToSelect.clientId }, + end: { clientId: blockToSelect.clientId }, + }; + } + case 'TOGGLE_SELECTION': + return { + ...BLOCK_SELECTION_INITIAL_STATE, + isEnabled: action.isSelectionEnabled, + }; + case 'SELECTION_CHANGE': + return { + ...BLOCK_SELECTION_INITIAL_STATE, + start: { + clientId: action.clientId, + attributeKey: action.attributeKey, + offset: action.startOffset, + }, + end: { + clientId: action.clientId, + attributeKey: action.attributeKey, + offset: action.endOffset, + }, + }; + } + + return state; +} + /** * Reducer returning the blocks state. * @@ -656,6 +798,8 @@ export const blocks = flow( return state; }, + + selection: blockSelection, } ); /** @@ -698,139 +842,6 @@ export function isCaretWithinFormattedText( state = false, action ) { return state; } -const BLOCK_SELECTION_EMPTY_OBJECT = {}; -const BLOCK_SELECTION_INITIAL_STATE = { - start: BLOCK_SELECTION_EMPTY_OBJECT, - end: BLOCK_SELECTION_EMPTY_OBJECT, - isMultiSelecting: false, - isEnabled: true, - initialPosition: null, -}; - -/** - * Reducer returning the block selection's state. - * - * @param {Object} state Current state. - * @param {Object} action Dispatched action. - * - * @return {Object} Updated state. - */ -export function blockSelection( state = BLOCK_SELECTION_INITIAL_STATE, action ) { - switch ( action.type ) { - case 'CLEAR_SELECTED_BLOCK': - return BLOCK_SELECTION_INITIAL_STATE; - case 'START_MULTI_SELECT': - if ( state.isMultiSelecting ) { - return state; - } - - return { - ...state, - isMultiSelecting: true, - initialPosition: null, - }; - case 'STOP_MULTI_SELECT': - if ( ! state.isMultiSelecting ) { - return state; - } - - return { - ...state, - isMultiSelecting: false, - initialPosition: null, - }; - case 'MULTI_SELECT': - return { - ...BLOCK_SELECTION_INITIAL_STATE, - isMultiSelecting: state.isMultiSelecting, - start: { clientId: action.start }, - end: { clientId: action.end }, - }; - case 'SELECT_BLOCK': - if ( - action.clientId === state.start.clientId && - action.clientId === state.end.clientId - ) { - return state; - } - - return { - ...BLOCK_SELECTION_INITIAL_STATE, - initialPosition: action.initialPosition, - start: { clientId: action.clientId }, - end: { clientId: action.clientId }, - }; - case 'REPLACE_INNER_BLOCKS': // REPLACE_INNER_BLOCKS and INSERT_BLOCKS should follow the same logic. - case 'INSERT_BLOCKS': { - if ( action.updateSelection ) { - return { - ...BLOCK_SELECTION_INITIAL_STATE, - start: { clientId: action.blocks[ 0 ].clientId }, - end: { clientId: action.blocks[ 0 ].clientId }, - }; - } - - return state; - } - case 'REMOVE_BLOCKS': - if ( - ! action.clientIds || - ! action.clientIds.length || - action.clientIds.indexOf( state.start.clientId ) === -1 - ) { - return state; - } - - return BLOCK_SELECTION_INITIAL_STATE; - case 'REPLACE_BLOCKS': { - if ( action.clientIds.indexOf( state.start.clientId ) === -1 ) { - return state; - } - - const indexToSelect = action.indexToSelect || action.blocks.length - 1; - const blockToSelect = action.blocks[ indexToSelect ]; - - if ( ! blockToSelect ) { - return BLOCK_SELECTION_INITIAL_STATE; - } - - if ( - blockToSelect.clientId === state.start.clientId && - blockToSelect.clientId === state.end.clientId - ) { - return state; - } - - return { - ...BLOCK_SELECTION_INITIAL_STATE, - start: { clientId: blockToSelect.clientId }, - end: { clientId: blockToSelect.clientId }, - }; - } - case 'TOGGLE_SELECTION': - return { - ...BLOCK_SELECTION_INITIAL_STATE, - isEnabled: action.isSelectionEnabled, - }; - case 'SELECTION_CHANGE': - return { - ...BLOCK_SELECTION_INITIAL_STATE, - start: { - clientId: action.clientId, - attributeKey: action.attributeKey, - offset: action.startOffset, - }, - end: { - clientId: action.clientId, - attributeKey: action.attributeKey, - offset: action.endOffset, - }, - }; - } - - return state; -} - export function blocksMode( state = {}, action ) { if ( action.type === 'TOGGLE_BLOCK_MODE' ) { const { clientId } = action; @@ -987,7 +998,6 @@ export default combineReducers( { blocks, isTyping, isCaretWithinFormattedText, - blockSelection, blocksMode, blockListSettings, insertionPoint, diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 6633611fbd9b19..95dfd06fefa6e2 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -350,7 +350,7 @@ export function getBlockCount( state, rootClientId ) { * @return {WPBlockSelection} Selection start information. */ export function getSelectionStart( state ) { - return state.blockSelection.start; + return state.blocks.selection.start; } /** @@ -362,7 +362,7 @@ export function getSelectionStart( state ) { * @return {WPBlockSelection} Selection end information. */ export function getSelectionEnd( state ) { - return state.blockSelection.end; + return state.blocks.selection.end; } /** @@ -375,7 +375,7 @@ export function getSelectionEnd( state ) { * @return {?string} Client ID of block selection start. */ export function getBlockSelectionStart( state ) { - return state.blockSelection.start.clientId; + return state.blocks.selection.start.clientId; } /** @@ -388,7 +388,7 @@ export function getBlockSelectionStart( state ) { * @return {?string} Client ID of block selection end. */ export function getBlockSelectionEnd( state ) { - return state.blockSelection.end.clientId; + return state.blocks.selection.end.clientId; } /** @@ -405,7 +405,7 @@ export function getSelectedBlockCount( state ) { return multiSelectedBlockCount; } - return state.blockSelection.start.clientId ? 1 : 0; + return state.blocks.selection.start.clientId ? 1 : 0; } /** @@ -416,7 +416,7 @@ export function getSelectedBlockCount( state ) { * @return {boolean} Whether a single block is selected. */ export function hasSelectedBlock( state ) { - const { start, end } = state.blockSelection; + const { start, end } = state.blocks.selection; return !! start.clientId && start.clientId === end.clientId; } @@ -429,7 +429,7 @@ export function hasSelectedBlock( state ) { * @return {?string} Selected block client ID. */ export function getSelectedBlockClientId( state ) { - const { start, end } = state.blockSelection; + const { start, end } = state.blocks.selection; // We need to check the block exists because the current blockSelection // reducer doesn't take into account when blocks are reset via undo. To be // removed when that's fixed. @@ -598,13 +598,13 @@ export function getNextBlockClientId( state, startClientId ) { * @return {?Object} Selected block. */ export function getSelectedBlocksInitialCaretPosition( state ) { - const { start, end } = state.blockSelection; + const { start, end } = state.blocks.selection; if ( start.clientId !== end.clientId || ! start.clientId ) { return null; } - return state.blockSelection.initialPosition; + return state.blocks.selection.initialPosition; } /** @@ -616,7 +616,7 @@ export function getSelectedBlocksInitialCaretPosition( state ) { */ export const getSelectedBlockClientIds = createSelector( ( state ) => { - const { start, end } = state.blockSelection; + const { start, end } = state.blocks.selection; if ( start.clientId === undefined || end.clientId === undefined ) { return EMPTY_ARRAY; @@ -646,8 +646,8 @@ export const getSelectedBlockClientIds = createSelector( }, ( state ) => [ state.blocks.order, - state.blockSelection.start.clientId, - state.blockSelection.end.clientId, + state.blocks.selection.start.clientId, + state.blocks.selection.end.clientId, ], ); @@ -660,7 +660,7 @@ export const getSelectedBlockClientIds = createSelector( * @return {Array} Multi-selected block client IDs. */ export function getMultiSelectedBlockClientIds( state ) { - const { start, end } = state.blockSelection; + const { start, end } = state.blocks.selection; if ( start.clientId === end.clientId ) { return EMPTY_ARRAY; @@ -791,8 +791,8 @@ export const isAncestorMultiSelected = createSelector( }, ( state ) => [ state.blocks.order, - state.blockSelection.start.clientId, - state.blockSelection.end.clientId, + state.blocks.selection.start.clientId, + state.blocks.selection.end.clientId, ], ); /** @@ -808,7 +808,7 @@ export const isAncestorMultiSelected = createSelector( * @return {?string} Client ID of block beginning multi-selection. */ export function getMultiSelectedBlocksStartClientId( state ) { - const { start, end } = state.blockSelection; + const { start, end } = state.blocks.selection; if ( start.clientId === end.clientId ) { return null; } @@ -828,7 +828,7 @@ export function getMultiSelectedBlocksStartClientId( state ) { * @return {?string} Client ID of block ending multi-selection. */ export function getMultiSelectedBlocksEndClientId( state ) { - const { start, end } = state.blockSelection; + const { start, end } = state.blocks.selection; if ( start.clientId === end.clientId ) { return null; } @@ -873,7 +873,7 @@ export function getBlockIndex( state, clientId, rootClientId ) { * @return {boolean} Whether block is selected and multi-selection exists. */ export function isBlockSelected( state, clientId ) { - const { start, end } = state.blockSelection; + const { start, end } = state.blocks.selection; if ( start.clientId !== end.clientId ) { return false; @@ -906,7 +906,7 @@ export function hasSelectedInnerBlock( state, clientId, deep = false ) { * Returns true if the block corresponding to the specified client ID is * currently selected but isn't the last of the selected blocks. Here "last" * refers to the block sequence in the document, _not_ the sequence of - * multi-selection, which is why `state.blockSelection.end` isn't used. + * multi-selection, which is why `state.blocks.selection.end` isn't used. * * @param {Object} state Editor state. * @param {string} clientId Block client ID. @@ -932,7 +932,7 @@ export function isBlockWithinSelection( state, clientId ) { * @return {boolean} Whether multi-selection has been made. */ export function hasMultiSelection( state ) { - const { start, end } = state.blockSelection; + const { start, end } = state.blocks.selection; return start.clientId !== end.clientId; } @@ -948,7 +948,7 @@ export function hasMultiSelection( state ) { * @return {boolean} True if multi-selecting, false if not. */ export function isMultiSelecting( state ) { - return state.blockSelection.isMultiSelecting; + return state.blocks.selection.isMultiSelecting; } /** @@ -959,7 +959,7 @@ export function isMultiSelecting( state ) { * @return {boolean} True if it should be possible to multi-select blocks, false if multi-selection is disabled. */ export function isSelectionEnabled( state ) { - return state.blockSelection.isEnabled; + return state.blocks.selection.isEnabled; } /** @@ -1008,12 +1008,12 @@ export function isCaretWithinFormattedText( state ) { export function getBlockInsertionPoint( state ) { let rootClientId, index; - const { insertionPoint, blockSelection } = state; + const { insertionPoint, blocks } = state; if ( insertionPoint !== null ) { return insertionPoint; } - const { end } = blockSelection; + const { end } = blocks.selection; if ( end.clientId ) { rootClientId = getBlockRootClientId( state, end.clientId ) || undefined; index = getBlockIndex( state, end.clientId, rootClientId ) + 1; diff --git a/packages/block-editor/src/store/test/effects.js b/packages/block-editor/src/store/test/effects.js index 827dde0d39b5c1..5c537982f996f2 100644 --- a/packages/block-editor/src/store/test/effects.js +++ b/packages/block-editor/src/store/test/effects.js @@ -107,11 +107,13 @@ describe( 'effects', () => { }; const dispatch = jest.fn(); const getState = () => ( { - blockSelection: { - start: { - clientId: blockB.clientId, - attributeKey: 'content', - offset: 0, + blocks: { + selection: { + start: { + clientId: blockB.clientId, + attributeKey: 'content', + offset: 0, + }, }, }, } ); @@ -171,11 +173,13 @@ describe( 'effects', () => { }; const dispatch = jest.fn(); const getState = () => ( { - blockSelection: { - start: { - clientId: blockB.clientId, - attributeKey: 'content', - offset: 0, + blocks: { + selection: { + start: { + clientId: blockB.clientId, + attributeKey: 'content', + offset: 0, + }, }, }, } ); @@ -238,11 +242,13 @@ describe( 'effects', () => { }; const dispatch = jest.fn(); const getState = () => ( { - blockSelection: { - start: { - clientId: blockB.clientId, - attributeKey: 'content2', - offset: 0, + blocks: { + selection: { + start: { + clientId: blockB.clientId, + attributeKey: 'content2', + offset: 0, + }, }, }, } ); diff --git a/packages/block-editor/src/store/test/reducer.js b/packages/block-editor/src/store/test/reducer.js index 7543fdad57dd8b..4f77dd42562d6f 100644 --- a/packages/block-editor/src/store/test/reducer.js +++ b/packages/block-editor/src/store/test/reducer.js @@ -259,6 +259,13 @@ describe( 'state', () => { clicken: [ newChildBlockId ], [ newChildBlockId ]: [], }, + selection: { + start: {}, + end: {}, + initialPosition: null, + isEnabled: true, + isMultiSelecting: false, + }, } ); } ); @@ -322,6 +329,13 @@ describe( 'state', () => { clicken: [ newChildBlockId ], [ newChildBlockId ]: [], }, + selection: { + start: {}, + end: {}, + initialPosition: null, + isEnabled: true, + isMultiSelecting: false, + }, } ); } ); @@ -433,6 +447,13 @@ describe( 'state', () => { [ newChildBlockId2 ]: [], [ newChildBlockId3 ]: [], }, + selection: { + start: {}, + end: {}, + initialPosition: null, + isEnabled: true, + isMultiSelecting: false, + }, } ); } ); @@ -504,6 +525,13 @@ describe( 'state', () => { clicken: [ newChildBlockId ], [ newChildBlockId ]: [], }, + selection: { + start: {}, + end: {}, + initialPosition: null, + isEnabled: true, + isMultiSelecting: false, + }, } ); } ); } ); @@ -517,6 +545,13 @@ describe( 'state', () => { order: {}, isPersistentChange: true, isIgnoredChange: false, + selection: { + start: {}, + end: {}, + initialPosition: null, + isEnabled: true, + isMultiSelecting: false, + }, } ); } ); diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index c038a40621eff6..4fce88cf1e354a 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -740,9 +740,11 @@ describe( 'selectors', () => { describe( 'hasSelectedBlock', () => { it( 'should return false if no selection', () => { const state = { - blockSelection: { - start: {}, - end: {}, + blocks: { + selection: { + start: {}, + end: {}, + }, }, }; @@ -751,9 +753,11 @@ describe( 'selectors', () => { it( 'should return false if multi-selection', () => { const state = { - blockSelection: { - start: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, - end: { clientId: '9db792c6-a25a-495d-adbd-97d56a4c4189' }, + blocks: { + selection: { + start: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, + end: { clientId: '9db792c6-a25a-495d-adbd-97d56a4c4189' }, + }, }, }; @@ -762,9 +766,11 @@ describe( 'selectors', () => { it( 'should return true if singular selection', () => { const state = { - blockSelection: { - start: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, - end: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, + blocks: { + selection: { + start: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, + end: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, + }, }, }; @@ -815,7 +821,7 @@ describe( 'selectors', () => { describe( 'getSelectedBlockClientId', () => { it( 'should return null if no block is selected', () => { const state = { - blockSelection: { start: {}, end: {} }, + blocks: { selection: { start: {}, end: {} } }, }; expect( getSelectedBlockClientId( state ) ).toBe( null ); @@ -823,7 +829,7 @@ describe( 'selectors', () => { it( 'should return null if there is multi selection', () => { const state = { - blockSelection: { start: { clientId: 23 }, end: { clientId: 123 } }, + blocks: { selection: { start: { clientId: 23 }, end: { clientId: 123 } } }, }; expect( getSelectedBlockClientId( state ) ).toBe( null ); @@ -837,8 +843,8 @@ describe( 'selectors', () => { name: 'fake block', }, }, + selection: { start: { clientId: 23 }, end: { clientId: 23 } }, }, - blockSelection: { start: { clientId: 23 }, end: { clientId: 23 } }, }; expect( getSelectedBlockClientId( state ) ).toEqual( 23 ); @@ -862,8 +868,8 @@ describe( 'selectors', () => { 23: [], 123: [], }, + selection: { start: {}, end: {} }, }, - blockSelection: { start: {}, end: {} }, }; expect( getSelectedBlock( state ) ).toBe( null ); @@ -885,8 +891,8 @@ describe( 'selectors', () => { 23: [], 123: [], }, + selection: { start: { clientId: 23 }, end: { clientId: 123 } }, }, - blockSelection: { start: { clientId: 23 }, end: { clientId: 123 } }, }; expect( getSelectedBlock( state ) ).toBe( null ); @@ -908,8 +914,8 @@ describe( 'selectors', () => { 23: [], 123: [], }, + selection: { start: { clientId: 23 }, end: { clientId: 23 } }, }, - blockSelection: { start: { clientId: 23 }, end: { clientId: 23 } }, }; expect( getSelectedBlock( state ) ).toEqual( { @@ -992,8 +998,8 @@ describe( 'selectors', () => { order: { '': [ 123, 23 ], }, + selection: { start: {}, end: {} }, }, - blockSelection: { start: {}, end: {} }, }; expect( getSelectedBlockClientIds( state ) ).toEqual( [] ); @@ -1005,8 +1011,8 @@ describe( 'selectors', () => { order: { '': [ 5, 4, 3, 2, 1 ], }, + selection: { start: { clientId: 2 }, end: { clientId: 2 } }, }, - blockSelection: { start: { clientId: 2 }, end: { clientId: 2 } }, }; expect( getSelectedBlockClientIds( state ) ).toEqual( [ 2 ] ); @@ -1018,8 +1024,8 @@ describe( 'selectors', () => { order: { '': [ 5, 4, 3, 2, 1 ], }, + selection: { start: { clientId: 2 }, end: { clientId: 4 } }, }, - blockSelection: { start: { clientId: 2 }, end: { clientId: 4 } }, }; expect( getSelectedBlockClientIds( state ) ).toEqual( [ 4, 3, 2 ] ); @@ -1032,8 +1038,8 @@ describe( 'selectors', () => { '': [ 5, 4, 3, 2, 1 ], 4: [ 9, 8, 7, 6 ], }, + selection: { start: { clientId: 7 }, end: { clientId: 9 } }, }, - blockSelection: { start: { clientId: 7 }, end: { clientId: 9 } }, }; expect( getSelectedBlockClientIds( state ) ).toEqual( [ 9, 8, 7 ] ); @@ -1047,8 +1053,8 @@ describe( 'selectors', () => { order: { '': [ 123, 23 ], }, + selection: { start: {}, end: {} }, }, - blockSelection: { start: {}, end: {} }, }; expect( getMultiSelectedBlockClientIds( state ) ).toEqual( [] ); @@ -1060,8 +1066,8 @@ describe( 'selectors', () => { order: { '': [ 5, 4, 3, 2, 1 ], }, + selection: { start: { clientId: 2 }, end: { clientId: 4 } }, }, - blockSelection: { start: { clientId: 2 }, end: { clientId: 4 } }, }; expect( getMultiSelectedBlockClientIds( state ) ).toEqual( [ 4, 3, 2 ] ); @@ -1074,8 +1080,8 @@ describe( 'selectors', () => { '': [ 5, 4, 3, 2, 1 ], 4: [ 9, 8, 7, 6 ], }, + selection: { start: { clientId: 7 }, end: { clientId: 9 } }, }, - blockSelection: { start: { clientId: 7 }, end: { clientId: 9 } }, }; expect( getMultiSelectedBlockClientIds( state ) ).toEqual( [ 9, 8, 7 ] ); @@ -1089,8 +1095,8 @@ describe( 'selectors', () => { byClientId: {}, attributes: {}, order: {}, + selection: { start: {}, end: {} }, }, - blockSelection: { start: {}, end: {} }, }; expect( @@ -1102,7 +1108,7 @@ describe( 'selectors', () => { describe( 'getMultiSelectedBlocksStartClientId', () => { it( 'returns null if there is no multi selection', () => { const state = { - blockSelection: { start: {}, end: {} }, + blocks: { selection: { start: {}, end: {} } }, }; expect( getMultiSelectedBlocksStartClientId( state ) ).toBeNull(); @@ -1110,7 +1116,7 @@ describe( 'selectors', () => { it( 'returns multi selection start', () => { const state = { - blockSelection: { start: { clientId: 2 }, end: { clientId: 4 } }, + blocks: { selection: { start: { clientId: 2 }, end: { clientId: 4 } } }, }; expect( getMultiSelectedBlocksStartClientId( state ) ).toBe( 2 ); @@ -1120,7 +1126,7 @@ describe( 'selectors', () => { describe( 'getMultiSelectedBlocksEndClientId', () => { it( 'returns null if there is no multi selection', () => { const state = { - blockSelection: { start: {}, end: {} }, + blocks: { selection: { start: {}, end: {} } }, }; expect( getMultiSelectedBlocksEndClientId( state ) ).toBeNull(); @@ -1128,7 +1134,7 @@ describe( 'selectors', () => { it( 'returns multi selection end', () => { const state = { - blockSelection: { start: { clientId: 2 }, end: { clientId: 4 } }, + blocks: { selection: { start: { clientId: 2 }, end: { clientId: 4 } } }, }; expect( getMultiSelectedBlocksEndClientId( state ) ).toBe( 4 ); @@ -1296,7 +1302,7 @@ describe( 'selectors', () => { describe( 'isBlockSelected', () => { it( 'should return true if the block is selected', () => { const state = { - blockSelection: { start: { clientId: 123 }, end: { clientId: 123 } }, + blocks: { selection: { start: { clientId: 123 }, end: { clientId: 123 } } }, }; expect( isBlockSelected( state, 123 ) ).toBe( true ); @@ -1304,7 +1310,7 @@ describe( 'selectors', () => { it( 'should return false if a multi-selection range exists', () => { const state = { - blockSelection: { start: { clientId: 123 }, end: { clientId: 124 } }, + blocks: { selection: { start: { clientId: 123 }, end: { clientId: 124 } } }, }; expect( isBlockSelected( state, 123 ) ).toBe( false ); @@ -1312,7 +1318,7 @@ describe( 'selectors', () => { it( 'should return false if the block is not selected', () => { const state = { - blockSelection: { start: {}, end: {} }, + blocks: { selection: { start: {}, end: {} } }, }; expect( isBlockSelected( state, 23 ) ).toBe( false ); @@ -1322,11 +1328,11 @@ describe( 'selectors', () => { describe( 'hasSelectedInnerBlock', () => { it( 'should return false if the selected block is a child of the given ClientId', () => { const state = { - blockSelection: { start: { clientId: 5 }, end: { clientId: 5 } }, blocks: { order: { 4: [ 3, 2, 1 ], }, + selection: { start: { clientId: 5 }, end: { clientId: 5 } }, }, }; @@ -1335,11 +1341,11 @@ describe( 'selectors', () => { it( 'should return true if the selected block is a child of the given ClientId', () => { const state = { - blockSelection: { start: { clientId: 3 }, end: { clientId: 3 } }, blocks: { order: { 4: [ 3, 2, 1 ], }, + selection: { start: { clientId: 3 }, end: { clientId: 3 } }, }, }; @@ -1352,8 +1358,8 @@ describe( 'selectors', () => { order: { 6: [ 5, 4, 3, 2, 1 ], }, + selection: { start: { clientId: 2 }, end: { clientId: 4 } }, }, - blockSelection: { start: { clientId: 2 }, end: { clientId: 4 } }, }; expect( hasSelectedInnerBlock( state, 6 ) ).toBe( true ); } ); @@ -1365,8 +1371,8 @@ describe( 'selectors', () => { 3: [ 2, 1 ], 6: [ 5, 4 ], }, + selection: { start: { clientId: 5 }, end: { clientId: 4 } }, }, - blockSelection: { start: { clientId: 5 }, end: { clientId: 4 } }, }; expect( hasSelectedInnerBlock( state, 3 ) ).toBe( false ); } ); @@ -1375,11 +1381,11 @@ describe( 'selectors', () => { describe( 'isBlockWithinSelection', () => { it( 'should return true if the block is selected but not the last', () => { const state = { - blockSelection: { start: { clientId: 5 }, end: { clientId: 3 } }, blocks: { order: { '': [ 5, 4, 3, 2, 1 ], }, + selection: { start: { clientId: 5 }, end: { clientId: 3 } }, }, }; @@ -1388,11 +1394,11 @@ describe( 'selectors', () => { it( 'should return false if the block is the last selected', () => { const state = { - blockSelection: { start: { clientId: 5 }, end: { clientId: 3 } }, blocks: { order: { '': [ 5, 4, 3, 2, 1 ], }, + selection: { start: { clientId: 5 }, end: { clientId: 3 } }, }, }; @@ -1401,11 +1407,11 @@ describe( 'selectors', () => { it( 'should return false if the block is not selected', () => { const state = { - blockSelection: { start: { clientId: 5 }, end: { clientId: 3 } }, blocks: { order: { '': [ 5, 4, 3, 2, 1 ], }, + selection: { start: { clientId: 5 }, end: { clientId: 3 } }, }, }; @@ -1414,11 +1420,11 @@ describe( 'selectors', () => { it( 'should return false if there is no selection', () => { const state = { - blockSelection: { start: {}, end: {} }, blocks: { order: { '': [ 5, 4, 3, 2, 1 ], }, + selection: { start: {}, end: {} }, }, }; @@ -1429,10 +1435,7 @@ describe( 'selectors', () => { describe( 'hasMultiSelection', () => { it( 'should return false if no selection', () => { const state = { - blockSelection: { - start: {}, - end: {}, - }, + blocks: { selection: { start: {}, end: {} } }, }; expect( hasMultiSelection( state ) ).toBe( false ); @@ -1440,9 +1443,11 @@ describe( 'selectors', () => { it( 'should return false if singular selection', () => { const state = { - blockSelection: { - start: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, - end: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, + blocks: { + selection: { + start: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, + end: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, + }, }, }; @@ -1451,9 +1456,11 @@ describe( 'selectors', () => { it( 'should return true if multi-selection', () => { const state = { - blockSelection: { - start: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, - end: { clientId: '9db792c6-a25a-495d-adbd-97d56a4c4189' }, + blocks: { + selection: { + start: { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' }, + end: { clientId: '9db792c6-a25a-495d-adbd-97d56a4c4189' }, + }, }, }; @@ -1467,8 +1474,8 @@ describe( 'selectors', () => { order: { '': [ 5, 4, 3, 2, 1 ], }, + selection: { start: { clientId: 2 }, end: { clientId: 4 } }, }, - blockSelection: { start: { clientId: 2 }, end: { clientId: 4 } }, }; it( 'should return true if the block is multi selected', () => { @@ -1486,8 +1493,8 @@ describe( 'selectors', () => { order: { '': [ 5, 4, 3, 2, 1 ], }, + selection: { start: { clientId: 2 }, end: { clientId: 4 } }, }, - blockSelection: { start: { clientId: 2 }, end: { clientId: 4 } }, }; it( 'should return true if the block is first in multi selection', () => { @@ -1558,9 +1565,7 @@ describe( 'selectors', () => { describe( 'isSelectionEnabled', () => { it( 'should return true if selection is enable', () => { const state = { - blockSelection: { - isEnabled: true, - }, + blocks: { selection: { isEnabled: true } }, }; expect( isSelectionEnabled( state ) ).toBe( true ); @@ -1568,9 +1573,7 @@ describe( 'selectors', () => { it( 'should return false if selection is disabled', () => { const state = { - blockSelection: { - isEnabled: false, - }, + blocks: { selection: { isEnabled: false } }, }; expect( isSelectionEnabled( state ) ).toBe( false ); @@ -1580,10 +1583,6 @@ describe( 'selectors', () => { describe( 'getBlockInsertionPoint', () => { it( 'should return the explicitly assigned insertion point', () => { const state = { - blockSelection: { - start: { clientId: 'clientId2' }, - end: { clientId: 'clientId2' }, - }, blocks: { byClientId: { clientId1: { clientId: 'clientId1' }, @@ -1598,6 +1597,10 @@ describe( 'selectors', () => { clientId1: [ 'clientId2' ], clientId2: [], }, + selection: { + start: { clientId: 'clientId2' }, + end: { clientId: 'clientId2' }, + }, }, insertionPoint: { rootClientId: undefined, @@ -1613,10 +1616,6 @@ describe( 'selectors', () => { it( 'should return an object for the selected block', () => { const state = { - blockSelection: { - start: { clientId: 'clientId1' }, - end: { clientId: 'clientId1' }, - }, blocks: { byClientId: { clientId1: { clientId: 'clientId1' }, @@ -1628,6 +1627,10 @@ describe( 'selectors', () => { '': [ 'clientId1' ], clientId1: [], }, + selection: { + start: { clientId: 'clientId1' }, + end: { clientId: 'clientId1' }, + }, }, insertionPoint: null, }; @@ -1640,10 +1643,6 @@ describe( 'selectors', () => { it( 'should return an object for the nested selected block', () => { const state = { - blockSelection: { - start: { clientId: 'clientId2' }, - end: { clientId: 'clientId2' }, - }, blocks: { byClientId: { clientId1: { clientId: 'clientId1' }, @@ -1658,6 +1657,10 @@ describe( 'selectors', () => { clientId1: [ 'clientId2' ], clientId2: [], }, + selection: { + start: { clientId: 'clientId2' }, + end: { clientId: 'clientId2' }, + }, }, insertionPoint: null, }; @@ -1670,10 +1673,6 @@ describe( 'selectors', () => { it( 'should return an object for the last multi selected clientId', () => { const state = { - blockSelection: { - start: { clientId: 'clientId1' }, - end: { clientId: 'clientId2' }, - }, blocks: { byClientId: { clientId1: { clientId: 'clientId1' }, @@ -1688,6 +1687,10 @@ describe( 'selectors', () => { clientId1: [], clientId2: [], }, + selection: { + start: { clientId: 'clientId1' }, + end: { clientId: 'clientId2' }, + }, }, insertionPoint: null, }; @@ -1700,10 +1703,6 @@ describe( 'selectors', () => { it( 'should return an object for the last block if no selection', () => { const state = { - blockSelection: { - start: {}, - end: {}, - }, blocks: { byClientId: { clientId1: { clientId: 'clientId1' }, @@ -1718,6 +1717,10 @@ describe( 'selectors', () => { clientId1: [], clientId2: [], }, + selection: { + start: {}, + end: {}, + }, }, insertionPoint: null, };