Skip to content

Commit

Permalink
Template part: prevent adding block in post editor or inside post tem…
Browse files Browse the repository at this point in the history
…plate 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
  • Loading branch information
ramonjd authored and michalczaplinski committed Oct 3, 2022
1 parent 61a1bc2 commit add234f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/block-library/src/template-part/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
36 changes: 20 additions & 16 deletions packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit add234f

Please sign in to comment.