Skip to content

Commit

Permalink
refactor: 抽取公用变量、方法到 BaseDataCellCopy 基类
Browse files Browse the repository at this point in the history
  • Loading branch information
Harvey Wang committed May 14, 2024
1 parent 8709849 commit 2b34b65
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
13 changes: 12 additions & 1 deletion packages/s2-core/src/utils/export/copy/base-data-cell-copy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { TAB_SEPARATOR, type DataItem } from '../../../common';
import {
AsyncRenderThreshold,
TAB_SEPARATOR,
type DataItem,
} from '../../../common';
import type {
CopyableHTML,
CopyablePlain,
Expand All @@ -16,6 +20,13 @@ export abstract class BaseDataCellCopy {

protected config: CopyAndExportUnifyConfig;

protected idleCallbackCount: number;

protected initIdleCallbackCount(rowLength: number) {
this.idleCallbackCount =
rowLength >= AsyncRenderThreshold ? AsyncRenderThreshold : rowLength;
}

constructor(params: SheetCopyConstructorParams) {
const { spreadsheet, isExport = false, config } = params;

Expand Down
24 changes: 11 additions & 13 deletions packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
zip,
} from 'lodash';
import {
AsyncRenderThreshold,
CornerNodeType,
EXTRA_FIELD,
VALUE_FIELD,
Expand Down Expand Up @@ -153,18 +152,20 @@ export class PivotDataCellCopy extends BaseDataCellCopy {
// 因为每次 requestIdleCallback 执行的时间不一样,所以需要记录下当前执行到的 this.leafRowNodes 和 this.leafColNodes
const dataMatrixIdleCallback = (deadline: IdleDeadline) => {
const rowLength: number = this.leafRowNodes.length;

// requestIdleCallback 浏览器空闲时会多次执行, 只有一行数据时执行一次即可, 避免生成重复数据
let count =
rowLength >= AsyncRenderThreshold
? AsyncRenderThreshold
: rowLength;
this.initIdleCallbackCount(rowLength);

while (
(deadline.timeRemaining() > 0 || deadline.didTimeout) &&
rowIndex <= rowLength - 1 &&
count > 0
this.idleCallbackCount > 0
) {
for (let j = rowIndex; j < rowLength && count > 0; j++) {
for (
let j = rowIndex;
j < rowLength && this.idleCallbackCount > 0;
j++
) {
const row: DataItem[] = [];
const rowNode = this.leafRowNodes[j];

Expand All @@ -185,18 +186,15 @@ export class PivotDataCellCopy extends BaseDataCellCopy {
}
rowIndex++;
matrix.push(row);
count--;
this.idleCallbackCount--;
}
}

if (rowIndex === rowLength) {
resolve(matrix);
} else {
// 重置 count,避免下次 requestIdleCallback 时 count 为 0
count =
rowLength >= AsyncRenderThreshold
? AsyncRenderThreshold
: rowLength;
// 重置 idleCallbackCount,避免下次 requestIdleCallback 时 idleCallbackCount 为 0
this.initIdleCallbackCount(rowLength);

requestIdleCallback(dataMatrixIdleCallback);
}
Expand Down
24 changes: 11 additions & 13 deletions packages/s2-core/src/utils/export/copy/table-copy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { map, zip } from 'lodash';
import {
AsyncRenderThreshold,
SERIES_NUMBER_FIELD,
getDefaultSeriesNumberText,
type CellMeta,
Expand Down Expand Up @@ -94,20 +93,22 @@ class TableDataCellCopy extends BaseDataCellCopy {
try {
const dataMatrixIdleCallback = (deadline: IdleDeadline) => {
const rowLength = this.displayData.length;

// requestIdleCallback 浏览器空闲时会多次执行, 只有一行数据时执行一次即可, 避免生成重复数据
let count =
rowLength >= AsyncRenderThreshold
? AsyncRenderThreshold
: rowLength;
this.initIdleCallbackCount(rowLength);

while (
(deadline.timeRemaining() > 0 ||
deadline.didTimeout ||
process.env['NODE_ENV'] === 'test') &&
rowIndex <= rowLength - 1 &&
count > 0
this.idleCallbackCount > 0
) {
for (let j = rowIndex; j < rowLength && count > 0; j++) {
for (
let j = rowIndex;
j < rowLength && this.idleCallbackCount > 0;
j++
) {
const rowData = this.displayData[j];
const row: string[] = [];

Expand All @@ -134,18 +135,15 @@ class TableDataCellCopy extends BaseDataCellCopy {
// 生成一行数据后,rowIndex + 1,下次 requestIdleCallback 时从下一行开始
rowIndex++;
result.push(row);
count--;
this.idleCallbackCount--;
}
}

if (rowIndex === rowLength) {
resolve(result);
} else {
// 重置 count,避免下次 requestIdleCallback 时 count 为 0
count =
rowLength >= AsyncRenderThreshold
? AsyncRenderThreshold
: rowLength;
// 重置 idleCallbackCount,避免下次 requestIdleCallback 时 idleCallbackCount 为 0
this.initIdleCallbackCount(rowLength);

requestIdleCallback(dataMatrixIdleCallback);
}
Expand Down

0 comments on commit 2b34b65

Please sign in to comment.