mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
[Editor] Avoid to have duplicated entries in the Annot array when saving an existing and modified annotation
This commit is contained in:
parent
64520a0c63
commit
71479fdd21
4 changed files with 93 additions and 4 deletions
|
@ -258,7 +258,7 @@ class Page {
|
|||
);
|
||||
}
|
||||
|
||||
#replaceIdByRef(annotations, deletedAnnotations) {
|
||||
#replaceIdByRef(annotations, deletedAnnotations, existingAnnotations) {
|
||||
for (const annotation of annotations) {
|
||||
if (annotation.id) {
|
||||
const ref = Ref.fromString(annotation.id);
|
||||
|
@ -270,6 +270,7 @@ class Page {
|
|||
deletedAnnotations.put(ref);
|
||||
continue;
|
||||
}
|
||||
existingAnnotations?.put(ref);
|
||||
annotation.ref = ref;
|
||||
delete annotation.id;
|
||||
}
|
||||
|
@ -295,7 +296,8 @@ class Page {
|
|||
});
|
||||
|
||||
const deletedAnnotations = new RefSet();
|
||||
this.#replaceIdByRef(annotations, deletedAnnotations);
|
||||
const existingAnnotations = new RefSet();
|
||||
this.#replaceIdByRef(annotations, deletedAnnotations, existingAnnotations);
|
||||
|
||||
const pageDict = this.pageDict;
|
||||
const annotationsArray = this.annotations.filter(
|
||||
|
@ -308,7 +310,10 @@ class Page {
|
|||
);
|
||||
|
||||
for (const { ref } of newData.annotations) {
|
||||
annotationsArray.push(ref);
|
||||
// Don't add an existing annotation ref to the annotations array.
|
||||
if (ref instanceof Ref && !existingAnnotations.has(ref)) {
|
||||
annotationsArray.push(ref);
|
||||
}
|
||||
}
|
||||
|
||||
const savedDict = pageDict.get("Annots");
|
||||
|
@ -431,7 +436,7 @@ class Page {
|
|||
const newAnnotations = newAnnotationsByPage.get(this.pageIndex);
|
||||
if (newAnnotations) {
|
||||
deletedAnnotations = new RefSet();
|
||||
this.#replaceIdByRef(newAnnotations, deletedAnnotations);
|
||||
this.#replaceIdByRef(newAnnotations, deletedAnnotations, null);
|
||||
newAnnotationsPromise = AnnotationFactory.printNewAnnotations(
|
||||
partialEvaluator,
|
||||
task,
|
||||
|
|
|
@ -823,6 +823,11 @@ class WorkerMessageHandler {
|
|||
.ensureXRef("trailer")
|
||||
.then(trailer => trailer.get("Prev"));
|
||||
});
|
||||
handler.on("GetAnnotArray", function (data) {
|
||||
return pdfManager.getPage(data.pageIndex).then(function (page) {
|
||||
return page.annotations.map(a => a.toString());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return workerHandlerName;
|
||||
|
|
|
@ -780,6 +780,11 @@ class PDFDocumentProxy {
|
|||
return this._transport.getXRefPrevValue();
|
||||
},
|
||||
});
|
||||
Object.defineProperty(this, "getAnnotArray", {
|
||||
value: pageIndex => {
|
||||
return this._transport.getAnnotArray(pageIndex);
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2405,6 +2410,13 @@ class WorkerTransport {
|
|||
return this.messageHandler.sendWithPromise("GetXRefPrevValue", null);
|
||||
},
|
||||
});
|
||||
Object.defineProperty(this, "getAnnotArray", {
|
||||
value: pageIndex => {
|
||||
return this.messageHandler.sendWithPromise("GetAnnotArray", {
|
||||
pageIndex,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue