@@ -13,6 +13,7 @@ import { __ } from '@wordpress/i18n';
1313/**
1414 * Internal dependencies
1515 */
16+ import { getSyncManager } from './sync' ;
1617import {
1718 applyPostChangesToCRDTDoc ,
1819 defaultApplyChangesToCRDTDoc ,
@@ -236,16 +237,23 @@ export const additionalEntityConfigLoaders = [
236237] ;
237238
238239/**
239- * Returns a function to be used to retrieve extra edits to apply before persisting a post type.
240+ * Apply extra edits before persisting a post type.
240241 *
241- * @param {Object } persistedRecord Already persisted Post
242- * @param {Object } edits Edits.
242+ * @param {Object } persistedRecord Already persisted Post
243+ * @param {Object } edits Edits.
244+ * @param {string } name Post type name.
245+ * @param {boolean } isTemplate Whether the post type is a template.
243246 * @return {Object } Updated edits.
244247 */
245- export const prePersistPostType = ( persistedRecord , edits ) => {
248+ export const prePersistPostType = (
249+ persistedRecord ,
250+ edits ,
251+ name ,
252+ isTemplate
253+ ) => {
246254 const newEdits = { } ;
247255
248- if ( persistedRecord ?. status === 'auto-draft' ) {
256+ if ( ! isTemplate && persistedRecord ?. status === 'auto-draft' ) {
249257 // Saving an auto-draft should create a draft by default.
250258 if ( ! edits . status && ! newEdits . status ) {
251259 newEdits . status = 'draft' ;
@@ -262,6 +270,19 @@ export const prePersistPostType = ( persistedRecord, edits ) => {
262270 }
263271 }
264272
273+ // Add meta for persisted CRDT document.
274+ if ( persistedRecord && window . __experimentalEnableSync ) {
275+ if ( globalThis . IS_GUTENBERG_PLUGIN ) {
276+ const objectType = `postType/${ name } ` ;
277+ const objectId = persistedRecord . id ;
278+ const meta = getSyncManager ( ) ?. createMeta ( objectType , objectId ) ;
279+ newEdits . meta = {
280+ ...edits . meta ,
281+ ...meta ,
282+ } ;
283+ }
284+ }
285+
265286 return newEdits ;
266287} ;
267288
@@ -298,7 +319,8 @@ async function loadPostTypeEntities() {
298319 ( isTemplate
299320 ? capitalCase ( record . slug ?? '' )
300321 : String ( record . id ) ) ,
301- __unstablePrePersist : isTemplate ? undefined : prePersistPostType ,
322+ __unstablePrePersist : ( persistedRecord , edits ) =>
323+ prePersistPostType ( persistedRecord , edits , name , isTemplate ) ,
302324 __unstable_rest_base : postType . rest_base ,
303325 supportsPagination : true ,
304326 getRevisionsUrl : ( parentId , revisionId ) =>
@@ -347,7 +369,9 @@ async function loadPostTypeEntities() {
347369 *
348370 * @type {Record< string, boolean > }
349371 */
350- supports : { } ,
372+ supports : {
373+ crdtPersistence : true ,
374+ } ,
351375 } ;
352376 }
353377 }
0 commit comments