@wordpress/upload-media
Version:
Core media upload logic.
129 lines • 5.12 kB
TypeScript
import type { createRegistry } from '@wordpress/data';
type WPDataRegistry = ReturnType<typeof createRegistry>;
import type { AdditionalData, BatchId, OnBatchSuccessHandler, OnChangeHandler, OnErrorHandler, OnSuccessHandler, Operation, PauseQueueAction, QueueItem, QueueItemId, Settings, State, UpdateSettingsAction } from './types';
import type { cancelItem } from './actions';
type ActionCreators = {
cancelItem: typeof cancelItem;
addItem: typeof addItem;
removeItem: typeof removeItem;
prepareItem: typeof prepareItem;
processItem: typeof processItem;
finishOperation: typeof finishOperation;
uploadItem: typeof uploadItem;
revokeBlobUrls: typeof revokeBlobUrls;
<T = Record<string, unknown>>(args: T): void;
};
type AllSelectors = typeof import('./selectors') & typeof import('./private-selectors');
type CurriedState<F> = F extends (state: State, ...args: infer P) => infer R ? (...args: P) => R : F;
type Selectors = {
[key in keyof AllSelectors]: CurriedState<AllSelectors[key]>;
};
type ThunkArgs = {
select: Selectors;
dispatch: ActionCreators;
registry: WPDataRegistry;
};
interface AddItemArgs {
file: File | Blob;
batchId?: BatchId;
onChange?: OnChangeHandler;
onSuccess?: OnSuccessHandler;
onError?: OnErrorHandler;
onBatchSuccess?: OnBatchSuccessHandler;
additionalData?: AdditionalData;
sourceUrl?: string;
sourceAttachmentId?: number;
abortController?: AbortController;
operations?: Operation[];
}
/**
* Adds a new item to the upload queue.
*
* @param $0
* @param $0.file File
* @param [$0.batchId] Batch ID.
* @param [$0.onChange] Function called each time a file or a temporary representation of the file is available.
* @param [$0.onSuccess] Function called after the file is uploaded.
* @param [$0.onBatchSuccess] Function called after a batch of files is uploaded.
* @param [$0.onError] Function called when an error happens.
* @param [$0.additionalData] Additional data to include in the request.
* @param [$0.sourceUrl] Source URL. Used when importing a file from a URL or optimizing an existing file.
* @param [$0.sourceAttachmentId] Source attachment ID. Used when optimizing an existing file for example.
* @param [$0.abortController] Abort controller for upload cancellation.
* @param [$0.operations] List of operations to perform. Defaults to automatically determined list, based on the file.
*/
export declare function addItem({ file: fileOrBlob, batchId, onChange, onSuccess, onBatchSuccess, onError, additionalData, sourceUrl, sourceAttachmentId, abortController, operations, }: AddItemArgs): ({ dispatch }: ThunkArgs) => Promise<void>;
/**
* Processes a single item in the queue.
*
* Runs the next operation in line and invokes any callbacks.
*
* @param id Item ID.
*/
export declare function processItem(id: QueueItemId): ({ select, dispatch }: ThunkArgs) => Promise<void>;
/**
* Returns an action object that pauses all processing in the queue.
*
* Useful for testing purposes.
*
* @return Action object.
*/
export declare function pauseQueue(): PauseQueueAction;
/**
* Resumes all processing in the queue.
*
* Dispatches an action object for resuming the queue itself,
* and triggers processing for each remaining item in the queue individually.
*/
export declare function resumeQueue(): ({ select, dispatch }: ThunkArgs) => Promise<void>;
/**
* Removes a specific item from the queue.
*
* @param id Item ID.
*/
export declare function removeItem(id: QueueItemId): ({ select, dispatch }: ThunkArgs) => Promise<void>;
/**
* Finishes an operation for a given item ID and immediately triggers processing the next one.
*
* @param id Item ID.
* @param updates Updated item data.
*/
export declare function finishOperation(id: QueueItemId, updates: Partial<QueueItem>): ({ dispatch }: ThunkArgs) => Promise<void>;
/**
* Prepares an item for initial processing.
*
* Determines the list of operations to perform for a given image,
* depending on its media type.
*
* For example, HEIF images first need to be converted, resized,
* compressed, and then uploaded.
*
* Or videos need to be compressed, and then need poster generation
* before upload.
*
* @param id Item ID.
*/
export declare function prepareItem(id: QueueItemId): ({ dispatch }: ThunkArgs) => Promise<void>;
/**
* Uploads an item to the server.
*
* @param id Item ID.
*/
export declare function uploadItem(id: QueueItemId): ({ select, dispatch }: ThunkArgs) => Promise<void>;
/**
* Revokes all blob URLs for a given item, freeing up memory.
*
* @param id Item ID.
*/
export declare function revokeBlobUrls(id: QueueItemId): ({ select, dispatch }: ThunkArgs) => Promise<void>;
/**
* Returns an action object that pauses all processing in the queue.
*
* Useful for testing purposes.
*
* @param settings
* @return Action object.
*/
export declare function updateSettings(settings: Partial<Settings>): UpdateSettingsAction;
export {};
//# sourceMappingURL=private-actions.d.ts.map