[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/dist/ -> list-reusable-blocks.js (source)

   1  /******/ (function() { // webpackBootstrap
   2  /******/     "use strict";
   3  /******/     // The require scope
   4  /******/     var __webpack_require__ = {};
   5  /******/     
   6  /************************************************************************/
   7  /******/     /* webpack/runtime/compat get default export */
   8  /******/     !function() {
   9  /******/         // getDefaultExport function for compatibility with non-harmony modules
  10  /******/         __webpack_require__.n = function(module) {
  11  /******/             var getter = module && module.__esModule ?
  12  /******/                 function() { return module['default']; } :
  13  /******/                 function() { return module; };
  14  /******/             __webpack_require__.d(getter, { a: getter });
  15  /******/             return getter;
  16  /******/         };
  17  /******/     }();
  18  /******/     
  19  /******/     /* webpack/runtime/define property getters */
  20  /******/     !function() {
  21  /******/         // define getter functions for harmony exports
  22  /******/         __webpack_require__.d = function(exports, definition) {
  23  /******/             for(var key in definition) {
  24  /******/                 if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  25  /******/                     Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  26  /******/                 }
  27  /******/             }
  28  /******/         };
  29  /******/     }();
  30  /******/     
  31  /******/     /* webpack/runtime/hasOwnProperty shorthand */
  32  /******/     !function() {
  33  /******/         __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  34  /******/     }();
  35  /******/     
  36  /******/     /* webpack/runtime/make namespace object */
  37  /******/     !function() {
  38  /******/         // define __esModule on exports
  39  /******/         __webpack_require__.r = function(exports) {
  40  /******/             if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  41  /******/                 Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  42  /******/             }
  43  /******/             Object.defineProperty(exports, '__esModule', { value: true });
  44  /******/         };
  45  /******/     }();
  46  /******/     
  47  /************************************************************************/
  48  var __webpack_exports__ = {};
  49  // ESM COMPAT FLAG
  50  __webpack_require__.r(__webpack_exports__);
  51  
  52  ;// CONCATENATED MODULE: external ["wp","element"]
  53  var external_wp_element_namespaceObject = window["wp"]["element"];
  54  ;// CONCATENATED MODULE: external ["wp","i18n"]
  55  var external_wp_i18n_namespaceObject = window["wp"]["i18n"];
  56  ;// CONCATENATED MODULE: external "lodash"
  57  var external_lodash_namespaceObject = window["lodash"];
  58  ;// CONCATENATED MODULE: external ["wp","apiFetch"]
  59  var external_wp_apiFetch_namespaceObject = window["wp"]["apiFetch"];
  60  var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_namespaceObject);
  61  ;// CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/utils/file.js
  62  /**
  63   * Downloads a file.
  64   *
  65   * @param {string} fileName    File Name.
  66   * @param {string} content     File Content.
  67   * @param {string} contentType File mime type.
  68   */
  69  function download(fileName, content, contentType) {
  70    const file = new window.Blob([content], {
  71      type: contentType
  72    }); // IE11 can't use the click to download technique
  73    // we use a specific IE11 technique instead.
  74  
  75    if (window.navigator.msSaveOrOpenBlob) {
  76      window.navigator.msSaveOrOpenBlob(file, fileName);
  77    } else {
  78      const a = document.createElement('a');
  79      a.href = URL.createObjectURL(file);
  80      a.download = fileName;
  81      a.style.display = 'none';
  82      document.body.appendChild(a);
  83      a.click();
  84      document.body.removeChild(a);
  85    }
  86  }
  87  /**
  88   * Reads the textual content of the given file.
  89   *
  90   * @param {File} file File.
  91   * @return {Promise<string>}  Content of the file.
  92   */
  93  
  94  function readTextFile(file) {
  95    const reader = new window.FileReader();
  96    return new Promise(resolve => {
  97      reader.onload = () => {
  98        resolve(reader.result);
  99      };
 100  
 101      reader.readAsText(file);
 102    });
 103  }
 104  
 105  ;// CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/utils/export.js
 106  /**
 107   * External dependencies
 108   */
 109  
 110  /**
 111   * WordPress dependencies
 112   */
 113  
 114  
 115  /**
 116   * Internal dependencies
 117   */
 118  
 119  
 120  /**
 121   * Export a reusable block as a JSON file.
 122   *
 123   * @param {number} id
 124   */
 125  
 126  async function exportReusableBlock(id) {
 127    const postType = await external_wp_apiFetch_default()({
 128      path: `/wp/v2/types/wp_block`
 129    });
 130    const post = await external_wp_apiFetch_default()({
 131      path: `/wp/v2/$postType.rest_base}/$id}?context=edit`
 132    });
 133    const title = post.title.raw;
 134    const content = post.content.raw;
 135    const fileContent = JSON.stringify({
 136      __file: 'wp_block',
 137      title,
 138      content
 139    }, null, 2);
 140    const fileName = (0,external_lodash_namespaceObject.kebabCase)(title) + '.json';
 141    download(fileName, fileContent, 'application/json');
 142  }
 143  
 144  /* harmony default export */ var utils_export = (exportReusableBlock);
 145  
 146  ;// CONCATENATED MODULE: external ["wp","components"]
 147  var external_wp_components_namespaceObject = window["wp"]["components"];
 148  ;// CONCATENATED MODULE: external ["wp","compose"]
 149  var external_wp_compose_namespaceObject = window["wp"]["compose"];
 150  ;// CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/utils/import.js
 151  /**
 152   * External dependencies
 153   */
 154  
 155  /**
 156   * WordPress dependencies
 157   */
 158  
 159  
 160  /**
 161   * Internal dependencies
 162   */
 163  
 164  
 165  /**
 166   * Import a reusable block from a JSON file.
 167   *
 168   * @param {File} file File.
 169   * @return {Promise} Promise returning the imported reusable block.
 170   */
 171  
 172  async function importReusableBlock(file) {
 173    const fileContent = await readTextFile(file);
 174    let parsedContent;
 175  
 176    try {
 177      parsedContent = JSON.parse(fileContent);
 178    } catch (e) {
 179      throw new Error('Invalid JSON file');
 180    }
 181  
 182    if (parsedContent.__file !== 'wp_block' || !parsedContent.title || !parsedContent.content || !(0,external_lodash_namespaceObject.isString)(parsedContent.title) || !(0,external_lodash_namespaceObject.isString)(parsedContent.content)) {
 183      throw new Error('Invalid Reusable block JSON file');
 184    }
 185  
 186    const postType = await external_wp_apiFetch_default()({
 187      path: `/wp/v2/types/wp_block`
 188    });
 189    const reusableBlock = await external_wp_apiFetch_default()({
 190      path: `/wp/v2/$postType.rest_base}`,
 191      data: {
 192        title: parsedContent.title,
 193        content: parsedContent.content,
 194        status: 'publish'
 195      },
 196      method: 'POST'
 197    });
 198    return reusableBlock;
 199  }
 200  
 201  /* harmony default export */ var utils_import = (importReusableBlock);
 202  
 203  ;// CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/components/import-form/index.js
 204  
 205  
 206  /**
 207   * WordPress dependencies
 208   */
 209  
 210  
 211  
 212  
 213  /**
 214   * Internal dependencies
 215   */
 216  
 217  
 218  
 219  function ImportForm(_ref) {
 220    let {
 221      instanceId,
 222      onUpload
 223    } = _ref;
 224    const inputId = 'list-reusable-blocks-import-form-' + instanceId;
 225    const formRef = (0,external_wp_element_namespaceObject.useRef)();
 226    const [isLoading, setIsLoading] = (0,external_wp_element_namespaceObject.useState)(false);
 227    const [error, setError] = (0,external_wp_element_namespaceObject.useState)(null);
 228    const [file, setFile] = (0,external_wp_element_namespaceObject.useState)(null);
 229  
 230    const onChangeFile = event => {
 231      setFile(event.target.files[0]);
 232      setError(null);
 233    };
 234  
 235    const onSubmit = event => {
 236      event.preventDefault();
 237  
 238      if (!file) {
 239        return;
 240      }
 241  
 242      setIsLoading({
 243        isLoading: true
 244      });
 245      utils_import(file).then(reusableBlock => {
 246        if (!formRef) {
 247          return;
 248        }
 249  
 250        setIsLoading(false);
 251        onUpload(reusableBlock);
 252      }).catch(errors => {
 253        if (!formRef) {
 254          return;
 255        }
 256  
 257        let uiMessage;
 258  
 259        switch (errors.message) {
 260          case 'Invalid JSON file':
 261            uiMessage = (0,external_wp_i18n_namespaceObject.__)('Invalid JSON file');
 262            break;
 263  
 264          case 'Invalid Reusable block JSON file':
 265            uiMessage = (0,external_wp_i18n_namespaceObject.__)('Invalid Reusable block JSON file');
 266            break;
 267  
 268          default:
 269            uiMessage = (0,external_wp_i18n_namespaceObject.__)('Unknown error');
 270        }
 271  
 272        setIsLoading(false);
 273        setError(uiMessage);
 274      });
 275    };
 276  
 277    const onDismissError = () => {
 278      setError(null);
 279    };
 280  
 281    return (0,external_wp_element_namespaceObject.createElement)("form", {
 282      className: "list-reusable-blocks-import-form",
 283      onSubmit: onSubmit,
 284      ref: formRef
 285    }, error && (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.Notice, {
 286      status: "error",
 287      onRemove: () => onDismissError()
 288    }, error), (0,external_wp_element_namespaceObject.createElement)("label", {
 289      htmlFor: inputId,
 290      className: "list-reusable-blocks-import-form__label"
 291    }, (0,external_wp_i18n_namespaceObject.__)('File')), (0,external_wp_element_namespaceObject.createElement)("input", {
 292      id: inputId,
 293      type: "file",
 294      onChange: onChangeFile
 295    }), (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.Button, {
 296      type: "submit",
 297      isBusy: isLoading,
 298      disabled: !file || isLoading,
 299      variant: "secondary",
 300      className: "list-reusable-blocks-import-form__button"
 301    }, (0,external_wp_i18n_namespaceObject._x)('Import', 'button label')));
 302  }
 303  
 304  /* harmony default export */ var import_form = ((0,external_wp_compose_namespaceObject.withInstanceId)(ImportForm));
 305  
 306  ;// CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/components/import-dropdown/index.js
 307  
 308  
 309  /**
 310   * External dependencies
 311   */
 312  
 313  /**
 314   * WordPress dependencies
 315   */
 316  
 317  
 318  
 319  /**
 320   * Internal dependencies
 321   */
 322  
 323  
 324  
 325  function ImportDropdown(_ref) {
 326    let {
 327      onUpload
 328    } = _ref;
 329    return (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.Dropdown, {
 330      position: "bottom right",
 331      contentClassName: "list-reusable-blocks-import-dropdown__content",
 332      renderToggle: _ref2 => {
 333        let {
 334          isOpen,
 335          onToggle
 336        } = _ref2;
 337        return (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.Button, {
 338          "aria-expanded": isOpen,
 339          onClick: onToggle,
 340          variant: "primary"
 341        }, (0,external_wp_i18n_namespaceObject.__)('Import from JSON'));
 342      },
 343      renderContent: _ref3 => {
 344        let {
 345          onClose
 346        } = _ref3;
 347        return (0,external_wp_element_namespaceObject.createElement)(import_form, {
 348          onUpload: (0,external_lodash_namespaceObject.flow)(onClose, onUpload)
 349        });
 350      }
 351    });
 352  }
 353  
 354  /* harmony default export */ var import_dropdown = (ImportDropdown);
 355  
 356  ;// CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/index.js
 357  
 358  
 359  /**
 360   * WordPress dependencies
 361   */
 362  
 363  
 364  /**
 365   * Internal dependencies
 366   */
 367  
 368  
 369   // Setup Export Links.
 370  
 371  document.body.addEventListener('click', event => {
 372    if (!event.target.classList.contains('wp-list-reusable-blocks__export')) {
 373      return;
 374    }
 375  
 376    event.preventDefault();
 377    utils_export(event.target.dataset.id);
 378  }); // Setup Import Form.
 379  
 380  document.addEventListener('DOMContentLoaded', () => {
 381    const button = document.querySelector('.page-title-action');
 382  
 383    if (!button) {
 384      return;
 385    }
 386  
 387    const showNotice = () => {
 388      const notice = document.createElement('div');
 389      notice.className = 'notice notice-success is-dismissible';
 390      notice.innerHTML = `<p>${(0,external_wp_i18n_namespaceObject.__)('Reusable block imported successfully!')}</p>`;
 391      const headerEnd = document.querySelector('.wp-header-end');
 392  
 393      if (!headerEnd) {
 394        return;
 395      }
 396  
 397      headerEnd.parentNode.insertBefore(notice, headerEnd);
 398    };
 399  
 400    const container = document.createElement('div');
 401    container.className = 'list-reusable-blocks__container';
 402    button.parentNode.insertBefore(container, button);
 403    (0,external_wp_element_namespaceObject.render)((0,external_wp_element_namespaceObject.createElement)(import_dropdown, {
 404      onUpload: showNotice
 405    }), container);
 406  });
 407  
 408  (window.wp = window.wp || {}).listReusableBlocks = __webpack_exports__;
 409  /******/ })()
 410  ;


Generated: Sun Aug 14 01:00:02 2022 Cross-referenced by PHPXref 0.7.1