UNPKG

@wordpress/block-library

Version:
146 lines (144 loc) 5.55 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-library/src/navigation-link/shared/update-attributes.js var update_attributes_exports = {}; __export(update_attributes_exports, { updateAttributes: () => updateAttributes }); module.exports = __toCommonJS(update_attributes_exports); var import_escape_html = require("@wordpress/escape-html"); var import_url = require("@wordpress/url"); var 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 (error) { 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, import_url.getPath)(originalUrlObj.toString())); const newPath = normalizePath((0, import_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; }; var updateAttributes = (updatedValue = {}, setAttributes, blockAttributes = {}) => { const { label: originalLabel = "", kind: originalKind = "", type: originalType = "" } = blockAttributes; const { title: newLabel = "", // the title of any provided Post. label: newLabelFromLabel = "", // alternative to title 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 useNewLabel = finalNewLabel && finalNewLabel !== originalLabel && // LinkControl without the title field relies // on the check below. Specifically, it assumes that // the URL is the same as a title. // This logic a) looks suspicious and b) should really // live in the LinkControl and not here. It's a great // candidate for future refactoring. newLabelWithoutHttp !== newUrlWithoutHttp; const label = useNewLabel ? (0, import_escape_html.escapeHTML)(finalNewLabel) : originalLabel || (0, import_escape_html.escapeHTML)(newUrlWithoutHttp); const type = newType === "post_tag" ? "tag" : newType.replace("-", "_"); const isBuiltInType = ["post", "page", "tag", "category"].indexOf(type) > -1; const isCustomLink = !newKind && !isBuiltInType || newKind === "custom"; const kind = isCustomLink ? "custom" : newKind; const attributes = { // Passed `url` may already be encoded. To prevent double encoding, decodeURI is executed to revert to the original string. ...newUrl !== void 0 ? { url: newUrl ? encodeURI((0, import_url.safeDecodeURI)(newUrl)) : newUrl } : {}, ...label && { label }, ...void 0 !== opensInNewTab && { opensInNewTab }, ...kind && { kind }, ...type && type !== "URL" && { type } }; if (newUrl && !newID && blockAttributes.id) { const shouldSever = shouldSeverEntityLink( blockAttributes.url, newUrl ); if (shouldSever) { 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 // Return the computed attributes object }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { updateAttributes }); //# sourceMappingURL=update-attributes.js.map