Skip to content

Commit 7789b02

Browse files
authored
Merge pull request #15 from LibreSign/fix/only-apply-auto-fit-when-load-file
fix: only apply auto fit when load file
2 parents 700115d + 212b76e commit 7789b02

2 files changed

Lines changed: 6 additions & 21 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@libresign/pdf-elements",
33
"description": "PDF viewer with draggable and resizable element overlays for Vue 2",
4-
"version": "0.2.2",
4+
"version": "0.2.3",
55
"author": "LibreCode <contact@librecode.coop>",
66
"private": false,
77
"main": "dist/pdf-elements.umd.js",

src/components/PDFElements.vue

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export default {
213213
boundHandleWheel: null,
214214
debouncedApplyZoom: null,
215215
visualScale: this.initialScale,
216-
resizeObserver: null,
216+
autoFitApplied: false,
217217
}
218218
},
219219
mounted() {
@@ -229,17 +229,6 @@ export default {
229229
window.addEventListener('resize', this.onViewportScroll)
230230
this.$el?.addEventListener('scroll', this.onViewportScroll, { passive: true })
231231
this.$el?.addEventListener('wheel', this.boundHandleWheel, { passive: false })
232-
if (this.autoFitZoom) {
233-
window.addEventListener('resize', this.adjustZoomToFit)
234-
if (typeof ResizeObserver !== 'undefined') {
235-
this.resizeObserver = new ResizeObserver(() => {
236-
this.scheduleAutoFitZoom()
237-
})
238-
if (this.$el) {
239-
this.resizeObserver.observe(this.$el)
240-
}
241-
}
242-
}
243232
},
244233
beforeUnmount() {
245234
if (this.zoomRafId) {
@@ -257,13 +246,6 @@ export default {
257246
window.removeEventListener('scroll', this.onViewportScroll)
258247
window.removeEventListener('resize', this.onViewportScroll)
259248
this.$el?.removeEventListener('scroll', this.onViewportScroll)
260-
if (this.autoFitZoom) {
261-
window.removeEventListener('resize', this.adjustZoomToFit)
262-
if (this.resizeObserver) {
263-
this.resizeObserver.disconnect()
264-
this.resizeObserver = null
265-
}
266-
}
267249
if (this.viewportRafId) {
268250
window.cancelAnimationFrame(this.viewportRafId)
269251
this.viewportRafId = 0
@@ -273,6 +255,7 @@ export default {
273255
async init() {
274256
if (!this.initFiles || this.initFiles.length === 0) return
275257
const docs = []
258+
this.autoFitApplied = false
276259
277260
for (let i = 0; i < this.initFiles.length; i++) {
278261
const file = this.initFiles[i]
@@ -852,14 +835,15 @@ export default {
852835
return Math.max(0.1, Math.min(2, availableWidth / maxPageWidth))
853836
},
854837
scheduleAutoFitZoom() {
838+
if (this.autoFitApplied) return
855839
if (this.zoomRafId) return
856840
this.zoomRafId = window.requestAnimationFrame(() => {
857841
this.zoomRafId = 0
858842
this.adjustZoomToFit()
859843
})
860844
},
861845
adjustZoomToFit() {
862-
if (!this.autoFitZoom || !this.pdfDocuments.length) return
846+
if (!this.autoFitZoom || this.autoFitApplied || !this.pdfDocuments.length) return
863847
864848
const widths = this.pdfDocuments
865849
.flatMap(doc => doc.pageWidths || [])
@@ -881,6 +865,7 @@ export default {
881865
}
882866
883867
const optimalScale = this.calculateOptimalScale(maxCanvasWidth)
868+
this.autoFitApplied = true
884869
if (Math.abs(optimalScale - this.scale) > 0.01) {
885870
this.scale = optimalScale
886871
this.visualScale = optimalScale

0 commit comments

Comments
 (0)