From 018a4b4534db0e27de298d57c66424f75d7bf402 Mon Sep 17 00:00:00 2001 From: Jiuqing Song Date: Fri, 22 Sep 2023 15:30:02 -0700 Subject: [PATCH] Content Model: Clear table selection fix (#2086) * Content Model: Clear table selection fix * fix build --- .../lib/modelApi/selection/setSelection.ts | 9 ++- .../modelApi/selection/setSelectionTest.ts | 76 +++++++++++++++++++ 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/packages-content-model/roosterjs-content-model-editor/lib/modelApi/selection/setSelection.ts b/packages-content-model/roosterjs-content-model-editor/lib/modelApi/selection/setSelection.ts index 08a61d52246..da1be47aa27 100644 --- a/packages-content-model/roosterjs-content-model-editor/lib/modelApi/selection/setSelection.ts +++ b/packages-content-model/roosterjs-content-model-editor/lib/modelApi/selection/setSelection.ts @@ -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); } diff --git a/packages-content-model/roosterjs-content-model-editor/test/modelApi/selection/setSelectionTest.ts b/packages-content-model/roosterjs-content-model-editor/test/modelApi/selection/setSelectionTest.ts index d16ee9a219c..3f43b3c1fde 100644 --- a/packages-content-model/roosterjs-content-model-editor/test/modelApi/selection/setSelectionTest.ts +++ b/packages-content-model/roosterjs-content-model-editor/test/modelApi/selection/setSelectionTest.ts @@ -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(); + }); });