Skip to content

Commit

Permalink
refactor: optimize text padding logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Sep 1, 2021
1 parent e812595 commit 0397b7c
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/s2-core/src/facet/header/series-number.ts
Expand Up @@ -5,7 +5,7 @@ import { measureTextWidth } from '@/utils/text';
import { getAdjustPosition } from '@/utils/text-absorption';
import { BBox, IGroup, IShape } from '@antv/g-canvas';
import { each } from 'lodash';
import { ViewMeta } from '@/common/interface';
import { Padding, ViewMeta } from '@/common/interface';
import { translateGroup } from '../utils';
import { BaseHeader, BaseHeaderConfig } from './base';

Expand Down Expand Up @@ -145,13 +145,7 @@ export class SeriesNumberHeader extends BaseHeader<BaseHeaderConfig> {
isLeaf,
isTotals,
} = cellData;
const padding = { ...rowCellTheme.cell.padding };
const labelWidth = measureTextWidth(
label,
this.headerConfig.spreadsheet.theme.rowCell.text,
);
padding.left = Math.max(Math.abs((cellWidth - labelWidth) / 2), 4);
padding.right = padding.left;
const padding = this.getTextPadding(label, cellWidth);
const textStyle =
isLeaf && !isTotals ? rowCellTheme.text : rowCellTheme.bolderText;
const textY = getAdjustPosition(
Expand All @@ -172,4 +166,15 @@ export class SeriesNumberHeader extends BaseHeader<BaseHeaderConfig> {
},
});
}

private getTextPadding(text: string, cellWidth: number): Padding {
const rowCellTheme = this.headerConfig.spreadsheet.theme.rowCell;
const textWidth = measureTextWidth(text, rowCellTheme.text);
const padding = Math.max(Math.abs((cellWidth - textWidth) / 2), 4);
return {
...rowCellTheme.cell.padding,
left: padding,
right: padding,
};
}
}

0 comments on commit 0397b7c

Please sign in to comment.