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

Try refactoring the reusable blocks to use a separate block editor #14367

Merged
merged 14 commits into from
Sep 10, 2019
Merged
2 changes: 0 additions & 2 deletions packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import '@wordpress/viewport';
/**
* Internal dependencies
*/
import './store';
import './hooks';

export * from './components';
export * from './utils';
export { storeConfig } from './store';
Expand Down
108 changes: 41 additions & 67 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
orderBy,
reduce,
some,
find,
} from 'lodash';
import createSelector from 'rememo';

Expand All @@ -24,7 +25,9 @@ import {
getBlockType,
getBlockTypes,
hasBlockSupport,
parse,
} from '@wordpress/blocks';
import { SVG, Rect, G, Path } from '@wordpress/components';

// Module constants

Expand All @@ -51,6 +54,7 @@ export const INSERTER_UTILITY_NONE = 0;
const MILLISECONDS_PER_HOUR = 3600 * 1000;
const MILLISECONDS_PER_DAY = 24 * 3600 * 1000;
const MILLISECONDS_PER_WEEK = 7 * 24 * 3600 * 1000;
const templateIcon = <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><Rect x="0" fill="none" width="24" height="24" /><G><Path d="M19 3H5c-1.105 0-2 .895-2 2v14c0 1.105.895 2 2 2h14c1.105 0 2-.895 2-2V5c0-1.105-.895-2-2-2zM6 6h5v5H6V6zm4.5 13C9.12 19 8 17.88 8 16.5S9.12 14 10.5 14s2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5zm3-6l3-5 3 5h-6z" /></G></SVG>;

/**
* Shared reference to an empty array for cases where it is important to avoid
Expand Down Expand Up @@ -645,29 +649,6 @@ export function getLastMultiSelectedBlockClientId( state ) {
return last( getMultiSelectedBlockClientIds( state ) ) || null;
}

/**
* Checks if possibleAncestorId is an ancestor of possibleDescendentId.
*
* @param {Object} state Editor state.
* @param {string} possibleAncestorId Possible ancestor client ID.
* @param {string} possibleDescendentId Possible descent client ID.
*
* @return {boolean} True if possibleAncestorId is an ancestor
* of possibleDescendentId, and false otherwise.
*/
const isAncestorOf = createSelector(
( state, possibleAncestorId, possibleDescendentId ) => {
let idToCheck = possibleDescendentId;
while ( possibleAncestorId !== idToCheck && idToCheck ) {
idToCheck = getBlockRootClientId( state, idToCheck );
}
return possibleAncestorId === idToCheck;
},
( state ) => [
state.blocks.order,
],
);

/**
* Returns true if a multi-selection exists, and the block corresponding to the
* specified client ID is the first block of the multi-selection set, or false
Expand Down Expand Up @@ -1111,41 +1092,6 @@ const canIncludeBlockTypeInInserter = ( state, blockType, rootClientId ) => {
return canInsertBlockTypeUnmemoized( state, blockType.name, rootClientId );
};

/**
* Returns whether we can show a reusable block in the inserter
*
* @param {Object} state Global State
* @param {Object} reusableBlock Reusable block object
* @param {?string} rootClientId Optional root client ID of block list.
*
* @return {boolean} Whether the given block type is allowed to be shown in the inserter.
*/
const canIncludeReusableBlockInInserter = ( state, reusableBlock, rootClientId ) => {
if ( ! canInsertBlockTypeUnmemoized( state, 'core/block', rootClientId ) ) {
return false;
}

const referencedBlockName = getBlockName( state, reusableBlock.clientId );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since a reusable block can have contain blocks, no blocks or a single block, I don't think it makes sense to validate if the content is "includable" in the inserter, we should only validate if core/block is allowed.

This was inconsistent before because of the core/template trick we had.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, is it something which could be extracted to its own pull request, if you're suggesting it ought not have been implemented this way in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think it can because right now the reusable block edit function uses this parsed block with BlockEdit component to render it and update. It's very much tied to the way the reusable blocks are edited today as part of the global editor.

if ( ! referencedBlockName ) {
return false;
}

const referencedBlockType = getBlockType( referencedBlockName );
if ( ! referencedBlockType ) {
return false;
}

if ( ! canInsertBlockTypeUnmemoized( state, referencedBlockName, rootClientId ) ) {
return false;
}

if ( isAncestorOf( state, reusableBlock.clientId, rootClientId ) ) {
return false;
}

return true;
};

/**
* Determines the items that appear in the inserter. Includes both static
* items (e.g. a regular block type) and dynamic items (e.g. a reusable block).
Expand Down Expand Up @@ -1246,8 +1192,11 @@ export const getInserterItems = createSelector(
const buildReusableBlockInserterItem = ( reusableBlock ) => {
const id = `core/block/${ reusableBlock.id }`;

const referencedBlockName = getBlockName( state, reusableBlock.clientId );
const referencedBlockType = getBlockType( referencedBlockName );
const referencedBlocks = __experimentalGetParsedReusableBlock( state, reusableBlock.id );
let referencedBlockType;
if ( referencedBlocks.length === 1 ) {
referencedBlockType = getBlockType( referencedBlocks[ 0 ].name );
}

const { time, count = 0 } = getInsertUsage( state, id ) || {};
const utility = calculateUtility( 'reusable', count, false );
Expand All @@ -1258,7 +1207,7 @@ export const getInserterItems = createSelector(
name: 'core/block',
initialAttributes: { ref: reusableBlock.id },
title: reusableBlock.title,
icon: referencedBlockType.icon,
icon: referencedBlockType ? referencedBlockType.icon : templateIcon,
category: 'reusable',
keywords: [],
isDisabled: false,
Expand All @@ -1271,9 +1220,9 @@ export const getInserterItems = createSelector(
.filter( ( blockType ) => canIncludeBlockTypeInInserter( state, blockType, rootClientId ) )
.map( buildBlockTypeInserterItem );

const reusableBlockInserterItems = getReusableBlocks( state )
.filter( ( block ) => canIncludeReusableBlockInInserter( state, block, rootClientId ) )
.map( buildReusableBlockInserterItem );
const reusableBlockInserterItems = canInsertBlockTypeUnmemoized( state, 'core/block', rootClientId ) ?
getReusableBlocks( state ).map( buildReusableBlockInserterItem ) :
[];

return orderBy(
[ ...blockTypeInserterItems, ...reusableBlockInserterItems ],
Expand Down Expand Up @@ -1310,9 +1259,9 @@ export const hasInserterItems = createSelector(
if ( hasBlockType ) {
return true;
}
const hasReusableBlock = some(
getReusableBlocks( state ),
( block ) => canIncludeReusableBlockInInserter( state, block, rootClientId )
const hasReusableBlock = (
canInsertBlockTypeUnmemoized( state, 'core/block', rootClientId ) &&
getReusableBlocks( state ).length > 0
);

return hasReusableBlock;
Expand Down Expand Up @@ -1363,6 +1312,31 @@ export function isLastBlockChangePersistent( state ) {
return state.blocks.isPersistentChange;
}

/**
* Returns the parsed block saved as shared block with the given ID.
*
* @param {Object} state Global application state.
* @param {number|string} ref The shared block's ID.
*
* @return {Object} The parsed block.
*/
export const __experimentalGetParsedReusableBlock = createSelector(
( state, ref ) => {
const reusableBlock = find(
getReusableBlocks( state ),
( block ) => block.id === ref
);
if ( ! reusableBlock ) {
return null;
}

return parse( reusableBlock.content );
},
( state ) => [
getReusableBlocks( state ),
],
);

/**
* Returns true if the most recent block change is be considered ignored, or
* false otherwise. An ignored change is one not to be committed by
Expand Down
Loading