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: 修复单元格宽高为0时的无意义渲染 #2024

Merged
merged 5 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions packages/s2-core/__tests__/unit/cell/col-cell-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ describe('Col Cell Tests', () => {
label: 'label',
fieldValue: 'fieldValue',
value: 'value',
width: 100,
height: 10,
} as unknown as Node;

test('should get correct col cell formatter', () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/s2-core/__tests__/unit/cell/data-cell-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ describe('Data Cell Tests', () => {
[VALUE_FIELD]: 'value',
[EXTRA_FIELD]: 12,
},
width: 100,
height: 100,
} as unknown as ViewMeta;

let s2: SpreadSheet;
Expand Down Expand Up @@ -95,6 +97,15 @@ describe('Data Cell Tests', () => {
} as PivotFacet;
});

test("shouldn't init when width or height is not positive", () => {
const dataCell = new DataCell({ ...meta, width: 0, height: 0 }, s2);
expect(dataCell.getTextShape()).toBeUndefined();
// @ts-ignore
expect(dataCell.backgroundShape).toBeUndefined();
// @ts-ignore
expect([...dataCell.stateShapes.keys()]).toBeEmpty();
});

test('should get text shape', () => {
const dataCell = new DataCell(meta, s2);
expect(dataCell.getTextShapes()).toEqual([dataCell.getTextShape()]);
Expand Down Expand Up @@ -206,6 +217,7 @@ describe('Data Cell Tests', () => {
const fieldValue = 27.334666666666667;
const anotherMeta = {
width: cellWidth,
height: 100,
valueField: 'value',
fieldValue,
data: {
Expand Down
5 changes: 5 additions & 0 deletions packages/s2-core/src/cell/base-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ export abstract class BaseCell<T extends SimpleBBox> extends Group {
/* common functions that will be used in subtype */
/* -------------------------------------------------------------------------- */

protected shouldInit() {
const { width, height } = this.meta;
return width > 0 && height > 0;
}

public getStyle<K extends keyof S2Theme = keyof CellThemes>(
name?: K,
): DefaultCellTheme | S2Theme[K] {
Expand Down
3 changes: 3 additions & 0 deletions packages/s2-core/src/cell/col-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export class ColCell extends HeaderCell {
}

protected initCell() {
if (!this.shouldInit()) {
wjgogogo marked this conversation as resolved.
Show resolved Hide resolved
return;
}
super.initCell();
// 1、draw rect background
this.drawBackgroundShape();
Expand Down
3 changes: 3 additions & 0 deletions packages/s2-core/src/cell/corner-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export class CornerCell extends HeaderCell {
public update() {}

protected initCell() {
if (!this.shouldInit()) {
return;
}
super.initCell();
this.resetTextAndConditionIconShapes();
this.drawBackgroundShape();
Expand Down
3 changes: 3 additions & 0 deletions packages/s2-core/src/cell/data-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ export class DataCell extends BaseCell<ViewMeta> {
}

protected initCell() {
if (!this.shouldInit()) {
return;
}
this.resetTextAndConditionIconShapes();
this.drawBackgroundShape();
this.drawInteractiveBgShape();
Expand Down
3 changes: 3 additions & 0 deletions packages/s2-core/src/cell/merged-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export class MergedCell extends DataCell {
public update() {}

protected initCell() {
if (!this.shouldInit()) {
return;
}
this.resetTextAndConditionIconShapes();
// TODO:1、交互态扩展; 2、合并后的单元格文字布局及文字内容(目前参考Excel合并后只保留第一个单元格子的数据)
this.conditions = this.spreadsheet.options.conditions;
Expand Down
3 changes: 3 additions & 0 deletions packages/s2-core/src/cell/row-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export class RowCell extends HeaderCell {
}

protected initCell() {
if (!this.shouldInit()) {
return;
}
super.initCell();
// 绘制单元格背景
this.drawBackgroundShape();
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/facet/base-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export abstract class BaseFacet {

protected getCellDraggedWidth(node: Node): number {
const { colCfg } = this.cfg;
return get(colCfg?.widthByFieldValue, `${node.value}`, node.width);
return get(colCfg?.widthByFieldValue, `${node.value}`);
}

hideScrollBar = () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/s2-core/src/facet/pivot-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
get,
isArray,
isEmpty,
isNil,
isNumber,
keys,
last,
map,
Expand Down Expand Up @@ -250,13 +250,13 @@ export class PivotFacet extends BaseFacet {
const cellDraggedWidth = this.getCellDraggedWidth(col);

// 1. 拖拽后的宽度优先级最高
if (cellDraggedWidth) {
if (isNumber(cellDraggedWidth)) {
return cellDraggedWidth;
}

// 2. 其次是自定义, 返回 null 则使用默认宽度
const cellCustomWidth = this.getCellCustomWidth(col, colCfg?.width);
if (!isNil(cellCustomWidth)) {
if (isNumber(cellCustomWidth)) {
return cellCustomWidth;
}

Expand Down Expand Up @@ -348,7 +348,7 @@ export class PivotFacet extends BaseFacet {
private getColNodeHeight(col: Node) {
const { colCfg } = this.cfg;
const userDraggedHeight = get(colCfg, `heightByField.${col.key}`);
return userDraggedHeight || colCfg?.height;
return userDraggedHeight ?? colCfg?.height;
}

/**
Expand Down Expand Up @@ -612,12 +612,12 @@ export class PivotFacet extends BaseFacet {

const cellDraggedWidth = get(rowCfg, `widthByField.${node.key}`);

if (cellDraggedWidth) {
if (isNumber(cellDraggedWidth)) {
return cellDraggedWidth;
}

const cellCustomWidth = this.getCellCustomWidth(node, rowCfg?.width);
if (!isNil(cellCustomWidth)) {
if (isNumber(cellCustomWidth)) {
return cellCustomWidth;
}

Expand Down
21 changes: 15 additions & 6 deletions packages/s2-core/src/facet/table-facet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import type { Group, IElement, IGroup } from '@antv/g-canvas';
import { get, isBoolean, isNil, last, maxBy, set, values } from 'lodash';
import {
get,
isBoolean,
isNil,
isNumber,
last,
maxBy,
set,
values,
} from 'lodash';
import { TableDataCell } from '../cell';
import {
FRONT_GROUND_GROUP_COL_FROZEN_Z_INDEX,
Expand Down Expand Up @@ -309,7 +318,7 @@ export class TableFacet extends BaseFacet {
levelSample.height = this.getColNodeHeight(levelSample);
colsHierarchy.height += levelSample.height;
}
const adaptiveColWitdth = this.getAdaptiveColWidth(colLeafNodes);
const adaptiveColWidth = this.getAdaptiveColWidth(colLeafNodes);
let currentCollIndex = 0;

for (let i = 0; i < allNodes.length; i++) {
Expand All @@ -320,7 +329,7 @@ export class TableFacet extends BaseFacet {
currentNode.x = preLeafNode.x + preLeafNode.width;
currentNode.width = this.calculateColLeafNodesWidth(
currentNode,
adaptiveColWitdth,
adaptiveColWidth,
);
layoutCoordinate(this.cfg, null, currentNode);
colsHierarchy.width += currentNode.width;
Expand Down Expand Up @@ -394,13 +403,13 @@ export class TableFacet extends BaseFacet {
const cellDraggedWidth = this.getCellDraggedWidth(col);

// 1. 拖拽后的宽度优先级最高
if (cellDraggedWidth) {
if (isNumber(cellDraggedWidth)) {
return cellDraggedWidth;
}

// 2. 其次是自定义, 返回 null 则使用默认宽度
const cellCustomWidth = this.getCellCustomWidth(col, colCfg?.width);
if (!isNil(cellCustomWidth)) {
if (isNumber(cellCustomWidth)) {
return cellCustomWidth;
}

Expand Down Expand Up @@ -471,7 +480,7 @@ export class TableFacet extends BaseFacet {
);

const customHeight = heightByField?.[String(index)];
if (customHeight) {
if (isNumber(customHeight)) {
return customHeight;
}
}
Expand Down