Skip to content

Commit

Permalink
fix: 支持单行调整
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Nov 19, 2024
1 parent 062c6db commit e200143
Show file tree
Hide file tree
Showing 11 changed files with 10,588 additions and 8,576 deletions.
18,940 changes: 10,399 additions & 8,541 deletions packages/s2-core/__tests__/spreadsheet/__snapshots__/multi-line-text-spec.ts.snap

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions packages/s2-core/__tests__/spreadsheet/multi-line-text-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,33 @@ describe('SpreadSheet Multi Line Text Tests', () => {

matchCellStyleSnapshot();
});

test('should render by maxLinesByField', async () => {
s2.changeSheetSize(800, 600);

updateStyle(Infinity);
await s2.render(false);

s2.setOptions({
style: {
rowCell: {
maxLinesByField: {
city: 3,
},
},
colCell: {
maxLinesByField: {
type: 2,
sub_type: 3,
[EXTRA_FIELD]: 4,
},
},
},
});
await s2.render(false);

matchCellStyleSnapshot();
});
});

describe('TableSheet', () => {
Expand Down Expand Up @@ -1098,5 +1125,32 @@ describe('SpreadSheet Multi Line Text Tests', () => {

matchCellStyleSnapshot();
});

test('should render by maxLinesByField', async () => {
s2.changeSheetSize(800, 600);

updateStyle(Infinity);
await s2.render(false);

s2.setOptions({
style: {
rowCell: {
maxLinesByField: {
// 行索引
'1': 2,
},
},
colCell: {
maxLinesByField: {
type: 2,
sub_type: 3,
},
},
},
});
await s2.render(false);

matchCellStyleSnapshot();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ Object {
"test-cell-b": 2,
},
"maxLines": 1,
"maxLinesByField": Object {
"test-cell-a": 1,
"test-cell-b": 1,
},
"showTreeLeafNodeAlignDot": false,
"textOverflow": "ellipsis",
"widthByField": null,
Expand All @@ -94,6 +98,9 @@ Object {
"testField": 2,
},
"maxLines": 1,
"maxLinesByField": Object {
"testField": 1,
},
"showTreeLeafNodeAlignDot": false,
"textOverflow": "ellipsis",
"widthByField": null,
Expand Down Expand Up @@ -130,6 +137,10 @@ Object {
"test-b": 2,
},
"maxLines": 1,
"maxLinesByField": Object {
"test-a": 1,
"test-b": 1,
},
"textOverflow": "ellipsis",
"widthByField": null,
"wordWrap": true,
Expand All @@ -143,6 +154,9 @@ Object {
"testField": 2,
},
"maxLines": 1,
"maxLinesByField": Object {
"testField": 1,
},
"textOverflow": "ellipsis",
"widthByField": null,
"wordWrap": true,
Expand All @@ -156,6 +170,9 @@ Object {
"testFieldId": 2,
},
"maxLines": 1,
"maxLinesByField": Object {
"testFieldId": 1,
},
"showTreeLeafNodeAlignDot": false,
"textOverflow": "ellipsis",
"widthByField": null,
Expand All @@ -171,6 +188,10 @@ Object {
"test-cell-b": 2,
},
"maxLines": 1,
"maxLinesByField": Object {
"test-cell-a": 1,
"test-cell-b": 1,
},
"showTreeLeafNodeAlignDot": false,
"textOverflow": "ellipsis",
"widthByField": null,
Expand All @@ -185,6 +206,9 @@ Object {
"0": 2,
},
"maxLines": 1,
"maxLinesByField": Object {
"0": 1,
},
"showTreeLeafNodeAlignDot": false,
"textOverflow": "ellipsis",
"widthByField": null,
Expand Down Expand Up @@ -227,6 +251,9 @@ Object {
"testField": 2,
},
"maxLines": 1,
"maxLinesByField": Object {
"testField": 1,
},
"showTreeLeafNodeAlignDot": false,
"textOverflow": "ellipsis",
"widthByField": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,15 +462,14 @@ describe('Interaction Row Column Resize Tests', () => {
const resizeDetail: ResizeParams = {
info: resizeInfo,
style: {
dataCell: {
maxLines: 1,
},
rowCell: {
height: 2,
heightByField: {
[resizeInfo.meta.field!]: 2,
},
maxLines: 1,
maxLinesByField: {
[resizeInfo.meta.field!]: 1,
},
},
},
};
Expand Down
6 changes: 5 additions & 1 deletion packages/s2-core/src/cell/base-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export abstract class BaseCell<T extends SimpleBBox> extends Group {

protected abstract getBorderPositions(): CellBorderPosition[];

protected abstract getTextStyle(): TextTheme;
protected abstract getTextStyle(): TextTheme & CellTextWordWrapStyle;

protected abstract getFormattedFieldValue(): FormatResult;

Expand Down Expand Up @@ -189,6 +189,8 @@ export abstract class BaseCell<T extends SimpleBBox> extends Group {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected handleRestOptions(...options: unknown[]) {}

protected getTextDraggedMaxLines(): number | void {}

/* -------------------------------------------------------------------------- */
/* common functions that will be used in subtype */
/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -484,6 +486,7 @@ export abstract class BaseCell<T extends SimpleBBox> extends Group {
// G 遵循浏览器的规范, 空间不足以展示省略号时, 会裁剪文字, 而不是展示省略号: https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow#ellipsis
const maxTextWidth = Math.max(this.getMaxTextWidth(), 0) + EXTRA_PIXEL;
const textStyle = this.getTextStyle();
const maxLines = this.getTextDraggedMaxLines() || textStyle?.maxLines;

// 在坐标计算 (getTextPosition) 之前, 预渲染一次, 提前生成 textShape, 获得文字宽度, 用于计算 icon 绘制坐标
this.renderTextShape({
Expand All @@ -492,6 +495,7 @@ export abstract class BaseCell<T extends SimpleBBox> extends Group {
y: 0,
text: this.getFieldValue()!,
wordWrapWidth: maxTextWidth,
maxLines,
});

if (this.isShallowRender()) {
Expand Down
9 changes: 9 additions & 0 deletions packages/s2-core/src/cell/col-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,13 @@ export class ColCell extends HeaderCell<ColHeaderConfig> {

return isNextSiblingNodeHidden && isPrevSiblingNodeHidden;
}

protected getTextDraggedMaxLines() {
const { colCell } = this.spreadsheet.options.style!;

return (
colCell?.maxLinesByField?.[this.meta.id] ??
colCell?.maxLinesByField?.[this.meta.field]
);
}
}
10 changes: 10 additions & 0 deletions packages/s2-core/src/cell/corner-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,14 @@ export class CornerCell extends HeaderCell<CornerHeaderConfig> {
this.spreadsheet.dataSet.getFieldName(this.meta.field),
);
}

protected getTextDraggedMaxLines() {
// 角头和列头高度一致
const { colCell } = this.spreadsheet.options.style!;

return (
colCell?.maxLinesByField?.[this.meta.id] ??
colCell?.maxLinesByField?.[this.meta.field]
);
}
}
10 changes: 10 additions & 0 deletions packages/s2-core/src/cell/data-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,14 @@ export class DataCell extends BaseCell<ViewMeta> {

updateShapeAttr(this.conditionIconShapes, SHAPE_STYLE_MAP.opacity, opacity);
}

protected getTextDraggedMaxLines() {
const { rowCell } = this.spreadsheet.options.style!;

// 数值和行高保持一致, 同时兼容明细表
return (
rowCell?.maxLinesByField?.[this.meta.id] ??
rowCell?.maxLinesByField?.[this.meta.rowId!]
);
}
}
9 changes: 9 additions & 0 deletions packages/s2-core/src/cell/row-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,13 @@ export class RowCell extends HeaderCell<RowHeaderConfig> {

return { x: textX, y: textStart };
}

protected getTextDraggedMaxLines() {
const { rowCell } = this.spreadsheet.options.style!;

return (
rowCell?.maxLinesByField?.[this.meta.id] ??
rowCell?.maxLinesByField?.[this.meta.field]
);
}
}
7 changes: 6 additions & 1 deletion packages/s2-core/src/common/interface/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ export interface BaseCellStyle {
}

export interface CellTextWordWrapStyle
extends Pick<TextStyleProps, 'wordWrap' | 'maxLines' | 'textOverflow'> {}
extends Pick<TextStyleProps, 'wordWrap' | 'maxLines' | 'textOverflow'> {
/**
* @private 内部使用
*/
maxLinesByField?: Record<string, number> | null;
}

export interface DataCellStyle extends CellTextWordWrapStyle {
/**
Expand Down
Loading

0 comments on commit e200143

Please sign in to comment.