@libresign/pdf-elements 0.2.0 → 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.0",
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
  })
@@ -294,6 +316,11 @@ export default {
294
316
  this.selectedDocIndex = 0
295
317
  this.selectedPageIndex = 0
296
318
  this.$emit('pdf-elements:end-init', { docsCount: docs.length })
319
+ this.$nextTick(() => {
320
+ if (this.autoFitZoom) {
321
+ this.scheduleAutoFitZoom()
322
+ }
323
+ })
297
324
  }
298
325
  },
299
326
 
@@ -793,6 +820,9 @@ export default {
793
820
  if (docIndex < 0 || docIndex >= this.pdfDocuments.length) return
794
821
  this.pdfDocuments[docIndex].pagesScale[pageIndex] = e.scale
795
822
  this.cachePageBounds()
823
+ if (this.autoFitZoom) {
824
+ this.scheduleAutoFitZoom()
825
+ }
796
826
  },
797
827
 
798
828
  formatPageNumber(currentPage, totalPages) {
@@ -821,20 +851,43 @@ export default {
821
851
  const availableWidth = containerWidth - 40
822
852
  return Math.max(0.1, Math.min(2, availableWidth / maxPageWidth))
823
853
  },
854
+ scheduleAutoFitZoom() {
855
+ if (this.zoomRafId) return
856
+ this.zoomRafId = window.requestAnimationFrame(() => {
857
+ this.zoomRafId = 0
858
+ this.adjustZoomToFit()
859
+ })
860
+ },
824
861
  adjustZoomToFit() {
825
862
  if (!this.autoFitZoom || !this.pdfDocuments.length) return
826
863
 
827
- const canvases = this.$el?.querySelectorAll('canvas')
828
- if (!canvases?.length) return
864
+ const widths = this.pdfDocuments
865
+ .flatMap(doc => doc.pageWidths || [])
866
+ .filter(width => width > 0)
829
867
 
830
- const maxCanvasWidth = Math.max(...Array.from(canvases).map(canvas =>
831
- canvas.width / (this.scale || 1),
832
- ))
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
+ }
833
882
 
834
883
  const optimalScale = this.calculateOptimalScale(maxCanvasWidth)
835
884
  if (Math.abs(optimalScale - this.scale) > 0.01) {
836
885
  this.scale = optimalScale
837
886
  this.visualScale = optimalScale
887
+ this.pdfDocuments.forEach((doc) => {
888
+ doc.pagesScale = doc.pagesScale.map(() => this.scale)
889
+ })
890
+ this.cachePageBounds()
838
891
  }
839
892
  },
840
893
  },
package/src/index.js CHANGED
@@ -1,7 +1,13 @@
1
1
  // SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
2
2
  // SPDX-License-Identifier: AGPL-3.0-or-later
3
3
 
4
+ import { GlobalWorkerOptions } from 'pdfjs-dist'
5
+ import pdfWorkerCode from 'pdfjs-dist/build/pdf.worker.min.mjs'
6
+
7
+ GlobalWorkerOptions.workerSrc = pdfWorkerCode
8
+
4
9
  import PDFElements from './components/PDFElements.vue'
10
+ export { setWorkerPath } from './utils/asyncReader.js'
5
11
 
6
12
  PDFElements.install = function(Vue) {
7
13
  if (PDFElements.install.installed) return
@@ -6,6 +6,10 @@ import pdfWorkerCode from 'pdfjs-dist/build/pdf.worker.min.mjs'
6
6
 
7
7
  GlobalWorkerOptions.workerSrc = pdfWorkerCode
8
8
 
9
+ export function setWorkerPath(path) {
10
+ GlobalWorkerOptions.workerSrc = path
11
+ }
12
+
9
13
  export function readAsArrayBuffer(file) {
10
14
  return new Promise((resolve, reject) => {
11
15
  const reader = new FileReader()