Skip to content

Commit 3f0d39b

Browse files
Merge pull request #20244 from calixteman/fix_deleting_popup
[Editor] Fix saving a deleted popup
2 parents 8bd13f7 + 2d5794f commit 3f0d39b

9 files changed

Lines changed: 82 additions & 46 deletions

File tree

src/core/annotation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,8 +1810,8 @@ class MarkupAnnotation extends Annotation {
18101810
});
18111811

18121812
const retRef = { ref: annotationRef };
1813-
if (annotation.popup) {
1814-
const popup = annotation.popup;
1813+
const { popup } = annotation;
1814+
if (popup) {
18151815
if (popup.deleted) {
18161816
annotationDict.delete("Popup");
18171817
annotationDict.delete("Contents");

src/core/primitives.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class Dict {
309309
}
310310

311311
delete(key) {
312-
delete this._map[key];
312+
this._map.delete(key);
313313
}
314314
}
315315

src/display/editor/editor.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,14 @@ class AnnotationEditor {
17491749
* @returns {Object | null}
17501750
*/
17511751
serialize(isForCopying = false, context = null) {
1752-
unreachable("An editor must be serializable");
1752+
return {
1753+
annotationType: this.mode,
1754+
pageIndex: this.pageIndex,
1755+
rect: this.getPDFRect(),
1756+
rotation: this.rotation,
1757+
structTreeParentId: this._structTreeParentId,
1758+
popupRef: this._initialData?.popupRef || "",
1759+
};
17531760
}
17541761

17551762
/**

src/display/editor/freetext.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -833,21 +833,14 @@ class FreeTextEditor extends AnnotationEditor {
833833
return this.serializeDeleted();
834834
}
835835

836-
const rect = this.getPDFRect();
837836
const color = AnnotationEditor._colorManager.convert(
838837
this.isAttachedToDOM ? getComputedStyle(this.editorDiv).color : this.color
839838
);
840-
841-
const serialized = {
842-
annotationType: AnnotationEditorType.FREETEXT,
839+
const serialized = Object.assign(super.serialize(isForCopying), {
843840
color,
844841
fontSize: this.#fontSize,
845842
value: this.#serializeContent(),
846-
pageIndex: this.pageIndex,
847-
rect,
848-
rotation: this.rotation,
849-
structTreeParentId: this._structTreeParentId,
850-
};
843+
});
851844
this.addComment(serialized);
852845

853846
if (isForCopying) {

src/display/editor/highlight.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,23 +1036,17 @@ class HighlightEditor extends AnnotationEditor {
10361036
return this.serializeDeleted();
10371037
}
10381038

1039-
const rect = this.getPDFRect();
10401039
const color = AnnotationEditor._colorManager.convert(
10411040
this._uiManager.getNonHCMColor(this.color)
10421041
);
1043-
1044-
const serialized = {
1045-
annotationType: AnnotationEditorType.HIGHLIGHT,
1042+
const serialized = super.serialize(isForCopying);
1043+
Object.assign(serialized, {
10461044
color,
10471045
opacity: this.opacity,
10481046
thickness: this.#thickness,
10491047
quadPoints: this.#serializeBoxes(),
1050-
outlines: this.#serializeOutlines(rect),
1051-
pageIndex: this.pageIndex,
1052-
rect,
1053-
rotation: this.#getRotation(),
1054-
structTreeParentId: this._structTreeParentId,
1055-
};
1048+
outlines: this.#serializeOutlines(serialized.rect),
1049+
});
10561050
this.addComment(serialized);
10571051

10581052
if (this.annotationElementId && !this.#hasElementChanged(serialized)) {

src/display/editor/ink.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,28 +247,23 @@ class InkEditor extends DrawingEditor {
247247
return this.serializeDeleted();
248248
}
249249

250-
const { lines, points, rect } = this.serializeDraw(isForCopying);
250+
const { lines, points } = this.serializeDraw(isForCopying);
251251
const {
252252
_drawingOptions: {
253253
stroke,
254254
"stroke-opacity": opacity,
255255
"stroke-width": thickness,
256256
},
257257
} = this;
258-
const serialized = {
259-
annotationType: AnnotationEditorType.INK,
258+
const serialized = Object.assign(super.serialize(isForCopying), {
260259
color: AnnotationEditor._colorManager.convert(stroke),
261260
opacity,
262261
thickness,
263262
paths: {
264263
lines,
265264
points,
266265
},
267-
pageIndex: this.pageIndex,
268-
rect,
269-
rotation: this.rotation,
270-
structTreeParentId: this._structTreeParentId,
271-
};
266+
});
272267
this.addComment(serialized);
273268

274269
if (isForCopying) {

src/display/editor/signature.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -374,21 +374,16 @@ class SignatureEditor extends DrawingEditor {
374374
return null;
375375
}
376376

377-
const { lines, points, rect } = this.serializeDraw(isForCopying);
377+
const { lines, points } = this.serializeDraw(isForCopying);
378378
const {
379379
_drawingOptions: { "stroke-width": thickness },
380380
} = this;
381-
const serialized = {
382-
annotationType: AnnotationEditorType.SIGNATURE,
381+
const serialized = Object.assign(super.serialize(isForCopying), {
383382
isSignature: true,
384383
areContours: this.#isExtracted,
385384
color: [0, 0, 0],
386385
thickness: this.#isExtracted ? 0 : thickness,
387-
pageIndex: this.pageIndex,
388-
rect,
389-
rotation: this.rotation,
390-
structTreeParentId: this._structTreeParentId,
391-
};
386+
});
392387
this.addComment(serialized);
393388
if (isForCopying) {
394389
serialized.paths = { lines, points };

src/display/editor/stamp.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -842,15 +842,10 @@ class StampEditor extends AnnotationEditor {
842842
return this.serializeDeleted();
843843
}
844844

845-
const serialized = {
846-
annotationType: AnnotationEditorType.STAMP,
845+
const serialized = Object.assign(super.serialize(isForCopying), {
847846
bitmapId: this.#bitmapId,
848-
pageIndex: this.pageIndex,
849-
rect: this.getPDFRect(),
850-
rotation: this.rotation,
851847
isSvg: this.#isSvg,
852-
structTreeParentId: this._structTreeParentId,
853-
};
848+
});
854849
this.addComment(serialized);
855850

856851
if (isForCopying) {

test/unit/annotation_spec.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5023,6 +5023,63 @@ describe("annotation", function () {
50235023
"endobj\n"
50245024
);
50255025
});
5026+
5027+
it("should update an existing Highlight annotation in removing its popup", async function () {
5028+
const popupRef = Ref.get(111, 0);
5029+
const highlightDict = new Dict();
5030+
highlightDict.set("Type", Name.get("Annot"));
5031+
highlightDict.set("Subtype", Name.get("Highlight"));
5032+
highlightDict.set("Rotate", 0);
5033+
highlightDict.set("CreationDate", "D:20190423");
5034+
highlightDict.set("Contents", "Hello PDF.js World !");
5035+
highlightDict.set("Popup", popupRef);
5036+
const highlightRef = Ref.get(143, 0);
5037+
5038+
const highlightPopupDict = new Dict();
5039+
highlightPopupDict.set("Type", Name.get("Annot"));
5040+
highlightPopupDict.set("Subtype", Name.get("Popup"));
5041+
highlightPopupDict.set("Open", false);
5042+
highlightPopupDict.set("Rect", [1, 2, 3, 4]);
5043+
highlightPopupDict.set("Parent", highlightRef);
5044+
5045+
const xref = (partialEvaluator.xref = new XRefMock([
5046+
{ ref: highlightRef, data: highlightDict },
5047+
{ ref: popupRef, data: highlightPopupDict },
5048+
]));
5049+
const changes = new RefSetCache();
5050+
5051+
const task = new WorkerTask("test Highlight update");
5052+
await AnnotationFactory.saveNewAnnotations(
5053+
partialEvaluator,
5054+
task,
5055+
[
5056+
{
5057+
annotationType: AnnotationEditorType.HIGHLIGHT,
5058+
rotation: 90,
5059+
popup: {
5060+
contents: "",
5061+
deleted: true,
5062+
rect: [1, 2, 3, 4],
5063+
},
5064+
id: "143R",
5065+
ref: highlightRef,
5066+
oldAnnotation: highlightDict,
5067+
popupRef,
5068+
},
5069+
],
5070+
null,
5071+
changes
5072+
);
5073+
5074+
const data = await writeChanges(changes, xref);
5075+
const base = data[0].data.replaceAll(/\(D:\d+\)/g, "(date)");
5076+
expect(base).toEqual(
5077+
"143 0 obj\n" +
5078+
"<< /Type /Annot /Subtype /Highlight /Rotate 90 /CreationDate (date) /M (date) " +
5079+
"/F 4>>\n" +
5080+
"endobj\n"
5081+
);
5082+
});
50265083
});
50275084

50285085
describe("UnderlineAnnotation", function () {

0 commit comments

Comments
 (0)