@wordpress/block-library
Version:
Block library for the WordPress editor.
82 lines (81 loc) • 2.52 kB
JavaScript
// packages/block-library/src/embed/embed-preview.js
import { getPhotoHtml } from "./util";
import clsx from "clsx";
import { __, sprintf } from "@wordpress/i18n";
import { Placeholder, SandBox } from "@wordpress/components";
import { BlockIcon } from "@wordpress/block-editor";
import { useState } from "@wordpress/element";
import { getAuthority } from "@wordpress/url";
import WpEmbedPreview from "./wp-embed-preview";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
function EmbedPreview({
preview,
previewable,
url,
type,
isSelected,
className,
icon,
label
}) {
const [interactive, setInteractive] = useState(false);
if (!isSelected && interactive) {
setInteractive(false);
}
const hideOverlay = () => {
setInteractive(true);
};
const { scripts } = preview;
const html = "photo" === type ? getPhotoHtml(preview) : preview.html;
const embedSourceUrl = getAuthority(url);
const iframeTitle = sprintf(
// translators: %s: host providing embed content e.g: www.youtube.com
__("Embedded content from %s"),
embedSourceUrl
);
const sandboxClassnames = clsx(
type,
className,
"wp-block-embed__wrapper"
);
const embedWrapper = "wp-embed" === type ? /* @__PURE__ */ jsx(WpEmbedPreview, { html }) : /* @__PURE__ */ jsxs("div", { className: "wp-block-embed__wrapper", children: [
/* @__PURE__ */ jsx(
SandBox,
{
html,
scripts,
title: iframeTitle,
type: sandboxClassnames,
onFocus: hideOverlay
}
),
!interactive && /* @__PURE__ */ jsx(
"div",
{
className: "block-library-embed__interactive-overlay",
onMouseUp: hideOverlay
}
)
] });
return /* @__PURE__ */ jsx(Fragment, { children: previewable ? embedWrapper : /* @__PURE__ */ jsxs(
Placeholder,
{
icon: /* @__PURE__ */ jsx(BlockIcon, { icon, showColors: true }),
label,
children: [
/* @__PURE__ */ jsx("p", { className: "components-placeholder__error", children: /* @__PURE__ */ jsx("a", { href: url, children: url }) }),
/* @__PURE__ */ jsx("p", { className: "components-placeholder__error", children: sprintf(
/* translators: %s: host providing embed content e.g: www.youtube.com */
__(
"Embedded content from %s can't be previewed in the editor."
),
embedSourceUrl
) })
]
}
) });
}
export {
EmbedPreview as default
};
//# sourceMappingURL=embed-preview.js.map