Skip to content

Commit

Permalink
Content Model: Clear table selection fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Sep 20, 2023
1 parent 5cef7ba commit 4aadb3d
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 cell_0_0 = createTableCell();
const cell_0_1 = createTableCell();
const cell_0_2 = createTableCell();
const cell_1_0 = createTableCell();
const cell_1_1 = createTableCell();
const cell_1_2 = createTableCell();
const cell_2_0 = createTableCell();
const cell_2_1 = createTableCell();
const cell_2_2 = createTableCell();

table.rows[0].cells.push(cell_0_0, cell_0_1, cell_0_2);
table.rows[1].cells.push(cell_1_0, cell_1_1, cell_1_2);
table.rows[2].cells.push(cell_2_0, cell_2_1, cell_2_2);

cell_0_0.isSelected = true;
cell_0_1.isSelected = true;
cell_1_0.isSelected = true;
cell_1_1.isSelected = true;

model.blocks.push(table);

setSelection(model, cell_1_1, cell_2_2);

expect(cell_0_0.isSelected).toBeFalsy();
expect(cell_0_1.isSelected).toBeFalsy();
expect(cell_0_2.isSelected).toBeFalsy();
expect(cell_1_0.isSelected).toBeFalsy();
expect(cell_1_1.isSelected).toBeTrue();
expect(cell_1_2.isSelected).toBeTrue();
expect(cell_2_0.isSelected).toBeFalsy();
expect(cell_2_1.isSelected).toBeTrue();
expect(cell_2_2.isSelected).toBeTrue();
});

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

const cell_0_0 = createTableCell();
const cell_0_1 = createTableCell();
const cell_0_2 = createTableCell();
const cell_1_0 = createTableCell();
const cell_1_1 = createTableCell();
const cell_1_2 = createTableCell();
const cell_2_0 = createTableCell();
const cell_2_1 = createTableCell();
const cell_2_2 = createTableCell();

table.rows[0].cells.push(cell_0_0, cell_0_1, cell_0_2);
table.rows[1].cells.push(cell_1_0, cell_1_1, cell_1_2);
table.rows[2].cells.push(cell_2_0, cell_2_1, cell_2_2);

cell_0_0.isSelected = true;
cell_0_1.isSelected = true;
cell_1_0.isSelected = true;
cell_1_1.isSelected = true;

model.blocks.push(table);

setSelection(model);

expect(cell_0_0.isSelected).toBeFalsy();
expect(cell_0_1.isSelected).toBeFalsy();
expect(cell_0_2.isSelected).toBeFalsy();
expect(cell_1_0.isSelected).toBeFalsy();
expect(cell_1_1.isSelected).toBeFalsy();
expect(cell_1_2.isSelected).toBeFalsy();
expect(cell_2_0.isSelected).toBeFalsy();
expect(cell_2_1.isSelected).toBeFalsy();
expect(cell_2_2.isSelected).toBeFalsy();
});
});

0 comments on commit 4aadb3d

Please sign in to comment.