Merge pull request #20184 from calixteman/use_cached_annotations

Use the cached annotations when collecting them by types
This commit is contained in:
calixteman 2025-08-24 11:57:17 +02:00 committed by GitHub
commit cfe08bd035
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 1 deletions

View File

@ -80,6 +80,8 @@ import { XRef } from "./xref.js";
const LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
class Page {
#areAnnotationsCached = false;
#resourcesPromise = null;
constructor({
@ -839,6 +841,8 @@ class Page {
return sortedAnnotations;
});
this.#areAnnotationsCached = true;
return shadow(this, "_parsedAnnotations", promise);
}
@ -858,8 +862,20 @@ class Page {
promises,
annotationGlobals
) {
const annots = await this.pdfManager.ensure(this, "annotations");
const { pageIndex } = this;
if (this.#areAnnotationsCached) {
const cachedAnnotations = await this._parsedAnnotations;
for (const { data } of cachedAnnotations) {
if (!types || types.has(data.annotationType)) {
data.pageIndex = pageIndex;
promises.push(Promise.resolve(data));
}
}
return;
}
const annots = await this.pdfManager.ensure(this, "annotations");
for (const annotationRef of annots) {
promises.push(
AnnotationFactory.create(

View File

@ -3256,6 +3256,42 @@ describe("api", function () {
]);
await loadingTask.destroy();
});
it("gets editable annotations after getting annotations on page 13", async function () {
const loadingTask = getDocument(
buildGetDocumentParams("tracemonkey_with_editable_annotations.pdf")
);
const pdfDoc = await loadingTask.promise;
const pdfPage = await pdfDoc.getPage(13);
await pdfPage.getAnnotations();
// Get all the editable annotations in the document.
const editableAnnotations = (
await pdfDoc.getAnnotationsByType(
new Set([
AnnotationType.FREETEXT,
AnnotationType.STAMP,
AnnotationType.INK,
AnnotationType.HIGHLIGHT,
]),
null
)
).map(annotation => ({
id: annotation.id,
subtype: annotation.subtype,
pageIndex: annotation.pageIndex,
}));
editableAnnotations.sort((a, b) => a.id.localeCompare(b.id));
expect(editableAnnotations).toEqual([
{ id: "1000R", subtype: "FreeText", pageIndex: 12 },
{ id: "1001R", subtype: "Stamp", pageIndex: 12 },
{ id: "1011R", subtype: "Stamp", pageIndex: 13 },
{ id: "997R", subtype: "Ink", pageIndex: 13 },
{ id: "998R", subtype: "Highlight", pageIndex: 13 },
]);
await loadingTask.destroy();
});
});
});