Skip to content

Commit

Permalink
Content Model: Clear table selection fix (#2086)
Browse files Browse the repository at this point in the history
* Content Model: Clear table selection fix

* fix build
  • Loading branch information
JiuqingSong authored Sep 22, 2023
1 parent 0e6ac85 commit 018a4b4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,15 @@ function setSelectionToTable(
start: Selectable | null,
end: Selectable | null
): boolean {
const notFoundCo: Coordinates = { x: -1, y: -1 };
const startCo = findCell(table, start);
const endCo = end ? findCell(table, end) : startCo;
const { x: firstX, y: firstY } = startCo ?? notFoundCo;
const { x: lastX, y: lastY } = (end ? findCell(table, end) : startCo) ?? notFoundCo;

if (!isInSelection && startCo && endCo) {
if (!isInSelection) {
for (let row = 0; row < table.rows.length; row++) {
for (let col = 0; col < table.rows[row].cells.length; col++) {
const isSelected =
row >= startCo.y && row <= endCo.y && col >= startCo.x && col <= endCo.x;
const isSelected = row >= firstY && row <= lastY && col >= firstX && col <= lastX;

setIsSelected(table.rows[row].cells[col], isSelected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,4 +725,80 @@ describe('setSelection', () => {
],
});
});

it('Update table selection', () => {
const model = createContentModelDocument();
const table = createTable(3);

const cell00 = createTableCell();
const cell01 = createTableCell();
const cell02 = createTableCell();
const cell10 = createTableCell();
const cell11 = createTableCell();
const cell12 = createTableCell();
const cell20 = createTableCell();
const cell21 = createTableCell();
const cell22 = createTableCell();

table.rows[0].cells.push(cell00, cell01, cell02);
table.rows[1].cells.push(cell10, cell11, cell12);
table.rows[2].cells.push(cell20, cell21, cell22);

cell00.isSelected = true;
cell01.isSelected = true;
cell10.isSelected = true;
cell11.isSelected = true;

model.blocks.push(table);

setSelection(model, cell11, cell22);

expect(cell00.isSelected).toBeFalsy();
expect(cell01.isSelected).toBeFalsy();
expect(cell02.isSelected).toBeFalsy();
expect(cell10.isSelected).toBeFalsy();
expect(cell11.isSelected).toBeTrue();
expect(cell12.isSelected).toBeTrue();
expect(cell20.isSelected).toBeFalsy();
expect(cell21.isSelected).toBeTrue();
expect(cell22.isSelected).toBeTrue();
});

it('Clear table selection', () => {
const model = createContentModelDocument();
const table = createTable(3);

const cell00 = createTableCell();
const cell01 = createTableCell();
const cell02 = createTableCell();
const cell10 = createTableCell();
const cell11 = createTableCell();
const cell12 = createTableCell();
const cell20 = createTableCell();
const cell21 = createTableCell();
const cell22 = createTableCell();

table.rows[0].cells.push(cell00, cell01, cell02);
table.rows[1].cells.push(cell10, cell11, cell12);
table.rows[2].cells.push(cell20, cell21, cell22);

cell00.isSelected = true;
cell01.isSelected = true;
cell10.isSelected = true;
cell11.isSelected = true;

model.blocks.push(table);

setSelection(model);

expect(cell00.isSelected).toBeFalsy();
expect(cell01.isSelected).toBeFalsy();
expect(cell02.isSelected).toBeFalsy();
expect(cell10.isSelected).toBeFalsy();
expect(cell11.isSelected).toBeFalsy();
expect(cell12.isSelected).toBeFalsy();
expect(cell20.isSelected).toBeFalsy();
expect(cell21.isSelected).toBeFalsy();
expect(cell22.isSelected).toBeFalsy();
});
});

0 comments on commit 018a4b4

Please sign in to comment.