Skip to content

Commit

Permalink
Make updateInsertUsage a proper action that can be unit tested
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Mar 22, 2022
1 parent a205079 commit 6598078
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 191 deletions.
8 changes: 8 additions & 0 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,14 @@ _Returns_

- `Object`: Action object

### updateInsertUsage

Updates the inserter usage statistics in the preferences store.

_Parameters_

- _blocks_ `Array`: The array of blocks that were inserted.

### updateSettings

Action that updates the block editor settings.
Expand Down
20 changes: 9 additions & 11 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export const replaceBlocks = (
indexToSelect,
initialPosition = 0,
meta
) => ( { select, dispatch, registry } ) => {
) => ( { select, dispatch } ) => {
/* eslint-enable jsdoc/valid-types */
clientIds = castArray( clientIds );
blocks = getBlocksWithDefaultStylesApplied(
Expand All @@ -391,7 +391,7 @@ export const replaceBlocks = (
}
}

updateInsertUsage( blocks, registry );
dispatch.updateInsertUsage( blocks );
dispatch( {
type: 'REPLACE_BLOCKS',
clientIds,
Expand Down Expand Up @@ -544,13 +544,11 @@ export function insertBlock(
}

/**
* Helper function that updates the insertion usage stats in the preferences
* store whenever a block is inserter or replaced.
* Updates the inserter usage statistics in the preferences store.
*
* @param {Array} blocks The array of blocks that were inserted.
* @param {Object} registry The data registry.
* @param {Array} blocks The array of blocks that were inserted.
*/
function updateInsertUsage( blocks, registry ) {
export const updateInsertUsage = ( blocks ) => ( { registry } ) => {
const previousInsertUsage =
registry.select( preferencesStore ).get( 'core', 'insertUsage' ) ?? {};

Expand All @@ -567,7 +565,7 @@ function updateInsertUsage( blocks, registry ) {
let id = match?.name ? `${ blockName }/${ match.name }` : blockName;
const _insert = { name: id };
if ( blockName === 'core/block' ) {
insert.ref = attributes.ref;
_insert.ref = attributes.ref;
id += '/' + attributes.ref;
}

Expand All @@ -586,7 +584,7 @@ function updateInsertUsage( blocks, registry ) {
registry
.dispatch( preferencesStore )
.set( 'core', 'insertUsage', updatedInsertUsage );
}
};

/* eslint-disable jsdoc/valid-types */
/**
Expand All @@ -607,7 +605,7 @@ export const insertBlocks = (
updateSelection = true,
initialPosition = 0,
meta
) => ( { select, dispatch, registry } ) => {
) => ( { select, dispatch } ) => {
/* eslint-enable jsdoc/valid-types */
if ( isObject( initialPosition ) ) {
meta = initialPosition;
Expand All @@ -630,7 +628,7 @@ export const insertBlocks = (
}
}
if ( allowedBlocks.length ) {
updateInsertUsage( blocks, registry );
dispatch.updateInsertUsage( blocks );
dispatch( {
type: 'INSERT_BLOCKS',
blocks: allowedBlocks,
Expand Down
Loading

0 comments on commit 6598078

Please sign in to comment.