-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathdiagramDownload.ts
More file actions
203 lines (176 loc) · 6.81 KB
/
diagramDownload.ts
File metadata and controls
203 lines (176 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Diagram download utilities
* Supports downloading SVG diagrams (Mermaid, PlantUML, etc.) as .svg or .png
*/
const DOWNLOAD_ICON = `<svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>`
const DIAGRAM_SELECTORS = `.mermaid-diagram, .plantuml-diagram`
// ─── Download helpers ────────────────────────────────────────────────────────
function triggerDownload(url: string, filename: string): void {
const a = document.createElement(`a`)
a.href = url
a.download = filename
a.style.display = `none`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
function cloneSvg(svgEl: SVGSVGElement): SVGSVGElement {
const clone = svgEl.cloneNode(true) as SVGSVGElement
if (!clone.hasAttribute(`xmlns`))
clone.setAttribute(`xmlns`, `http://www.w3.org/2000/svg`)
return clone
}
export function downloadDiagramSvg(svgEl: SVGSVGElement, filename = `diagram`): void {
const svgStr = new XMLSerializer().serializeToString(cloneSvg(svgEl))
const blob = new Blob([svgStr], { type: `image/svg+xml;charset=utf-8` })
const url = URL.createObjectURL(blob)
triggerDownload(url, `${filename}.svg`)
URL.revokeObjectURL(url)
}
export function downloadDiagramPng(svgEl: SVGSVGElement, filename = `diagram`): Promise<void> {
const rect = svgEl.getBoundingClientRect()
const width = rect.width || svgEl.viewBox?.baseVal?.width || 800
const height = rect.height || svgEl.viewBox?.baseVal?.height || 600
const scale = Math.max(2, window.devicePixelRatio)
const clone = cloneSvg(svgEl)
clone.setAttribute(`width`, String(width))
clone.setAttribute(`height`, String(height))
const svgStr = new XMLSerializer().serializeToString(clone)
// Use a percent-encoded data URL instead of a blob URL: drawing a blob-URL
// image onto canvas triggers a SecurityError ("tainted canvas") in Chromium.
// Data URLs are treated as same-origin and avoid this restriction.
const dataUrl = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgStr)}`
return new Promise((resolve, reject) => {
const img = new Image()
img.onload = () => {
const canvas = document.createElement(`canvas`)
canvas.width = Math.round(width * scale)
canvas.height = Math.round(height * scale)
const ctx = canvas.getContext(`2d`)
if (!ctx) {
reject(new Error(`No 2D canvas context`))
return
}
ctx.fillStyle = `#ffffff`
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.scale(scale, scale)
ctx.drawImage(img, 0, 0, width, height)
canvas.toBlob((blob) => {
if (!blob) {
reject(new Error(`PNG blob generation failed`))
return
}
const url = URL.createObjectURL(blob)
triggerDownload(url, `${filename}.png`)
URL.revokeObjectURL(url)
resolve()
}, `image/png`)
}
img.onerror = () => reject(new Error(`Failed to load SVG as image`))
img.src = dataUrl
})
}
// ─── Download bar DOM injection ───────────────────────────────────────────────
function makeButton(label: string, title: string): HTMLButtonElement {
const btn = document.createElement(`button`)
btn.className = `diagram-download-btn`
btn.type = `button`
btn.title = title
btn.innerHTML = `${DOWNLOAD_ICON}<span>${label}</span>`
return btn
}
function createDownloadBar(container: HTMLElement, idx: number): HTMLDivElement {
const bar = document.createElement(`div`)
bar.className = `diagram-download-bar`
const svgBtn = makeButton(`SVG`, `下载为 SVG`)
svgBtn.addEventListener(`click`, (e) => {
e.preventDefault()
e.stopPropagation()
const svg = container.querySelector<SVGSVGElement>(`svg`)
if (svg)
downloadDiagramSvg(svg, `diagram-${idx}`)
})
const pngBtn = makeButton(`PNG`, `下载为 PNG`)
pngBtn.addEventListener(`click`, async (e) => {
e.preventDefault()
e.stopPropagation()
const svg = container.querySelector<SVGSVGElement>(`svg`)
if (!svg)
return
pngBtn.disabled = true
const saved = pngBtn.innerHTML
pngBtn.innerHTML = `<span class="diagram-btn-loading">•••</span>`
try {
await downloadDiagramPng(svg, `diagram-${idx}`)
}
finally {
pngBtn.disabled = false
pngBtn.innerHTML = saved
}
})
bar.append(svgBtn, pngBtn)
return bar
}
function injectBar(container: HTMLElement, idx: number): void {
if (getComputedStyle(container).position === `static`)
container.style.position = `relative`
container.querySelector(`.diagram-download-bar`)?.remove()
container.appendChild(createDownloadBar(container, idx))
}
// ─── Overlay lifecycle ────────────────────────────────────────────────────────
export interface DiagramDownloadOverlay {
/** Permanently disconnect the observer — call on component unmount. */
cleanup: () => void
/** Remove all bars and stop injection. Call before copying / exporting. */
pause: () => void
/** Re-inject bars and resume injection. Call after copying / exporting. */
resume: () => void
}
/**
* Watches `outputEl` for diagram containers gaining SVG content and injects
* a SVG / PNG download bar into each one.
*/
export function setupDiagramDownloadOverlay(outputEl: HTMLElement): DiagramDownloadOverlay {
let paused = false
let counter = 0
function inject(el: HTMLElement) {
if (!paused && el.querySelector(`svg`) && !el.querySelector(`.diagram-download-bar`))
injectBar(el, ++counter)
}
function scan() {
outputEl.querySelectorAll<HTMLElement>(DIAGRAM_SELECTORS).forEach(inject)
}
scan()
let rafId: ReturnType<typeof requestAnimationFrame> | null = null
const observer = new MutationObserver(() => {
if (paused)
return
if (rafId !== null)
return
rafId = requestAnimationFrame(() => {
rafId = null
if (!paused)
scan()
})
})
observer.observe(outputEl, { childList: true, subtree: true })
return {
cleanup: () => {
if (rafId !== null)
cancelAnimationFrame(rafId)
observer.disconnect()
},
pause: () => {
paused = true
if (rafId !== null) {
cancelAnimationFrame(rafId)
rafId = null
}
outputEl.querySelectorAll(`.diagram-download-bar`).forEach(el => el.remove())
},
resume: () => {
paused = false
scan()
},
}
}