Prechádzať zdrojové kódy

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

liutian 4 týždňov pred
rodič
commit
373c421251

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

@@ -601,7 +601,7 @@ export default class Shape extends Base {
   }
 
   handleOutside () {
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.selectedElementName === this.annotation.name) {
       this.layer.selectedElementName = null
@@ -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()

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

@@ -135,7 +135,7 @@ export default class Ink extends Base {
   }
 
   handleOutside () {
-    this.hidden = !this.hidden
+    this.hidden = true
     if (this.layer.selectedElementName === this.annotation.name) {
       this.layer.selectedElementName = null
     }
@@ -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()

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

@@ -254,7 +254,7 @@ export default class Line extends Base {
   }
 
   handleOutside () {
-    this.hidden = !this.hidden
+    this.hidden = true
     if (this.layer.selectedElementName === this.annotation.name) {
       this.layer.selectedElementName = null
     }
@@ -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()

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

@@ -431,7 +431,7 @@ export default class Shape extends Base {
   }
 
   handleOutside () {
-    this.hidden = !this.hidden
+    this.hidden = true
     if (this.layer.selectedElementName === this.annotation.name) {
       this.layer.selectedElementName = null
     }
@@ -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()

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

@@ -407,7 +407,7 @@ export default class Stamp extends Base {
   }
 
   handleOutside () {
-    this.hidden = !this.hidden
+    this.hidden = true
     if (this.layer.selectedElementName === this.annotation.name) {
       this.layer.selectedElementName = null
     }
@@ -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()
     }
 

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

@@ -545,7 +545,7 @@ export default class CheckBox extends Base {
       document.getElementById('propertyPanelButton').click()
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
@@ -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()

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

@@ -532,7 +532,7 @@ export default class ComboBox extends Base {
       document.getElementById('propertyPanelButton').click()
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
@@ -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()

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

@@ -508,7 +508,7 @@ export default class ListBox extends Base {
       document.getElementById('propertyPanelButton').click()
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
@@ -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()

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

@@ -486,7 +486,7 @@ export default class PushButton extends Base {
       this.layer.selectedElementName = null
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
@@ -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()

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

@@ -548,7 +548,7 @@ export default class RadioButton extends Base {
       document.getElementById('propertyPanelButton').click()
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
@@ -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()

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

@@ -471,7 +471,7 @@ export default class TextField extends Base {
       this.layer.selectedElementName = null
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
@@ -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()

+ 8 - 3
packages/core/src/pdf_page_view.js

@@ -457,11 +457,16 @@ class PDFPageView {
 
   selectAnnotation(annotation) {
     annotation.name && (this.annotationStore.toSelectAnnotationName = annotation.name)
-    if (!this.compdfAnnotationLayer) return
-    const annotationsArray = this.compdfAnnotationLayer.annotationsArray
+    const compdfAnnotationLayer = this.compdfAnnotationLayer
+    if (!compdfAnnotationLayer) return
+    const annotationsArray = compdfAnnotationLayer.annotationsArray
     const annotationIndex = annotationsArray.findIndex(item => item.annotation.name === this.annotationStore.toSelectAnnotationName)
+    if (compdfAnnotationLayer.selectedElementName) {
+      const selectedAnnotationIndex = annotationsArray.findIndex(item => item.annotation.name === compdfAnnotationLayer.selectedElementName)
+      selectedAnnotationIndex > -1 && compdfAnnotationLayer.annotationsArray[selectedAnnotationIndex].handleOutside()
+    }
     if (annotationIndex > -1) {
-      this.compdfAnnotationLayer.annotationsArray[annotationIndex].selectAnnotation()
+      compdfAnnotationLayer.annotationsArray[annotationIndex].selectAnnotation()
       this.annotationStore.toSelectAnnotationName = null
     }
   }

+ 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
+      }
+    })
   }
 }