@wordpress/block-library
Version:
Block library for the WordPress editor.
280 lines (279 loc) • 12.4 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_keycodes = require("@wordpress/keycodes");
let _wordpress_data = require("@wordpress/data");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_element = require("@wordpress/element");
let _wordpress_components = require("@wordpress/components");
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_icons = require("@wordpress/icons");
let _wordpress_blocks = require("@wordpress/blocks");
let _wordpress_url = require("@wordpress/url");
let __social_list_js = require("./social-list.cjs");
let ___utils_hooks_js = require("../utils/hooks.cjs");
let ___utils_view_link_button_tsx = require("../utils/view-link-button.cjs");
___utils_view_link_button_tsx = __toESM(___utils_view_link_button_tsx);
let __is_valid_link_ts = require("./is-valid-link.cjs");
__is_valid_link_ts = __toESM(__is_valid_link_ts);
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/social-link/edit.jsx
const INVALID_LINK = {
type: "invalid",
message: (0, _wordpress_i18n.__)("Please enter a valid URL.")
};
/**
* Shows an error for an invalid Social Icon link once it is checked, without
* rejecting the link. The error is only set when the link is checked, on blur,
* because `URLInput` remounts its input the first time it is given
* a validity, which would otherwise interrupt typing.
*
* @param {string} url The link.
* @return {Object} The `URLInput` props, and `validate`, which checks the link
* and returns whether it is valid.
*/
function useLinkValidity(url) {
const [customValidity, setCustomValidity] = (0, _wordpress_element.useState)();
const inputRef = (0, _wordpress_element.useRef)();
if (customValidity && (0, __is_valid_link_ts.default)(url)) setCustomValidity(void 0);
const validate = () => {
if ((0, __is_valid_link_ts.default)(url)) return true;
(0, _wordpress_element.flushSync)(() => setCustomValidity(INVALID_LINK));
inputRef.current?.dispatchEvent(new window.Event("invalid"));
return false;
};
return {
customValidity,
inputRef,
validate
};
}
/**
* Returns the link the front end renders for a valid Social Icon link: an
* email address becomes a `mailto:` link, and a link without a protocol uses
* HTTPS.
*
* Keep this in sync with how `render_block_core_social_link()` in `index.php`
* builds the link, so View opens the same URL as the front end.
*
* @param {string} url The link.
* @return {string|undefined} The rendered link.
*/
const getRenderedLink = (url) => {
if (!url || !(0, __is_valid_link_ts.default)(url)) return;
if ((0, _wordpress_url.isEmail)(url) && !(0, _wordpress_url.getProtocol)(url)) return `mailto:${url}`;
if (/^[^:/?#]+:\d{1,5}(?:\/|$)/.test(url)) return `https://${url}`;
return (0, _wordpress_url.prependHTTPS)(url);
};
const SocialLinkURLPopover = ({ url, setAttributes, setPopover, popoverAnchor, clientId }) => {
const { removeBlock } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.URLPopover, {
anchor: popoverAnchor,
"aria-label": (0, _wordpress_i18n.__)("Edit social link"),
onClose: () => {
setPopover(false);
popoverAnchor?.focus();
},
onFocusOutside: () => setPopover(false),
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("form", {
className: "block-editor-url-popover__link-editor",
onSubmit: (event) => {
event.preventDefault();
setPopover(false);
popoverAnchor?.focus();
},
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: "block-editor-url-input",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.URLInput, {
value: url,
onChange: (nextURL) => setAttributes({ url: nextURL }),
placeholder: (0, _wordpress_i18n.__)("Enter social link"),
label: (0, _wordpress_i18n.__)("Enter social link"),
hideLabelFromVision: true,
disableSuggestions: true,
onKeyDown: (event) => {
if (!!url || event.defaultPrevented || ![_wordpress_keycodes.BACKSPACE, _wordpress_keycodes.DELETE].includes(event.keyCode)) return;
removeBlock(clientId);
},
suffix: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalInputControlSuffixWrapper, {
variant: "control",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, {
icon: _wordpress_icons.keyboardReturn,
label: (0, _wordpress_i18n.__)("Apply"),
type: "submit",
size: "small"
})
})
})
})
})
});
};
const SocialLinkEdit = ({ attributes, context, isSelected, setAttributes, clientId, name }) => {
const { url, service, label = "", rel } = attributes;
const dropdownMenuProps = (0, ___utils_hooks_js.useToolsPanelDropdownMenuProps)();
const { showLabels, iconColor, iconColorValue, iconBackgroundColor, iconBackgroundColorValue } = context;
const [showURLPopover, setPopover] = (0, _wordpress_element.useState)(false);
const { customValidity: linkValidity, inputRef: linkInputRef, validate: validateLink } = useLinkValidity(url);
const wrapperClasses = (0, clsx.default)("wp-social-link", "wp-block-social-link", "wp-social-link-" + service, {
"wp-social-link__is-incomplete": !url,
[`has-${iconColor}-color`]: iconColor,
[`has-${iconBackgroundColor}-background-color`]: iconBackgroundColor
});
const [popoverAnchor, setPopoverAnchor] = (0, _wordpress_element.useState)(null);
const isContentOnlyMode = (0, _wordpress_block_editor.useBlockEditingMode)() === "contentOnly";
const { activeVariation } = (0, _wordpress_data.useSelect)((select) => {
const { getActiveBlockVariation } = select(_wordpress_blocks.store);
return { activeVariation: getActiveBlockVariation(name, attributes) };
}, [name, attributes]);
const { icon, label: socialLinkName } = (0, __social_list_js.getSocialService)(activeVariation);
const socialLinkText = label.trim() === "" ? socialLinkName : label;
const ref = (0, _wordpress_element.useRef)();
const blockProps = (0, _wordpress_block_editor.useBlockProps)({
className: "wp-block-social-link-anchor",
ref: (0, _wordpress_compose.useMergeRefs)([setPopoverAnchor, ref]),
onClick: () => setPopover(true),
onKeyDown: (event) => {
if (event.keyCode === _wordpress_keycodes.ENTER) {
event.preventDefault();
setPopover(true);
}
}
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
isContentOnlyMode && showLabels && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockControls, {
group: "other",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Dropdown, {
popoverProps: { placement: "bottom-start" },
renderToggle: ({ isOpen, onToggle }) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarButton, {
onClick: onToggle,
"aria-haspopup": "true",
"aria-expanded": isOpen,
children: (0, _wordpress_i18n.__)("Text")
}),
renderContent: () => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextControl, {
className: "wp-block-social-link__toolbar_content_text",
label: (0, _wordpress_i18n.__)("Text"),
help: (0, _wordpress_i18n.__)("Provide a text label or use the default."),
value: label,
onChange: (value) => setAttributes({ label: value }),
placeholder: socialLinkName
})
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, {
group: "content",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalToolsPanel, {
label: (0, _wordpress_i18n.__)("Settings"),
resetAll: () => {
setAttributes({
url: void 0,
label: void 0
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
isShownByDefault: true,
label: (0, _wordpress_i18n.__)("Text"),
hasValue: () => !!label,
onDeselect: () => {
setAttributes({ label: void 0 });
},
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextControl, {
label: (0, _wordpress_i18n.__)("Text"),
help: (0, _wordpress_i18n.__)("The text is visible when enabled from the parent Social Icons block."),
value: label,
onChange: (value) => setAttributes({ label: value }),
placeholder: socialLinkName
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
isShownByDefault: true,
label: (0, _wordpress_i18n.__)("Link to"),
hasValue: () => !!url,
onDeselect: () => {
setAttributes({ url: void 0 });
},
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
onBlur: validateLink,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.URLInput, {
className: "wp-block-social-link__url-input",
label: (0, _wordpress_i18n.__)("Link to"),
placeholder: (0, _wordpress_i18n.__)("Enter social link"),
value: url || "",
onChange: (value) => setAttributes({ url: value || void 0 }),
disableSuggestions: true,
isFullWidth: true,
required: false,
inputRef: linkInputRef,
customValidity: linkValidity
})
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(___utils_view_link_button_tsx.default, { url: getRenderedLink(url) })
]
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, {
group: "advanced",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextControl, {
label: (0, _wordpress_i18n.__)("Link relation"),
help: (0, _wordpress_element.createInterpolateElement)((0, _wordpress_i18n.__)("The <a>Link Relation</a> attribute defines the relationship between a linked resource and the current document."), { a: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ExternalLink, { href: "https://developer.mozilla.org/docs/Web/HTML/Attributes/rel" }) }),
value: rel || "",
onChange: (value) => setAttributes({ rel: value })
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("li", {
role: "presentation",
className: wrapperClasses,
style: {
color: iconColorValue,
backgroundColor: iconBackgroundColorValue
},
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("button", {
"aria-haspopup": "dialog",
...blockProps,
role: "button",
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Icon, { icon }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
className: (0, clsx.default)("wp-block-social-link-label", { "screen-reader-text": !showLabels }),
children: socialLinkText
})]
}), isSelected && showURLPopover && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SocialLinkURLPopover, {
url,
setAttributes,
setPopover,
popoverAnchor,
clientId
})]
})
] });
};
exports.default = SocialLinkEdit;
//# sourceMappingURL=edit.cjs.map