Skip to content
Merged
Show file tree
Hide file tree
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: 39 additions & 45 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,11 @@ export const deleteEntityRecord =

await dispatch( removeItems( kind, name, recordId, true ) );

if ( globalThis.IS_GUTENBERG_PLUGIN ) {
if ( entityConfig.syncConfig ) {
const objectType = `${ kind }/${ name }`;
const objectId = recordId;
if ( entityConfig.syncConfig ) {
const objectType = `${ kind }/${ name }`;
const objectId = recordId;

getSyncManager()?.unload( objectType, objectId );
}
getSyncManager()?.unload( objectType, objectId );
}
} catch ( _error ) {
hasError = true;
Expand Down Expand Up @@ -427,35 +425,33 @@ export const editEntityRecord =
return acc;
}, {} ),
};
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
if ( entityConfig.syncConfig ) {
const objectType = `${ kind }/${ name }`;
const objectId = recordId;

// Determine whether this edit should create a new undo level.
//
// In Gutenberg, block changes flow through two callbacks:
// - `onInput`: For transient/in-progress changes (e.g., typing each
// character). These use `isCached: true` and get merged into
// the current undo item.
// - `onChange`: For persistent/completed changes (e.g., formatting
// transforms, block insertions). These use `isCached: false` and
// should create a new undo level.
//
// Additionally, `undoIgnore: true` means the change should not
// affect the undo history at all (e.g., selection-only changes).
const isNewUndoLevel = options.undoIgnore
? false
: ! options.isCached;

getSyncManager()?.update(
objectType,
objectId,
editsWithMerges,
LOCAL_EDITOR_ORIGIN,
{ isNewUndoLevel }
);
}
if ( entityConfig.syncConfig ) {
const objectType = `${ kind }/${ name }`;
const objectId = recordId;

// Determine whether this edit should create a new undo level.
//
// In Gutenberg, block changes flow through two callbacks:
// - `onInput`: For transient/in-progress changes (e.g., typing each
// character). These use `isCached: true` and get merged into
// the current undo item.
// - `onChange`: For persistent/completed changes (e.g., formatting
// transforms, block insertions). These use `isCached: false` and
// should create a new undo level.
//
// Additionally, `undoIgnore: true` means the change should not
// affect the undo history at all (e.g., selection-only changes).
const isNewUndoLevel = options.undoIgnore
? false
: ! options.isCached;

getSyncManager()?.update(
objectType,
objectId,
editsWithMerges,
LOCAL_EDITOR_ORIGIN,
{ isNewUndoLevel }
);
}
if ( ! options.undoIgnore ) {
select.getUndoManager().addRecord(
Expand Down Expand Up @@ -796,16 +792,14 @@ export const saveEntityRecord =
true,
edits
);
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
if ( entityConfig.syncConfig ) {
getSyncManager()?.update(
`${ kind }/${ name }`,
recordId,
updatedRecord,
LOCAL_EDITOR_ORIGIN,
{ isSave: true }
);
}
if ( entityConfig.syncConfig ) {
getSyncManager()?.update(
`${ kind }/${ name }`,
recordId,
updatedRecord,
LOCAL_EDITOR_ORIGIN,
{ isSave: true }
);
}
}
} catch ( _error ) {
Expand Down
120 changes: 54 additions & 66 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,8 @@ export const rootEntitiesConfig = [
].map( ( entity ) => {
const syncEnabledRootEntities = new Set( [ 'comment' ] );

if ( globalThis.IS_GUTENBERG_PLUGIN ) {
if ( syncEnabledRootEntities.has( entity.name ) ) {
entity.syncConfig = defaultSyncConfig;
}
if ( syncEnabledRootEntities.has( entity.name ) ) {
entity.syncConfig = defaultSyncConfig;
}
return entity;
} );
Expand Down Expand Up @@ -295,16 +293,14 @@ export const prePersistPostType = (
}

// Add meta for persisted CRDT document.
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
if ( persistedRecord ) {
const objectType = `postType/${ name }`;
const objectId = persistedRecord.id;
const meta = getSyncManager()?.createMeta( objectType, objectId );
newEdits.meta = {
...edits.meta,
...meta,
};
}
if ( persistedRecord ) {
const objectType = `postType/${ name }`;
const objectId = persistedRecord.id;
const meta = getSyncManager()?.createMeta( objectType, objectId );
newEdits.meta = {
...edits.meta,
...meta,
};
}

return newEdits;
Expand Down Expand Up @@ -359,60 +355,54 @@ async function loadPostTypeEntities() {
: DEFAULT_ENTITY_KEY,
};

if ( globalThis.IS_GUTENBERG_PLUGIN ) {
/**
* @type {import('@wordpress/sync').SyncConfig}
*/
entity.syncConfig = {
/**
* @type {import('@wordpress/sync').SyncConfig}
* Apply changes from the local editor to the local CRDT document so
* that those changes can be synced to other peers (via the provider).
*
* @param {import('@wordpress/sync').CRDTDoc} crdtDoc
* @param {Partial< import('@wordpress/sync').ObjectData >} changes
* @return {void}
*/
entity.syncConfig = {
/**
* Apply changes from the local editor to the local CRDT document so
* that those changes can be synced to other peers (via the provider).
*
* @param {import('@wordpress/sync').CRDTDoc} crdtDoc
* @param {Partial< import('@wordpress/sync').ObjectData >} changes
* @return {void}
*/
applyChangesToCRDTDoc: ( crdtDoc, changes ) =>
applyPostChangesToCRDTDoc( crdtDoc, changes, postType ),
applyChangesToCRDTDoc: ( crdtDoc, changes ) =>
applyPostChangesToCRDTDoc( crdtDoc, changes, postType ),

/**
* Create the awareness instance for the entity's CRDT document.
*
* @param {import('@wordpress/sync').CRDTDoc} ydoc
* @param {import('@wordpress/sync').ObjectID} objectId
* @return {import('@wordpress/sync').Awareness} Awareness instance
*/
createAwareness: ( ydoc, objectId ) => {
const kind = 'postType';
const id = parseInt( objectId, 10 );
return new PostEditorAwareness( ydoc, kind, name, id );
},
/**
* Create the awareness instance for the entity's CRDT document.
*
* @param {import('@wordpress/sync').CRDTDoc} ydoc
* @param {import('@wordpress/sync').ObjectID} objectId
* @return {import('@wordpress/sync').Awareness} Awareness instance
*/
createAwareness: ( ydoc, objectId ) => {
const kind = 'postType';
const id = parseInt( objectId, 10 );
return new PostEditorAwareness( ydoc, kind, name, id );
},

/**
* Extract changes from a CRDT document that can be used to update the
* local editor state.
*
* @param {import('@wordpress/sync').CRDTDoc} crdtDoc
* @param {import('@wordpress/sync').ObjectData} editedRecord
* @return {Partial< import('@wordpress/sync').ObjectData >} Changes to record
*/
getChangesFromCRDTDoc: ( crdtDoc, editedRecord ) =>
getPostChangesFromCRDTDoc(
crdtDoc,
editedRecord,
postType
),
/**
* Extract changes from a CRDT document that can be used to update the
* local editor state.
*
* @param {import('@wordpress/sync').CRDTDoc} crdtDoc
* @param {import('@wordpress/sync').ObjectData} editedRecord
* @return {Partial< import('@wordpress/sync').ObjectData >} Changes to record
*/
getChangesFromCRDTDoc: ( crdtDoc, editedRecord ) =>
getPostChangesFromCRDTDoc( crdtDoc, editedRecord, postType ),

/**
* Sync features supported by the entity.
*
* @type {Record< string, boolean >}
*/
supports: {
crdtPersistence: true,
},
};
}
/**
* Sync features supported by the entity.
*
* @type {Record< string, boolean >}
*/
supports: {
crdtPersistence: true,
},
};

return entity;
} );
Expand All @@ -439,9 +429,7 @@ async function loadTaxonomyEntities() {
supportsPagination: true,
};

if ( globalThis.IS_GUTENBERG_PLUGIN ) {
entity.syncConfig = defaultSyncConfig;
}
entity.syncConfig = defaultSyncConfig;

return entity;
} );
Expand Down
8 changes: 2 additions & 6 deletions packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ type EntityRecordKey = string | number;
* @return The undo manager.
*/
export function getUndoManager( state: State ) {
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
// undoManager is undefined until the first sync-enabled entity is loaded.
return getSyncManager()?.undoManager ?? state.undoManager;
}

return state.undoManager;
// undoManager is undefined until the first sync-enabled entity is loaded.
return getSyncManager()?.undoManager ?? state.undoManager;
}

/**
Expand Down
Loading
Loading