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

Reusable blocks support for widgets editor #26097

Merged
Merged
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
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/edit-widgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@wordpress/media-utils": "file:../media-utils",
"@wordpress/notices": "file:../notices",
"@wordpress/plugins": "file:../plugins",
"@wordpress/reusable-blocks": "file:../reusable-blocks",
"@wordpress/server-side-render": "file:../server-side-render",
"@wordpress/url": "file:../url",
"classnames": "^2.2.5",
Expand Down
3 changes: 2 additions & 1 deletion packages/edit-widgets/src/blocks/legacy-widget/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"supports": {
"html": false,
"customClassName": false
"customClassName": false,
"reusable": false
},
"parent": [
"core/widget-area"
Expand Down
1 change: 1 addition & 0 deletions packages/edit-widgets/src/blocks/widget-area/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"html": false,
"inserter": false,
"customClassName": false,
"reusable": false,
"__experimentalToolbar": false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,34 @@ import {
BlockEditorKeyboardShortcuts,
__unstableEditorStyles as EditorStyles,
} from '@wordpress/block-editor';
import { ReusableBlocksMenuItems } from '@wordpress/reusable-blocks';

/**
* Internal dependencies
*/
import KeyboardShortcuts from '../keyboard-shortcuts';
import { useEntityBlockEditor } from '@wordpress/core-data';
import { buildWidgetAreasPostId, KIND, POST_TYPE } from '../../store/utils';
import useLastSelectedWidgetArea from '../../hooks/use-last-selected-widget-area';

export default function WidgetAreasBlockEditorProvider( {
blockEditorSettings,
children,
...props
} ) {
const { hasUploadPermissions } = useSelect( ( select ) => ( {
hasUploadPermissions: defaultTo(
select( 'core' ).canUser( 'create', 'media' ),
true
),
widgetAreas: select( 'core/edit-widgets' ).getWidgetAreas(),
} ) );
const { hasUploadPermissions, reusableBlocks } = useSelect(
( select ) => ( {
hasUploadPermissions: defaultTo(
select( 'core' ).canUser( 'create', 'media' ),
true
),
widgetAreas: select( 'core/edit-widgets' ).getWidgetAreas(),
reusableBlocks: select( 'core' ).getEntityRecords(
'postType',
'wp_block'
),
} )
);

const settings = useMemo( () => {
let mediaUploadBlockEditor;
Expand All @@ -52,10 +61,13 @@ export default function WidgetAreasBlockEditorProvider( {
}
return {
...blockEditorSettings,
__experimentalReusableBlocks: reusableBlocks,
mediaUpload: mediaUploadBlockEditor,
templateLock: 'all',
};
}, [ blockEditorSettings, hasUploadPermissions ] );
}, [ blockEditorSettings, hasUploadPermissions, reusableBlocks ] );

const widgetAreaId = useLastSelectedWidgetArea();

const [ blocks, onInput, onChange ] = useEntityBlockEditor(
KIND,
Expand All @@ -78,7 +90,12 @@ export default function WidgetAreasBlockEditorProvider( {
settings={ settings }
useSubRegistry={ false }
{ ...props }
/>
>
{ children }
<ReusableBlocksMenuItems
rootClientId={ widgetAreaId }
/>
</BlockEditorProvider>
</FocusReturnProvider>
</DropZoneProvider>
</SlotFillProvider>
Expand Down
1 change: 1 addition & 0 deletions packages/edit-widgets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
registerCoreBlocks,
__experimentalRegisterExperimentalCoreBlocks,
} from '@wordpress/block-library';
import '@wordpress/reusable-blocks';

/**
* Internal dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import { withSelect } from '@wordpress/data';
import ReusableBlockConvertButton from './reusable-block-convert-button';
import ReusableBlockDeleteButton from './reusable-block-delete-button';

function ReusableBlocksMenuItems( { clientIds } ) {
function ReusableBlocksMenuItems( { clientIds, rootClientId } ) {
return (
<>
<ReusableBlockConvertButton clientIds={ clientIds } />
<ReusableBlockConvertButton
clientIds={ clientIds }
rootClientId={ rootClientId }
/>
{ clientIds.length === 1 && (
<ReusableBlockDeleteButton clientId={ clientIds[ 0 ] } />
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ import { STORE_KEY } from '../../store/constants';
/**
* Menu control to convert block(s) to reusable block.
*
* @param {Object} props Component props.
* @param {string[]} props.clientIds Client ids of selected blocks.
*
* @param {Object} props Component props.
* @param {string[]} props.clientIds Client ids of selected blocks.
* @param {string} props.rootClientId ID of the currently selected top-level block.
* @return {import('@wordpress/element').WPComponent} The menu control or null.
*/
export default function ReusableBlockConvertButton( { clientIds } ) {
export default function ReusableBlockConvertButton( {
clientIds,
rootClientId,
} ) {
const canConvert = useSelect(
( select ) => {
const { canUser } = select( 'core' );
Expand All @@ -46,7 +49,7 @@ export default function ReusableBlockConvertButton( { clientIds } ) {
// Hide when this is already a reusable block.
! isReusable &&
// Hide when reusable blocks are disabled.
canInsertBlockType( 'core/block' ) &&
canInsertBlockType( 'core/block', rootClientId ) &&
blocks.every(
( block ) =>
// Guard against the case where a regular block has *just* been converted.
Expand Down