@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
286 lines (285 loc) • 9.35 kB
JavaScript
// packages/editor/src/components/sidebar/dataform-post-summary.js
import { __ } from "@wordpress/i18n";
import { useDispatch, useSelect, useRegistry } from "@wordpress/data";
import { store as coreDataStore } from "@wordpress/core-data";
import { DataForm } from "@wordpress/dataviews";
import { Stack } from "@wordpress/ui";
import { useMemo } from "@wordpress/element";
import { useViewConfig } from "@wordpress/views";
import PostCardPanel from "../post-card-panel/index.mjs";
import PluginPostStatusInfo from "../plugin-post-status-info/index.mjs";
import PostPanelSection from "../post-panel-section/index.mjs";
import { store as editorStore } from "../../store/index.mjs";
import PostTrash from "../post-trash/index.mjs";
import usePostFields from "../post-fields/index.mjs";
import { usePostTemplatePanelMode } from "../post-template/hooks.mjs";
import revisionsField from "../../dataviews/fields/revisions/index.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var EMPTY_FORM = { layout: { type: "panel" }, fields: [] };
var VIEW_CONFIG_FIELDS = ["form"];
function useInspectorPanelVisibility(form) {
const {
isPostStatusRemoved,
featuredImageEnabled,
excerptEnabled,
discussionEnabled,
pageAttributesEnabled
} = useSelect((select) => {
const { isEditorPanelRemoved, isEditorPanelEnabled } = select(editorStore);
return {
isPostStatusRemoved: isEditorPanelRemoved("post-status"),
featuredImageEnabled: isEditorPanelEnabled("featured-image"),
excerptEnabled: isEditorPanelEnabled("post-excerpt"),
discussionEnabled: isEditorPanelEnabled("discussion-panel"),
pageAttributesEnabled: isEditorPanelEnabled("page-attributes")
};
}, []);
const visibleForm = useMemo(() => {
if (!form.fields?.length) {
return form;
}
const visibilityById = {
featured_media: featuredImageEnabled,
excerpt: excerptEnabled,
"post-content-info": true,
discussion: discussionEnabled && !isPostStatusRemoved,
parent: pageAttributesEnabled && !isPostStatusRemoved
};
const isFieldVisible = (id) => id in visibilityById ? visibilityById[id] : !isPostStatusRemoved;
const filterFields = (fields) => fields.reduce((acc, field) => {
const id = typeof field === "string" ? field : field.id;
if (!isFieldVisible(id)) {
return acc;
}
if (typeof field !== "string" && Array.isArray(field.children)) {
const children = filterFields(field.children);
if (!children.length) {
return acc;
}
acc.push({ ...field, children });
return acc;
}
acc.push(field);
return acc;
}, []);
return { ...form, fields: filterFields(form.fields) };
}, [
form,
isPostStatusRemoved,
featuredImageEnabled,
excerptEnabled,
discussionEnabled,
pageAttributesEnabled
]);
return visibleForm;
}
var ENTITIES = {
wp_template: {
root_site: {
kind: "root",
name: "site",
fields: ["posts_per_page", "default_comment_status"],
isVisible: (item) => ["home", "index"].includes(item.slug ?? "")
},
posttype_page: {
kind: "postType",
name: "page",
getId: (select) => select(coreDataStore).getEditedEntityRecord("root", "site")?.page_for_posts,
fields: ["posts_page_title"],
isVisible: (item) => ["home", "index"].includes(item.slug ?? "")
}
}
};
function bindFieldToNamespace(field, namespace, isVisible = () => true) {
const subItem = (item) => item?.[namespace] ?? {};
return {
...field,
getValue: ({ item }) => field.getValue ? field.getValue({ item: subItem(item) }) : subItem(item)[field.id],
setValue: ({ item, value }) => ({
[namespace]: field.setValue({ item: subItem(item), value })
}),
render: field.render ? (props) => field.render({ ...props, item: subItem(props.item) }) : void 0,
isVisible: (item) => isVisible(item) && !!item[namespace]
};
}
function DataFormPostSummary({ onActionPerformed }) {
const { postType, postId, isPostStatusRemoved, availableTemplates } = useSelect((select) => {
const {
getCurrentPostType,
getCurrentPostId,
isEditorPanelRemoved,
getEditorSettings
} = select(editorStore);
const _availableTemplates = select(
coreDataStore
).getCurrentTheme()?.is_block_theme ? null : getEditorSettings().availableTemplates ?? null;
return {
postType: getCurrentPostType(),
postId: getCurrentPostId(),
isPostStatusRemoved: isEditorPanelRemoved("post-status"),
availableTemplates: _availableTemplates
};
}, []);
const { form: formConfig } = useViewConfig({
kind: "postType",
name: postType,
fields: VIEW_CONFIG_FIELDS
});
const form = useInspectorPanelVisibility(formConfig ?? EMPTY_FORM);
const record = useSelect(
(select) => {
if (!postType || !postId) {
return null;
}
return select(coreDataStore).getEditedEntityRecord(
"postType",
postType,
postId
);
},
[postType, postId]
);
const templatePanelMode = usePostTemplatePanelMode();
const entityRecords = useSelect(
(select) => {
const { getEditedEntityRecord, canUser } = select(coreDataStore);
const records = {};
for (const [namespace, entity] of Object.entries(
ENTITIES[postType] ?? {}
)) {
if (!canUser("read", {
kind: entity.kind,
name: entity.name
})) {
continue;
}
const id = entity.getId ? entity.getId(select) : void 0;
if (entity.getId && !id) {
continue;
}
records[namespace] = getEditedEntityRecord(
entity.kind,
entity.name,
id
);
}
return records;
},
[postType]
);
const data = useMemo(() => {
if (!record) {
return record;
}
const extra = { ...entityRecords };
if (availableTemplates && Object.keys(availableTemplates).length) {
extra.available_templates = availableTemplates;
}
return { ...record, ...extra };
}, [record, entityRecords, availableTemplates]);
const { editEntityRecord } = useDispatch(coreDataStore);
const registry = useRegistry();
const fieldNamespaces = useMemo(() => {
const map = {};
for (const [namespace, entity] of Object.entries(
ENTITIES[postType] ?? {}
)) {
for (const id of entity.fields ?? []) {
map[id] = namespace;
}
}
return map;
}, [postType]);
const _fields = usePostFields({ postType });
const fields = useMemo(
() => _fields?.map((field) => {
const namespace = fieldNamespaces[field.id];
if (namespace) {
return bindFieldToNamespace(
field,
namespace,
ENTITIES[postType]?.[namespace]?.isVisible
);
}
if (field.id === "status") {
return {
...field,
elements: field.elements.filter(
(element) => element.value !== "trash"
)
};
}
if (field.id === "template") {
if (!templatePanelMode) {
return null;
}
if (templatePanelMode === "classic" && Object.keys(availableTemplates ?? {}).length === 0) {
return {
...field,
readOnly: true,
render: () => __("Default template")
};
}
return field;
}
return field;
}).filter(Boolean).concat(revisionsField),
[
_fields,
templatePanelMode,
availableTemplates,
fieldNamespaces,
postType
]
);
const onChange = (edits) => {
const entities = ENTITIES[postType] ?? {};
const baseEdits = {};
for (const [key, value] of Object.entries(edits)) {
const entity = entities[key];
if (entity) {
const id = entity.getId ? entity.getId(registry.select) : void 0;
editEntityRecord(entity.kind, entity.name, id, value);
} else {
baseEdits[key] = value;
}
}
if (!Object.keys(baseEdits).length) {
return;
}
if (baseEdits.status && baseEdits.status !== "future" && record?.status === "future" && new Date(record.date) > /* @__PURE__ */ new Date()) {
baseEdits.date = null;
}
if (baseEdits.status && baseEdits.status === "private" && record?.password) {
baseEdits.password = "";
}
editEntityRecord("postType", postType, postId, baseEdits);
};
return /* @__PURE__ */ jsx(PostPanelSection, { className: "editor-post-summary", children: /* @__PURE__ */ jsxs(Stack, { direction: "column", gap: "lg", children: [
/* @__PURE__ */ jsx(
PostCardPanel,
{
postType,
postId,
onActionPerformed
}
),
/* @__PURE__ */ jsx(
DataForm,
{
data,
fields,
form,
onChange
}
),
!isPostStatusRemoved && /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(PluginPostStatusInfo.Slot, { children: (fills) => fills.length > 0 && /* @__PURE__ */ jsx(Stack, { direction: "column", gap: "xs", children: fills }) }),
/* @__PURE__ */ jsx(PostTrash, { onActionPerformed })
] })
] }) });
}
export {
DataFormPostSummary as default
};
//# sourceMappingURL=dataform-post-summary.mjs.map