Skip to content
Open
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
3 changes: 3 additions & 0 deletions packages/block-library/src/table/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export function updateSelectedCell( state, selection, updateCell ) {
}

return {
...row,
cells: row.cells.map(
( cellAttributes, columnIndex ) => {
const cellLocation = {
Expand Down Expand Up @@ -248,6 +249,7 @@ export function insertColumn( state, { columnIndex } ) {
}

return {
...row,
cells: [
...row.cells.slice( 0, columnIndex ),
{
Expand Down Expand Up @@ -290,6 +292,7 @@ export function deleteColumn( state, { columnIndex } ) {
sectionName,
section
.map( ( row ) => ( {
...row,
cells:
row.cells.length >= columnIndex
? row.cells.filter(
Expand Down
119 changes: 119 additions & 0 deletions packages/block-library/src/table/test/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,30 @@ describe( 'insertColumn', () => {
expect( state ).toEqual( expected );
} );

it( 'preserves symbol row and cell properties when inserting a column', () => {
const syncId = Symbol( 'syncId' );
const tableWithSymbolIdentity = deepFreeze( {
body: [
{
[ syncId ]: 'row-1',
cells: [
{
[ syncId ]: 'cell-1',
content: 'test',
tag: 'td',
},
],
},
],
} );
const state = insertColumn( tableWithSymbolIdentity, {
columnIndex: 0,
} );

expect( state.body[ 0 ][ syncId ] ).toBe( 'row-1' );
expect( state.body[ 0 ].cells[ 1 ][ syncId ] ).toBe( 'cell-1' );
} );

it( 'adds `th` cells to the head', () => {
const state = insertColumn( tableWithHead, {
columnIndex: 1,
Expand Down Expand Up @@ -774,6 +798,34 @@ describe( 'deleteColumn', () => {
expect( state ).toEqual( expected );
} );

it( 'preserves symbol row and cell properties when deleting a column', () => {
const syncId = Symbol( 'syncId' );
const tableWithSymbolIdentity = deepFreeze( {
body: [
{
[ syncId ]: 'row-1',
cells: [
{
content: 'remove',
tag: 'td',
},
{
[ syncId ]: 'cell-2',
content: 'keep',
tag: 'td',
},
],
},
],
} );
const state = deleteColumn( tableWithSymbolIdentity, {
columnIndex: 0,
} );

expect( state.body[ 0 ][ syncId ] ).toBe( 'row-1' );
expect( state.body[ 0 ].cells[ 0 ][ syncId ] ).toBe( 'cell-2' );
} );

it( 'should delete all rows when only one column present', () => {
const tableWithOneColumn = {
body: [
Expand Down Expand Up @@ -1306,6 +1358,73 @@ describe( 'updateSelectedCell', () => {
} );
} );

it( 'preserves unknown row properties when updating a cell', () => {
const tableWithRowIdentity = deepFreeze( {
body: [
{
__unstableSyncId: 'row-1',
cells: [
{
content: '',
tag: 'td',
},
],
},
],
} );
const cellSelection = {
type: 'cell',
sectionName: 'body',
rowIndex: 0,
columnIndex: 0,
};
const updated = updateSelectedCell(
tableWithRowIdentity,
cellSelection,
( cell ) => ( {
...cell,
content: 'test',
} )
);

expect( updated.body[ 0 ].__unstableSyncId ).toBe( 'row-1' );
} );

it( 'preserves symbol row and cell properties when updating a cell', () => {
const syncId = Symbol( 'syncId' );
const tableWithSymbolIdentity = deepFreeze( {
body: [
{
[ syncId ]: 'row-1',
cells: [
{
[ syncId ]: 'cell-1',
content: '',
tag: 'td',
},
],
},
],
} );
const cellSelection = {
type: 'cell',
sectionName: 'body',
rowIndex: 0,
columnIndex: 0,
};
const updated = updateSelectedCell(
tableWithSymbolIdentity,
cellSelection,
( cell ) => ( {
...cell,
content: 'test',
} )
);

expect( updated.body[ 0 ][ syncId ] ).toBe( 'row-1' );
expect( updated.body[ 0 ].cells[ 0 ][ syncId ] ).toBe( 'cell-1' );
} );

it( 'updates every cell in the column when the selection type is `column`', () => {
const cellSelection = { type: 'column', columnIndex: 1 };
const updated = updateSelectedCell(
Expand Down
17 changes: 17 additions & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,23 @@ async function loadPostTypeEntities() {
syncedProperties
),

/**
* Extract load-time CRDT changes that can safely be written into the
* local editor state without treating the initial CRDT seed as the
* source of truth for server-provided default/template content.
*
* @param {import('@wordpress/sync').CRDTDoc} crdtDoc
* @param {import('@wordpress/sync').ObjectData} editedRecord
* @return {Partial< import('@wordpress/sync').ObjectData >} Changes to record
*/
getHydrationChangesFromCRDTDoc: ( crdtDoc, editedRecord ) =>
getPostChangesFromCRDTDoc(
crdtDoc,
editedRecord,
syncedProperties,
{ runtimeBlockChangesOnly: true }
),

/**
* Extract changes from a CRDT document that can be used to update the
* local editor state.
Expand Down
Loading
Loading