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

Fix: Remove editor usage from media blocks. #15548

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
22 changes: 18 additions & 4 deletions packages/block-library/src/audio/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { getBlobByURL, isBlobURL } from '@wordpress/blob';
import { compose } from '@wordpress/compose';
import {
Disabled,
IconButton,
Expand All @@ -18,9 +19,9 @@ import {
MediaPlaceholder,
RichText,
} from '@wordpress/block-editor';
import { mediaUpload } from '@wordpress/editor';
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { withSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -48,7 +49,12 @@ class AudioEdit extends Component {
}

componentDidMount() {
const { attributes, noticeOperations, setAttributes } = this.props;
const {
attributes,
mediaUpload,
noticeOperations,
setAttributes,
} = this.props;
const { id, src = '' } = attributes;

if ( ! id && isBlobURL( src ) ) {
Expand Down Expand Up @@ -200,5 +206,13 @@ class AudioEdit extends Component {
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
}
}

export default withNotices( AudioEdit );
export default compose( [
withSelect( ( select ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about useSelect ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @youknowriad I did not use useSelect because these are class components (where hooks could not be used), and most of the cases already had a withSelect usage. I guess if there are some benefits of the usage of the hook, we can create specific PR's with a refactor to hooks before the next block change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense

const { getSettings } = select( 'core/block-editor' );
const { __experimentalMediaUpload } = getSettings();
return {
mediaUpload: __experimentalMediaUpload,
};
} ),
withNotices,
] )( AudioEdit );
11 changes: 9 additions & 2 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
MediaPlaceholder,
RichText,
} from '@wordpress/block-editor';
import { mediaUpload } from '@wordpress/editor';
import { Component } from '@wordpress/element';
import { __, _x } from '@wordpress/i18n';

Expand Down Expand Up @@ -55,7 +54,12 @@ class FileEdit extends Component {
}

componentDidMount() {
const { attributes, noticeOperations, setAttributes } = this.props;
const {
attributes,
mediaUpload,
noticeOperations,
setAttributes,
} = this.props;
const { downloadButtonText, href } = attributes;

// Upload a file drag-and-dropped into the editor
Expand Down Expand Up @@ -242,9 +246,12 @@ class FileEdit extends Component {
export default compose( [
withSelect( ( select, props ) => {
const { getMedia } = select( 'core' );
const { getSettings } = select( 'core/block-editor' );
const { __experimentalMediaUpload } = getSettings();
const { id } = props.attributes;
return {
media: id === undefined ? undefined : getMedia( id ),
mediaUpload: __experimentalMediaUpload,
};
} ),
withNotices,
Expand Down
32 changes: 30 additions & 2 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
* External dependencies
*/
import classnames from 'classnames';
import { filter, map } from 'lodash';
import { every, filter, forEach, map } from 'lodash';

/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';
import {
IconButton,
PanelBody,
Expand All @@ -25,6 +26,8 @@ import {
} from '@wordpress/block-editor';
import { Component } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { getBlobByURL, isBlobURL, revokeBlobURL } from '@wordpress/blob';
import { withSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -167,6 +170,20 @@ class GalleryEdit extends Component {
} );
}

componentDidMount() {
const { attributes, mediaUpload } = this.props;
const { images } = attributes;
if ( every( images, ( { url } ) => isBlobURL( url ) ) ) {
const filesList = map( images, ( { url } ) => getBlobByURL( url ) );
forEach( images, ( { url } ) => revokeBlobURL( url ) );
mediaUpload( {
filesList,
onFileChange: this.onSelectImages,
allowedTypes: [ 'image' ],
} );
}
}

componentDidUpdate( prevProps ) {
// Deselect images when deselecting the block
if ( ! this.props.isSelected && prevProps.isSelected ) {
Expand Down Expand Up @@ -305,5 +322,16 @@ class GalleryEdit extends Component {
);
}
}
export default compose( [
withSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );
const {
__experimentalMediaUpload,
} = getSettings();

export default withNotices( GalleryEdit );
return {
mediaUpload: __experimentalMediaUpload,
};
} ),
withNotices,
] )( GalleryEdit );
18 changes: 2 additions & 16 deletions packages/block-library/src/gallery/transforms.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/**
* External dependencies
*/
import { filter, every, map } from 'lodash';
import { filter, every } from 'lodash';

/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
import { mediaUpload } from '@wordpress/editor';
import { createBlobURL } from '@wordpress/blob';

/**
Expand Down Expand Up @@ -89,25 +88,12 @@ const transforms = {
isMatch( files ) {
return files.length !== 1 && every( files, ( file ) => file.type.indexOf( 'image/' ) === 0 );
},
transform( files, onChange ) {
transform( files ) {
const block = createBlock( 'core/gallery', {
images: files.map( ( file ) => pickRelevantMediaFiles( {
url: createBlobURL( file ),
} ) ),
} );
mediaUpload( {
filesList: files,
onFileChange: ( images ) => {
const imagesAttr = images.map(
pickRelevantMediaFiles,
);
onChange( block.clientId, {
ids: map( imagesAttr, 'id' ),
images: imagesAttr,
} );
},
allowedTypes: [ 'image' ],
} );
return block;
},
},
Expand Down
16 changes: 13 additions & 3 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
MediaPlaceholder,
RichText,
} from '@wordpress/block-editor';
import { mediaUpload } from '@wordpress/editor';
import { Component } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { getPath } from '@wordpress/url';
Expand Down Expand Up @@ -126,7 +125,12 @@ class ImageEdit extends Component {
}

componentDidMount() {
const { attributes, setAttributes, noticeOperations } = this.props;
const {
attributes,
mediaUpload,
noticeOperations,
setAttributes,
} = this.props;
const { id, url = '' } = attributes;

if ( isTemporaryImage( id, url ) ) {
Expand Down Expand Up @@ -720,13 +724,19 @@ export default compose( [
const { getMedia } = select( 'core' );
const { getSettings } = select( 'core/block-editor' );
const { id } = props.attributes;
const { maxWidth, isRTL, imageSizes } = getSettings();
const {
__experimentalMediaUpload,
imageSizes,
isRTL,
maxWidth,
} = getSettings();

return {
image: id ? getMedia( id ) : null,
maxWidth,
isRTL,
imageSizes,
mediaUpload: __experimentalMediaUpload,
};
} ),
withViewportMatch( { isLargeViewport: 'medium' } ),
Expand Down
18 changes: 16 additions & 2 deletions packages/block-library/src/video/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
MediaUploadCheck,
RichText,
} from '@wordpress/block-editor';
import { mediaUpload } from '@wordpress/editor';
import { Component, createRef } from '@wordpress/element';
import {
__,
Expand All @@ -32,6 +31,9 @@ import {
compose,
withInstanceId,
} from '@wordpress/compose';
import {
withSelect,
} from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -60,7 +62,12 @@ class VideoEdit extends Component {
}

componentDidMount() {
const { attributes, noticeOperations, setAttributes } = this.props;
const {
attributes,
mediaUpload,
noticeOperations,
setAttributes,
} = this.props;
const { id, src = '' } = attributes;
if ( ! id && isBlobURL( src ) ) {
const file = getBlobByURL( src );
Expand Down Expand Up @@ -306,6 +313,13 @@ class VideoEdit extends Component {
}

export default compose( [
withSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );
const { __experimentalMediaUpload } = getSettings();
return {
mediaUpload: __experimentalMediaUpload,
};
} ),
withNotices,
withInstanceId,
] )( VideoEdit );