@wordpress/blocks
Version:
Block API for WordPress.
329 lines (327 loc) • 11.5 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/blocks/src/api/factory.js
var factory_exports = {};
__export(factory_exports, {
__experimentalCloneSanitizedBlock: () => __experimentalCloneSanitizedBlock,
cloneBlock: () => cloneBlock,
createBlock: () => createBlock,
createBlocksFromInnerBlocksTemplate: () => createBlocksFromInnerBlocksTemplate,
findTransform: () => findTransform,
getBlockFromExample: () => getBlockFromExample,
getBlockTransforms: () => getBlockTransforms,
getPossibleBlockTransformations: () => getPossibleBlockTransformations,
isContainerGroupBlock: () => isContainerGroupBlock,
isWildcardBlockTransform: () => isWildcardBlockTransform,
switchToBlockType: () => switchToBlockType
});
module.exports = __toCommonJS(factory_exports);
var import_uuid = require("uuid");
var import_hooks = require("@wordpress/hooks");
var import_registration = require("./registration");
var import_utils = require("./utils");
function createBlock(name, attributes = {}, innerBlocks = []) {
if (!(0, import_utils.isBlockRegistered)(name)) {
return createBlock("core/missing", {
originalName: name,
originalContent: "",
originalUndelimitedContent: ""
});
}
const sanitizedAttributes = (0, import_utils.__experimentalSanitizeBlockAttributes)(
name,
attributes
);
const clientId = (0, import_uuid.v4)();
return {
clientId,
name,
isValid: true,
attributes: sanitizedAttributes,
innerBlocks
};
}
function createBlocksFromInnerBlocksTemplate(innerBlocksOrTemplate = []) {
return innerBlocksOrTemplate.map((innerBlock) => {
const innerBlockTemplate = Array.isArray(innerBlock) ? innerBlock : [
innerBlock.name,
innerBlock.attributes,
innerBlock.innerBlocks
];
const [name, attributes, innerBlocks = []] = innerBlockTemplate;
return createBlock(
name,
attributes,
createBlocksFromInnerBlocksTemplate(innerBlocks)
);
});
}
function __experimentalCloneSanitizedBlock(block, mergeAttributes = {}, newInnerBlocks) {
const { name } = block;
if (!(0, import_utils.isBlockRegistered)(name)) {
return createBlock("core/missing", {
originalName: name,
originalContent: "",
originalUndelimitedContent: ""
});
}
const clientId = (0, import_uuid.v4)();
const sanitizedAttributes = (0, import_utils.__experimentalSanitizeBlockAttributes)(name, {
...block.attributes,
...mergeAttributes
});
return {
...block,
clientId,
attributes: sanitizedAttributes,
innerBlocks: newInnerBlocks || block.innerBlocks.map(
(innerBlock) => __experimentalCloneSanitizedBlock(innerBlock)
)
};
}
function cloneBlock(block, mergeAttributes = {}, newInnerBlocks) {
const clientId = (0, import_uuid.v4)();
return {
...block,
clientId,
attributes: {
...block.attributes,
...mergeAttributes
},
innerBlocks: newInnerBlocks || block.innerBlocks.map((innerBlock) => cloneBlock(innerBlock))
};
}
var isPossibleTransformForSource = (transform, direction, blocks) => {
if (!blocks.length) {
return false;
}
const isMultiBlock = blocks.length > 1;
const firstBlockName = blocks[0].name;
const isValidForMultiBlocks = isWildcardBlockTransform(transform) || !isMultiBlock || transform.isMultiBlock;
if (!isValidForMultiBlocks) {
return false;
}
if (!isWildcardBlockTransform(transform) && !blocks.every((block) => block.name === firstBlockName)) {
return false;
}
const isBlockType = transform.type === "block";
if (!isBlockType) {
return false;
}
const sourceBlock = blocks[0];
const hasMatchingName = direction !== "from" || transform.blocks.indexOf(sourceBlock.name) !== -1 || isWildcardBlockTransform(transform);
if (!hasMatchingName) {
return false;
}
if (!isMultiBlock && direction === "from" && isContainerGroupBlock(sourceBlock.name) && isContainerGroupBlock(transform.blockName)) {
return false;
}
if (!maybeCheckTransformIsMatch(transform, blocks)) {
return false;
}
return true;
};
var getBlockTypesForPossibleFromTransforms = (blocks) => {
if (!blocks.length) {
return [];
}
const allBlockTypes = (0, import_registration.getBlockTypes)();
const blockTypesWithPossibleFromTransforms = allBlockTypes.filter(
(blockType) => {
const fromTransforms = getBlockTransforms("from", blockType.name);
return !!findTransform(fromTransforms, (transform) => {
return isPossibleTransformForSource(
transform,
"from",
blocks
);
});
}
);
return blockTypesWithPossibleFromTransforms;
};
var getBlockTypesForPossibleToTransforms = (blocks) => {
if (!blocks.length) {
return [];
}
const sourceBlock = blocks[0];
const blockType = (0, import_registration.getBlockType)(sourceBlock.name);
const transformsTo = blockType ? getBlockTransforms("to", blockType.name) : [];
const possibleTransforms = transformsTo.filter((transform) => {
return transform && isPossibleTransformForSource(transform, "to", blocks);
});
const blockNames = possibleTransforms.map((transformation) => transformation.blocks).flat();
return blockNames.map(import_registration.getBlockType);
};
var isWildcardBlockTransform = (t) => t && t.type === "block" && Array.isArray(t.blocks) && t.blocks.includes("*");
var isContainerGroupBlock = (name) => name === (0, import_registration.getGroupingBlockName)();
function getPossibleBlockTransformations(blocks) {
if (!blocks.length) {
return [];
}
const blockTypesForFromTransforms = getBlockTypesForPossibleFromTransforms(blocks);
const blockTypesForToTransforms = getBlockTypesForPossibleToTransforms(blocks);
return [
.../* @__PURE__ */ new Set([
...blockTypesForFromTransforms,
...blockTypesForToTransforms
])
];
}
function findTransform(transforms, predicate) {
const hooks = (0, import_hooks.createHooks)();
for (let i = 0; i < transforms.length; i++) {
const candidate = transforms[i];
if (predicate(candidate)) {
hooks.addFilter(
"transform",
"transform/" + i.toString(),
(result) => result ? result : candidate,
candidate.priority
);
}
}
return hooks.applyFilters("transform", null);
}
function getBlockTransforms(direction, blockTypeOrName) {
if (blockTypeOrName === void 0) {
return (0, import_registration.getBlockTypes)().map(({ name }) => getBlockTransforms(direction, name)).flat();
}
const blockType = (0, import_utils.normalizeBlockType)(blockTypeOrName);
const { name: blockName, transforms } = blockType || {};
if (!transforms || !Array.isArray(transforms[direction])) {
return [];
}
const usingMobileTransformations = transforms.supportedMobileTransforms && Array.isArray(transforms.supportedMobileTransforms);
const filteredTransforms = usingMobileTransformations ? transforms[direction].filter((t) => {
if (t.type === "raw") {
return true;
}
if (t.type === "prefix") {
return true;
}
if (!t.blocks || !t.blocks.length) {
return false;
}
if (isWildcardBlockTransform(t)) {
return true;
}
return t.blocks.every(
(transformBlockName) => transforms.supportedMobileTransforms.includes(
transformBlockName
)
);
}) : transforms[direction];
return filteredTransforms.map((transform) => ({
...transform,
blockName,
usingMobileTransformations
}));
}
function maybeCheckTransformIsMatch(transform, blocks) {
if (typeof transform.isMatch !== "function") {
return true;
}
const sourceBlock = blocks[0];
const attributes = transform.isMultiBlock ? blocks.map((block2) => block2.attributes) : sourceBlock.attributes;
const block = transform.isMultiBlock ? blocks : sourceBlock;
return transform.isMatch(attributes, block);
}
function switchToBlockType(blocks, name) {
const blocksArray = Array.isArray(blocks) ? blocks : [blocks];
const isMultiBlock = blocksArray.length > 1;
const firstBlock = blocksArray[0];
const sourceName = firstBlock.name;
const transformationsFrom = getBlockTransforms("from", name);
const transformationsTo = getBlockTransforms("to", sourceName);
const transformation = findTransform(
transformationsTo,
(t) => t.type === "block" && (isWildcardBlockTransform(t) || t.blocks.indexOf(name) !== -1) && (!isMultiBlock || t.isMultiBlock) && maybeCheckTransformIsMatch(t, blocksArray)
) || findTransform(
transformationsFrom,
(t) => t.type === "block" && (isWildcardBlockTransform(t) || t.blocks.indexOf(sourceName) !== -1) && (!isMultiBlock || t.isMultiBlock) && maybeCheckTransformIsMatch(t, blocksArray)
);
if (!transformation) {
return null;
}
let transformationResults;
if (transformation.isMultiBlock) {
if ("__experimentalConvert" in transformation) {
transformationResults = transformation.__experimentalConvert(blocksArray);
} else {
transformationResults = transformation.transform(
blocksArray.map((currentBlock) => currentBlock.attributes),
blocksArray.map((currentBlock) => currentBlock.innerBlocks)
);
}
} else if ("__experimentalConvert" in transformation) {
transformationResults = transformation.__experimentalConvert(firstBlock);
} else {
transformationResults = transformation.transform(
firstBlock.attributes,
firstBlock.innerBlocks
);
}
if (transformationResults === null || typeof transformationResults !== "object") {
return null;
}
transformationResults = Array.isArray(transformationResults) ? transformationResults : [transformationResults];
if (transformationResults.some(
(result) => !(0, import_registration.getBlockType)(result.name)
)) {
return null;
}
const hasSwitchedBlock = transformationResults.some(
(result) => result.name === name
);
if (!hasSwitchedBlock) {
return null;
}
const ret = transformationResults.map((result, index, results) => {
return (0, import_hooks.applyFilters)(
"blocks.switchToBlockType.transformedBlock",
result,
blocks,
index,
results
);
});
return ret;
}
var getBlockFromExample = (name, example) => createBlock(
name,
example.attributes,
(example.innerBlocks ?? []).map(
(innerBlock) => getBlockFromExample(innerBlock.name, innerBlock)
)
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
__experimentalCloneSanitizedBlock,
cloneBlock,
createBlock,
createBlocksFromInnerBlocksTemplate,
findTransform,
getBlockFromExample,
getBlockTransforms,
getPossibleBlockTransformations,
isContainerGroupBlock,
isWildcardBlockTransform,
switchToBlockType
});
//# sourceMappingURL=factory.js.map