@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 6.57 kB
Source Map (JSON)
{"version":3,"file":"utils.cjs","names":["cleanForSlug"],"sources":["../../../src/navigation/edit/utils.js"],"sourcesContent":["import clsx from 'clsx';\nimport { cleanForSlug } from '@wordpress/url';\n\nfunction getComputedStyle( node ) {\n\treturn node.ownerDocument.defaultView.getComputedStyle( node );\n}\n\nexport function detectColors(\n\tcolorsDetectionElement,\n\tsetColor,\n\tsetBackground\n) {\n\tif ( ! colorsDetectionElement ) {\n\t\treturn;\n\t}\n\tsetColor( getComputedStyle( colorsDetectionElement ).color );\n\n\tlet backgroundColorNode = colorsDetectionElement;\n\tlet backgroundColor =\n\t\tgetComputedStyle( backgroundColorNode ).backgroundColor;\n\twhile (\n\t\tbackgroundColor === 'rgba(0, 0, 0, 0)' &&\n\t\tbackgroundColorNode.parentNode &&\n\t\tbackgroundColorNode.parentNode.nodeType ===\n\t\t\tbackgroundColorNode.parentNode.ELEMENT_NODE\n\t) {\n\t\tbackgroundColorNode = backgroundColorNode.parentNode;\n\t\tbackgroundColor =\n\t\t\tgetComputedStyle( backgroundColorNode ).backgroundColor;\n\t}\n\n\tsetBackground( backgroundColor );\n}\n\n/**\n * Determine the colors for a menu.\n *\n * Order of priority is:\n * 1: Overlay custom colors (if submenu)\n * 2: Overlay theme colors (if submenu)\n * 3: Custom colors\n * 4: Theme colors\n * 5: Global styles\n *\n * @param {Object} context\n * @param {boolean} isSubMenu\n */\nexport function getColors( context, isSubMenu ) {\n\tconst {\n\t\ttextColor,\n\t\tcustomTextColor,\n\t\tbackgroundColor,\n\t\tcustomBackgroundColor,\n\t\toverlayTextColor,\n\t\tcustomOverlayTextColor,\n\t\toverlayBackgroundColor,\n\t\tcustomOverlayBackgroundColor,\n\t\tstyle,\n\t} = context;\n\n\tconst colors = {};\n\n\tif ( isSubMenu && !! customOverlayTextColor ) {\n\t\tcolors.customTextColor = customOverlayTextColor;\n\t} else if ( isSubMenu && !! overlayTextColor ) {\n\t\tcolors.textColor = overlayTextColor;\n\t} else if ( !! customTextColor ) {\n\t\tcolors.customTextColor = customTextColor;\n\t} else if ( !! textColor ) {\n\t\tcolors.textColor = textColor;\n\t} else if ( !! style?.color?.text ) {\n\t\tcolors.customTextColor = style.color.text;\n\t}\n\n\tif ( isSubMenu && !! customOverlayBackgroundColor ) {\n\t\tcolors.customBackgroundColor = customOverlayBackgroundColor;\n\t} else if ( isSubMenu && !! overlayBackgroundColor ) {\n\t\tcolors.backgroundColor = overlayBackgroundColor;\n\t} else if ( !! customBackgroundColor ) {\n\t\tcolors.customBackgroundColor = customBackgroundColor;\n\t} else if ( !! backgroundColor ) {\n\t\tcolors.backgroundColor = backgroundColor;\n\t} else if ( !! style?.color?.background ) {\n\t\tcolors.customTextColor = style.color.background;\n\t}\n\n\treturn colors;\n}\n\nexport function getNavigationChildBlockProps( innerBlocksColors ) {\n\treturn {\n\t\tclassName: clsx( 'wp-block-navigation__submenu-container', {\n\t\t\t'has-text-color': !! (\n\t\t\t\tinnerBlocksColors.textColor || innerBlocksColors.customTextColor\n\t\t\t),\n\t\t\t[ `has-${ innerBlocksColors.textColor }-color` ]:\n\t\t\t\t!! innerBlocksColors.textColor,\n\t\t\t'has-background': !! (\n\t\t\t\tinnerBlocksColors.backgroundColor ||\n\t\t\t\tinnerBlocksColors.customBackgroundColor\n\t\t\t),\n\t\t\t[ `has-${ innerBlocksColors.backgroundColor }-background-color` ]:\n\t\t\t\t!! innerBlocksColors.backgroundColor,\n\t\t} ),\n\t\tstyle: {\n\t\t\tcolor: innerBlocksColors.customTextColor,\n\t\t\tbackgroundColor: innerBlocksColors.customBackgroundColor,\n\t\t},\n\t};\n}\n\n/**\n * Return a unique template part title based on\n * the given title and existing template parts.\n *\n * This implementation is copied from:\n * packages/fields/src/components/create-template-part-modal/utils.js\n *\n * @param {string} title The original template part title.\n * @param {Object} templateParts The array of template part entities.\n * @return {string} A unique template part title.\n */\nexport const getUniqueTemplatePartTitle = ( title, templateParts ) => {\n\tconst lowercaseTitle = title.toLowerCase();\n\tconst existingTitles = templateParts.map( ( templatePart ) =>\n\t\ttemplatePart.title.rendered.toLowerCase()\n\t);\n\n\tif ( ! existingTitles.includes( lowercaseTitle ) ) {\n\t\treturn title;\n\t}\n\n\tlet suffix = 2;\n\twhile ( existingTitles.includes( `${ lowercaseTitle } ${ suffix }` ) ) {\n\t\tsuffix++;\n\t}\n\n\treturn `${ title } ${ suffix }`;\n};\n\n/**\n * Get a valid slug for a template part.\n * Currently template parts only allow latin chars.\n * The fallback slug will receive suffix by default.\n *\n * This implementation is copied from:\n * packages/fields/src/components/create-template-part-modal/utils.js\n *\n * @param {string} title The template part title.\n * @return {string} A valid template part slug.\n */\nexport const getCleanTemplatePartSlug = ( title ) => {\n\treturn cleanForSlug( title ).replace( /[^\\w-]+/g, '' ) || 'wp-custom-part';\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,SAAS,iBAAkB,MAAO;CACjC,OAAO,KAAK,cAAc,YAAY,iBAAkB,IAAK;AAC9D;AAEA,SAAgB,aACf,wBACA,UACA,eACC;CACD,IAAK,CAAE,wBACN;CAED,SAAU,iBAAkB,sBAAuB,CAAC,CAAC,KAAM;CAE3D,IAAI,sBAAsB;CAC1B,IAAI,kBACH,iBAAkB,mBAAoB,CAAC,CAAC;CACzC,OACC,oBAAoB,sBACpB,oBAAoB,cACpB,oBAAoB,WAAW,aAC9B,oBAAoB,WAAW,cAC/B;EACD,sBAAsB,oBAAoB;EAC1C,kBACC,iBAAkB,mBAAoB,CAAC,CAAC;CAC1C;CAEA,cAAe,eAAgB;AAChC;;;;;;;;;;;;;;AAeA,SAAgB,UAAW,SAAS,WAAY;CAC/C,MAAM,EACL,WACA,iBACA,iBACA,uBACA,kBACA,wBACA,wBACA,8BACA,UACG;CAEJ,MAAM,SAAS,CAAC;CAEhB,IAAK,aAAa,CAAC,CAAE,wBACpB,OAAO,kBAAkB;MACnB,IAAK,aAAa,CAAC,CAAE,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