Fix svg using Blob URLs (#332)

main
Michael Fix 7 years ago committed by GitHub
parent 517aca7c8f
commit 2276435fef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -73,7 +73,7 @@ class Editor extends React.Component {
this.props.onUpdate(this.state) this.props.onUpdate(this.state)
} }
getCarbonImage({ format, type } = { format: 'png' }) { getCarbonImage({ format } = { format: 'png' }) {
// if safari, get image from api // if safari, get image from api
if ( if (
navigator.userAgent.indexOf('Safari') !== -1 && navigator.userAgent.indexOf('Safari') !== -1 &&
@ -112,15 +112,16 @@ class Editor extends React.Component {
height height
} }
if (type === 'blob') if (format === 'svg') {
return domtoimage return domtoimage
.toBlob(node, config) .toSvg(node, config)
.then(blob => window.URL.createObjectURL(blob, { type: 'image/png' })) .then(dataUrl => dataUrl.split(' ').join(' '))
.then(uri => uri.slice(uri.indexOf(',') + 1))
if (format === 'svg') .then(data => new Blob([data], { type: 'image/svg+xml' }))
return domtoimage.toSvg(node, config).then(dataUrl => dataUrl.split(' ').join(' ')) .then(data => window.URL.createObjectURL(data))
}
return domtoimage.toPng(node, config) return domtoimage.toBlob(node, config).then(blob => window.URL.createObjectURL(blob))
} }
updateSetting(key, value) { updateSetting(key, value) {
@ -130,9 +131,7 @@ class Editor extends React.Component {
save({ id: format = 'png' }) { save({ id: format = 'png' }) {
const link = document.createElement('a') const link = document.createElement('a')
const type = format === 'png' ? 'blob' : undefined return this.getCarbonImage({ format }).then(url => {
return this.getCarbonImage({ format, type }).then(url => {
link.download = `carbon.${format}` link.download = `carbon.${format}`
link.href = url link.href = url
document.body.appendChild(link) document.body.appendChild(link)

Loading…
Cancel
Save