Skip to content

Commit

Permalink
fix: 修复紧凑模式下, 文本带有 '\n' 换行符时 maxLines 配置未生效和文本溢出的问题 closes #2963 #2900
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Nov 13, 2024
1 parent e565b8a commit c0875ac
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/s2-core/__tests__/data/data-issue-2385.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"province": "浙江",
"city": "杭州",
"type": "纸张",
"price": "2",
"price": "哈哈哈\n哈哈哈",
"cost": "1.5"
},
{
Expand Down Expand Up @@ -153,7 +153,7 @@
"type": "圆规",
"province": "吉林",
"city": "白山",
"price": "111",
"price": "aa",
"cost": "1.5"
}
],
Expand Down
56 changes: 44 additions & 12 deletions packages/s2-core/__tests__/spreadsheet/compare-layout-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,54 @@ describe('Compare Layout Tests', () => {
test.each([
{ showDefaultHeaderActionIcon: true },
{ showDefaultHeaderActionIcon: false },
])(
'should get max col width for pivot sheet and same font size by %o',
async (options) => {
const s2 = new PivotSheet(getContainer(), mockDataConfig, {
...s2Options,
...options,
});

await s2.render();

const colLeafNodes = s2.facet.getColLeafNodes();

expect(Math.floor(colLeafNodes[0].width)).toBeCloseTo(122);
expect(Math.floor(colLeafNodes[1].width)).toEqual(95);
expectTextOverflowing(s2);
},
);

// 覆盖 (数值/中文/英文) 三种场景
test.each([
{ showDefaultHeaderActionIcon: true, fontSize: 20 },
{ showDefaultHeaderActionIcon: true, fontSize: 12 },
{ showDefaultHeaderActionIcon: false, fontSize: 20 },
{ showDefaultHeaderActionIcon: false, fontSize: 12 },
])('should get max col width for pivot sheet by %o', async (options) => {
const s2 = new PivotSheet(getContainer(), mockDataConfig, {
...s2Options,
...options,
showDefaultHeaderActionIcon: options.showDefaultHeaderActionIcon,
});

s2.setTheme({
dataCell: {
text: {
fontSize: 20,
fontSize: options.fontSize,
},
},
});
await s2.render();

const isLargeFontSize = options.fontSize === 20;
const colLeafNodes = s2.facet.getColLeafNodes();

expect(Math.floor(colLeafNodes[0].width)).toBeCloseTo(191);
expect(Math.floor(colLeafNodes[1].width)).toEqual(92);
expect(Math.floor(colLeafNodes[0].width)).toBeCloseTo(
isLargeFontSize ? 191 : 122,
);
expect(Math.floor(colLeafNodes[1].width)).toEqual(
isLargeFontSize ? 145 : 95,
);
expectTextOverflowing(s2);
});

Expand Down Expand Up @@ -90,13 +119,15 @@ describe('Compare Layout Tests', () => {
await s2.render();

const colLeafNodes = s2.facet.getColLeafNodes();

expect(Math.floor(colLeafNodes[0].width)).toBeCloseTo(183);
expectTextOverflowing(s2);
const { dataCellWidthList, colLeafNodeWidthList } = mapWidthList(s2);
const expectWidth = 183;

expect(dataCellWidthList.every((width) => width === 183)).toBeTruthy();
expect(colLeafNodeWidthList).toEqual([183]);
expect(Math.floor(colLeafNodes[0].width)).toBeCloseTo(expectWidth);
expect(
dataCellWidthList.every((width) => width === expectWidth),
).toBeTruthy();
expect(colLeafNodeWidthList).toEqual([expectWidth]);
expectTextOverflowing(s2);
});

test.each([
Expand Down Expand Up @@ -137,12 +168,13 @@ describe('Compare Layout Tests', () => {

expect(dataCellWidthList).toEqual(
options.showDefaultHeaderActionIcon
? [209, 209, 209, 209, 110, 110, 110, 110, 85, 85, 85, 85]
: [209, 209, 209, 209, 110, 110, 110, 110, 69, 69, 69, 69],
? [209, 209, 209, 209, 163, 163, 163, 163, 85, 85, 85, 85]
: [209, 209, 209, 209, 163, 163, 163, 163, 69, 69, 69, 69],
);
expect(colLeafNodeWidthList).toEqual(
options.showDefaultHeaderActionIcon ? [209, 110, 85] : [209, 110, 69],
options.showDefaultHeaderActionIcon ? [209, 163, 85] : [209, 163, 69],
);
expectTextOverflowing(s2);
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ describe('SpreadSheet Multi Line Text Tests', () => {
// wordWrap 关闭时, 不会渲染省略号
cells.forEach((cell) => {
expect(cell.getActualText()).not.toContain('...');
expect(cell.isTextOverflowing()).toBeFalsy();
});
});
expectColHierarchyHeight(90);
Expand Down Expand Up @@ -629,6 +630,7 @@ describe('SpreadSheet Multi Line Text Tests', () => {
// wordWrap 关闭时, 不会渲染省略号
cells.forEach((cell) => {
expect(cell.getActualText()).not.toContain('...');
expect(cell.isTextOverflowing()).toBeFalsy();
});
});
});
Expand Down
13 changes: 2 additions & 11 deletions packages/s2-core/src/utils/merge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isArray, isEmpty, isEqual, isString, mergeWith, uniq } from 'lodash';
import { DEFAULT_DATA_CONFIG } from '../common/constant/dataConfig';
import { DEFAULT_OPTIONS, LayoutWidthType } from '../common/constant/options';
import { DEFAULT_OPTIONS } from '../common/constant/options';
import type {
CustomHeaderFields,
Fields,
Expand Down Expand Up @@ -68,14 +68,5 @@ export const setupDataConfig = (
export const setupOptions = (
options: Partial<S2Options> | null | undefined,
): S2Options => {
const mergedOptions = customMerge<S2Options>(DEFAULT_OPTIONS, options);

if (
mergedOptions.style?.layoutWidthType === LayoutWidthType.Compact &&
mergedOptions.style?.dataCell!.maxLines! <= 1
) {
mergedOptions.style.dataCell!.wordWrap = false;
}

return mergedOptions;
return customMerge<S2Options>(DEFAULT_OPTIONS, options);
};

0 comments on commit c0875ac

Please sign in to comment.