Skip to content

Commit

Permalink
Fix 237733: [Table] The size of the pasted table becomes larger (#2268)
Browse files Browse the repository at this point in the history
* Fix 237733

* fix build
  • Loading branch information
JiuqingSong authored Dec 13, 2023
1 parent 0f36271 commit dd6f645
Show file tree
Hide file tree
Showing 24 changed files with 271 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { addSegment, createSelectionMarker } from 'roosterjs-content-model-dom';
import type { ContentModelTableRow } from 'roosterjs-content-model-types';
import type { TableSelectionCoordinates } from '../table/getSelectedCells';
import type {
ContentModelTableRow,
TableSelectionCoordinates,
} from 'roosterjs-content-model-types';

/**
* @internal
Expand All @@ -9,8 +11,8 @@ export function collapseTableSelection(
rows: ContentModelTableRow[],
selection: TableSelectionCoordinates
) {
const { firstCol, firstRow } = selection;
const cell = rows[firstRow]?.cells[firstCol];
const { firstColumn, firstRow } = selection;
const cell = rows[firstRow]?.cells[firstColumn];
if (cell) {
addSegment(cell, createSelectionMarker());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getSelectedCells } from './getSelectedCells';
import { updateTableCellMetadata } from 'roosterjs-content-model-core';
import { getSelectedCells, updateTableCellMetadata } from 'roosterjs-content-model-core';
import type {
ContentModelTable,
ContentModelTableCell,
Expand Down Expand Up @@ -63,7 +62,7 @@ function alignTableCellInternal(

if (sel) {
for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) {
for (let colIndex = sel.firstCol; colIndex <= sel.lastCol; colIndex++) {
for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) {
const cell = table.rows[rowIndex]?.cells[colIndex];
const format = cell?.format;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { collapseTableSelection } from '../selection/collapseTableSelection';
import { getSelectedCells } from './getSelectedCells';
import { getSelectedCells } from 'roosterjs-content-model-core';
import type { ContentModelTable } from 'roosterjs-content-model-types';

/**
Expand All @@ -10,17 +10,20 @@ export function deleteTableColumn(table: ContentModelTable) {

if (sel) {
for (let rowIndex = 0; rowIndex < table.rows.length; rowIndex++) {
const cellInNextCol = table.rows[rowIndex].cells[sel.lastCol + 1];
const cellInNextCol = table.rows[rowIndex].cells[sel.lastColumn + 1];

if (cellInNextCol) {
cellInNextCol.spanLeft =
cellInNextCol.spanLeft && table.rows[rowIndex].cells[sel.firstCol].spanLeft;
cellInNextCol.spanLeft && table.rows[rowIndex].cells[sel.firstColumn].spanLeft;
}

table.rows[rowIndex].cells.splice(sel.firstCol, sel.lastCol - sel.firstCol + 1);
table.rows[rowIndex].cells.splice(
sel.firstColumn,
sel.lastColumn - sel.firstColumn + 1
);
}

table.widths.splice(sel.firstCol, sel.lastCol - sel.firstCol + 1);
table.widths.splice(sel.firstColumn, sel.lastColumn - sel.firstColumn + 1);
collapseTableSelection(table.rows, sel);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { collapseTableSelection } from '../selection/collapseTableSelection';
import { getSelectedCells } from './getSelectedCells';
import { getSelectedCells } from 'roosterjs-content-model-core';
import type { ContentModelTable } from 'roosterjs-content-model-types';

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createTableCell } from 'roosterjs-content-model-dom';
import { getSelectedCells } from './getSelectedCells';
import { getSelectedCells } from 'roosterjs-content-model-core';
import type {
ContentModelTable,
TableHorizontalInsertOperation,
Expand All @@ -16,12 +16,12 @@ export function insertTableColumn(
const insertLeft = operation == 'insertLeft';

if (sel) {
for (let i = sel?.firstCol; i <= sel.lastCol; i++) {
for (let i = sel?.firstColumn; i <= sel.lastColumn; i++) {
table.rows.forEach(row => {
const cell = row.cells[insertLeft ? sel.firstCol : sel.lastCol];
const cell = row.cells[insertLeft ? sel.firstColumn : sel.lastColumn];

row.cells.splice(
insertLeft ? sel.firstCol : sel.lastCol + 1,
insertLeft ? sel.firstColumn : sel.lastColumn + 1,
0,
createTableCell(
cell.spanLeft,
Expand All @@ -33,9 +33,9 @@ export function insertTableColumn(
);
});
table.widths.splice(
insertLeft ? sel.firstCol : sel.lastCol + 1,
insertLeft ? sel.firstColumn : sel.lastColumn + 1,
0,
table.widths[insertLeft ? sel.firstCol : sel.lastCol]
table.widths[insertLeft ? sel.firstColumn : sel.lastColumn]
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createTableCell } from 'roosterjs-content-model-dom';
import { getSelectedCells } from './getSelectedCells';
import { getSelectedCells } from 'roosterjs-content-model-core';
import type {
ContentModelTable,
TableVerticalInsertOperation,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { canMergeCells } from './canMergeCells';
import { getSelectedCells } from './getSelectedCells';
import { getSelectedCells } from 'roosterjs-content-model-core';
import type { ContentModelTable } from 'roosterjs-content-model-types';

/**
Expand All @@ -8,13 +8,16 @@ import type { ContentModelTable } from 'roosterjs-content-model-types';
export function mergeTableCells(table: ContentModelTable) {
const sel = getSelectedCells(table);

if (sel && canMergeCells(table.rows, sel.firstRow, sel.firstCol, sel.lastRow, sel.lastCol)) {
if (
sel &&
canMergeCells(table.rows, sel.firstRow, sel.firstColumn, sel.lastRow, sel.lastColumn)
) {
for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) {
for (let colIndex = sel.firstCol; colIndex <= sel.lastCol; colIndex++) {
for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) {
const cell = table.rows[rowIndex].cells[colIndex];

if (cell) {
cell.spanLeft = colIndex > sel.firstCol;
cell.spanLeft = colIndex > sel.firstColumn;
cell.spanAbove = rowIndex > sel.firstRow;

delete cell.cachedElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { canMergeCells } from './canMergeCells';
import { getSelectedCells } from './getSelectedCells';
import { getSelectedCells } from 'roosterjs-content-model-core';
import type {
ContentModelTable,
TableHorizontalMergeOperation,
Expand All @@ -16,7 +16,7 @@ export function mergeTableColumn(
const mergeLeft = operation == 'mergeLeft';

if (sel) {
const mergingColIndex = mergeLeft ? sel.firstCol : sel.lastCol + 1;
const mergingColIndex = mergeLeft ? sel.firstColumn : sel.lastColumn + 1;

if (mergingColIndex > 0 && mergingColIndex < table.rows[0].cells.length) {
for (let rowIndex = sel.firstRow; rowIndex <= sel.lastRow; rowIndex++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { canMergeCells } from './canMergeCells';
import { getSelectedCells } from './getSelectedCells';
import { getSelectedCells } from 'roosterjs-content-model-core';
import type { ContentModelTable, TableVerticalMergeOperation } from 'roosterjs-content-model-types';

/**
Expand All @@ -13,7 +13,7 @@ export function mergeTableRow(table: ContentModelTable, operation: TableVertical
const mergingRowIndex = mergeAbove ? sel.firstRow : sel.lastRow + 1;

if (mergingRowIndex > 0 && mergingRowIndex < table.rows.length) {
for (let colIndex = sel.firstCol; colIndex <= sel.lastCol; colIndex++) {
for (let colIndex = sel.firstColumn; colIndex <= sel.lastColumn; colIndex++) {
const cell = table.rows[mergingRowIndex].cells[colIndex];

if (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createTableCell } from 'roosterjs-content-model-dom';
import { getSelectedCells } from './getSelectedCells';
import { getSelectedCells } from 'roosterjs-content-model-core';
import type { ContentModelTable } from 'roosterjs-content-model-types';

const MIN_WIDTH = 30;
Expand All @@ -11,7 +11,7 @@ export function splitTableCellHorizontally(table: ContentModelTable) {
const sel = getSelectedCells(table);

if (sel) {
for (let colIndex = sel.lastCol; colIndex >= sel.firstCol; colIndex--) {
for (let colIndex = sel.lastColumn; colIndex >= sel.firstColumn; colIndex--) {
if (
table.rows.every(
(row, rowIndex) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createTableCell } from 'roosterjs-content-model-dom';
import { getSelectedCells } from './getSelectedCells';
import { getSelectedCells } from 'roosterjs-content-model-core';
import type { ContentModelTable, ContentModelTableRow } from 'roosterjs-content-model-types';

const MIN_HEIGHT = 22;
Expand All @@ -24,11 +24,13 @@ export function splitTableCellVertically(table: ContentModelTable) {
if (
belowRow?.cells.every(
(belowCell, colIndex) =>
colIndex < sel.firstCol || colIndex > sel.lastCol || belowCell.spanAbove
colIndex < sel.firstColumn ||
colIndex > sel.lastColumn ||
belowCell.spanAbove
)
) {
belowRow.cells.forEach((belowCell, colIndex) => {
if (colIndex >= sel.firstCol && colIndex <= sel.lastCol) {
if (colIndex >= sel.firstColumn && colIndex <= sel.lastColumn) {
belowCell.spanAbove = false;
delete belowCell.cachedElement;
}
Expand All @@ -50,7 +52,7 @@ export function splitTableCellVertically(table: ContentModelTable) {

newCell.dataset = { ...cell.dataset };

if (colIndex < sel.firstCol || colIndex > sel.lastCol) {
if (colIndex < sel.firstColumn || colIndex > sel.lastColumn) {
newCell.spanAbove = true;
} else {
newCell.isSelected = cell.isSelected;
Expand Down
Loading

0 comments on commit dd6f645

Please sign in to comment.