@@ -9,14 +9,15 @@ import { capitalCase, pascalCase } from 'change-case';
99import apiFetch from '@wordpress/api-fetch' ;
1010import { __unstableSerializeAndClean , parse } from '@wordpress/blocks' ;
1111import { __ } from '@wordpress/i18n' ;
12- import { RichTextData } from '@wordpress/rich-text' ;
1312
1413/**
1514 * Internal dependencies
1615 */
1716import {
17+ applyPostChangesToCRDTDoc ,
1818 defaultApplyChangesToCRDTDoc ,
1919 defaultGetChangesFromCRDTDoc ,
20+ getPostChangesFromCRDTDoc ,
2021} from './utils/crdt' ;
2122
2223export const DEFAULT_ENTITY_KEY = 'id' ;
@@ -256,29 +257,6 @@ export const prePersistPostType = ( persistedRecord, edits ) => {
256257 return newEdits ;
257258} ;
258259
259- const serialisableBlocksCache = new WeakMap ( ) ;
260-
261- function makeBlockAttributesSerializable ( attributes ) {
262- const newAttributes = { ...attributes } ;
263- for ( const [ key , value ] of Object . entries ( attributes ) ) {
264- if ( value instanceof RichTextData ) {
265- newAttributes [ key ] = value . valueOf ( ) ;
266- }
267- }
268- return newAttributes ;
269- }
270-
271- function makeBlocksSerializable ( blocks ) {
272- return blocks . map ( ( block ) => {
273- const { innerBlocks, attributes, ...rest } = block ;
274- return {
275- ...rest ,
276- attributes : makeBlockAttributesSerializable ( attributes ) ,
277- innerBlocks : makeBlocksSerializable ( innerBlocks ) ,
278- } ;
279- } ) ;
280- }
281-
282260/**
283261 * Returns the list of post type entities.
284262 *
@@ -293,7 +271,41 @@ async function loadPostTypeEntities() {
293271 name
294272 ) ;
295273 const namespace = postType ?. rest_namespace ?? 'wp/v2' ;
296- const syncedProperties = new Set ( [ 'blocks' ] ) ;
274+
275+ /**
276+ * @type {import('@wordpress/sync').SyncConfig }
277+ */
278+ const syncConfig = {
279+ /**
280+ * Apply changes from the local editor to the local CRDT document so
281+ * that those changes can be synced to other peers (via the provider).
282+ *
283+ * @param {import('@wordpress/sync').CRDTDoc } crdtDoc
284+ * @param {Partial< import('@wordpress/sync').ObjectData > } changes
285+ * @return {void }
286+ */
287+ applyChangesToCRDTDoc : ( crdtDoc , changes ) =>
288+ applyPostChangesToCRDTDoc ( crdtDoc , changes , postType ) ,
289+
290+ /**
291+ * Extract changes from a CRDT document that can be used to update the
292+ * local editor state.
293+ *
294+ * @param {import('@wordpress/sync').CRDTDoc } crdtDoc
295+ * @param {import('@wordpress/sync').ObjectData } editedRecord
296+ * @return {Partial< import('@wordpress/sync').ObjectData > } Changes to record
297+ */
298+ getChangesFromCRDTDoc : ( crdtDoc , editedRecord ) =>
299+ getPostChangesFromCRDTDoc ( crdtDoc , editedRecord , postType ) ,
300+
301+ /**
302+ * Sync features supported by the entity.
303+ *
304+ * @type {Record< string, boolean > }
305+ */
306+ supports : { } ,
307+ } ;
308+
297309 return {
298310 kind : 'postType' ,
299311 baseURL : `/${ namespace } /${ postType . rest_base } ` ,
@@ -314,58 +326,7 @@ async function loadPostTypeEntities() {
314326 : String ( record . id ) ) ,
315327 __unstablePrePersist : isTemplate ? undefined : prePersistPostType ,
316328 __unstable_rest_base : postType . rest_base ,
317- syncConfig : {
318- /**
319- * Apply changes from the local editor to the local CRDT document so
320- * that those changes can be synced to other peers (via the provider).
321- *
322- * @param {import('@wordpress/sync').CRDTDoc } crdtDoc
323- * @param {Partial< import('@wordpress/sync').ObjectData > } changes
324- * @return {void }
325- */
326- applyChangesToCRDTDoc : ( crdtDoc , changes ) => {
327- const document = crdtDoc . getMap ( 'document' ) ;
328-
329- Object . entries ( changes ) . forEach ( ( [ key , value ] ) => {
330- if ( ! syncedProperties . has ( key ) ) {
331- return ;
332- }
333-
334- if ( typeof value !== 'function' ) {
335- if ( key === 'blocks' ) {
336- if ( ! serialisableBlocksCache . has ( value ) ) {
337- serialisableBlocksCache . set (
338- value ,
339- makeBlocksSerializable ( value )
340- ) ;
341- }
342-
343- value = serialisableBlocksCache . get ( value ) ;
344- }
345-
346- if ( document . get ( key ) !== value ) {
347- document . set ( key , value ) ;
348- }
349- }
350- } ) ;
351- } ,
352-
353- /**
354- * Extract changes from a CRDT document that can be used to update the
355- * local editor state.
356- *
357- * @param {import('@wordpress/sync').CRDTDoc } crdtDoc
358- * @return {Partial< import('@wordpress/sync').ObjectData > } Changes to record
359- */
360- getChangesFromCRDTDoc : defaultGetChangesFromCRDTDoc ,
361-
362- /**
363- * Sync features supported by the entity.
364- *
365- * @type {Record< string, boolean > }
366- */
367- supports : { } ,
368- } ,
329+ syncConfig,
369330 supportsPagination : true ,
370331 getRevisionsUrl : ( parentId , revisionId ) =>
371332 `/${ namespace } /${
0 commit comments