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

Refactor resetBlocks to action-generators #14594

Closed
wants to merge 7 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -1130,18 +1130,14 @@ _Returns_

<a name="resetBlocks" href="#resetBlocks">#</a> **resetBlocks**

Returns an action object used in signalling that blocks state should be
Returns an action generator used in signalling that blocks state should be
reset to the specified array of blocks, taking precedence over any other
content reflected as an edit in state.

_Parameters_

- _blocks_ `Array`: Array of blocks.

_Returns_

- `Object`: Action object.

<a name="resetSelection" href="#resetSelection">#</a> **resetSelection**

Returns an action object used in signalling that selection state should be
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@wordpress/components": "file:../components",
"@wordpress/compose": "file:../compose",
"@wordpress/data": "file:../data",
"@wordpress/data-controls": "file:../data-controls",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
"@wordpress/element": "file:../element",
Expand Down
92 changes: 45 additions & 47 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import {
createBlock,
hasBlockSupport,
cloneBlock,
doBlocksMatchTemplate,
} from '@wordpress/blocks';
import { speak } from '@wordpress/a11y';
import { __ } from '@wordpress/i18n';
import { select } from '@wordpress/data-controls';

/**
* Internal dependencies
*/
import { select } from './controls';
import { STORE_KEY } from './constants';

/**
* Generator which will yield a default block insert action if there
Expand All @@ -27,7 +29,7 @@ import { select } from './controls';
* replacement, etc).
*/
function* ensureDefaultBlock() {
const count = yield select( 'core/block-editor', 'getBlockCount' );
const count = yield select( STORE_KEY, 'getBlockCount' );

// To avoid a focus loss when removing the last block, assure there is
// always a default block if the last of the blocks have been removed.
Expand All @@ -37,19 +39,35 @@ function* ensureDefaultBlock() {
}

/**
* Returns an action object used in signalling that blocks state should be
* Returns an action generator used in signalling that blocks state should be
* reset to the specified array of blocks, taking precedence over any other
* content reflected as an edit in state.
*
* @param {Array} blocks Array of blocks.
*
* @return {Object} Action object.
* @yield {Object} Action object.
*/
export function resetBlocks( blocks ) {
return {
export function* resetBlocks( blocks ) {
yield {
type: 'RESET_BLOCKS',
blocks,
};
const template = yield select( STORE_KEY, 'getTemplate' );
const templateLock = yield select( STORE_KEY, 'getTemplateLock' );

// Unlocked templates are considered always valid because they act
// as default values only.
const isBlocksValidToTemplate =
! template ||
templateLock !== 'all' ||
doBlocksMatchTemplate( blocks, template );

const isValidTemplate = yield select( STORE_KEY, 'isValidTemplate' );

// Update if validity has changed.
if ( isBlocksValidToTemplate !== isValidTemplate ) {
yield setTemplateValidity( isBlocksValidToTemplate );
}
}

/**
Expand Down Expand Up @@ -158,7 +176,7 @@ export function selectBlock( clientId, initialPosition = null ) {
*/
export function* selectPreviousBlock( clientId ) {
const previousBlockClientId = yield select(
'core/block-editor',
STORE_KEY,
'getPreviousBlockClientId',
clientId
);
Expand All @@ -176,7 +194,7 @@ export function* selectPreviousBlock( clientId ) {
*/
export function* selectNextBlock( clientId ) {
const nextBlockClientId = yield select(
'core/block-editor',
STORE_KEY,
'getNextBlockClientId',
clientId
);
Expand Down Expand Up @@ -293,18 +311,18 @@ export function* replaceBlocks( clientIds, blocks, indexToSelect ) {
clientIds = castArray( clientIds );
blocks = getBlocksWithDefaultStylesApplied(
castArray( blocks ),
yield select( 'core/block-editor', 'getSettings' )
yield select( STORE_KEY, 'getSettings' )
);
const rootClientId = yield select(
'core/block-editor',
STORE_KEY,
'getBlockRootClientId',
first( clientIds )
);
// Replace is valid if the new blocks can be inserted in the root block.
for ( let index = 0; index < blocks.length; index++ ) {
const block = blocks[ index ];
const canInsertBlock = yield select(
'core/block-editor',
STORE_KEY,
'canInsertBlockType',
block.name,
rootClientId
Expand Down Expand Up @@ -375,7 +393,7 @@ export function* moveBlockToPosition(
index
) {
const templateLock = yield select(
'core/block-editor',
STORE_KEY,
'getTemplateLock',
fromRootClientId
);
Expand Down Expand Up @@ -405,14 +423,10 @@ export function* moveBlockToPosition(
return;
}

const blockName = yield select(
'core/block-editor',
'getBlockName',
clientId
);
const blockName = yield select( STORE_KEY, 'getBlockName', clientId );

const canInsertBlock = yield select(
'core/block-editor',
STORE_KEY,
'canInsertBlockType',
blockName,
toRootClientId
Expand Down Expand Up @@ -463,12 +477,12 @@ export function* insertBlocks(
) {
blocks = getBlocksWithDefaultStylesApplied(
castArray( blocks ),
yield select( 'core/block-editor', 'getSettings' )
yield select( STORE_KEY, 'getSettings' )
);
const allowedBlocks = [];
for ( const block of blocks ) {
const isValid = yield select(
'core/block-editor',
STORE_KEY,
'canInsertBlockType',
block.name,
rootClientId
Expand Down Expand Up @@ -573,15 +587,11 @@ export function* removeBlocks( clientIds, selectPrevious = true ) {

clientIds = castArray( clientIds );
const rootClientId = yield select(
'core/block-editor',
STORE_KEY,
'getBlockRootClientId',
clientIds[ 0 ]
);
const isLocked = yield select(
'core/block-editor',
'getTemplateLock',
rootClientId
);
const isLocked = yield select( STORE_KEY, 'getTemplateLock', rootClientId );
if ( isLocked ) {
return;
}
Expand Down Expand Up @@ -883,13 +893,9 @@ export function* duplicateBlocks( clientIds ) {
if ( ! clientIds && ! clientIds.length ) {
return;
}
const blocks = yield select(
'core/block-editor',
'getBlocksByClientId',
clientIds
);
const blocks = yield select( STORE_KEY, 'getBlocksByClientId', clientIds );
const rootClientId = yield select(
'core/block-editor',
STORE_KEY,
'getBlockRootClientId',
clientIds[ 0 ]
);
Expand All @@ -909,7 +915,7 @@ export function* duplicateBlocks( clientIds ) {
}

const lastSelectedIndex = yield select(
'core/block-editor',
STORE_KEY,
'getBlockIndex',
last( castArray( clientIds ) ),
rootClientId
Expand All @@ -934,21 +940,17 @@ export function* insertBeforeBlock( clientId ) {
return;
}
const rootClientId = yield select(
'core/block-editor',
STORE_KEY,
'getBlockRootClientId',
clientId
);
const isLocked = yield select(
'core/block-editor',
'getTemplateLock',
rootClientId
);
const isLocked = yield select( STORE_KEY, 'getTemplateLock', rootClientId );
if ( isLocked ) {
return;
}

const firstSelectedIndex = yield select(
'core/block-editor',
STORE_KEY,
'getBlockIndex',
clientId,
rootClientId
Expand All @@ -966,21 +968,17 @@ export function* insertAfterBlock( clientId ) {
return;
}
const rootClientId = yield select(
'core/block-editor',
STORE_KEY,
'getBlockRootClientId',
clientId
);
const isLocked = yield select(
'core/block-editor',
'getTemplateLock',
rootClientId
);
const isLocked = yield select( STORE_KEY, 'getTemplateLock', rootClientId );
if ( isLocked ) {
return;
}

const firstSelectedIndex = yield select(
'core/block-editor',
STORE_KEY,
'getBlockIndex',
clientId,
rootClientId
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/store/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const STORE_KEY = 'core/block-editor';
32 changes: 0 additions & 32 deletions packages/block-editor/src/store/controls.js

This file was deleted.

36 changes: 1 addition & 35 deletions packages/block-editor/src/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { findKey } from 'lodash';
import { speak } from '@wordpress/a11y';
import {
getBlockType,
doBlocksMatchTemplate,
switchToBlockType,
synchronizeBlocksWithTemplate,
cloneBlock,
Expand All @@ -21,51 +20,19 @@ import { create, toHTMLString, insert, remove } from '@wordpress/rich-text';
* Internal dependencies
*/
import {
resetBlocks,
replaceBlocks,
selectBlock,
setTemplateValidity,
resetBlocks,
selectionChange,
} from './actions';
import {
getBlock,
getBlocks,
getSelectedBlockCount,
getTemplateLock,
getTemplate,
isValidTemplate,
getSelectionStart,
} from './selectors';

/**
* Block validity is a function of blocks state (at the point of a
* reset) and the template setting. As a compromise to its placement
* across distinct parts of state, it is implemented here as a side-
* effect of the block reset action.
*
* @param {Object} action RESET_BLOCKS action.
* @param {Object} store Store instance.
*
* @return {?Object} New validity set action if validity has changed.
*/
export function validateBlocksToTemplate( action, store ) {
const state = store.getState();
const template = getTemplate( state );
const templateLock = getTemplateLock( state );

// Unlocked templates are considered always valid because they act
// as default values only.
const isBlocksValidToTemplate =
! template ||
templateLock !== 'all' ||
doBlocksMatchTemplate( action.blocks, template );

// Update if validity has changed.
if ( isBlocksValidToTemplate !== isValidTemplate( state ) ) {
return setTemplateValidity( isBlocksValidToTemplate );
}
}

export default {
MERGE_BLOCKS( action, store ) {
const { dispatch } = store;
Expand Down Expand Up @@ -219,7 +186,6 @@ export default {
)
);
},
RESET_BLOCKS: [ validateBlocksToTemplate ],
MULTI_SELECT: ( action, { getState } ) => {
const blockCount = getSelectedBlockCount( getState() );

Expand Down
Loading