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

[Mobile] - Support background colors for Group block #25994

Merged
merged 21 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
24d300a
Mobile - Support background/foreground colors for Group block
Oct 9, 2020
a30dc93
Merge branch 'master' into rnmobile/feature/group-background-color
Oct 13, 2020
9879181
Mobile - Enable ColorEdit
Oct 13, 2020
6cdd62a
Mobile - Add paddings to text blocks with background color
Oct 14, 2020
7cb28a4
Mobile - Add clear button to palette color screen
Oct 14, 2020
4932590
Merge branch 'master' into rnmobile/feature/group-background-color
Oct 14, 2020
758ca6b
Mobile - Global styles context utils and tests
Oct 15, 2020
62c6a0c
Merge branch 'master' into rnmobile/feature/group-background-color
Oct 16, 2020
da212b4
Mobile - Palette screen - Change text and keep the same sheet when re…
Oct 16, 2020
2285e7b
Merge branch 'master' into rnmobile/feature/group-background-color
Nov 2, 2020
cf73441
Mobile - Quote block - Support to inherit colors from parent block
Nov 2, 2020
6e0c9c7
Merge branch 'master' into rnmobile/feature/group-background-color
Nov 4, 2020
a35f539
Mobile - Block quotation and Rich text styles
Nov 9, 2020
9c5940b
Merge branch 'master' into rnmobile/feature/group-background-color
Nov 11, 2020
417b7fd
Mobile - Group block - Remove extra bottom padding when a background …
Nov 11, 2020
4d6a69f
Mobile - Color settings - Remove clear button, to be added in another PR
Nov 16, 2020
3a49d3d
Mobile - Disable ColorEdit for blocks that have color support
Nov 16, 2020
ed1e241
Merge branch 'master' into rnmobile/feature/group-background-color
Nov 16, 2020
3dc8a96
Merge branch 'master' into rnmobile/feature/group-background-color
Nov 17, 2020
209ced5
Mobile - Group - Background color fixes
Nov 18, 2020
eb98d1a
Mobile - Update changelog
Nov 20, 2020
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
107 changes: 72 additions & 35 deletions packages/block-editor/src/components/block-list/block.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import { View, Text, TouchableWithoutFeedback, Dimensions } from 'react-native';
* WordPress dependencies
*/
import { Component, createRef } from '@wordpress/element';
import { GlobalStylesContext, WIDE_ALIGNMENTS } from '@wordpress/components';
import {
GlobalStylesContext,
getMergedGlobalStyles,
WIDE_ALIGNMENTS,
} from '@wordpress/components';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import {
getBlockType,
__experimentalGetAccessibleBlockLabel as getAccessibleBlockLabel,
} from '@wordpress/blocks';
import { __experimentalUseEditorFeature as useEditorFeature } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand All @@ -23,6 +28,66 @@ import BlockEdit from '../block-edit';
import BlockInvalidWarning from './block-invalid-warning';
import BlockMobileToolbar from '../block-mobile-toolbar';

function BlockForType( {
attributes,
clientId,
contentStyle,
getBlockWidth,
insertBlocksAfter,
isSelected,
mergeBlocks,
name,
onBlockFocus,
onCaretVerticalPositionChange,
onChange,
onDeleteBlock,
onReplace,
parentWidth,
wrapperProps,
} ) {
const defaultColors = useEditorFeature( 'color.palette' ) || [];

return (
<GlobalStylesContext.Consumer>
{ ( globalStyle ) => {
const mergedStyle = getMergedGlobalStyles(
globalStyle,
wrapperProps.style,
attributes,
defaultColors
);

return (
<GlobalStylesContext.Provider value={ mergedStyle }>
<BlockEdit
name={ name }
isSelected={ isSelected }
attributes={ attributes }
setAttributes={ onChange }
onFocus={ onBlockFocus }
onReplace={ onReplace }
insertBlocksAfter={ insertBlocksAfter }
mergeBlocks={ mergeBlocks }
onCaretVerticalPositionChange={
onCaretVerticalPositionChange
}
// Block level styles
wrapperProps={ wrapperProps }
// inherited styles merged with block level styles
mergedStyle={ mergedStyle }
clientId={ clientId }
parentWidth={ parentWidth }
contentStyle={ contentStyle }
onDeleteBlock={ onDeleteBlock }
dratwas marked this conversation as resolved.
Show resolved Hide resolved
/>
<View onLayout={ getBlockWidth } />
</GlobalStylesContext.Provider>
);
} }
</GlobalStylesContext.Consumer>
);
}

class BlockListBlock extends Component {
constructor() {
super( ...arguments );
Expand Down Expand Up @@ -65,40 +130,12 @@ class BlockListBlock extends Component {

getBlockForType() {
return (
<GlobalStylesContext.Consumer>
{ ( globalStyle ) => {
const mergedStyle = {
...globalStyle,
...this.props.wrapperProps.style,
};
return (
<GlobalStylesContext.Provider value={ mergedStyle }>
<BlockEdit
name={ this.props.name }
isSelected={ this.props.isSelected }
attributes={ this.props.attributes }
setAttributes={ this.props.onChange }
onFocus={ this.onFocus }
onReplace={ this.props.onReplace }
insertBlocksAfter={ this.insertBlocksAfter }
mergeBlocks={ this.props.mergeBlocks }
onCaretVerticalPositionChange={
this.props.onCaretVerticalPositionChange
}
// Block level styles
wrapperProps={ this.props.wrapperProps }
// inherited styles merged with block level styles
mergedStyle={ mergedStyle }
clientId={ this.props.clientId }
parentWidth={ this.props.parentWidth }
contentStyle={ this.props.contentStyle }
onDeleteBlock={ this.props.onDeleteBlock }
/>
<View onLayout={ this.getBlockWidth } />
</GlobalStylesContext.Provider>
);
} }
</GlobalStylesContext.Consumer>
<BlockForType
{ ...this.props }
onBlockFocus={ this.onFocus }
insertBlocksAfter={ this.insertBlocksAfter }
getBlockWidth={ this.getBlockWidth }
/>
);
}

Expand Down
13 changes: 1 addition & 12 deletions packages/block-editor/src/hooks/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ export const COLOR_SUPPORT_KEY = 'color';
const EMPTY_ARRAY = [];

const hasColorSupport = ( blockType ) => {
if ( Platform.OS !== 'web' ) {
return false;
}
const colorSupport = getBlockSupport( blockType, COLOR_SUPPORT_KEY );
return (
colorSupport &&
Expand Down Expand Up @@ -68,20 +65,12 @@ const hasGradientSupport = ( blockType ) => {
};

const hasBackgroundColorSupport = ( blockType ) => {
if ( Platform.OS !== 'web' ) {
return false;
}

const colorSupport = getBlockSupport( blockType, COLOR_SUPPORT_KEY );

return colorSupport && colorSupport.background !== false;
};

const hasTextColorSupport = ( blockType ) => {
if ( Platform.OS !== 'web' ) {
return false;
}

const colorSupport = getBlockSupport( blockType, COLOR_SUPPORT_KEY );

return colorSupport && colorSupport.text !== false;
Expand Down Expand Up @@ -226,7 +215,7 @@ export function ColorEdit( props ) {
localAttributes.current = attributes;
}, [ attributes ] );

if ( ! hasColorSupport( blockName ) ) {
if ( ! hasColorSupport( blockName ) || Platform.OS !== 'web' ) {
return null;
}

Expand Down
35 changes: 31 additions & 4 deletions packages/block-library/src/group/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { View } from 'react-native';
*/
import { withSelect } from '@wordpress/data';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import { InnerBlocks, withColors } from '@wordpress/block-editor';
import { InnerBlocks } from '@wordpress/block-editor';
import { useCallback } from '@wordpress/element';
import { WIDE_ALIGNMENTS } from '@wordpress/components';

Expand All @@ -21,7 +21,9 @@ function GroupEdit( {
attributes,
hasInnerBlocks,
isSelected,
isLastInnerBlockSelected,
getStylesFromColorScheme,
mergedStyle,
} ) {
const { align } = attributes;
const isFullWidth = align === WIDE_ALIGNMENTS.alignments.full;
Expand Down Expand Up @@ -64,6 +66,14 @@ function GroupEdit( {
! hasInnerBlocks &&
isFullWidth &&
styles.fullWidth,
mergedStyle,
isSelected &&
hasInnerBlocks &&
mergedStyle?.backgroundColor &&
styles.hasBackgroundAppender,
isLastInnerBlockSelected &&
mergedStyle?.backgroundColor &&
styles.isLastInnerBlockSelected,
] }
>
<InnerBlocks renderAppender={ isSelected && renderAppender } />
Expand All @@ -72,14 +82,31 @@ function GroupEdit( {
}

export default compose( [
withColors( 'backgroundColor' ),
withSelect( ( select, { clientId } ) => {
const { getBlock } = select( 'core/block-editor' );
const {
getBlock,
getBlockIndex,
hasSelectedInnerBlock,
getSelectedBlockClientId,
} = select( 'core/block-editor' );

const block = getBlock( clientId );
const hasInnerBlocks = !! ( block && block.innerBlocks.length );
const isInnerBlockSelected =
hasInnerBlocks && hasSelectedInnerBlock( clientId, true );
let isLastInnerBlockSelected = false;

if ( isInnerBlockSelected ) {
const { innerBlocks } = block;
const selectedBlockClientId = getSelectedBlockClientId();
const totalInnerBlocks = innerBlocks.length - 1;
const blockIndex = getBlockIndex( selectedBlockClientId, clientId );
isLastInnerBlockSelected = totalInnerBlocks === blockIndex;
}

return {
hasInnerBlocks: !! ( block && block.innerBlocks.length ),
hasInnerBlocks,
isLastInnerBlockSelected,
};
} ),
withPreferredColorScheme,
Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/group/editor.native.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@
margin-left: $block-edge-to-content;
margin-right: $block-edge-to-content;
}

.hasBackgroundAppender {
padding-bottom: $grid-unit-30;
}

.isLastInnerBlockSelected {
padding-bottom: 0;
}
2 changes: 2 additions & 0 deletions packages/block-library/src/quote/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ export default function QuoteEdit( {
onReplace,
className,
insertBlocksAfter,
mergedStyle,
} ) {
const { align, value, citation } = attributes;
const blockProps = useBlockProps( {
className: classnames( className, {
[ `has-text-align-${ align }` ]: align,
} ),
style: mergedStyle,
dratwas marked this conversation as resolved.
Show resolved Hide resolved
} );

return (
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ export {
default as GlobalStylesContext,
useGlobalStyles,
withGlobalStyles,
getMergedGlobalStyles,
} from './mobile/global-styles-context';
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
/**
* External dependencies
*/
import { pick } from 'lodash';

/**
* WordPress dependencies
*/
import { createContext, useContext } from '@wordpress/element';

/**
* Internal dependencies
*/
import {
BLOCK_STYLE_ATTRIBUTES,
getBlockPaddings,
getBlockColors,
} from './utils';

const GlobalStylesContext = createContext( { style: {} } );

export const getMergedGlobalStyles = (
globalStyle,
wrapperPropsStyle,
blockAttributes,
defaultColors
) => {
const blockStyleAttributes = pick(
blockAttributes,
BLOCK_STYLE_ATTRIBUTES
);
const mergedStyle = {
...globalStyle,
...wrapperPropsStyle,
};
const blockPaddings = getBlockPaddings(
mergedStyle,
wrapperPropsStyle,
blockStyleAttributes
);
const blockColors = getBlockColors( blockStyleAttributes, defaultColors );

return { ...mergedStyle, ...blockPaddings, ...blockColors };
};

export const useGlobalStyles = () => {
const globalStyles = useContext( GlobalStylesContext );

Expand Down
Loading