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] Fix ungroup functionality in WarningMaxDepthExceeded component #56445

Merged
merged 1 commit into from
Nov 23, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TouchableWithoutFeedback, View } from 'react-native';
*/
import { __, sprintf } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -18,54 +18,71 @@ import UnsupportedBlockDetails from '../unsupported-block-details';
import { store as blockEditorStore } from '../../store';
import { MAX_NESTING_DEPTH } from './constants';
import useUnsupportedBlockEditor from '../use-unsupported-block-editor';
import {
useConvertToGroupButtons,
useConvertToGroupButtonProps,
} from '../convert-to-group-buttons';

const EMPTY_ARRAY = [];

const WarningMaxDepthExceeded = ( { clientId } ) => {
const [ showDetails, setShowDetails ] = useState( false );

const { isSelected, innerBlocks } = useSelect(
( select ) => {
const { getBlock, isBlockSelected } = select( blockEditorStore );
return {
innerBlocks: getBlock( clientId )?.innerBlocks || [],
isSelected: isBlockSelected( clientId ),
};
},
const isSelected = useSelect(
( select ) => select( blockEditorStore ).isBlockSelected( clientId ),
[ clientId ]
);
const { replaceBlocks } = useDispatch( blockEditorStore );

// We rely on the logic related to the Group/Ungroup buttons used in the block options to
// determine whether to use the Ungroup action.
const convertToGroupButtonProps = useConvertToGroupButtonProps( [
clientId,
] );
const { isUngroupable } = convertToGroupButtonProps;
const convertToGroupButtons = useConvertToGroupButtons( {
...convertToGroupButtonProps,
} );
const onUngroup = convertToGroupButtons.ungroup.onSelect;

const {
isUnsupportedBlockEditorSupported,
canEnableUnsupportedBlockEditor,
} = useUnsupportedBlockEditor( clientId );

const onUngroup = () => {
if ( ! innerBlocks.length ) {
return;
}

replaceBlocks( clientId, innerBlocks );
};

let description;
// When UBE can't be used, the description mentions using the web browser to edit the block.
/* translators: Warning related to having blocks deeply nested. %d: The deepest nesting level. */
const descriptionFormat = __(
'Blocks nested deeper than %d levels may not render properly in the mobile editor.'
);
let description = sprintf( descriptionFormat, MAX_NESTING_DEPTH );
if (
! isUnsupportedBlockEditorSupported &&
! canEnableUnsupportedBlockEditor
) {
/* translators: Warning related to having blocks deeply nested. %d: The deepest nesting level. */
const descriptionFormat = __(
'Blocks nested deeper than %d levels may not render properly in the mobile editor. For this reason, we recommend flattening the content by ungrouping the block or editing the block using your web browser.'
);
description = sprintf( descriptionFormat, MAX_NESTING_DEPTH );
// When UBE can't be used, the description mentions using the web browser to edit the block.
description +=
' ' +
/* translators: Recommendation included in a warning related to having blocks deeply nested. */
__(
'For this reason, we recommend editing the block using your web browser.'
);
}
// Otherwise, the description mentions using the web editor (i.e. UBE).
else {
/* translators: Warning related to having blocks deeply nested. %d: The deepest nesting level. */
const descriptionFormat = __(
'Blocks nested deeper than %d levels may not render properly in the mobile editor. For this reason, we recommend flattening the content by ungrouping the block or editing the block using the web editor.'
);
description = sprintf( descriptionFormat, MAX_NESTING_DEPTH );
description +=
' ' +
/* translators: Recommendation included in a warning related to having blocks deeply nested. */
__(
'For this reason, we recommend editing the block using the web editor.'
);
}
// If the block can be flattened, we also suggest to ungroup the block.
if ( isUngroupable ) {
description +=
' ' +
/* translators: Alternative option included in a warning related to having blocks deeply nested. */
__(
'Alternatively, you can flatten the content by ungrouping the block.'
);
}

return (
Expand All @@ -88,9 +105,16 @@ const WarningMaxDepthExceeded = ( { clientId } ) => {
onCloseSheet={ () => setShowDetails( false ) }
title={ __( 'Deeply nested block' ) }
description={ description }
customActions={ [
{ label: __( 'Ungroup block' ), onPress: onUngroup },
] }
customActions={
isUngroupable
? [
{
label: __( 'Ungroup block' ),
onPress: onUngroup,
},
]
: EMPTY_ARRAY
}
/>
</View>
</TouchableWithoutFeedback>
Expand Down