@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
8 lines (7 loc) • 5.96 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/utils/media-upload/index.js"],
"sourcesContent": ["/**\n * External dependencies\n */\nimport { v4 as uuid } from 'uuid';\n\n/**\n * WordPress dependencies\n */\nimport { select, dispatch } from '@wordpress/data';\nimport { store as coreDataStore } from '@wordpress/core-data';\nimport { uploadMedia } from '@wordpress/media-utils';\n\n/**\n * Internal dependencies\n */\nimport { store as editorStore } from '../../store';\n\nconst noop = () => {};\n\n/**\n * Upload a media file when the file upload button is activated.\n * Wrapper around uploadMedia() that injects the current post ID.\n *\n * @param {Object} $0 Parameters object passed to the function.\n * @param {?Object} $0.additionalData Additional data to include in the request.\n * @param {string} $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.\n * @param {Array} $0.filesList List of files.\n * @param {?number} $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.\n * @param {Function} $0.onError Function called when an error happens.\n * @param {Function} $0.onFileChange Function called each time a file or a temporary representation of the file is available.\n * @param {Function} $0.onSuccess Function called after the final representation of the file is available.\n * @param {boolean} $0.multiple Whether to allow multiple files to be uploaded.\n */\nexport default function mediaUpload( {\n\tadditionalData = {},\n\tallowedTypes,\n\tfilesList,\n\tmaxUploadFileSize,\n\tonError = noop,\n\tonFileChange,\n\tonSuccess,\n\tmultiple = true,\n} ) {\n\tconst { receiveEntityRecords } = dispatch( coreDataStore );\n\tconst { getCurrentPost, getEditorSettings } = select( editorStore );\n\tconst {\n\t\tlockPostAutosaving,\n\t\tunlockPostAutosaving,\n\t\tlockPostSaving,\n\t\tunlockPostSaving,\n\t} = dispatch( editorStore );\n\n\tconst wpAllowedMimeTypes = getEditorSettings().allowedMimeTypes;\n\tconst lockKey = `image-upload-${ uuid() }`;\n\tlet imageIsUploading = false;\n\tmaxUploadFileSize =\n\t\tmaxUploadFileSize || getEditorSettings().maxUploadFileSize;\n\tconst currentPost = getCurrentPost();\n\t// Templates and template parts' numerical ID is stored in `wp_id`.\n\tconst currentPostId =\n\t\ttypeof currentPost?.id === 'number'\n\t\t\t? currentPost.id\n\t\t\t: currentPost?.wp_id;\n\tconst setSaveLock = () => {\n\t\tif ( window.__clientSideMediaProcessing ) {\n\t\t\treturn; // Skip - handled by useUploadSaveLock in editor provider\n\t\t}\n\t\tlockPostSaving( lockKey );\n\t\tlockPostAutosaving( lockKey );\n\t\timageIsUploading = true;\n\t};\n\n\tconst postData = currentPostId ? { post: currentPostId } : {};\n\tconst clearSaveLock = () => {\n\t\tif ( window.__clientSideMediaProcessing ) {\n\t\t\treturn; // Skip - handled by useUploadSaveLock in editor provider\n\t\t}\n\t\tunlockPostSaving( lockKey );\n\t\tunlockPostAutosaving( lockKey );\n\t\timageIsUploading = false;\n\t};\n\n\tuploadMedia( {\n\t\tallowedTypes,\n\t\tfilesList,\n\t\tonFileChange: ( file ) => {\n\t\t\t// When client-side media processing is enabled, save locking\n\t\t\t// is handled by useUploadSaveLock in the editor provider.\n\t\t\tif ( ! window.__clientSideMediaProcessing ) {\n\t\t\t\tif ( ! imageIsUploading ) {\n\t\t\t\t\tsetSaveLock();\n\t\t\t\t} else {\n\t\t\t\t\tclearSaveLock();\n\t\t\t\t}\n\t\t\t}\n\t\t\tonFileChange?.( file );\n\n\t\t\t// Files are initially received by `onFileChange` as a blob.\n\t\t\t// After that the function is called a second time with the file as an entity.\n\t\t\t// For core-data, we only care about receiving/invalidating entities.\n\t\t\tconst entityFiles = file.filter( ( _file ) => _file?.id );\n\t\t\tif ( entityFiles?.length ) {\n\t\t\t\tconst invalidateCache = true;\n\t\t\t\treceiveEntityRecords(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'attachment',\n\t\t\t\t\tentityFiles,\n\t\t\t\t\tundefined,\n\t\t\t\t\tinvalidateCache\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t\tonSuccess,\n\t\tadditionalData: {\n\t\t\t...postData,\n\t\t\t...additionalData,\n\t\t},\n\t\tmaxUploadFileSize,\n\t\tonError: ( { message } ) => {\n\t\t\tif ( ! window.__clientSideMediaProcessing ) {\n\t\t\t\tclearSaveLock();\n\t\t\t}\n\t\t\tonError( message );\n\t\t},\n\t\twpAllowedMimeTypes,\n\t\tmultiple,\n\t} );\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA2B;AAK3B,kBAAiC;AACjC,uBAAuC;AACvC,yBAA4B;AAK5B,mBAAqC;AAErC,IAAM,OAAO,MAAM;AAAC;AAgBL,SAAR,YAA8B;AAAA,EACpC,iBAAiB,CAAC;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA,EACA,UAAU;AAAA,EACV;AAAA,EACA;AAAA,EACA,WAAW;AACZ,GAAI;AACH,QAAM,EAAE,qBAAqB,QAAI,sBAAU,iBAAAA,KAAc;AACzD,QAAM,EAAE,gBAAgB,kBAAkB,QAAI,oBAAQ,aAAAC,KAAY;AAClE,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,QAAI,sBAAU,aAAAA,KAAY;AAE1B,QAAM,qBAAqB,kBAAkB,EAAE;AAC/C,QAAM,UAAU,oBAAiB,YAAAC,IAAK,CAAE;AACxC,MAAI,mBAAmB;AACvB,sBACC,qBAAqB,kBAAkB,EAAE;AAC1C,QAAM,cAAc,eAAe;AAEnC,QAAM,gBACL,OAAO,aAAa,OAAO,WACxB,YAAY,KACZ,aAAa;AACjB,QAAM,cAAc,MAAM;AACzB,QAAK,OAAO,6BAA8B;AACzC;AAAA,IACD;AACA,mBAAgB,OAAQ;AACxB,uBAAoB,OAAQ;AAC5B,uBAAmB;AAAA,EACpB;AAEA,QAAM,WAAW,gBAAgB,EAAE,MAAM,cAAc,IAAI,CAAC;AAC5D,QAAM,gBAAgB,MAAM;AAC3B,QAAK,OAAO,6BAA8B;AACzC;AAAA,IACD;AACA,qBAAkB,OAAQ;AAC1B,yBAAsB,OAAQ;AA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