Skip to content

Commit

Permalink
Lodash: Refactor away from BlockMover (#43853)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Sep 5, 2022
1 parent 907a65e commit 031fdb6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
11 changes: 6 additions & 5 deletions packages/block-editor/src/components/block-mover/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { castArray, first, last } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -62,7 +61,10 @@ const BlockMoverButton = forwardRef(
ref
) => {
const instanceId = useInstanceId( BlockMoverButton );
const blocksCount = castArray( clientIds ).length;
const normalizedClientIds = Array.isArray( clientIds )
? clientIds
: [ clientIds ];
const blocksCount = normalizedClientIds.length;

const {
blockType,
Expand All @@ -81,12 +83,11 @@ const BlockMoverButton = forwardRef(
getBlock,
getBlockListSettings,
} = select( blockEditorStore );
const normalizedClientIds = castArray( clientIds );
const firstClientId = first( normalizedClientIds );
const firstClientId = normalizedClientIds[ 0 ];
const blockRootClientId = getBlockRootClientId( firstClientId );
const firstBlockIndex = getBlockIndex( firstClientId );
const lastBlockIndex = getBlockIndex(
last( normalizedClientIds )
normalizedClientIds[ normalizedClientIds.length - 1 ]
);
const blockOrder = getBlockOrder( blockRootClientId );
const block = getBlock( firstClientId );
Expand Down
15 changes: 8 additions & 7 deletions packages/block-editor/src/components/block-mover/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { first, last, castArray } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -30,13 +29,15 @@ function BlockMover( { clientIds, hideDragHandle } ) {
getBlockOrder,
getBlockRootClientId,
} = select( blockEditorStore );
const normalizedClientIds = castArray( clientIds );
const firstClientId = first( normalizedClientIds );
const _rootClientId = getBlockRootClientId(
first( normalizedClientIds )
);
const normalizedClientIds = Array.isArray( clientIds )
? clientIds
: [ clientIds ];
const firstClientId = normalizedClientIds[ 0 ];
const _rootClientId = getBlockRootClientId( firstClientId );
const firstIndex = getBlockIndex( firstClientId );
const lastIndex = getBlockIndex( last( normalizedClientIds ) );
const lastIndex = getBlockIndex(
normalizedClientIds[ normalizedClientIds.length - 1 ]
);
const blockOrder = getBlockOrder( _rootClientId );

return {
Expand Down
33 changes: 20 additions & 13 deletions packages/block-editor/src/components/block-mover/index.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { first, last, partial, castArray } from 'lodash';
import { Platform } from 'react-native';

/**
Expand Down Expand Up @@ -149,12 +148,16 @@ export default compose(
getBlockRootClientId,
getBlockOrder,
} = select( blockEditorStore );
const normalizedClientIds = castArray( clientIds );
const firstClientId = first( normalizedClientIds );
const normalizedClientIds = Array.isArray( clientIds )
? clientIds
: [ clientIds ];
const firstClientId = normalizedClientIds[ 0 ];
const rootClientId = getBlockRootClientId( firstClientId );
const blockOrder = getBlockOrder( rootClientId );
const firstIndex = getBlockIndex( firstClientId );
const lastIndex = getBlockIndex( last( normalizedClientIds ) );
const lastIndex = getBlockIndex(
normalizedClientIds[ normalizedClientIds.length - 1 ]
);

return {
firstIndex,
Expand All @@ -169,15 +172,19 @@ export default compose(
const { moveBlocksDown, moveBlocksUp, moveBlocksToPosition } =
dispatch( blockEditorStore );
return {
onMoveDown: partial( moveBlocksDown, clientIds, rootClientId ),
onMoveUp: partial( moveBlocksUp, clientIds, rootClientId ),
onLongMove: ( targetIndex ) =>
partial(
moveBlocksToPosition,
clientIds,
rootClientId,
targetIndex
),
onMoveDown: ( ...args ) =>
moveBlocksDown( clientIds, rootClientId, ...args ),
onMoveUp: ( ...args ) =>
moveBlocksUp( clientIds, rootClientId, ...args ),
onLongMove:
( targetIndex ) =>
( ...args ) =>
moveBlocksToPosition(
clientIds,
rootClientId,
targetIndex,
...args
),
};
} ),
withInstanceId
Expand Down

0 comments on commit 031fdb6

Please sign in to comment.