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

[RNMobile] Refactor simplify media flow redux store changes #30123

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
60328e3
Moved the last block inserted actions from editor to the block-editor
jd-alexander Mar 19, 2021
42592ba
Moved the last block inserted reducer from editor to the block-editor
jd-alexander Mar 19, 2021
b19dc1f
Moved the last block inserted selector from editor to the block-editor
jd-alexander Mar 19, 2021
9c6e0e6
Fixed es-lint error.
jd-alexander Mar 19, 2021
0eac8e9
Moved last block inserted actions test from editor to the block-editor
jd-alexander Mar 19, 2021
1798f52
Moved last block inserted reducer test from editor to the block-editor
jd-alexander Mar 19, 2021
aa8d426
Moved last block inserted selector test from editor to the block-editor
jd-alexander Mar 19, 2021
e872c82
Moved all calls to last block inserted from editor to block-editor
jd-alexander Mar 19, 2021
286fc73
Merge branch 'rnmobile/simplify_image_insertion_flow-redux-changes' i…
jd-alexander Mar 19, 2021
a035571
last block inserter usage in menu native migrated : editor to block-e…
jd-alexander Mar 19, 2021
9fdba04
[RNMobile] Refactor: Simplify media flow redux migration (#30238)
fluiddot Mar 26, 2021
317cd03
Removed add/clear last block inserted actions and tests due to refactor
jd-alexander Mar 27, 2021
4665b5f
Removed addLastBlockInserted from the insert menu's onPress.
jd-alexander Mar 27, 2021
27ad05d
rewrote the tests for the lastBlockInserted reducer.
jd-alexander Mar 27, 2021
f7894f3
rewrote tests for wasBlockJustInserted selector.
jd-alexander Mar 29, 2021
69856b1
optimized clientId
jd-alexander Mar 29, 2021
770a61a
removed unneeded clientId.
jd-alexander Mar 29, 2021
e8aefa8
put the expectedSource inside the test meta object.
jd-alexander Mar 29, 2021
ab46c9d
used expectedState insted {} for state related tests.
jd-alexander Mar 29, 2021
371ab67
used expectedState instead {} for state related tests.
jd-alexander Mar 29, 2021
f4b5960
Merge remote-tracking branch 'origin/rnmobile/simplify-media-flow-red…
jd-alexander Mar 29, 2021
ae46525
removed parentheses from describe name.
jd-alexander Mar 30, 2021
5c1d9fc
return the same state instead of empty state.
jd-alexander Mar 30, 2021
9307bf6
made changes to test name so its intent is clearer.
jd-alexander Mar 30, 2021
0d0164c
made the insertion source optional.
jd-alexander Mar 30, 2021
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
12 changes: 7 additions & 5 deletions packages/block-editor/src/components/inserter/menu.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ function InserterMenu( {
insertDefaultBlock,
} = useDispatch( blockEditorStore );

const { addLastBlockInserted } = useDispatch( 'core/editor' );

const {
items,
destinationRootClientId,
Expand Down Expand Up @@ -135,9 +133,13 @@ function InserterMenu( {
innerBlocks
);

addLastBlockInserted( newBlock.clientId );

insertBlock( newBlock, insertionIndex, destinationRootClientId );
insertBlock(
newBlock,
insertionIndex,
destinationRootClientId,
true,
{ source: 'inserter_menu' }
);
},
[ insertBlock, destinationRootClientId, insertionIndex ]
);
Expand Down
13 changes: 11 additions & 2 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,16 +538,25 @@ export function* moveBlockToPosition(
* @param {?number} index Index at which block should be inserted.
* @param {?string} rootClientId Optional root client ID of block list on which to insert.
* @param {?boolean} updateSelection If true block selection will be updated. If false, block selection will not change. Defaults to true.
* @param {?Object} meta Optional Meta values to be passed to the action object.
*
* @return {Object} Action object.
*/
export function insertBlock(
block,
index,
rootClientId,
updateSelection = true
updateSelection = true,
meta
) {
return insertBlocks( [ block ], index, rootClientId, updateSelection );
return insertBlocks(
[ block ],
index,
rootClientId,
updateSelection,
0,
meta
);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,31 @@ export function highlightedBlock( state, action ) {
return state;
}

/**
* Reducer returning the block insertion event list state.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function lastBlockInserted( state = {}, action ) {
switch ( action.type ) {
case 'INSERT_BLOCKS':
if ( ! action.updateSelection || ! action.blocks.length ) {
return state;
}

const clientId = action.blocks[ 0 ].clientId;
const source = action.meta?.source;

return { clientId, source };
case 'RESET_BLOCKS':
return {};
}
return state;
}

export default combineReducers( {
blocks,
isTyping,
Expand All @@ -1748,4 +1773,5 @@ export default combineReducers( {
hasBlockMovingClientId,
automaticChangeStatus,
highlightedBlock,
lastBlockInserted,
} );
16 changes: 16 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2084,3 +2084,19 @@ export const __experimentalGetActiveBlockIdByBlockNames = createSelector(
validBlockNames,
]
);

/**
* Tells if the block with the passed clientId was just inserted.
*
* @param {Object} state Global application state.
* @param {Object} clientId Client Id of the block.
* @param {?string} source Optional insertion source of the block.
* @return {boolean} True if the block matches the last block inserted from the specified source.
*/
export function wasBlockJustInserted( state, clientId, source ) {
const { lastBlockInserted } = state;
return (
lastBlockInserted.clientId === clientId &&
lastBlockInserted.source === source
);
}
99 changes: 99 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
template,
blockListSettings,
lastBlockAttributesChange,
lastBlockInserted,
} from '../reducer';

describe( 'state', () => {
Expand Down Expand Up @@ -2944,4 +2945,102 @@ describe( 'state', () => {
expect( state ).toBe( null );
} );
} );

describe( 'lastBlockInserted', () => {
it( 'should return client id if last block inserted is called with action INSERT_BLOCKS', () => {
const expectedClientId = '62bfef6e-d5e9-43ba-b7f9-c77cf354141f';

const action = {
blocks: [
{
clientId: expectedClientId,
},
],
meta: {
source: 'inserter_menu',
},
type: 'INSERT_BLOCKS',
updateSelection: true,
};

const state = lastBlockInserted( {}, action );

expect( state.clientId ).toBe( expectedClientId );
} );

it( 'should return inserter_menu source if last block inserted is called with action INSERT_BLOCKS', () => {
const expectedSource = 'inserter_menu';

const action = {
blocks: [
{
clientId: '62bfef6e-d5e9-43ba-b7f9-c77cf354141f',
},
],
meta: {
source: expectedSource,
},
type: 'INSERT_BLOCKS',
updateSelection: true,
};

const state = lastBlockInserted( {}, action );

expect( state.source ).toBe( expectedSource );
} );

it( 'should return state if last block inserted is called with action INSERT_BLOCKS that is not a updateSelection', () => {
const expectedState = {
clientId: '9db792c6-a25a-495d-adbd-97d56a4c4189',
};

const action = {
blocks: [
{
clientId: '62bfef6e-d5e9-43ba-b7f9-c77cf354141f',
},
],
meta: {
source: 'inserter_menu',
},
type: 'INSERT_BLOCKS',
updateSelection: false,
};

const state = lastBlockInserted( expectedState, action );

expect( state ).toEqual( expectedState );
} );

it( 'should return state if last block inserted is called with action INSERT_BLOCKS and block list is empty', () => {
const expectedState = {
clientId: '9db792c6-a25a-495d-adbd-97d56a4c4189',
};

const action = {
blocks: [],
meta: {
source: 'inserter_menu',
},
type: 'INSERT_BLOCKS',
updateSelection: true,
};

const state = lastBlockInserted( expectedState, action );

expect( state ).toEqual( expectedState );
} );

it( 'should return empty state if last block inserted is called with action RESET_BLOCKS', () => {
const expectedState = {};

const action = {
type: 'RESET_BLOCKS',
};

const state = lastBlockInserted( expectedState, action );

expect( state ).toEqual( expectedState );
} );
} );
} );
51 changes: 51 additions & 0 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const {
__experimentalGetParsedReusableBlock,
__experimentalGetAllowedPatterns,
__experimentalGetScopedBlockPatterns,
wasBlockJustInserted,
} = selectors;

describe( 'selectors', () => {
Expand Down Expand Up @@ -3450,6 +3451,56 @@ describe( 'selectors', () => {
);
} );
} );

describe( 'wasBlockJustInserted', () => {
it( 'should return true if the client id passed to wasBlockJustInserted is found within the state', () => {
const expectedClientId = '62bfef6e-d5e9-43ba-b7f9-c77cf354141f';
const source = 'inserter_menu';

const state = {
lastBlockInserted: {
clientId: expectedClientId,
source,
},
};

expect(
wasBlockJustInserted( state, expectedClientId, source )
).toBe( true );
} );

it( 'should return false if the client id passed to wasBlockJustInserted is not found within the state', () => {
const expectedClientId = '62bfef6e-d5e9-43ba-b7f9-c77cf354141f';
const unexpectedClientId = '62bfsed4-d5e9-43ba-b7f9-c77cf565756s';
const source = 'inserter_menu';

const state = {
lastBlockInserted: {
clientId: unexpectedClientId,
source,
},
};

expect(
wasBlockJustInserted( state, expectedClientId, source )
).toBe( false );
} );

it( 'should return false if the source passed to wasBlockJustInserted is not found within the state', () => {
const clientId = '62bfef6e-d5e9-43ba-b7f9-c77cf354141f';
const expectedSource = 'inserter_menu';

const state = {
lastBlockInserted: {
clientId,
},
};

expect(
wasBlockJustInserted( state, clientId, expectedSource )
).toBe( false );
} );
} );
} );

describe( '__experimentalGetParsedReusableBlock', () => {
Expand Down
Loading