@veltra/sheet 4.1.0 → 4.1.2
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 +33 -5
- package/dist/components/sheet/formula-bar.js +95 -11
- package/dist/components/sheet/insert-image.js +33 -4
- package/dist/components/sheet/sheet-context-menu.js +56 -11
- package/dist/components/sheet/sheet.js +30 -6
- package/dist/components/sheet/style.d.ts +2 -0
- package/dist/components/sheet/style.js +2 -0
- package/dist/components/sheet/use-formula-ref-select.js +11 -3
- package/dist/components/sheet/use-sheet-grid.js +12 -3
- package/dist/types/sheet.d.ts +4 -3
- package/package.json +2 -4
- package/src/components/sheet/__test__/float-image-menu.test.ts +72 -6
- package/src/components/sheet/__test__/formula-bar.test.ts +203 -0
- package/src/components/sheet/__test__/insert-image.test.ts +60 -0
- package/src/components/sheet/__test__/sheet-component.test.ts +68 -3
- package/src/components/sheet/__test__/sheet-context-menu.test.ts +96 -17
- package/src/components/sheet/__test__/use-formula-ref-select.test.ts +12 -1
- package/src/components/sheet/float-image-menu.ts +26 -4
- package/src/components/sheet/formula-bar.vue +106 -14
- package/src/components/sheet/insert-image.ts +32 -3
- package/src/components/sheet/sheet-context-menu.ts +48 -12
- package/src/components/sheet/sheet.vue +62 -28
- package/src/components/sheet/style.ts +3 -1
- package/src/components/sheet/use-formula-ref-select.ts +13 -0
- package/src/components/sheet/use-sheet-grid.ts +30 -9
- package/src/types/sheet.ts +4 -3
|
@@ -2,7 +2,7 @@ import { Sheet } from 'infinitable/sheet'
|
|
|
2
2
|
import { describe, expect, it } from 'vite-plus/test'
|
|
3
3
|
|
|
4
4
|
import { createSheetContext } from '../../../tools/context'
|
|
5
|
-
import { buildFloatImageMenus } from '../float-image-menu'
|
|
5
|
+
import { buildFloatImageMenus, embedFloatImageToCell } from '../float-image-menu'
|
|
6
6
|
import type { ImagePreviewSource } from '../popup-helpers'
|
|
7
7
|
|
|
8
8
|
/** 插入一张浮动图(data 字节 + 可选 src/link),返回 id */
|
|
@@ -43,14 +43,14 @@ function handlesSpy() {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
describe('buildFloatImageMenus(浮动图右键独立菜单)', () => {
|
|
46
|
-
it('无链接浮动图:预览 / 设置超链接 / 删除图片(无取消超链接);预览源 src 优先', () => {
|
|
46
|
+
it('无链接浮动图:预览 / 设置超链接 / 嵌入单元格 / 删除图片(无取消超链接);预览源 src 优先', () => {
|
|
47
47
|
const sheet = new Sheet()
|
|
48
48
|
const ctx = createSheetContext(sheet)
|
|
49
49
|
const id = insertFloatImage(sheet, { src: 'https://example.com/a.png' })
|
|
50
50
|
const spy = handlesSpy()
|
|
51
51
|
|
|
52
52
|
const menus = buildFloatImageMenus(ctx, id, spy.handles)
|
|
53
|
-
expect(menus.map((m) => m.label)).toEqual(['预览', '设置超链接', '删除图片'])
|
|
53
|
+
expect(menus.map((m) => m.label)).toEqual(['预览', '设置超链接', '嵌入单元格', '删除图片'])
|
|
54
54
|
|
|
55
55
|
menus.find((m) => m.label === '预览')!.callback!()
|
|
56
56
|
expect(spy.previews).toEqual([{ src: 'https://example.com/a.png' }])
|
|
@@ -78,7 +78,13 @@ describe('buildFloatImageMenus(浮动图右键独立菜单)', () => {
|
|
|
78
78
|
const spy = handlesSpy()
|
|
79
79
|
|
|
80
80
|
const menus = buildFloatImageMenus(ctx, id, spy.handles)
|
|
81
|
-
expect(menus.map((m) => m.label)).toEqual([
|
|
81
|
+
expect(menus.map((m) => m.label)).toEqual([
|
|
82
|
+
'预览',
|
|
83
|
+
'设置超链接',
|
|
84
|
+
'取消超链接',
|
|
85
|
+
'嵌入单元格',
|
|
86
|
+
'删除图片'
|
|
87
|
+
])
|
|
82
88
|
|
|
83
89
|
menus.find((m) => m.label === '设置超链接')!.callback!()
|
|
84
90
|
expect(spy.popups).toEqual([
|
|
@@ -109,7 +115,65 @@ describe('buildFloatImageMenus(浮动图右键独立菜单)', () => {
|
|
|
109
115
|
expect(buildFloatImageMenus(ctx, 'missing', handlesSpy().handles)).toEqual([])
|
|
110
116
|
})
|
|
111
117
|
|
|
112
|
-
it('
|
|
118
|
+
it('嵌入单元格:浮动图转锚点格嵌入图(src 优先 / data 字节两形态,link 随迁),单 undo 还原', () => {
|
|
119
|
+
const sheet = new Sheet()
|
|
120
|
+
const ctx = createSheetContext(sheet)
|
|
121
|
+
const addr = { row: 2, col: 3 }
|
|
122
|
+
const id = sheet.insertImage({
|
|
123
|
+
data: new Uint8Array([1, 2, 3]),
|
|
124
|
+
type: 'png',
|
|
125
|
+
anchor: { from: addr },
|
|
126
|
+
src: 'https://example.com/a.png',
|
|
127
|
+
link: 'https://example.com'
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
embedFloatImageToCell(ctx, id)
|
|
131
|
+
// 浮动图消失,锚点格出现同源嵌入图(src 优先于字节),link 随迁
|
|
132
|
+
expect(ctx.getImages().some((img) => img.id === id)).toBe(false)
|
|
133
|
+
expect(ctx.getCellImage(addr)).toEqual({
|
|
134
|
+
src: 'https://example.com/a.png',
|
|
135
|
+
type: 'png',
|
|
136
|
+
link: 'https://example.com'
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
// 事务合并:一次 undo 同时还原浮动图、清掉嵌入图
|
|
140
|
+
expect(ctx.undo()).toBe(true)
|
|
141
|
+
expect(ctx.getImages().some((img) => img.id === id)).toBe(true)
|
|
142
|
+
expect(ctx.hasCellImage(addr)).toBe(false)
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
it('嵌入单元格:data 字节图迁字节;目标格已有嵌入图则覆盖;id 不存在无操作', () => {
|
|
146
|
+
const sheet = new Sheet()
|
|
147
|
+
const ctx = createSheetContext(sheet)
|
|
148
|
+
const addr = { row: 1, col: 1 }
|
|
149
|
+
const id = sheet.insertImage({
|
|
150
|
+
data: new Uint8Array([1, 2, 3]),
|
|
151
|
+
type: 'png',
|
|
152
|
+
anchor: { from: addr }
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
embedFloatImageToCell(ctx, id)
|
|
156
|
+
expect(ctx.getCellImage(addr)).toEqual({ data: new Uint8Array([1, 2, 3]), type: 'png' })
|
|
157
|
+
|
|
158
|
+
// 目标格已有嵌入图 → 覆盖(undo 一次仍回到转换前)
|
|
159
|
+
const other = insertFloatImage(sheet)
|
|
160
|
+
sheet.updateImage(other, { anchor: { from: { row: 5, col: 5 } } })
|
|
161
|
+
embedFloatImageToCell(ctx, other)
|
|
162
|
+
expect(ctx.hasCellImage({ row: 5, col: 5 })).toBe(true)
|
|
163
|
+
expect(ctx.getCellImage({ row: 5, col: 5 })).toEqual({
|
|
164
|
+
data: new Uint8Array([1, 2, 3]),
|
|
165
|
+
type: 'png'
|
|
166
|
+
})
|
|
167
|
+
expect(ctx.undo()).toBe(true)
|
|
168
|
+
expect(ctx.hasCellImage({ row: 5, col: 5 })).toBe(false)
|
|
169
|
+
expect(ctx.getImages().some((img) => img.id === other)).toBe(true)
|
|
170
|
+
|
|
171
|
+
// id 不存在(并发删除):无操作
|
|
172
|
+
embedFloatImageToCell(ctx, 'missing')
|
|
173
|
+
expect(ctx.getImages().some((img) => img.id === other)).toBe(true)
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('readonly:设置/取消/删除/嵌入单元格禁用、预览可用;句柄缺省:预览/设置禁用', () => {
|
|
113
177
|
const sheet = new Sheet()
|
|
114
178
|
const ctx = createSheetContext(sheet)
|
|
115
179
|
const id = insertFloatImage(sheet, { link: 'https://example.com' })
|
|
@@ -118,13 +182,15 @@ describe('buildFloatImageMenus(浮动图右键独立菜单)', () => {
|
|
|
118
182
|
expect(readonly.find((m) => m.label === '预览')?.disabled).toBeFalsy()
|
|
119
183
|
expect(readonly.find((m) => m.label === '设置超链接')?.disabled).toBe(true)
|
|
120
184
|
expect(readonly.find((m) => m.label === '取消超链接')?.disabled).toBe(true)
|
|
185
|
+
expect(readonly.find((m) => m.label === '嵌入单元格')?.disabled).toBe(true)
|
|
121
186
|
expect(readonly.find((m) => m.label === '删除图片')?.disabled).toBe(true)
|
|
122
187
|
|
|
123
188
|
const noHandles = buildFloatImageMenus(ctx, id)
|
|
124
189
|
expect(noHandles.find((m) => m.label === '预览')?.disabled).toBe(true)
|
|
125
190
|
expect(noHandles.find((m) => m.label === '设置超链接')?.disabled).toBe(true)
|
|
126
|
-
//
|
|
191
|
+
// 取消/嵌入/删除只依赖 ctx,句柄缺省仍可用
|
|
127
192
|
expect(noHandles.find((m) => m.label === '取消超链接')?.disabled).toBeFalsy()
|
|
193
|
+
expect(noHandles.find((m) => m.label === '嵌入单元格')?.disabled).toBeFalsy()
|
|
128
194
|
expect(noHandles.find((m) => m.label === '删除图片')?.disabled).toBeFalsy()
|
|
129
195
|
})
|
|
130
196
|
})
|
|
@@ -631,6 +631,78 @@ describe('USheet 公式栏:函数补全与引用选择', () => {
|
|
|
631
631
|
expect(fxInput(el).value).toBe('=A1+B1')
|
|
632
632
|
})
|
|
633
633
|
|
|
634
|
+
it('shift 扩展:替换上次插入的引用(extend 回交),连续扩展锚点不重置', async () => {
|
|
635
|
+
const sheet = new Workbook().activeSheet
|
|
636
|
+
const { el, exposed } = mountFormulaBar(sheet)
|
|
637
|
+
await nextTick()
|
|
638
|
+
sheet.selectCell({ row: 2, col: 0 })
|
|
639
|
+
await nextTick()
|
|
640
|
+
|
|
641
|
+
setFxText(el, '=SUM(')
|
|
642
|
+
await nextTick()
|
|
643
|
+
exposed.value!.handleRefSelect(parseRange('A1'))
|
|
644
|
+
await nextTick()
|
|
645
|
+
expect(fxInput(el).value).toBe('=SUM(A1')
|
|
646
|
+
// 插入后光标离开触发位(上下文断裂),拾取延续仍成立
|
|
647
|
+
expect(exposed.value!.hasRefInsert()).toBe(true)
|
|
648
|
+
|
|
649
|
+
// shift+点选扩展(引擎 extend 回交):A1 被替换为 A1:C3 而非追加
|
|
650
|
+
exposed.value!.handleRefSelect(parseRange('A1:C3'))
|
|
651
|
+
await nextTick()
|
|
652
|
+
expect(fxInput(el).value).toBe('=SUM(A1:C3')
|
|
653
|
+
expect(fxInput(el).selectionStart).toBe('=SUM(A1:C3'.length)
|
|
654
|
+
|
|
655
|
+
// 连续扩展:引擎锚点恒为首次拾取锚点,A1:C3 整体替换为 A1:D5
|
|
656
|
+
exposed.value!.handleRefSelect(parseRange('A1:D5'))
|
|
657
|
+
await nextTick()
|
|
658
|
+
expect(fxInput(el).value).toBe('=SUM(A1:D5')
|
|
659
|
+
})
|
|
660
|
+
|
|
661
|
+
it('shift 扩展无上次插入(无 extend 上下文)时按普通插入处理', async () => {
|
|
662
|
+
const sheet = new Workbook().activeSheet
|
|
663
|
+
const { el, exposed } = mountFormulaBar(sheet)
|
|
664
|
+
await nextTick()
|
|
665
|
+
sheet.selectCell({ row: 2, col: 0 })
|
|
666
|
+
await nextTick()
|
|
667
|
+
|
|
668
|
+
setFxText(el, '=SUM(')
|
|
669
|
+
await nextTick()
|
|
670
|
+
exposed.value!.handleRefSelect(parseRange('B2'))
|
|
671
|
+
await nextTick()
|
|
672
|
+
expect(fxInput(el).value).toBe('=SUM(B2')
|
|
673
|
+
})
|
|
674
|
+
|
|
675
|
+
it('用户手动输入 / 提交后引用拾取延续终止:extend 不再替换', async () => {
|
|
676
|
+
const sheet = new Workbook().activeSheet
|
|
677
|
+
const { el, exposed } = mountFormulaBar(sheet)
|
|
678
|
+
await nextTick()
|
|
679
|
+
sheet.selectCell({ row: 2, col: 0 })
|
|
680
|
+
await nextTick()
|
|
681
|
+
|
|
682
|
+
setFxText(el, '=SUM(')
|
|
683
|
+
await nextTick()
|
|
684
|
+
exposed.value!.handleRefSelect(parseRange('A1'))
|
|
685
|
+
await nextTick()
|
|
686
|
+
expect(exposed.value!.hasRefInsert()).toBe(true)
|
|
687
|
+
|
|
688
|
+
// 用户输入逗号(input 事件):延续状态清除,回到光标上下文判定
|
|
689
|
+
const input = fxInput(el)
|
|
690
|
+
input.value = '=SUM(A1,'
|
|
691
|
+
input.setSelectionRange(9, 9)
|
|
692
|
+
input.dispatchEvent(new Event('input', { bubbles: true }))
|
|
693
|
+
await nextTick()
|
|
694
|
+
expect(exposed.value!.hasRefInsert()).toBe(false)
|
|
695
|
+
exposed.value!.handleRefSelect(parseRange('B2'))
|
|
696
|
+
await nextTick()
|
|
697
|
+
// 光标在逗号后处于引用上下文 → 普通插入 B2,而非替换 A1
|
|
698
|
+
expect(fxInput(el).value).toBe('=SUM(A1,B2')
|
|
699
|
+
|
|
700
|
+
// Enter 提交:会话结束,延续状态随编辑态一并清除
|
|
701
|
+
input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }))
|
|
702
|
+
await nextTick()
|
|
703
|
+
expect(exposed.value!.hasRefInsert()).toBe(false)
|
|
704
|
+
})
|
|
705
|
+
|
|
634
706
|
it('多行文本:默认保持单行高度,鼠标进入展开,移出后收起', async () => {
|
|
635
707
|
const workbook = new Workbook()
|
|
636
708
|
const sheet = workbook.activeSheet
|
|
@@ -661,6 +733,137 @@ describe('USheet 公式栏:函数补全与引用选择', () => {
|
|
|
661
733
|
})
|
|
662
734
|
})
|
|
663
735
|
|
|
736
|
+
describe('USheet 公式栏:Point mode 拾取会话(Excel 语义)', () => {
|
|
737
|
+
it('拾取会话中普通点选画布:替换上次引用、编辑不中断、blur 不提交;Enter 提交替换后公式', async () => {
|
|
738
|
+
const workbook = new Workbook()
|
|
739
|
+
const sheet = workbook.activeSheet
|
|
740
|
+
const exposed: { value: SheetExposed | undefined } = { value: undefined }
|
|
741
|
+
const { el } = mount(() => ({ workbook, rows: 20, cols: 8 }), exposed)
|
|
742
|
+
await nextTick()
|
|
743
|
+
sheet.selectCell({ row: 2, col: 0 })
|
|
744
|
+
await nextTick()
|
|
745
|
+
|
|
746
|
+
const input = setFxText(el, '=SUM(')
|
|
747
|
+
await nextTick()
|
|
748
|
+
|
|
749
|
+
// 首拾取 A4(引擎选区事件管线:pointerdown 起手 → pointerup 收敛回交)
|
|
750
|
+
const gridEl = gridEventTarget(el)
|
|
751
|
+
const pick = (x: number, y: number) => {
|
|
752
|
+
gridEl.dispatchEvent(
|
|
753
|
+
new PointerEvent('pointerdown', { clientX: x, clientY: y, bubbles: true })
|
|
754
|
+
)
|
|
755
|
+
gridEl.dispatchEvent(new PointerEvent('pointerup', { clientX: x, clientY: y, bubbles: true }))
|
|
756
|
+
}
|
|
757
|
+
pick(51, 117)
|
|
758
|
+
await nextTick()
|
|
759
|
+
expect(input.value).toBe('=SUM(A4')
|
|
760
|
+
|
|
761
|
+
// 普通点选 C3(画布几何:行号列 46、列头 28、列宽 80、行高 28)→ 替换 A4 而非追加
|
|
762
|
+
pick(211, 89)
|
|
763
|
+
await nextTick()
|
|
764
|
+
expect(input.value).toBe('=SUM(C3')
|
|
765
|
+
|
|
766
|
+
// 编辑未中断:模型未写入(真实 blur 已在第二次 pointerdown 时被挂起并消费)
|
|
767
|
+
expect(sheet.getCellData({ row: 2, col: 0 })).toBeUndefined()
|
|
768
|
+
expect(input.value).toBe('=SUM(C3')
|
|
769
|
+
|
|
770
|
+
// Enter 提交替换后的公式
|
|
771
|
+
input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }))
|
|
772
|
+
await nextTick()
|
|
773
|
+
expect(sheet.getCellData({ row: 2, col: 0 })).toMatchObject({ f: 'SUM(C3' })
|
|
774
|
+
})
|
|
775
|
+
|
|
776
|
+
it('拾取会话中点选被编辑格自身:忽略不替换(防循环引用),会话延续', async () => {
|
|
777
|
+
const sheet = new Workbook().activeSheet
|
|
778
|
+
const { el, exposed } = mountFormulaBar(sheet)
|
|
779
|
+
await nextTick()
|
|
780
|
+
sheet.selectCell({ row: 2, col: 0 })
|
|
781
|
+
await nextTick()
|
|
782
|
+
|
|
783
|
+
setFxText(el, '=SUM(')
|
|
784
|
+
await nextTick()
|
|
785
|
+
exposed.value!.handleRefSelect(parseRange('A1'))
|
|
786
|
+
await nextTick()
|
|
787
|
+
expect(fxInput(el).value).toBe('=SUM(A1')
|
|
788
|
+
|
|
789
|
+
// 被编辑格 = A3:拾取视作无操作
|
|
790
|
+
exposed.value!.handleRefSelect(parseRange('A3'))
|
|
791
|
+
await nextTick()
|
|
792
|
+
expect(fxInput(el).value).toBe('=SUM(A1')
|
|
793
|
+
expect(exposed.value!.hasRefInsert()).toBe(true)
|
|
794
|
+
})
|
|
795
|
+
})
|
|
796
|
+
|
|
797
|
+
describe('USheet 公式栏:引用染色框(画布视觉反馈)', () => {
|
|
798
|
+
function mountFormulaBarWithHighlights(
|
|
799
|
+
sheet: Workbook['activeSheet'],
|
|
800
|
+
onHighlights: (items: { range: ReturnType<typeof parseRange>; color: string }[]) => void
|
|
801
|
+
) {
|
|
802
|
+
const el = document.createElement('div')
|
|
803
|
+
document.body.appendChild(el)
|
|
804
|
+
containers.push(el)
|
|
805
|
+
const exposed: { value: InstanceType<typeof UFormulaBar> | undefined } = { value: undefined }
|
|
806
|
+
const app = createApp({
|
|
807
|
+
render: () =>
|
|
808
|
+
h(UFormulaBar, {
|
|
809
|
+
sheet,
|
|
810
|
+
context: createSheetContext(sheet),
|
|
811
|
+
setRefHighlights: onHighlights,
|
|
812
|
+
ref: (value: unknown) => {
|
|
813
|
+
exposed.value = value as InstanceType<typeof UFormulaBar> | undefined
|
|
814
|
+
}
|
|
815
|
+
})
|
|
816
|
+
})
|
|
817
|
+
app.mount(el)
|
|
818
|
+
apps.push(app)
|
|
819
|
+
return { el, exposed }
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
it('公式编辑中按文本扫描逐引用上框(出现顺序循环取色);手输/拾取替换/会话结束同步', async () => {
|
|
823
|
+
const sheet = new Workbook().activeSheet
|
|
824
|
+
const highlights: { range: ReturnType<typeof parseRange>; color: string }[][] = []
|
|
825
|
+
const { el, exposed } = mountFormulaBarWithHighlights(sheet, (items) => highlights.push(items))
|
|
826
|
+
await nextTick()
|
|
827
|
+
sheet.selectCell({ row: 5, col: 5 })
|
|
828
|
+
await nextTick()
|
|
829
|
+
|
|
830
|
+
// 手输多引用:蓝 / 绿按出现顺序(B2:C4 → (1,1)-(3,2))
|
|
831
|
+
setFxText(el, '=SUM(A1,B2:C4)')
|
|
832
|
+
await nextTick()
|
|
833
|
+
expect(highlights.at(-1)).toEqual([
|
|
834
|
+
{ range: { start: { row: 0, col: 0 }, end: { row: 0, col: 0 } }, color: '#2563eb' },
|
|
835
|
+
{ range: { start: { row: 1, col: 1 }, end: { row: 3, col: 2 } }, color: '#16a34a' }
|
|
836
|
+
])
|
|
837
|
+
|
|
838
|
+
// 非公式文本:清除
|
|
839
|
+
setFxText(el, 'plain')
|
|
840
|
+
await nextTick()
|
|
841
|
+
expect(highlights.at(-1)).toEqual([])
|
|
842
|
+
|
|
843
|
+
// 拾取会话:插入 B2:C4 → 染色;普通点选 D1 替换 → 染色跟随(仍单引用、蓝色)
|
|
844
|
+
setFxText(el, '=SUM(')
|
|
845
|
+
await nextTick()
|
|
846
|
+
expect(highlights.at(-1)).toEqual([])
|
|
847
|
+
exposed.value!.handleRefSelect(parseRange('B2:C4'))
|
|
848
|
+
await nextTick()
|
|
849
|
+
expect(highlights.at(-1)).toEqual([
|
|
850
|
+
{ range: { start: { row: 1, col: 1 }, end: { row: 3, col: 2 } }, color: '#2563eb' }
|
|
851
|
+
])
|
|
852
|
+
exposed.value!.handleRefSelect(parseRange('D1'))
|
|
853
|
+
await nextTick()
|
|
854
|
+
expect(fxInput(el).value).toBe('=SUM(D1')
|
|
855
|
+
expect(highlights.at(-1)).toEqual([
|
|
856
|
+
{ range: { start: { row: 0, col: 3 }, end: { row: 0, col: 3 } }, color: '#2563eb' }
|
|
857
|
+
])
|
|
858
|
+
|
|
859
|
+
// 提交(编辑态结束):清除
|
|
860
|
+
expect(highlights.at(-1)).toHaveLength(1)
|
|
861
|
+
fxInput(el).dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }))
|
|
862
|
+
await nextTick()
|
|
863
|
+
expect(highlights.at(-1)).toEqual([])
|
|
864
|
+
})
|
|
865
|
+
})
|
|
866
|
+
|
|
664
867
|
// ─── 目标格选区高亮保持(弹框打开与参数点选期间)──────────────
|
|
665
868
|
|
|
666
869
|
type GridTable = ReturnType<SheetGrid['getTable']>
|
|
@@ -4,8 +4,10 @@ import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
|
4
4
|
import { createSheetContext } from '../../../tools/context'
|
|
5
5
|
import {
|
|
6
6
|
INSERT_IMAGE_ACCEPT,
|
|
7
|
+
insertCellImageFromFile,
|
|
7
8
|
insertImageFromFile,
|
|
8
9
|
insertImageFromUrl,
|
|
10
|
+
pickAndEmbedCellImage,
|
|
9
11
|
pickAndInsertImage,
|
|
10
12
|
resolveImageType
|
|
11
13
|
} from '../insert-image'
|
|
@@ -138,6 +140,64 @@ describe('insertImageFromUrl', () => {
|
|
|
138
140
|
})
|
|
139
141
|
})
|
|
140
142
|
|
|
143
|
+
describe('insertCellImageFromFile / pickAndEmbedCellImage', () => {
|
|
144
|
+
it('支持格式 → 嵌入活动格(insertCellImage 字节载荷)', async () => {
|
|
145
|
+
const sheet = new Sheet()
|
|
146
|
+
const ctx = createSheetContext(sheet)
|
|
147
|
+
ctx.selectCell({ row: 3, col: 4 })
|
|
148
|
+
const bytes = [9, 8, 7]
|
|
149
|
+
await insertCellImageFromFile(ctx, makeFile('logo.png', 'image/png', bytes))
|
|
150
|
+
expect(ctx.hasCellImage({ row: 3, col: 4 })).toBe(true)
|
|
151
|
+
const payload = ctx.getCellImage({ row: 3, col: 4 })!
|
|
152
|
+
expect(payload.type).toBe('png')
|
|
153
|
+
expect([...payload.data!]).toEqual(bytes)
|
|
154
|
+
expect(mocks.message.error).not.toHaveBeenCalled()
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('格内已有嵌入图 → 整体覆盖替换', async () => {
|
|
158
|
+
const sheet = new Sheet()
|
|
159
|
+
const ctx = createSheetContext(sheet)
|
|
160
|
+
const addr = { row: 1, col: 1 }
|
|
161
|
+
ctx.insertCellImage(addr, { src: 'https://example.com/old.png' })
|
|
162
|
+
ctx.selectCell(addr)
|
|
163
|
+
await insertCellImageFromFile(ctx, makeFile('new.gif', 'image/gif', [1, 2]))
|
|
164
|
+
const payload = ctx.getCellImage(addr)!
|
|
165
|
+
expect(payload.src).toBeUndefined()
|
|
166
|
+
expect(payload.type).toBe('gif')
|
|
167
|
+
expect([...payload.data!]).toEqual([1, 2])
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('不支持格式 → message.error,不写入', async () => {
|
|
171
|
+
const sheet = new Sheet()
|
|
172
|
+
const ctx = createSheetContext(sheet)
|
|
173
|
+
ctx.selectCell({ row: 0, col: 0 })
|
|
174
|
+
await insertCellImageFromFile(ctx, makeFile('x.bmp', 'image/bmp'))
|
|
175
|
+
expect(ctx.hasCellImage({ row: 0, col: 0 })).toBe(false)
|
|
176
|
+
expect(mocks.message.error).toHaveBeenCalledWith(expect.stringContaining('不支持的图片格式'))
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('无活动格 → message.error,不写入', async () => {
|
|
180
|
+
const sheet = new Sheet()
|
|
181
|
+
const ctx = createSheetContext(sheet)
|
|
182
|
+
sheet.selection.clear()
|
|
183
|
+
await insertCellImageFromFile(ctx, makeFile('a.png', 'image/png'))
|
|
184
|
+
expect(mocks.message.error).toHaveBeenCalledWith('请先选择单元格')
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it('pickAndEmbedCellImage:创建隐藏 file input(同一 accept)并 click', () => {
|
|
188
|
+
const sheet = new Sheet()
|
|
189
|
+
const ctx = createSheetContext(sheet)
|
|
190
|
+
const click = vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(() => {})
|
|
191
|
+
pickAndEmbedCellImage(ctx)
|
|
192
|
+
const input = document.body.querySelector<HTMLInputElement>('input[type="file"]')
|
|
193
|
+
expect(input).toBeTruthy()
|
|
194
|
+
expect(input!.accept).toBe(INSERT_IMAGE_ACCEPT)
|
|
195
|
+
expect(input!.hidden).toBe(true)
|
|
196
|
+
expect(click).toHaveBeenCalled()
|
|
197
|
+
click.mockRestore()
|
|
198
|
+
})
|
|
199
|
+
})
|
|
200
|
+
|
|
141
201
|
describe('pickAndInsertImage', () => {
|
|
142
202
|
it('创建隐藏 file input(同一 accept)并 click', () => {
|
|
143
203
|
const sheet = new Sheet()
|
|
@@ -1288,6 +1288,8 @@ describe('USheet 引擎链接 / 图片事件透传', () => {
|
|
|
1288
1288
|
const leave: unknown[] = []
|
|
1289
1289
|
const click: unknown[] = []
|
|
1290
1290
|
const imageClick: unknown[] = []
|
|
1291
|
+
// 链接打开已由组件接管(直开 / 确认后开),stub 掉 window.open 隔离浏览器行为
|
|
1292
|
+
const openSpy = vi.spyOn(window, 'open').mockImplementation(() => null)
|
|
1291
1293
|
mount(
|
|
1292
1294
|
() => ({
|
|
1293
1295
|
workbook,
|
|
@@ -1329,6 +1331,8 @@ describe('USheet 引擎链接 / 图片事件透传', () => {
|
|
|
1329
1331
|
}
|
|
1330
1332
|
for (const listener of table.cellLinkClickListeners) listener(clickEvent)
|
|
1331
1333
|
expect(click).toEqual([clickEvent])
|
|
1334
|
+
// 单元格链接:组件接管直开(保持引擎原默认行为)
|
|
1335
|
+
expect(openSpy).toHaveBeenCalledWith('https://example.com', '_blank', 'noopener,noreferrer')
|
|
1332
1336
|
|
|
1333
1337
|
// 图片链接点击:kind 区分嵌入图(cell)与浮动图(float)
|
|
1334
1338
|
const cellImageLink = {
|
|
@@ -1349,6 +1353,58 @@ describe('USheet 引擎链接 / 图片事件透传', () => {
|
|
|
1349
1353
|
listener(floatImageLink)
|
|
1350
1354
|
}
|
|
1351
1355
|
expect(imageClick).toEqual([cellImageLink, floatImageLink])
|
|
1356
|
+
openSpy.mockRestore()
|
|
1357
|
+
})
|
|
1358
|
+
|
|
1359
|
+
it('图片链接单击不直接打开:弹确认框,确认后 window.open、取消不打开', async () => {
|
|
1360
|
+
const workbook = createWorkbook()
|
|
1361
|
+
const exposed: { value: SheetExposed | undefined } = { value: undefined }
|
|
1362
|
+
mount(() => ({ workbook, rows: 10, cols: 6 }), exposed)
|
|
1363
|
+
await nextTick()
|
|
1364
|
+
|
|
1365
|
+
const openSpy = vi.spyOn(window, 'open').mockImplementation(() => null)
|
|
1366
|
+
const table = exposed.value!.getGrid()!.getTable()
|
|
1367
|
+
const event = {
|
|
1368
|
+
kind: 'float' as const,
|
|
1369
|
+
id: 'img-1',
|
|
1370
|
+
url: 'https://example.com/float.png',
|
|
1371
|
+
originalEvent: new MouseEvent('pointerup')
|
|
1372
|
+
}
|
|
1373
|
+
// happy-dom 无 transition:关闭动画不结束,旧确认框残留在 body,断言只认最后一份
|
|
1374
|
+
const latestConfirmBox = (): HTMLElement => {
|
|
1375
|
+
const boxes = document.body.querySelectorAll<HTMLElement>('.u-message-confirm')
|
|
1376
|
+
return boxes[boxes.length - 1]!
|
|
1377
|
+
}
|
|
1378
|
+
const confirmButtons = (): HTMLButtonElement[] => [
|
|
1379
|
+
...latestConfirmBox().querySelectorAll<HTMLButtonElement>('.u-message-confirm__footer button')
|
|
1380
|
+
]
|
|
1381
|
+
|
|
1382
|
+
// 点击 → 不直接打开,弹确认框
|
|
1383
|
+
for (const listener of table.imageLinkClickListeners) listener(event)
|
|
1384
|
+
await nextTick()
|
|
1385
|
+
expect(openSpy).not.toHaveBeenCalled()
|
|
1386
|
+
expect(latestConfirmBox().textContent).toContain('https://example.com/float.png')
|
|
1387
|
+
|
|
1388
|
+
// 取消 → 不打开
|
|
1389
|
+
confirmButtons()
|
|
1390
|
+
.find((btn) => btn.textContent?.includes('取消'))!
|
|
1391
|
+
.click()
|
|
1392
|
+
await nextTick()
|
|
1393
|
+
expect(openSpy).not.toHaveBeenCalled()
|
|
1394
|
+
|
|
1395
|
+
// 再点 → 确认「打开」→ window.open 新标签
|
|
1396
|
+
for (const listener of table.imageLinkClickListeners) listener(event)
|
|
1397
|
+
await nextTick()
|
|
1398
|
+
confirmButtons()
|
|
1399
|
+
.find((btn) => btn.textContent?.includes('打开'))!
|
|
1400
|
+
.click()
|
|
1401
|
+
await nextTick()
|
|
1402
|
+
expect(openSpy).toHaveBeenCalledWith(
|
|
1403
|
+
'https://example.com/float.png',
|
|
1404
|
+
'_blank',
|
|
1405
|
+
'noopener,noreferrer'
|
|
1406
|
+
)
|
|
1407
|
+
openSpy.mockRestore()
|
|
1352
1408
|
})
|
|
1353
1409
|
|
|
1354
1410
|
it('浮动图右键:引擎 floatObjects.onContextMenu → float-context-menu emit(层坐标 + 页面坐标)', async () => {
|
|
@@ -1539,7 +1595,7 @@ describe('USheet 图片 UI(浮动图右键独立菜单 / 嵌入图 body 菜单
|
|
|
1539
1595
|
return document.body.querySelector<HTMLElement>('.u-image-previewer')
|
|
1540
1596
|
}
|
|
1541
1597
|
|
|
1542
|
-
it('浮动图右键弹独立菜单:预览 / 设置超链接 / 删除图片;无链接无取消项;不弹 body 菜单', async () => {
|
|
1598
|
+
it('浮动图右键弹独立菜单:预览 / 设置超链接 / 嵌入单元格 / 删除图片;无链接无取消项;不弹 body 菜单', async () => {
|
|
1543
1599
|
const workbook = createWorkbook()
|
|
1544
1600
|
const exposed: { value: SheetExposed | undefined } = { value: undefined }
|
|
1545
1601
|
mount(() => ({ workbook, rows: 10, cols: 6 }), exposed)
|
|
@@ -1551,6 +1607,7 @@ describe('USheet 图片 UI(浮动图右键独立菜单 / 嵌入图 body 菜单
|
|
|
1551
1607
|
|
|
1552
1608
|
expect(menuItem('预览')!.classList.contains('is-disabled')).toBe(false)
|
|
1553
1609
|
expect(menuItem('设置超链接')).toBeTruthy()
|
|
1610
|
+
expect(menuItem('嵌入单元格')).toBeTruthy()
|
|
1554
1611
|
expect(menuItem('删除图片')).toBeTruthy()
|
|
1555
1612
|
expect(menuItem('取消超链接')).toBeUndefined()
|
|
1556
1613
|
// 独立菜单:最新弹层不含 body 菜单项(引擎保证右键不穿透到格)
|
|
@@ -1599,7 +1656,7 @@ describe('USheet 图片 UI(浮动图右键独立菜单 / 嵌入图 body 菜单
|
|
|
1599
1656
|
expect(ctx.getImages().some((img) => img.id === id)).toBe(true)
|
|
1600
1657
|
})
|
|
1601
1658
|
|
|
1602
|
-
it('嵌入图 body
|
|
1659
|
+
it('嵌入图 body 菜单:纯图片项(预览 / 图片超链接 / 取消超链接 / 转浮动 / 替换 / 移除),无普通格项;移除可 undo、link 覆盖写', async () => {
|
|
1603
1660
|
const workbook = createWorkbook()
|
|
1604
1661
|
const exposed: { value: SheetExposed | undefined } = { value: undefined }
|
|
1605
1662
|
mount(() => ({ workbook, rows: 10, cols: 6 }), exposed)
|
|
@@ -1609,13 +1666,21 @@ describe('USheet 图片 UI(浮动图右键独立菜单 / 嵌入图 body 菜单
|
|
|
1609
1666
|
ctx.insertCellImage(addr, { src: 'https://example.com/cell.png' })
|
|
1610
1667
|
ctx.selectCell(addr)
|
|
1611
1668
|
|
|
1612
|
-
// 无链接:预览 / 图片超链接 /
|
|
1669
|
+
// 无链接:预览 / 图片超链接 / 替换图片 / 移除图片,无取消项;
|
|
1670
|
+
// 不出合并 / 数据格式 / 超链接 / 插入图片等普通格项(右键语义是图片)
|
|
1613
1671
|
popBodyContextMenu(exposed, 0, 0)
|
|
1614
1672
|
await nextTick()
|
|
1615
1673
|
expect(menuItem('预览')).toBeTruthy()
|
|
1616
1674
|
expect(menuItem('图片超链接')).toBeTruthy()
|
|
1675
|
+
expect(menuItem('转为浮动图片')).toBeTruthy()
|
|
1676
|
+
expect(menuItem('替换图片')).toBeTruthy()
|
|
1617
1677
|
expect(menuItem('移除图片')).toBeTruthy()
|
|
1618
1678
|
expect(menuItem('取消超链接')).toBeUndefined()
|
|
1679
|
+
expect(menuItem('合并单元格')).toBeUndefined()
|
|
1680
|
+
expect(menuItem('设置数据格式')).toBeUndefined()
|
|
1681
|
+
expect(menuItem('插入超链接')).toBeUndefined()
|
|
1682
|
+
expect(menuItem('插入图片')).toBeUndefined()
|
|
1683
|
+
expect(menuItem('嵌入图片')).toBeUndefined()
|
|
1619
1684
|
|
|
1620
1685
|
// 图片超链接 → 弹窗确认 → 覆盖写 link(src 保留)
|
|
1621
1686
|
menuItem('图片超链接')!.click()
|