-
Notifications
You must be signed in to change notification settings - Fork 4.8k
In-editor revisions: preserve client IDs #75028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/editor/src/components/post-revisions-preview/preserve-client-ids.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { diffArrays } from 'diff/lib/diff/array'; | ||
|
|
||
| /** | ||
| * Preserves clientIds from previously rendered blocks to prevent flashing. | ||
| * Uses LCS algorithm to match blocks by name. | ||
| * | ||
| * This compares the newly parsed blocks against the last rendered blocks | ||
| * to maintain React key stability. | ||
| * | ||
| * @param {Array} newBlocks Newly parsed blocks with fresh clientIds. | ||
| * @param {Array} prevBlocks Previously rendered blocks with stable clientIds. | ||
| * @return {Array} Blocks with preserved clientIds where possible. | ||
| */ | ||
| export function preserveClientIds( newBlocks, prevBlocks ) { | ||
| if ( ! prevBlocks?.length || ! newBlocks?.length ) { | ||
| return newBlocks; | ||
| } | ||
|
|
||
| // Create signatures for LCS matching using block name. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, the comment is a bit due to the terminology (LCS matching, signatures). What matters is that we'll be diffing sequences of block names. |
||
| const newSigs = newBlocks.map( ( block ) => block.name ); | ||
| const prevSigs = prevBlocks.map( ( block ) => block.name ); | ||
|
|
||
| const diffResult = diffArrays( prevSigs, newSigs ); | ||
|
|
||
| let newIndex = 0; | ||
| let prevIndex = 0; | ||
| const result = []; | ||
|
|
||
| for ( const chunk of diffResult ) { | ||
| if ( chunk.removed ) { | ||
| // Blocks only in prev render - skip them. | ||
| prevIndex += chunk.count; | ||
| } else if ( chunk.added ) { | ||
| // Blocks only in new render - keep new clientIds. | ||
| for ( let i = 0; i < chunk.count; i++ ) { | ||
| result.push( newBlocks[ newIndex++ ] ); | ||
| } | ||
| } else { | ||
| // Matched blocks - preserve clientIds from prev render. | ||
| for ( let i = 0; i < chunk.count; i++ ) { | ||
| const newBlock = newBlocks[ newIndex++ ]; | ||
| const prevBlock = prevBlocks[ prevIndex++ ]; | ||
| result.push( { | ||
| ...newBlock, | ||
| clientId: prevBlock.clientId, | ||
| innerBlocks: preserveClientIds( | ||
| newBlock.innerBlocks, | ||
| prevBlock.innerBlocks | ||
| ), | ||
| } ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
246 changes: 246 additions & 0 deletions
246
packages/editor/src/components/post-revisions-preview/test/preserve-client-ids.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { preserveClientIds } from '../preserve-client-ids'; | ||
|
|
||
| describe( 'preserveClientIds', () => { | ||
| it( 'should return newBlocks when prevBlocks is empty', () => { | ||
| const newBlocks = [ | ||
| { name: 'core/paragraph', clientId: 'new-1', attributes: {} }, | ||
| ]; | ||
| expect( preserveClientIds( newBlocks, [] ) ).toBe( newBlocks ); | ||
| expect( preserveClientIds( newBlocks, null ) ).toBe( newBlocks ); | ||
| expect( preserveClientIds( newBlocks, undefined ) ).toBe( newBlocks ); | ||
| } ); | ||
|
|
||
| it( 'should return newBlocks when newBlocks is empty', () => { | ||
| const prevBlocks = [ | ||
| { name: 'core/paragraph', clientId: 'prev-1', attributes: {} }, | ||
| ]; | ||
| expect( preserveClientIds( [], prevBlocks ) ).toEqual( [] ); | ||
| expect( preserveClientIds( null, prevBlocks ) ).toBe( null ); | ||
| expect( preserveClientIds( undefined, prevBlocks ) ).toBe( undefined ); | ||
| } ); | ||
|
|
||
| it( 'should preserve clientIds for identical blocks', () => { | ||
| const prevBlocks = [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'prev-1', | ||
| attributes: { content: 'Hello' }, | ||
| originalContent: 'Hello', | ||
| innerBlocks: [], | ||
| }, | ||
| { | ||
| name: 'core/heading', | ||
| clientId: 'prev-2', | ||
| attributes: { level: 2 }, | ||
| originalContent: 'Title', | ||
| innerBlocks: [], | ||
| }, | ||
| ]; | ||
| const newBlocks = [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'new-1', | ||
| attributes: { content: 'Hello' }, | ||
| originalContent: 'Hello', | ||
| innerBlocks: [], | ||
| }, | ||
| { | ||
| name: 'core/heading', | ||
| clientId: 'new-2', | ||
| attributes: { level: 2 }, | ||
| originalContent: 'Title', | ||
| innerBlocks: [], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = preserveClientIds( newBlocks, prevBlocks ); | ||
|
|
||
| expect( result[ 0 ].clientId ).toBe( 'prev-1' ); | ||
| expect( result[ 1 ].clientId ).toBe( 'prev-2' ); | ||
| } ); | ||
|
|
||
| it( 'should keep new clientIds for added blocks', () => { | ||
| const prevBlocks = [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'prev-1', | ||
| attributes: { content: 'First' }, | ||
| originalContent: 'First', | ||
| innerBlocks: [], | ||
| }, | ||
| ]; | ||
| const newBlocks = [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'new-1', | ||
| attributes: { content: 'First' }, | ||
| originalContent: 'First', | ||
| innerBlocks: [], | ||
| }, | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'new-2', | ||
| attributes: { content: 'Second' }, | ||
| originalContent: 'Second', | ||
| innerBlocks: [], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = preserveClientIds( newBlocks, prevBlocks ); | ||
|
|
||
| expect( result[ 0 ].clientId ).toBe( 'prev-1' ); | ||
| expect( result[ 1 ].clientId ).toBe( 'new-2' ); | ||
| } ); | ||
|
|
||
| it( 'should handle removed blocks', () => { | ||
| const prevBlocks = [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'prev-1', | ||
| attributes: { content: 'First' }, | ||
| originalContent: 'First', | ||
| innerBlocks: [], | ||
| }, | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'prev-2', | ||
| attributes: { content: 'Second' }, | ||
| originalContent: 'Second', | ||
| innerBlocks: [], | ||
| }, | ||
| ]; | ||
| const newBlocks = [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'new-2', | ||
| attributes: { content: 'Second' }, | ||
| originalContent: 'Second', | ||
| innerBlocks: [], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = preserveClientIds( newBlocks, prevBlocks ); | ||
|
|
||
| expect( result ).toHaveLength( 1 ); | ||
| // Matches by name only, so first paragraph matches first paragraph. | ||
| expect( result[ 0 ].clientId ).toBe( 'prev-1' ); | ||
| } ); | ||
|
|
||
| it( 'should preserve clientIds for inner blocks recursively', () => { | ||
| const prevBlocks = [ | ||
| { | ||
| name: 'core/group', | ||
| clientId: 'prev-group', | ||
| attributes: {}, | ||
| originalContent: '', | ||
| innerBlocks: [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'prev-inner-1', | ||
| attributes: { content: 'Inner' }, | ||
| originalContent: 'Inner', | ||
| innerBlocks: [], | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
| const newBlocks = [ | ||
| { | ||
| name: 'core/group', | ||
| clientId: 'new-group', | ||
| attributes: {}, | ||
| originalContent: '', | ||
| innerBlocks: [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'new-inner-1', | ||
| attributes: { content: 'Inner' }, | ||
| originalContent: 'Inner', | ||
| innerBlocks: [], | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = preserveClientIds( newBlocks, prevBlocks ); | ||
|
|
||
| expect( result[ 0 ].clientId ).toBe( 'prev-group' ); | ||
| expect( result[ 0 ].innerBlocks[ 0 ].clientId ).toBe( 'prev-inner-1' ); | ||
| } ); | ||
|
|
||
| it( 'should preserve clientId even when attributes differ (matches by name only)', () => { | ||
| const prevBlocks = [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'prev-1', | ||
| attributes: { content: 'Old content' }, | ||
| originalContent: 'Old content', | ||
| innerBlocks: [], | ||
| }, | ||
| ]; | ||
| const newBlocks = [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'new-1', | ||
| attributes: { content: 'New content' }, | ||
| originalContent: 'New content', | ||
| innerBlocks: [], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = preserveClientIds( newBlocks, prevBlocks ); | ||
|
|
||
| expect( result[ 0 ].clientId ).toBe( 'prev-1' ); | ||
| } ); | ||
|
|
||
| it( 'should handle blocks with same name but different content using LCS', () => { | ||
| const prevBlocks = [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'prev-a', | ||
| attributes: { content: 'A' }, | ||
| originalContent: 'A', | ||
| innerBlocks: [], | ||
| }, | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'prev-b', | ||
| attributes: { content: 'B' }, | ||
| originalContent: 'B', | ||
| innerBlocks: [], | ||
| }, | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'prev-c', | ||
| attributes: { content: 'C' }, | ||
| originalContent: 'C', | ||
| innerBlocks: [], | ||
| }, | ||
| ]; | ||
| const newBlocks = [ | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'new-a', | ||
| attributes: { content: 'A' }, | ||
| originalContent: 'A', | ||
| innerBlocks: [], | ||
| }, | ||
| { | ||
| name: 'core/paragraph', | ||
| clientId: 'new-c', | ||
| attributes: { content: 'C' }, | ||
| originalContent: 'C', | ||
| innerBlocks: [], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = preserveClientIds( newBlocks, prevBlocks ); | ||
|
|
||
| // Matches by name only, so matches in order (first to first, second to second). | ||
| expect( result[ 0 ].clientId ).toBe( 'prev-a' ); | ||
| expect( result[ 1 ].clientId ).toBe( 'prev-b' ); | ||
| } ); | ||
| } ); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is misleading, as it delegates the diffing. I'd say it's irrelevant too. :)