UNPKG

@wordpress/block-library

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