Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add use-block-editor-settings native version
  • Loading branch information
fluiddot committed May 11, 2021
commit 68434fcda2a3d9693b411ff8b2e68dbd254233bc
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,18 @@ function useBlockEditorSettings( settings, hasTemplate ) {
const { canUserUseUnfilteredHTML, isPostTitleSelected } = select(
editorStore
);
const isWeb = Platform.OS === 'web';
const { canUser } = select( coreStore );

return {
canUseUnfilteredHTML: canUserUseUnfilteredHTML(),
reusableBlocks: select( coreStore ).getEntityRecords(
'postType',
'wp_block',
/**
* Unbounded queries are not supported on native so as a workaround, we set per_page with the maximum value that native version can handle.
* Related issue: https://github.com/wordpress-mobile/gutenberg-mobile/issues/2661
*/
{ per_page: Platform.select( { web: -1, native: 10 } ) }
),
reusableBlocks: isWeb
? select( coreStore ).getEntityRecords(
'postType',
'wp_block',
{ per_page: -1 }
)
: [], // Reusable blocks are fetched in the native version of this hook.
Comment on lines +46 to +52
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reusable blocks are fetched in the native version of this hook, so we only fetch them for the web version.

hasUploadPermissions: defaultTo(
canUser( 'create', 'media' ),
true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import useBlockEditorSettings from './use-block-editor-settings.js';

function useNativeBlockEditorSettings( settings, hasTemplate ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The purpose of this hook is to add custom code to the useBlockEditorSettings hook, which will be only executed in the native version of the editor.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the context here. I can see that useNativeBlockEditorSettings is utilizing the web variant to create the editorSettings and then based on if support for reusable block is enabled or not based on the site type, we are then querying the coreStore for the said blocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah exactly, the main difference between this version and the web is that capabilities is only available in native. This is used, as you commented, to control whether it should query the reusable blocks.

const capabilities = settings.capabilities ?? {};
const editorSettings = useBlockEditorSettings( settings, hasTemplate );

const supportReusableBlock = capabilities.reusableBlock === true;
const { reusableBlocks } = useSelect(
( select ) => ( {
reusableBlocks: supportReusableBlock
? select( coreStore ).getEntityRecords(
'postType',
'wp_block',
// Unbounded queries are not supported on native so as a workaround, we set per_page with the maximum value that native version can handle.
// Related issue: https://github.com/wordpress-mobile/gutenberg-mobile/issues/2661
{ per_page: 100 }
)
: [],
} ),
[ supportReusableBlock ]
);

return useMemo(
() => ( {
...editorSettings,
__experimentalReusableBlocks: reusableBlocks,
} ),
[ reusableBlocks ]
);
}

export default useNativeBlockEditorSettings;