UNPKG

@wordpress/blocks

Version:
181 lines (180 loc) 6.22 kB
// packages/blocks/src/store/selectors.js import removeAccents from "remove-accents"; import { createSelector } from "@wordpress/data"; import { RichTextData } from "@wordpress/rich-text"; import deprecated from "@wordpress/deprecated"; import { getValueFromObjectPath, matchesAttributes } from "./utils"; import { hasContentRoleAttribute as privateHasContentRoleAttribute } from "./private-selectors"; var getNormalizedBlockType = (state, nameOrType) => "string" === typeof nameOrType ? getBlockType(state, nameOrType) : nameOrType; var getBlockTypes = createSelector( (state) => Object.values(state.blockTypes), (state) => [state.blockTypes] ); function getBlockType(state, name) { return state.blockTypes[name]; } function getBlockStyles(state, name) { return state.blockStyles[name]; } var getBlockVariations = createSelector( (state, blockName, scope) => { const variations = state.blockVariations[blockName]; if (!variations || !scope) { return variations; } return variations.filter((variation) => { return (variation.scope || ["block", "inserter"]).includes( scope ); }); }, (state, blockName) => [state.blockVariations[blockName]] ); function getActiveBlockVariation(state, blockName, attributes, scope) { const variations = getBlockVariations(state, blockName, scope); if (!variations) { return variations; } const blockType = getBlockType(state, blockName); const attributeKeys = Object.keys(blockType?.attributes || {}); let match; let maxMatchedAttributes = 0; for (const variation of variations) { if (Array.isArray(variation.isActive)) { const definedAttributes = variation.isActive.filter( (attribute) => { const topLevelAttribute = attribute.split(".")[0]; return attributeKeys.includes(topLevelAttribute); } ); const definedAttributesLength = definedAttributes.length; if (definedAttributesLength === 0) { continue; } const isMatch = definedAttributes.every((attribute) => { const variationAttributeValue = getValueFromObjectPath( variation.attributes, attribute ); if (variationAttributeValue === void 0) { return false; } let blockAttributeValue = getValueFromObjectPath( attributes, attribute ); if (blockAttributeValue instanceof RichTextData) { blockAttributeValue = blockAttributeValue.toHTMLString(); } return matchesAttributes( blockAttributeValue, variationAttributeValue ); }); if (isMatch && definedAttributesLength > maxMatchedAttributes) { match = variation; maxMatchedAttributes = definedAttributesLength; } } else if (variation.isActive?.(attributes, variation.attributes)) { return match || variation; } } if (!match && ["block", "transform"].includes(scope)) { match = variations.find( (variation) => variation?.isDefault && !Object.hasOwn(variation, "isActive") ); } return match; } function getDefaultBlockVariation(state, blockName, scope) { const variations = getBlockVariations(state, blockName, scope); const defaultVariation = [...variations].reverse().find(({ isDefault }) => !!isDefault); return defaultVariation || variations[0]; } function getCategories(state) { return state.categories; } function getCollections(state) { return state.collections; } function getDefaultBlockName(state) { return state.defaultBlockName; } function getFreeformFallbackBlockName(state) { return state.freeformFallbackBlockName; } function getUnregisteredFallbackBlockName(state) { return state.unregisteredFallbackBlockName; } function getGroupingBlockName(state) { return state.groupingBlockName; } var getChildBlockNames = createSelector( (state, blockName) => { return getBlockTypes(state).filter((blockType) => { return blockType.parent?.includes(blockName); }).map(({ name }) => name); }, (state) => [state.blockTypes] ); var getBlockSupport = (state, nameOrType, feature, defaultSupports) => { const blockType = getNormalizedBlockType(state, nameOrType); if (!blockType?.supports) { return defaultSupports; } return getValueFromObjectPath( blockType.supports, feature, defaultSupports ); }; function hasBlockSupport(state, nameOrType, feature, defaultSupports) { return !!getBlockSupport(state, nameOrType, feature, defaultSupports); } function getNormalizedSearchTerm(term) { return removeAccents(term ?? "").toLowerCase().trim(); } function isMatchingSearchTerm(state, nameOrType, searchTerm = "") { const blockType = getNormalizedBlockType(state, nameOrType); const normalizedSearchTerm = getNormalizedSearchTerm(searchTerm); const isSearchMatch = (candidate) => getNormalizedSearchTerm(candidate).includes(normalizedSearchTerm); return isSearchMatch(blockType.title) || blockType.keywords?.some(isSearchMatch) || isSearchMatch(blockType.category) || typeof blockType.description === "string" && isSearchMatch(blockType.description); } var hasChildBlocks = (state, blockName) => { return getChildBlockNames(state, blockName).length > 0; }; var hasChildBlocksWithInserterSupport = (state, blockName) => { return getChildBlockNames(state, blockName).some((childBlockName) => { return hasBlockSupport(state, childBlockName, "inserter", true); }); }; var __experimentalHasContentRoleAttribute = (...args) => { deprecated("__experimentalHasContentRoleAttribute", { since: "6.7", version: "6.8", hint: "This is a private selector." }); return privateHasContentRoleAttribute(...args); }; export { __experimentalHasContentRoleAttribute, getActiveBlockVariation, getBlockStyles, getBlockSupport, getBlockType, getBlockTypes, getBlockVariations, getCategories, getChildBlockNames, getCollections, getDefaultBlockName, getDefaultBlockVariation, getFreeformFallbackBlockName, getGroupingBlockName, getUnregisteredFallbackBlockName, hasBlockSupport, hasChildBlocks, hasChildBlocksWithInserterSupport, isMatchingSearchTerm }; //# sourceMappingURL=selectors.js.map