feat: add SVG export option to canvas export modal
Adds an SVG format choice below the Ultra quality option in the export modal. PNG quality options still drive raster export; the new SVG button exports a scalable vector via html-to-image toSvg. Closes #211 ha-relevant: yes
This commit is contained in:
@@ -2,7 +2,7 @@ import { useState } from 'react'
|
|||||||
import { Download, Loader2 } from 'lucide-react'
|
import { Download, Loader2 } from 'lucide-react'
|
||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog'
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { exportToPng, EXPORT_QUALITY_OPTIONS, type ExportQuality } from '@/utils/export'
|
import { exportToPng, exportToSvg, EXPORT_QUALITY_OPTIONS, type ExportQuality, type ExportFormat } from '@/utils/export'
|
||||||
|
|
||||||
interface ExportModalProps {
|
interface ExportModalProps {
|
||||||
open: boolean
|
open: boolean
|
||||||
@@ -12,6 +12,7 @@ interface ExportModalProps {
|
|||||||
|
|
||||||
export function ExportModal({ open, onClose, getElement }: ExportModalProps) {
|
export function ExportModal({ open, onClose, getElement }: ExportModalProps) {
|
||||||
const [quality, setQuality] = useState<ExportQuality>('high')
|
const [quality, setQuality] = useState<ExportQuality>('high')
|
||||||
|
const [format, setFormat] = useState<ExportFormat>('png')
|
||||||
const [exporting, setExporting] = useState(false)
|
const [exporting, setExporting] = useState(false)
|
||||||
|
|
||||||
const handleExport = async () => {
|
const handleExport = async () => {
|
||||||
@@ -19,7 +20,11 @@ export function ExportModal({ open, onClose, getElement }: ExportModalProps) {
|
|||||||
if (!el) return
|
if (!el) return
|
||||||
setExporting(true)
|
setExporting(true)
|
||||||
try {
|
try {
|
||||||
await exportToPng(el, quality)
|
if (format === 'svg') {
|
||||||
|
await exportToSvg(el)
|
||||||
|
} else {
|
||||||
|
await exportToPng(el, quality)
|
||||||
|
}
|
||||||
onClose()
|
onClose()
|
||||||
} finally {
|
} finally {
|
||||||
setExporting(false)
|
setExporting(false)
|
||||||
@@ -30,7 +35,7 @@ export function ExportModal({ open, onClose, getElement }: ExportModalProps) {
|
|||||||
<Dialog open={open} onOpenChange={(v) => !v && onClose()}>
|
<Dialog open={open} onOpenChange={(v) => !v && onClose()}>
|
||||||
<DialogContent className="bg-[#161b22] border-border max-w-sm">
|
<DialogContent className="bg-[#161b22] border-border max-w-sm">
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle className="text-foreground">Export as PNG</DialogTitle>
|
<DialogTitle className="text-foreground">Export Canvas</DialogTitle>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
|
|
||||||
<div className="space-y-2 py-2">
|
<div className="space-y-2 py-2">
|
||||||
@@ -38,10 +43,10 @@ export function ExportModal({ open, onClose, getElement }: ExportModalProps) {
|
|||||||
<button
|
<button
|
||||||
key={opt.value}
|
key={opt.value}
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => setQuality(opt.value)}
|
onClick={() => { setFormat('png'); setQuality(opt.value) }}
|
||||||
className={[
|
className={[
|
||||||
'w-full flex items-center justify-between px-3 py-2.5 rounded-md border text-sm transition-colors',
|
'w-full flex items-center justify-between px-3 py-2.5 rounded-md border text-sm transition-colors',
|
||||||
quality === opt.value
|
format === 'png' && quality === opt.value
|
||||||
? 'border-[#00d4ff] bg-[#00d4ff10] text-foreground'
|
? 'border-[#00d4ff] bg-[#00d4ff10] text-foreground'
|
||||||
: 'border-border bg-[#0d1117] text-muted-foreground hover:border-muted-foreground',
|
: 'border-border bg-[#0d1117] text-muted-foreground hover:border-muted-foreground',
|
||||||
].join(' ')}
|
].join(' ')}
|
||||||
@@ -50,6 +55,19 @@ export function ExportModal({ open, onClose, getElement }: ExportModalProps) {
|
|||||||
<span className="text-xs opacity-70">{opt.hint}</span>
|
<span className="text-xs opacity-70">{opt.hint}</span>
|
||||||
</button>
|
</button>
|
||||||
))}
|
))}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setFormat('svg')}
|
||||||
|
className={[
|
||||||
|
'w-full flex items-center justify-between px-3 py-2.5 rounded-md border text-sm transition-colors',
|
||||||
|
format === 'svg'
|
||||||
|
? 'border-[#00d4ff] bg-[#00d4ff10] text-foreground'
|
||||||
|
: 'border-border bg-[#0d1117] text-muted-foreground hover:border-muted-foreground',
|
||||||
|
].join(' ')}
|
||||||
|
>
|
||||||
|
<span className="font-medium">SVG</span>
|
||||||
|
<span className="text-xs opacity-70">vector — scalable, small file</span>
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<DialogFooter className="gap-2">
|
<DialogFooter className="gap-2">
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ import { render, screen, fireEvent, waitFor } from '@testing-library/react'
|
|||||||
import { ExportModal } from '../ExportModal'
|
import { ExportModal } from '../ExportModal'
|
||||||
|
|
||||||
const mockExportToPng = vi.fn()
|
const mockExportToPng = vi.fn()
|
||||||
|
const mockExportToSvg = vi.fn()
|
||||||
vi.mock('@/utils/export', () => ({
|
vi.mock('@/utils/export', () => ({
|
||||||
exportToPng: (...args: unknown[]) => mockExportToPng(...args),
|
exportToPng: (...args: unknown[]) => mockExportToPng(...args),
|
||||||
|
exportToSvg: (...args: unknown[]) => mockExportToSvg(...args),
|
||||||
EXPORT_QUALITY_OPTIONS: [
|
EXPORT_QUALITY_OPTIONS: [
|
||||||
{ value: 'standard', label: 'Standard', pixelRatio: 1, hint: '1× — small file' },
|
{ value: 'standard', label: 'Standard', pixelRatio: 1, hint: '1× — small file' },
|
||||||
{ value: 'high', label: 'High', pixelRatio: 2, hint: '2× — recommended' },
|
{ value: 'high', label: 'High', pixelRatio: 2, hint: '2× — recommended' },
|
||||||
@@ -20,6 +22,7 @@ describe('ExportModal', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
mockExportToPng.mockResolvedValue(undefined)
|
mockExportToPng.mockResolvedValue(undefined)
|
||||||
|
mockExportToSvg.mockResolvedValue(undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('renders all three quality options', () => {
|
it('renders all three quality options', () => {
|
||||||
@@ -49,6 +52,28 @@ describe('ExportModal', () => {
|
|||||||
await waitFor(() => expect(mockExportToPng).toHaveBeenCalledWith(el, 'standard'))
|
await waitFor(() => expect(mockExportToPng).toHaveBeenCalledWith(el, 'standard'))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('renders an SVG option under the quality options', () => {
|
||||||
|
render(<ExportModal open onClose={onClose} getElement={getElement} />)
|
||||||
|
expect(screen.getByText('SVG')).toBeInTheDocument()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('selects SVG and calls exportToSvg on Download click', async () => {
|
||||||
|
render(<ExportModal open onClose={onClose} getElement={getElement} />)
|
||||||
|
fireEvent.click(screen.getByText('SVG').closest('button')!)
|
||||||
|
expect(screen.getByText('SVG').closest('button')!.className).toContain('border-[#00d4ff]')
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: /download/i }))
|
||||||
|
await waitFor(() => expect(mockExportToSvg).toHaveBeenCalledWith(el))
|
||||||
|
expect(mockExportToPng).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('switches back to PNG when a quality option is clicked after SVG', () => {
|
||||||
|
render(<ExportModal open onClose={onClose} getElement={getElement} />)
|
||||||
|
fireEvent.click(screen.getByText('SVG').closest('button')!)
|
||||||
|
fireEvent.click(screen.getByText('Ultra').closest('button')!)
|
||||||
|
expect(screen.getByText('Ultra').closest('button')!.className).toContain('border-[#00d4ff]')
|
||||||
|
expect(screen.getByText('SVG').closest('button')!.className).not.toContain('border-[#00d4ff]')
|
||||||
|
})
|
||||||
|
|
||||||
it('closes after successful export', async () => {
|
it('closes after successful export', async () => {
|
||||||
render(<ExportModal open onClose={onClose} getElement={getElement} />)
|
render(<ExportModal open onClose={onClose} getElement={getElement} />)
|
||||||
fireEvent.click(screen.getByRole('button', { name: /download/i }))
|
fireEvent.click(screen.getByRole('button', { name: /download/i }))
|
||||||
@@ -69,6 +94,6 @@ describe('ExportModal', () => {
|
|||||||
|
|
||||||
it('does not render when closed', () => {
|
it('does not render when closed', () => {
|
||||||
render(<ExportModal open={false} onClose={onClose} getElement={getElement} />)
|
render(<ExportModal open={false} onClose={onClose} getElement={getElement} />)
|
||||||
expect(screen.queryByText('Export as PNG')).not.toBeInTheDocument()
|
expect(screen.queryByText('Export Canvas')).not.toBeInTheDocument()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||||
import { exportToPng, EXPORT_QUALITY_OPTIONS } from '../export'
|
import { exportToPng, exportToSvg, EXPORT_QUALITY_OPTIONS } from '../export'
|
||||||
|
|
||||||
const mockToPng = vi.fn()
|
const mockToPng = vi.fn()
|
||||||
vi.mock('html-to-image', () => ({ toPng: (...args: unknown[]) => mockToPng(...args) }))
|
const mockToSvg = vi.fn()
|
||||||
|
vi.mock('html-to-image', () => ({
|
||||||
|
toPng: (...args: unknown[]) => mockToPng(...args),
|
||||||
|
toSvg: (...args: unknown[]) => mockToSvg(...args),
|
||||||
|
}))
|
||||||
|
|
||||||
describe('exportToPng', () => {
|
describe('exportToPng', () => {
|
||||||
let el: HTMLElement
|
let el: HTMLElement
|
||||||
@@ -18,6 +22,7 @@ describe('exportToPng', () => {
|
|||||||
)
|
)
|
||||||
appendSpy = vi.spyOn(document.body, 'appendChild').mockImplementation((n) => n)
|
appendSpy = vi.spyOn(document.body, 'appendChild').mockImplementation((n) => n)
|
||||||
mockToPng.mockResolvedValue('data:image/png;base64,abc')
|
mockToPng.mockResolvedValue('data:image/png;base64,abc')
|
||||||
|
mockToSvg.mockResolvedValue('data:image/svg+xml;base64,abc')
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -56,6 +61,35 @@ describe('exportToPng', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('exportToSvg', () => {
|
||||||
|
let el: HTMLElement
|
||||||
|
let clickSpy: ReturnType<typeof vi.fn>
|
||||||
|
let createSpy: ReturnType<typeof vi.spyOn>
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
el = document.createElement('div')
|
||||||
|
clickSpy = vi.fn()
|
||||||
|
createSpy = vi.spyOn(document, 'createElement').mockReturnValue(
|
||||||
|
Object.assign(document.createElement('a'), { click: clickSpy }) as HTMLAnchorElement
|
||||||
|
)
|
||||||
|
mockToSvg.mockResolvedValue('data:image/svg+xml;base64,abc')
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
createSpy.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('calls toSvg with dark background color', async () => {
|
||||||
|
await exportToSvg(el)
|
||||||
|
expect(mockToSvg).toHaveBeenCalledWith(el, expect.objectContaining({ backgroundColor: '#0d1117' }))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('triggers a download', async () => {
|
||||||
|
await exportToSvg(el)
|
||||||
|
expect(clickSpy).toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('EXPORT_QUALITY_OPTIONS', () => {
|
describe('EXPORT_QUALITY_OPTIONS', () => {
|
||||||
it('has exactly three options', () => {
|
it('has exactly three options', () => {
|
||||||
expect(EXPORT_QUALITY_OPTIONS).toHaveLength(3)
|
expect(EXPORT_QUALITY_OPTIONS).toHaveLength(3)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { toPng } from 'html-to-image'
|
import { toPng, toSvg } from 'html-to-image'
|
||||||
|
|
||||||
export type ExportQuality = 'standard' | 'high' | 'ultra'
|
export type ExportQuality = 'standard' | 'high' | 'ultra'
|
||||||
|
export type ExportFormat = 'png' | 'svg'
|
||||||
|
|
||||||
export const EXPORT_QUALITY_OPTIONS: { value: ExportQuality; label: string; pixelRatio: number; hint: string }[] = [
|
export const EXPORT_QUALITY_OPTIONS: { value: ExportQuality; label: string; pixelRatio: number; hint: string }[] = [
|
||||||
{ value: 'standard', label: 'Standard', pixelRatio: 1, hint: '1× — small file' },
|
{ value: 'standard', label: 'Standard', pixelRatio: 1, hint: '1× — small file' },
|
||||||
@@ -18,8 +19,23 @@ export async function exportToPng(element: HTMLElement, quality: ExportQuality =
|
|||||||
} as Partial<CSSStyleDeclaration>,
|
} as Partial<CSSStyleDeclaration>,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
triggerDownload(dataUrl, 'homelable-canvas.png')
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function exportToSvg(element: HTMLElement): Promise<void> {
|
||||||
|
const dataUrl = await toSvg(element, {
|
||||||
|
backgroundColor: '#0d1117',
|
||||||
|
style: {
|
||||||
|
'--xy-controls-display': 'none',
|
||||||
|
} as Partial<CSSStyleDeclaration>,
|
||||||
|
})
|
||||||
|
|
||||||
|
triggerDownload(dataUrl, 'homelable-canvas.svg')
|
||||||
|
}
|
||||||
|
|
||||||
|
function triggerDownload(dataUrl: string, filename: string): void {
|
||||||
const link = document.createElement('a')
|
const link = document.createElement('a')
|
||||||
link.download = 'homelable-canvas.png'
|
link.download = filename
|
||||||
link.href = dataUrl
|
link.href = dataUrl
|
||||||
link.click()
|
link.click()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user