Jelajahi Sumber

fix: 修复选中注释及删除注释 API 问题

liutian 3 minggu lalu
induk
melakukan
a576602268

+ 1 - 1
packages/core/src/annotation/freetext.js

@@ -884,7 +884,7 @@ export default class Shape extends Base {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 1 - 1
packages/core/src/annotation/ink.js

@@ -417,7 +417,7 @@ export default class Ink extends Base {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 1 - 1
packages/core/src/annotation/line.js

@@ -472,7 +472,7 @@ export default class Line extends Base {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 1 - 1
packages/core/src/annotation/link.js

@@ -617,7 +617,7 @@ export default class Link extends Base {
 
   handleDelete (event) {
     if (!this.annotationContainer) return
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     if (event) this.handleOutside()

+ 1 - 1
packages/core/src/annotation/redaction.js

@@ -342,7 +342,7 @@ export default class Redaction extends Base {
 
   handleDelete (event) {
     if (!this.annotationContainer) return
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     if (event) this.handleOutside()

+ 1 - 1
packages/core/src/annotation/shape.js

@@ -593,7 +593,7 @@ export default class Shape extends Base {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 1 - 1
packages/core/src/annotation/stamp.js

@@ -577,7 +577,7 @@ export default class Stamp extends Base {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 1 - 1
packages/core/src/annotation/text.js

@@ -70,7 +70,7 @@ export default class Text extends Base {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
 

+ 1 - 1
packages/core/src/form/check_box.js

@@ -710,7 +710,7 @@ export default class CheckBox extends Base {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 1 - 1
packages/core/src/form/combo_box.js

@@ -697,7 +697,7 @@ export default class ComboBox extends Base {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 1 - 1
packages/core/src/form/list_box.js

@@ -673,7 +673,7 @@ export default class ListBox extends Base {
   }
 
   handleDelete(event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 1 - 1
packages/core/src/form/push_button.js

@@ -651,7 +651,7 @@ export default class PushButton extends Base {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 1 - 1
packages/core/src/form/radio_button.js

@@ -713,7 +713,7 @@ export default class RadioButton extends Base {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 1 - 1
packages/core/src/form/text_field.js

@@ -656,7 +656,7 @@ export default class TextField extends Base {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 42 - 4
packages/core/src/index.js

@@ -466,6 +466,11 @@ class ComPDFKitViewer {
     }
   }
 
+  deleteAnnotations(annotation) {
+    const annotations = Array.isArray(annotation) ? annotation : [annotation]
+    this.pdfViewer.deleteAnnotations(annotations)
+  }
+
   addEvent(eventName, fn) {
     this.eventBus._on(eventName, fn);
   }
@@ -2059,7 +2064,10 @@ class ComPDFKitViewer {
   }
 
   handleCreateSignature(data) {
-    this.addAnnotations(data)
+    this.eventBus.dispatch('annotationChange', {
+      type: 'add',
+      annotation: data
+    })
   }
 
   webViewerDistanceChanged(evt) {
@@ -3050,7 +3058,7 @@ class ComPDFKitViewer {
     }
   }
 
-  handleStamp(data) {
+  async handleStamp(data) {
     if (!data) return
     let imgUi = data.stampContainer
     let pageNum = data.page
@@ -3094,7 +3102,6 @@ class ComPDFKitViewer {
         type: 'stamp',
         date: new Date(),
         pageIndex: pageNum - 1,
-        // rect,
         contents: data.text,
         time: data.time,
         stampColor: data.color,
@@ -3102,7 +3109,38 @@ class ComPDFKitViewer {
       }
     }
     annotData.stampShape = 0
-    this.addAnnotations(annotData)
+
+    if (!annotData.rect) {
+      const rect = await this.messageHandler.sendWithPromise('GetStampRect', {
+        doc: this.doc,
+        pagePtr: this.pagesPtr[pageNum - 1].pagePtr,
+        annotation: annotData
+      })
+      if (rect && rect.code) {
+        console.log(result.message)
+        alert(result.message)
+        return false
+      }
+      const pageWidth = pageView.viewport.viewBox[2]
+      const pageHeight = pageView.viewport.viewBox[3]
+      const imgWidth = rect.right - rect.left
+      const imgHeight = rect.bottom - rect.top
+
+      const left = (pageWidth - imgWidth) / 2
+      const top = (pageHeight - imgHeight) / 2
+      const right = (pageWidth + imgWidth) / 2
+      const bottom = (pageHeight + imgHeight) / 2
+      annotData.rect = {
+        left,
+        right,
+        top,
+        bottom
+      }
+    }
+    this.eventBus.dispatch('annotationChange', {
+      type: 'add',
+      annotation: annotData
+    })
   }
 
   async compare(data) {

+ 1 - 1
packages/core/src/markup/text_annotation.js

@@ -249,7 +249,7 @@ class TextAnnotation extends BaseAnnotation {
   }
 
   handleDelete (event) {
-    if (this.layer.tool && event) {
+    if (this.layer.tool && event && event !== 'delete') {
       event.stopPropagation()
     }
     this.handleOutside()

+ 14 - 0
packages/core/src/pdf_viewer.js

@@ -792,6 +792,20 @@ class PDFViewer {
     }
   }
 
+  deleteAnnotations(annotation) {
+    const annotations = Array.isArray(annotation) ? annotation : [annotation]
+    for (let i = 0; i < annotations.length; i++) {
+      const annotation = annotations[i]
+      const pageIndex = annotation.pageIndex
+      const page = this._pages[annotation.pageIndex]
+      if (!page) continue
+      if (page.compdfAnnotationLayer) {
+        const annot = page.compdfAnnotationLayer.annotationsArray.find(item => item.annotation.name === annotation.name);
+        annot && annot.handleDelete('delete')
+      }
+    }
+  }
+
   _resetView() {
     this._pages = [];
     this._currentPageNumber = 1;

+ 26 - 0
packages/core/src/worker/compdfkit_worker.js

@@ -1953,6 +1953,32 @@ class CPDFWorker {
         rect.top
       )
     })
+
+    messageHandler.on('GetStampRect', (data) => {
+      const { doc, pagePtr, annotation } = data
+      const { contents, time, stampShape, stampColor } = annotation
+      const typeInt = AnnotationType[annotation.type.toUpperCase()]
+      const annotPtr = Module._CreateAnnot(doc, pagePtr, typeInt)
+
+      const contentsPtr = stringToNewUTF8(contents)
+      const timePtr = stringToNewUTF8(time)
+
+      Module._SetTextStamp(annotPtr, contentsPtr, timePtr, stampShape, stampColor, 0)
+      const result = licenseErrorMessage('PDF Annotation STAMPS')
+      if (result.code) {
+        return result
+      }
+
+      Module._UpdateAnnotAp(annotPtr, 0)
+      Module._GetAnnotRect(pagePtr, annotPtr)
+      Module._RemoveAnnot(annotPtr)
+      return {
+        left: Rect.Left,
+        top: Rect.Top,
+        right: Rect.Right,
+        bottom: Rect.Bottom
+      }
+    })
   }
 }