Skip to content

Commit

Permalink
Reduce indentation when parsing new annotations in getOperatorList
Browse files Browse the repository at this point in the history
This code has, over the years, become more complex and less indentation generally helps readability.
  • Loading branch information
Snuffleupagus committed May 25, 2024
1 parent ce52ce0 commit 27436d5
Showing 1 changed file with 60 additions and 59 deletions.
119 changes: 60 additions & 59 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,77 +436,78 @@ class Page {
options: this.evaluatorOptions,
});

const newAnnotationsByPage = !this.xfaFactory
const newAnnotsByPage = !this.xfaFactory
? getNewAnnotationsMap(annotationStorage)
: null;
const newAnnots = newAnnotsByPage?.get(this.pageIndex);
let newAnnotationsPromise = Promise.resolve(null);
let deletedAnnotations = null;

let newAnnotationsPromise = Promise.resolve(null);
if (newAnnotationsByPage) {
const newAnnotations = newAnnotationsByPage.get(this.pageIndex);
if (newAnnotations) {
const annotationGlobalsPromise =
this.pdfManager.ensureDoc("annotationGlobals");
let imagePromises;

// An annotation can contain a reference to a bitmap, but this bitmap
// is defined in another annotation. So we need to find this annotation
// and generate the bitmap.
const missingBitmaps = new Set();
for (const { bitmapId, bitmap } of newAnnotations) {
if (bitmapId && !bitmap && !missingBitmaps.has(bitmapId)) {
missingBitmaps.add(bitmapId);
}
if (newAnnots) {
const annotationGlobalsPromise =
this.pdfManager.ensureDoc("annotationGlobals");
let imagePromises;

// An annotation can contain a reference to a bitmap, but this bitmap
// is defined in another annotation. So we need to find this annotation
// and generate the bitmap.
const missingBitmaps = new Set();
for (const { bitmapId, bitmap } of newAnnots) {
if (bitmapId && !bitmap && !missingBitmaps.has(bitmapId)) {
missingBitmaps.add(bitmapId);
}
}

const { isOffscreenCanvasSupported } = this.evaluatorOptions;
if (missingBitmaps.size > 0) {
const annotationWithBitmaps = newAnnotations.slice();
for (const [key, annotation] of annotationStorage) {
if (!key.startsWith(AnnotationEditorPrefix)) {
continue;
}
if (annotation.bitmap && missingBitmaps.has(annotation.bitmapId)) {
annotationWithBitmaps.push(annotation);
}
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
// 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
);
}
// 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(
newAnnots,
this.xref,
isOffscreenCanvasSupported
);
}

deletedAnnotations = new RefSet();
this.#replaceIdByRef(newAnnotations, deletedAnnotations, null);
deletedAnnotations = new RefSet();
this.#replaceIdByRef(newAnnots, deletedAnnotations, null);

newAnnotationsPromise = annotationGlobalsPromise.then(
annotationGlobals => {
if (!annotationGlobals) {
return null;
}

return AnnotationFactory.printNewAnnotations(
annotationGlobals,
partialEvaluator,
task,
newAnnotations,
imagePromises
);
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);

handler.send("StartRenderPage", {
Expand Down

0 comments on commit 27436d5

Please sign in to comment.