Skip to content

Commit

Permalink
refactor: updated screenshot.js to use dynamic module imports
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcMcIntosh committed Dec 8, 2020
1 parent eb73191 commit 2e6fc1e
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/toolbar/preview/screenshot.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
let html2canvasPromise;

const screenshot = async () => {
document.getElementById('prismic-toolbar-v2').setAttribute('data-html2canvas-ignore', true);
if (!html2canvasPromise) html2canvasPromise = script('https://html2canvas.hertzen.com/dist/html2canvas.min.js');
await html2canvasPromise;

const canvas = await window.html2canvas(document.body, {
logging: false,
width: '100%',
height: window.innerHeight,
});
return new Promise(resolve => canvas.toBlob(resolve, 'image/jpeg', 0.6));
};

function script(src) {
return new Promise(resolve => {
const el = document.createElement('script');
el.src = src;
document.head.appendChild(el);
el.addEventListener('load', () => resolve(el));
});
const options = { logging: false, width: '100%', height: window.innerHeight };

return import('https://html2canvas.hertzen.com/dist/html2canvas.esm.js')
.then(html2canvas => html2canvas(document.body, options))
.then(canvas => new Promise(resolve => canvas.toBlob(resolve, 'image/jpeg', 0.6)));
}

export default screenshot;

0 comments on commit 2e6fc1e

Please sign in to comment.