Skip to content

Commit 97bc19e

Browse files
committed
Throttle zoom, cleanup code.
1 parent 0a1fec7 commit 97bc19e

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

kolibri/plugins/document_pdf_render/assets/src/views/index.vue

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
:defaultHeight="pageHeight"
4343
:defaultWidth="pageWidth"
4444
:scale="scale"
45-
:pageNum="index + 1">
46-
</page-component>
45+
:pageNum="index + 1"/>
4746
</div>
4847
</div>
4948

@@ -60,8 +59,8 @@
6059
import responsiveElement from 'kolibri.coreVue.mixins.responsiveElement';
6160
import responsiveWindow from 'kolibri.coreVue.mixins.responsiveWindow';
6261
import { sessionTimeSpent } from 'kolibri.coreVue.vuex.getters';
63-
import { debounce } from 'lodash';
64-
import pageComponent from './pdfPage';
62+
import { debounce, throttle } from 'lodash';
63+
import pageComponent from './pageComponent';
6564
6665
// Source from which PDFJS loads its service worker, this is based on the __publicPath
6766
// global that is defined in the Kolibri webpack pipeline, and the additional entry in the PDF renderer's
@@ -75,6 +74,8 @@
7574
// Minimum height of the PDF viewer in pixels
7675
const minViewerHeight = 400;
7776
77+
const scaleIncrement = 0.25;
78+
7879
export default {
7980
name: 'documentPDFRender',
8081
mixins: [responsiveWindow, responsiveElement],
@@ -123,12 +124,12 @@
123124
this.isFullscreen = !this.isFullscreen;
124125
}
125126
},
126-
zoomIn() {
127-
this.scale += 0.1;
128-
},
129-
zoomOut() {
130-
this.scale -= 0.1;
131-
},
127+
zoomIn: throttle(function() {
128+
this.scale += scaleIncrement;
129+
}, renderDebounceTime),
130+
zoomOut: throttle(function() {
131+
this.scale -= scaleIncrement;
132+
}, renderDebounceTime),
132133
getPage(pageNum) {
133134
return this.pdfDocument.getPage(pageNum);
134135
},

kolibri/plugins/document_pdf_render/assets/src/views/pdfPage.vue renamed to kolibri/plugins/document_pdf_render/assets/src/views/pageComponent.vue

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@
5757
},
5858
},
5959
methods: {
60+
cancelRender() {
61+
if (this.renderTask) {
62+
this.renderTask.cancel();
63+
}
64+
delete this.renderTask;
65+
this.rendered = false;
66+
},
6067
clearPage() {
6168
const canvasContext = this.$refs.canvas.getContext('2d');
6269
// Clear canvas
@@ -67,19 +74,23 @@
6774
// Get viewport, which contains directions to be passed into render function
6875
return this.pdfPage.getViewport(this.scale);
6976
},
70-
setPageDimensions(viewport) {
71-
// Set height and width based on the viewport
72-
this.height = viewport.height;
73-
this.width = viewport.width;
77+
setPageDimensions() {
78+
// Set height and width based on the the pdfPage information and the scale
79+
this.height = this.pdfPage.view[3] * this.scale;
80+
this.width = this.pdfPage.view[2] * this.scale;
7481
},
75-
renderPage() {
82+
renderPage(newVal, oldVal) {
83+
if (typeof newVal === 'number' && typeof oldVal === 'number' && newVal !== oldVal) {
84+
// Change values are numeric, so we should assume it is a change in scale
85+
this.cancelRender();
86+
}
7687
if (this.pdfPage && this.active) {
7788
if (!this.renderTask && !this.rendered) {
7889
const canvasContext = this.$refs.canvas.getContext('2d');
7990
8091
const viewport = this.getViewport();
8192
82-
this.setPageDimensions(viewport);
93+
this.setPageDimensions();
8394
8495
this.renderTask = this.pdfPage.render({
8596
canvasContext,
@@ -98,15 +109,12 @@
98109
}
99110
} else if (this.pdfPage && this.pdfPage.getViewport) {
100111
// We have a pdfPage, so use this opportunity to set the current page width and height
101-
this.setPageDimensions(this.getViewport());
112+
this.setPageDimensions();
102113
} else {
103114
// No pdfPage and not active, either we are not being asked to render a page yet, or it has been removed
104115
// so we should tear down any existing page
105-
if (this.renderTask) {
106-
this.renderTask.cancel();
107-
} else {
108-
this.clearPage();
109-
}
116+
this.cancelRender();
117+
this.clearPage();
110118
}
111119
},
112120
},

0 commit comments

Comments
 (0)