antd-import-export 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -42
- package/README_EN.md +59 -28
- package/dist/components/EditableImportCell.d.ts +12 -0
- package/dist/components/ExportButton.d.ts +20 -0
- package/dist/components/ImportButton.d.ts +17 -0
- package/dist/components/importPreviewStatus.d.ts +20 -0
- package/dist/core/excel/exportFile.d.ts +16 -0
- package/dist/core/excel/runtime.d.ts +2 -0
- package/dist/core/httpError.d.ts +16 -1
- package/dist/core/limits.d.ts +5 -1
- package/dist/core/task/exportRunner.d.ts +4 -0
- package/dist/core/task/importRunner.d.ts +21 -0
- package/dist/core/task/index.d.ts +12 -0
- package/dist/core/task/stats.d.ts +16 -0
- package/dist/core/task/store.d.ts +17 -0
- package/dist/core/task/types.d.ts +105 -0
- package/dist/index.cjs +6 -6
- package/dist/index.d.ts +8 -12
- package/dist/index.mjs +827 -777
- package/dist/react/useTaskDetail.d.ts +1 -1
- package/package.json +1 -1
- package/dist/components/BatchExportButton.d.ts +0 -10
- package/dist/components/ExcelExportButton.d.ts +0 -15
- package/dist/components/ExcelImportButton.d.ts +0 -11
- package/dist/core/taskManager.d.ts +0 -123
package/README.md
CHANGED
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
|
|
16
16
|
[在线演示](https://demo.29dev.cn/antd-import-export/)
|
|
17
17
|
|
|
18
|
-
|
|
19
18
|
## 依赖要求
|
|
20
19
|
|
|
21
20
|
| 依赖 | 版本 |
|
|
@@ -25,20 +24,6 @@
|
|
|
25
24
|
| antd | >=6.0.0 |
|
|
26
25
|
| @ant-design/icons | >=5.0.0 |
|
|
27
26
|
|
|
28
|
-
## 大文件表现
|
|
29
|
-
|
|
30
|
-
Excel 的解析和生成在主线程执行——这是 ExcelJS 自身的用法,官方没有提供 Worker 方案。代价是处理大文件时页面会短暂无响应:
|
|
31
|
-
|
|
32
|
-
| 行数 | 导入解析 | 导出生成 |
|
|
33
|
-
| --- | --- | --- |
|
|
34
|
-
| 1,000 | 约 25ms | 约 60ms |
|
|
35
|
-
| 5,000 | 约 125ms | 约 300ms |
|
|
36
|
-
| 20,000 | 约 500ms | 约 1 秒 |
|
|
37
|
-
|
|
38
|
-
单次上限 2 万行,超出会在前端直接拦截。生成文件期间取消按钮会禁用,因为主线程被占满时点击不会即时生效,避免让用户误以为没响应。
|
|
39
|
-
|
|
40
|
-
绝大多数实际文件在几千行以内,感知不到停顿。
|
|
41
|
-
|
|
42
27
|
## 字段定义
|
|
43
28
|
|
|
44
29
|
导入导出的一切都由 `FieldSchema[]` 驱动:导出用它生成表头,导入模板用它写填写要求,解析时用它逐格校验。
|
|
@@ -69,10 +54,10 @@ const fieldSchemas: FieldSchema[] = [
|
|
|
69
54
|
| `boolean` | required / true_label / false_label / default_value | 单元格填文案,接口值转成布尔 |
|
|
70
55
|
| `date` | required / default_value | 严格 `YYYY-MM-DD` |
|
|
71
56
|
| `time` | required / default_value | 严格 `HH:mm`,输出补零 |
|
|
72
|
-
| `datetime` | required / default_value | 严格 `YYYY-MM-DD HH:mm` |
|
|
73
|
-
| `list` | required / list_max_length / text_max_length / item_key? / default_value |
|
|
57
|
+
| `datetime` | required / default_value | 严格 `YYYY-MM-DD HH:mm` 或 `YYYY-MM-DD HH:mm:ss`,秒为 0 时不带秒 |
|
|
58
|
+
| `list` | required / list_max_length / text_max_length / item_key? / default_value | 中英文分号分隔,导出统一用英文分号 |
|
|
74
59
|
| `single_choice` | required / option_map / default_value | 单元格填文案,接口值取 option_map 里的值 |
|
|
75
|
-
| `multiple_choice` | required / option_map / default_value |
|
|
60
|
+
| `multiple_choice` | required / option_map / default_value | 中英文分号分隔,导出统一用英文分号 |
|
|
76
61
|
|
|
77
62
|
`importable: false` 的列只出现在导出文件里,不参与导入模板和解析。
|
|
78
63
|
|
|
@@ -94,59 +79,77 @@ const result = validateField("19.99", {
|
|
|
94
79
|
## 组件
|
|
95
80
|
|
|
96
81
|
```tsx
|
|
97
|
-
import {
|
|
82
|
+
import { ImportButton, ExportButton, exportRowsToFile } from "antd-import-export";
|
|
98
83
|
|
|
99
84
|
// 导入: 内置模板下载、文件解析、预览编辑、分批提交、行级错误回填
|
|
100
|
-
<
|
|
85
|
+
<ImportButton
|
|
101
86
|
title="供应商"
|
|
102
87
|
fieldSchemas={dataSchema}
|
|
103
|
-
importRows={(
|
|
104
|
-
|
|
88
|
+
importRows={(rows, signal) => supplierImportData(rows, signal)}
|
|
89
|
+
afterImport={onRefresh}
|
|
105
90
|
/>;
|
|
106
91
|
|
|
107
92
|
// 按当前筛选条件导出, 内部用游标分批拉取
|
|
108
|
-
<
|
|
93
|
+
<ExportButton
|
|
109
94
|
title="供应商"
|
|
110
95
|
fieldSchemas={dataSchema}
|
|
111
|
-
filters={queryForm}
|
|
112
96
|
totalCount={dataCount}
|
|
113
|
-
|
|
97
|
+
// 筛选条件在自己的 exportRows 里合并, 分页参数由包传入
|
|
98
|
+
exportRows={(params, signal) => supplierExportData({ ...queryForm, ...params }, signal)}
|
|
114
99
|
/>;
|
|
115
|
-
|
|
116
|
-
// 导出已勾选的行
|
|
117
|
-
<BatchExportButton title="供应商" fieldSchemas={dataSchema} selectedRows={selectedRows} />;
|
|
118
100
|
```
|
|
119
101
|
|
|
120
|
-
|
|
102
|
+
两个组件都不发请求,只调用你传进来的 `importRows` / `exportRows`。
|
|
103
|
+
|
|
104
|
+
退出登录或切换账号时调用 `resetTasks()`,会中止所有在途的导入导出并清空任务。
|
|
121
105
|
|
|
122
|
-
|
|
106
|
+
导出已勾选的行这类「数据已经在内存里」的场景,包内只提供函数,按钮由你自己渲染:
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
import { exportRowsToFile } from "antd-import-export";
|
|
110
|
+
|
|
111
|
+
await exportRowsToFile({ title: "供应商", fieldSchemas: dataSchema, rows: selectedRows });
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### ImportButton
|
|
123
115
|
|
|
124
116
|
| prop | 类型 | 说明 |
|
|
125
117
|
| --- | --- | --- |
|
|
126
118
|
| `title` | `string` | 按钮、模板文件名、任务标题共用 |
|
|
127
119
|
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
120
|
+
| `maxRows` | `number` | 单次导入上限,默认 2 万行;超出直接拦在提交前 |
|
|
128
121
|
| `importRows` | `(rows, signal) => Promise<Record<string, unknown>[]>` | 一批行提交,返回等长结果;**空对象表示成功** |
|
|
129
|
-
| `
|
|
122
|
+
| `beforeImport` | `() => void \| Promise<void>` | 提交前触发,可做二次确认或自定义校验;抛错即中止 |
|
|
123
|
+
| `afterImport` | `() => void` | 任务结束(成功/失败/取消)时触发一次,通常用来刷新列表 |
|
|
130
124
|
|
|
131
|
-
###
|
|
125
|
+
### ExportButton
|
|
132
126
|
|
|
133
127
|
| prop | 类型 | 说明 |
|
|
134
128
|
| --- | --- | --- |
|
|
135
129
|
| `title` | `string` | 文件名与任务标题 |
|
|
136
130
|
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
137
|
-
| `
|
|
138
|
-
| `
|
|
139
|
-
| `
|
|
140
|
-
| `
|
|
131
|
+
| `totalCount` | `number` | 列表当前条数,只用于判断有没有数据;上限以接口返回的总数为准 |
|
|
132
|
+
| `maxRows` | `number` | 单次导出上限,默认 2 万行;每页返回后都会校验 |
|
|
133
|
+
| `exportRows` | `(params, signal) => Promise<{ count, results }>` | 游标分页;**每行必须带数字 `id`** |
|
|
134
|
+
| `beforeExport` | `() => void \| Promise<void>` | 创建任务前触发,可做二次确认或自定义校验;抛错即中止 |
|
|
135
|
+
| `afterExport` | `() => void` | 任务结束(成功/失败/取消)时触发一次 |
|
|
141
136
|
|
|
142
|
-
###
|
|
137
|
+
### exportRowsToFile
|
|
143
138
|
|
|
144
|
-
|
|
|
139
|
+
| 参数 | 类型 | 说明 |
|
|
145
140
|
| --- | --- | --- |
|
|
146
|
-
| `title` | `string` |
|
|
141
|
+
| `title` | `string` | 用于生成 `标题_时间戳.xlsx` |
|
|
147
142
|
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
148
|
-
| `
|
|
149
|
-
| `
|
|
143
|
+
| `rows` | `object[]` | 已在内存中的行 |
|
|
144
|
+
| `maxRows` | `number` | 单次导出上限,默认 2 万行 |
|
|
145
|
+
|
|
146
|
+
返回 `Promise<Blob>`。空数组或超过 2 万行时**抛出可直接展示的错误**;不弹提示、不管理 loading,由调用方决定交互。
|
|
147
|
+
|
|
148
|
+
### 导入接口需要幂等
|
|
149
|
+
|
|
150
|
+
一批请求失败时,前端无法确定服务端是否已经写入。任务会把这些行标成「结果未知,重试可能重复」,用户继续任务时会重新提交这一批数据。
|
|
151
|
+
|
|
152
|
+
因此导入接口应按业务唯一键(如编号)做「存在则更新、不存在则新增」,不要在无唯一约束的情况下纯追加。
|
|
150
153
|
|
|
151
154
|
### 导入结果约定
|
|
152
155
|
|
|
@@ -166,7 +169,7 @@ import { ExcelImportButton, ExcelExportButton, BatchExportButton } from "antd-im
|
|
|
166
169
|
|
|
167
170
|
## Excel 读写
|
|
168
171
|
|
|
169
|
-
ExcelJS
|
|
172
|
+
ExcelJS 按需加载(首次导入/导出时才引入),解析和生成在主线程执行。组件接入后这一步由组件内部完成,也可以直接用:
|
|
170
173
|
|
|
171
174
|
```ts
|
|
172
175
|
import { parseImportFile, buildExportBlob, downloadImportTemplate } from "antd-import-export";
|
|
@@ -178,6 +181,10 @@ const blob = await buildExportBlob(rows, fieldSchemas);
|
|
|
178
181
|
await downloadImportTemplate(fieldSchemas, "供应商导入模板");
|
|
179
182
|
```
|
|
180
183
|
|
|
184
|
+
### 取消语义
|
|
185
|
+
|
|
186
|
+
`parseImportFile` / `buildExportBlob` 的 `signal` 只有在**开始前**和**同步计算结束后**才生效。ExcelJS 的 `load` / `writeBuffer` 内部是一整段同步执行,无法中途打断——这也是生成期间禁用取消按钮的原因。
|
|
187
|
+
|
|
181
188
|
## 模块格式
|
|
182
189
|
|
|
183
190
|
同时提供 ESM 与 CJS,类型声明通过 `exports` 分发。
|
package/README_EN.md
CHANGED
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
|
|
15
15
|
[Live demo](https://demo.29dev.cn/antd-import-export/)
|
|
16
16
|
|
|
17
|
-
|
|
18
17
|
## Requirements
|
|
19
18
|
|
|
20
19
|
| Package | Version |
|
|
@@ -24,20 +23,6 @@
|
|
|
24
23
|
| antd | >=6.0.0 |
|
|
25
24
|
| @ant-design/icons | >=5.0.0 |
|
|
26
25
|
|
|
27
|
-
## Large files
|
|
28
|
-
|
|
29
|
-
Parsing and generating happen on the main thread — that is how ExcelJS works, it ships no worker mode. Very large files block the UI briefly:
|
|
30
|
-
|
|
31
|
-
| Rows | Import parse | Export build |
|
|
32
|
-
| --- | --- | --- |
|
|
33
|
-
| 1,000 | ~25ms | ~60ms |
|
|
34
|
-
| 5,000 | ~125ms | ~300ms |
|
|
35
|
-
| 20,000 | ~500ms | ~1s |
|
|
36
|
-
|
|
37
|
-
The hard limit is 20,000 rows per operation. The cancel button is disabled while the file is being generated, because clicks cannot take effect while the main thread is busy.
|
|
38
|
-
|
|
39
|
-
Most real-world files stay in the low thousands of rows and show no noticeable pause.
|
|
40
|
-
|
|
41
26
|
## Field schema
|
|
42
27
|
|
|
43
28
|
Everything is driven by `FieldSchema[]`: export headers, template hints and cell-level validation.
|
|
@@ -62,10 +47,10 @@ const fieldSchemas: FieldSchema[] = [
|
|
|
62
47
|
| `boolean` | required / true_label / false_label / default_value | labels in the cell, boolean on the wire |
|
|
63
48
|
| `date` | required / default_value | strict `YYYY-MM-DD` |
|
|
64
49
|
| `time` | required / default_value | strict `HH:mm` |
|
|
65
|
-
| `datetime` | required / default_value | strict `YYYY-MM-DD HH:mm` |
|
|
66
|
-
| `list` | required / list_max_length / text_max_length / item_key? / default_value |
|
|
50
|
+
| `datetime` | required / default_value | strict `YYYY-MM-DD HH:mm` or `YYYY-MM-DD HH:mm:ss`; seconds are dropped when zero |
|
|
51
|
+
| `list` | required / list_max_length / text_max_length / item_key? / default_value | Chinese or English semicolons; exports always use `;` |
|
|
67
52
|
| `single_choice` | required / option_map / default_value | label in the cell, mapped value on the wire |
|
|
68
|
-
| `multiple_choice` | required / option_map / default_value |
|
|
53
|
+
| `multiple_choice` | required / option_map / default_value | Chinese or English semicolons; exports always use `;` |
|
|
69
54
|
|
|
70
55
|
Columns marked `importable: false` only appear in exported files.
|
|
71
56
|
|
|
@@ -86,27 +71,69 @@ validateField("19.99", {
|
|
|
86
71
|
## Components
|
|
87
72
|
|
|
88
73
|
```tsx
|
|
89
|
-
import {
|
|
74
|
+
import { ImportButton, ExportButton, exportRowsToFile } from "antd-import-export";
|
|
90
75
|
|
|
91
|
-
<
|
|
76
|
+
<ImportButton
|
|
92
77
|
title="Supplier"
|
|
93
78
|
fieldSchemas={fieldSchemas}
|
|
94
|
-
importRows={(
|
|
95
|
-
|
|
79
|
+
importRows={(rows, signal) => importSuppliers(rows, signal)}
|
|
80
|
+
afterImport={refresh}
|
|
96
81
|
/>;
|
|
97
82
|
|
|
98
|
-
<
|
|
83
|
+
<ExportButton
|
|
99
84
|
title="Supplier"
|
|
100
85
|
fieldSchemas={fieldSchemas}
|
|
101
|
-
filters={queryForm}
|
|
102
86
|
totalCount={totalCount}
|
|
103
|
-
|
|
87
|
+
// merge your own filters inside exportRows; paging params come from the package
|
|
88
|
+
exportRows={(params, signal) => exportSuppliers({ ...queryForm, ...params }, signal)}
|
|
104
89
|
/>;
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Neither component issues requests: they call the `importRows` / `exportRows` you provide.
|
|
93
|
+
|
|
94
|
+
Call `resetTasks()` on logout or account switch to abort every in-flight import/export and clear the task list.
|
|
95
|
+
|
|
96
|
+
To export rows that are already in memory, render your own button and call the function:
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
import { exportRowsToFile } from "antd-import-export";
|
|
105
100
|
|
|
106
|
-
|
|
101
|
+
await exportRowsToFile({ title: "Supplier", fieldSchemas, rows: selectedRows });
|
|
107
102
|
```
|
|
108
103
|
|
|
109
|
-
|
|
104
|
+
### ImportButton
|
|
105
|
+
|
|
106
|
+
| prop | type | notes |
|
|
107
|
+
| --- | --- | --- |
|
|
108
|
+
| `title` | `string` | shared by the button, template file name and task title |
|
|
109
|
+
| `fieldSchemas` | `FieldSchema[]` | field definitions |
|
|
110
|
+
| `maxRows` | `number` | per-import limit, defaults to 20,000 rows |
|
|
111
|
+
| `importRows` | `(rows, signal) => Promise<Record<string, unknown>[]>` | submits one batch, returns the same length; **empty object means success** |
|
|
112
|
+
| `beforeImport` | `() => void \| Promise<void>` | runs before submitting; throw to abort |
|
|
113
|
+
| `afterImport` | `() => void` | fires once when the task ends (completed / failed / canceled) |
|
|
114
|
+
|
|
115
|
+
### ExportButton
|
|
116
|
+
|
|
117
|
+
| prop | type | notes |
|
|
118
|
+
| --- | --- | --- |
|
|
119
|
+
| `title` | `string` | file name and task title |
|
|
120
|
+
| `fieldSchemas` | `FieldSchema[]` | field definitions |
|
|
121
|
+
| `totalCount` | `number` | current row count, only used to detect an empty list |
|
|
122
|
+
| `maxRows` | `number` | per-export limit, defaults to 20,000 rows; checked after every page |
|
|
123
|
+
| `exportRows` | `(params, signal) => Promise<{ count, results }>` | cursor paging; **every row needs a numeric `id`** |
|
|
124
|
+
| `beforeExport` | `() => void \| Promise<void>` | runs before the task is created; throw to abort |
|
|
125
|
+
| `afterExport` | `() => void` | fires once when the task ends (completed / failed / canceled) |
|
|
126
|
+
|
|
127
|
+
### exportRowsToFile
|
|
128
|
+
|
|
129
|
+
| option | type | notes |
|
|
130
|
+
| --- | --- | --- |
|
|
131
|
+
| `title` | `string` | used to build `title_timestamp.xlsx` |
|
|
132
|
+
| `fieldSchemas` | `FieldSchema[]` | field definitions |
|
|
133
|
+
| `rows` | `object[]` | rows already in memory |
|
|
134
|
+
| `maxRows` | `number` | per-export limit, defaults to 20,000 rows |
|
|
135
|
+
|
|
136
|
+
Returns `Promise<Blob>`. Throws a ready-to-display error when `rows` is empty or exceeds 20,000; it does not show messages or manage loading state.
|
|
110
137
|
|
|
111
138
|
### Two prerequisites
|
|
112
139
|
|
|
@@ -115,7 +142,7 @@ The components never issue requests themselves: they call the `importRows` / `fe
|
|
|
115
142
|
|
|
116
143
|
## Excel I/O
|
|
117
144
|
|
|
118
|
-
ExcelJS
|
|
145
|
+
ExcelJS is loaded on demand and runs on the main thread.
|
|
119
146
|
|
|
120
147
|
```ts
|
|
121
148
|
import { parseImportFile, buildExportBlob, downloadImportTemplate } from "antd-import-export";
|
|
@@ -125,6 +152,10 @@ const blob = await buildExportBlob(rows, fieldSchemas);
|
|
|
125
152
|
await downloadImportTemplate(fieldSchemas, "template");
|
|
126
153
|
```
|
|
127
154
|
|
|
155
|
+
### Cancellation semantics
|
|
156
|
+
|
|
157
|
+
The `signal` of `parseImportFile` / `buildExportBlob` only takes effect **before** the work starts and **after** the synchronous part finishes. ExcelJS runs `load` / `writeBuffer` as one synchronous block, so it cannot be interrupted mid-way — that is why the cancel button is disabled while the file is being generated.
|
|
158
|
+
|
|
128
159
|
## Module format
|
|
129
160
|
|
|
130
161
|
Ships both ESM and CJS, with type declarations resolved through `exports`.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { ImportRow } from "../core/excel/importRow";
|
|
3
|
+
import type { FieldSchema } from "../core/schema";
|
|
4
|
+
interface EditableImportCellProps {
|
|
5
|
+
field: FieldSchema;
|
|
6
|
+
record: ImportRow;
|
|
7
|
+
disabled: boolean;
|
|
8
|
+
onChange: (value: unknown) => void;
|
|
9
|
+
onSave: (value: unknown) => void;
|
|
10
|
+
}
|
|
11
|
+
declare const EditableImportCell: React.FC<EditableImportCellProps>;
|
|
12
|
+
export default EditableImportCell;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { FieldSchema } from "../core/schema";
|
|
3
|
+
import type { PagedData } from "../core/types";
|
|
4
|
+
export interface ExportButtonProps {
|
|
5
|
+
title: string;
|
|
6
|
+
fieldSchemas: FieldSchema[];
|
|
7
|
+
/** 当前列表的条数, 只用于判断有没有数据; 实际上限以接口返回的总数为准 */
|
|
8
|
+
totalCount: number;
|
|
9
|
+
/** 单次导出上限, 默认 2 万行 */
|
|
10
|
+
maxRows?: number;
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
/** 拉取一页待导出的数据; 分页参数由包传入, 筛选条件在这里合并 */
|
|
13
|
+
exportRows: (params: Record<string, unknown>, signal: AbortSignal) => Promise<PagedData<object>>;
|
|
14
|
+
/** 创建任务前触发, 可做二次确认或自定义校验; 抛错则中止, 不创建任务 */
|
|
15
|
+
beforeExport?: () => void | Promise<void>;
|
|
16
|
+
/** 导出任务结束(成功/失败/取消)时回调一次 */
|
|
17
|
+
afterExport?: () => void;
|
|
18
|
+
}
|
|
19
|
+
declare const ExportButton: React.FC<ExportButtonProps>;
|
|
20
|
+
export default ExportButton;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { ImportRowErrorBody } from "../core/types";
|
|
3
|
+
import type { FieldSchema } from "../core/schema";
|
|
4
|
+
export interface ImportButtonProps {
|
|
5
|
+
title: string;
|
|
6
|
+
fieldSchemas: FieldSchema[];
|
|
7
|
+
/** 单次导入上限, 默认 2 万行 */
|
|
8
|
+
maxRows?: number;
|
|
9
|
+
/** 提交一批待导入的行, 返回等长的逐行结果; 空对象表示成功 */
|
|
10
|
+
importRows: (rows: Record<string, unknown>[], signal: AbortSignal) => Promise<ImportRowErrorBody[]>;
|
|
11
|
+
/** 开始提交前触发, 可做二次确认或自定义校验; 抛错则中止, 不创建任务 */
|
|
12
|
+
beforeImport?: () => void | Promise<void>;
|
|
13
|
+
/** 导入任务结束(成功/失败/取消)时回调一次, 通常用来刷新列表 */
|
|
14
|
+
afterImport: () => void;
|
|
15
|
+
}
|
|
16
|
+
declare const ImportButton: React.FC<ImportButtonProps>;
|
|
17
|
+
export default ImportButton;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ImportRow } from "../core/excel/importRow";
|
|
2
|
+
import type { TaskItem } from "../core/task";
|
|
3
|
+
export type DataStatusKey = "valid" | "invalid";
|
|
4
|
+
export type ImportStatusKey = "pending" | "running" | "completed" | "failed" | "canceled";
|
|
5
|
+
export declare function getTaskMeta(task: TaskItem): {
|
|
6
|
+
label: string;
|
|
7
|
+
color: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function getRemainingTimeText(task: TaskItem, remainingSeconds: number | null): string | null;
|
|
10
|
+
export declare function getTaskProgressStatus(task: TaskItem): "exception" | "success" | "normal";
|
|
11
|
+
export declare const dataStatusMeta: Record<DataStatusKey, {
|
|
12
|
+
label: string;
|
|
13
|
+
color: string;
|
|
14
|
+
}>;
|
|
15
|
+
export declare const importStatusMeta: Record<ImportStatusKey, {
|
|
16
|
+
label: string;
|
|
17
|
+
color: string;
|
|
18
|
+
}>;
|
|
19
|
+
export declare function getDataStatusKey(record: ImportRow): DataStatusKey;
|
|
20
|
+
export declare function getImportStatusKey(record: ImportRow, taskId: string | undefined, task: TaskItem | undefined): ImportStatusKey;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FieldSchema } from "../schema";
|
|
2
|
+
/** 生成 `标题_20261123153300` 形式的文件名(不含扩展名) */
|
|
3
|
+
export declare function makeExportFileName(title: string): string;
|
|
4
|
+
export interface ExportRowsToFileOptions {
|
|
5
|
+
title: string;
|
|
6
|
+
fieldSchemas: FieldSchema[];
|
|
7
|
+
rows: object[];
|
|
8
|
+
/** 单次导出上限, 默认 2 万行 */
|
|
9
|
+
maxRows?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 导出内存中的一批行: 校验条数上限、按约定生成文件名并下载。
|
|
13
|
+
* 只负责数据到文件, 不弹提示也不管理 loading;
|
|
14
|
+
* 没有数据或超出上限时抛出可直接展示的错误, 由调用方决定如何反馈。
|
|
15
|
+
*/
|
|
16
|
+
export declare function exportRowsToFile(options: ExportRowsToFileOptions): Promise<Blob>;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { FieldSchema } from "../schema";
|
|
2
2
|
import type { ImportParseResult } from "./parseSheet";
|
|
3
3
|
export { autoAdjustColumnWidths } from "./worksheet";
|
|
4
|
+
/** 统一按 .xlsx 下载; 文件名由调用方拼好 */
|
|
5
|
+
export declare function saveWorkbook(blob: Blob, fileName: string): void;
|
|
4
6
|
/** 导出: 生成 xlsx 内容, 由调用方决定何时下载 */
|
|
5
7
|
export declare function buildExportBlob(data: object[], fieldSchemas: FieldSchema[], signal?: AbortSignal): Promise<Blob>;
|
|
6
8
|
/** 导出: 把列表数据渲染成 Excel 并下载 */
|
package/dist/core/httpError.d.ts
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
|
+
/** HTTP 错误的最小形状: 不绑定具体请求库, 认不出时由调用方按普通异常处理 */
|
|
2
|
+
export interface HttpErrorShape {
|
|
3
|
+
isAxiosError?: boolean;
|
|
4
|
+
response?: {
|
|
5
|
+
status?: number;
|
|
6
|
+
data?: unknown;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** HTTP 失败的大类; 具体文案由调用方决定 */
|
|
10
|
+
export type HttpFailureKind = "network" | "unauthorized" | "forbidden" | "notFound" | "badRequest" | "server" | "unknown";
|
|
1
11
|
/** 是否像一次 HTTP 请求失败: 有响应, 或请求库明确标记过 */
|
|
2
|
-
export declare function isHttpError(error: unknown):
|
|
12
|
+
export declare function isHttpError(error: unknown): error is HttpErrorShape;
|
|
3
13
|
/** 取 HTTP 状态码; 网络中断这类没有响应的错误返回 undefined */
|
|
4
14
|
export declare function httpStatusOf(error: unknown): number | undefined;
|
|
5
15
|
/** 取响应体, 用于展示后端返回的具体错误 */
|
|
6
16
|
export declare function httpResponseData(error: unknown): unknown;
|
|
17
|
+
/**
|
|
18
|
+
* 把错误归成 HTTP 大类, 供各调用方拼自己的文案。
|
|
19
|
+
* 返回 undefined 表示不是 HTTP 失败, 调用方应按普通异常处理。
|
|
20
|
+
*/
|
|
21
|
+
export declare function classifyHttpFailure(error: unknown): HttpFailureKind | undefined;
|
package/dist/core/limits.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** 单次导入/导出的默认行数上限 */
|
|
2
2
|
export declare const MAX_IMPORT_ROWS = 20000;
|
|
3
3
|
export declare const MAX_EXPORT_ROWS = 20000;
|
|
4
4
|
/** 单个导入文件大小上限 */
|
|
5
5
|
export declare const MAX_IMPORT_FILE_SIZE: number;
|
|
6
|
+
/** 条数超出上限时返回可直接展示的提示, 未超出返回 null */
|
|
7
|
+
export declare function formatRowLimitMessage(rowCount: number, maxRows: number, direction: "导入" | "导出"): string | null;
|
|
8
|
+
/** 超出上限时抛出可直接展示的错误 */
|
|
9
|
+
export declare function assertRowLimit(rowCount: number, maxRows: number, direction: "导入" | "导出"): void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ImportRow } from "../excel/importRow";
|
|
2
|
+
import type { ImportTaskOptions } from "./types";
|
|
3
|
+
export declare function clearImportFlush(taskId: string): void;
|
|
4
|
+
/** 清空合批队列与定时器; 退出登录、清空任务时调用 */
|
|
5
|
+
export declare function clearImportFlushTimer(): void;
|
|
6
|
+
/** 新增并立即执行导入任务; 任务内部按配置的并发数逐行执行 */
|
|
7
|
+
export declare function enqueueImport(options: ImportTaskOptions): string;
|
|
8
|
+
/**
|
|
9
|
+
* 继续导入: 从头扫一遍, 只执行非已完成行, 让暂停/失败后编辑过的行重新提交.
|
|
10
|
+
*/
|
|
11
|
+
export declare function continueTask(taskId: string): void;
|
|
12
|
+
/** 暂停导入: 当前请求/当前行结束后停下, 不 abort 已发出的导入写请求 */
|
|
13
|
+
export declare function pauseTask(taskId: string): void;
|
|
14
|
+
/** 暂停时保存待导入行的编辑结果, 继续任务时使用新数据 */
|
|
15
|
+
export declare function updateImportRowData(taskId: string, rowIndex: number, row: ImportRow): void;
|
|
16
|
+
/** 校验失败行仍留在任务里, 只标记跳过并保存校验错误; 修复后取消标记即可, 不移动数组 */
|
|
17
|
+
export declare function setImportRowValidation(taskId: string, rowIndex: number, validationErrors: Record<string, string>): void;
|
|
18
|
+
/** 暂停时删除尚未导入的行: 从后续调度中跳过, 已完成/失败/进行中的行不允许删除 */
|
|
19
|
+
export declare function deleteImportRows(taskId: string, rowIndexes: number[]): void;
|
|
20
|
+
/** 编辑失败行某个字段后, 清掉该字段上一次的后端错误 */
|
|
21
|
+
export declare function clearImportRowFieldError(taskId: string, rowIndex: number, field: string): void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type { ExportTaskOptions, ImportRowStatus, ImportTaskOptions, TaskItem, TaskStatus } from "./types";
|
|
2
|
+
export { ImportPauseError } from "./types";
|
|
3
|
+
export { getTasksSnapshot, subscribeTasks } from "./store";
|
|
4
|
+
export { downloadTask, enqueueExport } from "./exportRunner";
|
|
5
|
+
export { clearImportRowFieldError, continueTask, deleteImportRows, enqueueImport, pauseTask, setImportRowValidation, updateImportRowData, } from "./importRunner";
|
|
6
|
+
export { formatTaskPercent, formatTaskRemainingSeconds, getTaskDisplayPercent, getTaskRemainingSeconds, getTaskSuccessCount, } from "./stats";
|
|
7
|
+
/** 取消: 停止任务但保留任务和预览, 由用户关闭/清除 */
|
|
8
|
+
export declare function cancelTask(taskId: string): void;
|
|
9
|
+
/** 移除任务; 执行中的任务先中断, 完成任务的 Blob 也会随引用释放 */
|
|
10
|
+
export declare function dismissTask(taskId: string): void;
|
|
11
|
+
/** 退出登录/切换账号时清空当前用户的任务, 同时中断所有在途请求 */
|
|
12
|
+
export declare function resetTasks(): void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { TaskItem, TaskSample } from "./types";
|
|
2
|
+
/** 采样只保留最近 15 秒(最多 20 个点): 用近期速度估算剩余时间, 不被任务开始阶段的慢速拖住 */
|
|
3
|
+
export declare function appendSample(previousSamples: TaskSample[], processed: number): TaskSample[];
|
|
4
|
+
/**
|
|
5
|
+
* 剩余时间估算: 采样只覆盖最近 15 秒, 取首尾两点算近期速度, 再按剩余行数外推.
|
|
6
|
+
* 任务刚起步、采样不足或速度算不出来时返回 null, 调用方显示"计算中".
|
|
7
|
+
*/
|
|
8
|
+
export declare function getTaskRemainingSeconds(task: TaskItem): number | null;
|
|
9
|
+
/** 导入任务的完成数只看成功导入的行; 失败/跳过/校验失败都不算完成 */
|
|
10
|
+
export declare function getTaskSuccessCount(task: TaskItem): number;
|
|
11
|
+
/** 显示用百分比: 未完成时直接截断, 不四舍五入成 100% */
|
|
12
|
+
export declare function getTaskDisplayPercent(task: TaskItem): number;
|
|
13
|
+
/** 百分比展示固定一位小数; 传入值已经截断, 这里不会再发生进位 */
|
|
14
|
+
export declare function formatTaskPercent(percent: number): string;
|
|
15
|
+
/** 剩余时间转中文文案; null 表示采样点还不够, 算不出速度 */
|
|
16
|
+
export declare function formatTaskRemainingSeconds(remainingSeconds: number | null): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ExportRuntime, ImportRuntime, TaskItem } from "./types";
|
|
2
|
+
export declare const exportRuntimes: Map<string, ExportRuntime>;
|
|
3
|
+
export declare const importRuntimes: Map<string, ImportRuntime>;
|
|
4
|
+
export declare const controllers: Map<string, AbortController>;
|
|
5
|
+
/** 供框架层订阅任务变化; store 本身不依赖 React */
|
|
6
|
+
export declare function subscribeTasks(listener: () => void): () => void;
|
|
7
|
+
/** 供框架层读取当前任务快照; 没有变化时返回同一引用 */
|
|
8
|
+
export declare function getTasksSnapshot(): TaskItem[];
|
|
9
|
+
export declare function makeTaskId(): string;
|
|
10
|
+
export declare function findTask(taskId: string): TaskItem | undefined;
|
|
11
|
+
export declare function updateTask(taskId: string, patch: Partial<TaskItem>): void;
|
|
12
|
+
/** 新增任务并通知订阅者 */
|
|
13
|
+
export declare function addTask(task: TaskItem): void;
|
|
14
|
+
/** 移除任务并通知订阅者 */
|
|
15
|
+
export declare function removeTask(taskId: string): void;
|
|
16
|
+
/** 清空所有任务与在途请求 */
|
|
17
|
+
export declare function resetStore(): void;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { ImportRow } from "../excel/importRow";
|
|
2
|
+
import type { FieldSchema } from "../schema";
|
|
3
|
+
import type { ImportRowFieldError, ImportRowResult, PagedData } from "../types";
|
|
4
|
+
export type TaskStatus = "running" | "paused" | "completed" | "failed" | "canceled";
|
|
5
|
+
type TaskType = "export" | "import";
|
|
6
|
+
export interface TaskSample {
|
|
7
|
+
time: number;
|
|
8
|
+
processed: number;
|
|
9
|
+
}
|
|
10
|
+
export type ImportRowStatus = "queued" | "running" | "completed" | "failed" | "canceled" | "deleted";
|
|
11
|
+
export interface ImportRowState {
|
|
12
|
+
status: ImportRowStatus;
|
|
13
|
+
message?: string;
|
|
14
|
+
fieldErrors?: ImportRowFieldError[];
|
|
15
|
+
validationErrors?: Record<string, string>;
|
|
16
|
+
}
|
|
17
|
+
interface ImportFailure {
|
|
18
|
+
index: number;
|
|
19
|
+
rowNumber: number;
|
|
20
|
+
message: string;
|
|
21
|
+
fieldErrors?: ImportRowFieldError[];
|
|
22
|
+
}
|
|
23
|
+
export interface TaskItem {
|
|
24
|
+
id: string;
|
|
25
|
+
type: TaskType;
|
|
26
|
+
title: string;
|
|
27
|
+
status: TaskStatus;
|
|
28
|
+
pausing?: boolean;
|
|
29
|
+
total: number;
|
|
30
|
+
processed: number;
|
|
31
|
+
errorMessage?: string;
|
|
32
|
+
fileName?: string;
|
|
33
|
+
blob?: Blob;
|
|
34
|
+
failures?: ImportFailure[];
|
|
35
|
+
rowStates?: ImportRowState[];
|
|
36
|
+
/** 导入成功行数; 导出任务不使用 */
|
|
37
|
+
successCount?: number;
|
|
38
|
+
/** 导入失败行数; 导出任务不使用 */
|
|
39
|
+
failureCount?: number;
|
|
40
|
+
/** 导入中因前端校验待修复的行数 */
|
|
41
|
+
validationFailedCount?: number;
|
|
42
|
+
/** 导出任务正在生成文件/触发下载 */
|
|
43
|
+
finalizing?: boolean;
|
|
44
|
+
/** 导入任务正在取消, 在途行结束前保持该状态 */
|
|
45
|
+
canceling?: boolean;
|
|
46
|
+
samples: TaskSample[];
|
|
47
|
+
}
|
|
48
|
+
export interface TaskState {
|
|
49
|
+
tasks: TaskItem[];
|
|
50
|
+
}
|
|
51
|
+
export interface ExportTaskOptions {
|
|
52
|
+
title: string;
|
|
53
|
+
fileName: string;
|
|
54
|
+
fieldSchemas: FieldSchema[];
|
|
55
|
+
exportRows: (params: Record<string, unknown>, signal: AbortSignal) => Promise<PagedData<object>>;
|
|
56
|
+
/** 单次导出上限, 默认 2 万行; 每页返回后都会校验 */
|
|
57
|
+
maxRows?: number;
|
|
58
|
+
/** 任务结束(成功/失败/取消)时回调一次, 用于埋点或清理 */
|
|
59
|
+
onFinished?: () => void;
|
|
60
|
+
}
|
|
61
|
+
/** 导入行遇到权限/系统错误时立即暂停任务, 不按普通行级失败继续执行 */
|
|
62
|
+
export declare class ImportPauseError extends Error {
|
|
63
|
+
constructor(message: string);
|
|
64
|
+
}
|
|
65
|
+
export interface ImportTaskOptions {
|
|
66
|
+
title: string;
|
|
67
|
+
/** 唯一数据源: 行值、校验错误、行号都在这里, 提交时再派生接口 payload */
|
|
68
|
+
rows: ImportRow[];
|
|
69
|
+
fieldSchemas: FieldSchema[];
|
|
70
|
+
/** 文件里实际存在的列; 更新时未包含的列表示不修改 */
|
|
71
|
+
includedKeys: string[];
|
|
72
|
+
importRows: (rows: ImportRow[], signal: AbortSignal) => Promise<ImportRowResult[]>;
|
|
73
|
+
/** 单次导入上限, 默认 2 万行 */
|
|
74
|
+
maxRows?: number;
|
|
75
|
+
/** 每批行数; 批越小请求返回越快, 默认 20 */
|
|
76
|
+
batchSize?: number;
|
|
77
|
+
/** 同时执行的批次数量, 默认 3 */
|
|
78
|
+
concurrency?: number;
|
|
79
|
+
onFinished?: () => void;
|
|
80
|
+
}
|
|
81
|
+
export interface ExportRuntime {
|
|
82
|
+
options: ExportTaskOptions;
|
|
83
|
+
cursorId?: number;
|
|
84
|
+
rows: object[];
|
|
85
|
+
total: number;
|
|
86
|
+
cancelRequested: boolean;
|
|
87
|
+
controller: AbortController | null;
|
|
88
|
+
}
|
|
89
|
+
export interface ImportRuntime {
|
|
90
|
+
options: ImportTaskOptions;
|
|
91
|
+
nextIndex: number;
|
|
92
|
+
processed: number;
|
|
93
|
+
successCount: number;
|
|
94
|
+
failures: ImportFailure[];
|
|
95
|
+
rowStates: ImportRowState[];
|
|
96
|
+
validationFailedCount: number;
|
|
97
|
+
pauseRequested: boolean;
|
|
98
|
+
cancelRequested: boolean;
|
|
99
|
+
deletedIndexes: Set<number>;
|
|
100
|
+
skippedIndexes: Set<number>;
|
|
101
|
+
/** 本轮执行累计的失败行数, 超过阈值触发暂停 */
|
|
102
|
+
runFailureCount: number;
|
|
103
|
+
controller: AbortController | null;
|
|
104
|
+
}
|
|
105
|
+
export {};
|