ソースを参照

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

liutian 3 週間 前
コミット
c31719d548

+ 17 - 11
packages/core/src/annotation/freetext.js

@@ -601,15 +601,15 @@ 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
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLine.removeEventListener('dblclick', this.onDbclick)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLine?.removeEventListener('dblclick', this.onDbclick)
+    this.outerLineContainer?.remove()
 
     if (this.layer.selectedElementName === this.annotation.name) {
       this.layer.selectedElementName = null
@@ -887,9 +887,8 @@ export default class Shape extends Base {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.annotationContainer.remove()
-    this.annotation.isDelete = true
+
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -899,12 +898,19 @@ export default class Shape extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   openReply () {
     this.eventBus.dispatch('openAnnotationReply', this.annotation)
   }

+ 16 - 10
packages/core/src/annotation/ink.js

@@ -135,13 +135,13 @@ 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
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLineContainer?.remove()
   }
 
 
@@ -420,9 +420,8 @@ export default class Ink extends Base {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.annotationContainer.remove()
-    this.annotation.isDelete = true
+
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -432,12 +431,19 @@ export default class Ink extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleKeydown (event) {
     // if (event.key === 'Delete' || event.key === 'Backspace') {
     //   this.handleDelete()

+ 20 - 2
packages/core/src/annotation/layer.js

@@ -628,8 +628,8 @@ class ComPDFAnnotationLayer {
 
   emptyAnnotations() {
     const length = this.annotationsArray.length
-    for (let i = 0; i < length; i++) {
-      this.annotationsArray[i].handleDelete()
+    for (let i = length - 1; i >= 0; i--) {
+      this.annotationsArray[i].delete()
     }
 
     this.annotationsArray = []
@@ -956,6 +956,24 @@ class ComPDFAnnotationLayer {
     this.pageDiv.removeEventListener('click', this.onHandleAddSign)
     this.eventBus.dispatch('initCursorStyle')
   }
+
+  getAnnoationIndex(annotation) {
+    const annotationsArray = this.annotationsArray
+    const index = annotationsArray.findIndex(item => item.annotation.name === annotation.name);
+    return index
+  }
+
+  deleteAnnotationInLayer (annotation) {
+    const annotationsArray = this.annotationsArray
+    const index = this.getAnnoationIndex(annotation)
+    index > -1 && annotationsArray.splice(index, 1)
+  }
+
+  deleteAnnotation(annotation) {
+    const annotationsArray = this.annotationsArray
+    const index = this.getAnnoationIndex(annotation)
+    index > -1 && annotationsArray[index].handleDelete()
+  }
 }
 
 export default ComPDFAnnotationLayer;

+ 16 - 10
packages/core/src/annotation/line.js

@@ -254,13 +254,13 @@ 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
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLineContainer?.remove()
   }
 
   getActualPoint (
@@ -475,9 +475,8 @@ export default class Line extends Base {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.annotationContainer.remove()
-    this.annotation.isDelete = true
+
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -487,12 +486,19 @@ export default class Line extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleKeydown (event) {
     // if (event.key === 'Delete' || event.key === 'Backspace') {
     //   this.handleDelete()

+ 14 - 10
packages/core/src/annotation/link.js

@@ -452,9 +452,9 @@ export default class Link extends Base {
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLineContainer?.remove()
 
     if (!this.annotation.url && !this.annotation.destPage) {
       this.handleDelete()
@@ -616,14 +616,11 @@ export default class Link extends Base {
   }
 
   handleDelete (event) {
-    if (!this.annotationContainer) return
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    if (event) this.handleOutside()
-    this.annotationContainer.remove()
 
-    this.annotation.isDelete = true
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -633,12 +630,19 @@ export default class Link extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleMouseUp (event) {
     if (event.button !== 0 && event.type === 'mouseup') return
     if (this.layer.tool) {

+ 13 - 10
packages/core/src/annotation/redaction.js

@@ -314,9 +314,9 @@ export default class Redaction extends Base {
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLineContainer?.remove()
     document.removeEventListener('keydown', this.onKeydown)
   }
 
@@ -341,14 +341,10 @@ export default class Redaction extends Base {
   };
 
   handleDelete (event) {
-    if (!this.annotationContainer) return
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    if (event) this.handleOutside()
-    this.annotationContainer.remove()
 
-    this.annotation.isDelete = true
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -358,12 +354,19 @@ export default class Redaction extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleMouseUp (event) {
     if (event.button !== 0 && event.type === 'mouseup') return
     if (this.layer.tool) {

+ 15 - 10
packages/core/src/annotation/shape.js

@@ -431,13 +431,13 @@ 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
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLineContainer?.remove()
   }
 
   handleMouseDown (event) {
@@ -596,10 +596,8 @@ export default class Shape extends Base {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.annotationContainer.remove()
 
-    this.annotation.isDelete = true
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -609,12 +607,19 @@ export default class Shape extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleMouseUp (event) {
     if (event.button !== 0 && event.type === 'mouseup') return
     if (this.layer.tool) {

+ 12 - 7
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
     }
@@ -580,10 +580,8 @@ export default class Stamp extends Base {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.annotationContainer?.remove()
 
-    this.annotation.isDelete = true
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -593,12 +591,19 @@ export default class Stamp extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleMouseUp (event) {
     if (event.button !== 0 && event.type === 'mouseup') return
     if (this.layer.tool) {

+ 15 - 10
packages/core/src/annotation/text.js

@@ -74,13 +74,7 @@ export default class Text extends Base {
       event.stopPropagation()
     }
 
-    this.textEditorContainer.removeEventListener('click', this.handleStopPropagation)
-    this.textEditorContainer.removeEventListener('paste', this.onPaste)
-    
-
-    this.annotationContainer.remove()
-    this.textEditorContainer.remove()
-    this.annotation.isDelete = true
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -90,12 +84,23 @@ export default class Text extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+
+    this.textEditorContainer?.removeEventListener('click', this.handleStopPropagation)
+    this.textEditorContainer?.removeEventListener('paste', this.onPaste)
+    this.annotationContainer?.remove()
+    this.textEditorContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handlestopPropagation(event) {
     event.stopPropagation()
   }

+ 21 - 11
packages/core/src/form/check_box.js

@@ -540,21 +540,26 @@ export default class CheckBox extends Base {
     }
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   handleOutside () {
     if (!this.hidden && this.layer.annotationStore.selectedElementName === this.annotation.name) {
       document.getElementById('propertyPanelButton').click()
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLineContainer?.remove()
 
-    this.eventBus._on('checkboxPropertyPanelChanged', this.handlePropertyPanel)
+    this.eventBus._off('checkboxPropertyPanelChanged', this.handlePropertyPanel)
   }
 
   handleMouseDown (event) {
@@ -713,10 +718,8 @@ export default class CheckBox extends Base {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.annotationContainer.remove()
 
-    this.annotation.isDelete = true
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -726,12 +729,19 @@ export default class CheckBox extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleMouseUp (event) {
     if (this.layer.tool) {
       event.stopPropagation()

+ 20 - 10
packages/core/src/form/combo_box.js

@@ -527,19 +527,24 @@ export default class ComboBox extends Base {
     }
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   handleOutside () {
     if (!this.hidden && this.layer.annotationStore.selectedElementName === this.annotation.name) {
       document.getElementById('propertyPanelButton').click()
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLineContainer?.remove()
 
     this.eventBus._off('comboBoxPropertyPanelChanged', this.handlePropertyPanel)
   }
@@ -700,10 +705,8 @@ export default class ComboBox extends Base {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.annotationContainer.remove()
 
-    this.annotation.isDelete = true
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -713,12 +716,19 @@ export default class ComboBox extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleMouseUp (event) {
     if (this.layer.tool) {
       event.stopPropagation()

+ 19 - 7
packages/core/src/form/list_box.js

@@ -503,19 +503,24 @@ export default class ListBox extends Base {
     }
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   handleOutside() {
     if (!this.hidden && this.layer.annotationStore.selectedElementName === this.annotation.name) {
       document.getElementById('propertyPanelButton').click()
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLineContainer?.remove()
 
     this.eventBus._off('listBoxPropertyPanelChanged', this.handlePropertyPanel)
   }
@@ -676,10 +681,8 @@ export default class ListBox extends Base {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.annotationContainer.remove()
 
-    this.annotation.isDelete = true
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -695,6 +698,15 @@ export default class ListBox extends Base {
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleMouseUp(event) {
     if (this.layer.tool) {
       event.stopPropagation()

+ 19 - 11
packages/core/src/form/push_button.js

@@ -478,22 +478,26 @@ export default class PushButton extends Base {
     }
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   handleOutside () {
     if (!this.hidden && this.layer.annotationStore.selectedElementName === this.annotation.name) {
       document.getElementById('propertyPanelButton').click()
       
       this.eventBus.dispatch('propertyChange', null)
-      this.layer.selectedElementName = null
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLineContainer?.remove()
 
     this.eventBus._off('pushButtonPropertyPanelChanged', this.handlePropertyPanel)
   }
@@ -654,10 +658,8 @@ export default class PushButton extends Base {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.annotationContainer.remove()
 
-    this.annotation.isDelete = true
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -667,12 +669,18 @@ export default class PushButton extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleMouseUp (event) {
     if (this.layer.tool) {
       event.stopPropagation()

+ 20 - 10
packages/core/src/form/radio_button.js

@@ -543,19 +543,24 @@ export default class RadioButton extends Base {
     }
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   handleOutside () {
     if (!this.hidden && this.layer.annotationStore.selectedElementName === this.annotation.name) {
       document.getElementById('propertyPanelButton').click()
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLineContainer?.remove()
 
     this.eventBus._off('radioButtonPropertyPanelChanged', this.handlePropertyPanel)
   }
@@ -716,10 +721,8 @@ export default class RadioButton extends Base {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.annotationContainer.remove()
 
-    this.annotation.isDelete = true
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -729,12 +732,19 @@ export default class RadioButton extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleMouseUp (event) {
     if (this.layer.tool) {
       event.stopPropagation()

+ 18 - 11
packages/core/src/form/signature_fields.js

@@ -383,6 +383,11 @@ export default class SignatureFields extends Base {
     }
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   handleClick (event) {
     if (!this.hidden || document.fullscreenElement || this.layer.annotationStore.creating || ['redaction', 'remove'].includes(this.layer.tool) || document.querySelector('.compare-document-container').contains(event?.target) || this.layer.toolMode === 'editor' || document.getElementById("sign-image-save")) return
     if (this.isDigital) return
@@ -399,18 +404,14 @@ export default class SignatureFields extends Base {
   }
 
   handleOutside () {
-    if (!this.hidden && this.layer.annotationStore.selectedElementName === this.annotation.name) {
-      this.layer.annotationStore.selectedElementName = null
-    }
-
     this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
     }
-    this.outerLine.removeEventListener('mousedown', this.onMousedown)
-    this.outerLine.removeEventListener('touchstart', this.onMousedown)
-    this.outerLineContainer.remove()
+    this.outerLine?.removeEventListener('mousedown', this.onMousedown)
+    this.outerLine?.removeEventListener('touchstart', this.onMousedown)
+    this.outerLineContainer?.remove()
   }
 
   handleMouseDown (event) {
@@ -567,15 +568,12 @@ export default class SignatureFields extends Base {
 
   handleDelete (data) {
     const event = data instanceof Event ? data : null
-    if (!this.annotationContainer) return
     if (!event && data && data.signaturePtr !== this.annotation.signaturePtr) return
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    if (event) this.handleOutside()
-    this.annotationContainer.remove()
 
-    this.annotation.isDelete = true
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -588,6 +586,15 @@ export default class SignatureFields extends Base {
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleMouseUp (event) {
     if (event.button !== 0 && event.type === 'mouseup') return
     if (this.layer.tool) {

+ 18 - 9
packages/core/src/form/text_field.js

@@ -463,15 +463,19 @@ export default class TextField extends Base {
     }
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   handleOutside () {
     if (!this.hidden && this.layer.annotationStore.selectedElementName === this.annotation.name) {
       document.getElementById('propertyPanelButton').click()
-      
+
       this.eventBus.dispatch('propertyChange', null)
-      this.layer.selectedElementName = null
     }
 
-    this.hidden = !this.hidden
+    this.hidden = true
 
     if (this.layer.annotationStore.selectedElementName === this.annotation.name) {
       this.layer.annotationStore.selectedElementName = null
@@ -659,10 +663,8 @@ export default class TextField extends Base {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.annotationContainer.remove()
 
-    this.annotation.isDelete = true
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -672,12 +674,19 @@ export default class TextField extends Base {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.annotationContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   handleMouseUp (event) {
     if (this.layer.tool) {
       event.stopPropagation()

+ 42 - 11
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);
   }
@@ -1765,13 +1770,6 @@ class ComPDFKitViewer {
           delete this.annotations[annotation.pageIndex]
         }
         this.redactionList = this.redactionList.filter((item) => item.name !== annotation.name)
-      } else if (data.type === 'empty') {
-        annotations.splice(index, 1)
-        if (!annotations.length) {
-          delete this.annotations[annotation.pageIndex]
-        }
-        this.redactionList = this.redactionList.filter((item) => item.pageIndex !== annotation.pageIndex)
-        return
       } else {
         if (!this.webviewerServer) {
           annotation.doc = this.doc
@@ -2059,7 +2057,10 @@ class ComPDFKitViewer {
   }
 
   handleCreateSignature(data) {
-    this.addAnnotations(data)
+    this.eventBus.dispatch('annotationChange', {
+      type: 'add',
+      annotation: data
+    })
   }
 
   webViewerDistanceChanged(evt) {
@@ -3050,7 +3051,7 @@ class ComPDFKitViewer {
     }
   }
 
-  handleStamp(data) {
+  async handleStamp(data) {
     if (!data) return
     let imgUi = data.stampContainer
     let pageNum = data.page
@@ -3094,7 +3095,6 @@ class ComPDFKitViewer {
         type: 'stamp',
         date: new Date(),
         pageIndex: pageNum - 1,
-        // rect,
         contents: data.text,
         time: data.time,
         stampColor: data.color,
@@ -3102,7 +3102,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) {

+ 13 - 7
packages/core/src/markup/text_annotation.js

@@ -216,7 +216,7 @@ class TextAnnotation extends BaseAnnotation {
   }
 
   handleOutside () {
-    this.hidden = !this.hidden
+    this.hidden = true
     if (this.layer.selectedElementName === this.annotation.name) {
       this.layer.selectedElementName = null
     }
@@ -252,9 +252,8 @@ class TextAnnotation extends BaseAnnotation {
     if (this.layer.tool && event) {
       event.stopPropagation()
     }
-    this.handleOutside()
-    this.markupContainer.remove()
-    this.annotation.isDelete = true
+
+    this.delete()
     const annotationData = {
       type: 'delete',
       annotation: {
@@ -264,12 +263,19 @@ class TextAnnotation extends BaseAnnotation {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
   }
 
+  delete () {
+    this.handleOutside()
+    this.markupContainer?.remove()
+    this.annotation.isDelete = true
+    this.layer.deleteAnnotationInLayer({
+      name: this.annotation.name
+    })
+  }
+
   setCss (ele, cssText) {
     if (!ele) return
     if (cssText) {

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

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

@@ -792,6 +792,19 @@ 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) {
+        page.compdfAnnotationLayer.deleteAnnotation(annotation)
+      }
+    }
+  }
+
   _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
+      }
+    })
   }
 }