@wordpress/blocks
Version:
Block API for WordPress.
233 lines (231 loc) • 8.96 kB
JavaScript
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 __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/blocks/src/store/selectors.js
var selectors_exports = {};
__export(selectors_exports, {
__experimentalHasContentRoleAttribute: () => __experimentalHasContentRoleAttribute,
getActiveBlockVariation: () => getActiveBlockVariation,
getBlockStyles: () => getBlockStyles,
getBlockSupport: () => getBlockSupport,
getBlockType: () => getBlockType,
getBlockTypes: () => getBlockTypes,
getBlockVariations: () => getBlockVariations,
getCategories: () => getCategories,
getChildBlockNames: () => getChildBlockNames,
getCollections: () => getCollections,
getDefaultBlockName: () => getDefaultBlockName,
getDefaultBlockVariation: () => getDefaultBlockVariation,
getFreeformFallbackBlockName: () => getFreeformFallbackBlockName,
getGroupingBlockName: () => getGroupingBlockName,
getUnregisteredFallbackBlockName: () => getUnregisteredFallbackBlockName,
hasBlockSupport: () => hasBlockSupport,
hasChildBlocks: () => hasChildBlocks,
hasChildBlocksWithInserterSupport: () => hasChildBlocksWithInserterSupport,
isMatchingSearchTerm: () => isMatchingSearchTerm
});
module.exports = __toCommonJS(selectors_exports);
var import_remove_accents = __toESM(require("remove-accents"));
var import_data = require("@wordpress/data");
var import_rich_text = require("@wordpress/rich-text");
var import_deprecated = __toESM(require("@wordpress/deprecated"));
var import_utils = require("./utils");
var import_private_selectors = require("./private-selectors");
var getNormalizedBlockType = (state, nameOrType) => "string" === typeof nameOrType ? getBlockType(state, nameOrType) : nameOrType;
var getBlockTypes = (0, import_data.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 = (0, import_data.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 = (0, import_utils.getValueFromObjectPath)(
variation.attributes,
attribute
);
if (variationAttributeValue === void 0) {
return false;
}
let blockAttributeValue = (0, import_utils.getValueFromObjectPath)(
attributes,
attribute
);
if (blockAttributeValue instanceof import_rich_text.RichTextData) {
blockAttributeValue = blockAttributeValue.toHTMLString();
}
return (0, import_utils.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 = (0, import_data.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 (0, import_utils.getValueFromObjectPath)(
blockType.supports,
feature,
defaultSupports
);
};
function hasBlockSupport(state, nameOrType, feature, defaultSupports) {
return !!getBlockSupport(state, nameOrType, feature, defaultSupports);
}
function getNormalizedSearchTerm(term) {
return (0, import_remove_accents.default)(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) => {
(0, import_deprecated.default)("__experimentalHasContentRoleAttribute", {
since: "6.7",
version: "6.8",
hint: "This is a private selector."
});
return (0, import_private_selectors.hasContentRoleAttribute)(...args);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
__experimentalHasContentRoleAttribute,
getActiveBlockVariation,
getBlockStyles,
getBlockSupport,
getBlockType,
getBlockTypes,
getBlockVariations,
getCategories,
getChildBlockNames,
getCollections,
getDefaultBlockName,
getDefaultBlockVariation,
getFreeformFallbackBlockName,
getGroupingBlockName,
getUnregisteredFallbackBlockName,
hasBlockSupport,
hasChildBlocks,
hasChildBlocksWithInserterSupport,
isMatchingSearchTerm
});
//# sourceMappingURL=selectors.js.map