From 4aadb3ddb2911ed7dba7754fa2de332b01cee0ba Mon Sep 17 00:00:00 2001 From: Jiuqing Song Date: Wed, 20 Sep 2023 10:55:18 -0700 Subject: [PATCH] Content Model: Clear table selection fix --- .../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..a7033ce97fd 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 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(); + }); });