@libresign/pdf-elements 0.2.1 → 0.2.2

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.2",
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
+ resizeObserver: null,
216
217
  }
217
218
  },
218
219
  mounted() {
@@ -230,6 +231,14 @@ export default {
230
231
  this.$el?.addEventListener('wheel', this.boundHandleWheel, { passive: false })
231
232
  if (this.autoFitZoom) {
232
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
+ }
233
242
  }
234
243
  },
235
244
  beforeUnmount() {
@@ -250,6 +259,10 @@ export default {
250
259
  this.$el?.removeEventListener('scroll', this.onViewportScroll)
251
260
  if (this.autoFitZoom) {
252
261
  window.removeEventListener('resize', this.adjustZoomToFit)
262
+ if (this.resizeObserver) {
263
+ this.resizeObserver.disconnect()
264
+ this.resizeObserver = null
265
+ }
253
266
  }
254
267
  if (this.viewportRafId) {
255
268
  window.cancelAnimationFrame(this.viewportRafId)
@@ -274,8 +287,16 @@ export default {
274
287
  }
275
288
 
276
289
  const pages = []
290
+ const pageWidths = Array(pdfDoc.numPages).fill(0)
277
291
  for (let p = 1; p <= pdfDoc.numPages; p++) {
278
- pages.push(pdfDoc.getPage(p))
292
+ const pagePromise = pdfDoc.getPage(p)
293
+ pagePromise.then((page) => {
294
+ pageWidths[p - 1] = page.getViewport({ scale: 1 }).width
295
+ if (this.autoFitZoom) {
296
+ this.scheduleAutoFitZoom()
297
+ }
298
+ })
299
+ pages.push(pagePromise)
279
300
  }
280
301
 
281
302
  docs.push({
@@ -284,6 +305,7 @@ export default {
284
305
  pdfDoc,
285
306
  numPages: pdfDoc.numPages,
286
307
  pages,
308
+ pageWidths,
287
309
  pagesScale: Array(pdfDoc.numPages).fill(this.scale),
288
310
  allObjects: Array(pdfDoc.numPages).fill(0).map(() => []),
289
311
  })
@@ -296,7 +318,7 @@ export default {
296
318
  this.$emit('pdf-elements:end-init', { docsCount: docs.length })
297
319
  this.$nextTick(() => {
298
320
  if (this.autoFitZoom) {
299
- this.adjustZoomToFit()
321
+ this.scheduleAutoFitZoom()
300
322
  }
301
323
  })
302
324
  }
@@ -798,6 +820,9 @@ export default {
798
820
  if (docIndex < 0 || docIndex >= this.pdfDocuments.length) return
799
821
  this.pdfDocuments[docIndex].pagesScale[pageIndex] = e.scale
800
822
  this.cachePageBounds()
823
+ if (this.autoFitZoom) {
824
+ this.scheduleAutoFitZoom()
825
+ }
801
826
  },
802
827
 
803
828
  formatPageNumber(currentPage, totalPages) {
@@ -826,15 +851,34 @@ export default {
826
851
  const availableWidth = containerWidth - 40
827
852
  return Math.max(0.1, Math.min(2, availableWidth / maxPageWidth))
828
853
  },
854
+ scheduleAutoFitZoom() {
855
+ if (this.zoomRafId) return
856
+ this.zoomRafId = window.requestAnimationFrame(() => {
857
+ this.zoomRafId = 0
858
+ this.adjustZoomToFit()
859
+ })
860
+ },
829
861
  adjustZoomToFit() {
830
862
  if (!this.autoFitZoom || !this.pdfDocuments.length) return
831
863
 
832
- const canvases = this.$el?.querySelectorAll('canvas')
833
- if (!canvases?.length) return
864
+ const widths = this.pdfDocuments
865
+ .flatMap(doc => doc.pageWidths || [])
866
+ .filter(width => width > 0)
834
867
 
835
- const maxCanvasWidth = Math.max(...Array.from(canvases).map(canvas =>
836
- canvas.width / (this.scale || 1),
837
- ))
868
+ let maxCanvasWidth = 0
869
+ if (widths.length) {
870
+ maxCanvasWidth = Math.max(...widths)
871
+ } else {
872
+ if (this.autoFitZoom) {
873
+ this.scheduleAutoFitZoom()
874
+ return
875
+ }
876
+ const canvases = this.$el?.querySelectorAll('canvas')
877
+ if (!canvases?.length) return
878
+ maxCanvasWidth = Math.max(...Array.from(canvases).map(canvas =>
879
+ canvas.width / (this.scale || 1),
880
+ ))
881
+ }
838
882
 
839
883
  const optimalScale = this.calculateOptimalScale(maxCanvasWidth)
840
884
  if (Math.abs(optimalScale - this.scale) > 0.01) {