UNPKG

@wordpress/editor

Version:
72 lines (71 loc) 2.49 kB
// packages/editor/src/components/site-export/index.js import { __, _x } from "@wordpress/i18n"; import { MenuItem } from "@wordpress/components"; import apiFetch from "@wordpress/api-fetch"; import { download } from "@wordpress/icons"; import { useDispatch, useSelect } from "@wordpress/data"; import { downloadBlob } from "@wordpress/blob"; import { store as coreStore } from "@wordpress/core-data"; import { store as noticesStore } from "@wordpress/notices"; import { store as editorStore } from "../../store/index.mjs"; import { TEMPLATE_POST_TYPE, TEMPLATE_PART_POST_TYPE } from "../../store/constants.mjs"; import { jsx } from "react/jsx-runtime"; function SiteExport() { const canExport = useSelect((select) => { const postType = select(editorStore).getCurrentPostType(); if (postType !== TEMPLATE_POST_TYPE && postType !== TEMPLATE_PART_POST_TYPE) { return false; } const targetHints = select(coreStore).getCurrentTheme()?._links?.["wp:export-theme"]?.[0]?.targetHints ?? {}; return !!targetHints.allow?.includes("GET"); }, []); const { createErrorNotice } = useDispatch(noticesStore); if (!canExport) { return null; } async function handleExport() { try { const response = await apiFetch({ path: "/wp-block-editor/v1/export", parse: false, headers: { Accept: "application/zip" } }); const blob = await response.blob(); const contentDisposition = response.headers.get( "content-disposition" ); const contentDispositionMatches = contentDisposition.match(/=(.+)\.zip/); const fileName = contentDispositionMatches[1] ? contentDispositionMatches[1] : "edit-site-export"; downloadBlob(fileName + ".zip", blob, "application/zip"); } catch (errorResponse) { let error = {}; try { error = await errorResponse.json(); } catch { } const errorMessage = error.message && error.code !== "unknown_error" ? error.message : __("An error occurred while creating the site export."); createErrorNotice(errorMessage, { type: "snackbar" }); } } return /* @__PURE__ */ jsx( MenuItem, { role: "menuitem", icon: download, onClick: handleExport, info: __( "Download your theme with updated templates and styles." ), children: _x("Export", "site exporter menu item") } ); } export { SiteExport as default }; //# sourceMappingURL=index.mjs.map