clean up getCarbonImage a bit

main
Mike Fix 4 years ago
parent db1115a55c
commit f934296b93
No known key found for this signature in database
GPG Key ID: 1D85E862314CA79F

@ -126,11 +126,6 @@ class Editor extends React.Component {
const node = this.carbonNode.current const node = this.carbonNode.current
const map = new Map() const map = new Map()
const undoMap = value => {
map.forEach((value, node) => (node.innerHTML = value))
return value
}
if (isPNG) { if (isPNG) {
node.querySelectorAll('span[role="presentation"]').forEach(node => { node.querySelectorAll('span[role="presentation"]').forEach(node => {
if (node.innerText && node.innerText.match(/%[A-Fa-f0-9]{2}/)) { if (node.innerText && node.innerText.match(/%[A-Fa-f0-9]{2}/)) {
@ -176,27 +171,25 @@ class Editor extends React.Component {
// TODO consolidate type/format to only use one param // TODO consolidate type/format to only use one param
if (type === 'objectURL') { if (type === 'objectURL') {
if (format === 'svg') { if (format === 'svg') {
return ( return domtoimage
domtoimage .toSvg(node, config)
.toSvg(node, config) .then(dataUrl =>
.then(dataUrl => dataUrl
dataUrl .replace(/ /g, ' ')
.replace(/ /g, ' ') // https://github.com/tsayen/dom-to-image/blob/fae625bce0970b3a039671ea7f338d05ecb3d0e8/src/dom-to-image.js#L551
// https://github.com/tsayen/dom-to-image/blob/fae625bce0970b3a039671ea7f338d05ecb3d0e8/src/dom-to-image.js#L551 .replace(/%23/g, '#')
.replace(/%23/g, '#') .replace(/%0A/g, '\n')
.replace(/%0A/g, '\n') // https://stackoverflow.com/questions/7604436/xmlparseentityref-no-name-warnings-while-loading-xml-into-a-php-file
// remove other fonts which are not used .replace(/&(?!#?[a-z0-9]+;)/g, '&')
.replace( // remove other fonts which are not used
new RegExp('@font-face\\s+{\\s+font-family: (?!"*' + fontFamily + ').*?}', 'g'), .replace(
'' new RegExp('@font-face\\s+{\\s+font-family: (?!"*' + fontFamily + ').*?}', 'g'),
) ''
) )
// https://stackoverflow.com/questions/7604436/xmlparseentityref-no-name-warnings-while-loading-xml-into-a-php-file )
.then(dataUrl => dataUrl.replace(/&(?!#?[a-z0-9]+;)/g, '&')) .then(uri => uri.slice(uri.indexOf(',') + 1))
.then(uri => uri.slice(uri.indexOf(',') + 1)) .then(data => new Blob([data], { type: 'image/svg+xml' }))
.then(data => new Blob([data], { type: 'image/svg+xml' })) .then(data => window.URL.createObjectURL(data))
.then(data => window.URL.createObjectURL(data))
)
} }
return await domtoimage.toBlob(node, config).then(blob => window.URL.createObjectURL(blob)) return await domtoimage.toBlob(node, config).then(blob => window.URL.createObjectURL(blob))
@ -209,7 +202,7 @@ class Editor extends React.Component {
// Twitter needs regular dataurls // Twitter needs regular dataurls
return await domtoimage.toPng(node, config) return await domtoimage.toPng(node, config)
} finally { } finally {
undoMap() map.forEach((value, node) => (node.innerHTML = value))
} }
} }

Loading…
Cancel
Save