@libresign/pdf-elements 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@libresign/pdf-elements",
3
3
  "description": "PDF viewer with draggable and resizable element overlays for Vue 2",
4
- "version": "0.2.1",
4
+ "version": "0.2.3",
5
5
  "author": "LibreCode <contact@librecode.coop>",
6
6
  "private": false,
7
7
  "main": "dist/pdf-elements.umd.js",
@@ -213,6 +213,7 @@ export default {
213
213
  boundHandleWheel: null,
214
214
  debouncedApplyZoom: null,
215
215
  visualScale: this.initialScale,
216
+ autoFitApplied: false,
216
217
  }
217
218
  },
218
219
  mounted() {
@@ -228,9 +229,6 @@ export default {
228
229
  window.addEventListener('resize', this.onViewportScroll)
229
230
  this.$el?.addEventListener('scroll', this.onViewportScroll, { passive: true })
230
231
  this.$el?.addEventListener('wheel', this.boundHandleWheel, { passive: false })
231
- if (this.autoFitZoom) {
232
- window.addEventListener('resize', this.adjustZoomToFit)
233
- }
234
232
  },
235
233
  beforeUnmount() {
236
234
  if (this.zoomRafId) {
@@ -248,9 +246,6 @@ export default {
248
246
  window.removeEventListener('scroll', this.onViewportScroll)
249
247
  window.removeEventListener('resize', this.onViewportScroll)
250
248
  this.$el?.removeEventListener('scroll', this.onViewportScroll)
251
- if (this.autoFitZoom) {
252
- window.removeEventListener('resize', this.adjustZoomToFit)
253
- }
254
249
  if (this.viewportRafId) {
255
250
  window.cancelAnimationFrame(this.viewportRafId)
256
251
  this.viewportRafId = 0
@@ -260,6 +255,7 @@ export default {
260
255
  async init() {
261
256
  if (!this.initFiles || this.initFiles.length === 0) return
262
257
  const docs = []
258
+ this.autoFitApplied = false
263
259
 
264
260
  for (let i = 0; i < this.initFiles.length; i++) {
265
261
  const file = this.initFiles[i]
@@ -274,8 +270,16 @@ export default {
274
270
  }
275
271
 
276
272
  const pages = []
273
+ const pageWidths = Array(pdfDoc.numPages).fill(0)
277
274
  for (let p = 1; p <= pdfDoc.numPages; p++) {
278
- pages.push(pdfDoc.getPage(p))
275
+ const pagePromise = pdfDoc.getPage(p)
276
+ pagePromise.then((page) => {
277
+ pageWidths[p - 1] = page.getViewport({ scale: 1 }).width
278
+ if (this.autoFitZoom) {
279
+ this.scheduleAutoFitZoom()
280
+ }
281
+ })
282
+ pages.push(pagePromise)
279
283
  }
280
284
 
281
285
  docs.push({
@@ -284,6 +288,7 @@ export default {
284
288
  pdfDoc,
285
289
  numPages: pdfDoc.numPages,
286
290
  pages,
291
+ pageWidths,
287
292
  pagesScale: Array(pdfDoc.numPages).fill(this.scale),
288
293
  allObjects: Array(pdfDoc.numPages).fill(0).map(() => []),
289
294
  })
@@ -296,7 +301,7 @@ export default {
296
301
  this.$emit('pdf-elements:end-init', { docsCount: docs.length })
297
302
  this.$nextTick(() => {
298
303
  if (this.autoFitZoom) {
299
- this.adjustZoomToFit()
304
+ this.scheduleAutoFitZoom()
300
305
  }
301
306
  })
302
307
  }
@@ -798,6 +803,9 @@ export default {
798
803
  if (docIndex < 0 || docIndex >= this.pdfDocuments.length) return
799
804
  this.pdfDocuments[docIndex].pagesScale[pageIndex] = e.scale
800
805
  this.cachePageBounds()
806
+ if (this.autoFitZoom) {
807
+ this.scheduleAutoFitZoom()
808
+ }
801
809
  },
802
810
 
803
811
  formatPageNumber(currentPage, totalPages) {
@@ -826,17 +834,38 @@ export default {
826
834
  const availableWidth = containerWidth - 40
827
835
  return Math.max(0.1, Math.min(2, availableWidth / maxPageWidth))
828
836
  },
837
+ scheduleAutoFitZoom() {
838
+ if (this.autoFitApplied) return
839
+ if (this.zoomRafId) return
840
+ this.zoomRafId = window.requestAnimationFrame(() => {
841
+ this.zoomRafId = 0
842
+ this.adjustZoomToFit()
843
+ })
844
+ },
829
845
  adjustZoomToFit() {
830
- if (!this.autoFitZoom || !this.pdfDocuments.length) return
831
-
832
- const canvases = this.$el?.querySelectorAll('canvas')
833
- if (!canvases?.length) return
834
-
835
- const maxCanvasWidth = Math.max(...Array.from(canvases).map(canvas =>
836
- canvas.width / (this.scale || 1),
837
- ))
846
+ if (!this.autoFitZoom || this.autoFitApplied || !this.pdfDocuments.length) return
847
+
848
+ const widths = this.pdfDocuments
849
+ .flatMap(doc => doc.pageWidths || [])
850
+ .filter(width => width > 0)
851
+
852
+ let maxCanvasWidth = 0
853
+ if (widths.length) {
854
+ maxCanvasWidth = Math.max(...widths)
855
+ } else {
856
+ if (this.autoFitZoom) {
857
+ this.scheduleAutoFitZoom()
858
+ return
859
+ }
860
+ const canvases = this.$el?.querySelectorAll('canvas')
861
+ if (!canvases?.length) return
862
+ maxCanvasWidth = Math.max(...Array.from(canvases).map(canvas =>
863
+ canvas.width / (this.scale || 1),
864
+ ))
865
+ }
838
866
 
839
867
  const optimalScale = this.calculateOptimalScale(maxCanvasWidth)
868
+ this.autoFitApplied = true
840
869
  if (Math.abs(optimalScale - this.scale) > 0.01) {
841
870
  this.scale = optimalScale
842
871
  this.visualScale = optimalScale