Commit 5246626d authored by Lukas Eipert's avatar Lukas Eipert

Simplify pdf.js logic

Instead of complicated splicing, we can simply return all pages.
Promise.all will take care of the correct ordering for us.
parent 6d3b203d
...@@ -14,7 +14,6 @@ export default { ...@@ -14,7 +14,6 @@ export default {
}, },
data() { data() {
return { return {
loading: false,
pages: [], pages: [],
}; };
}, },
...@@ -37,23 +36,18 @@ export default { ...@@ -37,23 +36,18 @@ export default {
return pdfjsLib return pdfjsLib
.getDocument(this.document) .getDocument(this.document)
.then(this.renderPages) .then(this.renderPages)
.then(() => this.$emit('pdflabload')) .then(pages => {
.catch(error => this.$emit('pdflaberror', error)) this.pages = pages;
.then(() => { this.$emit('pdflabload');
// Trigger a Vue update: https://vuejs.org/v2/guide/list.html#Caveats })
this.pages.splice(this.pages.length); .catch(error => {
this.loading = false; this.$emit('pdflaberror', error);
}); });
}, },
renderPages(pdf) { renderPages(pdf) {
const pagePromises = []; const pagePromises = [];
this.loading = true;
for (let num = 1; num <= pdf.numPages; num += 1) { for (let num = 1; num <= pdf.numPages; num += 1) {
pagePromises.push( pagePromises.push(pdf.getPage(num));
pdf.getPage(num).then(p => {
this.pages[p.pageIndex] = p;
}),
);
} }
return Promise.all(pagePromises); return Promise.all(pagePromises);
}, },
...@@ -65,8 +59,8 @@ export default { ...@@ -65,8 +59,8 @@ export default {
<div v-if="hasPDF" class="pdf-viewer"> <div v-if="hasPDF" class="pdf-viewer">
<page <page
v-for="(page, index) in pages" v-for="(page, index) in pages"
v-if="page"
:key="index" :key="index"
:v-if="!loading"
:page="page" :page="page"
:number="index + 1" :number="index + 1"
/> />
......
...@@ -39,7 +39,9 @@ export default { ...@@ -39,7 +39,9 @@ export default {
.then(() => { .then(() => {
this.rendering = false; this.rendering = false;
}) })
.catch(error => this.$emit('pdflaberror', error)); .catch(error => {
this.$emit('pdflaberror', error);
});
}, },
}; };
</script> </script>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment