diff --git a/packages/block-editor/src/components/block-list/block.native.js b/packages/block-editor/src/components/block-list/block.native.js index 918c414ba833ce..5e138de2e909ec 100644 --- a/packages/block-editor/src/components/block-list/block.native.js +++ b/packages/block-editor/src/components/block-list/block.native.js @@ -66,7 +66,9 @@ class BlockListBlock extends Component { this.props.onCaretVerticalPositionChange } clientId={ this.props.clientId } + parentWidth={ this.props.parentWidth } contentStyle={ this.props.contentStyle } + onDeleteBlock={ this.props.onDeleteBlock } /> ); } @@ -93,7 +95,7 @@ class BlockListBlock extends Component { isDimmed, isTouchable, onDeleteBlock, - horizontalDirection, + isStackedHorizontally, hasParent, isParentSelected, onSelect, @@ -180,7 +182,9 @@ class BlockListBlock extends Component { ) } diff --git a/packages/block-editor/src/components/block-list/index.native.js b/packages/block-editor/src/components/block-list/index.native.js index 830f92559b5cea..a86e4150931d26 100644 --- a/packages/block-editor/src/components/block-list/index.native.js +++ b/packages/block-editor/src/components/block-list/index.native.js @@ -86,6 +86,7 @@ export class BlockList extends Component { shouldShowInnerBlockAppender() { const { blockClientIds, renderAppender } = this.props; + return renderAppender && blockClientIds.length > 0; } @@ -95,7 +96,6 @@ export class BlockList extends Component { blockClientIds, title, header, - withFooter = true, isReadOnly, isRootList, horizontal, @@ -104,6 +104,8 @@ export class BlockList extends Component { shouldShowInsertionPointAfter, marginVertical = styles.defaultBlock.marginTop, marginHorizontal = styles.defaultBlock.marginLeft, + isStackedHorizontally, + horizontalAlignment, } = this.props; const { blockToolbar, blockBorder, headerToolbar } = styles; @@ -144,7 +146,12 @@ export class BlockList extends Component { contentContainerStyle={ horizontal && styles.horizontalContentContainer } - style={ ! isRootList && styles.overflowVisible } + style={ [ + ! isRootList && styles.overflowVisible, + isStackedHorizontally && styles.horizontal, + horizontalAlignment && + styles[ `is-aligned-${ horizontalAlignment }` ], + ] } data={ blockClientIds } keyExtractor={ identity } extraData={ forceRefresh } @@ -157,9 +164,7 @@ export class BlockList extends Component { ListEmptyComponent={ ! isReadOnly && this.renderDefaultBlockAppender } - ListFooterComponent={ - ! isReadOnly && withFooter && this.renderBlockListFooter - } + ListFooterComponent={ this.renderBlockListFooter } /> { this.shouldShowInnerBlockAppender() && ( @@ -188,7 +193,7 @@ export class BlockList extends Component { shouldShowInsertionPointAfter, marginVertical = styles.defaultBlock.marginTop, marginHorizontal = styles.defaultBlock.marginLeft, - horizontalDirection, + isStackedHorizontally, contentResizeMode, contentStyle, onAddBlock, @@ -218,7 +223,8 @@ export class BlockList extends Component { onCaretVerticalPositionChange={ this.onCaretVerticalPositionChange } - horizontalDirection={ horizontalDirection } + parentWidth={ this.props.parentWidth } + isStackedHorizontally={ isStackedHorizontally } contentStyle={ contentStyle } onAddBlock={ onAddBlock } onDeleteBlock={ onDeleteBlock } @@ -234,18 +240,29 @@ export class BlockList extends Component { renderBlockListFooter() { const paragraphBlock = createBlock( 'core/paragraph' ); - return ( - <> - { - this.addBlockToEndOfPost( paragraphBlock ); - } } - > - - - - ); + const { + isReadOnly, + withFooter = true, + renderFooterAppender, + } = this.props; + + if ( ! isReadOnly && withFooter ) { + return ( + <> + { + this.addBlockToEndOfPost( paragraphBlock ); + } } + > + + + + ); + } else if ( renderFooterAppender ) { + return renderFooterAppender(); + } + return null; } } @@ -260,7 +277,7 @@ export default compose( [ getSettings, } = select( 'core/block-editor' ); - const horizontalDirection = + const isStackedHorizontally = __experimentalMoverDirection === 'horizontal'; const selectedBlockClientId = getSelectedBlockClientId(); @@ -269,7 +286,7 @@ export default compose( [ const blockInsertionPointIsVisible = isBlockInsertionPointVisible(); const shouldShowInsertionPointBefore = ( clientId ) => { return ( - ! horizontalDirection && + ! isStackedHorizontally && blockInsertionPointIsVisible && insertionPoint.rootClientId === rootClientId && // if list is empty, show the insertion point (via the default appender) @@ -280,7 +297,7 @@ export default compose( [ }; const shouldShowInsertionPointAfter = ( clientId ) => { return ( - ! horizontalDirection && + ! isStackedHorizontally && blockInsertionPointIsVisible && insertionPoint.rootClientId === rootClientId && // if the insertion point is at the end of the list @@ -301,7 +318,7 @@ export default compose( [ selectedBlockClientId, isReadOnly, isRootList: rootClientId === undefined, - horizontalDirection, + isStackedHorizontally, }; } ), withDispatch( ( dispatch ) => { diff --git a/packages/block-editor/src/components/block-list/style.native.scss b/packages/block-editor/src/components/block-list/style.native.scss index 78994455ca0f30..146dd9463336a6 100644 --- a/packages/block-editor/src/components/block-list/style.native.scss +++ b/packages/block-editor/src/components/block-list/style.native.scss @@ -81,6 +81,23 @@ height: $mobile-block-toolbar-height; } +.horizontal { + flex-direction: row; + flex-wrap: wrap; +} + +.is-aligned-left { + justify-content: flex-start; +} + +.is-aligned-center { + justify-content: center; +} + +.is-aligned-right { + justify-content: flex-end; +} + .overflowVisible { overflow: visible; } diff --git a/packages/block-editor/src/components/block-mobile-toolbar/index.native.js b/packages/block-editor/src/components/block-mobile-toolbar/index.native.js index b4a56abc4df1b0..6e3fc8aaa1fdd2 100644 --- a/packages/block-editor/src/components/block-mobile-toolbar/index.native.js +++ b/packages/block-editor/src/components/block-mobile-toolbar/index.native.js @@ -23,12 +23,12 @@ const BlockMobileToolbar = ( { clientId, onDelete, order, - horizontalDirection, + isStackedHorizontally, } ) => ( diff --git a/packages/block-editor/src/components/block-mover/index.native.js b/packages/block-editor/src/components/block-mover/index.native.js index 5ca80153bee052..818d7b35f3ac23 100644 --- a/packages/block-editor/src/components/block-mover/index.native.js +++ b/packages/block-editor/src/components/block-mover/index.native.js @@ -52,7 +52,7 @@ const BlockMover = ( { onMoveUp, firstIndex, rootClientId, - horizontalDirection, + isStackedHorizontally, } ) => { const { backwardButtonIcon, @@ -61,7 +61,7 @@ const BlockMover = ( { forwardButtonHint, firstBlockTitle, lastBlockTitle, - } = horizontalDirection ? horizontalMover : verticalMover; + } = isStackedHorizontally ? horizontalMover : verticalMover; if ( isLocked || ( isFirst && isLast && ! rootClientId ) ) { return null; @@ -72,7 +72,7 @@ const BlockMover = ( { forwardButtonProp, backwardButtonProp ) => { - if ( isRTL && horizontalDirection ) { + if ( isRTL && isStackedHorizontally ) { // for RTL and horizontal direction switch prop between forward and backward button if ( isBackwardButton ) { return forwardButtonProp; // set forwardButtonProp for backward button @@ -89,9 +89,10 @@ const BlockMover = ( { const direction = isBackwardButton ? -1 : 1; const toIndex = fromIndex + direction; // position after move - const { backwardButtonTitle, forwardButtonTitle } = horizontalDirection - ? horizontalMover - : verticalMover; + const { + backwardButtonTitle, + forwardButtonTitle, + } = isStackedHorizontally ? horizontalMover : verticalMover; const buttonTitle = switchButtonPropIfRTL( isBackwardButton, diff --git a/packages/block-editor/src/components/button-block-appender/index.native.js b/packages/block-editor/src/components/button-block-appender/index.native.js index 4a3be1a5b5ac06..e879ef243a0ca8 100644 --- a/packages/block-editor/src/components/button-block-appender/index.native.js +++ b/packages/block-editor/src/components/button-block-appender/index.native.js @@ -20,6 +20,7 @@ function ButtonBlockAppender( { rootClientId, getStylesFromColorScheme, showSeparator, + isFloating = false, onAddBlock, } ) { const appenderStyle = { @@ -31,7 +32,9 @@ function ButtonBlockAppender( { }; const addBlockButtonStyle = getStylesFromColorScheme( styles.addBlockButton, - styles.addBlockButtonDark + isFloating + ? styles.floatingAddBlockButtonDark + : styles.addBlockButtonDark ); return ( @@ -45,7 +48,12 @@ function ButtonBlockAppender( { disabled={ disabled } fixedRatio={ false } > - + { return ( ); diff --git a/packages/block-editor/src/components/inner-blocks/index.native.js b/packages/block-editor/src/components/inner-blocks/index.native.js index d462fa40d04865..035b7316f5f335 100644 --- a/packages/block-editor/src/components/inner-blocks/index.native.js +++ b/packages/block-editor/src/components/inner-blocks/index.native.js @@ -20,10 +20,6 @@ import { compose } from '@wordpress/compose'; */ import ButtonBlockAppender from './button-block-appender'; import DefaultBlockAppender from './default-block-appender'; - -/** - * Internal dependencies - */ import BlockList from '../block-list'; import { withBlockEditContext } from '../block-edit/context'; @@ -112,7 +108,9 @@ class InnerBlocks extends Component { const { clientId, renderAppender, + renderFooterAppender, __experimentalMoverDirection, + parentWidth, horizontal, scrollEnabled, contentResizeMode, @@ -121,6 +119,7 @@ class InnerBlocks extends Component { onDeleteBlock, marginVertical, marginHorizontal, + horizontalAlignment, } = this.props; const { templateInProcess } = this.state; @@ -132,10 +131,13 @@ class InnerBlocks extends Component { marginHorizontal={ marginHorizontal } rootClientId={ clientId } renderAppender={ renderAppender } + renderFooterAppender={ renderFooterAppender } withFooter={ false } __experimentalMoverDirection={ __experimentalMoverDirection } + parentWidth={ parentWidth } + horizontalAlignment={ horizontalAlignment } horizontal={ horizontal } scrollEnabled={ scrollEnabled } contentResizeMode={ contentResizeMode } diff --git a/packages/block-editor/src/hooks/index.native.js b/packages/block-editor/src/hooks/index.native.js index 43c5327b4fa5c3..f0d8c10b92b63b 100644 --- a/packages/block-editor/src/hooks/index.native.js +++ b/packages/block-editor/src/hooks/index.native.js @@ -1,6 +1,8 @@ /** * Internal dependencies */ +export { AlignmentHookSettingsProvider } from './align'; + import './custom-class-name'; import './generated-class-name'; import './style'; diff --git a/packages/block-library/src/button/edit.native.js b/packages/block-library/src/button/edit.native.js index d772e4b307b394..6d897d1cf31fa5 100644 --- a/packages/block-library/src/button/edit.native.js +++ b/packages/block-library/src/button/edit.native.js @@ -1,7 +1,13 @@ /** * External dependencies */ -import { View, AccessibilityInfo, Platform, Clipboard } from 'react-native'; +import { + View, + AccessibilityInfo, + Platform, + Clipboard, + Text, +} from 'react-native'; /** * WordPress dependencies */ @@ -52,37 +58,51 @@ class ButtonEdit extends Component { this.onChangeURL = this.onChangeURL.bind( this ); this.onClearSettings = this.onClearSettings.bind( this ); this.onLayout = this.onLayout.bind( this ); + this.onSetMaxWidth = this.onSetMaxWidth.bind( this ); this.dismissSheet = this.dismissSheet.bind( this ); this.getURLFromClipboard = this.getURLFromClipboard.bind( this ); this.onShowLinkSettings = this.onShowLinkSettings.bind( this ); this.onHideLinkSettings = this.onHideLinkSettings.bind( this ); this.onToggleButtonFocus = this.onToggleButtonFocus.bind( this ); this.setRef = this.setRef.bind( this ); + this.onRemove = this.onRemove.bind( this ); + this.getPlaceholderWidth = this.getPlaceholderWidth.bind( this ); // `isEditingURL` property is used to prevent from automatically pasting // URL from clipboard while trying to clear `Button URL` field and then // manually adding specific link this.isEditingURL = false; - const isButtonFocused = - Platform.OS === 'ios' ? ! props.hasParents : true; - this.state = { maxWidth: INITIAL_MAX_WIDTH, isLinkSheetVisible: false, - isButtonFocused, + isButtonFocused: true, + placeholderTextWidth: 0, }; } + componentDidMount() { + this.onSetMaxWidth(); + } + componentDidUpdate( prevProps, prevState ) { const { selectedId, setAttributes, editorSidebarOpened, attributes: { url }, + parentWidth, } = this.props; const { isLinkSheetVisible, isButtonFocused } = this.state; + if ( prevProps.selectedId !== selectedId ) { + this.onToggleButtonFocus( true ); + } + + if ( prevProps.parentWidth !== parentWidth ) { + this.onSetMaxWidth(); + } + if ( ( prevProps.editorSidebarOpened && ! editorSidebarOpened ) || ( prevState.isLinkSheetVisible && ! isLinkSheetVisible ) @@ -241,9 +261,34 @@ class ButtonEdit extends Component { onLayout( { nativeEvent } ) { const { width } = nativeEvent.layout; - const { marginRight } = styles.button; - const buttonSpacing = 2 * marginRight; - this.setState( { maxWidth: width - buttonSpacing } ); + this.onSetMaxWidth( width ); + } + + onSetMaxWidth( width ) { + const { maxWidth } = this.state; + const { parentWidth } = this.props; + const { marginRight: spacing } = styles.button; + + const isParentWidthChanged = maxWidth !== parentWidth; + const isWidthChanged = maxWidth !== width; + + if ( parentWidth && ! width && isParentWidthChanged ) { + this.setState( { + maxWidth: parentWidth, + } ); + } else if ( ! parentWidth && width && isWidthChanged ) { + this.setState( { maxWidth: width - spacing } ); + } + } + + onRemove() { + const { numOfButtons, onDeleteBlock, onReplace } = this.props; + + if ( numOfButtons === 1 ) { + onDeleteBlock(); + } else { + onReplace( [] ); + } } dismissSheet() { @@ -305,8 +350,40 @@ class ButtonEdit extends Component { this.richTextRef = richText; } + // Render `Text` with `placeholderText` styled as a placeholder + // to calculate its width which then is set as a `minWidth` + getPlaceholderWidth( placeholderText ) { + const { maxWidth, placeholderTextWidth } = this.state; + return ( + { + const textWidth = + nativeEvent.lines[ 0 ] && nativeEvent.lines[ 0 ].width; + if ( textWidth && textWidth !== placeholderTextWidth ) { + this.setState( { + placeholderTextWidth: Math.min( + textWidth, + maxWidth + ), + } ); + } + } } + > + { placeholderText } + + ); + } + render() { - const { attributes, isSelected, clientId, onReplace } = this.props; + const { + attributes, + isSelected, + clientId, + onReplace, + mergeBlocks, + parentWidth, + } = this.props; const { placeholder, text, @@ -315,7 +392,16 @@ class ButtonEdit extends Component { linkTarget, rel, } = attributes; - const { maxWidth, isLinkSheetVisible, isButtonFocused } = this.state; + const { + maxWidth, + isLinkSheetVisible, + isButtonFocused, + placeholderTextWidth, + } = this.state; + + if ( parentWidth === 0 ) { + return null; + } const borderRadiusValue = borderRadius !== undefined @@ -334,7 +420,7 @@ class ButtonEdit extends Component { const minWidth = isButtonFocused || ( ! isButtonFocused && text && text !== '' ) ? 1 - : styles.button.minWidth; + : placeholderTextWidth; // To achieve proper expanding and shrinking `RichText` on Android, there is a need to set // a `placeholder` as an empty string when `RichText` is focused, // because `AztecView` is calculating a `minWidth` based on placeholder text. @@ -346,7 +432,8 @@ class ButtonEdit extends Component { const backgroundColor = this.getBackgroundColor(); return ( - + + { this.getPlaceholderWidth( placeholderText ) } { + this.onToggleButtonFocus( false ); + this.onSetMaxWidth(); + } } selectionColor={ this.getTextColor() } onReplace={ onReplace } - onRemove={ () => onReplace( [] ) } + onRemove={ this.onRemove } + onMerge={ mergeBlocks } /> @@ -453,19 +544,22 @@ class ButtonEdit extends Component { export default compose( [ withInstanceId, withColors( 'backgroundColor', { textColor: 'color' } ), - withSelect( ( select ) => { + withSelect( ( select, { clientId } ) => { const { isEditorSidebarOpened } = select( 'core/edit-post' ); - const { getSelectedBlockClientId, getBlockParents } = select( - 'core/block-editor' - ); + const { + getSelectedBlockClientId, + getBlockCount, + getBlockRootClientId, + } = select( 'core/block-editor' ); + const parentId = getBlockRootClientId( clientId ); const selectedId = getSelectedBlockClientId(); - const hasParents = getBlockParents( selectedId ).length > 0; + const numOfButtons = getBlockCount( parentId ); return { selectedId, editorSidebarOpened: isEditorSidebarOpened(), - hasParents, + numOfButtons, }; } ), withDispatch( ( dispatch ) => { diff --git a/packages/block-library/src/button/editor.native.scss b/packages/block-library/src/button/editor.native.scss index 16dff6478dc350..47194070090401 100644 --- a/packages/block-library/src/button/editor.native.scss +++ b/packages/block-library/src/button/editor.native.scss @@ -4,6 +4,12 @@ } .outline { + // Border Width has to be spread since width is thicker + // on Android when border radius is decreased to 0 + border-left-width: $border-width; + border-right-width: $border-width; + border-top-width: $border-width; + border-bottom-width: $border-width; position: absolute; left: -($block-spacing + $border-width); right: -($block-spacing + $border-width); @@ -25,7 +31,7 @@ padding: $block-spacing; max-width: 580px; min-width: 108px; - margin: $panel-padding; + margin: 2 * $panel-padding; } .fallbackButton { @@ -40,3 +46,10 @@ .placeholderTextColor { color: rgba($color: $white, $alpha: 0.43); } + +.placeholder { + font-family: $default-regular-font; + min-height: 22px; + font-size: 16px; + display: none; +} diff --git a/packages/block-library/src/button/index.native.js b/packages/block-library/src/button/index.native.js deleted file mode 100644 index b3a3b7c131dfe1..00000000000000 --- a/packages/block-library/src/button/index.native.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * Internal dependencies - */ -import { settings as webSettings } from './index.js'; - -export { metadata, name } from './index.js'; - -export const settings = { - ...webSettings, - parent: undefined, -}; diff --git a/packages/block-library/src/buttons/edit.native.js b/packages/block-library/src/buttons/edit.native.js new file mode 100644 index 00000000000000..c80c844afd41e6 --- /dev/null +++ b/packages/block-library/src/buttons/edit.native.js @@ -0,0 +1,142 @@ +/** + * External dependencies + */ +import { View } from 'react-native'; +/** + * WordPress dependencies + */ +import { + InnerBlocks, + __experimentalAlignmentHookSettingsProvider as AlignmentHookSettingsProvider, +} from '@wordpress/block-editor'; +import { withSelect, withDispatch } from '@wordpress/data'; +import { compose, useResizeObserver } from '@wordpress/compose'; +import { createBlock } from '@wordpress/blocks'; +import { useState, useEffect } from '@wordpress/element'; + +/** + * Internal dependencies + */ +import { name as buttonBlockName } from '../button/'; +import styles from './editor.scss'; + +const ALLOWED_BLOCKS = [ buttonBlockName ]; +const BUTTONS_TEMPLATE = [ [ 'core/button' ] ]; + +function ButtonsEdit( { + isSelected, + attributes, + onDelete, + onAddNextButton, + shouldDelete, + isInnerButtonSelected, +} ) { + const { align } = attributes; + const [ resizeObserver, sizes ] = useResizeObserver(); + const [ maxWidth, setMaxWidth ] = useState( 0 ); + const shouldRenderFooterAppender = isSelected || isInnerButtonSelected; + const { marginLeft: spacing } = styles.spacing; + + useEffect( () => { + const margins = 2 * styles.parent.marginRight; + const { width } = sizes || {}; + if ( width ) { + setMaxWidth( width - margins ); + } + }, [ sizes ] ); + + function renderFooterAppender() { + return ( + + + + ); + } + + // Inside buttons block alignment options are not supported. + const alignmentHooksSetting = { + isEmbedButton: true, + }; + + return ( + + { resizeObserver } + + + ); +} + +export default compose( + withSelect( ( select, { clientId } ) => { + const { + getBlockCount, + getBlockParents, + getSelectedBlockClientId, + } = select( 'core/block-editor' ); + const selectedBlockClientId = getSelectedBlockClientId(); + const selectedBlockParents = getBlockParents( + selectedBlockClientId, + true + ); + + return { + // The purpose of `shouldDelete` check is giving the ability to pass to + // mobile toolbar function called `onDelete` which removes the whole + // `Buttons` container along with the last inner button when + // there is exactly one button. + shouldDelete: getBlockCount( clientId ) === 1, + isInnerButtonSelected: selectedBlockParents[ 0 ] === clientId, + }; + } ), + withDispatch( ( dispatch, { clientId }, registry ) => { + const { replaceInnerBlocks, selectBlock, removeBlock } = dispatch( + 'core/block-editor' + ); + const { getBlocks, getBlockOrder } = registry.select( + 'core/block-editor' + ); + const innerBlocks = getBlocks( clientId ); + + return { + // The purpose of `onAddNextButton` is giving the ability to automatically + // adding `Button` inside `Buttons` block on the appender press event. + onAddNextButton: ( selectedId ) => { + const order = getBlockOrder( clientId ); + const selectedButtonIndex = order.findIndex( + ( i ) => i === selectedId + ); + + const index = + selectedButtonIndex === -1 + ? order.length + 1 + : selectedButtonIndex; + + const insertedBlock = createBlock( 'core/button' ); + + innerBlocks.splice( index + 1, 0, insertedBlock ); + + replaceInnerBlocks( clientId, innerBlocks, true ); + selectBlock( insertedBlock.clientId ); + }, + onDelete: () => { + removeBlock( clientId ); + }, + }; + } ) +)( ButtonsEdit ); diff --git a/packages/block-library/src/buttons/editor.native.scss b/packages/block-library/src/buttons/editor.native.scss new file mode 100644 index 00000000000000..f4816da8494d41 --- /dev/null +++ b/packages/block-library/src/buttons/editor.native.scss @@ -0,0 +1,12 @@ +.parent { + margin: $block-edge-to-content; +} + +.appenderContainer { + flex: 1; + margin: $block-selected-margin; +} + +.spacing { + margin: 2 * $block-selected-margin; +} diff --git a/packages/block-library/src/index.native.js b/packages/block-library/src/index.native.js index 4efee4a3deda82..6f6c5049eb7662 100644 --- a/packages/block-library/src/index.native.js +++ b/packages/block-library/src/index.native.js @@ -50,6 +50,7 @@ import * as verse from './verse'; import * as video from './video'; import * as tagCloud from './tag-cloud'; import * as group from './group'; +import * as buttons from './buttons'; export const coreBlocks = [ // Common blocks are grouped at the top to prioritize their display @@ -96,6 +97,7 @@ export const coreBlocks = [ textColumns, verse, video, + buttons, ].reduce( ( accumulator, block ) => { accumulator[ block.name ] = block; return accumulator; @@ -154,6 +156,7 @@ export const registerCoreBlocks = () => { button, spacer, shortcode, + buttons, latestPosts, devOnly( verse ), cover, diff --git a/packages/rich-text/src/component/index.native.js b/packages/rich-text/src/component/index.native.js index 22c9a3d3d050f1..c106193134a30c 100644 --- a/packages/rich-text/src/component/index.native.js +++ b/packages/rich-text/src/component/index.native.js @@ -811,7 +811,7 @@ export class RichText extends Component { ...style, ...( this.isIOS && minWidth && maxWidth ? { width } - : {} ), + : { maxWidth } ), minHeight: this.state.height, } } text={ {