Skip to content

Commit

Permalink
Fix resize table with width (#2940)
Browse files Browse the repository at this point in the history
* Fix resize table with width

* fix test
  • Loading branch information
JiuqingSong authored Feb 7, 2025
1 parent 9dbe329 commit 6388b93
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
MIN_ALLOWED_TABLE_CELL_WIDTH,
mutateBlock,
MIN_ALLOWED_TABLE_CELL_HEIGHT,
parseValueWithUnit,
} from 'roosterjs-content-model-dom';
import type { DragAndDropHandler } from '../../../pluginUtils/DragAndDrop/DragAndDropHandler';
import type { IEditor, ReadonlyContentModelTable } from 'roosterjs-content-model-types';
Expand Down Expand Up @@ -46,7 +47,15 @@ export function createCellResizer(

(anchorContainer || document.body).appendChild(div);

const context: CellResizerContext = { editor, td, table, isRTL, zoomScale, onStart };
const context: CellResizerContext = {
editor,
td,
table,
isRTL,
zoomScale,
onStart,
originalWidth: parseValueWithUnit(table.style.width),
};
const setPosition = isHorizontal ? setHorizontalPosition : setVerticalPosition;
setPosition(context, div);

Expand Down Expand Up @@ -79,6 +88,7 @@ export interface CellResizerContext {
table: HTMLTableElement;
isRTL: boolean;
zoomScale: number;
originalWidth: number;
onStart: () => void;
}

Expand Down Expand Up @@ -230,6 +240,13 @@ export function onDraggingVertical(
}
}

if (context.originalWidth > 0) {
const newWidth = context.originalWidth + change + 'px';

mutableTable.format.width = newWidth;
table.style.width = newWidth;
}

return true;
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('Cell Resizer tests', () => {
isRTL: false,
zoomScale: 1,
onStart: onStartSpy,
originalWidth: 0,
};
const editorCMTable = getCMTableFromTable(editor, target as HTMLTableElement);

Expand Down Expand Up @@ -119,6 +120,7 @@ describe('Cell Resizer tests', () => {
isRTL: false,
zoomScale: 1,
onStart: onStartSpy,
originalWidth: 0,
};
const delta = 10 * growth;
const beforeHeight = getCurrentTable(editor).rows[cellRow].getBoundingClientRect()
Expand Down Expand Up @@ -194,6 +196,7 @@ describe('Cell Resizer tests', () => {
isRTL: false,
zoomScale: 1,
onStart: onStartSpy,
originalWidth: 0,
};
const delta = 10 * growth;
const beforeWidth = getCurrentTable(editor).rows[cellRow].cells[
Expand Down

0 comments on commit 6388b93

Please sign in to comment.