@wordpress/block-library
Version:
Block library for the WordPress editor.
94 lines (93 loc) • 3.73 kB
JavaScript
import { __, _x, sprintf } from "@wordpress/i18n";
import { useMemo, useState } from "@wordpress/element";
import { useDispatch, useRegistry, useSelect } from "@wordpress/data";
import { Button, FlexBlock, FlexItem, __experimentalHStack, __experimentalSpacer } from "@wordpress/components";
import { SelectControl } from "@wordpress/ui";
import { store } from "@wordpress/core-data";
import { store as store$1 } from "@wordpress/notices";
import { useCreateTemplatePartFromBlocks } from "./utils/hooks.mjs";
import { transformWidgetToBlock } from "./utils/transformers.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/template-part/edit/import-controls.jsx
const SIDEBARS_QUERY = {
per_page: -1,
_fields: "id,name,description,status,widgets"
};
function TemplatePartImportControls({ area, setAttributes }) {
const [selectedSidebar, setSelectedSidebar] = useState("");
const [isBusy, setIsBusy] = useState(false);
const registry = useRegistry();
const { sidebars, hasResolved } = useSelect((select) => {
const { getSidebars, hasFinishedResolution } = select(store);
return {
sidebars: getSidebars(SIDEBARS_QUERY),
hasResolved: hasFinishedResolution("getSidebars", [SIDEBARS_QUERY])
};
}, []);
const { createErrorNotice } = useDispatch(store$1);
const createFromBlocks = useCreateTemplatePartFromBlocks(area, setAttributes);
const options = 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: null,
label: __("Select widget area")
}, ...sidebarOptions];
}, [sidebars]);
if (!hasResolved) return /* @__PURE__ */ jsx(__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(store);
const widgets = await getWidgets({
sidebar: sidebar.value,
_embed: "about"
});
const skippedWidgets = /* @__PURE__ */ new Set();
const blocks = widgets.flatMap((widget) => {
const block = transformWidgetToBlock(widget);
if (!block) {
skippedWidgets.add(widget.id_base);
return [];
}
return block;
});
await createFromBlocks(blocks, sprintf(__("Widget area: %s"), sidebar.label));
if (skippedWidgets.size) createErrorNotice(sprintf(__("Unable to import the following widgets: %s."), Array.from(skippedWidgets).join(", ")), { type: "snackbar" });
setIsBusy(false);
}
return /* @__PURE__ */ jsx(__experimentalSpacer, {
marginBottom: "4",
children: /* @__PURE__ */ jsxs(__experimentalHStack, {
as: "form",
onSubmit: createFromWidgets,
children: [/* @__PURE__ */ jsx(FlexBlock, { children: /* @__PURE__ */ jsx(SelectControl, {
label: __("Import widget area"),
value: options.find((item) => item.value === (selectedSidebar || null)) ?? null,
items: options,
onValueChange: (item) => setSelectedSidebar(item?.value ?? ""),
disabled: !options.length
}) }), /* @__PURE__ */ jsx(FlexItem, {
style: { marginTop: "auto" },
children: /* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
variant: "primary",
type: "submit",
isBusy,
"aria-disabled": isBusy || !selectedSidebar,
children: _x("Import", "button label")
})
})]
})
});
}
export { TemplatePartImportControls };
//# sourceMappingURL=import-controls.mjs.map