[refactor] deduplicate download functionality in useImageMenuOptions (#5569)

Replace duplicated download implementation in saveImage function with
the existing downloadFile utility from downloadUtil. This removes 18
lines of duplicate code while maintaining identical functionality.

Changes:
- Import downloadFile from @/base/common/downloadUtil
- Replace manual anchor element creation with downloadFile call
- Maintain same URL preprocessing (removing preview parameter)
- Keep existing error handling

The downloadFile utility already includes comprehensive test coverage
and handles filename extraction, DOM manipulation, and cleanup.
This commit is contained in:
Christian Byrne
2025-09-14 18:26:41 -07:00
committed by GitHub
parent 96a663704f
commit fe09f88ea3

View File

@@ -1,5 +1,6 @@
import { useI18n } from 'vue-i18n'
import { downloadFile } from '@/base/common/downloadUtil'
import { useCommandStore } from '@/stores/commandStore'
import type { MenuOption } from './useMoreOptionsMenu'
@@ -69,22 +70,7 @@ export function useImageMenuOptions() {
try {
const url = new URL(img.src)
url.searchParams.delete('preview')
const a = document.createElement('a')
a.href = url.toString()
a.setAttribute(
'download',
new URLSearchParams(url.search).get('filename') ?? 'image.png'
)
a.style.display = 'none'
document.body.appendChild(a)
a.click()
requestAnimationFrame(() => {
if (document.body.contains(a)) {
document.body.removeChild(a)
}
})
downloadFile(url.toString())
} catch (error) {
console.error('Failed to save image:', error)
}