@veltra/sheet 4.1.3 → 4.1.5
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/dist/components/sheet/float-image-menu.js +1 -2
- package/dist/components/sheet/formula-bar.js +1 -2
- package/dist/components/sheet/formula-suggest-list.js +1 -2
- package/dist/components/sheet/import-file.js +3 -5
- package/dist/components/sheet/insert-count-menu-item.js +1 -2
- package/dist/components/sheet/insert-image.js +1 -2
- package/dist/components/sheet/popup-helpers.js +1 -2
- package/dist/components/sheet/popups/border-popup.js +1 -2
- package/dist/components/sheet/popups/border-preset-glyph.js +1 -2
- package/dist/components/sheet/popups/export-popup.js +1 -2
- package/dist/components/sheet/popups/fill-color-popup.js +1 -2
- package/dist/components/sheet/popups/find-popup.js +1 -2
- package/dist/components/sheet/popups/font-color-popup.js +1 -2
- package/dist/components/sheet/popups/font-size-popup.js +1 -2
- package/dist/components/sheet/popups/functions-popup.js +1 -2
- package/dist/components/sheet/popups/hyperlink-popup.js +1 -2
- package/dist/components/sheet/popups/import.worker.d.ts +1 -3
- package/dist/components/sheet/popups/import.worker.js +23462 -3
- package/dist/components/sheet/popups/insert-image-popup.js +1 -2
- package/dist/components/sheet/sheet-context-menu.js +0 -1
- package/dist/components/sheet/sheet-tabs.js +1 -2
- package/dist/components/sheet/sheet-toolbar.js +1 -2
- package/dist/components/sheet/sheet.js +1 -2
- package/dist/components/sheet/use-cell-link-tooltip.js +1 -2
- package/dist/components/sheet/use-find-replace.js +1 -2
- package/dist/components/sheet/use-formula-ref-select.js +1 -2
- package/dist/components/sheet/use-formula-suggest.js +0 -1
- package/dist/components/sheet/use-sheet-grid.js +1 -2
- package/dist/components/sheet/use-sheet-state.js +1 -2
- package/dist/components/sheet/use-sheet-tabs-bar.js +1 -2
- package/dist/components/sheet/use-tool-groups.js +1 -2
- package/dist/components/sheet/use-tool-popup.js +1 -2
- package/dist/components/sheet/use-toolbar-scroll.js +1 -2
- package/dist/tools/apply-style.js +1 -2
- package/dist/tools/builtin.js +1 -2
- package/dist/tools/context.d.ts +1 -3
- package/dist/tools/context.js +0 -1
- package/dist/tools/download.js +9 -6
- package/dist/tools/export.worker.d.ts +1 -3
- package/dist/tools/export.worker.js +23459 -3
- package/dist/tools/registry.d.ts +1 -3
- package/dist/tools/registry.js +1 -2
- package/dist/types/sheet.d.ts +1 -3
- package/package.json +5 -5
- package/src/components/sheet/__test__/formula-bar.test.ts +4 -3
- package/src/components/sheet/__test__/import-file.test.ts +57 -4
- package/src/components/sheet/__test__/insert-image.test.ts +3 -3
- package/src/components/sheet/__test__/sheet-context-menu.test.ts +2 -2
- package/src/components/sheet/import-file.ts +3 -3
- package/src/tools/__test__/builtin.test.ts +2 -2
- package/src/tools/__test__/context.test.ts +2 -2
- package/src/tools/__test__/download.test.ts +108 -0
- package/src/tools/download.ts +9 -5
package/dist/tools/registry.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { SheetContext } from "./context.js";
|
|
2
|
-
//#region src/tools/registry.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* 工具注册表:USheet 工具栏的扩展点。
|
|
5
4
|
*
|
|
@@ -68,5 +67,4 @@ export declare const defaultToolRegistry: ToolRegistry;
|
|
|
68
67
|
/** 注册工具到默认注册表 */
|
|
69
68
|
export declare function registerTool(tool: SheetTool): void;
|
|
70
69
|
/** 从默认注册表注销工具;id 不存在时返回 false */
|
|
71
|
-
export declare function unregisterTool(id: string): boolean;
|
|
72
|
-
//#endregion
|
|
70
|
+
export declare function unregisterTool(id: string): boolean;
|
package/dist/tools/registry.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BUILTIN_TOOLS } from "./builtin.js";
|
|
2
2
|
import { TypedEventEmitter } from "infinitable/sheet";
|
|
3
|
-
|
|
3
|
+
// src/tools/registry.ts
|
|
4
4
|
/** 缺省分组名 */
|
|
5
5
|
const DEFAULT_TOOL_GROUP = "default";
|
|
6
6
|
var ToolRegistry = class {
|
|
@@ -76,5 +76,4 @@ function registerTool(tool) {
|
|
|
76
76
|
function unregisterTool(id) {
|
|
77
77
|
return defaultToolRegistry.unregister(id);
|
|
78
78
|
}
|
|
79
|
-
//#endregion
|
|
80
79
|
export { DEFAULT_TOOL_GROUP, ToolRegistry, defaultToolRegistry, registerTool, unregisterTool };
|
package/dist/types/sheet.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { CellAddress, ResolveCellRenderer, ResolveCellStyleHook, ResolveDisplayV
|
|
|
3
3
|
import { ComputedRef } from "vue";
|
|
4
4
|
import { DeconstructValue } from "@veltra/utils";
|
|
5
5
|
import { CellLinkClickEvent, CellLinkHoverEvent, CellLinkLeaveEvent, FloatContextMenuEvent, ImageLinkClickEvent } from "infinitable";
|
|
6
|
-
//#region src/types/sheet.d.ts
|
|
7
6
|
/** 电子表格组件属性 */
|
|
8
7
|
export interface SheetProps {
|
|
9
8
|
/** 工作簿实例(多 sheet / 跨表公式的载体);缺省内部自建(单 sheet) */
|
|
@@ -140,5 +139,4 @@ export interface _SheetExposed {
|
|
|
140
139
|
getGrid: () => SheetGrid | undefined;
|
|
141
140
|
}
|
|
142
141
|
/** 电子表格组件暴露的属性和方法 */
|
|
143
|
-
export type SheetExposed = DeconstructValue<_SheetExposed>;
|
|
144
|
-
//#endregion
|
|
142
|
+
export type SheetExposed = DeconstructValue<_SheetExposed>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veltra/sheet",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.5",
|
|
4
4
|
"homepage": "https://github.com/cabinet-fe/ultra-ui#readme",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/cabinet-fe/ultra-ui/issues"
|
|
@@ -34,11 +34,11 @@
|
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"@cat-kit/core": ">=1.2.1",
|
|
36
36
|
"@cat-kit/fe": ">=1.2.1",
|
|
37
|
-
"@veltra/desktop": "^
|
|
37
|
+
"@veltra/desktop": "^2.0.0",
|
|
38
38
|
"@veltra/icons": "^1.5.0",
|
|
39
|
-
"@veltra/styles": "^
|
|
40
|
-
"@veltra/utils": "^
|
|
41
|
-
"infinitable": "^0.
|
|
39
|
+
"@veltra/styles": "^2.0.0",
|
|
40
|
+
"@veltra/utils": "^2.0.0",
|
|
41
|
+
"infinitable": "^0.3.4",
|
|
42
42
|
"vue": ">=3.5.44"
|
|
43
43
|
}
|
|
44
44
|
}
|
|
@@ -363,12 +363,13 @@ async function flushPopup(): Promise<void> {
|
|
|
363
363
|
describe('USheet 公式栏:fx 函数弹框', () => {
|
|
364
364
|
it('fx 渲染为可点击 button;无活动单元格时禁用且不打开弹框', async () => {
|
|
365
365
|
const sheet = new Workbook().activeSheet
|
|
366
|
+
// 清空选区(Sheet 构造默认选 A1):与 fx 输入框同一禁用条件。
|
|
367
|
+
// 0.3 起 SelectionModel 移除 clear(),restoreState 静默不发事件,
|
|
368
|
+
// 组件只认挂载时的模型选区,故在挂载前置空
|
|
369
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
366
370
|
const { el } = mountFormulaBar(sheet)
|
|
367
371
|
await nextTick()
|
|
368
372
|
|
|
369
|
-
// 清空选区(Sheet 构造默认选 A1):与 fx 输入框同一禁用条件
|
|
370
|
-
sheet.selection.clear()
|
|
371
|
-
await nextTick()
|
|
372
373
|
const button = fxButton(el)
|
|
373
374
|
expect(button.tagName).toBe('BUTTON')
|
|
374
375
|
expect(button.disabled).toBe(true)
|
|
@@ -7,6 +7,34 @@ import { ref } from 'vue'
|
|
|
7
7
|
|
|
8
8
|
import { IMPORT_FILE_ACCEPT, importFromFile, pickAndImportFile } from '../import-file'
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* worker 替身:happy-dom 自带 Worker(构造成功但 URL 不可加载,异步才触发
|
|
12
|
+
* onerror),使回退时机不确定、既有用例偶发拿到空 danger 调用——统一替换为
|
|
13
|
+
* 可编程替身。默认脚本为同步 onerror(worker 不可用 → 主线程回退),单测可
|
|
14
|
+
* 按 scenario 覆写响应(如 { ok: false })。
|
|
15
|
+
*/
|
|
16
|
+
type FakeMessageEvent = { data: unknown }
|
|
17
|
+
class FakeWorker {
|
|
18
|
+
static script: (worker: FakeWorker) => void = (worker) => {
|
|
19
|
+
queueMicrotask(() => worker.onerror?.(new Error('worker unavailable')))
|
|
20
|
+
}
|
|
21
|
+
static instances: FakeWorker[] = []
|
|
22
|
+
onmessage: ((e: FakeMessageEvent) => void) | null = null
|
|
23
|
+
onerror: ((e: unknown) => void) | null = null
|
|
24
|
+
terminated = false
|
|
25
|
+
constructor() {
|
|
26
|
+
FakeWorker.instances.push(this)
|
|
27
|
+
}
|
|
28
|
+
postMessage(): void {
|
|
29
|
+
queueMicrotask(() => FakeWorker.script(this))
|
|
30
|
+
}
|
|
31
|
+
terminate(): void {
|
|
32
|
+
this.terminated = true
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const defaultWorkerScript = FakeWorker.script
|
|
36
|
+
globalThis.Worker = FakeWorker as unknown as typeof Worker
|
|
37
|
+
|
|
10
38
|
const mocks = vi.hoisted(() => {
|
|
11
39
|
const message = vi.fn(() => ({ close: vi.fn(), id: 'loading', onClosed: Promise.resolve() }))
|
|
12
40
|
for (const type of ['success', 'warn', 'info', 'error', 'default']) {
|
|
@@ -77,7 +105,7 @@ function makeOptions(
|
|
|
77
105
|
}
|
|
78
106
|
}
|
|
79
107
|
|
|
80
|
-
/** 触发 xlsx
|
|
108
|
+
/** 触发 xlsx 导入并等解析回退链完成(worker 替身 → 主线程 importXlsx → 确认弹层) */
|
|
81
109
|
async function triggerXlsxAndAwait(
|
|
82
110
|
options: ReturnType<typeof makeOptions>,
|
|
83
111
|
name = 'test.xlsx'
|
|
@@ -88,9 +116,7 @@ async function triggerXlsxAndAwait(
|
|
|
88
116
|
text: () => Promise.resolve('')
|
|
89
117
|
} as File
|
|
90
118
|
importFromFile(file, options)
|
|
91
|
-
await
|
|
92
|
-
await Promise.resolve()
|
|
93
|
-
await Promise.resolve()
|
|
119
|
+
await vi.waitFor(() => expect(mocks.messageConfirm.danger).toHaveBeenCalledTimes(1))
|
|
94
120
|
}
|
|
95
121
|
|
|
96
122
|
/** 取 messageConfirm.danger 注册的 onClosed 回调 */
|
|
@@ -102,6 +128,8 @@ function getOnClosed(): (action: string) => void {
|
|
|
102
128
|
}
|
|
103
129
|
|
|
104
130
|
afterEach(() => {
|
|
131
|
+
FakeWorker.script = defaultWorkerScript
|
|
132
|
+
FakeWorker.instances = []
|
|
105
133
|
vi.clearAllMocks()
|
|
106
134
|
document.body.querySelectorAll('input[type="file"]').forEach((el) => el.remove())
|
|
107
135
|
})
|
|
@@ -218,3 +246,28 @@ describe('pickAndImportFile', () => {
|
|
|
218
246
|
click.mockRestore()
|
|
219
247
|
})
|
|
220
248
|
})
|
|
249
|
+
|
|
250
|
+
describe('worker { ok: false } 主线程回退', () => {
|
|
251
|
+
it('worker 内部失败:不 reject,回退主线程 importXlsx 并照常进入确认', async () => {
|
|
252
|
+
FakeWorker.script = (worker) => {
|
|
253
|
+
worker.onmessage?.({ data: { type: 'done', ok: false, error: 'bare specifier boom' } })
|
|
254
|
+
}
|
|
255
|
+
const replaced = vi.fn()
|
|
256
|
+
const options = makeOptions({ onWorkbookReplaced: replaced })
|
|
257
|
+
await triggerXlsxAndAwait(options)
|
|
258
|
+
|
|
259
|
+
// 主线程回退确实发生:importXlsx 被调用、worker 已终止,且其结果进入确认 → 替换
|
|
260
|
+
expect(mocks.importXlsx).toHaveBeenCalledTimes(1)
|
|
261
|
+
expect(FakeWorker.instances[0]!.terminated).toBe(true)
|
|
262
|
+
expect(mocks.message.error).not.toHaveBeenCalled()
|
|
263
|
+
|
|
264
|
+
const onClosed = getOnClosed()
|
|
265
|
+
await onClosed('confirm')
|
|
266
|
+
expect(mocks.replaceWorkbookWithSnapshots).toHaveBeenCalledWith(
|
|
267
|
+
expect.anything(),
|
|
268
|
+
[{ name: 'S1', snapshot: EMPTY_SNAPSHOT, defaultRowHeight: 20, defaultColWidth: 64 }],
|
|
269
|
+
0
|
|
270
|
+
)
|
|
271
|
+
expect(replaced).toHaveBeenCalledTimes(1)
|
|
272
|
+
})
|
|
273
|
+
})
|
|
@@ -81,7 +81,7 @@ describe('insertImageFromFile', () => {
|
|
|
81
81
|
it('无活动格 → message.error,不写入', async () => {
|
|
82
82
|
const sheet = new Sheet()
|
|
83
83
|
const ctx = createSheetContext(sheet)
|
|
84
|
-
sheet.selection.
|
|
84
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
85
85
|
const id = await insertImageFromFile(ctx, makeFile('a.png', 'image/png'))
|
|
86
86
|
expect(id).toBeUndefined()
|
|
87
87
|
expect(ctx.getImages()).toHaveLength(0)
|
|
@@ -135,7 +135,7 @@ describe('insertImageFromUrl', () => {
|
|
|
135
135
|
it('无活动格 → message.error,不写入', () => {
|
|
136
136
|
const sheet = new Sheet()
|
|
137
137
|
const ctx = createSheetContext(sheet)
|
|
138
|
-
sheet.selection.
|
|
138
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
139
139
|
expect(insertImageFromUrl(ctx, 'https://example.com/a.png')).toBeUndefined()
|
|
140
140
|
expect(ctx.getImages()).toHaveLength(0)
|
|
141
141
|
expect(mocks.message.error).toHaveBeenCalledWith('请先选择单元格')
|
|
@@ -181,7 +181,7 @@ describe('insertCellImageFromFile / pickAndEmbedCellImage', () => {
|
|
|
181
181
|
it('无活动格 → message.error,不写入', async () => {
|
|
182
182
|
const sheet = new Sheet()
|
|
183
183
|
const ctx = createSheetContext(sheet)
|
|
184
|
-
sheet.selection.
|
|
184
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
185
185
|
await insertCellImageFromFile(ctx, makeFile('a.png', 'image/png'))
|
|
186
186
|
expect(mocks.message.error).toHaveBeenCalledWith('请先选择单元格')
|
|
187
187
|
})
|
|
@@ -319,7 +319,7 @@ describe('build*Menus', () => {
|
|
|
319
319
|
const sheet = new Sheet()
|
|
320
320
|
const ctx = createSheetContext(sheet)
|
|
321
321
|
sheet.setCellValue({ row: 0, col: 0 }, 1)
|
|
322
|
-
sheet.selection.
|
|
322
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
323
323
|
applyNumFmtToSelection(ctx, { type: 'date' })
|
|
324
324
|
expect(ctx.getCellStyle({ row: 0, col: 0 })).toBeUndefined()
|
|
325
325
|
// undo 直接回滚到 setCellValue 之前,证明未新增 undo 单元
|
|
@@ -331,7 +331,7 @@ describe('build*Menus', () => {
|
|
|
331
331
|
it('body 插入图片 / 嵌入图片 / 设置数据格式:无活动格时禁用', () => {
|
|
332
332
|
const sheet = new Sheet()
|
|
333
333
|
const ctx = createSheetContext(sheet)
|
|
334
|
-
sheet.selection.
|
|
334
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
335
335
|
const menus = buildBodyMenus(ctx)
|
|
336
336
|
expect(menus.find((m) => m.label === '插入图片')?.disabled).toBe(true)
|
|
337
337
|
expect(menus.find((m) => m.label === '嵌入图片')?.disabled).toBe(true)
|
|
@@ -72,12 +72,12 @@ async function parseXlsxAsync(
|
|
|
72
72
|
return
|
|
73
73
|
}
|
|
74
74
|
if (settled) return
|
|
75
|
-
|
|
75
|
+
// worker 内部失败(解析异常 / 产物缺依赖)与加载失败同口径:降级主线程
|
|
76
76
|
if (!data.ok) {
|
|
77
|
-
|
|
78
|
-
reject(new Error(data.error ?? 'worker 解析失败'))
|
|
77
|
+
fallback()
|
|
79
78
|
return
|
|
80
79
|
}
|
|
80
|
+
settled = true
|
|
81
81
|
worker!.terminate()
|
|
82
82
|
resolve({ sheets: data.sheets ?? [], activeIndex: data.activeIndex ?? 0 })
|
|
83
83
|
}
|
|
@@ -139,7 +139,7 @@ describe('内置工具(dogfood 扩展机制)', () => {
|
|
|
139
139
|
expect(border.popup).toBe('border')
|
|
140
140
|
// 默认 A1 有选区 → 可用
|
|
141
141
|
expect(fill.disabled?.(ctx)).toBe(false)
|
|
142
|
-
sheet.selection.
|
|
142
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
143
143
|
expect(fill.disabled?.(ctx)).toBe(true)
|
|
144
144
|
expect(border.disabled?.(ctx)).toBe(true)
|
|
145
145
|
})
|
|
@@ -229,7 +229,7 @@ describe('内置工具(dogfood 扩展机制)', () => {
|
|
|
229
229
|
expect(tool.popup).toBe('insert-image')
|
|
230
230
|
expect(tool.group).toBe('insert')
|
|
231
231
|
expect(tool.disabled?.(ctx)).toBe(false)
|
|
232
|
-
sheet.selection.
|
|
232
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
233
233
|
expect(tool.disabled?.(ctx)).toBe(true)
|
|
234
234
|
})
|
|
235
235
|
|
|
@@ -218,7 +218,7 @@ describe('SheetContext', () => {
|
|
|
218
218
|
expect(ctx.getColWidth(4)).toBe(MIN_ROW_COL_SIZE)
|
|
219
219
|
|
|
220
220
|
// 空选区:无写入目标,不产生任何尺寸
|
|
221
|
-
sheet.selection.
|
|
221
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
222
222
|
ctx.setRowHeightBySelection(60)
|
|
223
223
|
ctx.setColWidthBySelection(200)
|
|
224
224
|
expect(ctx.getRowHeight(0)).toBeUndefined()
|
|
@@ -238,7 +238,7 @@ describe('SheetContext', () => {
|
|
|
238
238
|
|
|
239
239
|
// 空选区:模型无写入,同步钩子不触发
|
|
240
240
|
syncAxisSizes.mockClear()
|
|
241
|
-
sheet.selection.
|
|
241
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
242
242
|
ctx.setRowHeightBySelection(60)
|
|
243
243
|
ctx.setColWidthBySelection(60)
|
|
244
244
|
expect(syncAxisSizes).not.toHaveBeenCalled()
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vite-plus/test'
|
|
2
|
+
|
|
3
|
+
import type { SheetContext } from '../context'
|
|
4
|
+
import { exportWorkbookFile } from '../download'
|
|
5
|
+
|
|
6
|
+
const mocks = vi.hoisted(() => ({
|
|
7
|
+
saveBlob: vi.fn(),
|
|
8
|
+
exportWorkbookXlsx: vi.fn(() => new Uint8Array([1, 2, 3])),
|
|
9
|
+
exportSheetCsv: vi.fn(() => 'csv')
|
|
10
|
+
}))
|
|
11
|
+
|
|
12
|
+
vi.mock('@cat-kit/fe', () => ({ saveBlob: mocks.saveBlob }))
|
|
13
|
+
|
|
14
|
+
vi.mock('infinitable/sheet', async (importActual) => {
|
|
15
|
+
const actual = await importActual<typeof import('infinitable/sheet')>()
|
|
16
|
+
return {
|
|
17
|
+
...actual,
|
|
18
|
+
exportWorkbookXlsx: mocks.exportWorkbookXlsx,
|
|
19
|
+
exportSheetCsv: mocks.exportSheetCsv
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
/** 最小 workbook 形状:download.ts 只读 name / snapshot / activeSheetIndex / activeSheet.name */
|
|
24
|
+
function makeCtx(): SheetContext {
|
|
25
|
+
const workbook = {
|
|
26
|
+
getSheets: () => [{ name: 'S1', snapshot: () => ({ cells: [] }) }],
|
|
27
|
+
activeSheetIndex: 0,
|
|
28
|
+
activeSheet: { name: 'S1' }
|
|
29
|
+
}
|
|
30
|
+
return { workbook } as unknown as SheetContext
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** postMessage 后按预设响应回消息的 worker 替身 */
|
|
34
|
+
class FakeWorker {
|
|
35
|
+
static respond: ((worker: FakeWorker) => void) | undefined
|
|
36
|
+
onmessage: ((e: { data: unknown }) => void) | null = null
|
|
37
|
+
onerror: ((e: unknown) => void) | null = null
|
|
38
|
+
terminated = false
|
|
39
|
+
postMessage(): void {
|
|
40
|
+
const respond = FakeWorker.respond
|
|
41
|
+
if (respond) queueMicrotask(() => respond(this))
|
|
42
|
+
}
|
|
43
|
+
terminate(): void {
|
|
44
|
+
this.terminated = true
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function useFakeWorker(): () => void {
|
|
49
|
+
const original = globalThis.Worker
|
|
50
|
+
globalThis.Worker = FakeWorker as unknown as typeof Worker
|
|
51
|
+
return () => {
|
|
52
|
+
globalThis.Worker = original
|
|
53
|
+
FakeWorker.respond = undefined
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
describe('exportWorkbookFile worker 降级', () => {
|
|
58
|
+
it('worker { ok: false }:不 reject,回退主线程 exportWorkbookXlsx 并照常下载', async () => {
|
|
59
|
+
const restore = useFakeWorker()
|
|
60
|
+
FakeWorker.respond = (worker) => {
|
|
61
|
+
worker.onmessage?.({ data: { ok: false, error: 'bare specifier boom' } })
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
await expect(exportWorkbookFile(makeCtx())).resolves.toBeUndefined()
|
|
65
|
+
|
|
66
|
+
expect(mocks.exportWorkbookXlsx).toHaveBeenCalledTimes(1)
|
|
67
|
+
expect(mocks.saveBlob).toHaveBeenCalledTimes(1)
|
|
68
|
+
const [blob, filename] = mocks.saveBlob.mock.calls[0]!
|
|
69
|
+
expect(blob).toBeInstanceOf(Blob)
|
|
70
|
+
expect(blob.type).toBe('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
|
71
|
+
expect(filename).toBe('S1.xlsx')
|
|
72
|
+
} finally {
|
|
73
|
+
restore()
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('worker { ok: true }:使用 worker 产物,不回退主线程', async () => {
|
|
78
|
+
const restore = useFakeWorker()
|
|
79
|
+
FakeWorker.respond = (worker) => {
|
|
80
|
+
worker.onmessage?.({ data: { ok: true, buffer: new Uint8Array([9, 9, 9]) } })
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
await exportWorkbookFile(makeCtx())
|
|
84
|
+
expect(mocks.exportWorkbookXlsx).not.toHaveBeenCalled()
|
|
85
|
+
const [blob] = mocks.saveBlob.mock.calls[0]!
|
|
86
|
+
const bytes = new Uint8Array(await blob.arrayBuffer())
|
|
87
|
+
expect(Array.from(bytes)).toEqual([9, 9, 9])
|
|
88
|
+
} finally {
|
|
89
|
+
restore()
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('worker 失败且主线程也抛错:错误向调用方传播(导出面板负责提示)', async () => {
|
|
94
|
+
const restore = useFakeWorker()
|
|
95
|
+
FakeWorker.respond = (worker) => {
|
|
96
|
+
worker.onmessage?.({ data: { ok: false, error: 'boom' } })
|
|
97
|
+
}
|
|
98
|
+
mocks.exportWorkbookXlsx.mockImplementationOnce(() => {
|
|
99
|
+
throw new Error('非法表名')
|
|
100
|
+
})
|
|
101
|
+
try {
|
|
102
|
+
await expect(exportWorkbookFile(makeCtx())).rejects.toThrow('非法表名')
|
|
103
|
+
expect(mocks.saveBlob).not.toHaveBeenCalled()
|
|
104
|
+
} finally {
|
|
105
|
+
restore()
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
})
|
package/src/tools/download.ts
CHANGED
|
@@ -29,22 +29,26 @@ async function exportWorkbookXlsxAsync(workbook: Workbook): Promise<Uint8Array>
|
|
|
29
29
|
}
|
|
30
30
|
return await new Promise<Uint8Array>((resolve, reject) => {
|
|
31
31
|
let settled = false
|
|
32
|
-
// worker
|
|
32
|
+
// worker 加载/启动失败(如打包产物缺文件)或内部失败 → 降级主线程导出
|
|
33
33
|
const fallback = (): void => {
|
|
34
34
|
if (settled) return
|
|
35
35
|
settled = true
|
|
36
36
|
worker!.terminate()
|
|
37
|
-
|
|
37
|
+
try {
|
|
38
|
+
resolve(exportWorkbookXlsx(workbook))
|
|
39
|
+
} catch (err) {
|
|
40
|
+
reject(err)
|
|
41
|
+
}
|
|
38
42
|
}
|
|
39
43
|
worker!.onmessage = (e: MessageEvent<ExportWorkerResponse>) => {
|
|
40
44
|
if (settled) return
|
|
41
|
-
settled = true
|
|
42
|
-
worker!.terminate()
|
|
43
45
|
const data = e.data
|
|
44
46
|
if (!data.ok || !data.buffer) {
|
|
45
|
-
|
|
47
|
+
fallback()
|
|
46
48
|
return
|
|
47
49
|
}
|
|
50
|
+
settled = true
|
|
51
|
+
worker!.terminate()
|
|
48
52
|
resolve(data.buffer)
|
|
49
53
|
}
|
|
50
54
|
worker!.onerror = () => fallback()
|