Skip to content
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

Content Model: Clear table selection fix #2086

Merged
merged 6 commits into from
Sep 22, 2023
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
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();
});
});