@wordpress/block-library
Version:
Block library for the WordPress editor.
98 lines (97 loc) • 3.96 kB
JavaScript
import { useCallback } from "@wordpress/element";
import { useBlockBindingsUtils, useBlockEditingMode } from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import { store } from "@wordpress/core-data";
// packages/block-library/src/navigation-link/shared/use-entity-binding.js
/**
* Builds entity binding configuration for navigation link URLs.
* This function generates the structure used to bind navigation link URLs to their entity sources.
*
* Using a function instead of a constant allows for future enhancements where the binding
* might need dynamic data (e.g., entity ID, context-specific arguments).
*
* @param {('post-type'|'taxonomy')} kind - The kind of entity. Only 'post-type' and 'taxonomy' are supported.
* @return {Object} Entity binding configuration object
* @throws {Error} If kind is not 'post-type' or 'taxonomy'
*/
function buildNavigationLinkEntityBinding(kind) {
if (kind === void 0) throw new Error("buildNavigationLinkEntityBinding requires a kind parameter. Only \"post-type\" and \"taxonomy\" are supported.");
if (kind !== "post-type" && kind !== "taxonomy") throw new Error(`Invalid kind "${kind}" provided to buildNavigationLinkEntityBinding. Only 'post-type' and 'taxonomy' are supported.`);
return { url: {
source: kind === "taxonomy" ? "core/term-data" : "core/post-data",
args: { field: "link" }
} };
}
/**
* Shared hook for entity binding functionality in Navigation blocks.
*
* This hook provides common entity binding logic that can be used by both
* Navigation Link and Navigation Submenu blocks to maintain feature parity.
*
* @param {Object} props - Hook parameters
* @param {string} props.clientId - Block client ID
* @param {Object} props.attributes - Block attributes
* @return {Object} Hook return value
*/
function useEntityBinding({ clientId, attributes }) {
const { updateBlockBindings } = useBlockBindingsUtils(clientId);
const { metadata, id, kind, type } = attributes;
const blockEditingMode = useBlockEditingMode();
const hasUrlBinding = !!metadata?.bindings?.url && !!id;
const expectedSource = kind === "post-type" ? "core/post-data" : "core/term-data";
const hasCorrectBinding = hasUrlBinding && metadata?.bindings?.url?.source === expectedSource;
const { isBoundEntityAvailable, entityRecord } = useSelect((select) => {
if (!hasCorrectBinding || !id) return {
isBoundEntityAvailable: false,
entityRecord: null
};
const isPostType = kind === "post-type";
const isTaxonomy = kind === "taxonomy";
if (!isPostType && !isTaxonomy) return {
isBoundEntityAvailable: false,
entityRecord: null
};
if (blockEditingMode === "disabled") return {
isBoundEntityAvailable: true,
entityRecord: null
};
const { getEntityRecord, hasFinishedResolution } = select(store);
const entityType = isTaxonomy ? "taxonomy" : "postType";
const typeForAPI = type === "tag" ? "post_tag" : type;
const record = getEntityRecord(entityType, typeForAPI, id);
return {
isBoundEntityAvailable: hasFinishedResolution("getEntityRecord", [
entityType,
typeForAPI,
id
]) ? record !== void 0 : true,
entityRecord: record || null
};
}, [
kind,
type,
id,
hasCorrectBinding,
blockEditingMode
]);
return {
hasUrlBinding: hasCorrectBinding,
isBoundEntityAvailable,
entityRecord,
clearBinding: useCallback(() => {
if (hasUrlBinding) updateBlockBindings({ url: void 0 });
}, [updateBlockBindings, hasUrlBinding]),
createBinding: useCallback((updatedAttributes) => {
const kindToUse = updatedAttributes?.kind ?? kind;
if (!kindToUse) return;
try {
const binding = buildNavigationLinkEntityBinding(kindToUse);
updateBlockBindings(binding);
} catch (error) {
console.warn("Failed to create entity binding:", error.message);
}
}, [updateBlockBindings, kind])
};
}
export { buildNavigationLinkEntityBinding, useEntityBinding };
//# sourceMappingURL=use-entity-binding.mjs.map