UNPKG

@wordpress/editor

Version:
123 lines (100 loc) 4.69 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _element = require("@wordpress/element"); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _lodash = require("lodash"); var _blocks = require("@wordpress/blocks"); var _data = require("@wordpress/data"); var _coreData = require("@wordpress/core-data"); var _compose = require("@wordpress/compose"); var _hooks = require("@wordpress/hooks"); /** * External dependencies */ /** * WordPress dependencies */ /** @typedef {import('@wordpress/compose').WPHigherOrderComponent} WPHigherOrderComponent */ /** @typedef {import('@wordpress/blocks').WPBlockSettings} WPBlockSettings */ /** * Object whose keys are the names of block attributes, where each value * represents the meta key to which the block attribute is intended to save. * * @see https://developer.wordpress.org/reference/functions/register_meta/ * * @typedef {Object<string,string>} WPMetaAttributeMapping */ /** * Given a mapping of attribute names (meta source attributes) to their * associated meta key, returns a higher order component that overrides its * `attributes` and `setAttributes` props to sync any changes with the edited * post's meta keys. * * @param {WPMetaAttributeMapping} metaAttributes Meta attribute mapping. * * @return {WPHigherOrderComponent} Higher-order component. */ const createWithMetaAttributeSource = metaAttributes => (0, _compose.createHigherOrderComponent)(BlockEdit => ({ attributes, setAttributes, ...props }) => { const postType = (0, _data.useSelect)(select => select('core/editor').getCurrentPostType(), []); const [meta, setMeta] = (0, _coreData.useEntityProp)('postType', postType, 'meta'); const mergedAttributes = (0, _element.useMemo)(() => ({ ...attributes, ...(0, _lodash.mapValues)(metaAttributes, metaKey => meta[metaKey]) }), [attributes, meta]); return (0, _element.createElement)(BlockEdit, (0, _extends2.default)({ attributes: mergedAttributes, setAttributes: nextAttributes => { const nextMeta = (0, _lodash.mapKeys)( // Filter to intersection of keys between the updated // attributes and those with an associated meta key. (0, _lodash.pickBy)(nextAttributes, (value, key) => metaAttributes[key]), // Rename the keys to the expected meta key name. (value, attributeKey) => metaAttributes[attributeKey]); if (!(0, _lodash.isEmpty)(nextMeta)) { setMeta(nextMeta); } setAttributes(nextAttributes); } }, props)); }, 'withMetaAttributeSource'); /** * Filters a registered block's settings to enhance a block's `edit` component * to upgrade meta-sourced attributes to use the post's meta entity property. * * @param {WPBlockSettings} settings Registered block settings. * * @return {WPBlockSettings} Filtered block settings. */ function shimAttributeSource(settings) { /** @type {WPMetaAttributeMapping} */ const metaAttributes = (0, _lodash.mapValues)((0, _lodash.pickBy)(settings.attributes, { source: 'meta' }), 'meta'); if (!(0, _lodash.isEmpty)(metaAttributes)) { settings.edit = createWithMetaAttributeSource(metaAttributes)(settings.edit); } return settings; } (0, _hooks.addFilter)('blocks.registerBlockType', 'core/editor/custom-sources-backwards-compatibility/shim-attribute-source', shimAttributeSource); // The above filter will only capture blocks registered after the filter was // added. There may already be blocks registered by this point, and those must // be updated to apply the shim. // // The following implementation achieves this, albeit with a couple caveats: // - Only blocks registered on the global store will be modified. // - The block settings are directly mutated, since there is currently no // mechanism to update an existing block registration. This is the reason for // `getBlockType` separate from `getBlockTypes`, since the latter returns a // _copy_ of the block registration (i.e. the mutation would not affect the // actual registered block settings). // // `getBlockTypes` or `getBlockType` implementation could change in the future // in regards to creating settings clones, but the corresponding end-to-end // tests for meta blocks should cover against any potential regressions. // // In the future, we could support updating block settings, at which point this // implementation could use that mechanism instead. (0, _data.select)(_blocks.store).getBlockTypes().map(({ name }) => (0, _data.select)(_blocks.store).getBlockType(name)).forEach(shimAttributeSource); //# sourceMappingURL=custom-sources-backwards-compatibility.js.map