[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/ -> blob.js (source)

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


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref