@libresign/pdf-elements 0.3.0 → 0.4.0

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.3.0",
4
+ "version": "0.4.0",
5
5
  "author": "LibreCode <contact@librecode.coop>",
6
6
  "private": false,
7
7
  "main": "dist/pdf-elements.umd.js",
@@ -25,6 +25,8 @@ SPDX-License-Identifier: AGPL-3.0-or-later
25
25
  class="overlay"
26
26
  @mousemove="handleMouseMove"
27
27
  @touchmove="handleMouseMove"
28
+ @click="handleOverlayClick(docIndex, pIndex, $event)"
29
+ @touchend="handleOverlayClick(docIndex, pIndex, $event)"
28
30
  >
29
31
  <div
30
32
  v-if="isAddingMode && previewPageDocIndex === docIndex && previewPageIndex === pIndex && previewElement && previewVisible"
@@ -194,6 +196,10 @@ export default {
194
196
  type: Boolean,
195
197
  default: false,
196
198
  },
199
+ emitObjectClick: {
200
+ type: Boolean,
201
+ default: false,
202
+ },
197
203
  ignoreClickOutsideSelectors: {
198
204
  type: Array,
199
205
  default: () => [],
@@ -651,6 +657,48 @@ export default {
651
657
  this.previewVisible = true
652
658
  })
653
659
  },
660
+ handleOverlayClick(docIndex, pageIndex, event) {
661
+ if (!this.emitObjectClick) return
662
+
663
+ const { x: clientX, y: clientY } = this.getPointerPosition(event)
664
+ if (!Number.isFinite(clientX) || !Number.isFinite(clientY)) return
665
+
666
+ this.cachePageBoundsForPage(docIndex, pageIndex)
667
+ const pageRect = this.getPageRect(docIndex, pageIndex)
668
+ if (!pageRect) return
669
+
670
+ const pagesScale = this.getDisplayedPageScale(docIndex, pageIndex) || 1
671
+ const relX = (clientX - pageRect.left) / pagesScale
672
+ const relY = (clientY - pageRect.top) / pagesScale
673
+
674
+ const doc = this.pdfDocuments?.[docIndex]
675
+ const pageObjects = doc?.allObjects?.[pageIndex] || []
676
+ let hitObject = null
677
+
678
+ for (let i = pageObjects.length - 1; i >= 0; i--) {
679
+ const object = pageObjects[i]
680
+ const x = Number(object.x)
681
+ const y = Number(object.y)
682
+ const width = Number(object.width)
683
+ const height = Number(object.height)
684
+ if (![x, y, width, height].every(Number.isFinite)) {
685
+ continue
686
+ }
687
+ if (relX >= x && relX <= x + width && relY >= y && relY <= y + height) {
688
+ hitObject = object
689
+ break
690
+ }
691
+ }
692
+
693
+ if (!hitObject) return
694
+
695
+ this.$emit('pdf-elements:object-click', {
696
+ docIndex,
697
+ pageIndex,
698
+ object: hitObject,
699
+ event,
700
+ })
701
+ },
654
702
 
655
703
  handleKeyDown(event) {
656
704
  if (event.key === 'Escape' && this.isAddingMode) {
@@ -1,11 +1,20 @@
1
1
  // SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
2
2
  // SPDX-License-Identifier: AGPL-3.0-or-later
3
3
 
4
- import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist'
4
+ import { getDocument, GlobalWorkerOptions, PDFWorker } from 'pdfjs-dist'
5
5
  import pdfWorkerCode from 'pdfjs-dist/build/pdf.worker.min.mjs'
6
6
 
7
7
  GlobalWorkerOptions.workerSrc = pdfWorkerCode
8
8
 
9
+ let sharedWorker = null
10
+
11
+ function getSharedWorker() {
12
+ if (!sharedWorker) {
13
+ sharedWorker = new PDFWorker({ name: 'libresign-pdf-worker' })
14
+ }
15
+ return sharedWorker
16
+ }
17
+
9
18
  export function setWorkerPath(path) {
10
19
  GlobalWorkerOptions.workerSrc = path
11
20
  }
@@ -20,5 +29,16 @@ export function readAsArrayBuffer(file) {
20
29
  }
21
30
 
22
31
  export function readAsPDF(file) {
23
- return getDocument(file).promise
32
+ const worker = getSharedWorker()
33
+ const isArrayBuffer = file instanceof ArrayBuffer
34
+ const isView = ArrayBuffer.isView(file)
35
+ const isBlob = typeof Blob !== 'undefined' && file instanceof Blob
36
+
37
+ if (file && typeof file === 'object' && !isArrayBuffer && !isView && !isBlob) {
38
+ return getDocument({ ...file, worker }).promise
39
+ }
40
+ if (typeof file === 'string') {
41
+ return getDocument({ url: file, worker }).promise
42
+ }
43
+ return getDocument({ data: file, worker }).promise
24
44
  }