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

Image: revert error handling #41221

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export function MediaPlaceholder( {
onDoubleClick,
onFilesPreUpload = noop,
onHTMLDrop = noop,
onClose = noop,
children,
mediaLibraryButton,
placeholder,
Expand Down Expand Up @@ -327,7 +326,6 @@ export function MediaPlaceholder( {
gallery={ multiple && onlyAllowsImages() }
multiple={ multiple }
onSelect={ onSelect }
onClose={ onClose }
allowedTypes={ allowedTypes }
value={
Array.isArray( value )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const MediaReplaceFlow = ( {
onSelect,
onSelectURL,
onFilesUpload = noop,
onCloseModal = noop,
name = __( 'Replace' ),
createNotice,
removeNotice,
Expand Down Expand Up @@ -158,7 +157,6 @@ const MediaReplaceFlow = ( {
selectMedia( media, onClose )
}
allowedTypes={ allowedTypes }
onClose={ onCloseModal }
render={ ( { open } ) => (
<MenuItem icon={ mediaIcon } onClick={ open }>
{ __( 'Open Media Library' ) }
Expand Down
54 changes: 1 addition & 53 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useEffect, useRef, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { __ } from '@wordpress/i18n';
import { image as icon } from '@wordpress/icons';

/**
Expand Down Expand Up @@ -85,20 +85,6 @@ function hasDefaultSize( image, defaultSize ) {
);
}

/**
* Checks if a media attachment object has been "destroyed",
* that is, removed from the media library. The core Media Library
* adds a `destroyed` property to a deleted attachment object in the media collection.
*
* @param {number} id The attachment id.
*
* @return {boolean} Whether the image has been destroyed.
*/
export function isMediaDestroyed( id ) {
const attachment = window?.wp?.media?.attachment( id ) || {};
return attachment.destroyed;
}

export function ImageEdit( {
attributes,
setAttributes,
Expand Down Expand Up @@ -139,41 +125,6 @@ export function ImageEdit( {
return pick( getSettings(), [ 'imageDefaultSize', 'mediaUpload' ] );
}, [] );

// A callback passed to MediaUpload,
// fired when the media modal closes.
function onCloseModal() {
if ( isMediaDestroyed( attributes?.id ) ) {
setAttributes( {
url: undefined,
id: undefined,
} );
}
}

/*
Runs an error callback if the image does not load.
If the error callback is triggered, we infer that that image
has been deleted.
*/
function onImageError( isReplaced = false ) {
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice(
sprintf(
/* translators: %s url or missing image */
__( 'Error loading image: %s' ),
url
)
);
// If the image block was not replaced with an embed,
// clear the attributes and trigger the placeholder.
if ( ! isReplaced ) {
setAttributes( {
url: undefined,
id: undefined,
} );
}
}

function onUploadError( message ) {
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice( message );
Expand Down Expand Up @@ -372,8 +323,6 @@ export function ImageEdit( {
containerRef={ ref }
context={ context }
clientId={ clientId }
onCloseModal={ onCloseModal }
onImageLoadError={ onImageError }
/>
) }
{ ! url && (
Expand All @@ -390,7 +339,6 @@ export function ImageEdit( {
onSelectURL={ onSelectURL }
notices={ noticeUI }
onError={ onUploadError }
onClose={ onCloseModal }
accept="image/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ { id, src } }
Expand Down
13 changes: 2 additions & 11 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { store as coreStore } from '@wordpress/core-data';
*/
import { createUpgradedEmbedBlock } from '../embed/util';
import useClientWidth from './use-client-width';
import { isExternalImage, isMediaDestroyed } from './edit';
import { isExternalImage } from './edit';

/**
* Module constants
Expand Down Expand Up @@ -76,14 +76,12 @@ export default function Image( {
isSelected,
insertBlocksAfter,
onReplace,
onCloseModal,
onSelectImage,
onSelectURL,
onUploadError,
containerRef,
context,
clientId,
onImageLoadError,
} ) {
const imageRef = useRef();
const captionRef = useRef();
Expand Down Expand Up @@ -225,13 +223,10 @@ export default function Image( {
// Check if there's an embed block that handles this URL, e.g., instagram URL.
// See: https://github.com/WordPress/gutenberg/pull/11472
const embedBlock = createUpgradedEmbedBlock( { attributes: { url } } );
const shouldReplace = undefined !== embedBlock;

if ( shouldReplace ) {
if ( undefined !== embedBlock ) {
onReplace( embedBlock );
}

onImageLoadError( shouldReplace );
}

function onSetHref( props ) {
Expand Down Expand Up @@ -303,9 +298,6 @@ export default function Image( {
if ( ! isSelected ) {
setIsEditingImage( false );
}
if ( isSelected && isMediaDestroyed( id ) ) {
onImageLoadError();
}
}, [ isSelected ] );

const canEditImage = id && naturalWidth && naturalHeight && imageEditing;
Expand Down Expand Up @@ -369,7 +361,6 @@ export default function Image( {
onSelect={ onSelectImage }
onSelectURL={ onSelectURL }
onError={ onUploadError }
onCloseModal={ onCloseModal }
/>
</BlockControls>
) }
Expand Down