@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
195 lines (193 loc) • 6.96 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/editor/src/components/post-template/hooks.js
var hooks_exports = {};
__export(hooks_exports, {
useAllowSwitchingTemplates: () => useAllowSwitchingTemplates,
useAvailableTemplates: () => useAvailableTemplates,
useCurrentTemplateSlug: () => useCurrentTemplateSlug,
useEditedPostContext: () => useEditedPostContext,
usePostTemplatePanelMode: () => usePostTemplatePanelMode
});
module.exports = __toCommonJS(hooks_exports);
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_core_data = require("@wordpress/core-data");
var import_i18n = require("@wordpress/i18n");
var import_store = require("../../store/index.cjs");
function useEditedPostContext() {
return (0, import_data.useSelect)((select) => {
const { getCurrentPostId, getCurrentPostType } = select(import_store.store);
return {
postId: getCurrentPostId(),
postType: getCurrentPostType()
};
}, []);
}
function useAllowSwitchingTemplates() {
const { postType, postId } = useEditedPostContext();
return (0, import_data.useSelect)(
(select) => {
const { canUser, getEntityRecord, getEntityRecords } = select(import_core_data.store);
const siteSettings = canUser("read", {
kind: "root",
name: "site"
}) ? getEntityRecord("root", "site") : void 0;
const isPostsPage = +postId === siteSettings?.page_for_posts;
const isFrontPage = postType === "page" && +postId === siteSettings?.page_on_front;
const templates = isFrontPage ? getEntityRecords("postType", "wp_template", {
per_page: -1
}) : [];
const hasFrontPage = isFrontPage && !!templates?.some(({ slug }) => slug === "front-page");
return !isPostsPage && !hasFrontPage;
},
[postId, postType]
);
}
function useTemplates(postType) {
return (0, import_data.useSelect)(
(select) => select(import_core_data.store).getEntityRecords("postType", "wp_template", {
per_page: -1,
post_type: postType
// We look at the combined templates for now (old endpoint)
// because posts only accept slugs for templates, not IDs.
}),
[postType]
);
}
function useAvailableTemplates() {
const { postType, postId } = useEditedPostContext();
const [postSlug] = (0, import_core_data.useEntityProp)("postType", postType, "slug", postId);
const currentTemplateSlug = useCurrentTemplateSlug();
const allowSwitchingTemplate = useAllowSwitchingTemplates();
const templates = useTemplates(postType);
const defaultTemplate = (0, import_data.useSelect)(
(select) => {
if (!window?.__experimentalDataFormInspector) {
return null;
}
if (!currentTemplateSlug) {
return null;
}
const { getDefaultTemplateId, getEntityRecord } = select(import_core_data.store);
let slug;
if (postSlug) {
slug = postType === "page" ? `${postType}-${postSlug}` : `single-${postType}-${postSlug}`;
} else {
slug = postType === "page" ? "page" : `single-${postType}`;
}
const templateId = getDefaultTemplateId({ slug });
if (!templateId) {
return null;
}
return getEntityRecord("postType", "wp_template", templateId);
},
[currentTemplateSlug, postSlug, postType]
);
return (0, import_element.useMemo)(
() => allowSwitchingTemplate && [
...(templates || []).filter(
(template) => template.is_custom && template.slug !== currentTemplateSlug && !!template.content.raw
// Skip empty templates.
),
defaultTemplate && {
...defaultTemplate,
title: {
rendered: (0, import_i18n.sprintf)(
// translators: %s: Template name
(0, import_i18n.__)("%s (default)"),
defaultTemplate.title.rendered
)
},
// That's extra custom prop in order to update to an empty template
// when we select the default template.
isDefault: true
}
].filter(Boolean),
[
templates,
defaultTemplate,
currentTemplateSlug,
allowSwitchingTemplate
]
);
}
function usePostTemplatePanelMode() {
return (0, import_data.useSelect)((select) => {
const { getEditorSettings, getCurrentTemplateId, getCurrentPostType } = select(import_store.store);
const { getPostType, canUser } = select(import_core_data.store);
const postTypeSlug = getCurrentPostType();
const postType = getPostType(postTypeSlug);
const settings = getEditorSettings();
const isBlockTheme = settings.__unstableIsBlockBasedTheme;
const hasTemplates = !!settings.availableTemplates && Object.keys(settings.availableTemplates).length > 0;
let isVisible;
if (!postType?.viewable) {
isVisible = false;
} else if (hasTemplates) {
isVisible = true;
} else if (!settings.supportsTemplateMode) {
isVisible = false;
} else {
isVisible = canUser("create", {
kind: "postType",
name: "wp_template"
}) ?? false;
}
const canViewTemplates = isVisible ? !!canUser("read", {
kind: "postType",
name: "wp_template"
}) : false;
if ((!isBlockTheme || !canViewTemplates) && isVisible) {
return "classic";
}
if (isBlockTheme && !!getCurrentTemplateId()) {
return "block-theme";
}
return null;
}, []);
}
function useCurrentTemplateSlug() {
const { postType, postId } = useEditedPostContext();
const templates = useTemplates(postType);
const entityTemplate = (0, import_data.useSelect)(
(select) => {
const post = select(import_core_data.store).getEditedEntityRecord(
"postType",
postType,
postId
);
return post?.template;
},
[postType, postId]
);
if (!entityTemplate) {
return;
}
return templates?.find((template) => template.slug === entityTemplate)?.slug;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useAllowSwitchingTemplates,
useAvailableTemplates,
useCurrentTemplateSlug,
useEditedPostContext,
usePostTemplatePanelMode
});
//# sourceMappingURL=hooks.cjs.map