@wordpress/blocks
Version:
Block API for WordPress.
295 lines (293 loc) • 11 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/api/utils.js
var utils_exports = {};
__export(utils_exports, {
__experimentalGetBlockAttributesNamesByRole: () => __experimentalGetBlockAttributesNamesByRole,
__experimentalSanitizeBlockAttributes: () => __experimentalSanitizeBlockAttributes,
getAccessibleBlockLabel: () => getAccessibleBlockLabel,
getBlockAttributesNamesByRole: () => getBlockAttributesNamesByRole,
getBlockLabel: () => getBlockLabel,
getDefault: () => getDefault,
isBlockRegistered: () => isBlockRegistered,
isContentBlock: () => isContentBlock,
isUnmodifiedBlock: () => isUnmodifiedBlock,
isUnmodifiedDefaultBlock: () => isUnmodifiedDefaultBlock,
isValidIcon: () => isValidIcon,
normalizeBlockType: () => normalizeBlockType,
normalizeIconObject: () => normalizeIconObject,
omit: () => omit
});
module.exports = __toCommonJS(utils_exports);
var import_colord = require("colord");
var import_names = __toESM(require("colord/plugins/names"));
var import_a11y = __toESM(require("colord/plugins/a11y"));
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_dom = require("@wordpress/dom");
var import_rich_text = require("@wordpress/rich-text");
var import_deprecated = __toESM(require("@wordpress/deprecated"));
var import_constants = require("./constants");
var import_registration = require("./registration");
(0, import_colord.extend)([import_names.default, import_a11y.default]);
var ICON_COLORS = ["#191e23", "#f8f9f9"];
function isUnmodifiedBlock(block, role) {
const blockAttributes = (0, import_registration.getBlockType)(block.name)?.attributes ?? {};
const attributesByRole = role ? Object.entries(blockAttributes).filter(([key, definition]) => {
if (role === "content" && key === "metadata") {
return Object.keys(block.attributes[key]?.bindings ?? {}).length > 0;
}
return definition.role === role || definition.__experimentalRole === role;
}) : [];
const attributesToCheck = !!attributesByRole.length ? attributesByRole : Object.entries(blockAttributes);
return attributesToCheck.every(([key, definition]) => {
const value = block.attributes[key];
if (definition.hasOwnProperty("default")) {
return value === definition.default;
}
if (definition.type === "rich-text") {
return !value?.length;
}
return value === void 0;
});
}
function isUnmodifiedDefaultBlock(block, role) {
return block.name === (0, import_registration.getDefaultBlockName)() && isUnmodifiedBlock(block, role);
}
function isValidIcon(icon) {
return !!icon && (typeof icon === "string" || (0, import_element.isValidElement)(icon) || typeof icon === "function" || icon instanceof import_element.Component);
}
function normalizeIconObject(icon) {
icon = icon || import_constants.BLOCK_ICON_DEFAULT;
if (isValidIcon(icon)) {
return { src: icon };
}
if ("background" in icon) {
const colordBgColor = (0, import_colord.colord)(icon.background);
const getColorContrast = (iconColor) => colordBgColor.contrast(iconColor);
const maxContrast = Math.max(...ICON_COLORS.map(getColorContrast));
return {
...icon,
foreground: icon.foreground ? icon.foreground : ICON_COLORS.find(
(iconColor) => getColorContrast(iconColor) === maxContrast
),
shadowColor: colordBgColor.alpha(0.3).toRgbString()
};
}
return icon;
}
function normalizeBlockType(blockTypeOrName) {
if (typeof blockTypeOrName === "string") {
return (0, import_registration.getBlockType)(blockTypeOrName);
}
return blockTypeOrName;
}
function getBlockLabel(blockType, attributes, context = "visual") {
const { __experimentalLabel: getLabel, title } = blockType;
const label = getLabel && getLabel(attributes, { context });
if (!label) {
return title;
}
if (label.toPlainText) {
return label.toPlainText();
}
return (0, import_dom.__unstableStripHTML)(label);
}
function getAccessibleBlockLabel(blockType, attributes, position, direction = "vertical") {
const title = blockType?.title;
const label = blockType ? getBlockLabel(blockType, attributes, "accessibility") : "";
const hasPosition = position !== void 0;
const hasLabel = label && label !== title;
if (hasPosition && direction === "vertical") {
if (hasLabel) {
return (0, import_i18n.sprintf)(
/* translators: accessibility text. 1: The block title. 2: The block row number. 3: The block label.. */
(0, import_i18n.__)("%1$s Block. Row %2$d. %3$s"),
title,
position,
label
);
}
return (0, import_i18n.sprintf)(
/* translators: accessibility text. 1: The block title. 2: The block row number. */
(0, import_i18n.__)("%1$s Block. Row %2$d"),
title,
position
);
} else if (hasPosition && direction === "horizontal") {
if (hasLabel) {
return (0, import_i18n.sprintf)(
/* translators: accessibility text. 1: The block title. 2: The block column number. 3: The block label.. */
(0, import_i18n.__)("%1$s Block. Column %2$d. %3$s"),
title,
position,
label
);
}
return (0, import_i18n.sprintf)(
/* translators: accessibility text. 1: The block title. 2: The block column number. */
(0, import_i18n.__)("%1$s Block. Column %2$d"),
title,
position
);
}
if (hasLabel) {
return (0, import_i18n.sprintf)(
/* translators: accessibility text. 1: The block title. 2: The block label. */
(0, import_i18n.__)("%1$s Block. %2$s"),
title,
label
);
}
return (0, import_i18n.sprintf)(
/* translators: accessibility text. %s: The block title. */
(0, import_i18n.__)("%s Block"),
title
);
}
function getDefault(attributeSchema) {
if (attributeSchema.default !== void 0) {
return attributeSchema.default;
}
if (attributeSchema.type === "rich-text") {
return new import_rich_text.RichTextData();
}
}
function isBlockRegistered(name) {
return (0, import_registration.getBlockType)(name) !== void 0;
}
function __experimentalSanitizeBlockAttributes(name, attributes) {
const blockType = (0, import_registration.getBlockType)(name);
if (void 0 === blockType) {
throw new Error(`Block type '${name}' is not registered.`);
}
return Object.entries(blockType.attributes).reduce(
(accumulator, [key, schema]) => {
const value = attributes[key];
if (void 0 !== value) {
if (schema.type === "rich-text") {
if (value instanceof import_rich_text.RichTextData) {
accumulator[key] = value;
} else if (typeof value === "string") {
accumulator[key] = import_rich_text.RichTextData.fromHTMLString(value);
}
} else if (schema.type === "string" && value instanceof import_rich_text.RichTextData) {
accumulator[key] = value.toHTMLString();
} else {
accumulator[key] = value;
}
} else {
const _default = getDefault(schema);
if (void 0 !== _default) {
accumulator[key] = _default;
}
}
if (["node", "children"].indexOf(schema.source) !== -1) {
if (typeof accumulator[key] === "string") {
accumulator[key] = [accumulator[key]];
} else if (!Array.isArray(accumulator[key])) {
accumulator[key] = [];
}
}
return accumulator;
},
{}
);
}
function getBlockAttributesNamesByRole(name, role) {
const attributes = (0, import_registration.getBlockType)(name)?.attributes;
if (!attributes) {
return [];
}
const attributesNames = Object.keys(attributes);
if (!role) {
return attributesNames;
}
return attributesNames.filter((attributeName) => {
const attribute = attributes[attributeName];
if (attribute?.role === role) {
return true;
}
if (attribute?.__experimentalRole === role) {
(0, import_deprecated.default)("__experimentalRole attribute", {
since: "6.7",
version: "6.8",
alternative: "role attribute",
hint: `Check the block.json of the ${name} block.`
});
return true;
}
return false;
});
}
var __experimentalGetBlockAttributesNamesByRole = (...args) => {
(0, import_deprecated.default)("__experimentalGetBlockAttributesNamesByRole", {
since: "6.7",
version: "6.8",
alternative: "getBlockAttributesNamesByRole"
});
return getBlockAttributesNamesByRole(...args);
};
function isContentBlock(name) {
const blockType = (0, import_registration.getBlockType)(name);
const attributes = blockType?.attributes;
const supportsContentRole = blockType?.supports?.contentRole;
if (supportsContentRole) {
return true;
}
if (!attributes) {
return false;
}
return !!Object.keys(attributes)?.some((attributeKey) => {
const attribute = attributes[attributeKey];
return attribute?.role === "content" || attribute?.__experimentalRole === "content";
});
}
function omit(object, keys) {
return Object.fromEntries(
Object.entries(object).filter(([key]) => !keys.includes(key))
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
__experimentalGetBlockAttributesNamesByRole,
__experimentalSanitizeBlockAttributes,
getAccessibleBlockLabel,
getBlockAttributesNamesByRole,
getBlockLabel,
getDefault,
isBlockRegistered,
isContentBlock,
isUnmodifiedBlock,
isUnmodifiedDefaultBlock,
isValidIcon,
normalizeBlockType,
normalizeIconObject,
omit
});
//# sourceMappingURL=utils.js.map