Jelajahi Sumber

fix: 导入XFDF问题修复

wzl 9 bulan lalu
induk
melakukan
71c7574b98

+ 33 - 13
packages/core/src/index.js

@@ -567,7 +567,7 @@ class ComPDFKitViewer {
         annotationData = [annotationData]
       }
       for (let i = 0; i < annotationData.length; i++) {
-        annotationData[i].type = type
+        !annotationData[i].type && (annotationData[i].type = type)
         const annotation = this.#formatAnnotation(annotationData[i])
         annotations.push(annotation)
       }
@@ -846,6 +846,7 @@ class ComPDFKitViewer {
     if (annotation.replies?.length) {
       for (const reply of annotation.replies) {
         reply.name = reply.name || uuidv4()
+        reply.mainName = reply.mainName || annotation.name
         reply.contents = reply.content
         delete reply.content
         reply.date = parseAdobePDFTimestamp(rawAnnotation.recentlyModifyDate || rawAnnotation.date || rawAnnotation.creationdate)
@@ -1083,18 +1084,37 @@ class ComPDFKitViewer {
       }
     });
     if (this.webviewerServer) {
-      const annotationsXml = parseAnnotationFromXml(source)
+      const formData = new FormData();
+      formData.append('xfdfFile', data);
+      formData.append('pdfId', this._pdfId);
 
-      const annotations = []
-      for (let i = 0; i < annotationsXml.length; i++) {
-        const annotation = this.#formatAnnotation(annotationsXml[i])
-        annotations.push(annotation)
-      }
-      if (!annotations.length) return
-      this.handleAnnotationChange({
-        type: 'add',
-        annotation: annotations
+      await fetch(this.optionUrl.webviewBaseUrl + this.optionUrl.uploadAnnotationsUrl, {
+        method: 'POST',
+        headers: {
+          'Authorization': this._token
+        },
+        body: formData
       })
+        .then((response) => response.json())
+        .then((res) => {
+          if (res.code === "200") {
+            const data = res.data
+            this._pdfId = data.pdfId
+            const rawAnnotations = data.annotateJson
+            const annotations = this.convertAnnotation(rawAnnotations)
+            if (annotations) {
+              this.annotations = null
+              // this.emptyAnnotations()
+              this.initAnnotations(annotations, true)
+            }
+          }
+        })
+        .catch((error) => {
+          if (error.name !== 'AbortError') {
+            console.log(error)
+            return { error: true, message: error.message }
+          }
+        });
     } else {
       const xfdfBuffer = new Uint8Array(source)
       await this.messageHandler.sendWithPromise('XFDFImportAnnotations', {
@@ -3338,7 +3358,7 @@ class ComPDFKitViewer {
     const { type, annotation, reply, reviewAnnotState, markedAnnotState } = data
     const { pagePtr, annotPtr } = annotation
 
-    const annot = this.annotations[annotation.pageIndex].find(item => item.name === annotation.name)
+    const annot = this.annotations[annotation.pageIndex].find(item => item?.name === annotation.name)
     if (!annot) return
     let annotateHandles = []
     
@@ -3494,7 +3514,7 @@ class ComPDFKitViewer {
             author: this.annotator
           })
   
-          const replayAnnotation = this.annotations[annotation.pageIndex].find(item => item.name === replyItem.name)
+          const replayAnnotation = this.annotations[annotation.pageIndex].find(item => item?.name === replyItem.name)
           replayAnnotation.contents = reply.contents
         }
       }

+ 1 - 1
packages/core/src/ui_utils.js

@@ -1167,7 +1167,7 @@ const ANNOTATION_TYPE = {
 
 const findIndex = (name, items) => {
   const index = items.findIndex((item) => {
-    return item.name === name
+    return item?.name === name
   })
   return index
 }

+ 3 - 3
packages/webview/src/components/AnnotationContainer/AnnotationContent.vue

@@ -122,7 +122,7 @@
                 </n-popover>
                 <div class="re">
                   <Reply />
-                  <span v-if="item.replies?.length">{{ item.replies.filter(reply => reply.contents).length }}</span>
+                  <span v-if="item?.replies?.filter(reply => reply.contents)?.length">{{ item.replies.filter(reply => reply.contents).length }}</span>
                   <Return class="return" @click="selectReturn(item)" />
                 </div>
               </div>
@@ -180,7 +180,7 @@ const setAnnotationList = ({ annotations }) => {
   useDocument.initAnnotations(annotations)
   const instance = getCurrentInstance()
   instance?.proxy?.$forceUpdate()
-  // console.log(annotations && annotations[0])
+  // console.log(annotations)
   
   setTimeout(() => {
     const newObject = findNewObject(oldAnnotations, newAnnotations)
@@ -296,7 +296,7 @@ const addReply = () => {
       contents: replyText.value,
       date: new Date(),
       pageIndex: selectedAnnot.value.pageIndex,
-      index: selectedAnnot.value.replies?.length || 1
+      index: selectedAnnot.value.replies?.length || 0
     }
   }
   core.handleReplyAnnotation(data)