@wordpress/block-library
Version:
Block library for the WordPress editor.
165 lines (164 loc) • 6.16 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { useState } from "@wordpress/element";
import { useSelect } from "@wordpress/data";
import { Button, Flex, Modal, Notice, __experimentalHStack, __experimentalVStack, privateApis } from "@wordpress/components";
import { PlainText, privateApis as privateApis$1, store } from "@wordpress/block-editor";
import { fullscreen, square } from "@wordpress/icons";
import { useViewportMatch } from "@wordpress/compose";
import { unlock } from "../lock-unlock.mjs";
import Preview from "./preview.mjs";
import { parseContent, serializeContent } from "./utils.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/html/modal.jsx
const { Tabs: WCTabs } = unlock(privateApis);
const { useNativeUndo } = unlock(privateApis$1);
function HTMLEditModal({ onRequestClose, content, onUpdate }) {
const { html, css, js } = parseContent(content);
const [editedHtml, setEditedHtml] = useState(html);
const [editedCss, setEditedCss] = useState(css);
const [editedJs, setEditedJs] = useState(js);
const [isFullscreen, setIsFullscreen] = useState(false);
const nativeUndoRef = useNativeUndo();
const isMobileViewport = useViewportMatch("small", "<");
const { canUserUseUnfilteredHTML } = useSelect((select) => {
return { canUserUseUnfilteredHTML: select(store).getSettings().__experimentalCanUserUseUnfilteredHTML };
}, []);
const hasRestrictedContent = !canUserUseUnfilteredHTML && (css.trim() || js.trim());
const handleUpdate = () => {
onUpdate(serializeContent({
html: editedHtml,
css: canUserUseUnfilteredHTML ? editedCss : "",
js: canUserUseUnfilteredHTML ? editedJs : ""
}));
};
const handleUpdateAndClose = () => {
handleUpdate();
onRequestClose();
};
const toggleFullscreen = () => {
setIsFullscreen((prevState) => !prevState);
};
return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(Modal, {
title: __("Edit HTML"),
onRequestClose,
className: "block-library-html__modal",
size: "large",
isDismissible: false,
shouldCloseOnClickOutside: false,
isFullScreen: isFullscreen,
__experimentalHideHeader: true,
children: /* @__PURE__ */ jsx(WCTabs, {
orientation: "horizontal",
defaultTabId: "html",
children: /* @__PURE__ */ jsxs(__experimentalVStack, {
expanded: true,
children: [
/* @__PURE__ */ jsxs(__experimentalHStack, {
justify: "space-between",
className: "block-library-html__modal-header",
children: [/* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsxs(WCTabs.TabList, { children: [
/* @__PURE__ */ jsx(WCTabs.Tab, {
tabId: "html",
children: "HTML"
}),
canUserUseUnfilteredHTML && /* @__PURE__ */ jsx(WCTabs.Tab, {
tabId: "css",
children: "CSS"
}),
canUserUseUnfilteredHTML && /* @__PURE__ */ jsx(WCTabs.Tab, {
tabId: "js",
children: __("JavaScript")
})
] }) }), !isMobileViewport && /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
icon: isFullscreen ? square : fullscreen,
label: __("Enable/disable fullscreen"),
onClick: toggleFullscreen,
variant: "tertiary"
}) })]
}),
hasRestrictedContent && /* @__PURE__ */ jsx(Notice, {
status: "warning",
isDismissible: false,
className: "block-library-html__modal-notice",
children: __("This block contains CSS or JavaScript that will be removed when you save because you do not have permission to use unfiltered HTML.")
}),
/* @__PURE__ */ jsxs(Flex, {
direction: isMobileViewport ? "column" : "row",
className: "block-library-html__modal-tabs",
align: "stretch",
gap: 8,
children: [/* @__PURE__ */ jsxs("div", {
ref: nativeUndoRef,
className: "block-library-html__modal-content",
children: [
/* @__PURE__ */ jsx(WCTabs.TabPanel, {
tabId: "html",
focusable: false,
className: "block-library-html__modal-tab",
children: /* @__PURE__ */ jsx(PlainText, {
value: editedHtml,
onChange: setEditedHtml,
placeholder: __("Write HTML…"),
"aria-label": __("HTML"),
className: "block-library-html__modal-editor"
})
}),
canUserUseUnfilteredHTML && /* @__PURE__ */ jsx(WCTabs.TabPanel, {
tabId: "css",
focusable: false,
className: "block-library-html__modal-tab",
children: /* @__PURE__ */ jsx(PlainText, {
value: editedCss,
onChange: setEditedCss,
placeholder: __("Write CSS…"),
"aria-label": __("CSS"),
className: "block-library-html__modal-editor"
})
}),
canUserUseUnfilteredHTML && /* @__PURE__ */ jsx(WCTabs.TabPanel, {
tabId: "js",
focusable: false,
className: "block-library-html__modal-tab",
children: /* @__PURE__ */ jsx(PlainText, {
value: editedJs,
onChange: setEditedJs,
placeholder: __("Write JavaScript…"),
"aria-label": __("JavaScript"),
className: "block-library-html__modal-editor"
})
})
]
}), /* @__PURE__ */ jsx("div", {
className: "block-library-html__preview",
children: /* @__PURE__ */ jsx(Preview, { content: serializeContent({
html: editedHtml,
css: editedCss,
js: editedJs
}) })
})]
}),
/* @__PURE__ */ jsxs(__experimentalHStack, {
alignment: "center",
justify: "flex-end",
spacing: 4,
className: "block-library-html__modal-footer",
children: [/* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
variant: "tertiary",
onClick: onRequestClose,
children: __("Cancel")
}), /* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
variant: "primary",
onClick: handleUpdateAndClose,
children: __("Update")
})]
})
]
})
})
}) });
}
export { HTMLEditModal as default };
//# sourceMappingURL=modal.mjs.map