Refactor the code to use async/await
This commit is contained in:
committed by
M. Shull
parent
159021233a
commit
34e0211cb6
@@ -8,9 +8,12 @@ import html2canvas from 'html2canvas';
|
|||||||
* @param {DOMElement} element
|
* @param {DOMElement} element
|
||||||
* @return {Promise<String>} DataURL of the generated image.
|
* @return {Promise<String>} DataURL of the generated image.
|
||||||
*/
|
*/
|
||||||
export const fromDom = element => html2canvas(element, {
|
export const fromDom = async (element) => {
|
||||||
logging: false,
|
const canvas = await html2canvas(element, {
|
||||||
}).then(canvas => canvas.toDataURL('image/jpeg'));
|
logging: false,
|
||||||
|
});
|
||||||
|
return canvas.toDataURL('image/jpeg');
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a thumbnail from an URL.
|
* Generates a thumbnail from an URL.
|
||||||
@@ -24,18 +27,17 @@ export const fromUrl = url => new Promise((resolve, reject) => {
|
|||||||
iframe.src = protocol + url.replace(/^https?:/, '');
|
iframe.src = protocol + url.replace(/^https?:/, '');
|
||||||
iframe.style.opacity = 0;
|
iframe.style.opacity = 0;
|
||||||
iframe.scrolling = 'no';
|
iframe.scrolling = 'no';
|
||||||
iframe.onload = () => {
|
iframe.onload = async () => {
|
||||||
const container = iframe.contentDocument.documentElement
|
const container = iframe.contentDocument.documentElement;
|
||||||
container.style.padding = '10px 20px'
|
container.style.padding = '10px 20px';
|
||||||
fromDom(container)
|
try {
|
||||||
.then((image) => {
|
const image = await fromDom(container);
|
||||||
document.body.removeChild(iframe);
|
document.body.removeChild(iframe);
|
||||||
resolve(image);
|
resolve(image);
|
||||||
})
|
} catch (err) {
|
||||||
.catch(() => {
|
document.body.removeChild(iframe);
|
||||||
document.body.removeChild(iframe);
|
reject(MailPoet.I18n.t('errorWhileTakingScreenshot'));
|
||||||
reject(MailPoet.I18n.t('errorWhileTakingScreenshot'));
|
}
|
||||||
});
|
|
||||||
};
|
};
|
||||||
const onError = () => {
|
const onError = () => {
|
||||||
document.body.removeChild(iframe);
|
document.body.removeChild(iframe);
|
||||||
|
Reference in New Issue
Block a user