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

Restrict template part inserter support to the site editor using filters #37065

Closed
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
3 changes: 2 additions & 1 deletion packages/block-library/src/template-part/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"supports": {
"align": true,
"html": false,
"reusable": false
"reusable": false,
"inserter": false
},
"editorStyle": "wp-block-template-part-editor"
}
22 changes: 22 additions & 0 deletions packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import './hooks';
import { store as editSiteStore } from './store';
import Editor from './components/editor';
import List from './components/list';
import { addFilter, removeFilter } from '@wordpress/hooks';

/**
* Reinitializes the editor after the user chooses to reboot the editor after
Expand Down Expand Up @@ -57,7 +58,15 @@ export function initializeEditor( id, settings ) {
const reboot = reinitializeEditor.bind( null, target, settings );

dispatch( blocksStore ).__experimentalReapplyBlockTypeFilters();

addFilter(
'blocks.registerBlockType',
'core/edit-site/block-register',
addInsertersupportForTemplatePartBlock
);
registerCoreBlocks();
Copy link
Contributor Author

@adamziel adamziel Dec 2, 2021

Choose a reason for hiding this comment

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

If this approach sticks, I can see moving towards a declarative approach. E.g. imagine having an editor.json like:

{
    "blockOverrides": {
        "core/template-part": {
            "supports": {
                "inserter": true
            }
        }
    }
}

Or a call like

registerCoreBlocksWithOverrides([
    setBlockSupport("core/template-part", "inserter", true)
])

Copy link
Contributor

Choose a reason for hiding this comment

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

I like this idea of different per editor context. I don't want to derail the conversation as this is worth a separate issue, but how would a 3rd-party block achieve the same functionality (working only the site editor) if they can only pack their block.json settings but don't have access to the editor.json one?

removeFilter( 'blocks.registerBlockType', 'core/edit-site/block-register' );

if ( process.env.GUTENBERG_PHASE === 2 ) {
__experimentalRegisterExperimentalCoreBlocks( {
enableFSEBlocks: true,
Expand All @@ -70,6 +79,19 @@ export function initializeEditor( id, settings ) {
);
}

function addInsertersupportForTemplatePartBlock( definition ) {
if ( definition.name === 'core/template-part' ) {
return {
...definition,
supports: {
...definition.supports,
inserter: true,
},
};
}
return definition;
}

/**
* Initializes the site editor templates list screen.
*
Expand Down