UNPKG

@wordpress/block-library

Version:
251 lines (250 loc) • 9.39 kB
import clsx from "clsx"; import { BACKSPACE, DELETE, ENTER } from "@wordpress/keycodes"; import { useDispatch, useSelect } from "@wordpress/data"; import { BlockControls, InspectorControls, URLInput, URLPopover, store, useBlockEditingMode, useBlockProps } from "@wordpress/block-editor"; import { createInterpolateElement, flushSync, useRef, useState } from "@wordpress/element"; import { Button, Dropdown, ExternalLink, Icon, TextControl, ToolbarButton, __experimentalInputControlSuffixWrapper, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components"; import { useMergeRefs } from "@wordpress/compose"; import { __ } from "@wordpress/i18n"; import { keyboardReturn } from "@wordpress/icons"; import { store as store$1 } from "@wordpress/blocks"; import { getProtocol, isEmail, prependHTTPS } from "@wordpress/url"; import { getSocialService } from "./social-list.mjs"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import ViewLinkButton from "../utils/view-link-button.mjs"; import isValidLink from "./is-valid-link.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/social-link/edit.jsx const INVALID_LINK = { type: "invalid", message: __("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] = useState(); const inputRef = useRef(); if (customValidity && isValidLink(url)) setCustomValidity(void 0); const validate = () => { if (isValidLink(url)) return true; 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 || !isValidLink(url)) return; if (isEmail(url) && !getProtocol(url)) return `mailto:${url}`; if (/^[^:/?#]+:\d{1,5}(?:\/|$)/.test(url)) return `https://${url}`; return prependHTTPS(url); }; const SocialLinkURLPopover = ({ url, setAttributes, setPopover, popoverAnchor, clientId }) => { const { removeBlock } = useDispatch(store); return /* @__PURE__ */ jsx(URLPopover, { anchor: popoverAnchor, "aria-label": __("Edit social link"), onClose: () => { setPopover(false); popoverAnchor?.focus(); }, onFocusOutside: () => setPopover(false), children: /* @__PURE__ */ jsx("form", { className: "block-editor-url-popover__link-editor", onSubmit: (event) => { event.preventDefault(); setPopover(false); popoverAnchor?.focus(); }, children: /* @__PURE__ */ jsx("div", { className: "block-editor-url-input", children: /* @__PURE__ */ jsx(URLInput, { value: url, onChange: (nextURL) => setAttributes({ url: nextURL }), placeholder: __("Enter social link"), label: __("Enter social link"), hideLabelFromVision: true, disableSuggestions: true, onKeyDown: (event) => { if (!!url || event.defaultPrevented || ![BACKSPACE, DELETE].includes(event.keyCode)) return; removeBlock(clientId); }, suffix: /* @__PURE__ */ jsx(__experimentalInputControlSuffixWrapper, { variant: "control", children: /* @__PURE__ */ jsx(Button, { icon: keyboardReturn, label: __("Apply"), type: "submit", size: "small" }) }) }) }) }) }); }; const SocialLinkEdit = ({ attributes, context, isSelected, setAttributes, clientId, name }) => { const { url, service, label = "", rel } = attributes; const dropdownMenuProps = useToolsPanelDropdownMenuProps(); const { showLabels, iconColor, iconColorValue, iconBackgroundColor, iconBackgroundColorValue } = context; const [showURLPopover, setPopover] = useState(false); const { customValidity: linkValidity, inputRef: linkInputRef, validate: validateLink } = useLinkValidity(url); const wrapperClasses = clsx("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] = useState(null); const isContentOnlyMode = useBlockEditingMode() === "contentOnly"; const { activeVariation } = useSelect((select) => { const { getActiveBlockVariation } = select(store$1); return { activeVariation: getActiveBlockVariation(name, attributes) }; }, [name, attributes]); const { icon, label: socialLinkName } = getSocialService(activeVariation); const socialLinkText = label.trim() === "" ? socialLinkName : label; const ref = useRef(); const blockProps = useBlockProps({ className: "wp-block-social-link-anchor", ref: useMergeRefs([setPopoverAnchor, ref]), onClick: () => setPopover(true), onKeyDown: (event) => { if (event.keyCode === ENTER) { event.preventDefault(); setPopover(true); } } }); return /* @__PURE__ */ jsxs(Fragment, { children: [ isContentOnlyMode && showLabels && /* @__PURE__ */ jsx(BlockControls, { group: "other", children: /* @__PURE__ */ jsx(Dropdown, { popoverProps: { placement: "bottom-start" }, renderToggle: ({ isOpen, onToggle }) => /* @__PURE__ */ jsx(ToolbarButton, { onClick: onToggle, "aria-haspopup": "true", "aria-expanded": isOpen, children: __("Text") }), renderContent: () => /* @__PURE__ */ jsx(TextControl, { className: "wp-block-social-link__toolbar_content_text", label: __("Text"), help: __("Provide a text label or use the default."), value: label, onChange: (value) => setAttributes({ label: value }), placeholder: socialLinkName }) }) }), /* @__PURE__ */ jsx(InspectorControls, { group: "content", children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ url: void 0, label: void 0 }); }, dropdownMenuProps, children: [ /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { isShownByDefault: true, label: __("Text"), hasValue: () => !!label, onDeselect: () => { setAttributes({ label: void 0 }); }, children: /* @__PURE__ */ jsx(TextControl, { label: __("Text"), help: __("The text is visible when enabled from the parent Social Icons block."), value: label, onChange: (value) => setAttributes({ label: value }), placeholder: socialLinkName }) }), /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { isShownByDefault: true, label: __("Link to"), hasValue: () => !!url, onDeselect: () => { setAttributes({ url: void 0 }); }, children: /* @__PURE__ */ jsx("div", { onBlur: validateLink, children: /* @__PURE__ */ jsx(URLInput, { className: "wp-block-social-link__url-input", label: __("Link to"), placeholder: __("Enter social link"), value: url || "", onChange: (value) => setAttributes({ url: value || void 0 }), disableSuggestions: true, isFullWidth: true, required: false, inputRef: linkInputRef, customValidity: linkValidity }) }) }), /* @__PURE__ */ jsx(ViewLinkButton, { url: getRenderedLink(url) }) ] }) }), /* @__PURE__ */ jsx(InspectorControls, { group: "advanced", children: /* @__PURE__ */ jsx(TextControl, { label: __("Link relation"), help: createInterpolateElement(__("The <a>Link Relation</a> attribute defines the relationship between a linked resource and the current document."), { a: /* @__PURE__ */ jsx(ExternalLink, { href: "https://developer.mozilla.org/docs/Web/HTML/Attributes/rel" }) }), value: rel || "", onChange: (value) => setAttributes({ rel: value }) }) }), /* @__PURE__ */ jsxs("li", { role: "presentation", className: wrapperClasses, style: { color: iconColorValue, backgroundColor: iconBackgroundColorValue }, children: [/* @__PURE__ */ jsxs("button", { "aria-haspopup": "dialog", ...blockProps, role: "button", children: [/* @__PURE__ */ jsx(Icon, { icon }), /* @__PURE__ */ jsx("span", { className: clsx("wp-block-social-link-label", { "screen-reader-text": !showLabels }), children: socialLinkText })] }), isSelected && showURLPopover && /* @__PURE__ */ jsx(SocialLinkURLPopover, { url, setAttributes, setPopover, popoverAnchor, clientId })] }) ] }); }; export { SocialLinkEdit as default }; //# sourceMappingURL=edit.mjs.map