@veltra/sheet 4.0.0 → 4.1.1
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 +87 -0
- package/dist/components/sheet/insert-image.js +33 -4
- package/dist/components/sheet/popup-helpers.js +18 -2
- package/dist/components/sheet/popups/hyperlink-popup.js +127 -0
- package/dist/components/sheet/sheet-context-menu.js +122 -5
- package/dist/components/sheet/sheet.js +132 -15
- package/dist/components/sheet/style.d.ts +2 -0
- package/dist/components/sheet/style.js +2 -0
- package/dist/components/sheet/style2.css +62 -0
- package/dist/components/sheet/use-cell-link-tooltip.js +33 -0
- package/dist/components/sheet/use-sheet-grid.js +33 -1
- package/dist/components/sheet/use-sheet-state.js +1 -0
- package/dist/tools/context.d.ts +20 -2
- package/dist/tools/context.js +9 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/sheet.d.ts +51 -1
- package/package.json +2 -2
- package/src/components/sheet/__test__/float-image-menu.test.ts +196 -0
- package/src/components/sheet/__test__/hyperlink-popup.test.ts +176 -0
- package/src/components/sheet/__test__/insert-image.test.ts +60 -0
- package/src/components/sheet/__test__/sheet-component.test.ts +539 -16
- package/src/components/sheet/__test__/sheet-context-menu.test.ts +293 -4
- package/src/components/sheet/float-image-menu.ts +100 -0
- package/src/components/sheet/insert-image.ts +32 -3
- package/src/components/sheet/popup-helpers.ts +68 -2
- package/src/components/sheet/popups/hyperlink-popup.vue +112 -0
- package/src/components/sheet/sheet-context-menu.ts +125 -6
- package/src/components/sheet/sheet.vue +149 -3
- package/src/components/sheet/style.scss +66 -0
- package/src/components/sheet/style.ts +3 -1
- package/src/components/sheet/use-cell-link-tooltip.ts +42 -0
- package/src/components/sheet/use-sheet-grid.ts +62 -3
- package/src/components/sheet/use-sheet-state.ts +8 -2
- package/src/tools/__test__/context.test.ts +89 -0
- package/src/tools/context.ts +33 -2
- package/src/types/sheet.ts +56 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
2
|
+
import { createApp, h, type App } from 'vue'
|
|
3
|
+
|
|
4
|
+
import type { HyperlinkConfirmPayload } from '../popup-helpers'
|
|
5
|
+
import USheetHyperlinkPopup from '../popups/hyperlink-popup.vue'
|
|
6
|
+
|
|
7
|
+
const mocks = vi.hoisted(() => {
|
|
8
|
+
const message = Object.assign(vi.fn(), {
|
|
9
|
+
success: vi.fn(),
|
|
10
|
+
warn: vi.fn(),
|
|
11
|
+
info: vi.fn(),
|
|
12
|
+
error: vi.fn(),
|
|
13
|
+
default: vi.fn()
|
|
14
|
+
})
|
|
15
|
+
return { message }
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
vi.mock('@veltra/desktop', async () => {
|
|
19
|
+
const { defineComponent, h } = await import('vue')
|
|
20
|
+
return {
|
|
21
|
+
message: mocks.message,
|
|
22
|
+
UInput: defineComponent({
|
|
23
|
+
name: 'UInputStub',
|
|
24
|
+
props: ['modelValue', 'placeholder'],
|
|
25
|
+
emits: ['update:modelValue'],
|
|
26
|
+
setup(props, { emit }) {
|
|
27
|
+
return () =>
|
|
28
|
+
h('input', {
|
|
29
|
+
class: 'hyperlink-input-stub',
|
|
30
|
+
placeholder: props.placeholder as string,
|
|
31
|
+
value: props.modelValue as string,
|
|
32
|
+
onInput: (e: Event) => emit('update:modelValue', (e.target as HTMLInputElement).value)
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
}),
|
|
36
|
+
UButton: defineComponent({
|
|
37
|
+
name: 'UButtonStub',
|
|
38
|
+
props: ['disabled', 'type'],
|
|
39
|
+
emits: ['click'],
|
|
40
|
+
setup(props, { emit, slots }) {
|
|
41
|
+
return () =>
|
|
42
|
+
h(
|
|
43
|
+
'button',
|
|
44
|
+
{
|
|
45
|
+
class: 'hyperlink-btn-stub',
|
|
46
|
+
disabled: props.disabled as boolean,
|
|
47
|
+
onClick: () => emit('click')
|
|
48
|
+
},
|
|
49
|
+
slots.default?.()
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const apps: App[] = []
|
|
57
|
+
const containers: HTMLElement[] = []
|
|
58
|
+
|
|
59
|
+
interface MountResult {
|
|
60
|
+
confirms: HyperlinkConfirmPayload[]
|
|
61
|
+
closeCount: () => number
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function mountPopup(props: {
|
|
65
|
+
mode: 'cell' | 'image'
|
|
66
|
+
initial?: { url: string; text?: string }
|
|
67
|
+
}): MountResult {
|
|
68
|
+
let closed = 0
|
|
69
|
+
const confirms: HyperlinkConfirmPayload[] = []
|
|
70
|
+
const el = document.createElement('div')
|
|
71
|
+
document.body.appendChild(el)
|
|
72
|
+
containers.push(el)
|
|
73
|
+
const app = createApp({
|
|
74
|
+
setup() {
|
|
75
|
+
return () =>
|
|
76
|
+
h(USheetHyperlinkPopup, {
|
|
77
|
+
mode: props.mode,
|
|
78
|
+
initial: props.initial,
|
|
79
|
+
onConfirm: (payload: HyperlinkConfirmPayload) => confirms.push(payload),
|
|
80
|
+
onClose: () => {
|
|
81
|
+
closed++
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
app.mount(el)
|
|
87
|
+
apps.push(app)
|
|
88
|
+
return { confirms, closeCount: () => closed }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 弹窗根面板(Teleport 到 body) */
|
|
92
|
+
function dialogEl(): HTMLElement {
|
|
93
|
+
return document.body.querySelector<HTMLElement>('.u-sheet__link-dialog')!
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 按文案取弹窗内按钮(确定 / 取消) */
|
|
97
|
+
function dialogButton(text: string): HTMLButtonElement {
|
|
98
|
+
return [...dialogEl().querySelectorAll<HTMLButtonElement>('.hyperlink-btn-stub')].find((btn) =>
|
|
99
|
+
btn.textContent?.includes(text)
|
|
100
|
+
)!
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** 第 index 个输入框写入并同步 v-model */
|
|
104
|
+
async function setInput(index: number, value: string): Promise<void> {
|
|
105
|
+
const inputs = [...dialogEl().querySelectorAll<HTMLInputElement>('.hyperlink-input-stub')]
|
|
106
|
+
inputs[index]!.value = value
|
|
107
|
+
inputs[index]!.dispatchEvent(new Event('input', { bubbles: true }))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
afterEach(() => {
|
|
111
|
+
apps.splice(0).forEach((app) => app.unmount())
|
|
112
|
+
containers.splice(0).forEach((el) => el.remove())
|
|
113
|
+
vi.clearAllMocks()
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
describe('USheetHyperlinkPopup', () => {
|
|
117
|
+
it('空地址提交拦截:message.error 提示、不 confirm 也不关闭', async () => {
|
|
118
|
+
const result = mountPopup({ mode: 'cell' })
|
|
119
|
+
await Promise.resolve()
|
|
120
|
+
|
|
121
|
+
dialogButton('确定').click()
|
|
122
|
+
expect(mocks.message.error).toHaveBeenCalledWith('请输入链接地址')
|
|
123
|
+
expect(result.confirms).toEqual([])
|
|
124
|
+
expect(result.closeCount()).toBe(0)
|
|
125
|
+
|
|
126
|
+
// 填地址后提交放行
|
|
127
|
+
await setInput(0, 'https://example.com')
|
|
128
|
+
dialogButton('确定').click()
|
|
129
|
+
expect(mocks.message.error).toHaveBeenCalledTimes(1)
|
|
130
|
+
expect(result.confirms).toEqual([{ url: 'https://example.com' }])
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('cell 模式:显示文本留空提交不含 text(沿用单元格内容显示);填写则随载荷写入', async () => {
|
|
134
|
+
const result = mountPopup({
|
|
135
|
+
mode: 'cell',
|
|
136
|
+
initial: { url: 'https://old.example.com', text: '示例' }
|
|
137
|
+
})
|
|
138
|
+
await Promise.resolve()
|
|
139
|
+
|
|
140
|
+
// 编辑回填:改地址、清空显示文本
|
|
141
|
+
await setInput(0, 'https://new.example.com')
|
|
142
|
+
await setInput(1, '')
|
|
143
|
+
dialogButton('确定').click()
|
|
144
|
+
expect(result.confirms).toEqual([{ url: 'https://new.example.com' }])
|
|
145
|
+
expect('text' in result.confirms[0]!).toBe(false)
|
|
146
|
+
|
|
147
|
+
// 显示文本填写时随载荷提交
|
|
148
|
+
await setInput(0, 'https://example.com')
|
|
149
|
+
await setInput(1, ' 官网 ')
|
|
150
|
+
dialogButton('确定').click()
|
|
151
|
+
expect(result.confirms[1]).toEqual({ url: 'https://example.com', text: '官网' })
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('image 模式:仅地址一个字段(无显示文本行),标题为设置图片超链接', async () => {
|
|
155
|
+
mountPopup({ mode: 'image' })
|
|
156
|
+
await Promise.resolve()
|
|
157
|
+
|
|
158
|
+
expect(dialogEl().querySelectorAll('.hyperlink-input-stub')).toHaveLength(1)
|
|
159
|
+
expect(dialogEl().textContent).toContain('设置图片超链接')
|
|
160
|
+
expect(
|
|
161
|
+
[...dialogEl().querySelectorAll('.u-sheet__popup-label')].map((l) => l.textContent)
|
|
162
|
+
).toEqual(['地址'])
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('取消按钮与 Esc 均关闭(emit close,不 confirm)', async () => {
|
|
166
|
+
const result = mountPopup({ mode: 'cell' })
|
|
167
|
+
await Promise.resolve()
|
|
168
|
+
|
|
169
|
+
dialogButton('取消').click()
|
|
170
|
+
expect(result.closeCount()).toBe(1)
|
|
171
|
+
expect(result.confirms).toEqual([])
|
|
172
|
+
|
|
173
|
+
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
174
|
+
expect(result.closeCount()).toBe(2)
|
|
175
|
+
})
|
|
176
|
+
})
|
|
@@ -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()
|