From 3b67c0a846dbfdb6509906d893fc1ecf55ed25c3 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Tue, 13 Nov 2018 10:40:58 -0800 Subject: [PATCH] undo map even if conversion fails --- components/Editor.js | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/components/Editor.js b/components/Editor.js index d0ec762..052d2d4 100644 --- a/components/Editor.js +++ b/components/Editor.js @@ -125,7 +125,7 @@ class Editor extends React.Component { } } - getCarbonImage({ format, type } = { format: 'png' }) { + async getCarbonImage({ format, type } = { format: 'png' }) { // if safari, get image from api const isPNG = format !== 'svg' if ( @@ -150,7 +150,7 @@ class Editor extends React.Component { if (isPNG) { node.querySelectorAll('span[role="presentation"]').forEach(node => { - if (node.innerText && node.innerText.match(/%\S\S/)) { + if (node.innerText && node.innerText.match(/%\d\S/)) { map.set(node, node.innerText) node.innerText = encodeURIComponent(node.innerText) } @@ -178,24 +178,27 @@ class Editor extends React.Component { height } - if (type === 'blob') { - if (format === 'svg') { - return domtoimage - .toSvg(node, config) - .then(dataUrl => dataUrl.replace(/ /g, ' ')) - .then(uri => uri.slice(uri.indexOf(',') + 1)) - .then(data => new Blob([data], { type: 'image/svg+xml' })) - .then(data => window.URL.createObjectURL(data)) + try { + if (type === 'blob') { + if (format === 'svg') { + return domtoimage + .toSvg(node, config) + .then(dataUrl => dataUrl.replace(/ /g, ' ')) + .then(uri => uri.slice(uri.indexOf(',') + 1)) + .then(data => new Blob([data], { type: 'image/svg+xml' })) + .then(data => window.URL.createObjectURL(data)) + } + + return await domtoimage.toBlob(node, config).then(blob => window.URL.createObjectURL(blob)) } - return domtoimage - .toBlob(node, config) - .then(blob => window.URL.createObjectURL(blob)) - .then(undoMap) + // Twitter needs regular dataurls + return await domtoimage.toPng(node, config) + } catch (error) { + throw error + } finally { + undoMap() } - - // Twitter needs regular dataurls - return domtoimage.toPng(node, config).then(undoMap) } updateSetting(key, value) {