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

Block Library: Introduce the 'useUploadMediaFromBlobURL' utility hook #59350

Merged
merged 5 commits into from
Feb 29, 2024
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
28 changes: 9 additions & 19 deletions packages/block-library/src/audio/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { getBlobByURL, isBlobURL } from '@wordpress/blob';
import { isBlobURL } from '@wordpress/blob';
import {
Disabled,
PanelBody,
Expand All @@ -21,18 +21,17 @@ import {
MediaPlaceholder,
MediaReplaceFlow,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import { __, _x } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import { useDispatch } from '@wordpress/data';
import { audio as icon } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
*/
import { createUpgradedEmbedBlock } from '../embed/util';
import { useUploadMediaFromBlobURL } from '../utils/hooks';
import { Caption } from '../utils/caption';

const ALLOWED_MEDIA_TYPES = [ 'audio' ];
Expand All @@ -47,22 +46,13 @@ function AudioEdit( {
} ) {
const { id, autoplay, loop, preload, src } = attributes;
const isTemporaryAudio = ! id && isBlobURL( src );
const { getSettings } = useSelect( blockEditorStore );

useEffect( () => {
if ( ! id && isBlobURL( src ) ) {
const file = getBlobByURL( src );

if ( file ) {
getSettings().mediaUpload( {
filesList: [ file ],
onFileChange: ( [ media ] ) => onSelectAudio( media ),
onError: ( e ) => onUploadError( e ),
allowedTypes: ALLOWED_MEDIA_TYPES,
} );
}
}
}, [] );
useUploadMediaFromBlobURL( {
url: src,
allowedTypes: ALLOWED_MEDIA_TYPES,
onChange: onSelectAudio,
onError: onUploadError,
} );

function toggleAttribute( attribute ) {
return ( newValue ) => {
Expand Down
23 changes: 8 additions & 15 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { getBlobByURL, isBlobURL, revokeBlobURL } from '@wordpress/blob';
import { isBlobURL } from '@wordpress/blob';
import {
__unstableGetAnimateClassName as getAnimateClassName,
ResizableBox,
Expand Down Expand Up @@ -36,6 +36,7 @@ import { store as noticesStore } from '@wordpress/notices';
import FileBlockInspector from './inspector';
import { browserSupportsPdfs } from './utils';
import removeAnchorTag from '../utils/remove-anchor-tag';
import { useUploadMediaFromBlobURL } from '../utils/hooks';

export const MIN_PREVIEW_HEIGHT = 200;
export const MAX_PREVIEW_HEIGHT = 2000;
Expand Down Expand Up @@ -72,7 +73,6 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
displayPreview,
previewHeight,
} = attributes;
const { getSettings } = useSelect( blockEditorStore );
const { media } = useSelect(
( select ) => ( {
media:
Expand All @@ -86,20 +86,13 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
const { createErrorNotice } = useDispatch( noticesStore );
const { toggleSelection } = useDispatch( blockEditorStore );

useEffect( () => {
// Upload a file drag-and-dropped into the editor.
if ( isBlobURL( href ) ) {
const file = getBlobByURL( href );

getSettings().mediaUpload( {
filesList: [ file ],
onFileChange: ( [ newMedia ] ) => onSelectFile( newMedia ),
onError: onUploadError,
} );

revokeBlobURL( href );
}
useUploadMediaFromBlobURL( {
url: href,
onChange: onSelectFile,
onError: onUploadError,
} );

useEffect( () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

It might be a good idea to move the default downloadButtonText value handling to a server-side render function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree. The Home Link block takes such an approach: #58387

if ( RichText.isEmpty( downloadButtonText ) ) {
setAttributes( {
downloadButtonText: _x( 'Download', 'button label' ),
Expand Down
56 changes: 53 additions & 3 deletions packages/block-library/src/utils/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useLayoutEffect, useEffect, useRef } from '@wordpress/element';
import { getBlobByURL, isBlobURL, revokeBlobURL } from '@wordpress/blob';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';

/**
Expand All @@ -19,6 +22,53 @@ export function useCanEditEntity( kind, name, recordId ) {
);
}

export default {
useCanEditEntity,
};
/**
* Handles uploading a media file from a blob URL on mount.
*
* @param {Object} args Upload media arguments.
* @param {string} args.url Blob URL.
* @param {?Array} args.allowedTypes Array of allowed media types.
* @param {Function} args.onChange Function called when the media is uploaded.
* @param {Function} args.onError Function called when an error happens.
*/
export function useUploadMediaFromBlobURL( args = {} ) {
const latestArgs = useRef( args );
const { getSettings } = useSelect( blockEditorStore );

useLayoutEffect( () => {
latestArgs.current = args;
} );
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved

useEffect( () => {
if (
! latestArgs.current.url ||
! isBlobURL( latestArgs.current.url )
) {
return;
}

const file = getBlobByURL( latestArgs.current.url );
if ( ! file ) {
return;
}

const { url, allowedTypes, onChange, onError } = latestArgs.current;
const { mediaUpload } = getSettings();

mediaUpload( {
filesList: [ file ],
allowedTypes,
onFileChange: ( [ media ] ) => {
if ( isBlobURL( media?.url ) ) {
return;
}

revokeBlobURL( url );
onChange( media );
},
onError: ( message ) => {
onError( message );
},
} );
}, [ getSettings ] );
}
26 changes: 9 additions & 17 deletions packages/block-library/src/video/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { getBlobByURL, isBlobURL } from '@wordpress/blob';
import { isBlobURL } from '@wordpress/blob';
import {
BaseControl,
Button,
Expand All @@ -24,19 +24,19 @@ import {
MediaUploadCheck,
MediaReplaceFlow,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useRef, useEffect } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { useInstanceId } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { useDispatch } from '@wordpress/data';
import { video as icon } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
*/
import { createUpgradedEmbedBlock } from '../embed/util';
import { useUploadMediaFromBlobURL } from '../utils/hooks';
import VideoCommonSettings from './edit-common-settings';
import TracksEditor from './tracks-editor';
import Tracks from './tracks';
Expand Down Expand Up @@ -75,21 +75,13 @@ function VideoEdit( {
const posterImageButton = useRef();
const { id, controls, poster, src, tracks } = attributes;
const isTemporaryVideo = ! id && isBlobURL( src );
const { getSettings } = useSelect( blockEditorStore );

useEffect( () => {
if ( ! id && isBlobURL( src ) ) {
const file = getBlobByURL( src );
if ( file ) {
getSettings().mediaUpload( {
filesList: [ file ],
onFileChange: ( [ media ] ) => onSelectVideo( media ),
onError: onUploadError,
allowedTypes: ALLOWED_MEDIA_TYPES,
} );
}
}
}, [] );
useUploadMediaFromBlobURL( {
url: src,
allowedTypes: ALLOWED_MEDIA_TYPES,
onChange: onSelectVideo,
onError: onUploadError,
} );

useEffect( () => {
// Placeholder may be rendered.
Expand Down
Loading