Browse Source

add: Annotation 点击定位

liutian 8 months ago
parent
commit
725ca0062b

+ 5 - 0
packages/core/src/annotation/freetext.js

@@ -552,6 +552,11 @@ export default class Shape extends Base {
     };
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleContainerClick()
+  }
+
   updateTool () {
     if (!this.initial) return
     

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

@@ -87,6 +87,11 @@ export default class Ink extends Base {
     }
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   updateTool () {
     if (!this.initial) return
     if (this.hidden) {
@@ -108,7 +113,7 @@ export default class Ink extends Base {
 
   handleClick (event) {
     if (this.layer.tool) {
-      event.stopPropagation()
+      event && event.stopPropagation()
     }
     if (!this.hidden || this.layer.annotationStore.creating) return
     this.hidden = false

+ 5 - 0
packages/core/src/annotation/line.js

@@ -238,6 +238,11 @@ export default class Line extends Base {
     this.initial = true
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   handleClick () {
     if (!this.hidden || this.layer.annotationStore.creating) return
     this.hidden = false

+ 5 - 0
packages/core/src/annotation/shape.js

@@ -388,6 +388,11 @@ export default class Shape extends Base {
     };
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   updateTool () {
     if (!this.initial) return
     if (this.hidden) {

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

@@ -377,9 +377,14 @@ export default class Stamp extends Base {
     }
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   handleClick (event) {
     if (this.layer.tool) {
-      event.preventDefault()
+      event && event.preventDefault()
     }
     if (!this.hidden || this.layer.annotationStore.creating) return
     this.hidden = false

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

@@ -296,10 +296,15 @@ export default class Text extends Base {
     }
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   handleClick (event) {
     if (this.layer.addMode || this.layer.annotationStore.creating) return
     if (this.layer.tool) {
-      event.stopPropagation()
+      event && event.stopPropagation()
     }
     if (this.moving) return
     this.eventBus.dispatch('closeTextEditor', event.target.closest('.annotation.text'))

+ 19 - 1
packages/core/src/index.js

@@ -2547,6 +2547,24 @@ class ComPDFKitViewer {
     }
   }
 
+  selectAnnotation(annotation) {
+    const pageIndex = annotation.pageIndex
+    const pageView = this.pdfViewer._pages[pageIndex]
+    if (!pageView) return
+    pageView.selectAnnotation(annotation)
+  }
+
+  jumpToAnnotation(annotation) {
+    const { rect, pageIndex } = annotation
+    const left = rect.left * this.scale
+    const top = rect.top * this.scale
+    const pageView = this.pdfViewer._pages[pageIndex]
+    scrollIntoView(pageView.div, {
+      left,
+      top
+    })
+  }
+
   webViewerPageMode({ mode }) {
     // Handle the 'pagemode' hash parameter, see also `PDFLinkService_setHash`.
     let view;
@@ -4067,7 +4085,7 @@ class ComPDFKitViewer {
 
   setAnnotator(name) {
     this.annotator = name
-  },
+  }
 
   handleTextPopup(tool) {
     this.eventBus.dispatch('textPopupClicked', tool);

+ 5 - 0
packages/core/src/markup/text_annotation.js

@@ -198,6 +198,11 @@ class TextAnnotation extends BaseAnnotation {
     }
   }
 
+  selectAnnotation () {
+    if (this.layer.selectedElementName === this.annotation.name) return
+    this.handleClick()
+  }
+
   handleClick () {
     if (!this.hidden) return
     this.hidden = false

+ 9 - 0
packages/core/src/pdf_page_view.js

@@ -425,6 +425,15 @@ class PDFPageView {
     this.annotationEditorLayer.render(this.viewport, "display");
   }
 
+  selectAnnotation(annotation) {
+    if (!this.compdfAnnotationLayer) return
+    const annotationsArray = this.compdfAnnotationLayer.annotationsArray
+    const annotationIndex = annotationsArray.findIndex(item => item.annotation.name === annotation.name)
+    if (annotationIndex > -1) {
+      this.compdfAnnotationLayer.annotationsArray[annotationIndex].selectAnnotation()
+    }
+  }
+
   renderPage() {
     if (this.renderingState !== RenderingStates.INITIAL) {
       console.error("Must be in new state before drawing");

+ 7 - 6
packages/webview/src/components/AnnotationContainer/AnnotationContent.vue

@@ -192,14 +192,15 @@ const setAnnotationList = ({ annotations }) => {
 }
 core.addEvent('annotationChanged', setAnnotationList)
 
-const goToPage = (item) => {
-  core.pageNumberChanged({
-    value: (item.pageIndex * 1 + 1).toString()
-  })
+const goToPage = (annotation) => {
+  core.jumpToAnnotation(annotation)
+  setTimeout(() => {
+    core.selectAnnotation(annotation)
+  }, 300)
 
-  if (selectedAnnot.value?.name !== item.name) {
+  if (selectedAnnot.value?.name !== annotation.name) {
     replyText.value = ''
-    selectedAnnot.value = item
+    selectedAnnot.value = annotation
     showReplyInput.value = false
   }
 }

+ 4 - 0
packages/webview/src/core/index.js

@@ -69,6 +69,8 @@ import setHighlightLink from './setHighlightLink'
 import setHighlightForm from './setHighlightForm'
 import setAnnotator from './setAnnotator'
 import handleTextPopup from './handleTextPopup'
+import selectAnnotation from './selectAnnotation'
+import jumpToAnnotation from './jumpToAnnotation'
 
 export default {
   getDocumentViewer,
@@ -145,4 +147,6 @@ export default {
   setHighlightForm,
   setAnnotator,
   handleTextPopup,
+  selectAnnotation,
+  jumpToAnnotation,
 }

+ 3 - 0
packages/webview/src/core/jumpToAnnotation.js

@@ -0,0 +1,3 @@
+import core from '@/core'
+
+export default (annotation) => core.getDocumentViewer().jumpToAnnotation(annotation)

+ 3 - 0
packages/webview/src/core/selectAnnotation.js

@@ -0,0 +1,3 @@
+import core from '@/core'
+
+export default (annotation) => core.getDocumentViewer().selectAnnotation(annotation)