wp/wp-includes/js/dist/blob.js
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    41 /* harmony export */   getBlobByURL: () => (/* binding */ getBlobByURL),
    41 /* harmony export */   getBlobByURL: () => (/* binding */ getBlobByURL),
    42 /* harmony export */   getBlobTypeByURL: () => (/* binding */ getBlobTypeByURL),
    42 /* harmony export */   getBlobTypeByURL: () => (/* binding */ getBlobTypeByURL),
    43 /* harmony export */   isBlobURL: () => (/* binding */ isBlobURL),
    43 /* harmony export */   isBlobURL: () => (/* binding */ isBlobURL),
    44 /* harmony export */   revokeBlobURL: () => (/* binding */ revokeBlobURL)
    44 /* harmony export */   revokeBlobURL: () => (/* binding */ revokeBlobURL)
    45 /* harmony export */ });
    45 /* harmony export */ });
    46 /**
    46 /* wp:polyfill */
    47  * @type {Record<string, File|undefined>}
       
    48  */
       
    49 const cache = {};
    47 const cache = {};
    50 
    48 
    51 /**
    49 /**
    52  * Create a blob URL from a file.
    50  * Create a blob URL from a file.
    53  *
    51  *
    54  * @param {File} file The file to create a blob URL for.
    52  * @param file The file to create a blob URL for.
    55  *
    53  *
    56  * @return {string} The blob URL.
    54  * @return The blob URL.
    57  */
    55  */
    58 function createBlobURL(file) {
    56 function createBlobURL(file) {
    59   const url = window.URL.createObjectURL(file);
    57   const url = window.URL.createObjectURL(file);
    60   cache[url] = file;
    58   cache[url] = file;
    61   return url;
    59   return url;
    64 /**
    62 /**
    65  * Retrieve a file based on a blob URL. The file must have been created by
    63  * Retrieve a file based on a blob URL. The file must have been created by
    66  * `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
    64  * `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
    67  * `undefined`.
    65  * `undefined`.
    68  *
    66  *
    69  * @param {string} url The blob URL.
    67  * @param url The blob URL.
    70  *
    68  *
    71  * @return {File|undefined} The file for the blob URL.
    69  * @return The file for the blob URL.
    72  */
    70  */
    73 function getBlobByURL(url) {
    71 function getBlobByURL(url) {
    74   return cache[url];
    72   return cache[url];
    75 }
    73 }
    76 
    74 
    77 /**
    75 /**
    78  * Retrieve a blob type based on URL. The file must have been created by
    76  * Retrieve a blob type based on URL. The file must have been created by
    79  * `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
    77  * `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
    80  * `undefined`.
    78  * `undefined`.
    81  *
    79  *
    82  * @param {string} url The blob URL.
    80  * @param url The blob URL.
    83  *
    81  *
    84  * @return {string|undefined} The blob type.
    82  * @return The blob type.
    85  */
    83  */
    86 function getBlobTypeByURL(url) {
    84 function getBlobTypeByURL(url) {
    87   return getBlobByURL(url)?.type.split('/')[0]; // 0: media type , 1: file extension eg ( type: 'image/jpeg' ).
    85   return getBlobByURL(url)?.type.split('/')[0]; // 0: media type , 1: file extension eg ( type: 'image/jpeg' ).
    88 }
    86 }
    89 
    87 
    90 /**
    88 /**
    91  * Remove the resource and file cache from memory.
    89  * Remove the resource and file cache from memory.
    92  *
    90  *
    93  * @param {string} url The blob URL.
    91  * @param url The blob URL.
    94  */
    92  */
    95 function revokeBlobURL(url) {
    93 function revokeBlobURL(url) {
    96   if (cache[url]) {
    94   if (cache[url]) {
    97     window.URL.revokeObjectURL(url);
    95     window.URL.revokeObjectURL(url);
    98   }
    96   }
   100 }
    98 }
   101 
    99 
   102 /**
   100 /**
   103  * Check whether a url is a blob url.
   101  * Check whether a url is a blob url.
   104  *
   102  *
   105  * @param {string|undefined} url The URL.
   103  * @param url The URL.
   106  *
   104  *
   107  * @return {boolean} Is the url a blob url?
   105  * @return Is the url a blob url?
   108  */
   106  */
   109 function isBlobURL(url) {
   107 function isBlobURL(url) {
   110   if (!url || !url.indexOf) {
   108   if (!url || !url.indexOf) {
   111     return false;
   109     return false;
   112   }
   110   }
   130  * 	const filename = 'file.json';
   128  * 	const filename = 'file.json';
   131  *
   129  *
   132  * 	downloadBlob( filename, fileContent, 'application/json' );
   130  * 	downloadBlob( filename, fileContent, 'application/json' );
   133  * ```
   131  * ```
   134  *
   132  *
   135  * @param {string}   filename    File name.
   133  * @param filename    File name.
   136  * @param {BlobPart} content     File content (BufferSource | Blob | string).
   134  * @param content     File content (BufferSource | Blob | string).
   137  * @param {string}   contentType (Optional) File mime type. Default is `''`.
   135  * @param contentType (Optional) File mime type. Default is `''`.
   138  */
   136  */
   139 function downloadBlob(filename, content, contentType = '') {
   137 function downloadBlob(filename, content, contentType = '') {
   140   if (!filename || !content) {
   138   if (!filename || !content) {
   141     return;
   139     return;
   142   }
   140   }