@wordpress/block-library
Version:
Block library for the WordPress editor.
165 lines (164 loc) • 6.04 kB
JavaScript
import clsx from "clsx";
import { __, _x, sprintf } from "@wordpress/i18n";
import { useEffect, useState } from "@wordpress/element";
import { useDispatch, useSelect } from "@wordpress/data";
import { store, useBlockProps } from "@wordpress/block-editor";
import { store as store$1 } from "@wordpress/core-data";
import { Caption } from "../utils/caption.mjs";
import { createUpgradedEmbedBlock, fallback, findMoreSuitableBlock, getClassNames, getEmbedInfoByProvider, getMergedAttributesWithPreview, removeAspectRatioClasses, rewriteXToTwitter } from "./util.mjs";
import EmbedControls from "./embed-controls.mjs";
import { embedContentIcon } from "./icons.mjs";
import EmbedLoading from "./embed-loading.mjs";
import EmbedPlaceholder from "./embed-placeholder.mjs";
import EmbedPreview from "./embed-preview.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/embed/edit.jsx
const EmbedEdit = (props) => {
const { attributes: { providerNameSlug, previewable, responsive, url: attributesUrl }, attributes, isSelected, onReplace, setAttributes, insertBlocksAfter, onFocus } = props;
const defaultEmbedInfo = {
title: _x("Embed", "block title"),
icon: embedContentIcon
};
const { icon, title } = getEmbedInfoByProvider(providerNameSlug) || defaultEmbedInfo;
const [url, setURL] = useState(attributesUrl);
const [isEditingURL, setIsEditingURL] = useState(false);
const { invalidateResolution } = useDispatch(store$1);
const { __unstableMarkNextChangeAsNotPersistent } = useDispatch(store);
const { preview, fetching, themeSupportsResponsive, cannotEmbed, hasResolved } = useSelect((select) => {
const { getEmbedPreview, isPreviewEmbedFallback, getThemeSupports, hasFinishedResolution } = select(store$1);
if (!attributesUrl) return {
fetching: false,
cannotEmbed: false
};
const embedPreview = getEmbedPreview(attributesUrl);
const previewIsFallback = isPreviewEmbedFallback(attributesUrl);
const validPreview = !!embedPreview?.html;
const resolved = hasFinishedResolution("getEmbedPreview", [attributesUrl]);
return {
preview: validPreview ? embedPreview : void 0,
fetching: !resolved,
themeSupportsResponsive: getThemeSupports()["responsive-embeds"],
cannotEmbed: !validPreview || previewIsFallback,
hasResolved: resolved
};
}, [attributesUrl]);
/**
* Returns the attributes derived from the preview, merged with the current attributes.
*
* @return {Object} Merged attributes.
*/
const getMergedAttributes = () => getMergedAttributesWithPreview(attributes, preview, title, responsive);
function toggleResponsive(newAllowResponsive) {
const { className } = attributes;
const { html } = preview;
setAttributes({
allowResponsive: newAllowResponsive,
className: getClassNames(html, className, responsive && newAllowResponsive)
});
}
useEffect(() => {
if (!cannotEmbed || !hasResolved || !attributesUrl) return;
const newURL = attributesUrl.replace(/\/$/, "");
if (newURL === attributesUrl) return;
setURL(newURL);
setIsEditingURL(false);
__unstableMarkNextChangeAsNotPersistent();
setAttributes({ url: newURL });
}, [
attributesUrl,
cannotEmbed,
hasResolved,
setAttributes,
__unstableMarkNextChangeAsNotPersistent
]);
useEffect(() => {
if (!preview || isEditingURL) return;
const mergedAttributes = getMergedAttributes();
if (onReplace) {
const upgradedBlock = createUpgradedEmbedBlock(props, mergedAttributes);
if (upgradedBlock) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes(upgradedBlock.attributes);
return;
}
}
if (Object.keys(mergedAttributes).some((key) => mergedAttributes[key] !== attributes[key])) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes(mergedAttributes);
}
}, [preview, isEditingURL]);
const blockProps = useBlockProps();
if (fetching) return /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx(EmbedLoading, {})
});
const label = sprintf(__("%s URL"), title);
if (!preview || cannotEmbed || isEditingURL) return /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx(EmbedPlaceholder, {
icon,
label,
onFocus,
onSubmit: (event) => {
if (event) event.preventDefault();
const rewrittenURL = rewriteXToTwitter(url);
const blockClass = removeAspectRatioClasses(attributes.className);
setURL(rewrittenURL);
setAttributes({
url: rewrittenURL,
...findMoreSuitableBlock(rewrittenURL)?.attributes,
className: blockClass
});
setIsEditingURL(false);
},
value: url,
cannotEmbed,
onChange: (value) => setURL(value),
fallback: onReplace ? () => fallback(url, onReplace) : void 0,
tryAgain: () => {
invalidateResolution("getEmbedPreview", [url]);
}
})
});
const { caption, type, allowResponsive, className: classFromPreview } = getMergedAttributes();
const className = clsx(classFromPreview, props.className);
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(EmbedControls, {
showEditButton: preview && !cannotEmbed,
themeSupportsResponsive,
blockSupportsResponsive: responsive,
allowResponsive,
toggleResponsive,
switchBackToURLInput: () => setIsEditingURL(true)
}), /* @__PURE__ */ jsxs("figure", {
...blockProps,
className: clsx(blockProps.className, className, {
[`is-type-${type}`]: type,
[`is-provider-${providerNameSlug}`]: providerNameSlug,
[`wp-block-embed-${providerNameSlug}`]: providerNameSlug
}),
children: [/* @__PURE__ */ jsx(EmbedPreview, {
preview,
previewable,
className,
url,
type,
caption,
onCaptionChange: (value) => setAttributes({ caption: value }),
isSelected,
icon,
label,
insertBlocksAfter,
attributes,
setAttributes
}), /* @__PURE__ */ jsx(Caption, {
attributes,
setAttributes,
isSelected,
insertBlocksAfter,
label: __("Embed caption text"),
showToolbarButton: isSelected
})]
})] });
};
export { EmbedEdit as default };
//# sourceMappingURL=edit.mjs.map