[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  (function() {
   2  "use strict";
   3  var wp;
   4  (wp ||= {}).listReusableBlocks = (() => {
   5    var __create = Object.create;
   6    var __defProp = Object.defineProperty;
   7    var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
   8    var __getOwnPropNames = Object.getOwnPropertyNames;
   9    var __getProtoOf = Object.getPrototypeOf;
  10    var __hasOwnProp = Object.prototype.hasOwnProperty;
  11    var __commonJS = (cb, mod) => function __require() {
  12      return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  13    };
  14    var __copyProps = (to, from, except, desc) => {
  15      if (from && typeof from === "object" || typeof from === "function") {
  16        for (let key of __getOwnPropNames(from))
  17          if (!__hasOwnProp.call(to, key) && key !== except)
  18            __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  19      }
  20      return to;
  21    };
  22    var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  23      // If the importer is in node compatibility mode or this is not an ESM
  24      // file that has been converted to a CommonJS file using a Babel-
  25      // compatible transform (i.e. "__esModule" has not been set), then set
  26      // "default" to the CommonJS "module.exports" for node compatibility.
  27      isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  28      mod
  29    ));
  30  
  31    // package-external:@wordpress/element
  32    var require_element = __commonJS({
  33      "package-external:@wordpress/element"(exports, module) {
  34        module.exports = window.wp.element;
  35      }
  36    });
  37  
  38    // package-external:@wordpress/i18n
  39    var require_i18n = __commonJS({
  40      "package-external:@wordpress/i18n"(exports, module) {
  41        module.exports = window.wp.i18n;
  42      }
  43    });
  44  
  45    // package-external:@wordpress/components
  46    var require_components = __commonJS({
  47      "package-external:@wordpress/components"(exports, module) {
  48        module.exports = window.wp.components;
  49      }
  50    });
  51  
  52    // package-external:@wordpress/compose
  53    var require_compose = __commonJS({
  54      "package-external:@wordpress/compose"(exports, module) {
  55        module.exports = window.wp.compose;
  56      }
  57    });
  58  
  59    // package-external:@wordpress/api-fetch
  60    var require_api_fetch = __commonJS({
  61      "package-external:@wordpress/api-fetch"(exports, module) {
  62        module.exports = window.wp.apiFetch;
  63      }
  64    });
  65  
  66    // vendor-external:react/jsx-runtime
  67    var require_jsx_runtime = __commonJS({
  68      "vendor-external:react/jsx-runtime"(exports, module) {
  69        module.exports = window.ReactJSXRuntime;
  70      }
  71    });
  72  
  73    // package-external:@wordpress/blob
  74    var require_blob = __commonJS({
  75      "package-external:@wordpress/blob"(exports, module) {
  76        module.exports = window.wp.blob;
  77      }
  78    });
  79  
  80    // packages/list-reusable-blocks/build-module/index.mjs
  81    var import_element2 = __toESM(require_element(), 1);
  82    var import_i18n3 = __toESM(require_i18n(), 1);
  83  
  84    // packages/list-reusable-blocks/build-module/components/import-dropdown/index.mjs
  85    var import_components2 = __toESM(require_components(), 1);
  86    var import_compose2 = __toESM(require_compose(), 1);
  87    var import_i18n2 = __toESM(require_i18n(), 1);
  88  
  89    // packages/list-reusable-blocks/build-module/components/import-form/index.mjs
  90    var import_components = __toESM(require_components(), 1);
  91    var import_compose = __toESM(require_compose(), 1);
  92    var import_element = __toESM(require_element(), 1);
  93    var import_i18n = __toESM(require_i18n(), 1);
  94  
  95    // packages/list-reusable-blocks/build-module/utils/import.mjs
  96    var import_api_fetch = __toESM(require_api_fetch(), 1);
  97  
  98    // packages/list-reusable-blocks/build-module/utils/file.mjs
  99    function readTextFile(file) {
 100      const reader = new window.FileReader();
 101      return new Promise((resolve) => {
 102        reader.onload = () => {
 103          resolve(reader.result);
 104        };
 105        reader.readAsText(file);
 106      });
 107    }
 108  
 109    // packages/list-reusable-blocks/build-module/utils/import.mjs
 110    async function importReusableBlock(file) {
 111      const fileContent = await readTextFile(file);
 112      let parsedContent;
 113      try {
 114        parsedContent = JSON.parse(fileContent);
 115      } catch {
 116        throw new Error("Invalid JSON file");
 117      }
 118      if (parsedContent.__file !== "wp_block" || !parsedContent.title || !parsedContent.content || typeof parsedContent.title !== "string" || typeof parsedContent.content !== "string" || parsedContent.syncStatus && typeof parsedContent.syncStatus !== "string") {
 119        throw new Error("Invalid pattern JSON file");
 120      }
 121      const postType = await (0, import_api_fetch.default)({
 122        path: `/wp/v2/types/wp_block`
 123      });
 124      const reusableBlock = await (0, import_api_fetch.default)({
 125        path: `/wp/v2/$postType.rest_base}`,
 126        data: {
 127          title: parsedContent.title,
 128          content: parsedContent.content,
 129          status: "publish",
 130          meta: parsedContent.syncStatus === "unsynced" ? { wp_pattern_sync_status: parsedContent.syncStatus } : void 0
 131        },
 132        method: "POST"
 133      });
 134      return reusableBlock;
 135    }
 136    var import_default = importReusableBlock;
 137  
 138    // packages/list-reusable-blocks/build-module/components/import-form/index.mjs
 139    var import_jsx_runtime = __toESM(require_jsx_runtime(), 1);
 140    function ImportForm({ instanceId, onUpload }) {
 141      const inputId = "list-reusable-blocks-import-form-" + instanceId;
 142      const formRef = (0, import_element.useRef)(null);
 143      const [isLoading, setIsLoading] = (0, import_element.useState)(false);
 144      const [error, setError] = (0, import_element.useState)(null);
 145      const [file, setFile] = (0, import_element.useState)(null);
 146      const onChangeFile = (event) => {
 147        setFile(event.target.files?.[0] || null);
 148        setError(null);
 149      };
 150      const onSubmit = (event) => {
 151        event.preventDefault();
 152        if (!file) {
 153          return;
 154        }
 155        setIsLoading(true);
 156        import_default(file).then((reusableBlock) => {
 157          if (!formRef.current) {
 158            return;
 159          }
 160          setIsLoading(false);
 161          onUpload(reusableBlock);
 162        }).catch((errors) => {
 163          if (!formRef.current) {
 164            return;
 165          }
 166          let uiMessage;
 167          switch (errors.message) {
 168            case "Invalid JSON file":
 169              uiMessage = (0, import_i18n.__)("Invalid JSON file");
 170              break;
 171            case "Invalid pattern JSON file":
 172              uiMessage = (0, import_i18n.__)("Invalid pattern JSON file");
 173              break;
 174            default:
 175              uiMessage = (0, import_i18n.__)("Unknown error");
 176          }
 177          setIsLoading(false);
 178          setError(uiMessage);
 179        });
 180      };
 181      const onDismissError = () => {
 182        setError(null);
 183      };
 184      return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
 185        "form",
 186        {
 187          className: "list-reusable-blocks-import-form",
 188          onSubmit,
 189          ref: formRef,
 190          children: [
 191            error && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Notice, { status: "error", onRemove: () => onDismissError(), children: error }),
 192            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
 193              "label",
 194              {
 195                htmlFor: inputId,
 196                className: "list-reusable-blocks-import-form__label",
 197                children: (0, import_i18n.__)("File")
 198              }
 199            ),
 200            /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", { id: inputId, type: "file", onChange: onChangeFile }),
 201            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
 202              import_components.Button,
 203              {
 204                __next40pxDefaultSize: true,
 205                type: "submit",
 206                isBusy: isLoading,
 207                accessibleWhenDisabled: true,
 208                disabled: !file || isLoading,
 209                variant: "secondary",
 210                className: "list-reusable-blocks-import-form__button",
 211                children: (0, import_i18n._x)("Import", "button label")
 212              }
 213            )
 214          ]
 215        }
 216      );
 217    }
 218    var import_form_default = (0, import_compose.withInstanceId)(ImportForm);
 219  
 220    // packages/list-reusable-blocks/build-module/components/import-dropdown/index.mjs
 221    var import_jsx_runtime2 = __toESM(require_jsx_runtime(), 1);
 222    function ImportDropdown({ onUpload }) {
 223      return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
 224        import_components2.Dropdown,
 225        {
 226          popoverProps: { placement: "bottom-start" },
 227          contentClassName: "list-reusable-blocks-import-dropdown__content",
 228          renderToggle: ({ isOpen, onToggle }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
 229            import_components2.Button,
 230            {
 231              size: "compact",
 232              className: "list-reusable-blocks-import-dropdown__button",
 233              "aria-expanded": isOpen,
 234              onClick: onToggle,
 235              variant: "primary",
 236              children: (0, import_i18n2.__)("Import from JSON")
 237            }
 238          ),
 239          renderContent: ({ onClose }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_form_default, { onUpload: (0, import_compose2.pipe)(onClose, onUpload) })
 240        }
 241      );
 242    }
 243    var import_dropdown_default = ImportDropdown;
 244  
 245    // node_modules/tslib/tslib.es6.mjs
 246    var __assign = function() {
 247      __assign = Object.assign || function __assign2(t) {
 248        for (var s, i = 1, n = arguments.length; i < n; i++) {
 249          s = arguments[i];
 250          for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
 251        }
 252        return t;
 253      };
 254      return __assign.apply(this, arguments);
 255    };
 256  
 257    // node_modules/lower-case/dist.es2015/index.js
 258    function lowerCase(str) {
 259      return str.toLowerCase();
 260    }
 261  
 262    // node_modules/no-case/dist.es2015/index.js
 263    var DEFAULT_SPLIT_REGEXP = [/([a-z0-9])([A-Z])/g, /([A-Z])([A-Z][a-z])/g];
 264    var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
 265    function noCase(input, options) {
 266      if (options === void 0) {
 267        options = {};
 268      }
 269      var _a = options.splitRegexp, splitRegexp = _a === void 0 ? DEFAULT_SPLIT_REGEXP : _a, _b = options.stripRegexp, stripRegexp = _b === void 0 ? DEFAULT_STRIP_REGEXP : _b, _c = options.transform, transform = _c === void 0 ? lowerCase : _c, _d = options.delimiter, delimiter = _d === void 0 ? " " : _d;
 270      var result = replace(replace(input, splitRegexp, "$1\0$2"), stripRegexp, "\0");
 271      var start = 0;
 272      var end = result.length;
 273      while (result.charAt(start) === "\0")
 274        start++;
 275      while (result.charAt(end - 1) === "\0")
 276        end--;
 277      return result.slice(start, end).split("\0").map(transform).join(delimiter);
 278    }
 279    function replace(input, re, value) {
 280      if (re instanceof RegExp)
 281        return input.replace(re, value);
 282      return re.reduce(function(input2, re2) {
 283        return input2.replace(re2, value);
 284      }, input);
 285    }
 286  
 287    // node_modules/dot-case/dist.es2015/index.js
 288    function dotCase(input, options) {
 289      if (options === void 0) {
 290        options = {};
 291      }
 292      return noCase(input, __assign({ delimiter: "." }, options));
 293    }
 294  
 295    // node_modules/param-case/dist.es2015/index.js
 296    function paramCase(input, options) {
 297      if (options === void 0) {
 298        options = {};
 299      }
 300      return dotCase(input, __assign({ delimiter: "-" }, options));
 301    }
 302  
 303    // packages/list-reusable-blocks/build-module/utils/export.mjs
 304    var import_api_fetch2 = __toESM(require_api_fetch(), 1);
 305    var import_blob = __toESM(require_blob(), 1);
 306    async function exportReusableBlock(id) {
 307      const postType = await (0, import_api_fetch2.default)({
 308        path: `/wp/v2/types/wp_block`
 309      });
 310      const post = await (0, import_api_fetch2.default)({
 311        path: `/wp/v2/$postType.rest_base}/$id}?context=edit`
 312      });
 313      const title = post.title.raw;
 314      const content = post.content.raw;
 315      const syncStatus = post.wp_pattern_sync_status;
 316      const fileContent = JSON.stringify(
 317        {
 318          __file: "wp_block",
 319          title,
 320          content,
 321          syncStatus
 322        },
 323        null,
 324        2
 325      );
 326      const fileName = paramCase(title) + ".json";
 327      (0, import_blob.downloadBlob)(fileName, fileContent, "application/json");
 328    }
 329    var export_default = exportReusableBlock;
 330  
 331    // packages/list-reusable-blocks/build-module/index.mjs
 332    var import_jsx_runtime3 = __toESM(require_jsx_runtime(), 1);
 333    document.body.addEventListener("click", (event) => {
 334      const target = event.target;
 335      if (!target.classList.contains("wp-list-reusable-blocks__export")) {
 336        return;
 337      }
 338      event.preventDefault();
 339      const blockId = target.dataset.id;
 340      if (blockId) {
 341        export_default(Number(blockId));
 342      }
 343    });
 344    document.addEventListener("DOMContentLoaded", () => {
 345      const button = document.querySelector(".page-title-action");
 346      if (!button) {
 347        return;
 348      }
 349      const showNotice = () => {
 350        const notice = document.createElement("div");
 351        notice.className = "notice notice-success is-dismissible";
 352        notice.innerHTML = `<p>${(0, import_i18n3.__)("Pattern imported successfully!")}</p>`;
 353        const headerEnd = document.querySelector(".wp-header-end");
 354        if (!headerEnd || !headerEnd.parentNode) {
 355          return;
 356        }
 357        headerEnd.parentNode.insertBefore(notice, headerEnd);
 358      };
 359      const container = document.createElement("div");
 360      container.className = "list-reusable-blocks__container";
 361      if (!button.parentNode) {
 362        return;
 363      }
 364      button.parentNode.insertBefore(container, button);
 365      (0, import_element2.createRoot)(container).render(
 366        /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_element2.StrictMode, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_dropdown_default, { onUpload: showNotice }) })
 367      );
 368    });
 369  })();
 370  (window.wp ||= {}).listReusableBlocks = wp.listReusableBlocks;
 371  })();


Generated : Wed Aug 12 08:20:21 2026 Cross-referenced by PHPXref