Skip to content

Commit

Permalink
wait load the image in raw html
Browse files Browse the repository at this point in the history
  • Loading branch information
JandersonConstantino committed May 14, 2019
1 parent e299a8d commit d73a9cc
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/js/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const Print = {
}

// If printing image, wait for it to load inside the iframe
if (params.type === 'image') {
loadIframeImages(printDocument, params).then(() => {
if (printDocument.getElementsByTagName('img').length > 0) {
loadIframeImages(printDocument).then(() => {
performPrint(iframeElement, params)
})
} else {
Expand Down Expand Up @@ -70,22 +70,24 @@ function performPrint (iframeElement, params) {
}
}

function loadIframeImages (printDocument, params) {
const promises = params.printable.map((image, index) => loadIframeImage(printDocument, index))
function loadIframeImages (printDocument) {
let tagsImg = printDocument.getElementsByTagName('img')

const promises = []

for (let index = 0; index < tagsImg.length; index++) {
promises.push(loadIframeImage(tagsImg[index]))
}

return Promise.all(promises)
}

function loadIframeImage (printDocument, index) {
function loadIframeImage (image) {
return new Promise(resolve => {
const pollImage = () => {
let image = printDocument ? printDocument.getElementById('printableImage' + index) : null

if (!image || typeof image.naturalWidth === 'undefined' || image.naturalWidth === 0) {
setTimeout(pollImage, 500)
} else {
resolve()
}
!image || typeof image.naturalWidth === 'undefined' || image.naturalWidth === 0
? setTimeout(pollImage, 500)
: resolve()
}
pollImage()
})
Expand Down

0 comments on commit d73a9cc

Please sign in to comment.