UNPKG

@wordpress/upload-media

Version:
202 lines (201 loc) 4.56 kB
import { v4 as uuidv4 } from "uuid"; import { createBlobURL, isBlobURL, revokeBlobURL } from "@wordpress/blob"; import { cloneFile, convertBlobToFile } from "../utils"; import { StubFile } from "../stub-file"; import { ItemStatus, OperationType, Type } from "./types"; function addItem({ file: fileOrBlob, batchId, onChange, onSuccess, onBatchSuccess, onError, additionalData = {}, sourceUrl, sourceAttachmentId, abortController, operations }) { return async ({ dispatch }) => { const itemId = uuidv4(); const file = convertBlobToFile(fileOrBlob); let blobUrl; if (!(file instanceof StubFile)) { blobUrl = createBlobURL(file); dispatch({ type: Type.CacheBlobUrl, id: itemId, blobUrl }); } dispatch({ type: Type.Add, item: { id: itemId, batchId, status: ItemStatus.Processing, sourceFile: cloneFile(file), file, attachment: { url: blobUrl }, additionalData: { convert_format: false, ...additionalData }, onChange, onSuccess, onBatchSuccess, onError, sourceUrl, sourceAttachmentId, abortController: abortController || new AbortController(), operations: Array.isArray(operations) ? operations : [OperationType.Prepare] } }); dispatch.processItem(itemId); }; } function processItem(id) { return async ({ select, dispatch }) => { if (select.isPaused()) { return; } const item = select.getItem(id); const { attachment, onChange, onSuccess, onBatchSuccess, batchId } = item; const operation = Array.isArray(item.operations?.[0]) ? item.operations[0][0] : item.operations?.[0]; if (attachment) { onChange?.([attachment]); } if (!operation) { if (attachment) { onSuccess?.([attachment]); } dispatch.revokeBlobUrls(id); if (batchId && select.isBatchUploaded(batchId)) { onBatchSuccess?.(); } return; } if (!operation) { return; } dispatch({ type: Type.OperationStart, id, operation }); switch (operation) { case OperationType.Prepare: dispatch.prepareItem(item.id); break; case OperationType.Upload: dispatch.uploadItem(id); break; } }; } function pauseQueue() { return { type: Type.PauseQueue }; } function resumeQueue() { return async ({ select, dispatch }) => { dispatch({ type: Type.ResumeQueue }); for (const item of select.getAllItems()) { dispatch.processItem(item.id); } }; } function removeItem(id) { return async ({ select, dispatch }) => { const item = select.getItem(id); if (!item) { return; } dispatch({ type: Type.Remove, id }); }; } function finishOperation(id, updates) { return async ({ dispatch }) => { dispatch({ type: Type.OperationFinish, id, item: updates }); dispatch.processItem(id); }; } function prepareItem(id) { return async ({ dispatch }) => { const operations = [OperationType.Upload]; dispatch({ type: Type.AddOperations, id, operations }); dispatch.finishOperation(id, {}); }; } function uploadItem(id) { return async ({ select, dispatch }) => { const item = select.getItem(id); select.getSettings().mediaUpload({ filesList: [item.file], additionalData: item.additionalData, signal: item.abortController?.signal, onFileChange: ([attachment]) => { if (!isBlobURL(attachment.url)) { dispatch.finishOperation(id, { attachment }); } }, onSuccess: ([attachment]) => { dispatch.finishOperation(id, { attachment }); }, onError: (error) => { dispatch.cancelItem(id, error); } }); }; } function revokeBlobUrls(id) { return async ({ select, dispatch }) => { const blobUrls = select.getBlobUrls(id); for (const blobUrl of blobUrls) { revokeBlobURL(blobUrl); } dispatch({ type: Type.RevokeBlobUrls, id }); }; } function updateSettings(settings) { return { type: Type.UpdateSettings, settings }; } export { addItem, finishOperation, pauseQueue, prepareItem, processItem, removeItem, resumeQueue, revokeBlobUrls, updateSettings, uploadItem }; //# sourceMappingURL=private-actions.js.map