@wordpress/block-library
Version:
Block library for the WordPress editor.
197 lines (196 loc) • 8.27 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let clsx = require("clsx");
clsx = __toESM(clsx);
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_element = require("@wordpress/element");
let _wordpress_data = require("@wordpress/data");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_core_data = require("@wordpress/core-data");
let ___utils_caption_jsx = require("../utils/caption.cjs");
let __util_jsx = require("./util.cjs");
let __embed_controls_jsx = require("./embed-controls.cjs");
__embed_controls_jsx = __toESM(__embed_controls_jsx);
let __icons_jsx = require("./icons.cjs");
let __embed_loading_jsx = require("./embed-loading.cjs");
__embed_loading_jsx = __toESM(__embed_loading_jsx);
let __embed_placeholder_jsx = require("./embed-placeholder.cjs");
__embed_placeholder_jsx = __toESM(__embed_placeholder_jsx);
let __embed_preview_jsx = require("./embed-preview.cjs");
__embed_preview_jsx = __toESM(__embed_preview_jsx);
let react_jsx_runtime = require("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: (0, _wordpress_i18n._x)("Embed", "block title"),
icon: __icons_jsx.embedContentIcon
};
const { icon, title } = (0, __util_jsx.getEmbedInfoByProvider)(providerNameSlug) || defaultEmbedInfo;
const [url, setURL] = (0, _wordpress_element.useState)(attributesUrl);
const [isEditingURL, setIsEditingURL] = (0, _wordpress_element.useState)(false);
const { invalidateResolution } = (0, _wordpress_data.useDispatch)(_wordpress_core_data.store);
const { __unstableMarkNextChangeAsNotPersistent } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
const { preview, fetching, themeSupportsResponsive, cannotEmbed, hasResolved } = (0, _wordpress_data.useSelect)((select) => {
const { getEmbedPreview, isPreviewEmbedFallback, isRequestingEmbedPreview, getThemeSupports, hasFinishedResolution } = select(_wordpress_core_data.store);
if (!attributesUrl) return {
fetching: false,
cannotEmbed: false
};
const embedPreview = getEmbedPreview(attributesUrl);
const previewIsFallback = isPreviewEmbedFallback(attributesUrl);
const badEmbedProvider = embedPreview?.html === false && embedPreview?.type === void 0;
const wordpressCantEmbed = embedPreview?.data?.status === 404;
const validPreview = !!embedPreview && !badEmbedProvider && !wordpressCantEmbed;
return {
preview: validPreview ? embedPreview : void 0,
fetching: isRequestingEmbedPreview(attributesUrl),
themeSupportsResponsive: getThemeSupports()["responsive-embeds"],
cannotEmbed: !validPreview || previewIsFallback,
hasResolved: hasFinishedResolution("getEmbedPreview", [attributesUrl])
};
}, [attributesUrl]);
/**
* Returns the attributes derived from the preview, merged with the current attributes.
*
* @return {Object} Merged attributes.
*/
const getMergedAttributes = () => (0, __util_jsx.getMergedAttributesWithPreview)(attributes, preview, title, responsive);
function toggleResponsive(newAllowResponsive) {
const { className } = attributes;
const { html } = preview;
setAttributes({
allowResponsive: newAllowResponsive,
className: (0, __util_jsx.getClassNames)(html, className, responsive && newAllowResponsive)
});
}
(0, _wordpress_element.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
]);
(0, _wordpress_element.useEffect)(() => {
if (!preview || isEditingURL) return;
const mergedAttributes = getMergedAttributes();
if (onReplace) {
const upgradedBlock = (0, __util_jsx.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 = (0, _wordpress_block_editor.useBlockProps)();
if (fetching) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__embed_loading_jsx.default, {})
});
const label = (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("%s URL"), title);
if (!preview || cannotEmbed || isEditingURL) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__embed_placeholder_jsx.default, {
icon,
label,
onFocus,
onSubmit: (event) => {
if (event) event.preventDefault();
const rewrittenURL = (0, __util_jsx.rewriteXToTwitter)(url);
const blockClass = (0, __util_jsx.removeAspectRatioClasses)(attributes.className);
setURL(rewrittenURL);
setAttributes({
url: rewrittenURL,
...(0, __util_jsx.findMoreSuitableBlock)(rewrittenURL)?.attributes,
className: blockClass
});
setIsEditingURL(false);
},
value: url,
cannotEmbed,
onChange: (value) => setURL(value),
fallback: onReplace ? () => (0, __util_jsx.fallback)(url, onReplace) : void 0,
tryAgain: () => {
invalidateResolution("getEmbedPreview", [url]);
}
})
});
const { caption, type, allowResponsive, className: classFromPreview } = getMergedAttributes();
const className = (0, clsx.default)(classFromPreview, props.className);
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(__embed_controls_jsx.default, {
showEditButton: preview && !cannotEmbed,
themeSupportsResponsive,
blockSupportsResponsive: responsive,
allowResponsive,
toggleResponsive,
switchBackToURLInput: () => setIsEditingURL(true)
}), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("figure", {
...blockProps,
className: (0, clsx.default)(blockProps.className, className, {
[`is-type-${type}`]: type,
[`is-provider-${providerNameSlug}`]: providerNameSlug,
[`wp-block-embed-${providerNameSlug}`]: providerNameSlug
}),
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(__embed_preview_jsx.default, {
preview,
previewable,
className,
url,
type,
caption,
onCaptionChange: (value) => setAttributes({ caption: value }),
isSelected,
icon,
label,
insertBlocksAfter,
attributes,
setAttributes
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___utils_caption_jsx.Caption, {
attributes,
setAttributes,
isSelected,
insertBlocksAfter,
label: (0, _wordpress_i18n.__)("Embed caption text"),
showToolbarButton: isSelected
})]
})] });
};
exports.default = EmbedEdit;
//# sourceMappingURL=edit.cjs.map