-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathPreview.js
46 lines (44 loc) · 1.42 KB
/
Preview.js
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
import React from 'react'
import { Button } from 'antd'
import { Link } from 'react-router-dom'
import moment from 'moment'
import { Base64 } from 'js-base64'
import mermaid from 'mermaid'
class Preview extends React.Component {
constructor (props) {
super(props)
this.onDownloadSVG = this.onDownloadSVG.bind(this)
}
onDownloadSVG (event) {
event.target.href = `data:image/svg+xml;base64,${Base64.encode(this.container.innerHTML)}`
event.target.download = `mermaid-diagram-${moment().format('YYYYMMDDHHmmss')}.svg`
}
render () {
const { code, match: { url } } = this.props
return <div>
<div ref={div => { this.container = div }}>{code}</div>
<div className='separator' />
<Button type='primary'><Link to={url.replace('/edit/', '/view/')}>Link to View</Link></Button>
<Button type='primary'><a href='' download='' onClick={this.onDownloadSVG}>Download SVG</a></Button>
</div>
}
initMermaid () {
const { code, history, match: { url } } = this.props
try {
mermaid.parse(code)
mermaid.init(undefined, this.container)
} catch ({str, hash}) {
const base64 = Base64.encodeURI(str)
history.push(`${url}/error/${base64}`)
}
}
componentDidMount () {
this.initMermaid()
}
componentDidUpdate () {
this.container.removeAttribute('data-processed')
this.container.innerHTML = this.props.code
this.initMermaid()
}
}
export default Preview