@veltra/sheet 2.8.1 → 4.0.0
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/formula-bar.js +1 -1
- package/dist/components/sheet/import-file.js +4 -2
- package/dist/components/sheet/insert-image.js +1 -1
- package/dist/components/sheet/popup-helpers.js +1 -1
- package/dist/components/sheet/popups/border-popup.js +1 -2
- package/dist/components/sheet/popups/functions-popup.js +3 -3
- package/dist/components/sheet/popups/import.worker.d.ts +5 -1
- package/dist/components/sheet/popups/import.worker.js +4 -2
- package/dist/components/sheet/sheet-context-menu.js +8 -6
- package/dist/components/sheet/sheet.js +12 -2
- package/dist/components/sheet/use-find-replace.js +1 -2
- package/dist/components/sheet/use-formula-suggest.js +5 -4
- package/dist/components/sheet/use-sheet-grid.js +55 -4
- package/dist/components/sheet/use-sheet-state.js +1 -1
- package/dist/components/sheet/use-tool-popup.js +13 -2
- package/dist/index.d.ts +2 -2
- package/dist/tools/builtin.js +1 -1
- package/dist/tools/context.d.ts +1 -11
- package/dist/tools/download.js +3 -3
- package/dist/tools/export.worker.d.ts +1 -1
- package/dist/tools/export.worker.js +1 -1
- package/dist/tools/registry.js +1 -1
- package/dist/types/sheet.d.ts +12 -6
- package/package.json +2 -3
- package/src/components/sheet/__test__/canvas-mock.ts +49 -0
- package/src/components/sheet/__test__/find-shortcut-anchor.test.ts +117 -0
- package/src/components/sheet/__test__/formula-bar.test.ts +11 -9
- package/src/components/sheet/__test__/formula-capability.test.ts +160 -0
- package/src/components/sheet/__test__/functions-popup.test.ts +7 -7
- package/src/components/sheet/__test__/grid-setup.ts +3 -0
- package/src/components/sheet/__test__/grid-test-utils.ts +46 -0
- package/src/components/sheet/__test__/import-file.test.ts +44 -9
- package/src/components/sheet/__test__/insert-image-popup.test.ts +1 -1
- package/src/components/sheet/__test__/insert-image.test.ts +1 -1
- package/src/components/sheet/__test__/sheet-component.test.ts +9 -19
- package/src/components/sheet/__test__/sheet-context-menu.test.ts +45 -2
- package/src/components/sheet/__test__/sheet-grow-on-scroll.test.ts +105 -0
- package/src/components/sheet/__test__/sheet-scrollbar.test.ts +87 -0
- package/src/components/sheet/__test__/sheet-theme.test.ts +212 -0
- package/src/components/sheet/__test__/use-formula-suggest.test.ts +1 -1
- package/src/components/sheet/formula-bar.vue +4 -4
- package/src/components/sheet/import-file.ts +22 -9
- package/src/components/sheet/insert-image.ts +2 -2
- package/src/components/sheet/popup-helpers.ts +3 -3
- package/src/components/sheet/popups/border-popup.vue +4 -4
- package/src/components/sheet/popups/export-popup.vue +1 -1
- package/src/components/sheet/popups/find-popup.vue +1 -1
- package/src/components/sheet/popups/functions-popup.vue +3 -9
- package/src/components/sheet/popups/import.worker.ts +21 -7
- package/src/components/sheet/sheet-context-menu.ts +18 -8
- package/src/components/sheet/sheet-tabs.vue +2 -2
- package/src/components/sheet/sheet.vue +11 -3
- package/src/components/sheet/use-find-replace.ts +3 -7
- package/src/components/sheet/use-formula-suggest.ts +6 -8
- package/src/components/sheet/use-sheet-grid.ts +58 -6
- package/src/components/sheet/use-sheet-state.ts +2 -2
- package/src/components/sheet/use-tool-popup.ts +22 -2
- package/src/index.ts +3 -1
- package/src/tools/__test__/apply-style.test.ts +2 -2
- package/src/tools/__test__/builtin.test.ts +4 -4
- package/src/tools/__test__/context.test.ts +2 -2
- package/src/tools/apply-style.ts +3 -3
- package/src/tools/builtin.ts +2 -2
- package/src/tools/context.ts +11 -11
- package/src/tools/download.ts +4 -4
- package/src/tools/export.worker.ts +4 -7
- package/src/tools/registry.ts +1 -1
- package/src/types/sheet.ts +15 -7
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
2
|
+
import { createApp, h, nextTick, type App } from 'vue'
|
|
3
|
+
|
|
4
|
+
import { USheet } from '../../../index'
|
|
5
|
+
|
|
6
|
+
/** UDropdownStub 的 open 调用 trigger 记录(vi.mock 工厂提升,需 hoisted) */
|
|
7
|
+
const dropdownState = vi.hoisted(() => ({
|
|
8
|
+
openTriggers: [] as Array<HTMLElement | null | undefined>
|
|
9
|
+
}))
|
|
10
|
+
|
|
11
|
+
// 只替换 UDropdown 为记录 open 传参的桩(断言锚定 reference),其余 desktop 组件保持真实实现
|
|
12
|
+
vi.mock('@veltra/desktop', async (importActual) => {
|
|
13
|
+
const actual = await importActual<Record<string, unknown>>()
|
|
14
|
+
const { defineComponent, h } = await import('vue')
|
|
15
|
+
return {
|
|
16
|
+
...actual,
|
|
17
|
+
UDropdown: defineComponent({
|
|
18
|
+
name: 'UDropdownStub',
|
|
19
|
+
props: ['visible', 'trigger', 'width'],
|
|
20
|
+
setup(props, { expose, slots }) {
|
|
21
|
+
expose({
|
|
22
|
+
open(config?: { trigger?: HTMLElement }) {
|
|
23
|
+
dropdownState.openTriggers.push(config?.trigger ?? null)
|
|
24
|
+
},
|
|
25
|
+
close() {}
|
|
26
|
+
})
|
|
27
|
+
return () => h('div', { class: 'dropdown-stub' }, props.visible ? slots.content?.() : null)
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const apps: App[] = []
|
|
34
|
+
const containers: HTMLElement[] = []
|
|
35
|
+
|
|
36
|
+
function mount(props: () => Record<string, unknown> = () => ({})) {
|
|
37
|
+
const el = document.createElement('div')
|
|
38
|
+
el.style.width = '800px'
|
|
39
|
+
el.style.height = '600px'
|
|
40
|
+
document.body.appendChild(el)
|
|
41
|
+
containers.push(el)
|
|
42
|
+
const app = createApp({ render: () => h(USheet, props()) })
|
|
43
|
+
app.mount(el)
|
|
44
|
+
apps.push(app)
|
|
45
|
+
return { el }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 等待 scheduleOpen 的 setTimeout(0) 宏任务(避免同次事件冒泡误关)+ 渲染 */
|
|
49
|
+
async function flushPopup(): Promise<void> {
|
|
50
|
+
await nextTick()
|
|
51
|
+
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
52
|
+
await nextTick()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** 聚焦 grid(事件源在实例容器内)派发 Ctrl+F,冒泡到 window 的 onGlobalKeydown */
|
|
56
|
+
function pressCtrlF(el: HTMLElement): KeyboardEvent {
|
|
57
|
+
const grid = el.querySelector<HTMLElement>('.u-sheet__grid')!
|
|
58
|
+
const event = new KeyboardEvent('keydown', {
|
|
59
|
+
key: 'f',
|
|
60
|
+
ctrlKey: true,
|
|
61
|
+
bubbles: true,
|
|
62
|
+
cancelable: true
|
|
63
|
+
})
|
|
64
|
+
grid.dispatchEvent(event)
|
|
65
|
+
return event
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function lastOpenTrigger(): HTMLElement | null | undefined {
|
|
69
|
+
return dropdownState.openTriggers[dropdownState.openTriggers.length - 1]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
afterEach(() => {
|
|
73
|
+
while (apps.length) apps.pop()!.unmount()
|
|
74
|
+
while (containers.length) containers.pop()!.remove()
|
|
75
|
+
dropdownState.openTriggers.length = 0
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
describe('Ctrl+F 查找弹窗锚定', () => {
|
|
79
|
+
it('工具栏可见:聚焦 grid 按 Ctrl+F 打开,锚定「查找与替换」按钮(与按钮入口同锚点);再按关闭', async () => {
|
|
80
|
+
const { el } = mount()
|
|
81
|
+
await nextTick()
|
|
82
|
+
|
|
83
|
+
const event = pressCtrlF(el)
|
|
84
|
+
await flushPopup()
|
|
85
|
+
// 拦截浏览器原生查找 + 查找弹层打开
|
|
86
|
+
expect(event.defaultPrevented).toBe(true)
|
|
87
|
+
expect(el.querySelector('.u-sheet__find-input')).not.toBeNull()
|
|
88
|
+
// 锚定 reference 非空且指向工具栏 find 按钮(HTMLElement,与按钮入口完全同锚点)
|
|
89
|
+
const findButton = el.querySelector<HTMLElement>('[data-tool-id="find"]')!
|
|
90
|
+
expect(findButton).not.toBeNull()
|
|
91
|
+
expect(lastOpenTrigger()).toBe(findButton)
|
|
92
|
+
|
|
93
|
+
// 再按一次 Ctrl+F:toggle 关闭(不回归)
|
|
94
|
+
pressCtrlF(el)
|
|
95
|
+
await nextTick()
|
|
96
|
+
expect(el.querySelector('.u-sheet__find-input')).toBeNull()
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('工具栏隐藏(showToolbar=false):Ctrl+F 锚定回退 sheet 顶部容器(锚点非空且在 sheet 根内)', async () => {
|
|
100
|
+
const { el } = mount(() => ({ showToolbar: false }))
|
|
101
|
+
await nextTick()
|
|
102
|
+
|
|
103
|
+
pressCtrlF(el)
|
|
104
|
+
await flushPopup()
|
|
105
|
+
expect(el.querySelector('.u-sheet__find-input')).not.toBeNull()
|
|
106
|
+
// 锚定 reference 非空、指向 sheet 根内元素:工具栏容器(0 高贴 sheet 顶,
|
|
107
|
+
// bottom-start 定位下弹层自 sheet 左上角展开,落在 sheet 视口内而非视口原点)
|
|
108
|
+
const anchor = lastOpenTrigger()
|
|
109
|
+
expect(anchor).toBeInstanceOf(HTMLElement)
|
|
110
|
+
expect(el.contains(anchor as HTMLElement)).toBe(true)
|
|
111
|
+
expect((anchor as HTMLElement).classList.contains('u-sheet__toolbar-wrap')).toBe(true)
|
|
112
|
+
|
|
113
|
+
pressCtrlF(el)
|
|
114
|
+
await nextTick()
|
|
115
|
+
expect(el.querySelector('.u-sheet__find-input')).toBeNull()
|
|
116
|
+
})
|
|
117
|
+
})
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { parseRange } from '
|
|
2
|
-
import { Workbook } from '
|
|
3
|
-
import type { SheetGrid } from '
|
|
1
|
+
import { parseRange } from 'infinitable/sheet'
|
|
2
|
+
import { Workbook } from 'infinitable/sheet'
|
|
3
|
+
import type { SheetGrid } from 'infinitable/sheet'
|
|
4
4
|
import { afterEach, describe, expect, it } from 'vite-plus/test'
|
|
5
5
|
import { createApp, h, nextTick, type App } from 'vue'
|
|
6
6
|
|
|
@@ -8,6 +8,7 @@ import { USheet } from '../../../index'
|
|
|
8
8
|
import { createSheetContext } from '../../../tools/context'
|
|
9
9
|
import type { SheetExposed } from '../../../types'
|
|
10
10
|
import UFormulaBar from '../formula-bar.vue'
|
|
11
|
+
import { gridEventTarget } from './grid-test-utils'
|
|
11
12
|
|
|
12
13
|
const apps: App[] = []
|
|
13
14
|
const containers: HTMLElement[] = []
|
|
@@ -551,11 +552,12 @@ describe('USheet 公式栏:函数补全与引用选择', () => {
|
|
|
551
552
|
await nextTick()
|
|
552
553
|
|
|
553
554
|
// 画布数据格点选(引擎选区事件管线)→ 引用插入 + blur 挂起(引用选择);
|
|
554
|
-
//
|
|
555
|
-
// grid-selection),故插入断言在 pointerup
|
|
556
|
-
//
|
|
557
|
-
//
|
|
558
|
-
|
|
555
|
+
// 引用拾取为手势收敛语义(按下起手记录、抬手一次回交,见引擎
|
|
556
|
+
// grid-selection),故插入断言在 pointerup 之后。指针事件派发到 body 层
|
|
557
|
+
// 画布(canvas 档与原生档的事件源后代,见 gridEventTarget)。画布几何口径
|
|
558
|
+
// 与引擎 grid-theme 一致(行号列 46、列头 28、列宽 80、行高 28)→
|
|
559
|
+
// (51, 117) 命中 A4
|
|
560
|
+
const gridEl = gridEventTarget(el)
|
|
559
561
|
gridEl.dispatchEvent(
|
|
560
562
|
new PointerEvent('pointerdown', { clientX: 51, clientY: 117, bubbles: true })
|
|
561
563
|
)
|
|
@@ -576,7 +578,7 @@ describe('USheet 公式栏:函数补全与引用选择', () => {
|
|
|
576
578
|
expect(sheet.getCellData({ row: 2, col: 0 })).toBeUndefined()
|
|
577
579
|
expect(input.value).toBe('=SUM(A4')
|
|
578
580
|
|
|
579
|
-
// 网格选区拦截 → handleRefSelect
|
|
581
|
+
// 网格选区拦截 → handleRefSelect 路径(引擎选区事件管线,见引擎 grid-selection)
|
|
580
582
|
const { el: barEl, exposed: barExposed } = mountFormulaBar(sheet)
|
|
581
583
|
await nextTick()
|
|
582
584
|
setFxText(barEl, '=SUM(')
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 公式能力可达性(spec 第 4 项 / 下游 meta 报表合计场景):
|
|
3
|
+
* 写公式(setCellFormula / setCellValue '=' 前缀)、读公式(getCellData().f)、
|
|
4
|
+
* 求值(getDisplayValue)、自定义函数注册(infinitable 主入口)、
|
|
5
|
+
* 数据填充后自动重算——全部经 USheet 门面路径(getContext() 的 SheetContext)。
|
|
6
|
+
*/
|
|
7
|
+
import { registerFormulaFunction } from 'infinitable'
|
|
8
|
+
import { Workbook } from 'infinitable/sheet'
|
|
9
|
+
import { afterEach, describe, expect, it } from 'vite-plus/test'
|
|
10
|
+
import { createApp, h, nextTick, type App } from 'vue'
|
|
11
|
+
|
|
12
|
+
import { USheet } from '../../../index'
|
|
13
|
+
import type { SheetContext } from '../../../tools/context'
|
|
14
|
+
import type { SheetExposed } from '../../../types'
|
|
15
|
+
import USheetFunctionsPopup from '../popups/functions-popup.vue'
|
|
16
|
+
import { filterFormulaSuggestions } from '../use-formula-suggest'
|
|
17
|
+
|
|
18
|
+
const apps: App[] = []
|
|
19
|
+
const containers: HTMLElement[] = []
|
|
20
|
+
|
|
21
|
+
/** 挂载 USheet(真实组件 + 内部自建工作簿),返回活动门面 SheetContext */
|
|
22
|
+
function mountSheet(): { getContext: () => SheetContext } {
|
|
23
|
+
const exposed: { value: SheetExposed | undefined } = { value: undefined }
|
|
24
|
+
const el = document.createElement('div')
|
|
25
|
+
el.style.width = '800px'
|
|
26
|
+
el.style.height = '600px'
|
|
27
|
+
document.body.appendChild(el)
|
|
28
|
+
containers.push(el)
|
|
29
|
+
const app = createApp({
|
|
30
|
+
render: () =>
|
|
31
|
+
h(USheet, {
|
|
32
|
+
workbook: new Workbook(),
|
|
33
|
+
rows: 10,
|
|
34
|
+
cols: 6,
|
|
35
|
+
ref: (value: unknown) => {
|
|
36
|
+
exposed.value = value as SheetExposed | undefined
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
app.mount(el)
|
|
41
|
+
apps.push(app)
|
|
42
|
+
return { getContext: () => exposed.value!.getContext() }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 直接挂载函数弹框(与工具栏「函数」按钮 / fx 按钮同一组件) */
|
|
46
|
+
function mountFunctionsPopup(): HTMLElement {
|
|
47
|
+
const el = document.createElement('div')
|
|
48
|
+
document.body.appendChild(el)
|
|
49
|
+
containers.push(el)
|
|
50
|
+
const app = createApp({ render: () => h(USheetFunctionsPopup) })
|
|
51
|
+
app.mount(el)
|
|
52
|
+
apps.push(app)
|
|
53
|
+
return el
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** 点击左侧分类导航项(等一帧渲染),与 functions-popup.test.ts 同一交互 */
|
|
57
|
+
async function clickCategory(el: HTMLElement, category: string): Promise<void> {
|
|
58
|
+
const item = [...el.querySelectorAll<HTMLButtonElement>('.u-sheet__functions-nav-item')].find(
|
|
59
|
+
(button) => button.textContent?.trim() === category
|
|
60
|
+
)
|
|
61
|
+
item!.click()
|
|
62
|
+
await nextTick()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 宿主自定义函数(消费方经 `infinitable` 主入口注册,@veltra/sheet 不转售):
|
|
67
|
+
* 数值参数与区域数值求和——报表「合计」类函数的最小形态。
|
|
68
|
+
*/
|
|
69
|
+
function registerCustomTotal(): void {
|
|
70
|
+
registerFormulaFunction('VELTRATOTAL', {
|
|
71
|
+
meta: {
|
|
72
|
+
params: [{ name: 'number1' }, { name: 'number2', optional: true }, { name: '...' }],
|
|
73
|
+
description: '测试用自定义求和(公式能力可达性)',
|
|
74
|
+
category: '数学'
|
|
75
|
+
},
|
|
76
|
+
impl(args) {
|
|
77
|
+
let total = 0
|
|
78
|
+
for (const arg of args) {
|
|
79
|
+
if (Array.isArray(arg)) {
|
|
80
|
+
for (const item of arg) if (typeof item === 'number') total += item
|
|
81
|
+
} else if (typeof arg === 'number') {
|
|
82
|
+
total += arg
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return total
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
afterEach(() => {
|
|
91
|
+
while (apps.length) apps.pop()!.unmount()
|
|
92
|
+
while (containers.length) containers.pop()!.remove()
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe('公式能力可达性(USheet 门面路径)', () => {
|
|
96
|
+
it('setCellFormula 写入 SUM:getDisplayValue 得计算值;getCellData().f 读回公式(不含 = 前缀)', async () => {
|
|
97
|
+
const { getContext } = mountSheet()
|
|
98
|
+
await nextTick()
|
|
99
|
+
const ctx = getContext()
|
|
100
|
+
|
|
101
|
+
ctx.setCellValue({ row: 0, col: 1 }, 10)
|
|
102
|
+
ctx.setCellValue({ row: 1, col: 1 }, 20)
|
|
103
|
+
ctx.setCellValue({ row: 2, col: 1 }, 30)
|
|
104
|
+
ctx.setCellFormula({ row: 0, col: 0 }, '=SUM(B1:B3)')
|
|
105
|
+
|
|
106
|
+
expect(ctx.getDisplayValue({ row: 0, col: 0 })).toBe(60)
|
|
107
|
+
expect(ctx.getCellData({ row: 0, col: 0 })?.f).toBe('SUM(B1:B3)')
|
|
108
|
+
|
|
109
|
+
// setCellValue 的 '=' 前缀同样进公式通道(报表配置的另一种写法)
|
|
110
|
+
ctx.setCellValue({ row: 1, col: 0 }, '=SUM(B1:B2)')
|
|
111
|
+
expect(ctx.getDisplayValue({ row: 1, col: 0 })).toBe(30)
|
|
112
|
+
expect(ctx.getCellData({ row: 1, col: 0 })?.f).toBe('SUM(B1:B2)')
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('批量 setCells 填充被引用区域后公式自动重算(命令执行后引擎增量重算,无需手动刷新)', async () => {
|
|
116
|
+
const { getContext } = mountSheet()
|
|
117
|
+
await nextTick()
|
|
118
|
+
const ctx = getContext()
|
|
119
|
+
|
|
120
|
+
// 先写公式:被引用区域为空 → SUM 空区域为 0
|
|
121
|
+
ctx.setCellFormula({ row: 0, col: 0 }, '=SUM(B1:B3)')
|
|
122
|
+
expect(ctx.getDisplayValue({ row: 0, col: 0 })).toBe(0)
|
|
123
|
+
|
|
124
|
+
// 报表数据填充:一次 setCells = 一个 undo 单元,填充后合计自动得出
|
|
125
|
+
ctx.setCells([
|
|
126
|
+
{ addr: { row: 0, col: 1 }, data: { v: 10, t: 'n' } },
|
|
127
|
+
{ addr: { row: 1, col: 1 }, data: { v: 20, t: 'n' } },
|
|
128
|
+
{ addr: { row: 2, col: 1 }, data: { v: 30, t: 'n' } }
|
|
129
|
+
])
|
|
130
|
+
expect(ctx.getDisplayValue({ row: 0, col: 0 })).toBe(60)
|
|
131
|
+
|
|
132
|
+
// 修改被引用单元格 → 依赖格重算
|
|
133
|
+
ctx.setCellValue({ row: 1, col: 1 }, 25)
|
|
134
|
+
expect(ctx.getDisplayValue({ row: 0, col: 0 })).toBe(65)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('registerFormulaFunction(infinitable 主入口)注册的自定义函数:fx 补全与函数弹框可达、公式求值可用', async () => {
|
|
138
|
+
registerCustomTotal()
|
|
139
|
+
|
|
140
|
+
// fx 输入栏补全列表按前缀命中自定义函数
|
|
141
|
+
expect(filterFormulaSuggestions('VELTRATOTA').map((item) => item.name)).toEqual(['VELTRATOTAL'])
|
|
142
|
+
|
|
143
|
+
// 函数弹框「全部」列出其签名(与内置函数同一列表)
|
|
144
|
+
const popup = mountFunctionsPopup()
|
|
145
|
+
await clickCategory(popup, '全部')
|
|
146
|
+
const signatures = [...popup.querySelectorAll('.u-sheet__functions-signature')].map(
|
|
147
|
+
(node) => node.textContent
|
|
148
|
+
)
|
|
149
|
+
expect(signatures).toContain('VELTRATOTAL(number1, [number2], ...)')
|
|
150
|
+
|
|
151
|
+
// 求值:区域参数 + 标量参数混合(args 收到 [数组, 数值])
|
|
152
|
+
const { getContext } = mountSheet()
|
|
153
|
+
await nextTick()
|
|
154
|
+
const ctx = getContext()
|
|
155
|
+
ctx.setCellValue({ row: 0, col: 1 }, 10)
|
|
156
|
+
ctx.setCellValue({ row: 1, col: 1 }, 20)
|
|
157
|
+
ctx.setCellFormula({ row: 0, col: 0 }, '=VELTRATOTAL(B1:B2, 5)')
|
|
158
|
+
expect(ctx.getDisplayValue({ row: 0, col: 0 })).toBe(35)
|
|
159
|
+
})
|
|
160
|
+
})
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
listFormulaFunctions,
|
|
3
|
-
registerFormulaFunction
|
|
4
|
-
} from '@veltra/sheet-core/core/formula/functions.js'
|
|
1
|
+
import { listFormulaFunctions, registerFormulaFunction } from 'infinitable'
|
|
5
2
|
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
6
3
|
import { createApp, h, nextTick, type App } from 'vue'
|
|
7
4
|
|
|
@@ -97,7 +94,7 @@ describe('USheetFunctionsPopup', () => {
|
|
|
97
94
|
|
|
98
95
|
it('「全部」列出全部已注册函数;分类项只列声明该分类的函数', async () => {
|
|
99
96
|
registerFormulaFunction('MYTESTFN', {
|
|
100
|
-
meta: { params: ['value'], description: '测试未分类自定义函数' },
|
|
97
|
+
meta: { params: [{ name: 'value' }], description: '测试未分类自定义函数' },
|
|
101
98
|
impl: () => 1
|
|
102
99
|
})
|
|
103
100
|
const { el } = mountPopup()
|
|
@@ -110,9 +107,12 @@ describe('USheetFunctionsPopup', () => {
|
|
|
110
107
|
await clickCategory(el, '数学')
|
|
111
108
|
const expected = listFormulaFunctions().filter((fn) => fn.category === '数学')
|
|
112
109
|
expect(items(el).length).toBe(expected.length)
|
|
113
|
-
expect(signatures(el)).toContain('SUM(number1, number2, ...)')
|
|
114
110
|
expect(signatures(el)).not.toContain('MYTESTFN(value)')
|
|
115
111
|
|
|
112
|
+
// SUM 在官方注册表归「常用」分类
|
|
113
|
+
await clickCategory(el, '常用')
|
|
114
|
+
expect(signatures(el)).toContain('SUM(number1, [number2], ...)')
|
|
115
|
+
|
|
116
116
|
// 「常用」也不含未分类自定义函数
|
|
117
117
|
await clickCategory(el, '常用')
|
|
118
118
|
expect(signatures(el)).not.toContain('MYTESTFN(value)')
|
|
@@ -120,7 +120,7 @@ describe('USheetFunctionsPopup', () => {
|
|
|
120
120
|
|
|
121
121
|
it('搜索跨分类(含未分类函数):名称 / 描述大小写不敏感;无匹配显空态', async () => {
|
|
122
122
|
registerFormulaFunction('MYTESTFN', {
|
|
123
|
-
meta: { params: ['value'], description: '测试未分类自定义函数' },
|
|
123
|
+
meta: { params: [{ name: 'value' }], description: '测试未分类自定义函数' },
|
|
124
124
|
impl: () => 1
|
|
125
125
|
})
|
|
126
126
|
const { el } = mountPopup()
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SheetGrid 测试几何与事件工具。几何口径与官方 `infinitable/sheet` 的
|
|
3
|
+
* grid-theme 取值一致:行号列宽 46、列头高 28、列宽 80、行高 28。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const ROW_HEADER_WIDTH = 46
|
|
7
|
+
const HEADER_HEIGHT = 28
|
|
8
|
+
const DEFAULT_COL_WIDTH = 80
|
|
9
|
+
const DEFAULT_ROW_HEIGHT = 28
|
|
10
|
+
|
|
11
|
+
/** 数据格 (col, row) 的层坐标(+5 落在格内而非边线上) */
|
|
12
|
+
export const cellX = (col: number) => ROW_HEADER_WIDTH + col * DEFAULT_COL_WIDTH + 5
|
|
13
|
+
export const cellY = (row: number) => HEADER_HEIGHT + row * DEFAULT_ROW_HEIGHT + 5
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 引擎指针事件派发目标:body 层画布。引擎事件源是挂载容器,body 画布
|
|
17
|
+
* 是其后代,从它派发(冒泡)到达引擎监听。
|
|
18
|
+
*/
|
|
19
|
+
export function gridEventTarget(root: HTMLElement): HTMLElement {
|
|
20
|
+
return root.querySelector<HTMLElement>(".u-sheet__grid-instance canvas[data-layer-kind='body']")!
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 向容器派发 DOM 指针类事件(引擎 EventSystem 归一化为场景事件) */
|
|
24
|
+
export function fire(
|
|
25
|
+
container: HTMLElement,
|
|
26
|
+
type: string,
|
|
27
|
+
init: {
|
|
28
|
+
clientX?: number
|
|
29
|
+
clientY?: number
|
|
30
|
+
button?: number
|
|
31
|
+
bubbles?: boolean
|
|
32
|
+
ctrlKey?: boolean
|
|
33
|
+
shiftKey?: boolean
|
|
34
|
+
} = {}
|
|
35
|
+
): void {
|
|
36
|
+
const event = new MouseEvent(type, {
|
|
37
|
+
bubbles: init.bubbles ?? true,
|
|
38
|
+
cancelable: true,
|
|
39
|
+
clientX: init.clientX ?? 0,
|
|
40
|
+
clientY: init.clientY ?? 0,
|
|
41
|
+
button: init.button ?? 0,
|
|
42
|
+
ctrlKey: init.ctrlKey ?? false,
|
|
43
|
+
shiftKey: init.shiftKey ?? false
|
|
44
|
+
})
|
|
45
|
+
container.dispatchEvent(event)
|
|
46
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Sheet } from '
|
|
2
|
-
import { Workbook } from '
|
|
1
|
+
import type { Sheet } from 'infinitable/sheet'
|
|
2
|
+
import { Workbook } from 'infinitable/sheet'
|
|
3
3
|
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
4
4
|
import { ref } from 'vue'
|
|
5
5
|
|
|
@@ -16,7 +16,9 @@ const mocks = vi.hoisted(() => {
|
|
|
16
16
|
importCsv: vi.fn(),
|
|
17
17
|
importXlsx: vi.fn(() =>
|
|
18
18
|
Promise.resolve({
|
|
19
|
-
getSheets: ()
|
|
19
|
+
getSheets: (): MockSheet[] => [
|
|
20
|
+
{ name: 'S1', snapshot: () => EMPTY_SNAPSHOT, defaultRowHeight: 20, defaultColWidth: 64 }
|
|
21
|
+
],
|
|
20
22
|
activeSheetIndex: 0
|
|
21
23
|
})
|
|
22
24
|
),
|
|
@@ -34,13 +36,25 @@ const EMPTY_SNAPSHOT = {
|
|
|
34
36
|
cols: 0
|
|
35
37
|
}
|
|
36
38
|
|
|
39
|
+
/** importXlsx mock 的 sheet 形状(默认尺寸可选:不写即未声明 → 不透传路径) */
|
|
40
|
+
type MockSheet = {
|
|
41
|
+
name: string
|
|
42
|
+
snapshot: () => typeof EMPTY_SNAPSHOT
|
|
43
|
+
defaultRowHeight?: number
|
|
44
|
+
defaultColWidth?: number
|
|
45
|
+
}
|
|
46
|
+
|
|
37
47
|
vi.mock('@veltra/desktop', () => ({ message: mocks.message, messageConfirm: mocks.messageConfirm }))
|
|
38
48
|
|
|
39
|
-
vi.mock('
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
49
|
+
vi.mock('infinitable/sheet', async (importActual) => {
|
|
50
|
+
const actual = await importActual<typeof import('infinitable/sheet')>()
|
|
51
|
+
return {
|
|
52
|
+
...actual,
|
|
53
|
+
importCsv: mocks.importCsv,
|
|
54
|
+
importXlsx: mocks.importXlsx,
|
|
55
|
+
replaceWorkbookWithSnapshots: mocks.replaceWorkbookWithSnapshots
|
|
56
|
+
}
|
|
57
|
+
})
|
|
44
58
|
|
|
45
59
|
function makeOptions(
|
|
46
60
|
overrides: {
|
|
@@ -106,7 +120,7 @@ describe('importFromFile 导入确认兜底', () => {
|
|
|
106
120
|
expect(mocks.replaceWorkbookWithSnapshots).toHaveBeenCalledTimes(1)
|
|
107
121
|
expect(mocks.replaceWorkbookWithSnapshots).toHaveBeenCalledWith(
|
|
108
122
|
expect.anything(),
|
|
109
|
-
[{ name: 'S1', snapshot: EMPTY_SNAPSHOT }],
|
|
123
|
+
[{ name: 'S1', snapshot: EMPTY_SNAPSHOT, defaultRowHeight: 20, defaultColWidth: 64 }],
|
|
110
124
|
0
|
|
111
125
|
)
|
|
112
126
|
expect(replaced).toHaveBeenCalledTimes(1)
|
|
@@ -169,6 +183,27 @@ describe('importFromFile 导入确认兜底', () => {
|
|
|
169
183
|
})
|
|
170
184
|
})
|
|
171
185
|
|
|
186
|
+
describe('importFromFile 默认行列尺寸透传', () => {
|
|
187
|
+
it('文件未声明默认尺寸:条目不携带两字段(目标保持现状)', async () => {
|
|
188
|
+
mocks.importXlsx.mockImplementationOnce(() =>
|
|
189
|
+
Promise.resolve({
|
|
190
|
+
getSheets: () => [{ name: 'S1', snapshot: () => EMPTY_SNAPSHOT }],
|
|
191
|
+
activeSheetIndex: 0
|
|
192
|
+
})
|
|
193
|
+
)
|
|
194
|
+
const options = makeOptions()
|
|
195
|
+
await triggerXlsxAndAwait(options)
|
|
196
|
+
const onClosed = getOnClosed()
|
|
197
|
+
await onClosed('confirm')
|
|
198
|
+
|
|
199
|
+
expect(mocks.replaceWorkbookWithSnapshots).toHaveBeenCalledTimes(1)
|
|
200
|
+
const [entry] = mocks.replaceWorkbookWithSnapshots.mock.calls[0]![1]
|
|
201
|
+
expect(entry).toEqual({ name: 'S1', snapshot: EMPTY_SNAPSHOT })
|
|
202
|
+
expect(entry).not.toHaveProperty('defaultRowHeight')
|
|
203
|
+
expect(entry).not.toHaveProperty('defaultColWidth')
|
|
204
|
+
})
|
|
205
|
+
})
|
|
206
|
+
|
|
172
207
|
describe('pickAndImportFile', () => {
|
|
173
208
|
it('创建隐藏 file input(同一 accept)并 click', () => {
|
|
174
209
|
const click = vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(() => {})
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { createRange } from '
|
|
2
|
-
import { Workbook } from '
|
|
1
|
+
import { createRange } from 'infinitable/sheet'
|
|
2
|
+
import { Workbook } from 'infinitable/sheet'
|
|
3
3
|
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
4
4
|
import { createApp, h, nextTick, ref, type App, type Component } from 'vue'
|
|
5
5
|
|
|
6
|
-
// 引擎指针事件模拟与画布几何(cellX/cellY):setup 已跨包引用同目录 setup.ts
|
|
7
|
-
import { cellX, cellY, fire } from '../../../../../sheet-core/src/grid/__test__/grid-test-utils'
|
|
8
6
|
import { USheet } from '../../../index'
|
|
9
7
|
import { registerTool, unregisterTool } from '../../../tools/registry'
|
|
10
8
|
import type { SheetExposed } from '../../../types'
|
|
11
9
|
import { buildColHeaderMenus, buildRowHeaderMenus } from '../sheet-context-menu'
|
|
10
|
+
// 引擎指针事件模拟与画布几何(cellX/cellY):setup 已跨包引用同目录 setup.ts
|
|
11
|
+
import { cellX, cellY, fire, gridEventTarget } from './grid-test-utils'
|
|
12
12
|
|
|
13
13
|
const apps: App[] = []
|
|
14
14
|
const containers: HTMLElement[] = []
|
|
@@ -84,18 +84,8 @@ afterEach(() => {
|
|
|
84
84
|
})
|
|
85
85
|
|
|
86
86
|
describe('USheet 组件', () => {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
mount(() => ({}), exposedOn)
|
|
90
|
-
await nextTick()
|
|
91
|
-
// 引擎缺省 true(undefined 即开启),无需显式下发
|
|
92
|
-
expect(exposedOn.value!.getGrid()!.getTable().options.scrollbar).toBeUndefined()
|
|
93
|
-
|
|
94
|
-
const exposedOff: { value: SheetExposed | undefined } = { value: undefined }
|
|
95
|
-
mount(() => ({ scrollbar: false }), exposedOff)
|
|
96
|
-
await nextTick()
|
|
97
|
-
expect(exposedOff.value!.getGrid()!.getTable().options.scrollbar).toBe(false)
|
|
98
|
-
})
|
|
87
|
+
// scrollbar 形态与语义(缺省画布常驻档 / false / watch 重建)见同目录
|
|
88
|
+
// sheet-scrollbar.test.ts
|
|
99
89
|
|
|
100
90
|
it('挂载后默认选区 A1:名称框显示 A1、fx 输入栏可用', async () => {
|
|
101
91
|
const workbook = createWorkbook()
|
|
@@ -1214,7 +1204,7 @@ describe('USheet colResize(readonly 列宽拖拽)', () => {
|
|
|
1214
1204
|
await nextTick()
|
|
1215
1205
|
|
|
1216
1206
|
// 第 0 列右缘(46 + 80 = 126,±4 手柄区)列头带内拖拽:宽 = 起始 80 + 指针位移 42
|
|
1217
|
-
const instance = el
|
|
1207
|
+
const instance = gridEventTarget(el)
|
|
1218
1208
|
fire(instance, 'pointerdown', { clientX: 124, clientY: 10 })
|
|
1219
1209
|
fire(instance, 'pointermove', { clientX: 166, clientY: 10 })
|
|
1220
1210
|
fire(instance, 'pointerup', { clientX: 166, clientY: 10 })
|
|
@@ -1235,7 +1225,7 @@ describe('USheet colResize(readonly 列宽拖拽)', () => {
|
|
|
1235
1225
|
}))
|
|
1236
1226
|
await nextTick()
|
|
1237
1227
|
|
|
1238
|
-
const instance = el
|
|
1228
|
+
const instance = gridEventTarget(el)
|
|
1239
1229
|
fire(instance, 'pointerdown', { clientX: 124, clientY: 10 })
|
|
1240
1230
|
fire(instance, 'pointermove', { clientX: 166, clientY: 10 })
|
|
1241
1231
|
fire(instance, 'pointerup', { clientX: 166, clientY: 10 })
|
|
@@ -1277,7 +1267,7 @@ describe('fx 选区锚点', () => {
|
|
|
1277
1267
|
fxInput.value = '=SUM('
|
|
1278
1268
|
fxInput.setSelectionRange(5, 5)
|
|
1279
1269
|
fxInput.dispatchEvent(new Event('input', { bubbles: true }))
|
|
1280
|
-
const instance = el
|
|
1270
|
+
const instance = gridEventTarget(el)
|
|
1281
1271
|
fire(instance, 'pointerdown', { clientX: cellX(1), clientY: cellY(1) })
|
|
1282
1272
|
fire(instance, 'pointermove', { clientX: cellX(3), clientY: cellY(3) })
|
|
1283
1273
|
fire(instance, 'pointerup', { clientX: cellX(3), clientY: cellY(3) })
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { createRange } from '
|
|
2
|
-
import { Sheet } from '
|
|
1
|
+
import { createRange } from 'infinitable/sheet'
|
|
2
|
+
import { Sheet } from 'infinitable/sheet'
|
|
3
3
|
import { describe, expect, it } from 'vite-plus/test'
|
|
4
4
|
|
|
5
5
|
import { createSheetContext } from '../../../tools/context'
|
|
@@ -134,6 +134,49 @@ describe('build*Menus', () => {
|
|
|
134
134
|
expect(menus.find((m) => m.label === '取消冻结')?.disabled).toBe(false)
|
|
135
135
|
})
|
|
136
136
|
|
|
137
|
+
it('冻结以右键命中行列为准,不放大整行/整列残留选区(先冻行再冻列不失效)', () => {
|
|
138
|
+
const sheet = new Sheet()
|
|
139
|
+
const ctx = createSheetContext(sheet)
|
|
140
|
+
// 用户路径:右键行号 2 冻结到第 3 行 → 残留整行选区(行 2 × 全部列 0..5)
|
|
141
|
+
sheet.selectRange(wholeRowRange(2, 6))
|
|
142
|
+
sheet.setFrozen(3, 0)
|
|
143
|
+
// 随后右键列头 col 1:落点在整行选区内 → ensureContextMenuSelection 保留选区,
|
|
144
|
+
// 冻结列必须用命中的 col=1 而非选区末端 col=5(否则冻结全部列,视觉不生效)
|
|
145
|
+
ensureContextMenuSelection(
|
|
146
|
+
ctx,
|
|
147
|
+
{ kind: 'col-header', addr: null, col: 1 },
|
|
148
|
+
{ rows: 10, cols: 6 }
|
|
149
|
+
)
|
|
150
|
+
expect(ctx.getSelection().ranges[0]).toEqual(wholeRowRange(2, 6))
|
|
151
|
+
const menus = buildColHeaderMenus(ctx, { col: 1 })
|
|
152
|
+
menus.find((m) => m.label === '冻结到当前列')!.callback!()
|
|
153
|
+
expect(sheet.frozen).toEqual({ rows: 3, cols: 2 })
|
|
154
|
+
|
|
155
|
+
// 对称路径:先冻列 → 残留整列选区(全部行 0..9 × col 1)→ 右键行号 4 冻结到第 5 行
|
|
156
|
+
sheet.setFrozen(0, 0)
|
|
157
|
+
sheet.selectRange(wholeColRange(1, 10))
|
|
158
|
+
sheet.setFrozen(0, 2)
|
|
159
|
+
ensureContextMenuSelection(
|
|
160
|
+
ctx,
|
|
161
|
+
{ kind: 'row-header', addr: null, row: 4 },
|
|
162
|
+
{ rows: 10, cols: 6 }
|
|
163
|
+
)
|
|
164
|
+
const rowMenus = buildRowHeaderMenus(ctx, { row: 4 })
|
|
165
|
+
rowMenus.find((m) => m.label === '冻结到当前行')!.callback!()
|
|
166
|
+
expect(sheet.frozen).toEqual({ rows: 5, cols: 2 })
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('冻结项未传命中行列时回落选区末端(旧调用方兼容)', () => {
|
|
170
|
+
const sheet = new Sheet()
|
|
171
|
+
const ctx = createSheetContext(sheet)
|
|
172
|
+
sheet.selectRange(createRange({ row: 0, col: 0 }, { row: 2, col: 5 }))
|
|
173
|
+
buildRowHeaderMenus(ctx).find((m) => m.label === '冻结到当前行')!.callback!()
|
|
174
|
+
expect(sheet.frozen.rows).toBe(3)
|
|
175
|
+
sheet.setFrozen(0, 0)
|
|
176
|
+
buildColHeaderMenus(ctx).find((m) => m.label === '冻结到当前列')!.callback!()
|
|
177
|
+
expect(sheet.frozen.cols).toBe(6)
|
|
178
|
+
})
|
|
179
|
+
|
|
137
180
|
it('列头菜单对称', () => {
|
|
138
181
|
const sheet = new Sheet()
|
|
139
182
|
const ctx = createSheetContext(sheet)
|