Skip to content

Commit e1cc24c

Browse files
committed
Set the annotationType automatically in the Annotation constructor
Rather than assigning it manually in every extending class, we can utilize the fact that the `AnnotationType`-entries are simply the upper-case version of the `/Subtype` (when it exists) in the Annotation dictionary.
1 parent 61de564 commit e1cc24c

1 file changed

Lines changed: 8 additions & 34 deletions

File tree

src/core/annotation.js

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class AnnotationFactory {
179179

180180
if (
181181
collectByType &&
182-
!collectByType.has(AnnotationType[subtype.toUpperCase()])
182+
!collectByType.has(AnnotationType[subtype?.toUpperCase()])
183183
) {
184184
return null;
185185
}
@@ -660,7 +660,8 @@ function getTransformMatrix(rect, bbox, matrix) {
660660

661661
class Annotation {
662662
constructor(params) {
663-
const { dict, xref, annotationGlobals, ref, orphanFields } = params;
663+
const { annotationGlobals, dict, orphanFields, ref, subtype, xref } =
664+
params;
664665
const parentRef = orphanFields?.get(ref);
665666
if (parentRef) {
666667
dict.set("Parent", parentRef);
@@ -694,6 +695,7 @@ class Annotation {
694695

695696
// Expose public properties using a data object.
696697
this.data = {
698+
annotationType: AnnotationType[subtype?.toUpperCase()],
697699
annotationFlags: this.flags,
698700
borderStyle: this.borderStyle,
699701
color: this.color,
@@ -705,7 +707,7 @@ class Annotation {
705707
id: params.id,
706708
modificationDate: this.modificationDate,
707709
rect: this.rectangle,
708-
subtype: params.subtype,
710+
subtype,
709711
hasOwnCanvas: false,
710712
noRotate: !!(this.flags & AnnotationFlag.NOROTATE),
711713
noHTML: isLocked && isContentLocked,
@@ -1873,7 +1875,6 @@ class WidgetAnnotation extends Annotation {
18731875
const data = this.data;
18741876
this._needAppearances = params.needAppearances;
18751877

1876-
data.annotationType = AnnotationType.WIDGET;
18771878
if (data.fieldName === undefined) {
18781879
data.fieldName = this._constructFieldName(dict);
18791880
}
@@ -3856,7 +3857,6 @@ class TextAnnotation extends MarkupAnnotation {
38563857
this.data.noHTML = false;
38573858

38583859
const { dict } = params;
3859-
this.data.annotationType = AnnotationType.TEXT;
38603860

38613861
if (this.data.hasAppearance) {
38623862
this.data.name = "NoIcon";
@@ -3881,7 +3881,6 @@ class LinkAnnotation extends Annotation {
38813881
super(params);
38823882

38833883
const { dict, annotationGlobals } = params;
3884-
this.data.annotationType = AnnotationType.LINK;
38853884

38863885
// A link is never rendered on the main canvas so we must render its HTML
38873886
// version.
@@ -3913,7 +3912,6 @@ class PopupAnnotation extends Annotation {
39133912
super(params);
39143913

39153914
const { dict } = params;
3916-
this.data.annotationType = AnnotationType.POPUP;
39173915

39183916
// A pop-up is never rendered on the main canvas so we must render its HTML
39193917
// version.
@@ -4006,7 +4004,6 @@ class FreeTextAnnotation extends MarkupAnnotation {
40064004
this.data.noHTML = false;
40074005

40084006
const { annotationGlobals, evaluatorOptions, xref } = params;
4009-
this.data.annotationType = AnnotationType.FREETEXT;
40104007
this.setDefaultAppearance(params);
40114008
this._hasAppearance = !!this.appearance;
40124009

@@ -4235,7 +4232,6 @@ class LineAnnotation extends MarkupAnnotation {
42354232
super(params);
42364233

42374234
const { dict, xref } = params;
4238-
this.data.annotationType = AnnotationType.LINE;
42394235
this.data.hasOwnCanvas = this.data.noRotate;
42404236
this.data.noHTML = false;
42414237

@@ -4303,7 +4299,6 @@ class SquareAnnotation extends MarkupAnnotation {
43034299
super(params);
43044300

43054301
const { dict, xref } = params;
4306-
this.data.annotationType = AnnotationType.SQUARE;
43074302
this.data.hasOwnCanvas = this.data.noRotate;
43084303
this.data.noHTML = false;
43094304

@@ -4352,7 +4347,6 @@ class CircleAnnotation extends MarkupAnnotation {
43524347
super(params);
43534348

43544349
const { dict, xref } = params;
4355-
this.data.annotationType = AnnotationType.CIRCLE;
43564350

43574351
if (!this.appearance) {
43584352
// The default stroke color is black.
@@ -4416,7 +4410,6 @@ class PolylineAnnotation extends MarkupAnnotation {
44164410
super(params);
44174411

44184412
const { dict, xref } = params;
4419-
this.data.annotationType = AnnotationType.POLYLINE;
44204413
this.data.hasOwnCanvas = this.data.noRotate;
44214414
this.data.noHTML = false;
44224415
this.data.vertices = null;
@@ -4502,22 +4495,10 @@ class PolylineAnnotation extends MarkupAnnotation {
45024495
}
45034496
}
45044497

4505-
class PolygonAnnotation extends PolylineAnnotation {
4506-
constructor(params) {
4507-
// Polygons are specific forms of polylines, so reuse their logic.
4508-
super(params);
4509-
4510-
this.data.annotationType = AnnotationType.POLYGON;
4511-
}
4512-
}
4498+
// Polygons are specific forms of polylines, so reuse their logic.
4499+
class PolygonAnnotation extends PolylineAnnotation {}
45134500

4514-
class CaretAnnotation extends MarkupAnnotation {
4515-
constructor(params) {
4516-
super(params);
4517-
4518-
this.data.annotationType = AnnotationType.CARET;
4519-
}
4520-
}
4501+
class CaretAnnotation extends MarkupAnnotation {}
45214502

45224503
class InkAnnotation extends MarkupAnnotation {
45234504
constructor(params) {
@@ -4527,7 +4508,6 @@ class InkAnnotation extends MarkupAnnotation {
45274508
this.data.noHTML = false;
45284509

45294510
const { dict, xref } = params;
4530-
this.data.annotationType = AnnotationType.INK;
45314511
this.data.inkLists = [];
45324512
this.data.isEditable = !this.data.noHTML;
45334513
// We want to be able to add mouse listeners to the annotation.
@@ -4805,7 +4785,6 @@ class HighlightAnnotation extends MarkupAnnotation {
48054785
super(params);
48064786

48074787
const { dict, xref } = params;
4808-
this.data.annotationType = AnnotationType.HIGHLIGHT;
48094788
this.data.isEditable = !this.data.noHTML;
48104789
// We want to be able to add mouse listeners to the annotation.
48114790
this.data.noHTML = false;
@@ -4949,7 +4928,6 @@ class UnderlineAnnotation extends MarkupAnnotation {
49494928
super(params);
49504929

49514930
const { dict, xref } = params;
4952-
this.data.annotationType = AnnotationType.UNDERLINE;
49534931

49544932
const quadPoints = (this.data.quadPoints = getQuadPoints(dict, null));
49554933
if (quadPoints) {
@@ -4989,7 +4967,6 @@ class SquigglyAnnotation extends MarkupAnnotation {
49894967
super(params);
49904968

49914969
const { dict, xref } = params;
4992-
this.data.annotationType = AnnotationType.SQUIGGLY;
49934970

49944971
const quadPoints = (this.data.quadPoints = getQuadPoints(dict, null));
49954972
if (quadPoints) {
@@ -5035,7 +5012,6 @@ class StrikeOutAnnotation extends MarkupAnnotation {
50355012
super(params);
50365013

50375014
const { dict, xref } = params;
5038-
this.data.annotationType = AnnotationType.STRIKEOUT;
50395015

50405016
const quadPoints = (this.data.quadPoints = getQuadPoints(dict, null));
50415017
if (quadPoints) {
@@ -5077,7 +5053,6 @@ class StampAnnotation extends MarkupAnnotation {
50775053
constructor(params) {
50785054
super(params);
50795055

5080-
this.data.annotationType = AnnotationType.STAMP;
50815056
this.data.hasOwnCanvas = this.data.noRotate;
50825057
this.data.isEditable = !this.data.noHTML;
50835058
// We want to be able to add mouse listeners to the annotation.
@@ -5296,7 +5271,6 @@ class FileAttachmentAnnotation extends MarkupAnnotation {
52965271
const { dict } = params;
52975272
const file = new FileSpec(dict.get("FS"));
52985273

5299-
this.data.annotationType = AnnotationType.FILEATTACHMENT;
53005274
this.data.hasOwnCanvas = this.data.noRotate;
53015275
this.data.noHTML = false;
53025276
this.data.file = file.serializable;

0 commit comments

Comments
 (0)