UNPKG

@wordpress/block-library

Version:
115 lines (114 loc) • 5.27 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_escape_html = require("@wordpress/escape-html"); let _wordpress_url = require("@wordpress/url"); // packages/block-library/src/navigation-link/shared/update-attributes.js /** * Determines if an entity link should be severed based on URL changes. * * @param {string} originalUrl - The original URL * @param {string} newUrl - The new URL * @return {boolean} True if the entity link should be severed */ const shouldSeverEntityLink = (originalUrl, newUrl) => { if (!originalUrl || !newUrl) return false; const normalizePath = (path) => { if (!path) return ""; return path.replace(/\/+$/, ""); }; const createUrlObject = (url, baseUrl = null) => { try { const base = baseUrl || (typeof window !== "undefined" ? window.location.origin : "https://wordpress.org"); return new URL(url, base); } catch { return null; } }; const originalUrlObj = createUrlObject(originalUrl); if (!originalUrlObj) return true; const newUrlObj = createUrlObject(newUrl, originalUrl); if (!newUrlObj) return true; const originalHostname = originalUrlObj.hostname; const newHostname = newUrlObj.hostname; const originalPath = normalizePath((0, _wordpress_url.getPath)(originalUrlObj.toString())); const newPath = normalizePath((0, _wordpress_url.getPath)(newUrlObj.toString())); if (originalHostname !== newHostname || originalPath !== newPath) return true; const originalP = originalUrlObj.searchParams.get("p"); const newP = newUrlObj.searchParams.get("p"); if (originalP && newP && originalP !== newP) return true; const originalPageId = originalUrlObj.searchParams.get("page_id"); const newPageId = newUrlObj.searchParams.get("page_id"); if (originalPageId && newPageId && originalPageId !== newPageId) return true; if (originalP && newPageId || originalPageId && newP) return true; return false; }; /** * @typedef {'post-type'|'custom'|'taxonomy'|'post-type-archive'} WPNavigationLinkKind */ /** * Navigation Link Block Attributes * * @typedef {Object} WPNavigationLinkBlockAttributes * * @property {string} [label] Link text. * @property {WPNavigationLinkKind} [kind] Kind is used to differentiate between term and post ids to check post draft status. * @property {string} [type] The type such as post, page, tag, category and other custom types. * @property {string} [rel] The relationship of the linked URL. * @property {number} [id] A post or term id. * @property {boolean} [opensInNewTab] Sets link target to _blank when true. * @property {string} [url] Link href. * @property {string} [title] Link title attribute. */ /** * Link Control onChange handler that updates block attributes when a setting is changed. * * @param {Object} updatedValue New block attributes to update. * @param {Function} setAttributes Block attribute update function. * @param {WPNavigationLinkBlockAttributes} blockAttributes Current block attributes. */ const updateAttributes = (updatedValue = {}, setAttributes, blockAttributes = {}) => { const { label: originalLabel = "", kind: originalKind = "", type: originalType = "" } = blockAttributes; const { title: newLabel = "", label: newLabelFromLabel = "", url: newUrl, opensInNewTab, id: newID, kind: newKind = originalKind, type: newType = originalType } = updatedValue; const finalNewLabel = newLabel || newLabelFromLabel; const newLabelWithoutHttp = finalNewLabel.replace(/http(s?):\/\//gi, ""); const newUrlWithoutHttp = newUrl?.replace(/http(s?):\/\//gi, "") ?? ""; const label = finalNewLabel && finalNewLabel !== originalLabel && newLabelWithoutHttp !== newUrlWithoutHttp ? (0, _wordpress_escape_html.escapeHTML)(finalNewLabel) : originalLabel || (0, _wordpress_escape_html.escapeHTML)(newUrlWithoutHttp); const type = newType === "post_tag" ? "tag" : newType.replace("-", "_"); const isBuiltInType = [ "post", "page", "tag", "category" ].indexOf(type) > -1; const kind = !newKind && !isBuiltInType || newKind === "custom" ? "custom" : newKind; const attributes = { ...newUrl !== void 0 ? { url: newUrl ? encodeURI((0, _wordpress_url.safeDecodeURI)(newUrl)) : newUrl } : {}, ...label && { label }, ...void 0 !== opensInNewTab && { opensInNewTab }, ...kind && { kind }, ...type && type !== "URL" && { type } }; if (newUrl && !newID && blockAttributes.id) { if (shouldSeverEntityLink(blockAttributes.url, newUrl)) { attributes.id = void 0; attributes.kind = "custom"; attributes.type = "custom"; } } else if (newID && Number.isInteger(newID)) attributes.id = newID; else if (blockAttributes.id) { attributes.kind = kind; attributes.type = type; } setAttributes(attributes); const finalId = "id" in attributes ? attributes.id : blockAttributes.id; const finalKind = "kind" in attributes ? attributes.kind : blockAttributes.kind; return { isEntityLink: !!finalId && finalKind !== "custom", attributes }; }; exports.updateAttributes = updateAttributes; //# sourceMappingURL=update-attributes.cjs.map