UNPKG

@wordpress/block-editor

Version:
265 lines (263 loc) 9.16 kB
"use strict"; 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/block-editor/src/components/block-edit/edit.js var edit_exports = {}; __export(edit_exports, { default: () => edit_default }); module.exports = __toCommonJS(edit_exports); var import_clsx = __toESM(require("clsx")); var import_blocks = require("@wordpress/blocks"); var import_components = require("@wordpress/components"); var import_data = require("@wordpress/data"); var import_element = require("@wordpress/element"); var import_block_context = __toESM(require("../block-context")); var import_is_url_like = __toESM(require("../link-control/is-url-like")); var import_block_bindings = require("../../utils/block-bindings"); var import_lock_unlock = require("../../lock-unlock"); var import_private_block_context = require("../block-list/private-block-context"); var import_jsx_runtime = require("react/jsx-runtime"); var DEFAULT_BLOCK_CONTEXT = {}; var Edit = (props) => { const { name } = props; const blockType = (0, import_blocks.getBlockType)(name); if (!blockType) { return null; } const Component = blockType.edit || blockType.save; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { ...props }); }; var EditWithFilters = (0, import_components.withFilters)("editor.BlockEdit")(Edit); var EditWithGeneratedProps = (props) => { const { name, clientId, attributes, setAttributes } = props; const registry = (0, import_data.useRegistry)(); const blockType = (0, import_blocks.getBlockType)(name); const blockContext = (0, import_element.useContext)(import_block_context.default); const registeredSources = (0, import_data.useSelect)( (select) => (0, import_lock_unlock.unlock)(select(import_blocks.store)).getAllBlockBindingsSources(), [] ); const { bindableAttributes } = (0, import_element.useContext)(import_private_block_context.PrivateBlockContext); const { blockBindings, context, hasPatternOverrides } = (0, import_element.useMemo)(() => { const computedContext = blockType?.usesContext ? Object.fromEntries( Object.entries(blockContext).filter( ([key]) => blockType.usesContext.includes(key) ) ) : DEFAULT_BLOCK_CONTEXT; if (attributes?.metadata?.bindings) { Object.values(attributes?.metadata?.bindings || {}).forEach( (binding) => { registeredSources[binding?.source]?.usesContext?.forEach( (key) => { computedContext[key] = blockContext[key]; } ); } ); } return { blockBindings: (0, import_block_bindings.replacePatternOverridesDefaultBinding)( attributes?.metadata?.bindings, bindableAttributes ), context: computedContext, hasPatternOverrides: (0, import_block_bindings.hasPatternOverridesDefaultBinding)( attributes?.metadata?.bindings ) }; }, [ blockType?.usesContext, blockContext, attributes?.metadata?.bindings, bindableAttributes, registeredSources ]); const computedAttributes = (0, import_data.useSelect)( (select) => { if (!blockBindings) { return attributes; } const attributesFromSources = {}; const blockBindingsBySource = /* @__PURE__ */ new Map(); for (const [attributeName, binding] of Object.entries( blockBindings )) { const { source: sourceName, args: sourceArgs } = binding; const source = registeredSources[sourceName]; if (!source || !bindableAttributes?.includes(attributeName)) { continue; } blockBindingsBySource.set(source, { ...blockBindingsBySource.get(source), [attributeName]: { args: sourceArgs } }); } if (blockBindingsBySource.size) { for (const [source, bindings] of blockBindingsBySource) { let values = {}; if (!source.getValues) { Object.keys(bindings).forEach((attr) => { values[attr] = source.label; }); } else { values = source.getValues({ select, context, clientId, bindings }); } for (const [attributeName, value] of Object.entries( values )) { if (attributeName === "url" && (!value || !(0, import_is_url_like.default)(value))) { attributesFromSources[attributeName] = null; } else { attributesFromSources[attributeName] = value; } } } } return { ...attributes, ...attributesFromSources }; }, [ attributes, bindableAttributes, blockBindings, clientId, context, registeredSources ] ); const setBoundAttributes = (0, import_element.useCallback)( (nextAttributes) => { if (!blockBindings) { setAttributes(nextAttributes); return; } registry.batch(() => { const keptAttributes = { ...nextAttributes }; const blockBindingsBySource = /* @__PURE__ */ new Map(); for (const [attributeName, newValue] of Object.entries( keptAttributes )) { if (!blockBindings[attributeName] || !bindableAttributes?.includes(attributeName)) { continue; } const binding = blockBindings[attributeName]; const source = registeredSources[binding?.source]; if (!source?.setValues) { continue; } blockBindingsBySource.set(source, { ...blockBindingsBySource.get(source), [attributeName]: { args: binding.args, newValue } }); delete keptAttributes[attributeName]; } if (blockBindingsBySource.size) { for (const [ source, bindings ] of blockBindingsBySource) { source.setValues({ select: registry.select, dispatch: registry.dispatch, context, clientId, bindings }); } } const hasParentPattern = !!context["pattern/overrides"]; if ( // Don't update non-connected attributes if the block is using pattern overrides // and the editing is happening while overriding the pattern (not editing the original). !(hasPatternOverrides && hasParentPattern) && Object.keys(keptAttributes).length ) { if (hasPatternOverrides) { delete keptAttributes.caption; delete keptAttributes.href; } setAttributes(keptAttributes); } }); }, [ bindableAttributes, blockBindings, clientId, context, hasPatternOverrides, setAttributes, registeredSources, registry ] ); if (!blockType) { return null; } if (blockType.apiVersion > 1) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( EditWithFilters, { ...props, attributes: computedAttributes, context, setAttributes: setBoundAttributes } ); } const generatedClassName = (0, import_blocks.hasBlockSupport)(blockType, "className", true) ? (0, import_blocks.getBlockDefaultClassName)(name) : null; const className = (0, import_clsx.default)( generatedClassName, attributes?.className, props.className ); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( EditWithFilters, { ...props, attributes: computedAttributes, className, context, setAttributes: setBoundAttributes } ); }; var edit_default = EditWithGeneratedProps; //# sourceMappingURL=edit.js.map