UNPKG

@wordpress/block-library

Version:
126 lines (125 loc) • 5.58 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_url = require("@wordpress/url"); // packages/block-library/src/navigation/edit/utils.js function getComputedStyle(node) { return node.ownerDocument.defaultView.getComputedStyle(node); } function detectColors(colorsDetectionElement, setColor, setBackground) { if (!colorsDetectionElement) return; setColor(getComputedStyle(colorsDetectionElement).color); let backgroundColorNode = colorsDetectionElement; let backgroundColor = getComputedStyle(backgroundColorNode).backgroundColor; while (backgroundColor === "rgba(0, 0, 0, 0)" && backgroundColorNode.parentNode && backgroundColorNode.parentNode.nodeType === backgroundColorNode.parentNode.ELEMENT_NODE) { backgroundColorNode = backgroundColorNode.parentNode; backgroundColor = getComputedStyle(backgroundColorNode).backgroundColor; } setBackground(backgroundColor); } /** * Determine the colors for a menu. * * Order of priority is: * 1: Overlay custom colors (if submenu) * 2: Overlay theme colors (if submenu) * 3: Custom colors * 4: Theme colors * 5: Global styles * * @param {Object} context * @param {boolean} isSubMenu */ function getColors(context, isSubMenu) { const { textColor, customTextColor, backgroundColor, customBackgroundColor, overlayTextColor, customOverlayTextColor, overlayBackgroundColor, customOverlayBackgroundColor, style } = context; const colors = {}; if (isSubMenu && !!customOverlayTextColor) colors.customTextColor = customOverlayTextColor; else if (isSubMenu && !!overlayTextColor) colors.textColor = overlayTextColor; else if (!!customTextColor) colors.customTextColor = customTextColor; else if (!!textColor) colors.textColor = textColor; else if (!!style?.color?.text) colors.customTextColor = style.color.text; if (isSubMenu && !!customOverlayBackgroundColor) colors.customBackgroundColor = customOverlayBackgroundColor; else if (isSubMenu && !!overlayBackgroundColor) colors.backgroundColor = overlayBackgroundColor; else if (!!customBackgroundColor) colors.customBackgroundColor = customBackgroundColor; else if (!!backgroundColor) colors.backgroundColor = backgroundColor; else if (!!style?.color?.background) colors.customTextColor = style.color.background; return colors; } function getNavigationChildBlockProps(innerBlocksColors) { return { className: (0, clsx.default)("wp-block-navigation__submenu-container", { "has-text-color": !!(innerBlocksColors.textColor || innerBlocksColors.customTextColor), [`has-${innerBlocksColors.textColor}-color`]: !!innerBlocksColors.textColor, "has-background": !!(innerBlocksColors.backgroundColor || innerBlocksColors.customBackgroundColor), [`has-${innerBlocksColors.backgroundColor}-background-color`]: !!innerBlocksColors.backgroundColor }), style: { color: innerBlocksColors.customTextColor, backgroundColor: innerBlocksColors.customBackgroundColor } }; } /** * Return a unique template part title based on * the given title and existing template parts. * * This implementation is copied from: * packages/fields/src/components/create-template-part-modal/utils.js * * @param {string} title The original template part title. * @param {Object} templateParts The array of template part entities. * @return {string} A unique template part title. */ const getUniqueTemplatePartTitle = (title, templateParts) => { const lowercaseTitle = title.toLowerCase(); const existingTitles = templateParts.map((templatePart) => templatePart.title.rendered.toLowerCase()); if (!existingTitles.includes(lowercaseTitle)) return title; let suffix = 2; while (existingTitles.includes(`${lowercaseTitle} ${suffix}`)) suffix++; return `${title} ${suffix}`; }; /** * Get a valid slug for a template part. * Currently template parts only allow latin chars. * The fallback slug will receive suffix by default. * * This implementation is copied from: * packages/fields/src/components/create-template-part-modal/utils.js * * @param {string} title The template part title. * @return {string} A valid template part slug. */ const getCleanTemplatePartSlug = (title) => { return (0, _wordpress_url.cleanForSlug)(title).replace(/[^\w-]+/g, "") || "wp-custom-part"; }; exports.detectColors = detectColors; exports.getCleanTemplatePartSlug = getCleanTemplatePartSlug; exports.getColors = getColors; exports.getNavigationChildBlockProps = getNavigationChildBlockProps; exports.getUniqueTemplatePartTitle = getUniqueTemplatePartTitle; //# sourceMappingURL=utils.cjs.map