Skip to content

Commit

Permalink
Reusable blocks support for widgets editor (#26097)
Browse files Browse the repository at this point in the history
* Reusable blocks support for widgets editor

* Fix creating reusable blocks

* Add reusable: false

* Update component name

Co-authored-by: tellthemachines <isabel@tellthemachines.com>
  • Loading branch information
adamziel and tellthemachines committed Oct 19, 2020
1 parent 9fc1bb6 commit c195e3d
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 19 deletions.
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

0 comments on commit c195e3d

Please sign in to comment.