@wordpress/block-library
Version:
Block library for the WordPress editor.
76 lines (75 loc) • 3.26 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_element = require("@wordpress/element");
let _wordpress_data = require("@wordpress/data");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_dom = require("@wordpress/dom");
let _wordpress_escape_html = require("@wordpress/escape-html");
let __update_attributes_js = require("./update-attributes.cjs");
let __use_entity_binding_js = require("./use-entity-binding.cjs");
// packages/block-library/src/navigation-link/shared/use-handle-link-change.js
/**
* Custom hook that returns a callback for handling link selection/change.
* Manages the transition between entity links and custom links,
* including proper binding creation and cleanup.
*
* @param {Object} options - Configuration options
* @param {string} options.clientId - Block client ID
* @param {Object} options.attributes - Current block attributes
* @param {Function} options.setAttributes - Standard setAttribute function
* @param {boolean} options.allowTextUpdate - Whether this control can update the link text
* @return {Function} Callback function to handle link changes
*/
function useHandleLinkChange({ clientId, attributes, setAttributes, allowTextUpdate = false }) {
const { updateBlockAttributes } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
const { hasUrlBinding, createBinding, clearBinding } = (0, __use_entity_binding_js.useEntityBinding)({
clientId,
attributes
});
return (0, _wordpress_element.useCallback)((updatedLink) => {
if (!updatedLink) return;
const settingsUpdateAttributes = updatedLink.opensInNewTab !== void 0 ? { opensInNewTab: updatedLink.opensInNewTab } : {};
const attrs = {
url: updatedLink.url,
kind: updatedLink.kind,
type: updatedLink.type,
id: updatedLink.id,
...settingsUpdateAttributes
};
const currentText = attributes.label ? (0, _wordpress_dom.__unstableStripHTML)(attributes.label) : "";
const updatedText = updatedLink.title ?? "";
const hasTextUpdate = allowTextUpdate && updatedLink.title !== void 0 && updatedText !== currentText;
const textUpdateAttributes = hasTextUpdate ? { label: (0, _wordpress_escape_html.escapeHTML)(updatedText) } : {};
if (!attributes.label || attributes.label === "" || hasTextUpdate) attrs.title = updatedLink.title;
if (!updatedLink.id && hasUrlBinding) {
clearBinding();
updateBlockAttributes(clientId, {
url: updatedLink.url,
kind: "custom",
type: "custom",
id: void 0,
...settingsUpdateAttributes,
...textUpdateAttributes
});
} else {
const { isEntityLink, attributes: updatedAttributes } = (0, __update_attributes_js.updateAttributes)(attrs, setAttributes, attributes);
if (isEntityLink) createBinding(updatedAttributes);
else clearBinding();
if (Object.keys(textUpdateAttributes).length) updateBlockAttributes(clientId, textUpdateAttributes);
}
}, [
attributes,
allowTextUpdate,
clientId,
hasUrlBinding,
createBinding,
clearBinding,
setAttributes,
updateBlockAttributes
]);
}
exports.useHandleLinkChange = useHandleLinkChange;
//# sourceMappingURL=use-handle-link-change.cjs.map