[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  /******/ (function() { // webpackBootstrap
   2  /******/     "use strict";
   3  /******/     // The require scope
   4  /******/     var __webpack_require__ = {};
   5  /******/     
   6  /************************************************************************/
   7  /******/     /* webpack/runtime/define property getters */
   8  /******/     !function() {
   9  /******/         // define getter functions for harmony exports
  10  /******/         __webpack_require__.d = function(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  /******/     !function() {
  21  /******/         __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  22  /******/     }();
  23  /******/     
  24  /******/     /* webpack/runtime/make namespace object */
  25  /******/     !function() {
  26  /******/         // define __esModule on exports
  27  /******/         __webpack_require__.r = function(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  // ESM COMPAT FLAG
  38  __webpack_require__.r(__webpack_exports__);
  39  
  40  // EXPORTS
  41  __webpack_require__.d(__webpack_exports__, {
  42    "ReusableBlocksMenuItems": function() { return /* reexport */ reusable_blocks_menu_items; },
  43    "store": function() { return /* reexport */ store; }
  44  });
  45  
  46  // NAMESPACE OBJECT: ./node_modules/@wordpress/reusable-blocks/build-module/store/actions.js
  47  var actions_namespaceObject = {};
  48  __webpack_require__.r(actions_namespaceObject);
  49  __webpack_require__.d(actions_namespaceObject, {
  50    "__experimentalConvertBlockToStatic": function() { return __experimentalConvertBlockToStatic; },
  51    "__experimentalConvertBlocksToReusable": function() { return __experimentalConvertBlocksToReusable; },
  52    "__experimentalDeleteReusableBlock": function() { return __experimentalDeleteReusableBlock; },
  53    "__experimentalSetEditingReusableBlock": function() { return __experimentalSetEditingReusableBlock; }
  54  });
  55  
  56  // NAMESPACE OBJECT: ./node_modules/@wordpress/reusable-blocks/build-module/store/selectors.js
  57  var selectors_namespaceObject = {};
  58  __webpack_require__.r(selectors_namespaceObject);
  59  __webpack_require__.d(selectors_namespaceObject, {
  60    "__experimentalIsEditingReusableBlock": function() { return __experimentalIsEditingReusableBlock; }
  61  });
  62  
  63  ;// CONCATENATED MODULE: external ["wp","data"]
  64  var external_wp_data_namespaceObject = window["wp"]["data"];
  65  ;// CONCATENATED MODULE: external "lodash"
  66  var external_lodash_namespaceObject = window["lodash"];
  67  ;// CONCATENATED MODULE: external ["wp","blockEditor"]
  68  var external_wp_blockEditor_namespaceObject = window["wp"]["blockEditor"];
  69  ;// CONCATENATED MODULE: external ["wp","blocks"]
  70  var external_wp_blocks_namespaceObject = window["wp"]["blocks"];
  71  ;// CONCATENATED MODULE: external ["wp","i18n"]
  72  var external_wp_i18n_namespaceObject = window["wp"]["i18n"];
  73  ;// CONCATENATED MODULE: ./node_modules/@wordpress/reusable-blocks/build-module/store/actions.js
  74  /**
  75   * External dependencies
  76   */
  77  
  78  /**
  79   * WordPress dependencies
  80   */
  81  
  82  
  83  
  84  
  85  /**
  86   * Returns a generator converting a reusable block into a static block.
  87   *
  88   * @param {string} clientId The client ID of the block to attach.
  89   */
  90  
  91  const __experimentalConvertBlockToStatic = clientId => _ref => {
  92    let {
  93      registry
  94    } = _ref;
  95    const oldBlock = registry.select(external_wp_blockEditor_namespaceObject.store).getBlock(clientId);
  96    const reusableBlock = registry.select('core').getEditedEntityRecord('postType', 'wp_block', oldBlock.attributes.ref);
  97    const newBlocks = (0,external_wp_blocks_namespaceObject.parse)((0,external_lodash_namespaceObject.isFunction)(reusableBlock.content) ? reusableBlock.content(reusableBlock) : reusableBlock.content);
  98    registry.dispatch(external_wp_blockEditor_namespaceObject.store).replaceBlocks(oldBlock.clientId, newBlocks);
  99  };
 100  /**
 101   * Returns a generator converting one or more static blocks into a reusable block.
 102   *
 103   * @param {string[]} clientIds The client IDs of the block to detach.
 104   * @param {string}   title     Reusable block title.
 105   */
 106  
 107  const __experimentalConvertBlocksToReusable = (clientIds, title) => async _ref2 => {
 108    let {
 109      registry,
 110      dispatch
 111    } = _ref2;
 112    const reusableBlock = {
 113      title: title || (0,external_wp_i18n_namespaceObject.__)('Untitled Reusable block'),
 114      content: (0,external_wp_blocks_namespaceObject.serialize)(registry.select(external_wp_blockEditor_namespaceObject.store).getBlocksByClientId(clientIds)),
 115      status: 'publish'
 116    };
 117    const updatedRecord = await registry.dispatch('core').saveEntityRecord('postType', 'wp_block', reusableBlock);
 118    const newBlock = (0,external_wp_blocks_namespaceObject.createBlock)('core/block', {
 119      ref: updatedRecord.id
 120    });
 121    registry.dispatch(external_wp_blockEditor_namespaceObject.store).replaceBlocks(clientIds, newBlock);
 122  
 123    dispatch.__experimentalSetEditingReusableBlock(newBlock.clientId, true);
 124  };
 125  /**
 126   * Returns a generator deleting a reusable block.
 127   *
 128   * @param {string} id The ID of the reusable block to delete.
 129   */
 130  
 131  const __experimentalDeleteReusableBlock = id => async _ref3 => {
 132    let {
 133      registry
 134    } = _ref3;
 135    const reusableBlock = registry.select('core').getEditedEntityRecord('postType', 'wp_block', id); // Don't allow a reusable block with a temporary ID to be deleted.
 136  
 137    if (!reusableBlock) {
 138      return;
 139    } // Remove any other blocks that reference this reusable block.
 140  
 141  
 142    const allBlocks = registry.select(external_wp_blockEditor_namespaceObject.store).getBlocks();
 143    const associatedBlocks = allBlocks.filter(block => (0,external_wp_blocks_namespaceObject.isReusableBlock)(block) && block.attributes.ref === id);
 144    const associatedBlockClientIds = associatedBlocks.map(block => block.clientId); // Remove the parsed block.
 145  
 146    if (associatedBlockClientIds.length) {
 147      registry.dispatch(external_wp_blockEditor_namespaceObject.store).removeBlocks(associatedBlockClientIds);
 148    }
 149  
 150    await registry.dispatch('core').deleteEntityRecord('postType', 'wp_block', id);
 151  };
 152  /**
 153   * Returns an action descriptor for SET_EDITING_REUSABLE_BLOCK action.
 154   *
 155   * @param {string}  clientId  The clientID of the reusable block to target.
 156   * @param {boolean} isEditing Whether the block should be in editing state.
 157   * @return {Object} Action descriptor.
 158   */
 159  
 160  function __experimentalSetEditingReusableBlock(clientId, isEditing) {
 161    return {
 162      type: 'SET_EDITING_REUSABLE_BLOCK',
 163      clientId,
 164      isEditing
 165    };
 166  }
 167  
 168  ;// CONCATENATED MODULE: ./node_modules/@wordpress/reusable-blocks/build-module/store/reducer.js
 169  /**
 170   * WordPress dependencies
 171   */
 172  
 173  function isEditingReusableBlock() {
 174    let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
 175    let action = arguments.length > 1 ? arguments[1] : undefined;
 176  
 177    if ((action === null || action === void 0 ? void 0 : action.type) === 'SET_EDITING_REUSABLE_BLOCK') {
 178      return { ...state,
 179        [action.clientId]: action.isEditing
 180      };
 181    }
 182  
 183    return state;
 184  }
 185  /* harmony default export */ var reducer = ((0,external_wp_data_namespaceObject.combineReducers)({
 186    isEditingReusableBlock
 187  }));
 188  
 189  ;// CONCATENATED MODULE: ./node_modules/@wordpress/reusable-blocks/build-module/store/selectors.js
 190  /**
 191   * Returns true if reusable block is in the editing state.
 192   *
 193   * @param {Object} state    Global application state.
 194   * @param {number} clientId the clientID of the block.
 195   * @return {boolean} Whether the reusable block is in the editing state.
 196   */
 197  function __experimentalIsEditingReusableBlock(state, clientId) {
 198    return state.isEditingReusableBlock[clientId];
 199  }
 200  
 201  ;// CONCATENATED MODULE: ./node_modules/@wordpress/reusable-blocks/build-module/store/index.js
 202  /**
 203   * WordPress dependencies
 204   */
 205  
 206  /**
 207   * Internal dependencies
 208   */
 209  
 210  
 211  
 212  
 213  const STORE_NAME = 'core/reusable-blocks';
 214  /**
 215   * Store definition for the reusable blocks namespace.
 216   *
 217   * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
 218   *
 219   * @type {Object}
 220   */
 221  
 222  const store = (0,external_wp_data_namespaceObject.createReduxStore)(STORE_NAME, {
 223    actions: actions_namespaceObject,
 224    reducer: reducer,
 225    selectors: selectors_namespaceObject
 226  });
 227  (0,external_wp_data_namespaceObject.register)(store);
 228  
 229  ;// CONCATENATED MODULE: external ["wp","element"]
 230  var external_wp_element_namespaceObject = window["wp"]["element"];
 231  ;// CONCATENATED MODULE: external ["wp","components"]
 232  var external_wp_components_namespaceObject = window["wp"]["components"];
 233  ;// CONCATENATED MODULE: external ["wp","primitives"]
 234  var external_wp_primitives_namespaceObject = window["wp"]["primitives"];
 235  ;// CONCATENATED MODULE: ./node_modules/@wordpress/icons/build-module/library/symbol.js
 236  
 237  
 238  /**
 239   * WordPress dependencies
 240   */
 241  
 242  const symbol = (0,external_wp_element_namespaceObject.createElement)(external_wp_primitives_namespaceObject.SVG, {
 243    xmlns: "http://www.w3.org/2000/svg",
 244    viewBox: "0 0 24 24"
 245  }, (0,external_wp_element_namespaceObject.createElement)(external_wp_primitives_namespaceObject.Path, {
 246    d: "M21.3 10.8l-5.6-5.6c-.7-.7-1.8-.7-2.5 0l-5.6 5.6c-.7.7-.7 1.8 0 2.5l5.6 5.6c.3.3.8.5 1.2.5s.9-.2 1.2-.5l5.6-5.6c.8-.7.8-1.9.1-2.5zm-1 1.4l-5.6 5.6c-.1.1-.3.1-.4 0l-5.6-5.6c-.1-.1-.1-.3 0-.4l5.6-5.6s.1-.1.2-.1.1 0 .2.1l5.6 5.6c.1.1.1.3 0 .4zm-16.6-.4L10 5.5l-1-1-6.3 6.3c-.7.7-.7 1.8 0 2.5L9 19.5l1.1-1.1-6.3-6.3c-.2 0-.2-.2-.1-.3z"
 247  }));
 248  /* harmony default export */ var library_symbol = (symbol);
 249  
 250  ;// CONCATENATED MODULE: external ["wp","notices"]
 251  var external_wp_notices_namespaceObject = window["wp"]["notices"];
 252  ;// CONCATENATED MODULE: external ["wp","coreData"]
 253  var external_wp_coreData_namespaceObject = window["wp"]["coreData"];
 254  ;// CONCATENATED MODULE: ./node_modules/@wordpress/reusable-blocks/build-module/components/reusable-blocks-menu-items/reusable-block-convert-button.js
 255  
 256  
 257  /**
 258   * WordPress dependencies
 259   */
 260  
 261  
 262  
 263  
 264  
 265  
 266  
 267  
 268  
 269  /**
 270   * Internal dependencies
 271   */
 272  
 273  
 274  /**
 275   * Menu control to convert block(s) to reusable block.
 276   *
 277   * @param {Object}   props              Component props.
 278   * @param {string[]} props.clientIds    Client ids of selected blocks.
 279   * @param {string}   props.rootClientId ID of the currently selected top-level block.
 280   * @return {import('@wordpress/element').WPComponent} The menu control or null.
 281   */
 282  
 283  function ReusableBlockConvertButton(_ref) {
 284    let {
 285      clientIds,
 286      rootClientId
 287    } = _ref;
 288    const [isModalOpen, setIsModalOpen] = (0,external_wp_element_namespaceObject.useState)(false);
 289    const [title, setTitle] = (0,external_wp_element_namespaceObject.useState)('');
 290    const canConvert = (0,external_wp_data_namespaceObject.useSelect)(select => {
 291      var _getBlocksByClientId;
 292  
 293      const {
 294        canUser
 295      } = select(external_wp_coreData_namespaceObject.store);
 296      const {
 297        getBlocksByClientId,
 298        canInsertBlockType
 299      } = select(external_wp_blockEditor_namespaceObject.store);
 300      const blocks = (_getBlocksByClientId = getBlocksByClientId(clientIds)) !== null && _getBlocksByClientId !== void 0 ? _getBlocksByClientId : [];
 301      const isReusable = blocks.length === 1 && blocks[0] && (0,external_wp_blocks_namespaceObject.isReusableBlock)(blocks[0]) && !!select(external_wp_coreData_namespaceObject.store).getEntityRecord('postType', 'wp_block', blocks[0].attributes.ref);
 302  
 303      const _canConvert = // Hide when this is already a reusable block.
 304      !isReusable && // Hide when reusable blocks are disabled.
 305      canInsertBlockType('core/block', rootClientId) && blocks.every(block => // Guard against the case where a regular block has *just* been converted.
 306      !!block && // Hide on invalid blocks.
 307      block.isValid && // Hide when block doesn't support being made reusable.
 308      (0,external_wp_blocks_namespaceObject.hasBlockSupport)(block.name, 'reusable', true)) && // Hide when current doesn't have permission to do that.
 309      !!canUser('create', 'blocks');
 310  
 311      return _canConvert;
 312    }, [clientIds]);
 313    const {
 314      __experimentalConvertBlocksToReusable: convertBlocksToReusable
 315    } = (0,external_wp_data_namespaceObject.useDispatch)(store);
 316    const {
 317      createSuccessNotice,
 318      createErrorNotice
 319    } = (0,external_wp_data_namespaceObject.useDispatch)(external_wp_notices_namespaceObject.store);
 320    const onConvert = (0,external_wp_element_namespaceObject.useCallback)(async function (reusableBlockTitle) {
 321      try {
 322        await convertBlocksToReusable(clientIds, reusableBlockTitle);
 323        createSuccessNotice((0,external_wp_i18n_namespaceObject.__)('Reusable block created.'), {
 324          type: 'snackbar'
 325        });
 326      } catch (error) {
 327        createErrorNotice(error.message, {
 328          type: 'snackbar'
 329        });
 330      }
 331    }, [clientIds]);
 332  
 333    if (!canConvert) {
 334      return null;
 335    }
 336  
 337    return (0,external_wp_element_namespaceObject.createElement)(external_wp_blockEditor_namespaceObject.BlockSettingsMenuControls, null, _ref2 => {
 338      let {
 339        onClose
 340      } = _ref2;
 341      return (0,external_wp_element_namespaceObject.createElement)(external_wp_element_namespaceObject.Fragment, null, (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.MenuItem, {
 342        icon: library_symbol,
 343        onClick: () => {
 344          setIsModalOpen(true);
 345        }
 346      }, (0,external_wp_i18n_namespaceObject.__)('Add to Reusable blocks')), isModalOpen && (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.Modal, {
 347        title: (0,external_wp_i18n_namespaceObject.__)('Create Reusable block'),
 348        closeLabel: (0,external_wp_i18n_namespaceObject.__)('Close'),
 349        onRequestClose: () => {
 350          setIsModalOpen(false);
 351          setTitle('');
 352        },
 353        overlayClassName: "reusable-blocks-menu-items__convert-modal"
 354      }, (0,external_wp_element_namespaceObject.createElement)("form", {
 355        onSubmit: event => {
 356          event.preventDefault();
 357          onConvert(title);
 358          setIsModalOpen(false);
 359          setTitle('');
 360          onClose();
 361        }
 362      }, (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.TextControl, {
 363        label: (0,external_wp_i18n_namespaceObject.__)('Name'),
 364        value: title,
 365        onChange: setTitle
 366      }), (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.Flex, {
 367        className: "reusable-blocks-menu-items__convert-modal-actions",
 368        justify: "flex-end"
 369      }, (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.FlexItem, null, (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.Button, {
 370        variant: "tertiary",
 371        onClick: () => {
 372          setIsModalOpen(false);
 373          setTitle('');
 374        }
 375      }, (0,external_wp_i18n_namespaceObject.__)('Cancel'))), (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.FlexItem, null, (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.Button, {
 376        variant: "primary",
 377        type: "submit"
 378      }, (0,external_wp_i18n_namespaceObject.__)('Save')))))));
 379    });
 380  }
 381  
 382  ;// CONCATENATED MODULE: external ["wp","url"]
 383  var external_wp_url_namespaceObject = window["wp"]["url"];
 384  ;// CONCATENATED MODULE: ./node_modules/@wordpress/reusable-blocks/build-module/components/reusable-blocks-menu-items/reusable-blocks-manage-button.js
 385  
 386  
 387  /**
 388   * WordPress dependencies
 389   */
 390  
 391  
 392  
 393  
 394  
 395  
 396  
 397  /**
 398   * Internal dependencies
 399   */
 400  
 401  
 402  
 403  function ReusableBlocksManageButton(_ref) {
 404    let {
 405      clientId
 406    } = _ref;
 407    const {
 408      canRemove,
 409      isVisible
 410    } = (0,external_wp_data_namespaceObject.useSelect)(select => {
 411      const {
 412        getBlock,
 413        canRemoveBlock
 414      } = select(external_wp_blockEditor_namespaceObject.store);
 415      const {
 416        canUser
 417      } = select(external_wp_coreData_namespaceObject.store);
 418      const reusableBlock = getBlock(clientId);
 419      return {
 420        canRemove: canRemoveBlock(clientId),
 421        isVisible: !!reusableBlock && (0,external_wp_blocks_namespaceObject.isReusableBlock)(reusableBlock) && !!canUser('update', 'blocks', reusableBlock.attributes.ref)
 422      };
 423    }, [clientId]);
 424    const {
 425      __experimentalConvertBlockToStatic: convertBlockToStatic
 426    } = (0,external_wp_data_namespaceObject.useDispatch)(store);
 427  
 428    if (!isVisible) {
 429      return null;
 430    }
 431  
 432    return (0,external_wp_element_namespaceObject.createElement)(external_wp_blockEditor_namespaceObject.BlockSettingsMenuControls, null, (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.MenuItem, {
 433      href: (0,external_wp_url_namespaceObject.addQueryArgs)('edit.php', {
 434        post_type: 'wp_block'
 435      })
 436    }, (0,external_wp_i18n_namespaceObject.__)('Manage Reusable blocks')), canRemove && (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.MenuItem, {
 437      onClick: () => convertBlockToStatic(clientId)
 438    }, (0,external_wp_i18n_namespaceObject.__)('Convert to regular blocks')));
 439  }
 440  
 441  /* harmony default export */ var reusable_blocks_manage_button = (ReusableBlocksManageButton);
 442  
 443  ;// CONCATENATED MODULE: ./node_modules/@wordpress/reusable-blocks/build-module/components/reusable-blocks-menu-items/index.js
 444  
 445  
 446  /**
 447   * WordPress dependencies
 448   */
 449  
 450  
 451  /**
 452   * Internal dependencies
 453   */
 454  
 455  
 456  
 457  
 458  function ReusableBlocksMenuItems(_ref) {
 459    let {
 460      clientIds,
 461      rootClientId
 462    } = _ref;
 463    return (0,external_wp_element_namespaceObject.createElement)(external_wp_element_namespaceObject.Fragment, null, (0,external_wp_element_namespaceObject.createElement)(ReusableBlockConvertButton, {
 464      clientIds: clientIds,
 465      rootClientId: rootClientId
 466    }), clientIds.length === 1 && (0,external_wp_element_namespaceObject.createElement)(reusable_blocks_manage_button, {
 467      clientId: clientIds[0]
 468    }));
 469  }
 470  
 471  /* harmony default export */ var reusable_blocks_menu_items = ((0,external_wp_data_namespaceObject.withSelect)(select => {
 472    const {
 473      getSelectedBlockClientIds
 474    } = select(external_wp_blockEditor_namespaceObject.store);
 475    return {
 476      clientIds: getSelectedBlockClientIds()
 477    };
 478  })(ReusableBlocksMenuItems));
 479  
 480  ;// CONCATENATED MODULE: ./node_modules/@wordpress/reusable-blocks/build-module/components/index.js
 481  
 482  
 483  ;// CONCATENATED MODULE: ./node_modules/@wordpress/reusable-blocks/build-module/index.js
 484  
 485  
 486  
 487  (window.wp = window.wp || {}).reusableBlocks = __webpack_exports__;
 488  /******/ })()
 489  ;


Generated: Sat Aug 13 01:00:02 2022 Cross-referenced by PHPXref 0.7.1