Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cell): border width issue #859

Merged
merged 10 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/s2-core/__tests__/spreadsheet/table-sheet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ const options: S2Options = {
enableCopy: true,
hoverHighlight: false,
linkFields: ['order_id', 'customer_name'],
hiddenColumnFields: ['order_date'],
},
frozenRowCount: 2,
frozenColCount: 1,
frozenTrailingColCount: 1,
frozenTrailingRowCount: 1,
showDefaultHeaderActionIcon: true,
tooltip: {
operation: {
hiddenColumns: true,
},
},
};

describe('TableSheet normal spec', () => {
Expand Down
70 changes: 70 additions & 0 deletions packages/s2-core/__tests__/unit/utils/cell/cell.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { BorderPosition, CellTheme } from 'src/common/interface';
import { SimpleBBox } from '@antv/g-canvas';
import { AreaRange } from '@/common/interface/scroll';
import {
getContentArea,
getMaxTextWidth,
getTextAndFollowingIconPosition,
getTextAndIconPositionWhenHorizontalScrolling,
getBorderPositionAndStyle,
} from '@/utils/cell/cell';

describe('Cell Content Test', () => {
Expand Down Expand Up @@ -488,4 +490,72 @@ describe('Horizontal Scrolling Text Position Test', () => {
),
).toEqual(50);
});

test('should get border position', () => {
const contentBox = {
x: 0,
y: 0,
width: 200,
height: 50,
};
const style = {
verticalBorderColorOpacity: 1,
verticalBorderColor: '#000',
verticalBorderWidth: 2,
horizontalBorderColor: '#000',
horizontalBorderColorOpacity: 1,
horizontalBorderWidth: 2,
};
expect(
getBorderPositionAndStyle(
BorderPosition.LEFT,
contentBox,
style as CellTheme,
).position,
).toEqual({
x1: 1,
y1: 0,
x2: 1,
y2: 50,
});

expect(
getBorderPositionAndStyle(
BorderPosition.RIGHT,
contentBox,
style as CellTheme,
).position,
).toEqual({
x1: 199,
y1: 0,
x2: 199,
y2: 50,
});

expect(
getBorderPositionAndStyle(
BorderPosition.TOP,
contentBox,
style as CellTheme,
).position,
).toEqual({
x1: 0,
y1: 1,
x2: 200,
y2: 1,
});

expect(
getBorderPositionAndStyle(
BorderPosition.BOTTOM,
contentBox,
style as CellTheme,
).position,
).toEqual({
x1: 0,
y1: 49,
x2: 200,
y2: 49,
});
});
});
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 @@ -248,8 +248,8 @@ export abstract class BaseCell<T extends SimpleBBox> extends Group {
) {
if (isNumber(style)) {
const marginStyle = {
x: Math.ceil(x + style / 2), // TODO: 边框整体重构后需revisit
y: Math.ceil(y + style / 2),
x: x + style / 2, // TODO: 边框整体重构后需revisit
zxc0328 marked this conversation as resolved.
Show resolved Hide resolved
y: y + style / 2,
width: width - style - 1,
height: height - style - 1,
};
Expand Down
52 changes: 28 additions & 24 deletions packages/s2-core/src/cell/col-cell.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Point } from '@antv/g-canvas';
import { Point, SimpleBBox } from '@antv/g-canvas';
import { shouldAddResizeArea } from './../utils/interaction/resize';
import { HeaderCell } from './header-cell';
import {
Expand All @@ -13,6 +13,7 @@ import {
ResizeAreaEffect,
} from '@/common/constant';
import {
BorderPosition,
FormatResult,
TextAlign,
TextBaseline,
Expand All @@ -24,6 +25,7 @@ import { AreaRange } from '@/common/interface/scroll';
import {
getTextAndIconPositionWhenHorizontalScrolling,
getTextAndFollowingIconPosition,
getBorderPositionAndStyle,
} from '@/utils/cell/cell';

export class ColCell extends HeaderCell {
Expand All @@ -45,19 +47,18 @@ export class ColCell extends HeaderCell {
this.drawTextShape();
// draw action icons
this.drawActionIcons();
// draw right border
this.drawRightBorder();
// draw borders
this.drawBorders();
// draw resize ares
this.drawResizeArea();
this.update();
}

protected drawBackgroundShape() {
const { backgroundColor, horizontalBorderColor } = this.getStyle().cell;
const { backgroundColor } = this.getStyle().cell;
this.backgroundShape = renderRect(this, {
...this.getCellArea(),
fill: backgroundColor,
stroke: horizontalBorderColor,
});
}

Expand Down Expand Up @@ -302,24 +303,27 @@ export class ColCell extends HeaderCell {
this.drawVerticalResizeArea();
}

private drawRightBorder() {
if (!this.meta.isLeaf) {
const { height, viewportHeight } = this.headerConfig;
const { x, y, width: cellWidth, height: cellHeight } = this.meta;

renderLine(
this,
{
x1: x + cellWidth,
y1: y + cellHeight,
x2: x + cellWidth,
y2: y + height + viewportHeight,
},
{
stroke: this.theme.colCell.cell.horizontalBorderColor,
lineWidth: 1,
},
);
}
protected drawHorizontalBorder() {
const { position, style } = getBorderPositionAndStyle(
BorderPosition.TOP,
this.meta as SimpleBBox,
this.theme.colCell.cell,
);

renderLine(this, position, style);
}

protected drawVerticalBorder() {
const { position, style } = getBorderPositionAndStyle(
BorderPosition.RIGHT,
this.meta as SimpleBBox,
this.theme.colCell.cell,
);
renderLine(this, position, style);
}

protected drawBorders() {
this.drawHorizontalBorder();
this.drawVerticalBorder();
}
}
59 changes: 20 additions & 39 deletions packages/s2-core/src/cell/corner-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ import {
ResizeDirectionType,
S2Event,
} from '@/common/constant';
import { FormatResult, TextTheme } from '@/common/interface';
import { BorderPosition, FormatResult, TextTheme } from '@/common/interface';
import { CornerHeaderConfig } from '@/facet/header/corner';
import { getTextPosition, getVerticalPosition } from '@/utils/cell/cell';
import {
getTextPosition,
getVerticalPosition,
getBorderPositionAndStyle,
} from '@/utils/cell/cell';
import {
renderLine,
renderRect,
Expand Down Expand Up @@ -187,46 +191,23 @@ export class CornerCell extends HeaderCell {
* @private
*/
protected drawBorderShape() {
const { x, y, width, height } = this.getCellArea();
const {
horizontalBorderColor,
horizontalBorderWidth,
horizontalBorderColorOpacity,
verticalBorderColor,
verticalBorderWidth,
verticalBorderColorOpacity,
} = this.getStyle().cell;
const { position: horizontalPosition, style: horizontalStyle } =
getBorderPositionAndStyle(
BorderPosition.TOP,
this.getCellArea(),
this.getStyle().cell,
);
const { position: verticalPosition, style: verticalStyle } =
getBorderPositionAndStyle(
BorderPosition.LEFT,
this.getCellArea(),
this.getStyle().cell,
);

// horizontal border
renderLine(
this,
{
x1: x,
y1: y,
x2: x + width,
y2: y,
},
{
stroke: horizontalBorderColor,
lineWidth: horizontalBorderWidth,
opacity: horizontalBorderColorOpacity,
},
);
renderLine(this, horizontalPosition, horizontalStyle);
// vertical border
renderLine(
this,
{
x1: x + width,
y1: y,
x2: x + width,
y2: y + height,
},
{
stroke: verticalBorderColor,
lineWidth: verticalBorderWidth,
opacity: verticalBorderColorOpacity,
},
);
renderLine(this, verticalPosition, verticalStyle);
}

private isLastRowCornerCell() {
Expand Down
63 changes: 25 additions & 38 deletions packages/s2-core/src/cell/data-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import {
TextTheme,
ViewMeta,
ViewMetaIndexType,
BorderPosition,
} from '@/common/interface';
import { getMaxTextWidth } from '@/utils/cell/cell';
import { getMaxTextWidth, getBorderPositionAndStyle } from '@/utils/cell/cell';
import { includeCell } from '@/utils/cell/data-cell';
import { getIconPositionCfg } from '@/utils/condition/condition';
import {
Expand Down Expand Up @@ -406,47 +407,24 @@ export class DataCell extends BaseCell<ViewMeta> {
* @private
*/
protected drawBorderShape() {
const { x, y, height, width } = this.getCellArea();
const {
horizontalBorderColor,
horizontalBorderWidth,
horizontalBorderColorOpacity,
verticalBorderColor,
verticalBorderWidth,
verticalBorderColorOpacity,
} = this.getStyle().cell;
const { position: horizontalPosition, style: horizontalStyle } =
getBorderPositionAndStyle(
BorderPosition.BOTTOM,
this.getCellArea(),
this.getStyle().cell,
);
const { position: verticalPosition, style: verticalStyle } =
getBorderPositionAndStyle(
BorderPosition.RIGHT,
this.getCellArea(),
this.getStyle().cell,
);

// horizontal border
renderLine(
this,
{
x1: x,
y1: y + height,
x2: x + width,
y2: y + height,
},
{
stroke: horizontalBorderColor,
lineWidth: horizontalBorderWidth,
opacity: horizontalBorderColorOpacity,
},
);
renderLine(this, horizontalPosition, horizontalStyle);

// vertical border
renderLine(
this,
{
x1: x + width,
y1: y,
x2: x + width,
y2: y + height,
},
{
stroke: verticalBorderColor,
lineWidth: verticalBorderWidth,
opacity: verticalBorderColorOpacity,
},
);
renderLine(this, verticalPosition, verticalStyle);
}

/**
Expand Down Expand Up @@ -505,4 +483,13 @@ export class DataCell extends BaseCell<ViewMeta> {
1,
);
}

protected drawLeftBorder() {
const { position, style } = getBorderPositionAndStyle(
BorderPosition.LEFT,
this.getCellArea(),
this.getStyle().cell,
);
renderLine(this, position, style);
}
}
Loading