@wordpress/block-library
Version:
Block library for the WordPress editor.
128 lines (126 loc) • 4.04 kB
JavaScript
;
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/use-link-preview.js
var use_link_preview_exports = {};
__export(use_link_preview_exports, {
useLinkPreview: () => useLinkPreview
});
module.exports = __toCommonJS(use_link_preview_exports);
var import_i18n = require("@wordpress/i18n");
var import_url = require("@wordpress/url");
var import_block_editor = require("@wordpress/block-editor");
var import_lock_unlock = require("../../lock-unlock.cjs");
var { useRemoteUrlData } = (0, import_lock_unlock.unlock)(import_block_editor.privateApis);
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function computeDisplayUrl(url) {
if (!url) {
return { displayUrl: "", isExternal: false };
}
let displayUrl = (0, import_url.safeDecodeURI)(url);
let isExternal = false;
try {
const linkUrl = new URL(url);
const siteUrl = window.location.origin;
if (linkUrl.origin === siteUrl) {
let path = linkUrl.pathname + linkUrl.search + linkUrl.hash;
if (path.endsWith("/") && path.length > 1) {
path = path.slice(0, -1);
}
displayUrl = path;
} else {
isExternal = true;
}
} catch (e) {
displayUrl = (0, import_url.safeDecodeURI)(url);
}
return { displayUrl, isExternal };
}
function computeBadges({
url,
type,
isExternal,
entityStatus,
hasBinding,
isEntityAvailable
}) {
const badges = [];
if (url) {
if (isExternal) {
badges.push({
label: (0, import_i18n.__)("External link"),
intent: "default"
});
} else if (type) {
badges.push({ label: capitalize(type), intent: "default" });
}
}
if (!url) {
badges.push({ label: (0, import_i18n.__)("No link selected"), intent: "error" });
} else if (hasBinding && !isEntityAvailable) {
badges.push({ label: (0, import_i18n.__)("Deleted"), intent: "error" });
} else if (entityStatus) {
const statusMap = {
publish: { label: (0, import_i18n.__)("Published"), intent: "success" },
future: { label: (0, import_i18n.__)("Scheduled"), intent: "warning" },
draft: { label: (0, import_i18n.__)("Draft"), intent: "warning" },
pending: { label: (0, import_i18n.__)("Pending"), intent: "warning" },
private: { label: (0, import_i18n.__)("Private"), intent: "default" },
trash: { label: (0, import_i18n.__)("Trash"), intent: "error" }
};
const badge = statusMap[entityStatus];
if (badge) {
badges.push(badge);
}
}
return badges;
}
function useLinkPreview({
url,
title,
image,
type,
entityStatus,
hasBinding,
isEntityAvailable
}) {
const { richData } = useRemoteUrlData(title ? null : url);
const { displayUrl, isExternal } = computeDisplayUrl(url);
const badges = computeBadges({
url,
type,
isExternal,
entityStatus,
hasBinding,
isEntityAvailable
});
const displayTitle = url ? title || richData?.title || (0, import_url.safeDecodeURI)(url) : (0, import_i18n.__)("Add link");
return {
title: displayTitle,
url: displayUrl,
image,
badges
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useLinkPreview
});
//# sourceMappingURL=use-link-preview.cjs.map