-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathindex.jsx
More file actions
317 lines (294 loc) · 9.79 KB
/
index.jsx
File metadata and controls
317 lines (294 loc) · 9.79 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import React, { useEffect, useRef, useState } from 'react'
import { Button, message } from 'antd'
import { ZoomInOutlined, ZoomOutOutlined, SyncOutlined, DownloadOutlined } from '@ant-design/icons'
const defaultBg = '#1e1e1e'
function SvgPreview ({
svgSource = '',
svgUrl = '',
SvgComponent = null,
downloadName = 'diagram.svg',
background = defaultBg
}) {
const containerRef = useRef(null)
const innerRef = useRef(null)
const [content, setContent] = useState(svgSource || '')
const [scale, setScale] = useState(0.6)
const scaleRef = useRef(scale)
const [position, setPosition] = useState({ x: 0, y: 0 })
const positionRef = useRef(position)
const [isDragging, setIsDragging] = useState(false)
const isDraggingRef = useRef(isDragging)
const [startPos, setStartPos] = useState({ x: 0, y: 0 })
const startPosRef = useRef(startPos)
useEffect(() => {
scaleRef.current = scale
}, [scale])
useEffect(() => {
positionRef.current = position
}, [position])
useEffect(() => {
isDraggingRef.current = isDragging
}, [isDragging])
useEffect(() => {
startPosRef.current = startPos
}, [startPos])
// load remote svg if svgUrl provided
useEffect(() => {
let mounted = true
// if a React SvgComponent is provided, prefer rendering it instead of loading string
if (SvgComponent) {
if (mounted) setContent('')
return () => {
mounted = false
}
}
if (svgUrl) {
fetch(svgUrl)
.then((r) => r.text())
.then((t) => {
if (mounted) setContent(t)
})
.catch((e) => console.log('error', e.message))
} else {
setContent(svgSource)
}
return () => (mounted = false)
}, [svgUrl, svgSource])
// center on mount / when content changes
useEffect(() => {
const el = containerRef.current
const inner = innerRef.current
if (!el || !inner) return
const svg = inner.querySelector('svg')
if (!svg) return
// apply rendering hints
try {
svg.setAttribute('shape-rendering', 'geometricPrecision')
svg.setAttribute('text-rendering', 'geometricPrecision')
svg.style.imageRendering = 'optimizeQuality'
} catch (e) {
console.log('error', e.message)
}
const cRect = el.getBoundingClientRect()
const sRect = svg.getBoundingClientRect()
const cx = (cRect.width - sRect.width) / 2
const cy = (cRect.height - sRect.height) / 2
positionRef.current = { x: cx, y: cy }
setPosition({ x: cx, y: cy })
positionRef.current.__scale = scaleRef.current
}, [content, SvgComponent])
// native handlers for drag/touch/wheel
useEffect(() => {
const el = containerRef.current
if (!el) return
const onMouseDown = (e) => {
setIsDragging(true)
isDraggingRef.current = true
const x = e.clientX - positionRef.current.x
const y = e.clientY - positionRef.current.y
setStartPos({ x, y })
startPosRef.current = { x, y }
}
const onTouchStart = (e) => {
if (!e.touches || e.touches.length === 0) return
const t = e.touches[0]
setIsDragging(true)
isDraggingRef.current = true
const x = t.clientX - positionRef.current.x
const y = t.clientY - positionRef.current.y
setStartPos({ x, y })
startPosRef.current = { x, y }
}
const onTouchMove = (e) => {
if (!isDraggingRef.current || !e.touches || e.touches.length === 0) return
e.preventDefault()
const t = e.touches[0]
setPosition({ x: t.clientX - startPosRef.current.x, y: t.clientY - startPosRef.current.y })
}
const onTouchEnd = () => {
setIsDragging(false)
isDraggingRef.current = false
}
const onWheel = (e) => {
e.preventDefault()
const rect = el.getBoundingClientRect()
const mx = e.clientX - rect.left
const my = e.clientY - rect.top
const curScale = positionRef.current?.__scale || scaleRef.current || scale
const factor = e.deltaY < 0 ? 1.12 : 0.88
const ns = Math.min(5, Math.max(0.1, curScale * factor))
const ratio = ns / curScale
const curPos = positionRef.current || { x: 0, y: 0 }
const newX = mx - (mx - curPos.x) * ratio
const newY = my - (my - curPos.y) * ratio
positionRef.current = { x: newX, y: newY }
setPosition({ x: newX, y: newY })
positionRef.current.__scale = ns
scaleRef.current = ns
setScale(ns)
}
el.addEventListener('mousedown', onMouseDown)
el.addEventListener('touchstart', onTouchStart, { passive: true })
el.addEventListener('touchmove', onTouchMove, { passive: false })
el.addEventListener('touchend', onTouchEnd)
el.addEventListener('wheel', onWheel, { passive: false })
// mousemove / up on window to track dragging
const onMouseMove = (e) => {
if (!isDraggingRef.current) return
setPosition({ x: e.clientX - startPosRef.current.x, y: e.clientY - startPosRef.current.y })
}
const onMouseUp = () => setIsDragging(false)
window.addEventListener('mousemove', onMouseMove)
window.addEventListener('mouseup', onMouseUp)
return () => {
el.removeEventListener('mousedown', onMouseDown)
el.removeEventListener('touchstart', onTouchStart)
el.removeEventListener('touchmove', onTouchMove)
el.removeEventListener('touchend', onTouchEnd)
el.removeEventListener('wheel', onWheel)
window.removeEventListener('mousemove', onMouseMove)
window.removeEventListener('mouseup', onMouseUp)
}
}, [])
const handleZoomIn = () => {
setScale((prev) => {
const ns = Math.min(prev + 0.2, 5)
scaleRef.current = ns
return ns
})
}
const handleZoomOut = () => {
setScale((prev) => {
const ns = Math.max(prev - 0.2, 0.1)
scaleRef.current = ns
return ns
})
}
const handleReset = () => {
setScale(0.6)
scaleRef.current = 0.6
setPosition({ x: 0, y: 0 })
message.info('视图已重置')
}
const prepareSvgForDownload = (raw) => {
try {
let svg = raw.replace(/<br>/g, '<br/>')
if (!/xmlns="http:\/\/www.w3.org\/2000\/svg"/.test(svg)) {
svg = svg.replace('<svg', '<svg xmlns="http://www.w3.org/2000/svg"')
}
if (!/id="__download_bg__"/.test(svg)) {
svg = svg.replace(
/(<svg[^>]*>)/i,
`$1<rect id="__download_bg__" width="100%" height="100%" fill="${background}"/>`
)
}
return svg
} catch (e) {
console.log('error', e.message)
return raw
}
}
const handleDownload = async () => {
try {
let raw = content
if (!raw && svgUrl) {
const r = await fetch(svgUrl)
raw = await r.text()
}
// if no raw string but a SvgComponent is rendered, serialize the rendered SVG DOM
if (!raw && SvgComponent && innerRef.current) {
try {
const svgNode = innerRef?.current?.querySelector('svg')
if (svgNode) {
const cloned = svgNode.cloneNode(true)
if (!cloned.getAttribute('xmlns')) {
cloned.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
}
const ser = new XMLSerializer().serializeToString(cloned)
raw = ser
}
} catch (e) {
console.log('error', e.message)
}
}
if (!raw) throw new Error('no svg content')
const svg = prepareSvgForDownload(raw)
const blob = new Blob([svg], { type: 'image/svg+xml;charset=utf-8' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = downloadName
document.body.appendChild(a)
a.click()
a.remove()
URL.revokeObjectURL(url)
message.success('SVG 下载已开始')
} catch (e) {
console.log('error', e.message)
message.error('下载失败')
}
}
return (
<div style={{ height: '100%', position: 'relative' }}>
<div
ref={containerRef}
role='application'
aria-label='SVG viewer'
style={{
width: '100%',
height: '100%',
backgroundColor: background,
overflow: 'hidden',
cursor: isDragging ? 'grabbing' : 'grab',
position: 'relative'
}}
>
<div
ref={innerRef}
style={{
transformOrigin: '0 0',
transform: `translate(${position.x}px, ${position.y}px) scale(${scale})`,
transition: isDragging ? 'none' : 'transform 0.1s ease-out',
willChange: 'transform'
}}
>
{SvgComponent
? (
<SvgComponent style={{ width: '100%', height: '100%' }} />
)
: (
<div dangerouslySetInnerHTML={{ __html: content ? content.replace(/<br>/g, '<br/>') : '' }} />
)}
</div>
</div>
<div
aria-hidden
style={{
position: 'absolute',
bottom: 12,
right: 12,
zIndex: 1200,
background: 'rgba(0,0,0,0.45)',
padding: '6px 10px',
borderRadius: 10,
backdropFilter: 'blur(6px)',
display: 'flex',
alignItems: 'center',
gap: 8,
boxShadow: '0 6px 18px rgba(0,0,0,0.35)'
}}
>
<Button icon={<ZoomOutOutlined />} onClick={handleZoomOut} type='text' style={{ color: '#fff' }} />
<div style={{ color: '#fff', minWidth: 56, textAlign: 'center', fontWeight: 500 }}>
{Math.round(scale * 100)}%
</div>
<Button icon={<ZoomInOutlined />} onClick={handleZoomIn} type='text' style={{ color: '#fff' }} />
<Button icon={<SyncOutlined />} onClick={handleReset} type='text' style={{ color: '#fff' }} />
<Button icon={<DownloadOutlined />} onClick={handleDownload} type='primary' size='small'>
下载
</Button>
</div>
</div>
)
}
export default SvgPreview