Skip to content

Commit

Permalink
[Mobile] Use link picker in Image block (#27292)
Browse files Browse the repository at this point in the history
* Use LinkSettingsNavigation in Image block

* Revert mapping url attribute in link settings

* Map url -> href attribute for link picker in image block

* Don't overwrite mapped attribute when undefined

* Fix lint

* Conditionally render link options in link settings

* Move link settings panel in image block settings

* Auto-hide "Open in new tab" in rich text link settings

* Revert "Update tt1 blocks references. (#27770)"

This reverts commit c665998.

* Revert "Revert "Update tt1 blocks references. (#27770)""

This reverts commit f40b256.

* Fix assertion count in multi-entity-saving test

This is an attempt to fix a failing test - I manually counted 26
assertions, instead of 27, so it looks correct. However, it still is not
clear how this was passing before with 27.
  • Loading branch information
mkevins authored Dec 21, 2020
1 parent bb9ec5e commit 35bd1cb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 62 deletions.
77 changes: 48 additions & 29 deletions packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import {
Icon,
PanelBody,
TextControl,
ToggleControl,
ToolbarButton,
ToolbarGroup,
Image,
WIDE_ALIGNMENTS,
LinkSettingsNavigation,
} from '@wordpress/components';
import {
BlockCaption,
Expand All @@ -43,8 +43,6 @@ import { doAction, hasAction } from '@wordpress/hooks';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
import {
external,
link,
image as placeholderIcon,
textColor,
replace,
Expand Down Expand Up @@ -338,6 +336,49 @@ export class ImageEdit extends React.Component {
: width;
}

getLinkSettings() {
const { isLinkSheetVisible } = this.state;
const {
attributes: { href: url, ...unMappedAttributes },
setAttributes,
} = this.props;

const mappedAttributes = { ...unMappedAttributes, url };
const setMappedAttributes = ( { url: href, ...restAttributes } ) =>
href === undefined
? setAttributes( restAttributes )
: setAttributes( { ...restAttributes, href } );

const options = {
url: {
label: __( 'Image Link URL' ),
placeholder: __( 'Add URL' ),
autoFocus: false,
autoFill: true,
},
openInNewTab: {
label: __( 'Open in new tab' ),
},
linkRel: {
label: __( 'Link Rel' ),
placeholder: __( 'None' ),
},
};

return (
<LinkSettingsNavigation
isVisible={ isLinkSheetVisible }
attributes={ mappedAttributes }
onClose={ this.dismissSheet }
setAttributes={ setMappedAttributes }
withBottomSheet={ false }
hasPicker
options={ options }
showIcon={ false }
/>
);
}

render() {
const { isCaptionSelected } = this.state;
const {
Expand All @@ -347,16 +388,7 @@ export class ImageEdit extends React.Component {
imageSizes,
clientId,
} = this.props;
const {
align,
url,
alt,
href,
id,
linkTarget,
sizeSlug,
className,
} = attributes;
const { align, url, alt, id, sizeSlug, className } = attributes;

const sizeOptions = map( imageSizes, ( { name, slug } ) => ( {
value: slug,
Expand Down Expand Up @@ -392,22 +424,6 @@ export class ImageEdit extends React.Component {
) }
</PanelBody>
<PanelBody>
<TextControl
icon={ link }
label={ __( 'Link To' ) }
value={ href || '' }
valuePlaceholder={ __( 'Add URL' ) }
onChange={ this.onSetLinkDestination }
autoCapitalize="none"
autoCorrect={ false }
keyboardType="url"
/>
<ToggleControl
icon={ external }
label={ __( 'Open in new tab' ) }
checked={ linkTarget === '_blank' }
onChange={ this.onSetNewTab }
/>
{ image && sizeOptionsValid && (
<CycleSelectControl
icon={ expand }
Expand All @@ -427,6 +443,9 @@ export class ImageEdit extends React.Component {
onChangeValue={ this.updateAlt }
/>
</PanelBody>
<PanelBody title={ __( 'Link Settings' ) }>
{ this.getLinkSettings( true ) }
</PanelBody>
</InspectorControls>
);

Expand Down
44 changes: 24 additions & 20 deletions packages/components/src/mobile/link-settings/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,26 +224,30 @@ function LinkSettings( {
onChange={ onChangeLabel }
/>
) }
{ options.openInNewTab && (
<ToggleControl
icon={ showIcon && external }
label={ options.openInNewTab.label }
checked={ linkTarget === '_blank' }
onChange={ onChangeOpenInNewTab }
/>
) }
{ options.linkRel && (
<TextControl
icon={ showIcon && LinkRelIcon }
label={ options.linkRel.label }
value={ linkRelInputValue }
valuePlaceholder={ options.linkRel.placeholder }
onChange={ onChangeLinkRel }
onSubmit={ onCloseSettingsSheet }
autoCapitalize="none"
autoCorrect={ false }
keyboardType="url"
/>
{ !! urlInputValue && (
<>
{ options.openInNewTab && (
<ToggleControl
icon={ showIcon && external }
label={ options.openInNewTab.label }
checked={ linkTarget === '_blank' }
onChange={ onChangeOpenInNewTab }
/>
) }
{ options.linkRel && (
<TextControl
icon={ showIcon && LinkRelIcon }
label={ options.linkRel.label }
value={ linkRelInputValue }
valuePlaceholder={ options.linkRel.placeholder }
onChange={ onChangeLinkRel }
onSubmit={ onCloseSettingsSheet }
autoCapitalize="none"
autoCorrect={ false }
keyboardType="url"
/>
) }
</>
) }
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ const LinkSettingsScreen = ( {
}, [ navigation, route.params?.text, text ] );

return useMemo( () => {
const shouldShowLinkOptions = !! inputValue;

return (
<>
<BottomSheet.LinkCell
Expand All @@ -150,20 +152,25 @@ const LinkSettingsScreen = ( {
placeholder={ __( 'Add link text' ) }
onChangeValue={ setText }
onSubmit={ submit }
separatorType={ shouldShowLinkOptions ? undefined : 'none' }
/>
<BottomSheet.SwitchCell
icon={ external }
label={ __( 'Open in new tab' ) }
value={ opensInNewWindow }
onValueChange={ setOpensInNewWindows }
separatorType={ 'fullWidth' }
/>
<BottomSheet.Cell
label={ __( 'Remove link' ) }
labelStyle={ styles.clearLinkButton }
separatorType={ 'none' }
onPress={ removeLink }
/>
{ shouldShowLinkOptions && (
<>
<BottomSheet.SwitchCell
icon={ external }
label={ __( 'Open in new tab' ) }
value={ opensInNewWindow }
onValueChange={ setOpensInNewWindows }
separatorType={ 'fullWidth' }
/>
<BottomSheet.Cell
label={ __( 'Remove link' ) }
labelStyle={ styles.clearLinkButton }
separatorType={ 'none' }
onPress={ removeLink }
/>
</>
) }
</>
);
}, [ inputValue, text, opensInNewWindow, listProps.safeAreaBottomInset ] );
Expand Down

0 comments on commit 35bd1cb

Please sign in to comment.