wp/wp-includes/js/dist/blob.js
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     1 /******/ (function() { // webpackBootstrap
     1 /******/ (() => { // webpackBootstrap
     2 /******/ 	"use strict";
     2 /******/ 	"use strict";
     3 /******/ 	// The require scope
     3 /******/ 	// The require scope
     4 /******/ 	var __webpack_require__ = {};
     4 /******/ 	var __webpack_require__ = {};
     5 /******/ 	
     5 /******/ 	
     6 /************************************************************************/
     6 /************************************************************************/
     7 /******/ 	/* webpack/runtime/define property getters */
     7 /******/ 	/* webpack/runtime/define property getters */
     8 /******/ 	!function() {
     8 /******/ 	(() => {
     9 /******/ 		// define getter functions for harmony exports
     9 /******/ 		// define getter functions for harmony exports
    10 /******/ 		__webpack_require__.d = function(exports, definition) {
    10 /******/ 		__webpack_require__.d = (exports, definition) => {
    11 /******/ 			for(var key in definition) {
    11 /******/ 			for(var key in definition) {
    12 /******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
    12 /******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
    13 /******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
    13 /******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
    14 /******/ 				}
    14 /******/ 				}
    15 /******/ 			}
    15 /******/ 			}
    16 /******/ 		};
    16 /******/ 		};
    17 /******/ 	}();
    17 /******/ 	})();
    18 /******/ 	
    18 /******/ 	
    19 /******/ 	/* webpack/runtime/hasOwnProperty shorthand */
    19 /******/ 	/* webpack/runtime/hasOwnProperty shorthand */
    20 /******/ 	!function() {
    20 /******/ 	(() => {
    21 /******/ 		__webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
    21 /******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
    22 /******/ 	}();
    22 /******/ 	})();
    23 /******/ 	
    23 /******/ 	
    24 /******/ 	/* webpack/runtime/make namespace object */
    24 /******/ 	/* webpack/runtime/make namespace object */
    25 /******/ 	!function() {
    25 /******/ 	(() => {
    26 /******/ 		// define __esModule on exports
    26 /******/ 		// define __esModule on exports
    27 /******/ 		__webpack_require__.r = function(exports) {
    27 /******/ 		__webpack_require__.r = (exports) => {
    28 /******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
    28 /******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
    29 /******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
    29 /******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
    30 /******/ 			}
    30 /******/ 			}
    31 /******/ 			Object.defineProperty(exports, '__esModule', { value: true });
    31 /******/ 			Object.defineProperty(exports, '__esModule', { value: true });
    32 /******/ 		};
    32 /******/ 		};
    33 /******/ 	}();
    33 /******/ 	})();
    34 /******/ 	
    34 /******/ 	
    35 /************************************************************************/
    35 /************************************************************************/
    36 var __webpack_exports__ = {};
    36 var __webpack_exports__ = {};
    37 __webpack_require__.r(__webpack_exports__);
    37 __webpack_require__.r(__webpack_exports__);
    38 /* harmony export */ __webpack_require__.d(__webpack_exports__, {
    38 /* harmony export */ __webpack_require__.d(__webpack_exports__, {
    39 /* harmony export */   "createBlobURL": function() { return /* binding */ createBlobURL; },
    39 /* harmony export */   createBlobURL: () => (/* binding */ createBlobURL),
    40 /* harmony export */   "getBlobByURL": function() { return /* binding */ getBlobByURL; },
    40 /* harmony export */   downloadBlob: () => (/* binding */ downloadBlob),
    41 /* harmony export */   "getBlobTypeByURL": function() { return /* binding */ getBlobTypeByURL; },
    41 /* harmony export */   getBlobByURL: () => (/* binding */ getBlobByURL),
    42 /* harmony export */   "isBlobURL": function() { return /* binding */ isBlobURL; },
    42 /* harmony export */   getBlobTypeByURL: () => (/* binding */ getBlobTypeByURL),
    43 /* harmony export */   "revokeBlobURL": function() { return /* binding */ revokeBlobURL; }
    43 /* harmony export */   isBlobURL: () => (/* binding */ isBlobURL),
       
    44 /* harmony export */   revokeBlobURL: () => (/* binding */ revokeBlobURL)
    44 /* harmony export */ });
    45 /* harmony export */ });
    45 /**
       
    46  * Browser dependencies
       
    47  */
       
    48 const {
       
    49   createObjectURL,
       
    50   revokeObjectURL
       
    51 } = window.URL;
       
    52 /**
    46 /**
    53  * @type {Record<string, File|undefined>}
    47  * @type {Record<string, File|undefined>}
    54  */
    48  */
       
    49 const cache = {};
    55 
    50 
    56 const cache = {};
       
    57 /**
    51 /**
    58  * Create a blob URL from a file.
    52  * Create a blob URL from a file.
    59  *
    53  *
    60  * @param {File} file The file to create a blob URL for.
    54  * @param {File} file The file to create a blob URL for.
    61  *
    55  *
    62  * @return {string} The blob URL.
    56  * @return {string} The blob URL.
    63  */
    57  */
    64 
       
    65 function createBlobURL(file) {
    58 function createBlobURL(file) {
    66   const url = createObjectURL(file);
    59   const url = window.URL.createObjectURL(file);
    67   cache[url] = file;
    60   cache[url] = file;
    68   return url;
    61   return url;
    69 }
    62 }
       
    63 
    70 /**
    64 /**
    71  * Retrieve a file based on a blob URL. The file must have been created by
    65  * Retrieve a file based on a blob URL. The file must have been created by
    72  * `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
    66  * `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
    73  * `undefined`.
    67  * `undefined`.
    74  *
    68  *
    75  * @param {string} url The blob URL.
    69  * @param {string} url The blob URL.
    76  *
    70  *
    77  * @return {File|undefined} The file for the blob URL.
    71  * @return {File|undefined} The file for the blob URL.
    78  */
    72  */
    79 
       
    80 function getBlobByURL(url) {
    73 function getBlobByURL(url) {
    81   return cache[url];
    74   return cache[url];
    82 }
    75 }
       
    76 
    83 /**
    77 /**
    84  * Retrieve a blob type based on URL. The file must have been created by
    78  * Retrieve a blob type based on URL. The file must have been created by
    85  * `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
    79  * `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
    86  * `undefined`.
    80  * `undefined`.
    87  *
    81  *
    88  * @param {string} url The blob URL.
    82  * @param {string} url The blob URL.
    89  *
    83  *
    90  * @return {string|undefined} The blob type.
    84  * @return {string|undefined} The blob type.
    91  */
    85  */
       
    86 function getBlobTypeByURL(url) {
       
    87   return getBlobByURL(url)?.type.split('/')[0]; // 0: media type , 1: file extension eg ( type: 'image/jpeg' ).
       
    88 }
    92 
    89 
    93 function getBlobTypeByURL(url) {
       
    94   var _getBlobByURL;
       
    95 
       
    96   return (_getBlobByURL = getBlobByURL(url)) === null || _getBlobByURL === void 0 ? void 0 : _getBlobByURL.type.split('/')[0]; // 0: media type , 1: file extension eg ( type: 'image/jpeg' ).
       
    97 }
       
    98 /**
    90 /**
    99  * Remove the resource and file cache from memory.
    91  * Remove the resource and file cache from memory.
   100  *
    92  *
   101  * @param {string} url The blob URL.
    93  * @param {string} url The blob URL.
   102  */
    94  */
   103 
       
   104 function revokeBlobURL(url) {
    95 function revokeBlobURL(url) {
   105   if (cache[url]) {
    96   if (cache[url]) {
   106     revokeObjectURL(url);
    97     window.URL.revokeObjectURL(url);
   107   }
    98   }
   108 
       
   109   delete cache[url];
    99   delete cache[url];
   110 }
   100 }
       
   101 
   111 /**
   102 /**
   112  * Check whether a url is a blob url.
   103  * Check whether a url is a blob url.
   113  *
   104  *
   114  * @param {string} url The URL.
   105  * @param {string|undefined} url The URL.
   115  *
   106  *
   116  * @return {boolean} Is the url a blob url?
   107  * @return {boolean} Is the url a blob url?
   117  */
   108  */
   118 
       
   119 function isBlobURL(url) {
   109 function isBlobURL(url) {
   120   if (!url || !url.indexOf) {
   110   if (!url || !url.indexOf) {
   121     return false;
   111     return false;
   122   }
   112   }
       
   113   return url.indexOf('blob:') === 0;
       
   114 }
   123 
   115 
   124   return url.indexOf('blob:') === 0;
   116 /**
       
   117  * Downloads a file, e.g., a text or readable stream, in the browser.
       
   118  * Appropriate for downloading smaller file sizes, e.g., < 5 MB.
       
   119  *
       
   120  * Example usage:
       
   121  *
       
   122  * ```js
       
   123  * 	const fileContent = JSON.stringify(
       
   124  * 		{
       
   125  * 			"title": "My Post",
       
   126  * 		},
       
   127  * 		null,
       
   128  * 		2
       
   129  * 	);
       
   130  * 	const filename = 'file.json';
       
   131  *
       
   132  * 	downloadBlob( filename, fileContent, 'application/json' );
       
   133  * ```
       
   134  *
       
   135  * @param {string}   filename    File name.
       
   136  * @param {BlobPart} content     File content (BufferSource | Blob | string).
       
   137  * @param {string}   contentType (Optional) File mime type. Default is `''`.
       
   138  */
       
   139 function downloadBlob(filename, content, contentType = '') {
       
   140   if (!filename || !content) {
       
   141     return;
       
   142   }
       
   143   const file = new window.Blob([content], {
       
   144     type: contentType
       
   145   });
       
   146   const url = window.URL.createObjectURL(file);
       
   147   const anchorElement = document.createElement('a');
       
   148   anchorElement.href = url;
       
   149   anchorElement.download = filename;
       
   150   anchorElement.style.display = 'none';
       
   151   document.body.appendChild(anchorElement);
       
   152   anchorElement.click();
       
   153   document.body.removeChild(anchorElement);
       
   154   window.URL.revokeObjectURL(url);
   125 }
   155 }
   126 
   156 
   127 (window.wp = window.wp || {}).blob = __webpack_exports__;
   157 (window.wp = window.wp || {}).blob = __webpack_exports__;
   128 /******/ })()
   158 /******/ })()
   129 ;
   159 ;