|
@@ -18,6 +18,7 @@ import { InkSign } from "./ink_sign"
|
|
|
import MessageHandler from "./message_handler"
|
|
|
import JSZip from 'jszip'
|
|
|
import Outline from './Outline'
|
|
|
+import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
|
GlobalWorkerOptions.workerSrc = './lib/pdf.worker.min.js'
|
|
|
const CMAP_URL = './cmaps/'
|
|
@@ -486,8 +487,7 @@ class ComPDFKitViewer {
|
|
|
if (resp.code === "200") {
|
|
|
const data = resp.data
|
|
|
this._pdfId = data.pdfId
|
|
|
- // const rawAnnotations = data.annotateJson && JSON.parse(data.annotateJson)
|
|
|
- const rawAnnotations = data.annotateJson && data.annotateJson
|
|
|
+ const rawAnnotations = data.annotateJson && JSON.parse(data.annotateJson)
|
|
|
annotations = this.convertAnnotation(rawAnnotations)
|
|
|
} else if (resp.code === "319") {
|
|
|
|
|
@@ -560,11 +560,8 @@ class ComPDFKitViewer {
|
|
|
convertAnnotation(rawAnnotations) {
|
|
|
const annotations = []
|
|
|
|
|
|
- const rawAnnotationsData = JSON.parse(rawAnnotations)
|
|
|
-
|
|
|
- console.log(rawAnnotationsData)
|
|
|
- for (const type in rawAnnotationsData) {
|
|
|
- let annotationData = rawAnnotationsData[type]
|
|
|
+ for (const type in rawAnnotations) {
|
|
|
+ let annotationData = rawAnnotations[type]
|
|
|
if (!Array.isArray(annotationData)) {
|
|
|
annotationData = [annotationData]
|
|
|
}
|
|
@@ -680,15 +677,17 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
|
|
|
#formatAnnotation(rawAnnotation) {
|
|
|
- console.log(rawAnnotation)
|
|
|
const annotation = {}
|
|
|
annotation.type = rawAnnotation.type
|
|
|
rawAnnotation.rect && (annotation.rect = this.#formatRect(rawAnnotation))
|
|
|
annotation.date = parseAdobePDFTimestamp(rawAnnotation.creationdate)
|
|
|
annotation.pageIndex = Number(rawAnnotation.page)
|
|
|
- annotation.index && (annotation.index = Number(rawAnnotation.index))
|
|
|
+ annotation.index = Number(rawAnnotation.index)
|
|
|
annotation.contents = rawAnnotation.content || ''
|
|
|
annotation.opacity = round(rawAnnotation.opacity, 2) || 1
|
|
|
+ rawAnnotation.replies && (annotation.replies = rawAnnotation.replies)
|
|
|
+ rawAnnotation.markedAnnotState && (annotation.markedAnnotState = AnnotationStateString[rawAnnotation.markedAnnotState])
|
|
|
+ rawAnnotation.reviewAnnotState && (annotation.reviewAnnotState = AnnotationStateString[rawAnnotation.reviewAnnotState])
|
|
|
|
|
|
switch (rawAnnotation.type) {
|
|
|
case 'freetext':
|
|
@@ -837,6 +836,17 @@ class ComPDFKitViewer {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if (annotation.replies?.length) {
|
|
|
+ for (const reply of annotation.replies) {
|
|
|
+ reply.name = uuidv4()
|
|
|
+ reply.contents = reply.content
|
|
|
+ delete reply.content
|
|
|
+ reply.date = parseAdobePDFTimestamp(rawAnnotation.recentlyModifyDate || rawAnnotation.date || rawAnnotation.creationdate)
|
|
|
+ reply.pageIndex = Number(rawAnnotation.page)
|
|
|
+ delete reply.page
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return annotation
|
|
|
}
|
|
|
|
|
@@ -977,6 +987,7 @@ class ComPDFKitViewer {
|
|
|
typeInt,
|
|
|
scale: this.scale
|
|
|
})
|
|
|
+ if (attr && attr.replies && attr.replies.length) attr.replies.forEach(reply => reply.name = uuidv4())
|
|
|
}
|
|
|
Object.assign(annotation, attr)
|
|
|
annotations.push(annotation)
|
|
@@ -1021,6 +1032,7 @@ class ComPDFKitViewer {
|
|
|
typeInt,
|
|
|
scale: this.scale
|
|
|
})
|
|
|
+ if (attr && attr.replies && attr.replies.length) attr.replies.forEach(reply => reply.name = uuidv4())
|
|
|
}
|
|
|
Object.assign(annotation, attr)
|
|
|
annotations.push(annotation)
|
|
@@ -1454,7 +1466,7 @@ class ComPDFKitViewer {
|
|
|
if (!annotations) return
|
|
|
const index = findIndex(annotation.name, annotations)
|
|
|
if (this.webviewerServer) {
|
|
|
- annotation.index = index + 1
|
|
|
+ annotation.index = index
|
|
|
}
|
|
|
if (data.type === 'delete') {
|
|
|
annotations.splice(index, 1)
|
|
@@ -1619,6 +1631,11 @@ class ComPDFKitViewer {
|
|
|
'destPage' in annotation && (annotation.destPage = annotation.destPage - 1)
|
|
|
delete annotation.color
|
|
|
break
|
|
|
+ case 'reply':
|
|
|
+ annotation.page = annotation.pageIndex
|
|
|
+ annotation.targetPage = annotation.pageIndex + 1
|
|
|
+ delete annotation.pageIndex
|
|
|
+ break
|
|
|
}
|
|
|
|
|
|
if (annotation.operate === 'mod-form') {
|
|
@@ -1629,6 +1646,9 @@ class ComPDFKitViewer {
|
|
|
|
|
|
for (let key in annotation) {
|
|
|
switch (key) {
|
|
|
+ case 'index':
|
|
|
+ annotation.index += 1
|
|
|
+ break
|
|
|
case 'rect':
|
|
|
annotation.rect = this.#formatRectForBackend(annotation.rect, annotation.pageIndex)
|
|
|
break
|
|
@@ -1715,6 +1735,23 @@ class ComPDFKitViewer {
|
|
|
annotation.title && (annotation.value = annotation.title)
|
|
|
delete annotation.title
|
|
|
}
|
|
|
+ break
|
|
|
+ case 'replies':
|
|
|
+ for (const reply of annotation.replies) {
|
|
|
+ reply.page = reply.pageIndex
|
|
|
+ reply.targetPage = reply.pageIndex + 1
|
|
|
+ delete reply.pageIndex
|
|
|
+ }
|
|
|
+ break
|
|
|
+ case 'markedAnnotState':
|
|
|
+ annotation.markedAnnotState = AnnotationState[annotation.markedAnnotState]
|
|
|
+ break
|
|
|
+ case 'reviewAnnotState':
|
|
|
+ annotation.reviewAnnotState = AnnotationState[annotation.reviewAnnotState]
|
|
|
+ break
|
|
|
+ case 'repliesIndex':
|
|
|
+ annotation.repliesIndex += 1
|
|
|
+ break
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -3310,7 +3347,7 @@ class ComPDFKitViewer {
|
|
|
let annotateHandles = []
|
|
|
|
|
|
if (type === 'add') {
|
|
|
- if (reply) {
|
|
|
+ if (reply) { // 添加回复
|
|
|
if (!this.webviewerServer) {
|
|
|
await this.messageHandler.sendWithPromise('CreateReplyAnnotation', {
|
|
|
pagePtr,
|
|
@@ -3321,24 +3358,30 @@ class ComPDFKitViewer {
|
|
|
pagePtr,
|
|
|
annotPtr
|
|
|
})
|
|
|
- if (replies.length) annot.replies = replies
|
|
|
+ if (replies.length) {
|
|
|
+ replies.forEach(reply => reply.name = uuidv4())
|
|
|
+ annot.replies = replies
|
|
|
+ }
|
|
|
if (reviewAnnotState) annot.reviewAnnotState = reviewAnnotState
|
|
|
- if (reviewAnnotState) annot.markedAnnotState = markedAnnotState
|
|
|
+ if (markedAnnotState) annot.markedAnnotState = markedAnnotState
|
|
|
} else {
|
|
|
- console.log(annot, reply)
|
|
|
+ const index = findIndex(annot.name, this.annotations[annotation.pageIndex])
|
|
|
+ annot.index = index
|
|
|
+
|
|
|
annotateHandles.push({
|
|
|
operate: 'add-annot',
|
|
|
type: 'reply',
|
|
|
+ pageIndex: annot.pageIndex,
|
|
|
title: reply.author,
|
|
|
content: reply.contents,
|
|
|
date: reply.date,
|
|
|
- page: annot.pageIndex,
|
|
|
- index: annot.pageIndex + 1,
|
|
|
- targetPage: annot.pageIndex + 1,
|
|
|
+ index: annot.index
|
|
|
})
|
|
|
+ if (!annot.replies) annot.replies = []
|
|
|
+ annot.replies.push(reply)
|
|
|
}
|
|
|
}
|
|
|
- if (reviewAnnotState || markedAnnotState) {
|
|
|
+ if (reviewAnnotState || markedAnnotState) { // 添加有状态回复(首次设置注释状态)
|
|
|
if (!this.webviewerServer) {
|
|
|
const newState = await this.messageHandler.sendWithPromise('CreateReplyStateAnnotation', {
|
|
|
pagePtr,
|
|
@@ -3350,9 +3393,9 @@ class ComPDFKitViewer {
|
|
|
} else {
|
|
|
const data = {
|
|
|
operate: 'mod-annot',
|
|
|
- page: annot.pageIndex,
|
|
|
- index: annot.pageIndex + 1,
|
|
|
- targetPage: annot.pageIndex + 1,
|
|
|
+ index: annot.index,
|
|
|
+ pageIndex: annot.pageIndex,
|
|
|
+ targetPage: annot.pageIndex + 1
|
|
|
}
|
|
|
if (markedAnnotState) {
|
|
|
annot.markedAnnotState = markedAnnotState
|
|
@@ -3372,23 +3415,23 @@ class ComPDFKitViewer {
|
|
|
annotPtr: reply.annotPtr
|
|
|
})
|
|
|
} else {
|
|
|
- console.log(reply)
|
|
|
annotateHandles.push({
|
|
|
operate: 'del-annot',
|
|
|
- name: reply.name,
|
|
|
- index: reply.index,
|
|
|
- targetPage: reply.pageIndex + 1,
|
|
|
- pageIndex: reply.pageIndex
|
|
|
+ index: annot.index,
|
|
|
+ repliesIndex: reply.index,
|
|
|
+ pageIndex: reply.pageIndex,
|
|
|
+ targetPage: reply.pageIndex + 1
|
|
|
})
|
|
|
}
|
|
|
- annot.replies = annot.replies.filter(obj => obj.annotPtr !== reply.annotPtr)
|
|
|
-
|
|
|
- let replyOri = JSON.parse(JSON.stringify(reply))
|
|
|
- console.log(replyOri)
|
|
|
- annotateHandles.push(replyOri)
|
|
|
+ annot.replies = annot.replies.filter(item => item.name !== reply.name)
|
|
|
+ annot.replies.forEach(item => {
|
|
|
+ if (item.index > reply.index) {
|
|
|
+ item.index--
|
|
|
+ }
|
|
|
+ })
|
|
|
|
|
|
} else if (type === 'edit') {
|
|
|
- if (markedAnnotState || reviewAnnotState) {
|
|
|
+ if (markedAnnotState || reviewAnnotState) { // 修改注释状态
|
|
|
if (!this.webviewerServer) {
|
|
|
const newState = await this.messageHandler.sendWithPromise('SetState', {
|
|
|
reviewAnnotState,
|
|
@@ -3396,31 +3439,41 @@ class ComPDFKitViewer {
|
|
|
})
|
|
|
if (markedAnnotState) {
|
|
|
annot.markedAnnotState = newState
|
|
|
- } else if (reviewAnnotState) {
|
|
|
+ }
|
|
|
+ if (reviewAnnotState) {
|
|
|
annot.reviewAnnotState = newState
|
|
|
}
|
|
|
+ } else {
|
|
|
+ if (markedAnnotState) {
|
|
|
+ annot.markedAnnotState = markedAnnotState
|
|
|
+ }
|
|
|
+ if (reviewAnnotState) {
|
|
|
+ annot.reviewAnnotState = reviewAnnotState
|
|
|
+ }
|
|
|
+ let postData = JSON.parse(JSON.stringify(annot))
|
|
|
+ annotateHandles.push(postData)
|
|
|
}
|
|
|
-
|
|
|
- let annotOri = JSON.parse(JSON.stringify(annot))
|
|
|
- console.log(annotOri)
|
|
|
- annotateHandles.push(annotOri)
|
|
|
|
|
|
- } else {
|
|
|
+ } else { // 修改回复内容
|
|
|
if (!this.webviewerServer) {
|
|
|
this.messageHandler.sendWithPromise('EditReplyAnnotation', {
|
|
|
reply
|
|
|
})
|
|
|
}
|
|
|
- const index = annot.replies.findIndex(obj => obj.annotPtr === reply.annotPtr)
|
|
|
+ const index = annot.replies.findIndex(obj => obj.name === reply.name)
|
|
|
const replyItem = annot.replies[index]
|
|
|
annot.replies[index] = {
|
|
|
...replyItem,
|
|
|
...reply
|
|
|
}
|
|
|
-
|
|
|
- let replyOri = JSON.parse(JSON.stringify(reply))
|
|
|
- console.log(replyOri)
|
|
|
- annotateHandles.push(replyOri)
|
|
|
+ annotateHandles.push({
|
|
|
+ operate: 'mod-annot',
|
|
|
+ index: annot.index,
|
|
|
+ repliesIndex: reply.index,
|
|
|
+ pageIndex: reply.pageIndex,
|
|
|
+ targetPage: reply.pageIndex + 1,
|
|
|
+ content: reply.contents
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
// const res = await this.messageHandler.sendWithPromise('GetReplyAnnotation', {
|
|
@@ -3435,13 +3488,12 @@ class ComPDFKitViewer {
|
|
|
this.eventBus.dispatch('annotationChanged', { annotations: this.annotations })
|
|
|
|
|
|
if (this.webviewerServer) {
|
|
|
- console.log(annotateHandles)
|
|
|
- // this.#convertAnnotationsForBackend(annotateHandles)
|
|
|
- // this.#postAnnotations(annotateHandles)
|
|
|
+ this.#convertAnnotationsForBackend(annotateHandles)
|
|
|
+ this.#postAnnotations(annotateHandles)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async #postAnnotations (annotateHandles) {
|
|
|
+ async #postAnnotations(annotateHandles) {
|
|
|
const options = {
|
|
|
method: 'POST',
|
|
|
headers: {
|