UNPKG

@wordpress/block-library

Version:
162 lines (160 loc) 5.87 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-library/src/template-part/edit/import-controls.js var import_controls_exports = {}; __export(import_controls_exports, { TemplatePartImportControls: () => TemplatePartImportControls }); module.exports = __toCommonJS(import_controls_exports); var import_i18n = require("@wordpress/i18n"); var import_element = require("@wordpress/element"); var import_data = require("@wordpress/data"); var import_components = require("@wordpress/components"); var import_core_data = require("@wordpress/core-data"); var import_notices = require("@wordpress/notices"); var import_hooks = require("./utils/hooks"); var import_transformers = require("./utils/transformers"); var import_jsx_runtime = require("react/jsx-runtime"); var SIDEBARS_QUERY = { per_page: -1, _fields: "id,name,description,status,widgets" }; function TemplatePartImportControls({ area, setAttributes }) { const [selectedSidebar, setSelectedSidebar] = (0, import_element.useState)(""); const [isBusy, setIsBusy] = (0, import_element.useState)(false); const registry = (0, import_data.useRegistry)(); const { sidebars, hasResolved } = (0, import_data.useSelect)((select) => { const { getSidebars, hasFinishedResolution } = select(import_core_data.store); return { sidebars: getSidebars(SIDEBARS_QUERY), hasResolved: hasFinishedResolution("getSidebars", [ SIDEBARS_QUERY ]) }; }, []); const { createErrorNotice } = (0, import_data.useDispatch)(import_notices.store); const createFromBlocks = (0, import_hooks.useCreateTemplatePartFromBlocks)( area, setAttributes ); const options = (0, import_element.useMemo)(() => { const sidebarOptions = (sidebars ?? []).filter( (widgetArea) => widgetArea.id !== "wp_inactive_widgets" && widgetArea.widgets.length > 0 ).map((widgetArea) => { return { value: widgetArea.id, label: widgetArea.name }; }); if (!sidebarOptions.length) { return []; } return [ { value: "", label: (0, import_i18n.__)("Select widget area") }, ...sidebarOptions ]; }, [sidebars]); if (!hasResolved) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.__experimentalSpacer, { marginBottom: "0" }); } if (hasResolved && !options.length) { return null; } async function createFromWidgets(event) { event.preventDefault(); if (isBusy || !selectedSidebar) { return; } setIsBusy(true); const sidebar = options.find( ({ value }) => value === selectedSidebar ); const { getWidgets } = registry.resolveSelect(import_core_data.store); const widgets = await getWidgets({ sidebar: sidebar.value, _embed: "about" }); const skippedWidgets = /* @__PURE__ */ new Set(); const blocks = widgets.flatMap((widget) => { const block = (0, import_transformers.transformWidgetToBlock)(widget); if (!block) { skippedWidgets.add(widget.id_base); return []; } return block; }); await createFromBlocks( blocks, /* translators: %s: name of the widget area */ (0, import_i18n.sprintf)((0, import_i18n.__)("Widget area: %s"), sidebar.label) ); if (skippedWidgets.size) { createErrorNotice( (0, import_i18n.sprintf)( /* translators: %s: the list of widgets */ (0, import_i18n.__)("Unable to import the following widgets: %s."), Array.from(skippedWidgets).join(", ") ), { type: "snackbar" } ); } setIsBusy(false); } return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.__experimentalSpacer, { marginBottom: "4", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalHStack, { as: "form", onSubmit: createFromWidgets, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.FlexBlock, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.SelectControl, { label: (0, import_i18n.__)("Import widget area"), value: selectedSidebar, options, onChange: (value) => setSelectedSidebar(value), disabled: !options.length, __next40pxDefaultSize: true, __nextHasNoMarginBottom: true } ) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.FlexItem, { style: { marginBottom: "8px", marginTop: "auto" }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.Button, { __next40pxDefaultSize: true, variant: "primary", type: "submit", isBusy, "aria-disabled": isBusy || !selectedSidebar, children: (0, import_i18n._x)("Import", "button label") } ) } ) ] }) }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { TemplatePartImportControls }); //# sourceMappingURL=import-controls.js.map