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
31 changes: 21 additions & 10 deletions packages/core-data/src/utils/crdt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
// Changes that can be applied to a post entity record.
export type PostChanges = Partial< Post > & {
blocks?: Block[];
content?: Post[ 'content' ] | string;
excerpt?: Post[ 'excerpt' ] | string;
selection?: WPSelection;
title?: Post[ 'title' ] | string;
Expand All @@ -53,7 +54,9 @@ export type PostChanges = Partial< Post > & {
// A post record as represented in the CRDT document (Y.Map).
export interface YPostRecord extends YMapRecord {
author: number;
blocks: YBlocks;
// Blocks are undefined when they need to be re-parsed from content.
blocks: YBlocks | undefined;
content: string;
categories: number[];
comment_status: string;
date: string | null;
Expand All @@ -74,6 +77,7 @@ export interface YPostRecord extends YMapRecord {
const allowedPostProperties = new Set< string >( [
'author',
'blocks',
'content',
'categories',
'comment_status',
'date',
Expand Down Expand Up @@ -156,6 +160,14 @@ export function applyPostChangesToCRDTDoc(

switch ( key ) {
case 'blocks': {
// Blocks are undefined when they need to be re-parsed from content.
if ( ! newValue ) {
// Set to undefined instead of deleting the key. This is important
// since we iterate over the Y.Map keys in getPostChangesFromCRDTDoc.
ymap.set( key, undefined );
break;
}

let currentBlocks = ymap.get( key );

// Initialize.
Expand All @@ -164,22 +176,20 @@ export function applyPostChangesToCRDTDoc(
ymap.set( key, currentBlocks );
}

// Block[] from local changes.
const newBlocks = ( newValue as PostChanges[ 'blocks' ] ) ?? [];

// Block changes from typing are bundled with a 'selection' update.
// Pass the resulting cursor position to the mergeCrdtBlocks function.
const cursorPosition =
changes.selection?.selectionStart?.offset ?? null;

// Merge blocks does not need `setValue` because it is operating on a
// Yjs type that is already in the Y.Doc.
mergeCrdtBlocks( currentBlocks, newBlocks, cursorPosition );
mergeCrdtBlocks( currentBlocks, newValue, cursorPosition );
break;
}

case 'content':
case 'excerpt': {
const currentValue = ymap.get( 'excerpt' );
const currentValue = ymap.get( key );
const rawNewValue = getRawValue( newValue );

updateMapValue( ymap, key, currentValue, rawNewValue );
Expand Down Expand Up @@ -318,11 +328,11 @@ export function getPostChangesFromCRDTDoc(
ydoc.meta?.get( CRDT_DOC_META_PERSISTENCE_KEY ) &&
editedRecord.content
) {
const blocks = ymap.get( 'blocks' ) as YBlocks;
const blocksJson = ymap.get( 'blocks' )?.toJSON() ?? [];

return (
__unstableSerializeAndClean(
blocks.toJSON()
).trim() !== editedRecord.content.raw.trim()
__unstableSerializeAndClean( blocksJson ).trim() !==
getRawValue( editedRecord.content )
);
}

Expand Down Expand Up @@ -375,6 +385,7 @@ export function getPostChangesFromCRDTDoc(
return haveValuesChanged( currentValue, newValue );
}

case 'content':
case 'excerpt':
case 'title': {
return haveValuesChanged(
Expand Down
111 changes: 111 additions & 0 deletions packages/core-data/src/utils/test/crdt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,46 @@ describe( 'crdt', () => {
expect( blocks ).toBeInstanceOf( Y.Array );
} );

it( 'sets blocks to undefined when blocks value is undefined', () => {
// First, set some blocks.
map.set( 'blocks', new Y.Array< YBlock >() );

const changes = {
blocks: undefined,
};

applyPostChangesToCRDTDoc( doc, changes, mockPostType );

// The key should still exist, but the value should be undefined.
expect( map.has( 'blocks' ) ).toBe( true );
expect( map.get( 'blocks' ) ).toBeUndefined();
} );

it( 'syncs content as a string', () => {
const changes = {
content: 'Hello, world!',
} as PostChanges;

applyPostChangesToCRDTDoc( doc, changes, mockPostType );

expect( map.get( 'content' ) ).toBe( 'Hello, world!' );
} );

it( 'syncs content with RenderedText format', () => {
const changes = {
content: {
raw: '<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph -->',
rendered: '<p>Hello</p>',
},
} as PostChanges;

applyPostChangesToCRDTDoc( doc, changes, mockPostType );

expect( map.get( 'content' ) ).toBe(
'<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph -->'
);
} );

it( 'syncs meta fields', () => {
const changes = {
meta: {
Expand Down Expand Up @@ -352,6 +392,77 @@ describe( 'crdt', () => {
expect( changes ).toHaveProperty( 'blocks' );
} );

it( 'includes undefined blocks in changes', () => {
map.set( 'blocks', undefined );

const editedRecord = {
blocks: [
{
name: 'core/paragraph',
attributes: { content: 'Test' },
innerBlocks: [],
},
],
} as unknown as Post;

const changes = getPostChangesFromCRDTDoc(
doc,
editedRecord,
mockPostType
);

expect( changes ).toHaveProperty( 'blocks' );
expect( changes.blocks ).toBeUndefined();
} );

it( 'detects content changes from string value', () => {
map.set( 'content', 'New content' );

const editedRecord = {
content: 'Old content',
} as unknown as Post;

const changes = getPostChangesFromCRDTDoc(
doc,
editedRecord,
mockPostType
);

expect( changes.content ).toBe( 'New content' );
} );

it( 'detects content changes from RenderedText value', () => {
map.set( 'content', 'New content' );

const editedRecord = {
content: { raw: 'Old content', rendered: 'Old content' },
} as unknown as Post;

const changes = getPostChangesFromCRDTDoc(
doc,
editedRecord,
mockPostType
);

expect( changes.content ).toBe( 'New content' );
} );

it( 'excludes content when unchanged from RenderedText value', () => {
map.set( 'content', 'Same content' );

const editedRecord = {
content: { raw: 'Same content', rendered: 'Same content' },
} as unknown as Post;

const changes = getPostChangesFromCRDTDoc(
doc,
editedRecord,
mockPostType
);

expect( changes ).not.toHaveProperty( 'content' );
} );

it( 'includes meta in changes', () => {
const metaMap = createYMap();
metaMap.set( 'public_meta', 'new value' );
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ export const restoreRevision =

// Build the edits object with all restorable fields from the revision.
const edits = {
blocks: parse( revision.content.raw ),
blocks: undefined,
content: revision.content.raw,
};
if ( revision.title?.raw !== undefined ) {
Expand Down
Loading