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

[Edit Post]: Hide Template part in post editor #36810

Closed
wants to merge 11 commits into from
Closed
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function UncontrolledInnerBlocks( props ) {
const {
clientId,
allowedBlocks,
__unstableDisallowedBlocks,
__experimentalDefaultBlock,
__experimentalDirectInsert,
template,
Expand All @@ -59,6 +60,7 @@ function UncontrolledInnerBlocks( props ) {
useNestedSettingsUpdate(
clientId,
allowedBlocks,
__unstableDisallowedBlocks,
__experimentalDefaultBlock,
__experimentalDirectInsert,
templateLock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getLayoutType } from '../../layouts';
* @param {string} clientId The client ID of the block to update.
* @param {string[]} allowedBlocks An array of block names which are permitted
* in inner blocks.
* @param {string[]} __unstableDisallowedBlocks An array of block names which are not permitted in inner blocks.
* @param {?Array} __experimentalDefaultBlock The default block to insert: [ blockName, { blockAttributes } ].
* @param {?Function|boolean} __experimentalDirectInsert If a default block should be inserted directly by the
* appender.
Expand All @@ -36,6 +37,7 @@ import { getLayoutType } from '../../layouts';
export default function useNestedSettingsUpdate(
clientId,
allowedBlocks,
__unstableDisallowedBlocks,
__experimentalDefaultBlock,
__experimentalDirectInsert,
templateLock,
Expand Down Expand Up @@ -65,10 +67,15 @@ export default function useNestedSettingsUpdate(
// Memoize as inner blocks implementors often pass a new array on every
// render.
const _allowedBlocks = useMemo( () => allowedBlocks, allowedBlocks );
const _disallowedBlocks = useMemo(
() => __unstableDisallowedBlocks,
__unstableDisallowedBlocks
);
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved

useLayoutEffect( () => {
const newSettings = {
allowedBlocks: _allowedBlocks,
__unstableDisallowedBlocks: _disallowedBlocks,
templateLock:
templateLock === undefined ? parentLock : templateLock,
};
Expand Down Expand Up @@ -103,6 +110,7 @@ export default function useNestedSettingsUpdate(
clientId,
blockListSettings,
_allowedBlocks,
__unstableDisallowedBlocks,
__experimentalDefaultBlock,
__experimentalDirectInsert,
templateLock,
Expand Down
19 changes: 18 additions & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
filter,
mapKeys,
orderBy,
without,
} from 'lodash';
import createSelector from 'rememo';

Expand Down Expand Up @@ -1246,7 +1247,23 @@ const canInsertBlockTypeUnmemoized = (
return false;
}

const parentAllowedBlocks = parentBlockListSettings?.allowedBlocks;
let parentAllowedBlocks = parentBlockListSettings?.allowedBlocks;
const parentDisallowedBlocks =
parentBlockListSettings?.__unstableDisallowedBlocks;

// Handle the case where both properties are provided.
if ( parentAllowedBlocks && parentDisallowedBlocks ) {
parentAllowedBlocks = without(
parentAllowedBlocks,
...parentDisallowedBlocks
);
}
if (
! parentAllowedBlocks &&
parentDisallowedBlocks?.includes( blockName )
) {
return false;
}
const hasParentAllowedBlock = checkAllowList(
parentAllowedBlocks,
blockName
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-content/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function EditableContent( { layout, context = {} } ) {
onInput,
onChange,
__experimentalLayout: themeSupportsLayout ? usedLayout : undefined,
__unstableDisallowedBlocks: [ 'core/template-part' ],
}
);
return <div { ...props } />;
Expand Down
4 changes: 4 additions & 0 deletions packages/e2e-test-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ _Parameters_
- _object.excerpt_ `[string]`: Excerpt of the new post.
- _object.showWelcomeGuide_ `[boolean]`: Whether to show the welcome guide.

### createReusableBlock

Undocumented declaration.

### createURL

Creates new URL by parsing base URL, WPPath and query string.
Expand Down
35 changes: 35 additions & 0 deletions packages/e2e-test-utils/src/create-reusable-block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Internal dependencies
*/
import { insertBlock } from './inserter';
import { clickMenuItem } from './click-menu-item';
import { clickBlockToolbarButton } from './click-block-toolbar-button';

const reusableBlockNameInputSelector =
'.reusable-blocks-menu-items__convert-modal .components-text-control__input';

export const createReusableBlock = async ( content, title ) => {
// Insert a paragraph block
await insertBlock( 'Paragraph' );
await page.keyboard.type( content );

await clickBlockToolbarButton( 'Options' );
await clickMenuItem( 'Add to Reusable blocks' );
const nameInput = await page.waitForSelector(
reusableBlockNameInputSelector
);
await nameInput.click();
await page.keyboard.type( title );
await page.keyboard.press( 'Enter' );

// Wait for creation to finish
await page.waitForXPath(
'//*[contains(@class, "components-snackbar")]/*[text()="Reusable block created."]'
);

// Check that we have a reusable block on the page
const block = await page.waitForSelector(
'.block-editor-block-list__block[data-type="core/block"]'
);
expect( block ).not.toBeNull();
};
1 change: 1 addition & 0 deletions packages/e2e-test-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export { clickMenuItem } from './click-menu-item';
export { clickOnCloseModalButton } from './click-on-close-modal-button';
export { clickOnMoreMenuItem } from './click-on-more-menu-item';
export { createNewPost } from './create-new-post';
export { createReusableBlock } from './create-reusable-block';
export { createUser } from './create-user';
export { createURL } from './create-url';
export { deactivatePlugin } from './deactivate-plugin';
Expand Down
37 changes: 10 additions & 27 deletions packages/e2e-tests/specs/experiments/multi-entity-saving.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
trashAllPosts,
activateTheme,
clickButton,
createReusableBlock,
} from '@wordpress/e2e-test-utils';

/**
Expand All @@ -21,13 +22,9 @@ describe( 'Multi-entity save flow', () => {
const checkedBoxSelector = '.components-checkbox-control__checked';
const checkboxInputSelector = '.components-checkbox-control__input';
const entitiesSaveSelector = '.editor-entities-saved-states__save-button';
const templatePartSelector = '*[data-type="core/template-part"]';
const activatedTemplatePartSelector = `${ templatePartSelector }.block-editor-block-list__layout`;
const savePanelSelector = '.entities-saved-states__panel';
const closePanelButtonSelector =
'.editor-post-publish-panel__header-cancel-button button';
const createNewButtonSelector =
'//button[contains(text(), "New template part")]';

// Reusable assertions across Post/Site editors.
const assertAllBoxesChecked = async () => {
Expand Down Expand Up @@ -66,8 +63,6 @@ describe( 'Multi-entity save flow', () => {
const saveA11ySelector =
'.edit-post-layout__toggle-entities-saved-states-panel-button';
const publishPanelSelector = '.editor-post-publish-panel';
const confirmTitleButtonSelector =
'.wp-block-template-part__placeholder-create-new__title-form .components-button.is-primary';

// Reusable assertions inside Post editor.
const assertMultiSaveEnabled = async () => {
Expand Down Expand Up @@ -101,20 +96,10 @@ describe( 'Multi-entity save flow', () => {
await assertExistance( savePanelSelector, false );

// Add a template part and edit it.
await insertBlock( 'Template Part' );
const createNewButton = await page.waitForXPath(
createNewButtonSelector
);
await createNewButton.click();
const confirmTitleButton = await page.waitForSelector(
confirmTitleButtonSelector
);
await confirmTitleButton.click();

await page.waitForSelector( activatedTemplatePartSelector );
await page.click( '.block-editor-button-block-appender' );
await page.click( '.editor-block-list-item-paragraph' );
await page.keyboard.type( 'some words...' );
await createReusableBlock( 'Hi!', 'Test' );
await page.waitForSelector( 'p[data-type="core/paragraph"]' );
await page.click( 'p[data-type="core/paragraph"]' );
await page.keyboard.type( 'Oh!' );

// Should trigger multi-entity save button once template part edited.
await assertMultiSaveEnabled();
Expand Down Expand Up @@ -166,13 +151,11 @@ describe( 'Multi-entity save flow', () => {
await assertMultiSaveDisabled();
await assertExistance( saveA11ySelector, false );

// Update template part.
await page.click( templatePartSelector );
await page.click(
`${ templatePartSelector } .wp-block[data-type="core/paragraph"]`
);
await page.keyboard.type( '...some more words...' );
await page.keyboard.press( 'Enter' );
// Update reusable block again.
await page.click( 'p[data-type="core/paragraph"]' );
// We need to click again due to the clickthrough overlays in reusable blocks.
await page.click( 'p[data-type="core/paragraph"]' );
await page.keyboard.type( 'R!' );

// Multi-entity saving should be enabled.
await assertMultiSaveEnabled();
Expand Down
24 changes: 15 additions & 9 deletions packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,27 @@ function Editor( {
keepCaretInsideBlock,
};

// Defer to passed setting for `allowedBlockTypes` if provided as
// anything other than `true` (where `true` is equivalent to allow
// all block types).
const defaultAllowedBlockTypes =
true === settings.allowedBlockTypes
? map( blockTypes, 'name' )
: settings.allowedBlockTypes || [];
// Omit hidden block types if exists and non-empty.
if ( size( hiddenBlockTypes ) > 0 ) {
// Defer to passed setting for `allowedBlockTypes` if provided as
// anything other than `true` (where `true` is equivalent to allow
// all block types).
const defaultAllowedBlockTypes =
true === settings.allowedBlockTypes
? map( blockTypes, 'name' )
: settings.allowedBlockTypes || [];

result.allowedBlockTypes = without(
defaultAllowedBlockTypes,
...hiddenBlockTypes
);
}

// Hide `Template Part` when we are not in template mode.
if ( ! isTemplateMode ) {
result.allowedBlockTypes = without(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

After starting the changes to introduce an editor setting __unstableDisallowedBlockTypes, it seems that requires many changes and IMO adds much more complexity for now, since we need a well thought API for handling disallowing blocks per context(and whatever this context will be).

@youknowriad do you have strong opinion about this? I can finish them and push to check if you want.

Copy link
Contributor

Choose a reason for hiding this comment

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

The simpler, the better for me :)

defaultAllowedBlockTypes,
'core/template-part'
);
}
return result;
}, [
settings,
Expand All @@ -158,6 +163,7 @@ function Editor( {
setIsInserterOpened,
updatePreferredStyleVariations,
keepCaretInsideBlock,
isTemplateMode,
] );

const styles = useMemo( () => {
Expand Down