Skip to content

Commit

Permalink
[Mobile] - Quote block V2 support (#40133)
Browse files Browse the repository at this point in the history
* Mobile - Update Quote block to support V2

* Mobile - React Native editor tests - Update to pick the first main block list wrapper

* Mobile - Add Quote block V2 experimental flag

* Mobile - React native editor test - Revert block list change, this will be needed once the quote block v2 is enabled by default

* Mobile - Block library - registerCoreBlocks - Make param optional

* Mobile - Edit post test - Update beforeAll call

* Mobile - BlockQuotation - Restore removed code
  • Loading branch information
geriux committed Apr 13, 2022
1 parent 32bb60d commit 2fee6a6
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function BlockForType( {
onDeleteBlock,
onReplace,
parentWidth,
parentBlockAlignment,
wrapperProps,
blockWidth,
baseGlobalStyles,
Expand Down Expand Up @@ -95,6 +96,7 @@ function BlockForType( {
contentStyle={ contentStyle }
onDeleteBlock={ onDeleteBlock }
blockWidth={ blockWidth }
parentBlockAlignment={ parentBlockAlignment }
/>
<View onLayout={ getBlockWidth } />
</GlobalStylesContext.Provider>
Expand Down
14 changes: 12 additions & 2 deletions packages/block-library/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ const devOnly = ( block ) => ( !! __DEV__ ? block : null );
const iOSOnly = ( block ) =>
Platform.OS === 'ios' ? block : devOnly( block );

// To be removed once Quote V2 is released on the web editor.
function quoteCheck( quoteBlock, blocksFlags ) {
if ( blocksFlags?.__experimentalEnableQuoteBlockV2 ) {
quoteBlock.settings = quoteBlock?.settingsV2;
}
return quoteBlock;
}

// Hide the Classic block and SocialLink block
addFilter(
'blocks.registerBlockType',
Expand Down Expand Up @@ -230,8 +238,10 @@ addFilter(
*
* registerCoreBlocks();
* ```
* @param {Object} [blocksFlags] Experimental flags
*
*/
export const registerCoreBlocks = () => {
export const registerCoreBlocks = ( blocksFlags ) => {
// When adding new blocks to this list please also consider updating /src/block-support/supported-blocks.json in the Gutenberg-Mobile repo
[
paragraph,
Expand All @@ -244,7 +254,7 @@ export const registerCoreBlocks = () => {
nextpage,
separator,
list,
quote,
quoteCheck( quote, blocksFlags ),
mediaText,
preformatted,
gallery,
Expand Down
14 changes: 13 additions & 1 deletion packages/block-library/src/paragraph/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import { useSelect } from '@wordpress/data';

const name = 'core/paragraph';

const allowedParentBlockAlignments = [ 'left', 'center', 'right' ];

function ParagraphBlock( {
attributes,
mergeBlocks,
onReplace,
setAttributes,
style,
clientId,
parentBlockAlignment,
} ) {
const isRTL = useSelect( ( select ) => {
return !! select( blockEditorStore ).getSettings().isRTL;
Expand All @@ -40,6 +43,15 @@ function ParagraphBlock( {
const onAlignmentChange = useCallback( ( nextAlign ) => {
setAttributes( { align: nextAlign } );
}, [] );

const parentTextAlignment = allowedParentBlockAlignments.includes(
parentBlockAlignment
)
? parentBlockAlignment
: undefined;

const textAlignment = align || parentTextAlignment;

return (
<>
<BlockControls group="block">
Expand Down Expand Up @@ -82,7 +94,7 @@ function ParagraphBlock( {
onReplace={ onReplace }
onRemove={ onReplace ? () => onReplace( [] ) : undefined }
placeholder={ placeholder || __( 'Start writing…' ) }
textAlign={ align }
textAlign={ textAlignment }
__unstableEmbedURLOnPaste
/>
</>
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/quote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import settingsV2 from './v2';

const { name } = metadata;

export { metadata, name };
export { metadata, name, settingsV2 };

export const settingsV1 = {
icon,
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/quote/v2/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default function QuoteEdit( {
insertBlocksAfter,
clientId,
className,
style,
} ) {
const { citation, align } = attributes;

Expand All @@ -88,6 +89,7 @@ export default function QuoteEdit( {
className: classNames( className, {
[ `has-text-align-${ align }` ]: align,
} ),
...( ! isWebPlatform && { style } ),
} );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
Expand Down Expand Up @@ -128,6 +130,7 @@ export default function QuoteEdit( {
__unstableOnSplitAtEnd={ () =>
insertBlocksAfter( createBlock( 'core/paragraph' ) )
}
{ ...( ! isWebPlatform ? { textAlign: align } : {} ) }
/>
) }
</BlockQuotation>
Expand Down
4 changes: 3 additions & 1 deletion packages/edit-post/src/test/editor.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const unsupportedBlock = `
jest.useFakeTimers( 'legacy' );

describe( 'Editor', () => {
beforeAll( registerCoreBlocks );
beforeAll( () => {
registerCoreBlocks();
} );

it( 'detects unsupported block and sends hasUnsupportedBlocks true to native', () => {
RNReactNativeGutenbergBridge.editorDidMount = jest.fn();
Expand Down
18 changes: 12 additions & 6 deletions packages/primitives/src/block-quotation/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ import { View } from 'react-native';
/**
* WordPress dependencies
*/
import { Children, cloneElement } from '@wordpress/element';
import { withPreferredColorScheme } from '@wordpress/compose';
import { Children, cloneElement, forwardRef } from '@wordpress/element';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';
/**
* Internal dependencies
*/
import styles from './style.scss';

export const BlockQuotation = withPreferredColorScheme( ( props ) => {
const { getStylesFromColorScheme, style } = props;
export const BlockQuotation = forwardRef( ( { ...props }, ref ) => {
const { style } = props;

const blockQuoteStyle = [
getStylesFromColorScheme(
usePreferredColorSchemeStyle(
styles.wpBlockQuoteLight,
styles.wpBlockQuoteDark
),
style?.color && {
borderLeftColor: style.color,
},
style,
style?.backgroundColor && styles.paddingWithBackground,
];
const colorStyle = style?.color ? { color: style.color } : {};

Expand All @@ -43,5 +45,9 @@ export const BlockQuotation = withPreferredColorScheme( ( props ) => {
}
return child;
} );
return <View style={ blockQuoteStyle }>{ newChildren }</View>;
return (
<View ref={ ref } style={ blockQuoteStyle }>
{ newChildren }
</View>
);
} );
7 changes: 6 additions & 1 deletion packages/primitives/src/block-quotation/style.native.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
%wpBlockQuote-shared {
border-left-width: 4px;
border-left-style: solid;
padding-left: 8px;
padding-left: $block-edge-to-content;
padding-top: $dashed-border-space;
margin-left: 0;
}

Expand All @@ -19,3 +20,7 @@
margin-top: 16px;
font-size: 14px;
}

.paddingWithBackground {
padding-top: $block-edge-to-content - $block-selected-margin;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ data class GutenbergProps @JvmOverloads constructor(
?.let { putSerializable(PROP_IS_FSE_THEME, it) }
theme.getSerializable(PROP_GALLERY_WITH_IMAGE_BLOCKS)
?.let { putSerializable(PROP_GALLERY_WITH_IMAGE_BLOCKS, it) }
theme.getSerializable(PROP_QUOTE_BLOCK_V2)
?.let { putSerializable(PROP_QUOTE_BLOCK_V2, it) }
}
}

Expand Down Expand Up @@ -90,6 +92,7 @@ data class GutenbergProps @JvmOverloads constructor(
private const val PROP_FEATURES = "rawFeatures"
private const val PROP_IS_FSE_THEME = "isFSETheme"
private const val PROP_GALLERY_WITH_IMAGE_BLOCKS = "galleryWithImageBlocks"
private const val PROP_QUOTE_BLOCK_V2 = "quoteBlockV2"

const val PROP_LOCALE = "locale"
const val PROP_CAPABILITIES = "capabilities"
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native-bridge/ios/Gutenberg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ public class Gutenberg: UIResponder {
settingsUpdates["galleryWithImageBlocks"] = galleryWithImageBlocks
}

if let quoteBlockV2 = editorSettings?.quoteBlockV2 {
settingsUpdates["quoteBlockV2"] = quoteBlockV2
}

if let rawStyles = editorSettings?.rawStyles {
settingsUpdates["rawStyles"] = rawStyles
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public extension GutenbergBridgeDataSource {
public protocol GutenbergEditorSettings {
var isFSETheme: Bool { get }
var galleryWithImageBlocks: Bool { get }
var quoteBlockV2: Bool { get }
var rawStyles: String? { get }
var rawFeatures: String? { get }
var colors: [[String: String]]? { get }
Expand Down
12 changes: 8 additions & 4 deletions packages/react-native-editor/src/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ const gutenbergSetup = () => {

const setupInitHooks = () => {
addAction( 'native.pre-render', 'core/react-native-editor', ( props ) => {
registerBlocks();

const capabilities = props.capabilities ?? {};
const blocksFlags = {
__experimentalEnableQuoteBlockV2: props?.quoteBlockV2,
};

registerBlocks( blocksFlags );

// Unregister non-supported blocks by capabilities
if (
getBlockType( 'core/block' ) !== undefined &&
Expand Down Expand Up @@ -115,12 +119,12 @@ const setupInitHooks = () => {
};

let blocksRegistered = false;
const registerBlocks = () => {
const registerBlocks = ( blocksFlags ) => {
if ( blocksRegistered ) {
return;
}

registerCoreBlocks();
registerCoreBlocks( blocksFlags );

blocksRegistered = true;
};
Expand Down

0 comments on commit 2fee6a6

Please sign in to comment.