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

[WIP] Check if a block can be inserted when finding raw transforms #12799

Closed
Closed
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
5 changes: 4 additions & 1 deletion packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
* WordPress dependencies
*/
import { createHooks, applyFilters } from '@wordpress/hooks';
import { select } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -285,6 +286,8 @@ export function findTransform( transforms, predicate ) {
* @return {Array} Block transforms for direction.
*/
export function getBlockTransforms( direction, blockTypeOrName ) {
const { canInsertBlockType } = select( 'core/editor' );
Copy link
Member

Choose a reason for hiding this comment

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

This creates a circular dependency between editor and blocks. The blocks module should not be aware of editor.

Can this be enforced from the editor module instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

If we could introduce a filter, something like:

availableTransforms = applyFilters( 'blocks.transforms', transforms );

then the editor module could register a filter to remove any transforms to blocks that weren't allowed. How does that sound? It seems that this filtering does need to happen here, because if the editor gets a block type that isn't allowed, it doesn't have any information about what to do with it, because the content of the paragraph block that should be created instead could live in any attribute of the block that isn't allowed.

Copy link
Member

Choose a reason for hiding this comment

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

As an alternative to a filter, could we pass as an additional argument an array of "available" block names which can be applied as a filter for the block type? I could see this might be made more difficult by the fact there are other functions within the blocks module which call getBlockTransforms, it's not just called by the editor module.

My worry with a filter might be that in mind of a refactor like proposed in #7119, there may be multiple editors present which each have their own set of limited blocks, which seems difficult/impossible to manage from a single filter.

Copy link
Member Author

Choose a reason for hiding this comment

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

Passing in an argument would work for cases where editor needs a list of valid transforms, but pasteHandler from blocks is used in the RichText component to handle incoming pastes, and neither of these have a dependency on editor, and I figure that we don't want to introduce that either. So we'd have to have a new prop on RichText that was the list of allowed, or denied, block types, so that the RichText instances for the editor instance would have the right list. Does that seem right to you? It would mean altering every instantiation of a RichText component, wouldn't it?


// When retrieving transforms for all block types, recurse into self.
if ( blockTypeOrName === undefined ) {
return flatMap(
Expand All @@ -296,7 +299,7 @@ export function getBlockTransforms( direction, blockTypeOrName ) {
// Validate that block type exists and has array of direction.
const blockType = normalizeBlockType( blockTypeOrName );
const { name: blockName, transforms } = blockType || {};
if ( ! transforms || ! Array.isArray( transforms[ direction ] ) ) {
if ( ! canInsertBlockType( blockName ) || ! transforms || ! Array.isArray( transforms[ direction ] ) ) {
return [];
}

Expand Down