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

Make Reusable blocks available in the Site Editor #36511

Merged
merged 8 commits into from
Nov 24, 2021
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
1 change: 1 addition & 0 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-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@wordpress/notices": "file:../notices",
"@wordpress/plugins": "file:../plugins",
"@wordpress/primitives": "file:../primitives",
"@wordpress/reusable-blocks": "file:../reusable-blocks",
"@wordpress/url": "file:../url",
"classnames": "^2.3.1",
"downloadjs": "^1.4.7",
Expand Down
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useMergeRefs, useViewportMatch } from '@wordpress/compose';
import { ReusableBlocksMenuItems } from '@wordpress/reusable-blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -132,6 +133,7 @@ export default function BlockEditor( { setIsInserterOpen } ) {
) }
</__unstableBlockSettingsMenuFirstItem>
</BlockTools>
<ReusableBlocksMenuItems />
</BlockEditorProvider>
);
}
19 changes: 19 additions & 0 deletions packages/edit-site/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { store as coreDataStore } from '@wordpress/core-data';
import { createRegistrySelector } from '@wordpress/data';
import { uploadMedia } from '@wordpress/media-utils';
import { isTemplatePart } from '@wordpress/blocks';
import { Platform } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -64,6 +65,22 @@ export const getCanUserCreateMedia = createRegistrySelector( ( select ) => () =>
select( coreDataStore ).canUser( 'create', 'media' )
);

/**
* Returns any available Reusable blocks.
*
* @param {Object} state Global application state.
*
* @return {Array} The available reusable blocks.
*/
export const getReusableBlocks = createRegistrySelector( ( select ) => () => {
const isWeb = Platform.OS === 'web';
return isWeb
? select( coreDataStore ).getEntityRecords( 'postType', 'wp_block', {
per_page: -1,
} )
: [];
} );

/**
* Returns the settings, taking into account active features and permissions.
*
Expand All @@ -80,6 +97,7 @@ export const getSettings = createSelector(
focusMode: isFeatureActive( state, 'focusMode' ),
hasFixedToolbar: isFeatureActive( state, 'fixedToolbar' ),
__experimentalSetIsInserterOpened: setIsInserterOpen,
__experimentalReusableBlocks: getReusableBlocks( state ),
};

const canUserCreateMedia = getCanUserCreateMedia( state );
Expand All @@ -101,6 +119,7 @@ export const getSettings = createSelector(
state.settings,
isFeatureActive( state, 'focusMode' ),
isFeatureActive( state, 'fixedToolbar' ),
getReusableBlocks( state ),
]
);

Expand Down
24 changes: 24 additions & 0 deletions packages/edit-site/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ import {
getPreviousEditedPostId,
getPage,
getNavigationPanelActiveMenu,
getReusableBlocks,
isNavigationOpened,
isInserterOpened,
isListViewOpened,
} from '../selectors';

describe( 'selectors', () => {
const canUser = jest.fn( () => true );
const getEntityRecords = jest.fn( () => [] );
getCanUserCreateMedia.registry = {
select: jest.fn( () => ( { canUser } ) ),
};
getReusableBlocks.registry = {
select: jest.fn( () => ( { getEntityRecords } ) ),
};

describe( 'isFeatureActive', () => {
it( 'is tolerant to an undefined features preference', () => {
Expand Down Expand Up @@ -83,6 +88,22 @@ describe( 'selectors', () => {
} );
} );

describe( 'getReusableBlocks', () => {
it( "selects `getEntityRecords( 'postType', 'wp_block' )` from the core store", () => {
expect( getReusableBlocks() ).toEqual( [] );
expect( getReusableBlocks.registry.select ).toHaveBeenCalledWith(
coreDataStore
);
expect( getEntityRecords ).toHaveBeenCalledWith(
'postType',
'wp_block',
{
per_page: -1,
}
);
} );
} );

describe( 'getSettings', () => {
it( "returns the settings when the user can't create media", () => {
canUser.mockReturnValueOnce( false );
Expand All @@ -94,6 +115,7 @@ describe( 'selectors', () => {
focusMode: false,
hasFixedToolbar: false,
__experimentalSetIsInserterOpened: setInserterOpened,
__experimentalReusableBlocks: [],
} );
} );

Expand All @@ -108,12 +130,14 @@ describe( 'selectors', () => {
},
};
const setInserterOpened = () => {};

expect( getSettings( state, setInserterOpened ) ).toEqual( {
outlineMode: true,
key: 'value',
focusMode: true,
hasFixedToolbar: true,
__experimentalSetIsInserterOpened: setInserterOpened,
__experimentalReusableBlocks: [],
mediaUpload: expect.any( Function ),
} );
} );
Expand Down