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] Adjust vertical margins in InnerBlock #19960

Merged
merged 9 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 0 deletions packages/base-styles/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ $block-selected-to-content: $block-edge-to-content - $block-selected-margin - $b
$block-selected-child-to-content: $block-selected-to-content - $block-selected-child-margin - $block-selected-child-border-width;
$block-custom-appender-to-content: $block-selected-margin - $block-selected-border-width;
$block-media-container-to-content: $block-selected-child-margin + $block-selected-border-width;
$block-selected-vertical-margin-descendant: 3 * $block-selected-child-to-content;
$block-selected-vertical-margin-child: 3 * $block-selected-child-to-content - 6 * $block-selected-child-border-width;
jbinda marked this conversation as resolved.
Show resolved Hide resolved

// Buttons & UI Widgets
$radius-round-rectangle: 4px;
Expand Down
17 changes: 13 additions & 4 deletions packages/block-editor/src/components/block-list/block.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class BlockListBlock extends Component {
isAncestorSelected,
hasParent,
getStylesFromColorScheme,
shouldApplyVerticalMarginStyle,
} = this.props;

// if block does not have parent apply neutral or full
Expand All @@ -138,15 +139,18 @@ class BlockListBlock extends Component {

// return apply childOfSelected or childOfSelectedLeaf
// margins depending if block has children or not
return hasChildren ?
{ ...styles.childOfSelected, ...dashedBorderStyle } :
{ ...styles.childOfSelectedLeaf, ...dashedBorderStyle };
return {
...( hasChildren ? styles.childOfSelected : styles.childOfSelectedLeaf ),
...dashedBorderStyle,
...( shouldApplyVerticalMarginStyle && styles.marginVerticalChild ),
};
}

if ( isAncestorSelected ) { // ancestor of a block is selected
return {
...styles.descendantOfSelectedLeaf,
...( hasChildren && styles.marginHorizontalNone ),
...( hasChildren && { ...styles.marginHorizontalNone, ...styles.marginVerticalNone } ),
...( shouldApplyVerticalMarginStyle && styles.marginVerticalDescendant ),
};
}

Expand Down Expand Up @@ -251,6 +255,7 @@ export default compose( [

const order = getBlockIndex( clientId, rootClientId );
const isSelected = isBlockSelected( clientId );
const isFirstBlock = order === 0;
const isLastBlock = order === getBlocks().length - 1;
const block = __unstableGetBlockWithoutInnerBlocks( clientId );
const { name, attributes, isValid } = block || {};
Expand All @@ -275,6 +280,7 @@ export default compose( [
const commonAncestorIndex = parents.indexOf( commonAncestor ) - 1;
const firstToSelectId = commonAncestor ? parents[ commonAncestorIndex ] : parents[ parents.length - 1 ];

const parentCount = getBlockCount( parentId );
const hasChildren = ! isUnregisteredBlock && !! getBlockCount( clientId );
const hasParent = !! parentId;
const isParentSelected = selectedBlockClientId && selectedBlockClientId === parentId;
Expand All @@ -290,6 +296,8 @@ export default compose( [
const isInnerBlockHolder = name === getGroupingBlockName();
const isRootListInnerBlockHolder = ! isSelectedBlockNested && isInnerBlockHolder;

const shouldApplyVerticalMarginStyle = ! isLastBlock && ( ( isFirstBlock && parentCount === 2 ) || parentCount > 2 );
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand this correctly, this is trying to add a margin to every sibling except the last one, right?

This expression seems more complicated than it needs, if I understand each variable correctly. I see three possible cases regarding the number of children in the parent:

  • There's one block (parentCount < 2). Then this must be the last (and first) block, so ! isLastBlock === false and the margin shouldn't apply.
  • There are two blocks (parentCount == 2). We have already set a condition to not add a margin on the last block, so the isFirstBlock is only relevant for the other case, which will always be true: if I'm not the last of 2, I must be the first.
  • There are more than two blocks (parentCount > 2). Similarly, we've ruled out the case where we are the last block, and we want to add the margin for every other block.

So, if I'm not missing anything, the same logic should work with this simpler condition:

Suggested change
const shouldApplyVerticalMarginStyle = ! isLastBlock && ( ( isFirstBlock && parentCount === 2 ) || parentCount > 2 );
const shouldApplyVerticalMarginStyle = ! isLastBlock;

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 have check it and it's fine but after we modify isLastBlock as below:

In previous implementation it tries to check the order base of current clientId InnerBlocks count. Which isn't proper because we need to check the order in parent InnerBlock list.

I have checked and it seems that this flag isn't use anywhere so probably we can also prevent to pass it to component props

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 have also simplify passing the prop in latest commit


return {
icon,
name: name || 'core/missing',
Expand All @@ -311,6 +319,7 @@ export default compose( [
isDimmed,
isRootListInnerBlockHolder,
isUnregisteredBlock,
shouldApplyVerticalMarginStyle,
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => {
Expand Down
13 changes: 13 additions & 0 deletions packages/block-editor/src/components/block-list/block.native.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@
margin-right: 0;
}

.marginVerticalDescendant {
margin-bottom: $block-selected-vertical-margin-descendant;
}

.marginVerticalChild {
margin-bottom: $block-selected-vertical-margin-child;
}

.marginVerticalNone {
margin-top: 0;
margin-bottom: 0;
}

.blockTitle {
background-color: $gray;
padding-left: 8px;
Expand Down
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 @@ -148,7 +148,7 @@ export const registerCoreBlocks = () => {
mediaText,
preformatted,
gallery,
devOnly( group ),
group,
jbinda marked this conversation as resolved.
Show resolved Hide resolved
spacer,
shortcode,
].forEach( registerBlock );
Expand Down