Skip to content

Commit

Permalink
refactor: 统一单元格创建方式
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Dec 20, 2024
1 parent 5049c86 commit cc2f909
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
9 changes: 6 additions & 3 deletions packages/s2-core/__tests__/unit/utils/merge-cell-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('Merge Cells Test', () => {
},
getCellMeta: jest.fn(),
getDataCells: jest.fn(() => mockAllVisibleCells),
createDataCell: jest.fn(() => ({})),
} as unknown as BaseFacet;

mockOneCellEdges = [
Expand Down Expand Up @@ -220,9 +221,11 @@ describe('Merge Cells Test', () => {
.fn()
.mockImplementation((scalar) => mockMergeCellInfo[scalar]);

mockInstance.options = {
dataCell: ((meta: ViewMeta) => meta) as unknown as DataCellCallback,
};
mockInstance.facet.createDataCell = jest
.fn()
.mockImplementation(
((meta: ViewMeta) => meta) as unknown as DataCellCallback,
);

const { cells, cellsMeta } = getInvisibleInfo(
mockMergeCellInfo,
Expand Down
1 change: 1 addition & 0 deletions packages/s2-core/__tests__/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export const createFakeSpreadSheet = (config?: {
getFrozenOptions: jest.fn().mockReturnValue({
...DEFAULT_FROZEN_COUNTS,
}),
createDataCell: jest.fn(),
} as unknown as BaseFacet;
s2.container.render = jest.fn();
s2.store = new Store();
Expand Down
55 changes: 33 additions & 22 deletions packages/s2-core/src/facet/base-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,28 @@ export abstract class BaseFacet {
return this.getRealScrollX(scrollX);
}

public createDataCell(viewMeta: ViewMeta | null) {
if (!viewMeta) {
return;
}

const cell = this.spreadsheet.options.dataCell?.(
viewMeta,
this.spreadsheet,
)!;

if (!cell) {
return;
}

const { rowIndex, colIndex } = viewMeta;

cell.position = [rowIndex, colIndex];
cell.name = `${rowIndex}-${colIndex}`;

return cell;
}

realDataCellRender = (scrollX: number, scrollY: number) => {
const indexes = this.calculateXYIndexes(scrollX, scrollY);

Expand All @@ -1620,35 +1642,24 @@ export abstract class BaseFacet {

DebuggerUtil.getInstance().debugCallback(DEBUG_VIEW_RENDER, () => {
// add new cell in panelCell
each(willAddDataCells, ([i, j]) => {
const viewMeta = this.getCellMeta(j, i);

if (viewMeta) {
const cell = this.spreadsheet.options.dataCell?.(
viewMeta,
this.spreadsheet,
)!;

if (!cell) {
return;
}
each(willAddDataCells, ([colIndex, rowIndex]) => {
const viewMeta = this.getCellMeta(rowIndex, colIndex);
const cell = this.createDataCell(viewMeta);

cell.position = [j, i];
// mark cell for removing
cell.name = `${i}-${j}`;
this.addDataCell(cell);
if (!cell) {
return;
}

this.addDataCell(cell);
});
const allDataCells = getAllChildCells<DataCell>(
this.panelGroup.children as DataCell[],
DataCell,
);

const allDataCells = this.getDataCells();

// remove cell from panelCell
each(willRemoveDataCells, ([i, j]) => {
each(willRemoveDataCells, ([colIndex, rowIndex]) => {
const mountedDataCell = find(
allDataCells,
(cell) => cell.name === `${i}-${j}`,
(cell) => cell.name === `${rowIndex}-${colIndex}`,
);

mountedDataCell?.remove();
Expand Down
7 changes: 2 additions & 5 deletions packages/s2-core/src/facet/frozen-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,9 @@ export abstract class FrozenFacet extends BaseFacet {

if (viewMeta) {
viewMeta['isFrozenCorner'] = true;
const cell = this.spreadsheet.options.dataCell?.(
viewMeta,
this.spreadsheet,
)!;
const cell = this.createDataCell(viewMeta);

group.appendChild(cell);
group.appendChild(cell!);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class DataCellBrushSelection extends BaseBrushSelection {
meta.colIndex,
)!;

return this.spreadsheet.options.dataCell!(viewMeta, this.spreadsheet);
return this.spreadsheet.facet.createDataCell(viewMeta)!;
});
}

Expand Down
18 changes: 5 additions & 13 deletions packages/s2-core/src/utils/interaction/merge-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,17 @@ export const getInvisibleInfo = (
sheet: SpreadSheet,
) => {
const cells: DataCell[] = [];
let viewMeta: ViewMeta | undefined;
let viewMeta: ViewMeta | undefined | null;

forEach(invisibleCellInfo, (cellInfo) => {
const meta = sheet?.facet?.getCellMeta(
cellInfo.rowIndex!,
cellInfo.colIndex!,
);
const cell = sheet?.facet?.createDataCell(meta);

if (meta) {
const cell = sheet?.options?.dataCell?.(meta, meta.spreadsheet)!;

if (cell) {
/**
* 避免移除可视范围后丢失 position 属性: https://github.com/antvis/S2/issues/3048
* 绘制线依赖 position 属性 => packages/s2-core/src/utils/cell/merged-cell.ts#getRightAndBottomCells
*/
cell.position = [cellInfo.rowIndex!, cellInfo.colIndex!];
cells.push(cell!);
}
if (cell) {
cells.push(cell!);

viewMeta = cellInfo?.showText ? meta : viewMeta;
}
Expand Down Expand Up @@ -110,7 +102,7 @@ export const getTempMergedCell = (
cellsInfos,
allVisibleCells,
);
let viewMeta: ViewMeta | Node | undefined = cellsMeta;
let viewMeta: ViewMeta | Node | undefined | null = cellsMeta;
let mergedAllCells: DataCell[] = cells;
// some cells are invisible and some cells are visible
const isPartiallyVisible =
Expand Down

0 comments on commit cc2f909

Please sign in to comment.