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

Switch image and audio block to embed block if URL is embeddable #11472

Merged
merged 7 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions packages/block-library/src/audio/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import {
} from '@wordpress/editor';
import { getBlobByURL, isBlobURL } from '@wordpress/blob';

/**
* Internal dependencies
*/
import { createUpgradedEmbedBlock } from '../embed/util';

const ALLOWED_MEDIA_TYPES = [ 'audio' ];

class AudioEdit extends Component {
Expand Down Expand Up @@ -73,6 +78,14 @@ class AudioEdit extends Component {
// Set the block's src from the edit component's state, and switch off
// the editing UI.
if ( newSrc !== src ) {
// Check if there's an embed block that handles this URL.
const embedBlock = createUpgradedEmbedBlock(
{ attributes: { url: newSrc } }
);
if ( undefined !== embedBlock ) {
this.props.onReplace( embedBlock );
return;
}
setAttributes( { src: newSrc, id: undefined } );
}

Expand Down
14 changes: 13 additions & 1 deletion packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { createUpgradedEmbedBlock } from '../embed/util';
import ImageSize from './image-size';

/**
Expand Down Expand Up @@ -103,6 +104,7 @@ class ImageEdit extends Component {
this.getFilename = this.getFilename.bind( this );
this.toggleIsEditing = this.toggleIsEditing.bind( this );
this.onUploadError = this.onUploadError.bind( this );
this.onImageError = this.onImageError.bind( this );

this.state = {
captionFocused: false,
Expand Down Expand Up @@ -208,6 +210,16 @@ class ImageEdit extends Component {
} );
}

onImageError( url ) {
// Check if there's an embed block that handles this URL.
const embedBlock = createUpgradedEmbedBlock(
{ attributes: { url } }
);
if ( undefined !== embedBlock ) {
this.props.onReplace( embedBlock );
}
}

onSetCustomHref( value ) {
this.props.setAttributes( { href: value } );
}
Expand Down Expand Up @@ -509,7 +521,7 @@ class ImageEdit extends Component {
// Disable reason: Image itself is not meant to be
// interactive, but should direct focus to block
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
const img = <img src={ url } alt={ defaultedAlt } onClick={ this.onImageClick } />;
const img = <img src={ url } alt={ defaultedAlt } onClick={ this.onImageClick } onError={ () => this.onImageError( url ) } />;

if ( ! isResizable || ! imageWidthWithinContainer ) {
return (
Expand Down