Sfoglia il codice sorgente

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

liutian 4 settimane fa
parent
commit
2dbddcafdf

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

@@ -593,15 +593,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
@@ -925,9 +925,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: {
@@ -937,12 +936,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

@@ -131,13 +131,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()
   }
 
 
@@ -441,9 +441,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: {
@@ -453,12 +452,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

@@ -642,8 +642,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 = []
@@ -1009,6 +1009,24 @@ class ComPDFAnnotationLayer {
       }
     }
   }
+
+  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

@@ -256,13 +256,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 (
@@ -501,9 +501,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: {
@@ -513,12 +512,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

@@ -449,9 +449,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()
@@ -638,14 +638,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: {
@@ -655,12 +652,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

@@ -312,9 +312,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)
   }
 
@@ -339,14 +339,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: {
@@ -356,12 +352,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

@@ -433,13 +433,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) {
@@ -622,10 +622,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: {
@@ -635,12 +633,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
     }
@@ -603,10 +603,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: {
@@ -616,12 +614,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

@@ -73,13 +73,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: {
@@ -89,12 +83,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

@@ -559,21 +559,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) {
@@ -757,10 +762,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: {
@@ -770,12 +773,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

@@ -533,19 +533,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)
   }
@@ -731,10 +736,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: {
@@ -744,12 +747,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

@@ -509,19 +509,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)
   }
@@ -707,10 +712,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: {
@@ -726,6 +729,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

@@ -483,22 +483,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)
   }
@@ -683,10 +687,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: {
@@ -696,12 +698,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

@@ -562,19 +562,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)
   }
@@ -760,10 +765,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: {
@@ -773,12 +776,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

@@ -410,6 +410,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
@@ -426,18 +431,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) {
@@ -619,15 +620,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: {
@@ -640,6 +638,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

@@ -466,15 +466,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
@@ -690,10 +694,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: {
@@ -703,12 +705,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 - 10
packages/core/src/index.js

@@ -469,6 +469,11 @@ class ComPDFKitViewer {
     }
   }
 
+  deleteAnnotations(annotation) {
+    const annotations = Array.isArray(annotation) ? annotation : [annotation]
+    this.pdfViewer.deleteAnnotations(annotations)
+  }
+
   addEvent(eventName, fn) {
     this.eventBus._on(eventName, fn);
   }
@@ -1768,12 +1773,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]
-        }
-        return
       } else {
         if (!this.webviewerServer) {
           annotation.doc = this.doc
@@ -2061,7 +2060,10 @@ class ComPDFKitViewer {
   }
 
   handleCreateSignature(data) {
-    this.addAnnotations(data)
+    this.eventBus.dispatch('annotationChange', {
+      type: 'add',
+      annotation: data
+    })
   }
 
   webViewerDistanceChanged(evt) {
@@ -3057,7 +3059,7 @@ class ComPDFKitViewer {
     }
   }
 
-  handleStamp(data) {
+  async handleStamp(data) {
     if (!data) return
     let imgUi = data.stampContainer
     let pageNum = data.page
@@ -3101,7 +3103,6 @@ class ComPDFKitViewer {
         type: 'stamp',
         date: new Date(),
         pageIndex: pageNum - 1,
-        // rect,
         contents: data.text,
         time: data.time,
         stampColor: data.color,
@@ -3109,7 +3110,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) {

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

@@ -217,7 +217,7 @@ class TextAnnotation extends BaseAnnotation {
   }
 
   handleOutside () {
-    this.hidden = !this.hidden
+    this.hidden = true
     if (this.layer.selectedElementName === this.annotation.name) {
       this.layer.selectedElementName = null
     }
@@ -253,10 +253,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: {
@@ -266,13 +264,20 @@ class TextAnnotation extends BaseAnnotation {
       }
     }
     this.annotation.annotPtr && (annotationData.annotation.annotPtr = this.annotation.annotPtr)
-    if (!event) {
-      annotationData.type = 'empty'
-    }
+
     this.eventBus.dispatch('annotationChange', annotationData)
     this.layer.redrawMarkups({ pageIndex: this.page })
   }
 
+  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

@@ -794,6 +794,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

@@ -1954,6 +1954,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
+      }
+    })
   }
 }