UNPKG

@wordpress/editor

Version:
317 lines (315 loc) 11.6 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/editor/src/components/sidebar/dataform-post-summary.js var dataform_post_summary_exports = {}; __export(dataform_post_summary_exports, { default: () => DataFormPostSummary }); module.exports = __toCommonJS(dataform_post_summary_exports); var import_i18n = require("@wordpress/i18n"); var import_data = require("@wordpress/data"); var import_core_data = require("@wordpress/core-data"); var import_dataviews = require("@wordpress/dataviews"); var import_ui = require("@wordpress/ui"); var import_element = require("@wordpress/element"); var import_views = require("@wordpress/views"); var import_post_card_panel = __toESM(require("../post-card-panel/index.cjs")); var import_plugin_post_status_info = __toESM(require("../plugin-post-status-info/index.cjs")); var import_post_panel_section = __toESM(require("../post-panel-section/index.cjs")); var import_store = require("../../store/index.cjs"); var import_post_trash = __toESM(require("../post-trash/index.cjs")); var import_post_fields = __toESM(require("../post-fields/index.cjs")); var import_hooks = require("../post-template/hooks.cjs"); var import_revisions = __toESM(require("../../dataviews/fields/revisions/index.cjs")); var import_jsx_runtime = require("react/jsx-runtime"); var EMPTY_FORM = { layout: { type: "panel" }, fields: [] }; var VIEW_CONFIG_FIELDS = ["form"]; function useInspectorPanelVisibility(form) { const { isPostStatusRemoved, featuredImageEnabled, excerptEnabled, discussionEnabled, pageAttributesEnabled } = (0, import_data.useSelect)((select) => { const { isEditorPanelRemoved, isEditorPanelEnabled } = select(import_store.store); return { isPostStatusRemoved: isEditorPanelRemoved("post-status"), featuredImageEnabled: isEditorPanelEnabled("featured-image"), excerptEnabled: isEditorPanelEnabled("post-excerpt"), discussionEnabled: isEditorPanelEnabled("discussion-panel"), pageAttributesEnabled: isEditorPanelEnabled("page-attributes") }; }, []); const visibleForm = (0, import_element.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(import_core_data.store).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 } = (0, import_data.useSelect)((select) => { const { getCurrentPostType, getCurrentPostId, isEditorPanelRemoved, getEditorSettings } = select(import_store.store); const _availableTemplates = select( import_core_data.store ).getCurrentTheme()?.is_block_theme ? null : getEditorSettings().availableTemplates ?? null; return { postType: getCurrentPostType(), postId: getCurrentPostId(), isPostStatusRemoved: isEditorPanelRemoved("post-status"), availableTemplates: _availableTemplates }; }, []); const { form: formConfig } = (0, import_views.useViewConfig)({ kind: "postType", name: postType, fields: VIEW_CONFIG_FIELDS }); const form = useInspectorPanelVisibility(formConfig ?? EMPTY_FORM); const record = (0, import_data.useSelect)( (select) => { if (!postType || !postId) { return null; } return select(import_core_data.store).getEditedEntityRecord( "postType", postType, postId ); }, [postType, postId] ); const templatePanelMode = (0, import_hooks.usePostTemplatePanelMode)(); const entityRecords = (0, import_data.useSelect)( (select) => { const { getEditedEntityRecord, canUser } = select(import_core_data.store); 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 = (0, import_element.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 } = (0, import_data.useDispatch)(import_core_data.store); const registry = (0, import_data.useRegistry)(); const fieldNamespaces = (0, import_element.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 = (0, import_post_fields.default)({ postType }); const fields = (0, import_element.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: () => (0, import_i18n.__)("Default template") }; } return field; } return field; }).filter(Boolean).concat(import_revisions.default), [ _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__ */ (0, import_jsx_runtime.jsx)(import_post_panel_section.default, { className: "editor-post-summary", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ui.Stack, { direction: "column", gap: "lg", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_post_card_panel.default, { postType, postId, onActionPerformed } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_dataviews.DataForm, { data, fields, form, onChange } ), !isPostStatusRemoved && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_plugin_post_status_info.default.Slot, { children: (fills) => fills.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Stack, { direction: "column", gap: "xs", children: fills }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_post_trash.default, { onActionPerformed }) ] }) ] }) }); } //# sourceMappingURL=dataform-post-summary.cjs.map