UNPKG

@wordpress/block-library

Version:
185 lines (184 loc) • 7.56 kB
import clsx from "clsx"; import { __, sprintf } from "@wordpress/i18n"; import { DropdownMenu, TextControl, ToolbarButton, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components"; import { flipHorizontal, flipVertical, rotateRight } from "@wordpress/icons"; import { BlockControls, InspectorControls, __experimentalGetShadowClassesAndStyles, __experimentalGetSpacingClassesAndStyles, __experimentalUseBorderProps, __experimentalUseColorProps, getDimensionsClassesAndStyles, useBackgroundProps, useBlockEditingMode, useBlockProps } from "@wordpress/block-editor"; import { getBlockBindingsSource } from "@wordpress/blocks"; import { useCallback, useState } from "@wordpress/element"; import { Path, Rect, SVG } from "@wordpress/primitives"; import { useSelect } from "@wordpress/data"; import { store } from "@wordpress/core-data"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import HtmlRenderer from "../utils/html-renderer.mjs"; import { CustomInserterModal } from "./components/index.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/icon/edit.jsx const ICON_NAME_REGEX = /^[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?\/[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?$/; const IconPlaceholder = ({ className, style }) => /* @__PURE__ */ jsxs(SVG, { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 60 60", preserveAspectRatio: "none", fill: "none", "aria-hidden": "true", className: clsx("wp-block-icon__placeholder", className), style, children: [/* @__PURE__ */ jsx(Rect, { width: "60", height: "60", fill: "currentColor", fillOpacity: .1 }), /* @__PURE__ */ jsx(Path, { vectorEffect: "non-scaling-stroke", stroke: "currentColor", strokeOpacity: .25, d: "M60 60 0 0" })] }); function Edit({ attributes, setAttributes, isSelected, context }) { const { icon, ariaLabel, flipHorizontal: flipHorizontal$1, flipVertical: flipVertical$1, rotation, metadata } = attributes; const [isInserterOpen, setInserterOpen] = useState(false); const onChangeIcon = useCallback((name) => { setAttributes({ icon: name }); setInserterOpen(false); }, [setAttributes]); const isContentOnlyMode = useBlockEditingMode() === "contentOnly"; const { isIconBindingReadOnly = false, iconBindingReadOnlyMessage } = useSelect((select) => { if (!isSelected) return {}; const iconBinding = metadata?.bindings?.icon; const blockBindingsSource = getBlockBindingsSource(iconBinding?.source); return { isIconBindingReadOnly: !!iconBinding && !blockBindingsSource?.canUserEditValue?.({ select, context, args: iconBinding.args }), iconBindingReadOnlyMessage: blockBindingsSource?.label ? sprintf(__("Connected to %s"), blockBindingsSource.label) : __("Connected to dynamic data") }; }, [ context, isSelected, metadata?.bindings?.icon ]); const colorProps = __experimentalUseColorProps(attributes); const backgroundProps = useBackgroundProps(attributes); const spacingProps = __experimentalGetSpacingClassesAndStyles({ style: { spacing: { padding: attributes.style?.spacing?.padding } } }); const borderProps = __experimentalUseBorderProps(attributes); const shadowProps = __experimentalGetShadowClassesAndStyles(attributes); const dimensionsProps = getDimensionsClassesAndStyles(attributes); const iconToDisplay = useSelect((select) => { if (typeof icon !== "string" || !ICON_NAME_REGEX.test(icon)) return null; return select(store).getEntityRecord("root", "icon", icon); }, [icon])?.content || ""; const flipClasses = { "is-flip-horizontal": flipHorizontal$1, "is-flip-vertical": flipVertical$1 }; const rotationStyle = rotation ? { rotate: `${rotation}deg` } : {}; const iconToolbarButtonLabel = icon ? __("Replace") : __("Choose icon"); const blockControls = /* @__PURE__ */ jsxs(Fragment, { children: [icon && /* @__PURE__ */ jsxs(BlockControls, { group: "block", children: [ /* @__PURE__ */ jsx(ToolbarButton, { icon: flipHorizontal, label: __("Flip horizontal"), isPressed: flipHorizontal$1, onClick: () => setAttributes({ flipHorizontal: !flipHorizontal$1 }) }), /* @__PURE__ */ jsx(ToolbarButton, { icon: flipVertical, label: __("Flip vertical"), isPressed: flipVertical$1, onClick: () => setAttributes({ flipVertical: !flipVertical$1 }) }), /* @__PURE__ */ jsx(ToolbarButton, { icon: rotateRight, label: __("Rotate"), onClick: () => setAttributes({ rotation: ((rotation || 0) + 90) % 360 }) }) ] }), /* @__PURE__ */ jsxs(BlockControls, { group: "other", children: [/* @__PURE__ */ jsx(ToolbarButton, { disabled: isIconBindingReadOnly, label: isIconBindingReadOnly ? iconBindingReadOnlyMessage : void 0, showTooltip: isIconBindingReadOnly, "aria-label": isIconBindingReadOnly ? iconToolbarButtonLabel : void 0, onClick: () => { setInserterOpen(true); }, children: iconToolbarButtonLabel }), isContentOnlyMode && icon && /* @__PURE__ */ jsx(DropdownMenu, { icon: "", toggleProps: { as: ToolbarButton }, popoverProps: { className: "is-alternate" }, text: __("Label"), children: () => /* @__PURE__ */ jsx(TextControl, { className: "wp-block-icon__toolbar-content", label: __("Label"), value: ariaLabel || "", onChange: (value) => setAttributes({ ariaLabel: value }), help: __("Briefly describe the icon to help screen reader users. Leave blank for decorative icons.") }) })] })] }); const dropdownMenuProps = useToolsPanelDropdownMenuProps(); const inspectorControls = icon && /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(InspectorControls, { group: "settings", children: /* @__PURE__ */ jsx(__experimentalToolsPanel, { label: __("Settings"), resetAll: () => setAttributes({ ariaLabel: void 0 }), dropdownMenuProps, children: /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { label: __("Label"), isShownByDefault: true, hasValue: () => !!ariaLabel, onDeselect: () => setAttributes({ ariaLabel: void 0 }), children: /* @__PURE__ */ jsx(TextControl, { label: __("Label"), help: __("Briefly describe the icon to help screen reader users. Leave blank for decorative icons."), value: ariaLabel || "", onChange: (value) => setAttributes({ ariaLabel: value }) }) }) }) }) }); return /* @__PURE__ */ jsxs(Fragment, { children: [ blockControls, inspectorControls, /* @__PURE__ */ jsx("div", { ...useBlockProps(), children: iconToDisplay ? /* @__PURE__ */ jsx(HtmlRenderer, { html: iconToDisplay, wrapperProps: { className: clsx(colorProps.className, backgroundProps.className, borderProps.className, spacingProps.className, dimensionsProps.className, flipClasses), style: { ...colorProps.style, ...backgroundProps.style, ...borderProps.style, ...shadowProps.style, ...spacingProps.style, ...dimensionsProps.style, ...rotationStyle } } }) : /* @__PURE__ */ jsx(IconPlaceholder, { className: clsx(borderProps.className, spacingProps.className, dimensionsProps.className, flipClasses), style: { ...borderProps.style, ...shadowProps.style, ...spacingProps.style, ...dimensionsProps.style, ...rotationStyle, height: "auto" } }) }), isInserterOpen && /* @__PURE__ */ jsx(CustomInserterModal, { onClose: () => setInserterOpen(false), value: attributes.icon, onChange: onChangeIcon }) ] }); } export { Edit, Edit as default }; //# sourceMappingURL=edit.mjs.map