@veltra/desktop 1.10.0 → 1.12.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/ai-button/ai-button.js +117 -0
- package/dist/components/ai-button/ai-button.js.map +1 -0
- package/dist/components/ai-button/style.d.ts +1 -0
- package/dist/components/ai-button/style.js +3 -0
- package/dist/components/ai-button/style2.css +29 -0
- package/dist/components/code-editor/basic-setup.js +5 -5
- package/dist/components/code-editor/code-editor.js +2 -2
- package/dist/components/code-editor/langs/bash/index.js +2 -2
- package/dist/components/code-editor/langs/powershell/index.js +2 -2
- package/dist/components/code-editor/langs/spel/index.js +1 -1
- package/dist/components/code-editor/langs/spel/language.js +1 -1
- package/dist/components/code-editor/shell-extension.js +2 -2
- package/dist/components/file-viewer/previewers/image-previewer.js +28 -124
- package/dist/components/file-viewer/previewers/image-previewer.js.map +1 -1
- package/dist/components/file-viewer/previewers/pdf-worker-wrapper.js.map +1 -1
- package/dist/components/file-viewer/previewers/sheet-previewer.js +5 -5
- package/dist/components/file-viewer/previewers/sheet-previewer.js.map +1 -1
- package/dist/components/file-viewer/style2.css +42 -203
- package/dist/components/image-previewer/image-previewer.js +286 -0
- package/dist/components/image-previewer/image-previewer.js.map +1 -0
- package/dist/components/image-previewer/style.d.ts +1 -0
- package/dist/components/image-previewer/style.js +2 -0
- package/dist/components/image-previewer/style2.css +174 -0
- package/dist/components/index.d.ts +21 -19
- package/dist/components/index.js +41 -37
- package/dist/components/index.js.map +1 -1
- package/dist/index.d.ts +110 -106
- package/dist/index.js +108 -106
- package/dist/node_modules/.bun/@codemirror_autocomplete@6.20.3/node_modules/@codemirror/autocomplete/dist/index.js +3 -3
- package/dist/node_modules/.bun/@codemirror_autocomplete@6.20.3/node_modules/@codemirror/autocomplete/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/{@codemirror_commands@6.11.0 → @codemirror_commands@6.11.1}/node_modules/@codemirror/commands/dist/index.js +6 -6
- package/dist/node_modules/.bun/@codemirror_commands@6.11.1/node_modules/@codemirror/commands/dist/index.js.map +1 -0
- package/dist/node_modules/.bun/@codemirror_lang-css@6.3.1/node_modules/@codemirror/lang-css/dist/index.js +1 -1
- package/dist/node_modules/.bun/@codemirror_lang-css@6.3.1/node_modules/@codemirror/lang-css/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@codemirror_lang-html@6.4.12/node_modules/@codemirror/lang-html/dist/index.js +3 -3
- package/dist/node_modules/.bun/@codemirror_lang-html@6.4.12/node_modules/@codemirror/lang-html/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@codemirror_lang-java@6.0.2/node_modules/@codemirror/lang-java/dist/index.js +1 -1
- package/dist/node_modules/.bun/@codemirror_lang-java@6.0.2/node_modules/@codemirror/lang-java/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@codemirror_lang-javascript@6.2.5/node_modules/@codemirror/lang-javascript/dist/index.js +3 -3
- package/dist/node_modules/.bun/@codemirror_lang-javascript@6.2.5/node_modules/@codemirror/lang-javascript/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@codemirror_lang-json@6.0.2/node_modules/@codemirror/lang-json/dist/index.js +1 -1
- package/dist/node_modules/.bun/@codemirror_lang-json@6.0.2/node_modules/@codemirror/lang-json/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@codemirror_lang-markdown@6.5.2/node_modules/@codemirror/lang-markdown/dist/index.js +3 -3
- package/dist/node_modules/.bun/@codemirror_lang-markdown@6.5.2/node_modules/@codemirror/lang-markdown/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@codemirror_lang-sql@6.10.0/node_modules/@codemirror/lang-sql/dist/index.js +1 -1
- package/dist/node_modules/.bun/@codemirror_lang-sql@6.10.0/node_modules/@codemirror/lang-sql/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/{@codemirror_language@6.12.4 → @codemirror_language@6.13.1}/node_modules/@codemirror/language/dist/index.js +5 -168
- package/dist/node_modules/.bun/@codemirror_language@6.13.1/node_modules/@codemirror/language/dist/index.js.map +1 -0
- package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/powershell.js +1 -1
- package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/powershell.js.map +1 -1
- package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/shell.js +1 -1
- package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/shell.js.map +1 -1
- package/dist/node_modules/.bun/@codemirror_lint@6.9.7/node_modules/@codemirror/lint/dist/index.js +2 -2
- package/dist/node_modules/.bun/@codemirror_lint@6.9.7/node_modules/@codemirror/lint/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/{@codemirror_search@6.7.1 → @codemirror_search@6.7.2}/node_modules/@codemirror/search/dist/index.js +10 -3
- package/dist/node_modules/.bun/@codemirror_search@6.7.2/node_modules/@codemirror/search/dist/index.js.map +1 -0
- package/dist/node_modules/.bun/{@codemirror_state@6.7.1 → @codemirror_state@6.7.6}/node_modules/@codemirror/state/dist/index.js +50 -16
- package/dist/node_modules/.bun/@codemirror_state@6.7.6/node_modules/@codemirror/state/dist/index.js.map +1 -0
- package/dist/node_modules/.bun/@codemirror_streamparser@6.0.0/node_modules/@codemirror/streamparser/dist/index.js +169 -0
- package/dist/node_modules/.bun/@codemirror_streamparser@6.0.0/node_modules/@codemirror/streamparser/dist/index.js.map +1 -0
- package/dist/node_modules/.bun/@codemirror_theme-one-dark@6.1.3/node_modules/@codemirror/theme-one-dark/dist/index.js +2 -2
- package/dist/node_modules/.bun/@codemirror_theme-one-dark@6.1.3/node_modules/@codemirror/theme-one-dark/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/{@codemirror_view@6.43.9 → @codemirror_view@6.43.14}/node_modules/@codemirror/view/dist/index.js +159 -93
- package/dist/node_modules/.bun/@codemirror_view@6.43.14/node_modules/@codemirror/view/dist/index.js.map +1 -0
- package/dist/node_modules/.bun/@lezer_common@1.5.3/node_modules/@lezer/common/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@lezer_css@1.3.9/node_modules/@lezer/css/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@lezer_highlight@1.2.5/node_modules/@lezer/highlight/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@lezer_html@1.3.13/node_modules/@lezer/html/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@lezer_java@1.1.5/node_modules/@lezer/java/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@lezer_javascript@1.5.6/node_modules/@lezer/javascript/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@lezer_json@1.0.3/node_modules/@lezer/json/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@lezer_lr@1.4.11/node_modules/@lezer/lr/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@lezer_markdown@1.8.0/node_modules/@lezer/markdown/dist/index.js.map +1 -1
- package/dist/node_modules/.bun/@marijn_find-cluster-break@1.0.4/node_modules/@marijn/find-cluster-break/src/index.js.map +1 -1
- package/dist/node_modules/.bun/crelt@1.0.7/node_modules/crelt/index.js.map +1 -1
- package/dist/node_modules/.bun/style-mod@4.1.4/node_modules/style-mod/src/style-mod.js.map +1 -1
- package/dist/node_modules/.bun/w3c-keyname@2.2.8/node_modules/w3c-keyname/index.js.map +1 -1
- package/dist/types/ai-button.d.ts +42 -0
- package/dist/types/image-previewer.d.ts +48 -0
- package/dist/types/index.d.ts +3 -1
- package/package.json +16 -16
- package/src/components/ai-button/__test__/ai-button.test.ts +375 -0
- package/src/components/ai-button/ai-button.vue +87 -0
- package/src/components/ai-button/index.ts +1 -0
- package/src/components/ai-button/style.scss +19 -0
- package/src/components/ai-button/style.ts +3 -0
- package/src/components/file-viewer/__test__/file-viewer.test.ts +58 -0
- package/src/components/file-viewer/previewers/image-previewer.vue +25 -138
- package/src/components/file-viewer/previewers/sheet-previewer.vue +1 -1
- package/src/components/file-viewer/style.scss +54 -266
- package/src/components/image-previewer/__test__/image-previewer.test.ts +292 -0
- package/src/components/image-previewer/image-previewer.vue +314 -0
- package/src/components/image-previewer/index.ts +1 -0
- package/src/components/image-previewer/style.scss +188 -0
- package/src/components/image-previewer/style.ts +2 -0
- package/src/components/index.ts +4 -0
- package/src/types/ai-button.ts +44 -0
- package/src/types/image-previewer.ts +50 -0
- package/src/types/index.ts +3 -0
- package/dist/node_modules/.bun/@codemirror_commands@6.11.0/node_modules/@codemirror/commands/dist/index.js.map +0 -1
- package/dist/node_modules/.bun/@codemirror_language@6.12.4/node_modules/@codemirror/language/dist/index.js.map +0 -1
- package/dist/node_modules/.bun/@codemirror_search@6.7.1/node_modules/@codemirror/search/dist/index.js.map +0 -1
- package/dist/node_modules/.bun/@codemirror_state@6.7.1/node_modules/@codemirror/state/dist/index.js.map +0 -1
- package/dist/node_modules/.bun/@codemirror_view@6.43.9/node_modules/@codemirror/view/dist/index.js.map +0 -1
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
2
|
+
import { createApp, h, nextTick, ref } from 'vue'
|
|
3
|
+
|
|
4
|
+
import UImagePreviewer from '../image-previewer.vue'
|
|
5
|
+
|
|
6
|
+
async function flush() {
|
|
7
|
+
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
8
|
+
// transition 的 leave 移除挂在 rAF 上,happy-dom 的 rAF 晚于 setTimeout(0) 一拍
|
|
9
|
+
await new Promise((resolve) => requestAnimationFrame(() => resolve(0)))
|
|
10
|
+
await nextTick()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface MountOptions {
|
|
14
|
+
props?: Record<string, unknown>
|
|
15
|
+
/** 是否绑定 v-model:open(默认绑定,初值 true) */
|
|
16
|
+
bindOpen?: boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function mountPreviewer({ props, bindOpen = true }: MountOptions = {}) {
|
|
20
|
+
const host = document.createElement('div')
|
|
21
|
+
document.body.appendChild(host)
|
|
22
|
+
|
|
23
|
+
const opened = ref(true)
|
|
24
|
+
const onClose = vi.fn()
|
|
25
|
+
const onZoomChange = vi.fn()
|
|
26
|
+
const onError = vi.fn()
|
|
27
|
+
const app = createApp({
|
|
28
|
+
render() {
|
|
29
|
+
return h(UImagePreviewer, {
|
|
30
|
+
src: 'https://example.com/a.png',
|
|
31
|
+
...props,
|
|
32
|
+
...(bindOpen
|
|
33
|
+
? { open: opened.value, 'onUpdate:open': (v: boolean) => (opened.value = v) }
|
|
34
|
+
: {}),
|
|
35
|
+
onClose,
|
|
36
|
+
onZoomChange,
|
|
37
|
+
onError
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
app.mount(host)
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
host,
|
|
45
|
+
opened,
|
|
46
|
+
onClose,
|
|
47
|
+
onZoomChange,
|
|
48
|
+
onError,
|
|
49
|
+
unmount() {
|
|
50
|
+
app.unmount()
|
|
51
|
+
host.remove()
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function root() {
|
|
57
|
+
return document.body.querySelector<HTMLElement>('.u-image-previewer')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function stage() {
|
|
61
|
+
return document.body.querySelector<HTMLElement>('.u-image-previewer__stage')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function stageScale(): number {
|
|
65
|
+
const m = stage()!.style.transform.match(/scale\(([\d.]+)\)/)
|
|
66
|
+
if (!m) throw new Error(`无法解析 transform: ${stage()!.style.transform}`)
|
|
67
|
+
return Number(m[1])
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** happy-dom 的 WheelEvent 不携带 clientX / clientY,用 MouseEvent 模拟并补按键与 deltaY */
|
|
71
|
+
function wheel(target: Element, deltaY: number) {
|
|
72
|
+
const e = new MouseEvent('wheel', { clientX: 0, clientY: 0, ctrlKey: true, bubbles: true })
|
|
73
|
+
Object.defineProperty(e, 'deltaY', { value: deltaY })
|
|
74
|
+
target.dispatchEvent(e)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
afterEach(() => {
|
|
78
|
+
vi.restoreAllMocks()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
describe('UImagePreviewer 挂载与关闭', () => {
|
|
82
|
+
it('lightbox 默认 Teleport 到 body,渲染图片与关闭 / 缩放工具', async () => {
|
|
83
|
+
const { unmount } = mountPreviewer()
|
|
84
|
+
await flush()
|
|
85
|
+
|
|
86
|
+
const img = root()!.querySelector('img')
|
|
87
|
+
expect(img).toBeTruthy()
|
|
88
|
+
expect(img!.getAttribute('src')).toBe('https://example.com/a.png')
|
|
89
|
+
expect(img!.getAttribute('alt')).toBe('图片预览')
|
|
90
|
+
expect(root()!.querySelector('[aria-label="关闭"]')).toBeTruthy()
|
|
91
|
+
expect(root()!.querySelector('[aria-label="放大"]')).toBeTruthy()
|
|
92
|
+
expect(root()!.querySelector('.u-image-previewer__zoom-value')!.textContent).toBe('100%')
|
|
93
|
+
unmount()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('open 为 false 时不渲染', async () => {
|
|
97
|
+
const opened = ref(false)
|
|
98
|
+
const { unmount } = mountPreviewer({
|
|
99
|
+
props: { open: opened.value, 'onUpdate:open': (v: boolean) => (opened.value = v) },
|
|
100
|
+
bindOpen: false
|
|
101
|
+
})
|
|
102
|
+
await flush()
|
|
103
|
+
|
|
104
|
+
expect(root()).toBeNull()
|
|
105
|
+
unmount()
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('点击关闭按钮关闭并回写 open、发出 close,body 滚动解锁', async () => {
|
|
109
|
+
const { opened, onClose, unmount } = mountPreviewer()
|
|
110
|
+
await flush()
|
|
111
|
+
expect(document.body.style.overflow).toBe('hidden')
|
|
112
|
+
|
|
113
|
+
root()!.querySelector<HTMLElement>('[aria-label="关闭"]')!.click()
|
|
114
|
+
await vi.waitFor(() => {
|
|
115
|
+
expect(root()).toBeNull()
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
expect(onClose).toHaveBeenCalledTimes(1)
|
|
119
|
+
expect(opened.value).toBe(false)
|
|
120
|
+
expect(document.body.style.overflow).toBe('')
|
|
121
|
+
unmount()
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('点击图片外区域关闭,点击图片本身不关闭', async () => {
|
|
125
|
+
const { onClose, unmount } = mountPreviewer()
|
|
126
|
+
await flush()
|
|
127
|
+
|
|
128
|
+
root()!
|
|
129
|
+
.querySelector('img')!
|
|
130
|
+
.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, button: 0 }))
|
|
131
|
+
await nextTick()
|
|
132
|
+
expect(onClose).not.toHaveBeenCalled()
|
|
133
|
+
|
|
134
|
+
stage()!.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, button: 0 }))
|
|
135
|
+
await nextTick()
|
|
136
|
+
expect(onClose).toHaveBeenCalledTimes(1)
|
|
137
|
+
unmount()
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
it('Esc 关闭;closeOnEsc 为 false 时不关闭', async () => {
|
|
141
|
+
const { unmount } = mountPreviewer()
|
|
142
|
+
await flush()
|
|
143
|
+
|
|
144
|
+
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
145
|
+
await vi.waitFor(() => {
|
|
146
|
+
expect(root()).toBeNull()
|
|
147
|
+
})
|
|
148
|
+
unmount()
|
|
149
|
+
|
|
150
|
+
const keep = mountPreviewer({ props: { closeOnEsc: false } })
|
|
151
|
+
await flush()
|
|
152
|
+
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
153
|
+
await flush()
|
|
154
|
+
expect(root()).toBeTruthy()
|
|
155
|
+
keep.unmount()
|
|
156
|
+
})
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
describe('UImagePreviewer 缩放与平移', () => {
|
|
160
|
+
it('双击放大到 2,再次双击回到 1,并发出 zoom-change', async () => {
|
|
161
|
+
const { onZoomChange, unmount } = mountPreviewer()
|
|
162
|
+
await flush()
|
|
163
|
+
|
|
164
|
+
stage()!.dispatchEvent(new MouseEvent('dblclick', { bubbles: true }))
|
|
165
|
+
await nextTick()
|
|
166
|
+
expect(stageScale()).toBe(2)
|
|
167
|
+
expect(onZoomChange).toHaveBeenLastCalledWith(2)
|
|
168
|
+
expect(root()!.querySelector('.u-image-previewer__zoom-value')!.textContent).toBe('200%')
|
|
169
|
+
|
|
170
|
+
stage()!.dispatchEvent(new MouseEvent('dblclick', { bubbles: true }))
|
|
171
|
+
await nextTick()
|
|
172
|
+
expect(stageScale()).toBe(1)
|
|
173
|
+
expect(onZoomChange).toHaveBeenLastCalledWith(1)
|
|
174
|
+
unmount()
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
it('工具按钮按 10% 步进缩放,触达上下限后禁用', async () => {
|
|
178
|
+
const { onZoomChange, unmount } = mountPreviewer()
|
|
179
|
+
await flush()
|
|
180
|
+
|
|
181
|
+
root()!.querySelector<HTMLElement>('[aria-label="放大"]')!.click()
|
|
182
|
+
await nextTick()
|
|
183
|
+
expect(stageScale()).toBeCloseTo(1.1, 10)
|
|
184
|
+
expect(onZoomChange).toHaveBeenLastCalledWith(1.1)
|
|
185
|
+
|
|
186
|
+
root()!.querySelector<HTMLElement>('[aria-label="缩小"]')!.click()
|
|
187
|
+
await nextTick()
|
|
188
|
+
expect(stageScale()).toBe(1)
|
|
189
|
+
|
|
190
|
+
const zoomIn = root()!.querySelector<HTMLButtonElement>('[aria-label="放大"]')!
|
|
191
|
+
const zoomOut = root()!.querySelector<HTMLButtonElement>('[aria-label="缩小"]')!
|
|
192
|
+
// 100% + 20 步到 3.0 上限:放大按钮禁用;缩小到 0.5 下限:缩小按钮禁用
|
|
193
|
+
for (let i = 0; i < 20; i++) zoomIn.click()
|
|
194
|
+
await nextTick()
|
|
195
|
+
expect(stageScale()).toBe(3)
|
|
196
|
+
expect(zoomIn.disabled).toBe(true)
|
|
197
|
+
|
|
198
|
+
for (let i = 0; i < 25; i++) zoomOut.click()
|
|
199
|
+
await nextTick()
|
|
200
|
+
expect(stageScale()).toBe(0.5)
|
|
201
|
+
expect(zoomOut.disabled).toBe(true)
|
|
202
|
+
unmount()
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
it('ctrl + 滚轮缩放,普通滚轮不缩放', async () => {
|
|
206
|
+
const { unmount } = mountPreviewer()
|
|
207
|
+
await flush()
|
|
208
|
+
|
|
209
|
+
wheel(stage()!, -100)
|
|
210
|
+
await nextTick()
|
|
211
|
+
expect(stageScale()).toBeCloseTo(1.1, 10)
|
|
212
|
+
|
|
213
|
+
const plain = new MouseEvent('wheel', { clientX: 0, clientY: 0, bubbles: true })
|
|
214
|
+
Object.defineProperty(plain, 'deltaY', { value: -100 })
|
|
215
|
+
stage()!.dispatchEvent(plain)
|
|
216
|
+
await nextTick()
|
|
217
|
+
expect(stageScale()).toBeCloseTo(1.1, 10)
|
|
218
|
+
unmount()
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
it('放大后拖拽平移,scale 为 1 时按下不平移', async () => {
|
|
222
|
+
const { unmount } = mountPreviewer()
|
|
223
|
+
await flush()
|
|
224
|
+
|
|
225
|
+
const el = stage()!
|
|
226
|
+
el.dispatchEvent(new PointerEvent('pointerdown', { button: 0, clientX: 100, clientY: 100 }))
|
|
227
|
+
el.dispatchEvent(new PointerEvent('pointermove', { clientX: 150, clientY: 130 }))
|
|
228
|
+
el.dispatchEvent(new PointerEvent('pointerup', { clientX: 150, clientY: 130 }))
|
|
229
|
+
await nextTick()
|
|
230
|
+
expect(el.style.transform).not.toContain('translate3d(50px')
|
|
231
|
+
|
|
232
|
+
// 双击放大到 2 后同一拖拽产生位移
|
|
233
|
+
el.dispatchEvent(new MouseEvent('dblclick', { bubbles: true }))
|
|
234
|
+
await nextTick()
|
|
235
|
+
el.dispatchEvent(new PointerEvent('pointerdown', { button: 0, clientX: 100, clientY: 100 }))
|
|
236
|
+
el.dispatchEvent(new PointerEvent('pointermove', { clientX: 150, clientY: 130 }))
|
|
237
|
+
el.dispatchEvent(new PointerEvent('pointerup', { clientX: 150, clientY: 130 }))
|
|
238
|
+
await nextTick()
|
|
239
|
+
expect(el.style.transform).toContain('translate3d(50px, 30px, 0) scale(2)')
|
|
240
|
+
// 抬手后回到可平移光标态
|
|
241
|
+
expect(el.classList.contains('is-dragging')).toBe(false)
|
|
242
|
+
unmount()
|
|
243
|
+
})
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
describe('UImagePreviewer 加载语义', () => {
|
|
247
|
+
it('二进制源经 objectURL 渲染,切换与卸载时回收', async () => {
|
|
248
|
+
const createObjectURL = vi.fn(() => 'blob:mock-1')
|
|
249
|
+
const revokeObjectURL = vi.fn()
|
|
250
|
+
URL.createObjectURL = createObjectURL as typeof URL.createObjectURL
|
|
251
|
+
URL.revokeObjectURL = revokeObjectURL as typeof URL.revokeObjectURL
|
|
252
|
+
|
|
253
|
+
const { unmount } = mountPreviewer({ props: { src: new Blob(['a']) } })
|
|
254
|
+
await flush()
|
|
255
|
+
|
|
256
|
+
expect(createObjectURL).toHaveBeenCalledTimes(1)
|
|
257
|
+
expect(stage()!.querySelector('img')!.getAttribute('src')).toBe('blob:mock-1')
|
|
258
|
+
unmount()
|
|
259
|
+
expect(revokeObjectURL).toHaveBeenCalledWith('blob:mock-1')
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
it('加载失败进入失败态并发出 error', async () => {
|
|
263
|
+
const { onError, unmount } = mountPreviewer()
|
|
264
|
+
await flush()
|
|
265
|
+
|
|
266
|
+
stage()!
|
|
267
|
+
.querySelector('img')!
|
|
268
|
+
.dispatchEvent(new Event('error', { bubbles: true }))
|
|
269
|
+
await nextTick()
|
|
270
|
+
|
|
271
|
+
expect(stage()!.textContent).toContain('图片加载失败')
|
|
272
|
+
expect(onError).toHaveBeenCalledTimes(1)
|
|
273
|
+
unmount()
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
it('modal 为 false 时内嵌渲染:无遮罩与关闭按钮,宿主 class 落到预览面', async () => {
|
|
277
|
+
const { host, unmount } = mountPreviewer({ props: { modal: false, class: 'host-stage' } })
|
|
278
|
+
await flush()
|
|
279
|
+
|
|
280
|
+
// 未 Teleport:渲染在挂载点内,且无 modal 遮罩类 / 关闭 / 工具栏
|
|
281
|
+
expect(host.querySelector('.u-image-previewer__stage')).toBeTruthy()
|
|
282
|
+
expect(host.querySelector('.u-image-previewer__stage img')).toBeTruthy()
|
|
283
|
+
expect(host.querySelector('.u-image-previewer.is-modal')).toBeNull()
|
|
284
|
+
expect(host.querySelector('[aria-label="关闭"]')).toBeNull()
|
|
285
|
+
expect(host.querySelector('.u-image-previewer__toolbar')).toBeNull()
|
|
286
|
+
// 宿主 class 经 attrs 落到预览面(file-viewer 适配层依赖此行为挂 __image 覆盖类)
|
|
287
|
+
expect(host.querySelector('.u-image-previewer__stage')!.classList.contains('host-stage')).toBe(
|
|
288
|
+
true
|
|
289
|
+
)
|
|
290
|
+
unmount()
|
|
291
|
+
})
|
|
292
|
+
})
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<Teleport to="body" :disabled="!modal">
|
|
3
|
+
<transition :name="modal ? 'u-image-previewer-fade' : ''">
|
|
4
|
+
<div v-if="!modal || open" :class="[cls.b, bem.is('modal', modal)]">
|
|
5
|
+
<div
|
|
6
|
+
ref="stage"
|
|
7
|
+
:class="[cls.e('stage'), bem.is('pannable', scale > 1), bem.is('dragging', isDragging)]"
|
|
8
|
+
v-bind="attrs"
|
|
9
|
+
@mousedown.self="onBackdropMousedown"
|
|
10
|
+
@pointerdown="onPointerDown"
|
|
11
|
+
@pointermove="onPointerMove"
|
|
12
|
+
@pointerup="onPointerEnd"
|
|
13
|
+
@pointercancel="onPointerEnd"
|
|
14
|
+
@wheel="onWheel"
|
|
15
|
+
@dblclick="onDblclick"
|
|
16
|
+
>
|
|
17
|
+
<img
|
|
18
|
+
v-if="url"
|
|
19
|
+
:src="url"
|
|
20
|
+
:alt="alt ?? '图片预览'"
|
|
21
|
+
draggable="false"
|
|
22
|
+
@dragstart.prevent
|
|
23
|
+
@load="loading = false"
|
|
24
|
+
@error="onImgError"
|
|
25
|
+
/>
|
|
26
|
+
<div v-if="loading" :class="cls.e('loading')">加载中…</div>
|
|
27
|
+
<div v-else-if="failed" :class="cls.e('loading')">图片加载失败</div>
|
|
28
|
+
</div>
|
|
29
|
+
|
|
30
|
+
<template v-if="modal">
|
|
31
|
+
<button
|
|
32
|
+
:class="cls.e('close')"
|
|
33
|
+
type="button"
|
|
34
|
+
aria-label="关闭"
|
|
35
|
+
title="关闭"
|
|
36
|
+
@click="close"
|
|
37
|
+
>
|
|
38
|
+
<u-icon :size="18">
|
|
39
|
+
<Close />
|
|
40
|
+
</u-icon>
|
|
41
|
+
</button>
|
|
42
|
+
<div :class="cls.e('toolbar')">
|
|
43
|
+
<button
|
|
44
|
+
:class="cls.e('tool')"
|
|
45
|
+
type="button"
|
|
46
|
+
:disabled="scale <= ZOOM_MIN"
|
|
47
|
+
aria-label="缩小"
|
|
48
|
+
title="缩小"
|
|
49
|
+
@click="zoomOut"
|
|
50
|
+
>
|
|
51
|
+
<u-icon :size="16">
|
|
52
|
+
<ZoomOut />
|
|
53
|
+
</u-icon>
|
|
54
|
+
</button>
|
|
55
|
+
<span :class="cls.e('zoom-value')">{{ zoomPercent }}</span>
|
|
56
|
+
<button
|
|
57
|
+
:class="cls.e('tool')"
|
|
58
|
+
type="button"
|
|
59
|
+
:disabled="scale >= ZOOM_MAX"
|
|
60
|
+
aria-label="放大"
|
|
61
|
+
title="放大"
|
|
62
|
+
@click="zoomIn"
|
|
63
|
+
>
|
|
64
|
+
<u-icon :size="16">
|
|
65
|
+
<ZoomIn />
|
|
66
|
+
</u-icon>
|
|
67
|
+
</button>
|
|
68
|
+
<button
|
|
69
|
+
:class="cls.e('tool')"
|
|
70
|
+
type="button"
|
|
71
|
+
:disabled="scale === 1"
|
|
72
|
+
aria-label="重置视图"
|
|
73
|
+
title="重置视图"
|
|
74
|
+
@click="resetZoom"
|
|
75
|
+
>
|
|
76
|
+
<u-icon :size="16">
|
|
77
|
+
<Refresh />
|
|
78
|
+
</u-icon>
|
|
79
|
+
</button>
|
|
80
|
+
</div>
|
|
81
|
+
</template>
|
|
82
|
+
</div>
|
|
83
|
+
</transition>
|
|
84
|
+
</Teleport>
|
|
85
|
+
</template>
|
|
86
|
+
|
|
87
|
+
<script lang="ts" setup>
|
|
88
|
+
import { Close, Refresh, ZoomIn, ZoomOut } from '@veltra/icons/normal'
|
|
89
|
+
import { bem } from '@veltra/utils'
|
|
90
|
+
import { computed, onBeforeUnmount, ref, shallowRef, useAttrs, useTemplateRef, watch } from 'vue'
|
|
91
|
+
|
|
92
|
+
import type {
|
|
93
|
+
ImagePreviewerEmits,
|
|
94
|
+
ImagePreviewerExposed,
|
|
95
|
+
ImagePreviewerProps
|
|
96
|
+
} from '../../types/image-previewer'
|
|
97
|
+
import { ZOOM_MAX, ZOOM_MIN, ZOOM_STEP, clampZoom, toBlobUrl } from '../file-viewer/helper'
|
|
98
|
+
import { UIcon } from '../icon'
|
|
99
|
+
|
|
100
|
+
// 根为 Teleport,无 fallthrough 落点:attrs(含宿主 class)由内层预览面承接
|
|
101
|
+
defineOptions({ name: 'UImagePreviewer', inheritAttrs: false })
|
|
102
|
+
|
|
103
|
+
const {
|
|
104
|
+
src,
|
|
105
|
+
alt,
|
|
106
|
+
mime,
|
|
107
|
+
modal = true,
|
|
108
|
+
closeOnClickBackdrop = true,
|
|
109
|
+
closeOnEsc = true
|
|
110
|
+
} = defineProps<ImagePreviewerProps>()
|
|
111
|
+
|
|
112
|
+
const emit = defineEmits<ImagePreviewerEmits>()
|
|
113
|
+
|
|
114
|
+
const cls = bem('image-previewer')
|
|
115
|
+
const attrs = useAttrs()
|
|
116
|
+
|
|
117
|
+
const stage = useTemplateRef<HTMLDivElement>('stage')
|
|
118
|
+
|
|
119
|
+
const open = defineModel<boolean>('open', { default: true })
|
|
120
|
+
|
|
121
|
+
const url = shallowRef<string>('')
|
|
122
|
+
const loading = ref(false)
|
|
123
|
+
const failed = ref(false)
|
|
124
|
+
|
|
125
|
+
/** 缩放级别;缩放入口统一经 setScale 收敛到 [ZOOM_MIN, ZOOM_MAX] */
|
|
126
|
+
const scale = ref(1)
|
|
127
|
+
/** 拖拽起止各写一次,仅驱动 grabbing 光标 */
|
|
128
|
+
const isDragging = ref(false)
|
|
129
|
+
|
|
130
|
+
const zoomPercent = computed(() => `${Math.round(scale.value * 100)}%`)
|
|
131
|
+
|
|
132
|
+
// ---- pan 热状态:全部走普通变量,pointermove 不触发重渲染 ----
|
|
133
|
+
let panState:
|
|
134
|
+
| { pointerId: number; startX: number; startY: number; originX: number; originY: number }
|
|
135
|
+
| undefined
|
|
136
|
+
let offsetX = 0
|
|
137
|
+
let offsetY = 0
|
|
138
|
+
|
|
139
|
+
/** transform 唯一写入口:直接写预览面 style,绕开响应式 */
|
|
140
|
+
function applyTransform() {
|
|
141
|
+
const el = stage.value
|
|
142
|
+
if (!el) return
|
|
143
|
+
el.style.transform = `translate3d(${offsetX}px, ${offsetY}px, 0) scale(${scale.value})`
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function setScale(value: number) {
|
|
147
|
+
scale.value = clampZoom(value)
|
|
148
|
+
if (scale.value <= 1) {
|
|
149
|
+
// 未放大时不允许平移,offset 归零
|
|
150
|
+
offsetX = 0
|
|
151
|
+
offsetY = 0
|
|
152
|
+
}
|
|
153
|
+
applyTransform()
|
|
154
|
+
emit('zoom-change', scale.value)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function zoomIn() {
|
|
158
|
+
setScale(scale.value + ZOOM_STEP)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function zoomOut() {
|
|
162
|
+
setScale(scale.value - ZOOM_STEP)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function resetZoom() {
|
|
166
|
+
setScale(1)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
defineExpose<ImagePreviewerExposed>({ zoomIn, zoomOut, resetZoom })
|
|
170
|
+
|
|
171
|
+
function onPointerDown(e: PointerEvent) {
|
|
172
|
+
if (scale.value <= 1 || e.button !== 0) return
|
|
173
|
+
panState = {
|
|
174
|
+
pointerId: e.pointerId,
|
|
175
|
+
startX: e.clientX,
|
|
176
|
+
startY: e.clientY,
|
|
177
|
+
originX: offsetX,
|
|
178
|
+
originY: offsetY
|
|
179
|
+
}
|
|
180
|
+
isDragging.value = true
|
|
181
|
+
stage.value?.setPointerCapture(e.pointerId)
|
|
182
|
+
e.preventDefault()
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function onPointerMove(e: PointerEvent) {
|
|
186
|
+
if (!panState || panState.pointerId !== e.pointerId) return
|
|
187
|
+
offsetX = panState.originX + e.clientX - panState.startX
|
|
188
|
+
offsetY = panState.originY + e.clientY - panState.startY
|
|
189
|
+
applyTransform()
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function onPointerEnd(e: PointerEvent) {
|
|
193
|
+
if (!panState || panState.pointerId !== e.pointerId) return
|
|
194
|
+
const el = stage.value
|
|
195
|
+
if (el?.hasPointerCapture(e.pointerId)) {
|
|
196
|
+
el.releasePointerCapture(e.pointerId)
|
|
197
|
+
}
|
|
198
|
+
panState = undefined
|
|
199
|
+
isDragging.value = false
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function onWheel(e: WheelEvent) {
|
|
203
|
+
if (!(e.ctrlKey || e.metaKey)) return
|
|
204
|
+
e.preventDefault()
|
|
205
|
+
setScale(scale.value + (e.deltaY > 0 ? -ZOOM_STEP : ZOOM_STEP))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function onDblclick() {
|
|
209
|
+
if (scale.value === 1) {
|
|
210
|
+
setScale(2)
|
|
211
|
+
} else {
|
|
212
|
+
resetZoom()
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function onBackdropMousedown() {
|
|
217
|
+
if (!modal) return
|
|
218
|
+
if (closeOnClickBackdrop === false) return
|
|
219
|
+
// 放大后空白区域是平移热区,不作为关闭入口
|
|
220
|
+
if (scale.value > 1) return
|
|
221
|
+
close()
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function close() {
|
|
225
|
+
if (!modal) return
|
|
226
|
+
open.value = false
|
|
227
|
+
emit('close')
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function handleWindowKeydown(e: KeyboardEvent) {
|
|
231
|
+
if (e.key !== 'Escape') return
|
|
232
|
+
if (!modal || !open.value) return
|
|
233
|
+
if (closeOnEsc === false) return
|
|
234
|
+
e.stopPropagation()
|
|
235
|
+
close()
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
let revoke: (() => void) | undefined
|
|
239
|
+
|
|
240
|
+
function load() {
|
|
241
|
+
if (src === undefined || src === null) {
|
|
242
|
+
revoke?.()
|
|
243
|
+
revoke = undefined
|
|
244
|
+
url.value = ''
|
|
245
|
+
loading.value = false
|
|
246
|
+
failed.value = false
|
|
247
|
+
return
|
|
248
|
+
}
|
|
249
|
+
loading.value = true
|
|
250
|
+
failed.value = false
|
|
251
|
+
revoke?.()
|
|
252
|
+
const r = toBlobUrl(src, mime)
|
|
253
|
+
url.value = r.url
|
|
254
|
+
revoke = r.revoke
|
|
255
|
+
// 新图回到初始视图,并同步工具栏回显
|
|
256
|
+
setScale(1)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function onImgError(e: Event) {
|
|
260
|
+
loading.value = false
|
|
261
|
+
failed.value = true
|
|
262
|
+
emit('error', e)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
watch(
|
|
266
|
+
() => src,
|
|
267
|
+
() => load(),
|
|
268
|
+
{ immediate: true }
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
// lightbox 打开时:锁定 body 滚动 + 挂 Esc 监听
|
|
272
|
+
let previousBodyOverflow = ''
|
|
273
|
+
let isBodyLocked = false
|
|
274
|
+
|
|
275
|
+
function lockBody() {
|
|
276
|
+
if (isBodyLocked || typeof document === 'undefined') return
|
|
277
|
+
previousBodyOverflow = document.body.style.overflow
|
|
278
|
+
document.body.style.overflow = 'hidden'
|
|
279
|
+
isBodyLocked = true
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function unlockBody() {
|
|
283
|
+
if (!isBodyLocked || typeof document === 'undefined') return
|
|
284
|
+
document.body.style.overflow = previousBodyOverflow
|
|
285
|
+
isBodyLocked = false
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
watch(
|
|
289
|
+
[() => modal, open],
|
|
290
|
+
([modalOpen, openNow], _, onCleanup) => {
|
|
291
|
+
// 依赖重跑前与作用域销毁时统一解锁 + 摘除 keydown 监听
|
|
292
|
+
onCleanup(() => {
|
|
293
|
+
unlockBody()
|
|
294
|
+
if (typeof window !== 'undefined') {
|
|
295
|
+
window.removeEventListener('keydown', handleWindowKeydown, true)
|
|
296
|
+
}
|
|
297
|
+
})
|
|
298
|
+
if (modalOpen && openNow) {
|
|
299
|
+
lockBody()
|
|
300
|
+
if (typeof window !== 'undefined') {
|
|
301
|
+
window.addEventListener('keydown', handleWindowKeydown, true)
|
|
302
|
+
}
|
|
303
|
+
} else {
|
|
304
|
+
unlockBody()
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
{ immediate: true }
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
onBeforeUnmount(() => {
|
|
311
|
+
revoke?.()
|
|
312
|
+
url.value = ''
|
|
313
|
+
})
|
|
314
|
+
</script>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as UImagePreviewer } from './image-previewer.vue'
|