浏览代码

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

liutian 4 周之前
父节点
当前提交
0b1d157218

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

@@ -594,15 +594,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
@@ -926,9 +926,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: {
@@ -938,12 +937,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

@@ -132,13 +132,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()
   }
 
 
@@ -442,9 +442,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: {
@@ -454,12 +453,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

@@ -257,13 +257,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 (
@@ -502,9 +502,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: {
@@ -514,12 +513,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

@@ -450,9 +450,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()
@@ -639,14 +639,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: {
@@ -656,12 +653,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

@@ -313,9 +313,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)
   }
 
@@ -340,14 +340,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: {
@@ -357,12 +353,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

@@ -434,13 +434,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) {
@@ -623,10 +623,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: {
@@ -636,12 +634,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

@@ -408,7 +408,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
     }
@@ -604,10 +604,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: {
@@ -617,12 +615,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

@@ -556,21 +556,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) {
@@ -754,10 +759,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: {
@@ -767,12 +770,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

@@ -532,19 +532,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)
   }
@@ -730,10 +735,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: {
@@ -743,12 +746,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

@@ -508,19 +508,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)
   }
@@ -706,10 +711,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: {
@@ -725,6 +728,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

@@ -482,22 +482,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)
   }
@@ -682,10 +686,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: {
@@ -695,12 +697,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

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

@@ -407,6 +407,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
@@ -423,18 +428,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) {
@@ -616,15 +617,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: {
@@ -637,6 +635,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
@@ -687,10 +691,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: {
@@ -700,12 +702,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
+      }
+    })
   }
 }