Skip to content

Commit 4299b87

Browse files
committed
feat: implement auto fet zoom
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 33fe410 commit 4299b87

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/components/PDFElements.vue

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ export default {
179179
type: String,
180180
default: '{currentPage} of {totalPages}',
181181
},
182+
autoFitZoom: {
183+
type: Boolean,
184+
default: false,
185+
},
182186
},
183187
data() {
184188
return {
@@ -224,6 +228,9 @@ export default {
224228
window.addEventListener('resize', this.onViewportScroll)
225229
this.$el?.addEventListener('scroll', this.onViewportScroll, { passive: true })
226230
this.$el?.addEventListener('wheel', this.boundHandleWheel, { passive: false })
231+
if (this.autoFitZoom) {
232+
window.addEventListener('resize', this.adjustZoomToFit)
233+
}
227234
},
228235
beforeUnmount() {
229236
if (this.zoomRafId) {
@@ -241,6 +248,9 @@ export default {
241248
window.removeEventListener('scroll', this.onViewportScroll)
242249
window.removeEventListener('resize', this.onViewportScroll)
243250
this.$el?.removeEventListener('scroll', this.onViewportScroll)
251+
if (this.autoFitZoom) {
252+
window.removeEventListener('resize', this.adjustZoomToFit)
253+
}
244254
if (this.viewportRafId) {
245255
window.cancelAnimationFrame(this.viewportRafId)
246256
this.viewportRafId = 0
@@ -804,6 +814,29 @@ export default {
804814
const pagesScale = doc.pagesScale[pageIndex] || 1
805815
return pageRef.getCanvasMeasurement().canvasHeight / pagesScale
806816
},
817+
calculateOptimalScale(maxPageWidth) {
818+
const containerWidth = this.$el?.clientWidth || 0
819+
if (!containerWidth || !maxPageWidth) return 1
820+
821+
const availableWidth = containerWidth - 40
822+
return Math.max(0.1, Math.min(2, availableWidth / maxPageWidth))
823+
},
824+
adjustZoomToFit() {
825+
if (!this.autoFitZoom || !this.pdfDocuments.length) return
826+
827+
const canvases = this.$el?.querySelectorAll('canvas')
828+
if (!canvases?.length) return
829+
830+
const maxCanvasWidth = Math.max(...Array.from(canvases).map(canvas =>
831+
canvas.width / (this.scale || 1),
832+
))
833+
834+
const optimalScale = this.calculateOptimalScale(maxCanvasWidth)
835+
if (Math.abs(optimalScale - this.scale) > 0.01) {
836+
this.scale = optimalScale
837+
this.visualScale = optimalScale
838+
}
839+
},
807840
},
808841
}
809842
</script>

0 commit comments

Comments
 (0)