Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 38 additions & 46 deletions packages/editor/src/components/sidebar/dataform-post-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
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';
Expand Down Expand Up @@ -174,15 +174,26 @@ function bindFieldToNamespace( field, namespace, isVisible = () => true ) {
}

export default function DataFormPostSummary( { onActionPerformed } ) {
const { postType, postId, isPostStatusRemoved } = useSelect( ( select ) => {
const { getCurrentPostType, getCurrentPostId, isEditorPanelRemoved } =
select( editorStore );
return {
postType: getCurrentPostType(),
postId: getCurrentPostId(),
isPostStatusRemoved: isEditorPanelRemoved( 'post-status' ),
};
}, [] );
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,
Expand All @@ -207,27 +218,14 @@ export default function DataFormPostSummary( { onActionPerformed } ) {

const templatePanelMode = usePostTemplatePanelMode();

// Assemble every piece of supplementary data merged into the form `data`
// alongside the post record: read-only editor data that the post's own
// fields consume (e.g. the `template` field's `available_templates` in
// classic themes), and the records of other entities targeted by namespaced
// fields (keyed by `${ kind }_${ name }`) together with the id used to
// persist edits back to each one.
const { entityData, entityIds, availableTemplates } = useSelect(
const entityRecords = useSelect(
( select ) => {
const { getEditedEntityRecord, canUser, getCurrentTheme } =
select( coreDataStore );
const { getEditedEntityRecord, canUser } = select( coreDataStore );

const _availableTemplates = getCurrentTheme()?.is_block_theme
? null
: select( editorStore ).getEditorSettings()
.availableTemplates ?? {};

const extra = {};
const ids = {};
const records = {};

// Other entities the current post type needs merged into its form.
for ( const [ key, entity ] of Object.entries(
for ( const [ namespace, entity ] of Object.entries(
ENTITIES[ postType ] ?? {}
) ) {
if (
Expand All @@ -243,39 +241,32 @@ export default function DataFormPostSummary( { onActionPerformed } ) {
if ( entity.getId && ! id ) {
continue;
}
extra[ key ] = getEditedEntityRecord(
records[ namespace ] = getEditedEntityRecord(
entity.kind,
entity.name,
id
);
ids[ key ] = id;
}

return {
entityData: extra,
entityIds: ids,
availableTemplates: _availableTemplates,
};
return records;
},
[ postType ]
);

// Merge the supplementary data onto the record only when there is any.
// Merge the supplementary data onto the record.
const data = useMemo( () => {
if ( ! record ) {
return record;
}
const extra = { ...entityData };
const extra = { ...entityRecords };
if ( availableTemplates && Object.keys( availableTemplates ).length ) {
extra.available_templates = availableTemplates;
}
if ( ! Object.keys( extra ).length ) {
return record;
}
return { ...record, ...extra };
}, [ record, entityData, availableTemplates ] );
}, [ record, entityRecords, availableTemplates ] );

const { editEntityRecord } = useDispatch( coreDataStore );
const registry = useRegistry();

// Map of namespaced field id to the namespace key its entity is merged under.
const fieldNamespaces = useMemo( () => {
Expand Down Expand Up @@ -354,12 +345,13 @@ export default function DataFormPostSummary( { onActionPerformed } ) {
for ( const [ key, value ] of Object.entries( edits ) ) {
const entity = entities[ key ];
if ( entity ) {
editEntityRecord(
entity.kind,
entity.name,
entityIds[ key ],
value
);
// Resolve the id the same way it was resolved to read the
// record, so the save targets the right entity regardless of
// its key field (`undefined` for the `root/site` singleton).
const id = entity.getId
? entity.getId( registry.select )
: undefined;
editEntityRecord( entity.kind, entity.name, id, value );
} else {
baseEdits[ key ] = value;
}
Expand Down
Loading