Skip to content

Commit

Permalink
Update native image block to use correct image URL
Browse files Browse the repository at this point in the history
  • Loading branch information
derekblank committed Apr 26, 2022
1 parent c660f75 commit 760b9cf
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
blockSettingsScreens,
} from '@wordpress/block-editor';
import { __, _x, sprintf } from '@wordpress/i18n';
import { getProtocol, hasQueryArg } from '@wordpress/url';
import { getProtocol, hasQueryArg, isURL } from '@wordpress/url';
import { doAction, hasAction } from '@wordpress/hooks';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -207,6 +207,7 @@ export class ImageEdit extends Component {
this.onImagePressed = this.onImagePressed.bind( this );
this.onSetFeatured = this.onSetFeatured.bind( this );
this.onFocusCaption = this.onFocusCaption.bind( this );
this.onSelectURL = this.onSelectURL.bind( this );
this.updateAlignment = this.updateAlignment.bind( this );
this.accessibilityLabelCreator = this.accessibilityLabelCreator.bind(
this
Expand Down Expand Up @@ -461,6 +462,19 @@ export class ImageEdit extends Component {
} );
}

onSelectURL( newUrl ) {
const { attributes: url, setAttributes } = this.props;

if ( newUrl !== url ) {
if ( isURL( newUrl ) ) {
setAttributes( { url: newUrl, id: undefined } );
} else {
// TODO: throw createErrorNotice equivalent error from class component
// console.log( 'Invalid URL. Image file not found.' );
}
}
}

onFocusCaption() {
if ( this.props.onFocus ) {
this.props.onFocus();
Expand Down Expand Up @@ -716,6 +730,7 @@ export class ImageEdit extends Component {
<MediaPlaceholder
allowedTypes={ [ MEDIA_TYPE_IMAGE ] }
onSelect={ this.onSelectMediaUploadOption }
onSelectURL={ this.onSelectURL }
icon={ this.getPlaceholderIcon() }
onFocus={ this.props.onFocus }
autoOpenMediaUpload={
Expand Down Expand Up @@ -837,6 +852,7 @@ export class ImageEdit extends Component {
allowedTypes={ [ MEDIA_TYPE_IMAGE ] }
isReplacingMedia={ true }
onSelect={ this.onSelectMediaUploadOption }
onSelectURL={ this.onSelectURL }
render={ ( { open, getMediaOptions } ) => {
return getImageComponent( open, getMediaOptions );
} }
Expand Down

0 comments on commit 760b9cf

Please sign in to comment.