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

Block Library: Implement template blocks using nested block editors. #17118

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
20 changes: 20 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ function gutenberg_reregister_core_block_types() {
}
add_action( 'init', 'gutenberg_reregister_core_block_types' );

/**
* Adds new block categories needed by the Gutenberg plugin.
*
* @param array $categories List of block categories.
*
* @return array List of block categories with the new categories added.
*/
function gutenberg_filter_block_categories( $categories ) {
return array_merge(
$categories,
array(
array(
'slug' => 'theme',
'title' => __( 'Theme Blocks', 'gutenberg' ),
),
)
);
}
add_filter( 'block_categories', 'gutenberg_filter_block_categories' );

/**
* Registers a new block style.
*
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.

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 @@ -25,7 +26,9 @@ import {
getBlockTypes,
hasBlockSupport,
hasChildBlocksWithInserterSupport,
parse,
} from '@wordpress/blocks';
import { SVG, Rect, G, Path } from '@wordpress/components';

// Module constants

Expand All @@ -52,6 +55,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 @@ -646,29 +650,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 @@ -1112,41 +1093,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 );
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 @@ -1248,8 +1194,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 @@ -1260,7 +1209,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 @@ -1273,9 +1222,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 @@ -1312,9 +1261,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 @@ -1365,6 +1314,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