@veltra/sheet 3.0.0 → 4.1.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.
Files changed (39) hide show
  1. package/dist/components/sheet/float-image-menu.js +59 -0
  2. package/dist/components/sheet/import-file.js +3 -1
  3. package/dist/components/sheet/popup-helpers.js +18 -2
  4. package/dist/components/sheet/popups/hyperlink-popup.js +127 -0
  5. package/dist/components/sheet/popups/import.worker.d.ts +4 -0
  6. package/dist/components/sheet/popups/import.worker.js +3 -1
  7. package/dist/components/sheet/sheet-context-menu.js +81 -7
  8. package/dist/components/sheet/sheet.js +112 -17
  9. package/dist/components/sheet/style2.css +62 -0
  10. package/dist/components/sheet/use-cell-link-tooltip.js +33 -0
  11. package/dist/components/sheet/use-sheet-grid.js +82 -2
  12. package/dist/components/sheet/use-sheet-state.js +1 -0
  13. package/dist/tools/context.d.ts +20 -2
  14. package/dist/tools/context.js +9 -1
  15. package/dist/types/index.d.ts +2 -2
  16. package/dist/types/sheet.d.ts +54 -7
  17. package/package.json +2 -2
  18. package/src/components/sheet/__test__/float-image-menu.test.ts +130 -0
  19. package/src/components/sheet/__test__/grid-test-utils.ts +2 -3
  20. package/src/components/sheet/__test__/hyperlink-popup.test.ts +176 -0
  21. package/src/components/sheet/__test__/import-file.test.ts +33 -2
  22. package/src/components/sheet/__test__/sheet-component.test.ts +476 -18
  23. package/src/components/sheet/__test__/sheet-context-menu.test.ts +255 -2
  24. package/src/components/sheet/__test__/sheet-scrollbar.test.ts +23 -73
  25. package/src/components/sheet/__test__/sheet-theme.test.ts +212 -0
  26. package/src/components/sheet/float-image-menu.ts +78 -0
  27. package/src/components/sheet/import-file.ts +19 -2
  28. package/src/components/sheet/popup-helpers.ts +68 -2
  29. package/src/components/sheet/popups/hyperlink-popup.vue +112 -0
  30. package/src/components/sheet/popups/import.worker.ts +18 -4
  31. package/src/components/sheet/sheet-context-menu.ts +100 -7
  32. package/src/components/sheet/sheet.vue +120 -7
  33. package/src/components/sheet/style.scss +66 -0
  34. package/src/components/sheet/use-cell-link-tooltip.ts +42 -0
  35. package/src/components/sheet/use-sheet-grid.ts +111 -4
  36. package/src/components/sheet/use-sheet-state.ts +8 -2
  37. package/src/tools/__test__/context.test.ts +89 -0
  38. package/src/tools/context.ts +33 -2
  39. package/src/types/sheet.ts +59 -7
@@ -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)
@@ -177,17 +220,18 @@ describe('build*Menus', () => {
177
220
  expect(readonlyCol.render).toBeUndefined()
178
221
  })
179
222
 
180
- it('body 菜单:合并/取消合并 + 设置数据格式子菜单 + 插入图片,不含行列插入删除', () => {
223
+ it('body 菜单:合并/取消合并 + 设置数据格式子菜单 + 插入超链接 + 插入图片,不含行列插入删除', () => {
181
224
  const sheet = new Sheet()
182
225
  const ctx = createSheetContext(sheet)
183
226
  sheet.selectCell({ row: 0, col: 0 })
184
227
  const menus = buildBodyMenus(ctx)
185
- expect(menus).toHaveLength(5)
228
+ expect(menus).toHaveLength(6)
186
229
  expect(menus.map((m) => m.label ?? (m.divider ? '---' : ''))).toEqual([
187
230
  '合并单元格',
188
231
  '取消合并单元格',
189
232
  '---',
190
233
  '设置数据格式',
234
+ '插入超链接',
191
235
  '插入图片'
192
236
  ])
193
237
  const insertImage = menus.find((m) => m.label === '插入图片')!
@@ -286,6 +330,8 @@ describe('build*Menus', () => {
286
330
  const menus = buildBodyMenus(ctx)
287
331
  expect(menus.find((m) => m.label === '插入图片')?.disabled).toBe(true)
288
332
  expect(menus.find((m) => m.label === '设置数据格式')?.disabled).toBe(true)
333
+ // 无活动格:超链接项同样禁用
334
+ expect(menus.find((m) => m.label === '插入超链接')?.disabled).toBe(true)
289
335
  })
290
336
 
291
337
  it('行号/列头菜单仍含插入删除行列', () => {
@@ -321,3 +367,210 @@ describe('build*Menus', () => {
321
367
  expect(menus[0]?.keepOpen).toBe(true)
322
368
  })
323
369
  })
370
+
371
+ describe('buildBodyMenus 超链接项(按目标格链接状态出项)', () => {
372
+ /** 记录句柄调用的简单 spy(mode + 编辑回填载荷) */
373
+ function popupSpy() {
374
+ const calls: Array<{ mode: string; initial?: { url: string; text?: string } }> = []
375
+ const open = (mode: 'cell', initial?: { url: string; text?: string }): void => {
376
+ calls.push({ mode, initial })
377
+ }
378
+ return { calls, open }
379
+ }
380
+
381
+ it('无链接格:仅「插入超链接」,callback 经句柄以 cell 模式拉起弹窗(无回填)', () => {
382
+ const sheet = new Sheet()
383
+ const ctx = createSheetContext(sheet)
384
+ sheet.selectCell({ row: 0, col: 0 })
385
+ const spy = popupSpy()
386
+ const menus = buildBodyMenus(ctx, { openHyperlinkPopup: spy.open })
387
+ const item = menus.find((m) => m.label === '插入超链接')!
388
+ expect(item.disabled).toBeFalsy()
389
+ expect(menus.some((m) => m.label === '编辑超链接')).toBe(false)
390
+ expect(menus.some((m) => m.label === '取消超链接')).toBe(false)
391
+ item.callback!()
392
+ expect(spy.calls).toEqual([{ mode: 'cell', initial: undefined }])
393
+ })
394
+
395
+ it('有链接格:「编辑超链接」回填原 url/text;「取消超链接」clearCellLink 后 getCellLink undefined 且 undo 还原', () => {
396
+ const sheet = new Sheet()
397
+ const ctx = createSheetContext(sheet)
398
+ const addr = { row: 1, col: 2 }
399
+ sheet.selectCell(addr)
400
+ sheet.setCellLink(addr, { url: 'https://example.com', text: '示例' })
401
+ const spy = popupSpy()
402
+ const menus = buildBodyMenus(ctx, { openHyperlinkPopup: spy.open })
403
+
404
+ expect(menus.some((m) => m.label === '插入超链接')).toBe(false)
405
+ const edit = menus.find((m) => m.label === '编辑超链接')!
406
+ expect(edit.disabled).toBeFalsy()
407
+ edit.callback!()
408
+ expect(spy.calls).toEqual([
409
+ { mode: 'cell', initial: { url: 'https://example.com', text: '示例' } }
410
+ ])
411
+
412
+ const clear = menus.find((m) => m.label === '取消超链接')!
413
+ expect(clear.disabled).toBeFalsy()
414
+ clear.callback!()
415
+ expect(ctx.getCellLink(addr)).toBeUndefined()
416
+ expect(ctx.undo()).toBe(true)
417
+ expect(ctx.getCellLink(addr)).toEqual({ url: 'https://example.com', text: '示例' })
418
+ })
419
+
420
+ it('readonly:插入/编辑/取消项禁用;句柄缺省:插入/编辑项禁用', () => {
421
+ const sheet = new Sheet()
422
+ const ctx = createSheetContext(sheet)
423
+ sheet.selectCell({ row: 0, col: 0 })
424
+
425
+ // 句柄缺省(无宿主接线,如直接调用 build):插入禁用
426
+ const noHandle = buildBodyMenus(ctx)
427
+ expect(noHandle.find((m) => m.label === '插入超链接')?.disabled).toBe(true)
428
+
429
+ // readonly:插入禁用
430
+ const readonlyInsert = buildBodyMenus(ctx, { readonly: true, openHyperlinkPopup: () => {} })
431
+ expect(readonlyInsert.find((m) => m.label === '插入超链接')?.disabled).toBe(true)
432
+
433
+ // readonly + 有链接:编辑/取消均禁用
434
+ sheet.setCellLink({ row: 0, col: 0 }, { url: 'https://example.com' })
435
+ const readonlyEdit = buildBodyMenus(ctx, { readonly: true, openHyperlinkPopup: () => {} })
436
+ expect(readonlyEdit.find((m) => m.label === '编辑超链接')?.disabled).toBe(true)
437
+ expect(readonlyEdit.find((m) => m.label === '取消超链接')?.disabled).toBe(true)
438
+ // readonly 不产生模型写入
439
+ expect(readonlyEdit.find((m) => m.label === '取消超链接')?.callback).toBeTruthy()
440
+ })
441
+ })
442
+
443
+ describe('buildBodyMenus 嵌入图项(按目标格嵌入图状态出项)', () => {
444
+ const ADDR = { row: 1, col: 2 }
445
+
446
+ /** 弹窗句柄 spy(含图片目标记录) */
447
+ function popupSpy() {
448
+ const calls: Array<{
449
+ mode: 'image'
450
+ initial?: { url: string }
451
+ target?: { kind: 'cell'; addr: { row: number; col: number } }
452
+ }> = []
453
+ return {
454
+ calls,
455
+ open: (
456
+ mode: 'image',
457
+ initial?: { url: string },
458
+ target?: { kind: 'cell'; addr: { row: number; col: number } }
459
+ ): void => {
460
+ calls.push({ mode, initial, target })
461
+ }
462
+ }
463
+ }
464
+
465
+ /** 预览句柄 spy */
466
+ function previewSpy() {
467
+ const previews: unknown[] = []
468
+ return { previews, open: (source: unknown): void => previews.push(source) }
469
+ }
470
+
471
+ it('无嵌入图:不出现图片项', () => {
472
+ const sheet = new Sheet()
473
+ const ctx = createSheetContext(sheet)
474
+ sheet.selectCell(ADDR)
475
+ const labels = buildBodyMenus(ctx).map((m) => m.label ?? (m.divider ? '---' : ''))
476
+ expect(labels).toHaveLength(6)
477
+ for (const label of ['预览', '图片超链接', '取消超链接', '移除图片']) {
478
+ expect(labels).not.toContain(label)
479
+ }
480
+ })
481
+
482
+ it('无链接嵌入图:divider + 预览 / 图片超链接 / 移除图片(无取消超链接)', () => {
483
+ const sheet = new Sheet()
484
+ const ctx = createSheetContext(sheet)
485
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png' })
486
+ sheet.selectCell(ADDR)
487
+ const popup = popupSpy()
488
+ const preview = previewSpy()
489
+
490
+ const menus = buildBodyMenus(ctx, {
491
+ openHyperlinkPopup: popup.open,
492
+ openImagePreview: preview.open
493
+ })
494
+ expect(menus.map((m) => m.label ?? (m.divider ? '---' : ''))).toEqual([
495
+ '合并单元格',
496
+ '取消合并单元格',
497
+ '---',
498
+ '设置数据格式',
499
+ '插入超链接',
500
+ '插入图片',
501
+ '---',
502
+ '预览',
503
+ '图片超链接',
504
+ '移除图片'
505
+ ])
506
+
507
+ menus.find((m) => m.label === '预览')!.callback!()
508
+ expect(preview.previews).toEqual([{ src: 'https://example.com/a.png' }])
509
+ menus.find((m) => m.label === '图片超链接')!.callback!()
510
+ expect(popup.calls).toEqual([
511
+ { mode: 'image', initial: undefined, target: { kind: 'cell', addr: ADDR } }
512
+ ])
513
+ })
514
+
515
+ it('有链接嵌入图:含取消超链接;图片超链接回填 url;取消清 link 保其余字段且 undo 还原', () => {
516
+ const sheet = new Sheet()
517
+ const ctx = createSheetContext(sheet)
518
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png', link: 'https://example.com' })
519
+ sheet.selectCell(ADDR)
520
+ const popup = popupSpy()
521
+
522
+ const menus = buildBodyMenus(ctx, { openHyperlinkPopup: popup.open })
523
+ expect(menus.map((m) => m.label)).toContain('取消超链接')
524
+
525
+ menus.find((m) => m.label === '图片超链接')!.callback!()
526
+ expect(popup.calls).toEqual([
527
+ {
528
+ mode: 'image',
529
+ initial: { url: 'https://example.com' },
530
+ target: { kind: 'cell', addr: ADDR }
531
+ }
532
+ ])
533
+
534
+ menus.find((m) => m.label === '取消超链接')!.callback!()
535
+ expect(ctx.getCellImage(ADDR)).toEqual({ src: 'https://example.com/a.png' })
536
+ expect(ctx.undo()).toBe(true)
537
+ expect(ctx.getCellImage(ADDR)).toEqual({
538
+ src: 'https://example.com/a.png',
539
+ link: 'https://example.com'
540
+ })
541
+ })
542
+
543
+ it('移除图片:hasCellImage false 且 undo 还原', () => {
544
+ const sheet = new Sheet()
545
+ const ctx = createSheetContext(sheet)
546
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png' })
547
+ sheet.selectCell(ADDR)
548
+
549
+ buildBodyMenus(ctx).find((m) => m.label === '移除图片')!.callback!()
550
+ expect(ctx.hasCellImage(ADDR)).toBe(false)
551
+ expect(ctx.undo()).toBe(true)
552
+ expect(ctx.hasCellImage(ADDR)).toBe(true)
553
+ })
554
+
555
+ it('readonly:图片超链接/取消超链接/移除图片禁用、预览可用;句柄缺省:预览/图片超链接禁用', () => {
556
+ const sheet = new Sheet()
557
+ const ctx = createSheetContext(sheet)
558
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png', link: 'https://e.com' })
559
+ sheet.selectCell(ADDR)
560
+
561
+ const readonly = buildBodyMenus(ctx, {
562
+ readonly: true,
563
+ openHyperlinkPopup: () => {},
564
+ openImagePreview: () => {}
565
+ })
566
+ expect(readonly.find((m) => m.label === '预览')?.disabled).toBeFalsy()
567
+ expect(readonly.find((m) => m.label === '图片超链接')?.disabled).toBe(true)
568
+ expect(readonly.find((m) => m.label === '取消超链接')?.disabled).toBe(true)
569
+ expect(readonly.find((m) => m.label === '移除图片')?.disabled).toBe(true)
570
+
571
+ const noHandles = buildBodyMenus(ctx)
572
+ expect(noHandles.find((m) => m.label === '预览')?.disabled).toBe(true)
573
+ expect(noHandles.find((m) => m.label === '图片超链接')?.disabled).toBe(true)
574
+ expect(noHandles.find((m) => m.label === '移除图片')?.disabled).toBeFalsy()
575
+ })
576
+ })
@@ -10,7 +10,6 @@ const containers: HTMLElement[] = []
10
10
 
11
11
  function mount(props: () => Record<string, unknown>): {
12
12
  exposed: { value: SheetExposed | undefined }
13
- el: HTMLElement
14
13
  } {
15
14
  const exposed: { value: SheetExposed | undefined } = { value: undefined }
16
15
  const el = document.createElement('div')
@@ -29,12 +28,7 @@ function mount(props: () => Record<string, unknown>): {
29
28
  })
30
29
  app.mount(el)
31
30
  apps.push(app)
32
- return { exposed, el }
33
- }
34
-
35
- /** 引擎装配的原生滚动容器(wrapper 带 data-native-scroll 标记,见引擎 NativeScrollbarHost) */
36
- function nativeScrollHost(el: HTMLElement): HTMLElement | null {
37
- return el.querySelector<HTMLElement>('.u-sheet__grid-instance [data-native-scroll]')
31
+ return { exposed }
38
32
  }
39
33
 
40
34
  afterEach(() => {
@@ -42,20 +36,17 @@ afterEach(() => {
42
36
  while (containers.length) containers.pop()!.remove()
43
37
  })
44
38
 
45
- describe('USheet scrollbar(缺省原生档,全形态透传)', () => {
46
- it('缺省:原生档配置下发引擎,usesNativeScrollbar 为 true,grid 区装配原生滚动容器', async () => {
47
- const { exposed, el } = mount(() => ({ workbook: new Workbook(), rows: 10, cols: 6 }))
39
+ describe('USheet scrollbar(缺省画布常驻档,boolean 透传)', () => {
40
+ it('缺省 true:画布常驻档下发引擎', async () => {
41
+ const { exposed } = mount(() => ({ workbook: new Workbook(), rows: 10, cols: 6 }))
48
42
  await nextTick()
49
43
 
50
- const table = exposed.value!.getGrid()!.getTable()
51
- expect(table.options.scrollbar).toEqual({ mode: 'native' })
52
- expect(table.usesNativeScrollbar).toBe(true)
53
- // DOM 断言:引擎在挂载容器内装配真实 overflow 滚动容器(scrollbar-gutter: stable)
54
- expect(nativeScrollHost(el)).not.toBeNull()
44
+ // 组件缺省 true:引擎归一化为 canvas always + reserve,滚动条常驻画在画布内
45
+ expect(exposed.value!.getGrid()!.getTable().options.scrollbar).toBe(true)
55
46
  })
56
47
 
57
- it('scrollbar: false:整体关闭语义不变,无原生滚动容器', async () => {
58
- const { exposed, el } = mount(() => ({
48
+ it('scrollbar: false:整体关闭语义透传', async () => {
49
+ const { exposed } = mount(() => ({
59
50
  workbook: new Workbook(),
60
51
  rows: 10,
61
52
  cols: 6,
@@ -63,47 +54,12 @@ describe('USheet scrollbar(缺省原生档,全形态透传)', () => {
63
54
  }))
64
55
  await nextTick()
65
56
 
66
- const table = exposed.value!.getGrid()!.getTable()
67
- expect(table.options.scrollbar).toBe(false)
68
- expect(table.usesNativeScrollbar).toBe(false)
69
- expect(nativeScrollHost(el)).toBeNull()
70
- })
71
-
72
- it('scrollbar: true:旧语义(画布常驻滚动条)透传,不装配原生容器', async () => {
73
- const { exposed, el } = mount(() => ({
74
- workbook: new Workbook(),
75
- rows: 10,
76
- cols: 6,
77
- scrollbar: true
78
- }))
79
- await nextTick()
80
-
81
- const table = exposed.value!.getGrid()!.getTable()
82
- expect(table.options.scrollbar).toBe(true)
83
- expect(table.usesNativeScrollbar).toBe(false)
84
- expect(nativeScrollHost(el)).toBeNull()
85
- })
86
-
87
- it('对象形态按引用透传(mode: canvas 显式回画布档)', async () => {
88
- const canvasOption = { mode: 'canvas', visibility: 'always' } as const
89
- const { exposed, el } = mount(() => ({
90
- workbook: new Workbook(),
91
- rows: 10,
92
- cols: 6,
93
- scrollbar: canvasOption
94
- }))
95
- await nextTick()
96
-
97
- const table = exposed.value!.getGrid()!.getTable()
98
- // 引用透传:引擎收到宿主的同一对象(构造期选项,宿主保持稳定引用即可)
99
- expect(table.options.scrollbar).toBe(canvasOption)
100
- expect(table.usesNativeScrollbar).toBe(false)
101
- expect(nativeScrollHost(el)).toBeNull()
57
+ expect(exposed.value!.getGrid()!.getTable().options.scrollbar).toBe(false)
102
58
  })
103
59
 
104
- it('watch 重建:对象引用更替与 boolean 切换都触发网格重建,新配置生效', async () => {
105
- const state = ref<boolean | Record<string, unknown>>({ mode: 'native' })
106
- const { exposed, el } = mount(() => ({
60
+ it('watch 重建:boolean 切换触发网格重建,新配置生效', async () => {
61
+ const state = ref(true)
62
+ const { exposed } = mount(() => ({
107
63
  workbook: new Workbook(),
108
64
  rows: 10,
109
65
  cols: 6,
@@ -111,27 +67,21 @@ describe('USheet scrollbar(缺省原生档,全形态透传)', () => {
111
67
  }))
112
68
  await nextTick()
113
69
 
114
- const gridNative = exposed.value!.getGrid()
115
- expect(gridNative!.getTable().usesNativeScrollbar).toBe(true)
116
-
117
- // 对象引用更替(native → canvas 常驻):构造期选项变化 → 重建
118
- state.value = { mode: 'canvas', visibility: 'always' }
119
- await nextTick()
120
- const gridCanvas = exposed.value!.getGrid()
121
- expect(gridCanvas).not.toBe(gridNative)
122
- expect(gridCanvas!.getTable().options.scrollbar).toEqual({
123
- mode: 'canvas',
124
- visibility: 'always'
125
- })
126
- expect(gridCanvas!.getTable().usesNativeScrollbar).toBe(false)
127
- expect(nativeScrollHost(el)).toBeNull()
70
+ const gridOn = exposed.value!.getGrid()
71
+ expect(gridOn!.getTable().options.scrollbar).toBe(true)
128
72
 
129
- // boolean 切换(canvas → false):同样重建,整体关闭
73
+ // true → false:构造期选项变化 → 重建,整体关闭
130
74
  state.value = false
131
75
  await nextTick()
132
76
  const gridOff = exposed.value!.getGrid()
133
- expect(gridOff).not.toBe(gridCanvas)
77
+ expect(gridOff).not.toBe(gridOn)
134
78
  expect(gridOff!.getTable().options.scrollbar).toBe(false)
135
- expect(gridOff!.getTable().usesNativeScrollbar).toBe(false)
79
+
80
+ // false → true:恢复画布常驻档,同样重建
81
+ state.value = true
82
+ await nextTick()
83
+ const gridOnAgain = exposed.value!.getGrid()
84
+ expect(gridOnAgain).not.toBe(gridOff)
85
+ expect(gridOnAgain!.getTable().options.scrollbar).toBe(true)
136
86
  })
137
87
  })
@@ -0,0 +1,212 @@
1
+ import { currentTheme, lightTheme } from '@veltra/styles/theme'
2
+ import { Workbook } from 'infinitable/sheet'
3
+ import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
4
+ import { createApp, h, nextTick, type App } from 'vue'
5
+
6
+ import { USheet } from '../../../index'
7
+ import type { SheetExposed } from '../../../types'
8
+ import { resolveCanvasThemeOverride } from '../use-sheet-grid'
9
+
10
+ /**
11
+ * 画布主题接入(B3):CSS 变量 → 引擎 ThemeOverride 映射 + loadTheme 后缓存
12
+ * 实例原地换肤。变量样本值刻意避开引擎内置缺省色(body #FFF / 表头 #F5F5F5 /
13
+ * 网格线 #E1E4E8 / 冻结线 #B6BABF),断言命中即可区分 override 生效而非缺省。
14
+ */
15
+ const THEME_VARS = {
16
+ '--u-bg-color-top': '#123456',
17
+ '--u-bg-color-bottom': '#234567',
18
+ '--u-text-color-main': '#abcdef',
19
+ '--u-border-color': '#987654',
20
+ '--u-border-muted-color': '#654321'
21
+ } as const
22
+
23
+ /**
24
+ * mock 全局 getComputedStyle:仅覆盖 getPropertyValue 返回的主题 CSS 变量,
25
+ * 未给的变量回退真实计算值,其余属性 / 方法原样透传(挂载路径中引擎
26
+ * normalizeContainer 等仍真实消费 computed style)。返回还原函数。
27
+ */
28
+ function mockThemeVars(vars: Record<string, string>): () => void {
29
+ const real = getComputedStyle
30
+ vi.stubGlobal('getComputedStyle', (el: Element, pseudo?: string | null): CSSStyleDeclaration => {
31
+ const style = real(el, pseudo ?? undefined)
32
+ return new Proxy(style, {
33
+ get(target, prop) {
34
+ if (prop === 'getPropertyValue') {
35
+ return (name: string) => (name in vars ? vars[name]! : target.getPropertyValue(name))
36
+ }
37
+ const value = Reflect.get(target, prop, target)
38
+ return typeof value === 'function' ? value.bind(target) : value
39
+ }
40
+ })
41
+ })
42
+ return () => vi.unstubAllGlobals()
43
+ }
44
+
45
+ const apps: App[] = []
46
+ const containers: HTMLElement[] = []
47
+
48
+ function mount(props: () => Record<string, unknown>): {
49
+ exposed: { value: SheetExposed | undefined }
50
+ el: HTMLElement
51
+ } {
52
+ const exposed: { value: SheetExposed | undefined } = { value: undefined }
53
+ const el = document.createElement('div')
54
+ el.style.width = '800px'
55
+ el.style.height = '600px'
56
+ document.body.appendChild(el)
57
+ containers.push(el)
58
+ const app = createApp({
59
+ render: () =>
60
+ h(USheet, {
61
+ ...props(),
62
+ ref: (value: unknown) => {
63
+ exposed.value = value as SheetExposed | undefined
64
+ }
65
+ })
66
+ })
67
+ app.mount(el)
68
+ apps.push(app)
69
+ return { exposed, el }
70
+ }
71
+
72
+ function tabs(el: HTMLElement): HTMLButtonElement[] {
73
+ return [...el.querySelectorAll<HTMLButtonElement>('.u-sheet__tab')]
74
+ }
75
+
76
+ afterEach(() => {
77
+ while (apps.length) apps.pop()!.unmount()
78
+ while (containers.length) containers.pop()!.remove()
79
+ // 主题单例复位(换肤测试副作用),避免用例间串扰;此时实例已卸载、watcher 已停
80
+ currentTheme.value = undefined
81
+ vi.unstubAllGlobals()
82
+ })
83
+
84
+ describe('resolveCanvasThemeOverride:CSS 变量 → 引擎 ThemeOverride', () => {
85
+ it('变量齐全:按映射表换算(底色/表头/文字/网格线/冻结分隔线)', () => {
86
+ const restore = mockThemeVars({ ...THEME_VARS })
87
+ try {
88
+ expect(resolveCanvasThemeOverride(document.createElement('div'))).toEqual({
89
+ body: { background: '#123456', color: '#abcdef', borderColor: '#987654' },
90
+ header: { background: '#234567', color: '#abcdef', borderColor: '#987654' },
91
+ underlayBackgroundColor: '#123456',
92
+ interaction: { freezeDividerColor: '#654321' },
93
+ frameStyle: { color: '#987654' }
94
+ })
95
+ } finally {
96
+ restore()
97
+ }
98
+ })
99
+
100
+ it('未 loadTheme(变量全空):返回 undefined,保持引擎缺省白底', () => {
101
+ const restore = mockThemeVars({})
102
+ try {
103
+ expect(resolveCanvasThemeOverride(document.createElement('div'))).toBeUndefined()
104
+ } finally {
105
+ restore()
106
+ }
107
+ })
108
+
109
+ it('任一变量为空:返回 undefined,不半套主题', () => {
110
+ for (const key of Object.keys(THEME_VARS)) {
111
+ const restore = mockThemeVars({ ...THEME_VARS, [key]: '' })
112
+ try {
113
+ expect(resolveCanvasThemeOverride(document.createElement('div'))).toBeUndefined()
114
+ } finally {
115
+ restore()
116
+ }
117
+ }
118
+ })
119
+ })
120
+
121
+ describe('USheet 画布主题跟随', () => {
122
+ it('挂载时主题已生效:构造期 override 进入引擎主题', async () => {
123
+ const restore = mockThemeVars({ ...THEME_VARS })
124
+ try {
125
+ const { exposed } = mount(() => ({ workbook: new Workbook(), rows: 10, cols: 6 }))
126
+ await nextTick()
127
+
128
+ const theme = exposed.value!.getGrid()!.getTable().getTheme()
129
+ expect(theme.body.background).toBe('#123456')
130
+ expect(theme.header.background).toBe('#234567')
131
+ expect(theme.body.color).toBe('#abcdef')
132
+ expect(theme.header.color).toBe('#abcdef')
133
+ expect(theme.body.borderColor).toBe('#987654')
134
+ expect(theme.underlayBackgroundColor).toBe('#123456')
135
+ expect(theme.interaction.freezeDividerColor).toBe('#654321')
136
+ expect(theme.frameStyle.color).toBe('#987654')
137
+ } finally {
138
+ restore()
139
+ }
140
+ })
141
+
142
+ it('未 loadTheme 挂载:不带 override,保持引擎内置缺省色', async () => {
143
+ const { exposed } = mount(() => ({ workbook: new Workbook(), rows: 10, cols: 6 }))
144
+ await nextTick()
145
+
146
+ const theme = exposed.value!.getGrid()!.getTable().getTheme()
147
+ expect(theme.body.background).toBe('#FFF')
148
+ expect(theme.header.background).toBe('#F5F5F5')
149
+ })
150
+
151
+ it('loadTheme 实例替换:缓存中所有实例原地 updateTheme,零重建', async () => {
152
+ const workbook = new Workbook()
153
+ workbook.addSheet('Sheet2')
154
+ const { exposed, el } = mount(() => ({ workbook, rows: 10, cols: 6 }))
155
+ await nextTick()
156
+
157
+ // 切到 Sheet2 再切回:两个实例均入缓存(LRU)
158
+ tabs(el)[1]!.click()
159
+ await nextTick()
160
+ const grid2 = exposed.value!.getGrid()!
161
+ tabs(el)[0]!.click()
162
+ await nextTick()
163
+ const grid1 = exposed.value!.getGrid()!
164
+ expect(el.querySelectorAll('.u-sheet__grid-instance')).toHaveLength(2)
165
+ const instancesBefore = [...el.querySelectorAll('.u-sheet__grid-instance')]
166
+
167
+ // 换主题:CSS 变量就位 + loadTheme 替换主题实例 → flush 'post' 后全实例换肤
168
+ const restore = mockThemeVars({ ...THEME_VARS })
169
+ try {
170
+ currentTheme.value = lightTheme
171
+ await nextTick()
172
+ } finally {
173
+ restore()
174
+ }
175
+
176
+ // 缓存中所有实例(含隐藏的 Sheet2 实例)都已换肤
177
+ for (const grid of [grid1, grid2]) {
178
+ const theme = grid.getTable().getTheme()
179
+ expect(theme.body.background).toBe('#123456')
180
+ expect(theme.header.background).toBe('#234567')
181
+ expect(theme.interaction.freezeDividerColor).toBe('#654321')
182
+ }
183
+ // 零重建:实例容器与激活实例都是原对象(updateTheme 原地覆盖,不重建网格)
184
+ const instancesAfter = el.querySelectorAll('.u-sheet__grid-instance')
185
+ expect(instancesAfter).toHaveLength(2)
186
+ expect(instancesAfter[0]).toBe(instancesBefore[0])
187
+ expect(instancesAfter[1]).toBe(instancesBefore[1])
188
+ expect(exposed.value!.getGrid()).toBe(grid1)
189
+ })
190
+
191
+ it('主题 token 原地修改(deep):同样触发换肤', async () => {
192
+ const { exposed } = mount(() => ({ workbook: new Workbook(), rows: 10, cols: 6 }))
193
+ await nextTick()
194
+ const grid = exposed.value!.getGrid()!
195
+
196
+ // 实例替换但变量未注入:不换肤,保持缺省白底
197
+ currentTheme.value = lightTheme
198
+ await nextTick()
199
+ expect(grid.getTable().getTheme().body.background).toBe('#FFF')
200
+
201
+ // 主题 token 原地改(reactive deep)→ 重读 CSS 变量 → updateTheme
202
+ const restore = mockThemeVars({ ...THEME_VARS })
203
+ try {
204
+ lightTheme.theme.bg.color.top = '#123456'
205
+ await nextTick()
206
+ } finally {
207
+ restore()
208
+ lightTheme.theme.bg.color.top = '#ffffff'
209
+ }
210
+ expect(grid.getTable().getTheme().body.background).toBe('#123456')
211
+ })
212
+ })