From 5722a4ef1c470a3b7b7ad6b9d7f66f9d84dfbca9 Mon Sep 17 00:00:00 2001 From: Ramon Date: Wed, 28 Sep 2022 08:16:48 +1000 Subject: [PATCH] Template part: prevent adding block in post editor or inside post template or content blocks (#44480) * Reordering the filter so that any filters added int the block library get applied first, and become overridden by post editor initialization. Renaming `can` to `canInsert` for clarity * Updated comment * Formatting inline JS comment betterer :P --- .../block-library/src/template-part/index.js | 4 +-- packages/edit-post/src/index.js | 36 ++++++++++--------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/packages/block-library/src/template-part/index.js b/packages/block-library/src/template-part/index.js index 562e4f713ee1a9..c64f093427f95f 100644 --- a/packages/block-library/src/template-part/index.js +++ b/packages/block-library/src/template-part/index.js @@ -62,13 +62,13 @@ export const init = () => { 'blockEditor.__unstableCanInsertBlockType', 'removeTemplatePartsFromPostTemplates', ( - can, + canInsert, blockType, rootClientId, { getBlock, getBlockParentsByBlockName } ) => { if ( blockType.name !== 'core/template-part' ) { - return can; + return canInsert; } for ( const disallowedParentType of DISALLOWED_PARENTS ) { diff --git a/packages/edit-post/src/index.js b/packages/edit-post/src/index.js index d489456f31b7bf..5b7c4e01ead7b1 100644 --- a/packages/edit-post/src/index.js +++ b/packages/edit-post/src/index.js @@ -80,22 +80,6 @@ export function initializeEditor( settings, initialEdits ) { - // Prevent adding template part in the post editor. - // Only add the filter when the post editor is initialized, not imported. - addFilter( - 'blockEditor.__unstableCanInsertBlockType', - 'removeTemplatePartsFromInserter', - ( can, blockType ) => { - if ( - ! select( editPostStore ).isEditingTemplate() && - blockType.name === 'core/template-part' - ) { - return false; - } - return can; - } - ); - const target = document.getElementById( id ); const reboot = reinitializeEditor.bind( null, @@ -137,6 +121,26 @@ export function initializeEditor( } ); } + /* + * Prevent adding template part in the post editor. + * Only add the filter when the post editor is initialized, not imported. + * Also only add the filter(s) after registerCoreBlocks() + * so that common filters in the block library are not overwritten. + */ + addFilter( + 'blockEditor.__unstableCanInsertBlockType', + 'removeTemplatePartsFromInserter', + ( canInsert, blockType ) => { + if ( + ! select( editPostStore ).isEditingTemplate() && + blockType.name === 'core/template-part' + ) { + return false; + } + return canInsert; + } + ); + // Show a console log warning if the browser is not in Standards rendering mode. const documentMode = document.compatMode === 'CSS1Compat' ? 'Standards' : 'Quirks';