问题描述
我正在努力使用 pspdfkit 来正确创建和显示我的注释。我想像这样创建我的注释:
instance.addEventListener("annotations.create",createdAnnotations => {
const annotation = createdAnnotations
const serializedobject = toSerializableObject(annotation)
const annotationsJson = JSON.stringify(serializedobject)
console.log("clean",JSON.parse(annotationsJson))
dispatch(createPdfNote(annotationsJson,documentID))
.then(
err => {
if (!err) {
setSnackContent({
severity: "success",message: "Annotation successfully created",})
}
})
})
但是在创建时注释缺少注释的类型。
我如何检索它并能够使用 toSerializeObject
来创建我可以在之后重新创建的 JSON?
这是 instance.addEventListener 为我创建的 JSON:
action: null
additionalActions: null
backgroundColor: null
blendMode: "normal"
boundingBox: Object { left: 308,top: 118,width: 82,… }
createdAt: "2021-06-28T07:58:36.255Z"
creatorName: null
customData: null
hidden: false
id: "01F98T156YNAP5N28MAKKQJ2Y4"
isCommentThreadRoot: false
isDrawnNaturally: false
isSignature: false
linewidth: 5
lines: Array [ (12) […] ]
name: "01F98T156YNAP5N28MAKKQJ2Y4"
noprint: false
norotate: false
noView: false
noZoom: false
note: null
opacity: 1
pageIndex: 0
pdfObjectId: null
strokeColor: Object { r: 34,g: 147,b: 251 }
updatedAt: "2021-06-28T07:58:37.181Z"
不幸的是,我必须通过 pspdfkit 函数 toSerializeObject,fromSerializeObject
才能创建和显示注释,但是当我创建它们时它们给我错误 unsupported type
和 unsupported type change
当我尝试重新创建它们,仅使用 JSON.parse / JSON.stringify 并不能解决问题,并且当用户关闭查看器并重新创建时,它不会让我使用 instance.create
将创建的注释呈现给 DOM -再次打开它。
解决方法
所以,文档是垃圾,不会撒谎......该方法实际上是一个函数,检索到的数组是一个对象等。
如何获得我想要的东西的答案是:
instance.addEventListener("annotations.create",createdNote => {
const note = createdNote.get(0)
const serializedObjectNote = toSerializableObject(note)
const noteJson = JSON.stringify(serializedObjectNote)
if (!isLoadingNotes.current) {
dispatch(createPdfNote(noteJson,documentID))
.then(
err => {
if (!err) {
setSnackContent({
severity: "success",message: "Annotation successfully created",})
}
})
}
})