UNPKG

@wordpress/block-editor

Version:
96 lines (94 loc) 3.26 kB
"use strict"; 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/block-editor/src/hooks/metadata.js var metadata_exports = {}; __export(metadata_exports, { addMetaAttribute: () => addMetaAttribute, addTransforms: () => addTransforms }); module.exports = __toCommonJS(metadata_exports); var import_hooks = require("@wordpress/hooks"); var import_blocks = require("@wordpress/blocks"); var META_ATTRIBUTE_NAME = "metadata"; function addMetaAttribute(blockTypeSettings) { if (blockTypeSettings?.attributes?.[META_ATTRIBUTE_NAME]?.type) { return blockTypeSettings; } blockTypeSettings.attributes = { ...blockTypeSettings.attributes, [META_ATTRIBUTE_NAME]: { type: "object" } }; return blockTypeSettings; } function addTransforms(result, source, index, results) { if (results.length === 1 && result.innerBlocks.length === source.length) { return result; } if (results.length === 1 && source.length > 1 || results.length > 1 && source.length === 1) { return result; } if (results.length > 1 && source.length > 1 && results.length !== source.length) { return result; } const sourceMetadata = source[index]?.attributes?.metadata; if (!sourceMetadata) { return result; } const preservedMetadata = {}; if (sourceMetadata.noteId && !result.attributes?.metadata?.noteId) { preservedMetadata.noteId = sourceMetadata.noteId; } if (sourceMetadata.name && !result.attributes?.metadata?.name && (0, import_blocks.hasBlockSupport)(result.name, "renaming", true)) { preservedMetadata.name = sourceMetadata.name; } if (sourceMetadata.blockVisibility !== void 0 && !result.attributes?.metadata?.blockVisibility && (0, import_blocks.hasBlockSupport)(result.name, "visibility", true)) { preservedMetadata.blockVisibility = sourceMetadata.blockVisibility; } if (Object.keys(preservedMetadata).length > 0) { return { ...result, attributes: { ...result.attributes, metadata: { ...result.attributes.metadata, ...preservedMetadata } } }; } return result; } (0, import_hooks.addFilter)( "blocks.registerBlockType", "core/metadata/addMetaAttribute", addMetaAttribute ); (0, import_hooks.addFilter)( "blocks.switchToBlockType.transformedBlock", "core/metadata/addTransforms", addTransforms ); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { addMetaAttribute, addTransforms }); //# sourceMappingURL=metadata.js.map