@veltra/sheet 4.1.2 → 4.1.4
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 +2 -3
- 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/index.js +1 -2
- package/dist/tools/apply-style.js +1 -2
- package/dist/tools/builtin.js +308 -307
- 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 +4 -4
- package/dist/tools/registry.js +8 -4
- package/dist/types/sheet.d.ts +1 -3
- package/package.json +4 -6
- package/src/components/sheet/__test__/canvas-mock.ts +3 -1
- package/src/components/sheet/__test__/find-shortcut-anchor.test.ts +2 -0
- package/src/components/sheet/__test__/formula-bar.test.ts +6 -3
- package/src/components/sheet/__test__/formula-capability.test.ts +2 -0
- package/src/components/sheet/__test__/functions-popup.test.ts +2 -0
- package/src/components/sheet/__test__/hyperlink-popup.test.ts +2 -0
- package/src/components/sheet/__test__/import-file.test.ts +59 -4
- package/src/components/sheet/__test__/insert-count-menu-item.test.ts +2 -0
- package/src/components/sheet/__test__/insert-image-popup.test.ts +2 -0
- package/src/components/sheet/__test__/insert-image.test.ts +5 -3
- package/src/components/sheet/__test__/sheet-component.test.ts +4 -1
- package/src/components/sheet/__test__/sheet-context-menu.test.ts +2 -2
- package/src/components/sheet/__test__/sheet-grow-on-scroll.test.ts +2 -0
- package/src/components/sheet/__test__/sheet-scrollbar.test.ts +2 -0
- package/src/components/sheet/__test__/sheet-theme.test.ts +2 -0
- package/src/components/sheet/import-file.ts +3 -3
- package/src/index.ts +1 -3
- package/src/tools/__test__/builtin.test.ts +4 -4
- package/src/tools/__test__/context.test.ts +2 -2
- package/src/tools/__test__/download.test.ts +108 -0
- package/src/tools/builtin.ts +345 -344
- package/src/tools/download.ts +9 -5
- package/src/tools/registry.ts +8 -2
- package/src/tools/index.ts +0 -3
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
|
*
|
|
@@ -46,6 +45,8 @@ export declare class ToolRegistry {
|
|
|
46
45
|
private entries;
|
|
47
46
|
private seq;
|
|
48
47
|
private emitter;
|
|
48
|
+
/** 装入初始工具(仍经 register 校验);缺省空表 */
|
|
49
|
+
constructor(tools?: readonly SheetTool[]);
|
|
49
50
|
/** 注册工具;同 id 重复注册 = 替换定义并保留原注册位置 */
|
|
50
51
|
register(tool: SheetTool): void;
|
|
51
52
|
/** 注销工具;id 不存在时返回 false(不触发 change) */
|
|
@@ -59,12 +60,11 @@ export declare class ToolRegistry {
|
|
|
59
60
|
onChange(handler: () => void): () => void;
|
|
60
61
|
}
|
|
61
62
|
/**
|
|
62
|
-
* 全局默认注册表(USheet
|
|
63
|
+
* 全局默认注册表(USheet 默认渲染这里的工具;内置工具在构造时装入)。
|
|
63
64
|
* 工具是应用级扩展:注册一次,所有 USheet 实例共享。
|
|
64
65
|
*/
|
|
65
66
|
export declare const defaultToolRegistry: ToolRegistry;
|
|
66
67
|
/** 注册工具到默认注册表 */
|
|
67
68
|
export declare function registerTool(tool: SheetTool): void;
|
|
68
69
|
/** 从默认注册表注销工具;id 不存在时返回 false */
|
|
69
|
-
export declare function unregisterTool(id: string): boolean;
|
|
70
|
-
//#endregion
|
|
70
|
+
export declare function unregisterTool(id: string): boolean;
|
package/dist/tools/registry.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
import { BUILTIN_TOOLS } from "./builtin.js";
|
|
1
2
|
import { TypedEventEmitter } from "infinitable/sheet";
|
|
2
|
-
|
|
3
|
+
// src/tools/registry.ts
|
|
3
4
|
/** 缺省分组名 */
|
|
4
5
|
const DEFAULT_TOOL_GROUP = "default";
|
|
5
6
|
var ToolRegistry = class {
|
|
6
7
|
entries = /* @__PURE__ */ new Map();
|
|
7
8
|
seq = 0;
|
|
8
9
|
emitter = new TypedEventEmitter();
|
|
10
|
+
/** 装入初始工具(仍经 register 校验);缺省空表 */
|
|
11
|
+
constructor(tools = []) {
|
|
12
|
+
for (const tool of tools) this.register(tool);
|
|
13
|
+
}
|
|
9
14
|
/** 注册工具;同 id 重复注册 = 替换定义并保留原注册位置 */
|
|
10
15
|
register(tool) {
|
|
11
16
|
if (!tool.id) throw new Error("工具注册失败:id 不能为空");
|
|
@@ -59,10 +64,10 @@ var ToolRegistry = class {
|
|
|
59
64
|
}
|
|
60
65
|
};
|
|
61
66
|
/**
|
|
62
|
-
* 全局默认注册表(USheet
|
|
67
|
+
* 全局默认注册表(USheet 默认渲染这里的工具;内置工具在构造时装入)。
|
|
63
68
|
* 工具是应用级扩展:注册一次,所有 USheet 实例共享。
|
|
64
69
|
*/
|
|
65
|
-
const defaultToolRegistry = new ToolRegistry();
|
|
70
|
+
const defaultToolRegistry = new ToolRegistry(BUILTIN_TOOLS);
|
|
66
71
|
/** 注册工具到默认注册表 */
|
|
67
72
|
function registerTool(tool) {
|
|
68
73
|
defaultToolRegistry.register(tool);
|
|
@@ -71,5 +76,4 @@ function registerTool(tool) {
|
|
|
71
76
|
function unregisterTool(id) {
|
|
72
77
|
return defaultToolRegistry.unregister(id);
|
|
73
78
|
}
|
|
74
|
-
//#endregion
|
|
75
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.4",
|
|
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"
|
|
@@ -18,10 +18,8 @@
|
|
|
18
18
|
"sideEffects": [
|
|
19
19
|
"src/**/style.ts",
|
|
20
20
|
"src/**/*.scss",
|
|
21
|
-
"src/tools/builtin.ts",
|
|
22
21
|
"dist/**/style.js",
|
|
23
|
-
"dist/**/*.css"
|
|
24
|
-
"dist/tools/builtin.js"
|
|
22
|
+
"dist/**/*.css"
|
|
25
23
|
],
|
|
26
24
|
"exports": {
|
|
27
25
|
".": {
|
|
@@ -40,7 +38,7 @@
|
|
|
40
38
|
"@veltra/icons": "^1.5.0",
|
|
41
39
|
"@veltra/styles": "^1.7.14",
|
|
42
40
|
"@veltra/utils": "^1.7.14",
|
|
43
|
-
"infinitable": "^0.
|
|
44
|
-
"vue": ">=3.5.
|
|
41
|
+
"infinitable": "^0.3.4",
|
|
42
|
+
"vue": ">=3.5.44"
|
|
45
43
|
}
|
|
46
44
|
}
|
|
@@ -33,8 +33,10 @@ function createContext2dMock(canvas: HTMLCanvasElement): Context2dMock {
|
|
|
33
33
|
})
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
/** 补丁 HTMLCanvasElement,使 getContext('2d') 返回可用 mock */
|
|
36
|
+
/** 补丁 HTMLCanvasElement,使 getContext('2d') 返回可用 mock;node 环境无该全局,直接跳过 */
|
|
37
37
|
export function setupCanvasMock(): void {
|
|
38
|
+
if (typeof HTMLCanvasElement === 'undefined') return
|
|
39
|
+
|
|
38
40
|
const getContext = function (this: HTMLCanvasElement, contextId: string): unknown {
|
|
39
41
|
if (contextId !== '2d') return null
|
|
40
42
|
return createContext2dMock(this)
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
1
3
|
import { parseRange } from 'infinitable/sheet'
|
|
2
4
|
import { Workbook } from 'infinitable/sheet'
|
|
3
5
|
import type { SheetGrid } from 'infinitable/sheet'
|
|
@@ -361,12 +363,13 @@ async function flushPopup(): Promise<void> {
|
|
|
361
363
|
describe('USheet 公式栏:fx 函数弹框', () => {
|
|
362
364
|
it('fx 渲染为可点击 button;无活动单元格时禁用且不打开弹框', async () => {
|
|
363
365
|
const sheet = new Workbook().activeSheet
|
|
366
|
+
// 清空选区(Sheet 构造默认选 A1):与 fx 输入框同一禁用条件。
|
|
367
|
+
// 0.3 起 SelectionModel 移除 clear(),restoreState 静默不发事件,
|
|
368
|
+
// 组件只认挂载时的模型选区,故在挂载前置空
|
|
369
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
364
370
|
const { el } = mountFormulaBar(sheet)
|
|
365
371
|
await nextTick()
|
|
366
372
|
|
|
367
|
-
// 清空选区(Sheet 构造默认选 A1):与 fx 输入框同一禁用条件
|
|
368
|
-
sheet.selection.clear()
|
|
369
|
-
await nextTick()
|
|
370
373
|
const button = fxButton(el)
|
|
371
374
|
expect(button.tagName).toBe('BUTTON')
|
|
372
375
|
expect(button.disabled).toBe(true)
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
1
3
|
import type { Sheet } from 'infinitable/sheet'
|
|
2
4
|
import { Workbook } from 'infinitable/sheet'
|
|
3
5
|
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
@@ -5,6 +7,34 @@ import { ref } from 'vue'
|
|
|
5
7
|
|
|
6
8
|
import { IMPORT_FILE_ACCEPT, importFromFile, pickAndImportFile } from '../import-file'
|
|
7
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
|
+
|
|
8
38
|
const mocks = vi.hoisted(() => {
|
|
9
39
|
const message = vi.fn(() => ({ close: vi.fn(), id: 'loading', onClosed: Promise.resolve() }))
|
|
10
40
|
for (const type of ['success', 'warn', 'info', 'error', 'default']) {
|
|
@@ -75,7 +105,7 @@ function makeOptions(
|
|
|
75
105
|
}
|
|
76
106
|
}
|
|
77
107
|
|
|
78
|
-
/** 触发 xlsx
|
|
108
|
+
/** 触发 xlsx 导入并等解析回退链完成(worker 替身 → 主线程 importXlsx → 确认弹层) */
|
|
79
109
|
async function triggerXlsxAndAwait(
|
|
80
110
|
options: ReturnType<typeof makeOptions>,
|
|
81
111
|
name = 'test.xlsx'
|
|
@@ -86,9 +116,7 @@ async function triggerXlsxAndAwait(
|
|
|
86
116
|
text: () => Promise.resolve('')
|
|
87
117
|
} as File
|
|
88
118
|
importFromFile(file, options)
|
|
89
|
-
await
|
|
90
|
-
await Promise.resolve()
|
|
91
|
-
await Promise.resolve()
|
|
119
|
+
await vi.waitFor(() => expect(mocks.messageConfirm.danger).toHaveBeenCalledTimes(1))
|
|
92
120
|
}
|
|
93
121
|
|
|
94
122
|
/** 取 messageConfirm.danger 注册的 onClosed 回调 */
|
|
@@ -100,6 +128,8 @@ function getOnClosed(): (action: string) => void {
|
|
|
100
128
|
}
|
|
101
129
|
|
|
102
130
|
afterEach(() => {
|
|
131
|
+
FakeWorker.script = defaultWorkerScript
|
|
132
|
+
FakeWorker.instances = []
|
|
103
133
|
vi.clearAllMocks()
|
|
104
134
|
document.body.querySelectorAll('input[type="file"]').forEach((el) => el.remove())
|
|
105
135
|
})
|
|
@@ -216,3 +246,28 @@ describe('pickAndImportFile', () => {
|
|
|
216
246
|
click.mockRestore()
|
|
217
247
|
})
|
|
218
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
|
+
})
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
1
3
|
import { Sheet } from 'infinitable/sheet'
|
|
2
4
|
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
3
5
|
|
|
@@ -79,7 +81,7 @@ describe('insertImageFromFile', () => {
|
|
|
79
81
|
it('无活动格 → message.error,不写入', async () => {
|
|
80
82
|
const sheet = new Sheet()
|
|
81
83
|
const ctx = createSheetContext(sheet)
|
|
82
|
-
sheet.selection.
|
|
84
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
83
85
|
const id = await insertImageFromFile(ctx, makeFile('a.png', 'image/png'))
|
|
84
86
|
expect(id).toBeUndefined()
|
|
85
87
|
expect(ctx.getImages()).toHaveLength(0)
|
|
@@ -133,7 +135,7 @@ describe('insertImageFromUrl', () => {
|
|
|
133
135
|
it('无活动格 → message.error,不写入', () => {
|
|
134
136
|
const sheet = new Sheet()
|
|
135
137
|
const ctx = createSheetContext(sheet)
|
|
136
|
-
sheet.selection.
|
|
138
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
137
139
|
expect(insertImageFromUrl(ctx, 'https://example.com/a.png')).toBeUndefined()
|
|
138
140
|
expect(ctx.getImages()).toHaveLength(0)
|
|
139
141
|
expect(mocks.message.error).toHaveBeenCalledWith('请先选择单元格')
|
|
@@ -179,7 +181,7 @@ describe('insertCellImageFromFile / pickAndEmbedCellImage', () => {
|
|
|
179
181
|
it('无活动格 → message.error,不写入', async () => {
|
|
180
182
|
const sheet = new Sheet()
|
|
181
183
|
const ctx = createSheetContext(sheet)
|
|
182
|
-
sheet.selection.
|
|
184
|
+
sheet.selection.restoreState({ activeCell: null, ranges: [] })
|
|
183
185
|
await insertCellImageFromFile(ctx, makeFile('a.png', 'image/png'))
|
|
184
186
|
expect(mocks.message.error).toHaveBeenCalledWith('请先选择单元格')
|
|
185
187
|
})
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
1
3
|
import type { FloatObject, SceneEvent } from 'infinitable'
|
|
2
4
|
import { createRange } from 'infinitable/sheet'
|
|
3
5
|
import { Workbook } from 'infinitable/sheet'
|
|
4
6
|
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
5
7
|
import { createApp, h, nextTick, ref, type App, type Component } from 'vue'
|
|
6
8
|
|
|
7
|
-
import { USheet } from '../../../index'
|
|
8
9
|
import { registerTool, unregisterTool } from '../../../tools/registry'
|
|
9
10
|
import type { FloatContextMenuPayload, SheetExposed } from '../../../types'
|
|
11
|
+
// 从组件目录导入:内置工具由注册表构造时装入,不依赖包入口副作用
|
|
12
|
+
import { USheet } from '../index'
|
|
10
13
|
import { buildColHeaderMenus, buildRowHeaderMenus } from '../sheet-context-menu'
|
|
11
14
|
// 引擎指针事件模拟与画布几何(cellX/cellY):setup 已跨包引用同目录 setup.ts
|
|
12
15
|
import { cellX, cellY, fire, gridEventTarget } from './grid-test-utils'
|
|
@@ -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
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import './tools/builtin'
|
|
2
|
-
|
|
3
1
|
// SheetContextOptions:createSheetContext(公共导出)的第三参类型——导出函数
|
|
4
2
|
// 签名引用的稳定类型须随函数可达(无头场景宿主需引用该类型),不属引擎符号
|
|
5
3
|
export { createSheetContext, type SheetContext, type SheetContextOptions } from './tools/context'
|
|
@@ -13,6 +11,6 @@ export {
|
|
|
13
11
|
type SheetToolGroup
|
|
14
12
|
} from './tools/registry'
|
|
15
13
|
|
|
16
|
-
export {
|
|
14
|
+
export type { SheetProps, SheetEmits, SheetExposed } from './types'
|
|
17
15
|
|
|
18
16
|
export { USheet } from './components/sheet'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
2
|
|
|
3
|
-
import '
|
|
3
|
+
import { createRange } from 'infinitable/sheet'
|
|
4
4
|
import { Sheet } from 'infinitable/sheet'
|
|
5
5
|
import { Workbook } from 'infinitable/sheet'
|
|
6
6
|
import { describe, expect, it, vi } from 'vite-plus/test'
|
|
@@ -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
|
+
})
|