@wordpress/upload-media 是一个实验性的 WordPress 包,提供基于 @wordpress/data 存储的媒体上传队列处理系统,支持客户端文件预处理(如图像压缩)。它主要用于 @wordpress/block-editor,但也可独立使用。
import { store as uploadStore } from '@wordpress/upload-media';
import { dispatch } from '@wordpress/data';
dispatch( uploadStore ).updateSettings( /* ... */ );
dispatch( uploadStore ).addItems( [
/* ... */
] );This module is a media upload handler with a queue-like system that is implemented using a custom @wordpress/data store.
Such a system is useful for additional client-side processing of media files (e.g. image compression) before uploading them to a server.
It is typically used by @wordpress/block-editor but can also be leveraged outside of it.
Install the module
npm install @wordpress/upload-media --save
This is a basic example of how one can interact with the upload data store:
import { store as uploadStore } from '@wordpress/upload-media';
import { dispatch } from '@wordpress/data';
dispatch( uploadStore ).updateSettings( /* ... */ );
dispatch( uploadStore ).addItems( [
/* ... */
] );
Refer to the API reference below or the TypeScript types for further help.
The following set of dispatching action creators are available on the object returned by wp.data.dispatch( 'core/upload-media' ):
Adds a new item to the upload queue.
Parameters
AddItemsArgs:AddItemsArgs[ 'files' ]: Files[AddItemsArgs[ 'onChange' ]]: Function called each time a file or a temporary representation of the file is available.[AddItemsArgs[ 'onSuccess' ]]: Function called after the file is uploaded.[AddItemsArgs[ 'onBatchSuccess' ]]: Function called after a batch of files is uploaded.[AddItemsArgs[ 'onError' ]]: Function called when an error happens.[AddItemsArgs[ 'additionalData' ]]: Additional data to include in the request.[AddItemsArgs[ 'allowedTypes' ]]: Array with the types of media that can be uploaded, if unset all types are allowed.Cancels an item in the queue based on an error.
Parameters
QueueItemId: Item ID.Error: Error instance.onError callback.Retries a failed item in the queue.
Parameters
QueueItemId: Item ID.The following selectors are available on the object returned by wp.data.select( 'core/upload-media' ):
Returns all items currently being uploaded.
Parameters
State: Upload state.Returns
QueueItem[]: Queue items.Returns the media upload settings.
Parameters
State: Upload state.Returns
Settings: SettingsDetermines whether any upload is currently in progress.
Parameters
State: Upload state.Returns
boolean: Whether any upload is currently in progress.Determines whether an upload is currently in progress given an attachment ID.
Parameters
State: Upload state.number: Attachment ID.Returns
boolean: Whether upload is currently in progress for the given attachment.Determines whether an upload is currently in progress given an attachment URL.
Parameters
State: Upload state.string: Attachment URL.Returns
boolean: Whether upload is currently in progress for the given attachment.