Reduce indentation when parsing new annotations in getOperatorList

This code has, over the years, become more complex and less indentation generally helps readability.
This commit is contained in:
Jonas Jenwald 2024-05-25 11:53:59 +02:00
parent ce52ce063e
commit 27436d52b2

View File

@ -436,77 +436,78 @@ class Page {
options: this.evaluatorOptions, options: this.evaluatorOptions,
}); });
const newAnnotationsByPage = !this.xfaFactory const newAnnotsByPage = !this.xfaFactory
? getNewAnnotationsMap(annotationStorage) ? getNewAnnotationsMap(annotationStorage)
: null; : null;
const newAnnots = newAnnotsByPage?.get(this.pageIndex);
let newAnnotationsPromise = Promise.resolve(null);
let deletedAnnotations = null; let deletedAnnotations = null;
let newAnnotationsPromise = Promise.resolve(null); if (newAnnots) {
if (newAnnotationsByPage) { const annotationGlobalsPromise =
const newAnnotations = newAnnotationsByPage.get(this.pageIndex); this.pdfManager.ensureDoc("annotationGlobals");
if (newAnnotations) { let imagePromises;
const annotationGlobalsPromise =
this.pdfManager.ensureDoc("annotationGlobals");
let imagePromises;
// An annotation can contain a reference to a bitmap, but this bitmap // An annotation can contain a reference to a bitmap, but this bitmap
// is defined in another annotation. So we need to find this annotation // is defined in another annotation. So we need to find this annotation
// and generate the bitmap. // and generate the bitmap.
const missingBitmaps = new Set(); const missingBitmaps = new Set();
for (const { bitmapId, bitmap } of newAnnotations) { for (const { bitmapId, bitmap } of newAnnots) {
if (bitmapId && !bitmap && !missingBitmaps.has(bitmapId)) { if (bitmapId && !bitmap && !missingBitmaps.has(bitmapId)) {
missingBitmaps.add(bitmapId); missingBitmaps.add(bitmapId);
}
}
const { isOffscreenCanvasSupported } = this.evaluatorOptions;
if (missingBitmaps.size > 0) {
const annotationWithBitmaps = newAnnots.slice();
for (const [key, annotation] of annotationStorage) {
if (!key.startsWith(AnnotationEditorPrefix)) {
continue;
}
if (annotation.bitmap && missingBitmaps.has(annotation.bitmapId)) {
annotationWithBitmaps.push(annotation);
} }
} }
// The array annotationWithBitmaps cannot be empty: the check above
const { isOffscreenCanvasSupported } = this.evaluatorOptions; // makes sure to have at least one annotation containing the bitmap.
if (missingBitmaps.size > 0) { imagePromises = AnnotationFactory.generateImages(
const annotationWithBitmaps = newAnnotations.slice(); annotationWithBitmaps,
for (const [key, annotation] of annotationStorage) { this.xref,
if (!key.startsWith(AnnotationEditorPrefix)) { isOffscreenCanvasSupported
continue; );
} } else {
if (annotation.bitmap && missingBitmaps.has(annotation.bitmapId)) { imagePromises = AnnotationFactory.generateImages(
annotationWithBitmaps.push(annotation); newAnnots,
} this.xref,
} isOffscreenCanvasSupported
// The array annotationWithBitmaps cannot be empty: the check above
// makes sure to have at least one annotation containing the bitmap.
imagePromises = AnnotationFactory.generateImages(
annotationWithBitmaps,
this.xref,
isOffscreenCanvasSupported
);
} else {
imagePromises = AnnotationFactory.generateImages(
newAnnotations,
this.xref,
isOffscreenCanvasSupported
);
}
deletedAnnotations = new RefSet();
this.#replaceIdByRef(newAnnotations, deletedAnnotations, null);
newAnnotationsPromise = annotationGlobalsPromise.then(
annotationGlobals => {
if (!annotationGlobals) {
return null;
}
return AnnotationFactory.printNewAnnotations(
annotationGlobals,
partialEvaluator,
task,
newAnnotations,
imagePromises
);
}
); );
} }
deletedAnnotations = new RefSet();
this.#replaceIdByRef(newAnnots, deletedAnnotations, null);
newAnnotationsPromise = annotationGlobalsPromise.then(
annotationGlobals => {
if (!annotationGlobals) {
return null;
}
return AnnotationFactory.printNewAnnotations(
annotationGlobals,
partialEvaluator,
task,
newAnnots,
imagePromises
);
}
);
} }
const dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
const pageListPromise = dataPromises.then(([contentStream]) => { const pageListPromise = Promise.all([
contentStreamPromise,
resourcesPromise,
]).then(([contentStream]) => {
const opList = new OperatorList(intent, sink); const opList = new OperatorList(intent, sink);
handler.send("StartRenderPage", { handler.send("StartRenderPage", {