@wordpress/block-library
Version:
Block library for the WordPress editor.
91 lines (89 loc) • 3.31 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/term-description/use-term-description.js
var use_term_description_exports = {};
__export(use_term_description_exports, {
useTermDescription: () => useTermDescription
});
module.exports = __toCommonJS(use_term_description_exports);
var import_core_data = require("@wordpress/core-data");
var import_data = require("@wordpress/data");
function useTermDescription(termId, taxonomy) {
const [description, setDescription, fullDescription] = (0, import_core_data.useEntityProp)(
"taxonomy",
taxonomy,
"description",
termId
);
const templateBasedData = useTemplateBasedTermData();
const hasContext = Boolean(termId && taxonomy);
return {
hasContext,
setDescription,
termDescription: hasContext ? fullDescription?.rendered || description || "" : templateBasedData
};
}
function useTemplateBasedTermData() {
const templateSlug = (0, import_data.useSelect)((select) => {
const { getCurrentPostId, getCurrentPostType, getCurrentTemplateId } = select("core/editor");
const currentPostType = getCurrentPostType();
const templateId = getCurrentTemplateId() || (currentPostType === "wp_template" ? getCurrentPostId() : null);
return templateId ? select(import_core_data.store).getEditedEntityRecord(
"postType",
"wp_template",
templateId
)?.slug : null;
}, []);
const taxonomyMatches = templateSlug?.match(
/^(category|tag|taxonomy-([^-]+))$|^(((category|tag)|taxonomy-([^-]+))-(.+))$/
);
let taxonomy;
let termSlug;
if (taxonomyMatches) {
if (taxonomyMatches[1]) {
taxonomy = taxonomyMatches[2] ? taxonomyMatches[2] : taxonomyMatches[1];
} else if (taxonomyMatches[3]) {
taxonomy = taxonomyMatches[6] ? taxonomyMatches[6] : taxonomyMatches[4];
termSlug = taxonomyMatches[7];
}
taxonomy = taxonomy === "tag" ? "post_tag" : taxonomy;
}
return (0, import_data.useSelect)(
(select) => {
if (!taxonomy || !termSlug) {
return "";
}
const { getEntityRecords } = select(import_core_data.store);
const termRecords = getEntityRecords("taxonomy", taxonomy, {
slug: termSlug,
per_page: 1
});
if (termRecords && termRecords[0]) {
return termRecords[0].description || "";
}
return "";
},
[taxonomy, termSlug]
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useTermDescription
});
//# sourceMappingURL=use-term-description.js.map