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 d5ee559 commit a9ab0b8
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 88 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ Object {
"test-b": 1,
},
"textOverflow": "ellipsis",
"widthByField": null,
"wordWrap": true,
}
`;
Expand All @@ -149,7 +148,6 @@ Object {
"testField": 1,
},
"textOverflow": "ellipsis",
"widthByField": null,
"wordWrap": true,
}
`;
Expand Down
4 changes: 2 additions & 2 deletions packages/s2-core/src/cell/base-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ 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 {}
protected getResizedTextMaxLines(): number | void {}

/* -------------------------------------------------------------------------- */
/* common functions that will be used in subtype */
Expand Down Expand Up @@ -486,7 +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;
const maxLines = this.getResizedTextMaxLines() || textStyle?.maxLines;

// 在坐标计算 (getTextPosition) 之前, 预渲染一次, 提前生成 textShape, 获得文字宽度, 用于计算 icon 绘制坐标
this.renderTextShape({
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/cell/col-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ export class ColCell extends HeaderCell<ColHeaderConfig> {
return isNextSiblingNodeHidden && isPrevSiblingNodeHidden;
}

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

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/cell/corner-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class CornerCell extends HeaderCell<CornerHeaderConfig> {
);
}

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

Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/cell/data-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ export class DataCell extends BaseCell<ViewMeta> {
updateShapeAttr(this.conditionIconShapes, SHAPE_STYLE_MAP.opacity, opacity);
}

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

// 数值和行高保持一致, 同时兼容明细表
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/cell/row-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ export class RowCell extends HeaderCell<RowHeaderConfig> {
return { x: textX, y: textStart };
}

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

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/common/interface/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface ResizeInfo {
isResizeArea?: boolean;
isResizeMask?: boolean;

/** 当前拖拽热区对应的节点信息 */
/** 当前拖拽热区对应的单元格/节点信息 */
meta: Node | ViewMeta;
cell: S2CellType;

Expand Down
42 changes: 20 additions & 22 deletions packages/s2-core/src/interaction/row-column-resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@antv/g';
import { clone, isEmpty, throttle } from 'lodash';
import type {
CellTextWordWrapStyle,
DefaultCellTheme,
ResizeInteractionOptions,
ResizeParams,
Expand Down Expand Up @@ -254,23 +255,6 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
const displayWidth = isDisabled ? originalWidth : resizedWidth;
const displayHeight = isDisabled ? originalHeight : resizedHeight;

// // 高度调整时, 根据行高计算最大可展示的文本行数, 覆盖原本的 maxLines 配置
// if (resizeInfo.type === ResizeDirectionType.Vertical) {
// const { cell } = resizeInfo?.cell?.getStyle() as DefaultCellTheme;
// const padding = cell!.padding!.top! + cell!.padding!.bottom!;
// const lineHeight = resizeInfo?.cell?.getTextLineHeight()!;
// const maxLines = !isDisabled
// ? Math.max(1, Math.floor((displayHeight - padding) / lineHeight))
// : undefined;

// return {
// displayWidth,
// displayHeight,
// isDisabled,
// maxLines,
// };
// }

return {
displayWidth,
displayHeight,
Expand Down Expand Up @@ -397,6 +381,7 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
}

private getResizeHeightDetail(): ResizeDetail | null {
const { style } = this.spreadsheet.options;
const resizeInfo = this.getResizeInfo();
const { displayHeight } = this.getDisAllowResizeInfo();

Expand All @@ -405,8 +390,9 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
return {
eventType: S2Event.LAYOUT_RESIZE_COL_HEIGHT,
style: {
colCell: this.transform(
colCell: this.getResizedCellStyleByField(
this.getColCellHeightByField(resizeInfo, displayHeight!),
style?.colCell!,
displayHeight,
),
},
Expand All @@ -417,8 +403,9 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
eventType: S2Event.LAYOUT_RESIZE_ROW_HEIGHT,
style: {
rowCell: {
...this.transform(
...this.getResizedCellStyleByField(
this.getCellStyleByField(displayHeight),
style?.rowCell!,
displayHeight,
),
height: !this.isEffectRowOf(ResizeType.ALL)
Expand All @@ -433,13 +420,24 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
}
}

private transform(
private getResizedCellStyleByField(
heightByField: Record<string, number>,
cellStyle: CellTextWordWrapStyle,
displayHeight: number,
) {
const isEnableHeightAdaptive =
cellStyle?.maxLines! > 1 && cellStyle?.wordWrap;

if (isEnableHeightAdaptive) {
return {
heightByField,
};
}

// 如果开启了换行, 高度拖拽后动态计算 maxLines 的值, 已保证展示合理性.
const { cell } = this.getResizeInfo();
const { cell: cellStyle } = cell?.getStyle() as DefaultCellTheme;
const padding = cellStyle!.padding!.top! + cellStyle!.padding!.bottom!;
const { cell: cellTheme } = cell?.getStyle() as DefaultCellTheme;
const padding = cellTheme!.padding!.top! + cellTheme!.padding!.bottom!;
const lineHeight = cell?.getTextLineHeight()!;

const maxLines = Math.max(
Expand Down

0 comments on commit a9ab0b8

Please sign in to comment.