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

feat(components): 组件层更新时增加 loading 效果 close #1790 #2762

Merged
merged 6 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ CNAME
LICENSE
s2-site/.cache
s2-site/public
s2-site/*.tsx
.history
esm
dist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
expectCornerIconName(s2, 'Plus');
});

test('should emit collapse event', () => {
test('should emit collapse event', async () => {
const onCollapsed = jest.fn();

s2.on(S2Event.ROW_CELL_COLLAPSED, onCollapsed);
Expand All @@ -301,19 +301,21 @@ describe('SpreadSheet Collapse/Expand Tests', () => {

s2.emit(S2Event.ROW_CELL_COLLAPSED__PRIVATE, treeRowType);

await sleep(500);
expect(onCollapsed).toHaveBeenCalledWith(params);
expectCornerIconName(s2, 'Minus');
});

test('should emit collapse all event', () => {
test('should emit collapse all event', async () => {
const onCollapsed = jest.fn();

s2.on(S2Event.ROW_CELL_ALL_COLLAPSED, onCollapsed);

s2.emit(S2Event.ROW_CELL_ALL_COLLAPSED__PRIVATE, false);

await sleep(500);
expect(onCollapsed).toHaveBeenCalledWith(true);
expectCornerIconName(s2, 'Minus');
expectCornerIconName(s2, 'Plus');
});

// https://github.com/antvis/S2/issues/2607
Expand Down
16 changes: 11 additions & 5 deletions packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import { cloneDeep, last } from 'lodash';
import dataCfg from 'tests/data/simple-data.json';
import { waitForRender } from 'tests/util';
import { createPivotSheet, getContainer, sleep } from 'tests/util/helpers';
import type {
import {
BaseEvent,
BaseTooltipOperatorMenuOptions,
CornerCell,
HeaderCell,
TooltipOptions,
} from '../../../src';
Expand Down Expand Up @@ -843,7 +844,7 @@ describe('PivotSheet Tests', () => {
);

await pivotSheet.render();
const extraField = last(pivotSheet.facet.getCornerCells());
const extraField = last<CornerCell>(pivotSheet.facet.getCornerCells());

expect(extraField?.getActualText()).toEqual('数值');
});
Expand All @@ -867,16 +868,19 @@ describe('PivotSheet Tests', () => {

await pivotSheet.render();

const extraField = last(pivotSheet.facet.getCornerCells());
const extraField = last<CornerCell>(pivotSheet.facet.getCornerCells());

expect(extraField?.getActualText()).toEqual(cornerExtraFieldText);
});

describe('Tree Collapse Tests', () => {
test('should collapse rows with tree mode', () => {
test('should collapse rows with tree mode', async () => {
s2.setOptions({
hierarchyType: 'tree',
});

await s2.render(false);

const renderSpy = jest
.spyOn(s2, 'render')
.mockImplementation(async () => {});
Expand All @@ -901,6 +905,8 @@ describe('PivotSheet Tests', () => {

s2.emit(S2Event.ROW_CELL_COLLAPSED__PRIVATE, treeRowType);

await sleep(500);

expect(collapseRows).toHaveBeenCalledWith(collapsedRowsType);
expect(s2.options.style?.rowCell?.collapseFields).toEqual(
collapsedRowsType.collapseFields,
Expand Down Expand Up @@ -1187,7 +1193,7 @@ describe('PivotSheet Tests', () => {

// g5.0 destroy
expect(destroyFn).toHaveBeenCalled();
expect(document.body.contains(s2.getCanvasElement())).toBeFalse();
expect(document.body.contains(s2.getCanvasElement())).toBeFalsy();
});

describe('Test Layout by dataCfg fields', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/s2-core/src/common/interface/s2Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ export interface S2Options<
frozen?: S2PivotSheetFrozenOptions & S2BaseFrozenOptions;
}

/**
* 自定义渲染模式
*/
export interface S2RenderOptions {
/**
* 是否重新加载数据
Expand All @@ -374,7 +377,7 @@ export interface S2RenderOptions {
/**
* 是否重新生成数据集
*/
reBuildDataSet?: boolean;
rebuildDataSet?: boolean;

/**
* 是否重新生成列头隐藏信息
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/facet/header/corner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class CornerHeader extends BaseHeader<CornerHeaderConfig> {
}

/**
* Make cornerHeader scroll with hScrollBar
* Make cornerHeader scroll with hScrollBar
* @param scrollX
*/
public onCorScroll(scrollX: number, type: string): void {
Expand Down
5 changes: 3 additions & 2 deletions packages/s2-core/src/facet/table-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class TableFacet extends FrozenFacet {
}
}

private onSortHandler = (sortParams: SortParams) => {
private onSortHandler = async (sortParams: SortParams) => {
const s2 = this.spreadsheet;
let params = sortParams;

Expand Down Expand Up @@ -274,7 +274,8 @@ export class TableFacet extends FrozenFacet {

set(s2.dataCfg, 'sortParams', [...oldConfigs, ...params]);
s2.setDataCfg(s2.dataCfg);
s2.render(true);
await s2.render(true);

s2.emit(
S2Event.RANGE_SORTED,
(s2.dataSet as TableDataSet).getDisplayDataSet(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class RowColumnClick extends BaseEvent implements BaseEventImplement {
await hideColumnsByThunkGroup(this.spreadsheet, selectedColumnFields, true);
}

private handleExpandIconClick(node: Node) {
private async handleExpandIconClick(node: Node) {
const lastHiddenColumnsDetail = this.spreadsheet.store.get(
'hiddenColumnsDetail',
[],
Expand Down Expand Up @@ -249,6 +249,6 @@ export class RowColumnClick extends BaseEvent implements BaseEventImplement {
});
this.spreadsheet.store.set('hiddenColumnsDetail', hiddenColumnsDetail);
this.spreadsheet.interaction.reset();
this.spreadsheet.render(false);
await this.spreadsheet.render(false);
}
}
8 changes: 4 additions & 4 deletions packages/s2-core/src/interaction/row-column-resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
});
}

private renderResizedResult() {
private async renderResizedResult() {
const resizeInfo = this.getResizeInfo();
const {
style,
Expand Down Expand Up @@ -607,7 +607,7 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
}

this.spreadsheet.store.set('resized', true);
this.render();
await this.render();
}

private getResizeInfo(): ResizeInfo {
Expand Down Expand Up @@ -638,10 +638,10 @@ export class RowColumnResize extends BaseEvent implements BaseEventImplement {
};
}

private render() {
private async render() {
this.resizeStartPosition = {};
this.resizeTarget = null;
this.resizeReferenceGroup = null;
this.spreadsheet.render(false);
await this.spreadsheet.render(false);
}
}
18 changes: 10 additions & 8 deletions packages/s2-core/src/sheet-type/pivot-sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ export class PivotSheet extends SpreadSheet {
return this.dataSet?.fields?.valueInCols!;
}

public clearDrillDownData(rowNodeId?: string, preventRender?: boolean) {
public async clearDrillDownData(rowNodeId?: string, preventRender?: boolean) {
if (this.dataSet instanceof PivotDataSet) {
const cleaned = this.dataSet.clearDrillDownData(rowNodeId);

if (cleaned && !preventRender) {
// 重置当前交互
this.interaction.reset();
this.render(false);
await this.render(false);
}
}
}
Expand All @@ -97,7 +97,7 @@ export class PivotSheet extends SpreadSheet {
);
}

protected handleRowCellCollapsed(data: RowCellCollapsedParams) {
protected async handleRowCellCollapsed(data: RowCellCollapsedParams) {
const { isCollapsed, node } = data;
const { collapseFields: defaultCollapsedFields } =
this.options.style?.rowCell!;
Expand All @@ -114,15 +114,16 @@ export class PivotSheet extends SpreadSheet {
},
},
});
this.render(false);

await this.render(false);
this.emit(S2Event.ROW_CELL_COLLAPSED, {
isCollapsed,
collapseFields,
node,
});
}

protected handleRowCellToggleCollapseAll(isCollapsed: boolean) {
protected async handleRowCellToggleCollapseAll(isCollapsed: boolean) {
const collapseAll = !isCollapsed;

this.setOptions({
Expand All @@ -134,11 +135,12 @@ export class PivotSheet extends SpreadSheet {
},
},
});
this.render(false);

await this.render(false);
this.emit(S2Event.ROW_CELL_ALL_COLLAPSED, collapseAll);
}

public groupSortByMethod(sortMethod: SortMethod, meta: Node) {
public async groupSortByMethod(sortMethod: SortMethod, meta: Node) {
const { rows, columns } = this.dataCfg.fields;
const { hideValue } = this.options.style!.colCell!;
const sortField = this.isValueInCols() ? last(rows) : last(columns);
Expand Down Expand Up @@ -173,6 +175,6 @@ export class PivotSheet extends SpreadSheet {
...this.dataCfg,
sortParams,
});
this.render();
await this.render();
}
}
21 changes: 12 additions & 9 deletions packages/s2-core/src/sheet-type/spread-sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,12 @@ export abstract class SpreadSheet extends EE {
public abstract clearDrillDownData(
rowNodeId?: string,
preventRender?: boolean,
): void;
): Promise<void>;

public abstract groupSortByMethod(sortMethod: SortMethod, meta: Node): void;
public abstract groupSortByMethod(
sortMethod: SortMethod,
meta: Node,
): Promise<void> | void;

public constructor(
dom: S2MountContainer,
Expand Down Expand Up @@ -240,9 +243,9 @@ export abstract class SpreadSheet extends EE {
return this.options.tooltip?.render?.(this) || new BaseTooltip(this);
}

private getTargetCell(target: CellEventTarget) {
private getTargetCell(target: CellEventTarget): S2CellType {
// 刷选等场景, 以最后一个发生交互的单元格为准
return this.getCell(target) || last(this.interaction.getInteractedCells());
return this.getCell(target) || last(this.interaction.getInteractedCells())!;
}

/**
Expand Down Expand Up @@ -419,13 +422,13 @@ export abstract class SpreadSheet extends EE {

const {
reloadData = true,
reBuildDataSet = false,
rebuildDataSet = false,
reBuildHiddenColumnsDetail = true,
} = options || {};

this.emit(S2Event.LAYOUT_BEFORE_RENDER);

if (reBuildDataSet) {
if (rebuildDataSet) {
this.dataSet = this.getDataSet();
}

Expand All @@ -452,7 +455,7 @@ export abstract class SpreadSheet extends EE {
s2.render(false)
s2.render({
reloadData: true;
reBuildDataSet: true;
rebuildDataSet: true;
reBuildHiddenColumnsDetail: true;
})
*/
Expand Down Expand Up @@ -902,8 +905,8 @@ export abstract class SpreadSheet extends EE {
menu: {
selectedKeys,
items: menuItems,
onClick: ({ key: sortMethod }) => {
this.groupSortByMethod(sortMethod as SortMethod, meta);
onClick: async ({ key: sortMethod }) => {
await this.groupSortByMethod(sortMethod as SortMethod, meta);
this.emit(S2Event.RANGE_SORTED, event);
},
},
Expand Down
7 changes: 4 additions & 3 deletions packages/s2-core/src/sheet-type/table-sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export class TableSheet extends SpreadSheet {
return false;
}

public clearDrillDownData(): void {}
// eslint-disable-next-line no-empty-function
public async clearDrillDownData() {}

/**
* Check if the value is in the columns
Expand Down Expand Up @@ -81,7 +82,7 @@ export class TableSheet extends SpreadSheet {
this.off(S2Event.RANGE_FILTER);
}

public groupSortByMethod = (sortMethod: SortMethod, meta: Node) => {
public groupSortByMethod(sortMethod: SortMethod, meta: Node) {
const { field } = meta;

const sortParam: SortParam = {
Expand All @@ -91,5 +92,5 @@ export class TableSheet extends SpreadSheet {

this.updateSortMethodMap(meta.id, sortMethod);
this.emit(S2Event.RANGE_SORT, [sortParam]);
};
}
}
18 changes: 10 additions & 8 deletions packages/s2-core/src/ui/hd-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,19 @@ export class HdAdapter {
* DPR 改变也会触发 visualViewport 的 resize 事件, 预期是只监听双指缩放, 所以这里规避掉
* @see https://github.com/antvis/S2/issues/2072
*/
private renderByZoomScaleWithoutResizeEffect = (event: Event) => {
private renderByZoomScaleWithoutResizeEffect = async (event: Event) => {
this.isDevicePixelRatioChange = false;
this.renderByZoomScale(event);
await this.renderByZoomScale(event);
};

private renderByDevicePixelRatioChanged = () => {
private renderByDevicePixelRatioChanged = async () => {
this.isDevicePixelRatioChange = true;
this.renderByDevicePixelRatio();
await this.renderByDevicePixelRatio();
};

private renderByDevicePixelRatio = (ratio = window.devicePixelRatio) => {
private renderByDevicePixelRatio = async (
ratio = window.devicePixelRatio,
) => {
const {
container,
options: { width, height },
Expand All @@ -112,14 +114,14 @@ export class HdAdapter {
container.getConfig().devicePixelRatio = ratio;
container.resize(width!, height!);

this.spreadsheet.render(false);
await this.spreadsheet.render(false);
};

private renderByZoomScale = debounce((event: Event) => {
private renderByZoomScale = debounce(async (event: Event) => {
const ratio = Math.ceil((event.target as VisualViewport)?.scale);

if (ratio >= 1 && !this.isDevicePixelRatioChange) {
this.renderByDevicePixelRatio(ratio);
await this.renderByDevicePixelRatio(ratio);
}
}, 350);
}
Loading
Loading