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

[RNMobile] Enable reusable block only in WP.com sites #31744

Merged
merged 13 commits into from
May 21, 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
2 changes: 1 addition & 1 deletion packages/block-library/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export const registerCoreBlocks = () => {
pullquote,
file,
audio,
devOnly( reusableBlock ),
reusableBlock,
search,
devOnly( embed ),
].forEach( registerBlock );
Expand Down
20 changes: 19 additions & 1 deletion packages/editor/src/components/provider/index.native.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* External dependencies
*/
import memize from 'memize';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -70,6 +72,15 @@ class NativeEditorProvider extends Component {
this.post.type,
this.post
);
this.getEditorSettings = memize(
( settings, capabilities ) => ( {
...settings,
capabilities,
} ),
{
maxSize: 1,
}
);
Comment on lines +75 to +83
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the capabilities into the editor settings that are passed to the EditorProvider, this is required because we need them when executing the useNativeBlockEditorSettings hook.

Additionally, to prevent extra re-renders, I decided to memoize the editor settings object.

Copy link
Contributor

Choose a reason for hiding this comment

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

@fluiddot I was looking around the codebase for some context about what maxSize does but I wasn't able to figure it out. Could you please expound a bit on its usage?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it utilized to define the maximum size of the cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly, this parameter controls the size of the cache, here you can check the documentation of the memize package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There’s a similar logic in this file:

Copy link
Contributor

Choose a reason for hiding this comment

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

Good good. Thanks for confirming 🙇🏾

}

componentDidMount() {
Expand Down Expand Up @@ -267,11 +278,18 @@ class NativeEditorProvider extends Component {
const {
children,
post, // eslint-disable-line no-unused-vars
capabilities,
settings,
...props
} = this.props;
const editorSettings = this.getEditorSettings( settings, capabilities );

return (
<EditorProvider post={ this.post } { ...props }>
<EditorProvider
post={ this.post }
settings={ editorSettings }
{ ...props }
>
{ children }
</EditorProvider>
);
Expand Down
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: 100 } ) }
),
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;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ data class GutenbergProps @JvmOverloads constructor(
val enableUnsupportedBlockEditor: Boolean,
val canEnableUnsupportedBlockEditor: Boolean,
val enableAudioBlock: Boolean,
val enableReusableBlock: Boolean,
val localeSlug: String,
val postType: String,
val featuredImageId: Int,
Expand Down Expand Up @@ -45,6 +46,7 @@ data class GutenbergProps @JvmOverloads constructor(
putBoolean(PROP_CAPABILITIES_UNSUPPORTED_BLOCK_EDITOR, enableUnsupportedBlockEditor)
putBoolean(PROP_CAPABILITIES_CAN_ENABLE_UNSUPPORTED_BLOCK_EDITOR, canEnableUnsupportedBlockEditor)
putBoolean(PROP_CAPABILITIES_AUDIO_BLOCK, enableAudioBlock)
putBoolean(PROP_CAPABILITIES_REUSABLE_BLOCK, enableReusableBlock)
putBoolean(PROP_CAPABILITIES_CAN_VIEW_EDITOR_ONBOARDING, canViewEditorOnboarding)
}

Expand Down Expand Up @@ -74,6 +76,7 @@ data class GutenbergProps @JvmOverloads constructor(
const val PROP_CAPABILITIES_UNSUPPORTED_BLOCK_EDITOR = "unsupportedBlockEditor"
const val PROP_CAPABILITIES_CAN_ENABLE_UNSUPPORTED_BLOCK_EDITOR = "canEnableUnsupportedBlockEditor"
const val PROP_CAPABILITIES_AUDIO_BLOCK = "audioBlock"
const val PROP_CAPABILITIES_REUSABLE_BLOCK = "reusableBlock"
const val PROP_CAPABILITIES_CAN_VIEW_EDITOR_ONBOARDING = "canViewEditorOnboarding"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum Capabilities: String {
case unsupportedBlockEditor
case canEnableUnsupportedBlockEditor
case audioBlock
case reusableBlock
case canViewEditorOnboarding
}

Expand Down
17 changes: 4 additions & 13 deletions packages/react-native-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,19 @@ For each user feature we should also add a importance categorization label to i
-->

## Unreleased
- [***] Slash inserter

- [***] Slash inserter [#29772]
- [*] Audio block: Add Insert from URL functionality. [#27817]
- [*] Fix missing title for some unsupported blocks [#31743]
- [*] The BottomSheet Cell component now supports the help prop so that a hint can be supplied to all Cell based components. [#30885]
- [***] Enable reusable block only in WP.com sites [#31744]

## 1.53.0

- [*] Bottom-sheet: Add custom header [#30291]
- [*] Fixes color picker rendering bug when scrolling [#30994]
- [*] Add enableCaching param to fetch request on Android [#31186]
- [***] Add reusable blocks to the inserter menu. [#28495]
- [*] The BottomSheet Cell component now supports the help prop so that a hint can be supplied to all Cell based components. [#30885]

## 1.52.2

- [*] Disabled featured image banner on iOS. [#31681]
- [*] Fix missing title for some unsupported blocks [#31743]

## 1.52.1

- [*] Fixes for the generated localized strings files.

## 1.52.0

## 1.52.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected Bundle getLaunchOptions() {
capabilities.putBoolean(GutenbergProps.PROP_CAPABILITIES_XPOSTS, true);
capabilities.putBoolean(GutenbergProps.PROP_CAPABILITIES_UNSUPPORTED_BLOCK_EDITOR, true);
capabilities.putBoolean(GutenbergProps.PROP_CAPABILITIES_AUDIO_BLOCK, true);
capabilities.putBoolean(GutenbergProps.PROP_CAPABILITIES_REUSABLE_BLOCK, false);
capabilities.putBoolean(GutenbergProps.PROP_CAPABILITIES_CAN_VIEW_EDITOR_ONBOARDING, false);
bundle.putBundle(GutenbergProps.PROP_CAPABILITIES, capabilities);
return bundle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ extension GutenbergViewController: GutenbergBridgeDataSource {
.canEnableUnsupportedBlockEditor: unsupportedBlockCanBeActivated,
.mediaFilesCollectionBlock: true,
.audioBlock: true,
.reusableBlock: false,
.canViewEditorOnboarding: false
]
}
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
validateThemeGradients,
} from '@wordpress/block-editor';
import { dispatch } from '@wordpress/data';
import { unregisterBlockType } from '@wordpress/blocks';

const reactNativeSetup = () => {
// Disable warnings as they disrupt the user experience in dev mode
Expand Down Expand Up @@ -56,6 +57,11 @@ const setupInitHooks = () => {
'core/react-native-editor',
( props ) => {
setupLocale( props.locale, props.translations );

const capabilities = props.capabilities ?? {};
if ( capabilities.reusableBlock !== true ) {
unregisterBlockType( 'core/block' );
jd-alexander marked this conversation as resolved.
Show resolved Hide resolved
}
}
);

Expand Down