Skip to content

Commit

Permalink
[FIX] SheetUIPlugin: set column height for multi line content
Browse files Browse the repository at this point in the history
Previously, when double-clicking on the overlay header to auto-resize
column size, the auto-resizing did not work accurately for multi-line
content. This was due to auto-resizing taking the whole length of the
text without considering whether the content was multi-line or not.

To fix this, we modified the auto-resizing to consider the maximum length
of the broken content for multi-line cells. This will ensure that column height
is properly set for cells with multi-line content.

task_id: 3250821

closes #2497

X-original-commit: 1d7ff54
Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
  • Loading branch information
jash-odoo committed May 22, 2023
1 parent 1333832 commit 52bf836
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/plugins/ui_feature/ui_sheet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
FILTER_ICON_MARGIN,
ICON_EDGE_LENGTH,
NEWLINE,
PADDING_AUTORESIZE_HORIZONTAL,
} from "../../constants";
import { computeIconWidth, computeTextWidth, positions } from "../../helpers/index";
Expand Down Expand Up @@ -71,7 +72,8 @@ export class SheetUIPlugin extends UIPlugin {
getCellWidth(position: CellPosition): number {
const text = this.getCellText(position);
const style = this.getters.getCellComputedStyle(position);
let contentWidth = this.getTextWidth(text, style);
const multiLineText = text.split(NEWLINE);
let contentWidth = Math.max(...multiLineText.map((line) => this.getTextWidth(line, style)));
const icon = this.getters.getConditionalIcon(position);
if (icon) {
contentWidth += computeIconWidth(this.getters.getCellStyle(position));
Expand Down
15 changes: 14 additions & 1 deletion tests/plugins/formatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {
DEFAULT_FONT_SIZE,
FILTER_ICON_MARGIN,
ICON_EDGE_LENGTH,
NEWLINE,
PADDING_AUTORESIZE_HORIZONTAL,
PADDING_AUTORESIZE_VERTICAL,
} from "../../src/constants";
import { arg, functionRegistry } from "../../src/functions";
import { toString } from "../../src/functions/helpers";
import { fontSizeInPixels, toZone } from "../../src/helpers";
import { fontSizeInPixels, toCartesian, toZone } from "../../src/helpers";
import { Model } from "../../src/model";
import {
Arg,
Expand Down Expand Up @@ -448,6 +449,18 @@ describe("Autoresize", () => {
expect(model.getters.getColSize(sheetId, 0)).toBe(initCellWidth);
});

test("Can autoresize a column with multiline content", () => {
const content = `Hello this is \nmultiline content for test`;
setCellContent(model, "A1", content);
model.dispatch("AUTORESIZE_COLUMNS", { sheetId, cols: [0] });
const position = { sheetId, ...toCartesian("A1") };
const style = model.getters.getCellComputedStyle(position);
const multiLineText = content.split(NEWLINE);
expect(model.getters.getColSize(sheetId, 0)).toBe(
Math.max(...multiLineText.map((line) => model.getters.getTextWidth(line, style))) + hPadding
);
});

test("Can autoresize a column with text width smaller than cell width", () => {
setCellContent(model, "A1", TEXT);
const initCellWidth = sizes[0] + hPadding;
Expand Down

0 comments on commit 52bf836

Please sign in to comment.