Skip to content

Commit a62ceed

Browse files
committed
[Editor] Make highlight annotations editable (bug 1883884)
The goal of this patch is to be able to edit existing highlight annotations.
1 parent 0676ea1 commit a62ceed

12 files changed

+574
-28
lines changed

src/core/annotation.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,11 @@ class Annotation {
705705
this.data.pageIndex = params.pageIndex;
706706
}
707707

708+
const it = dict.get("IT");
709+
if (it instanceof Name) {
710+
this.data.it = it.name;
711+
}
712+
708713
this._isOffscreenCanvasSupported =
709714
params.evaluatorOptions.isOffscreenCanvasSupported;
710715
this._fallbackFontDict = null;
@@ -1377,6 +1382,7 @@ class Annotation {
13771382
class AnnotationBorderStyle {
13781383
constructor() {
13791384
this.width = 1;
1385+
this.rawWidth = 1;
13801386
this.style = AnnotationBorderStyleType.SOLID;
13811387
this.dashArray = [3];
13821388
this.horizontalCornerRadius = 0;
@@ -1407,6 +1413,7 @@ class AnnotationBorderStyle {
14071413
}
14081414
if (typeof width === "number") {
14091415
if (width > 0) {
1416+
this.rawWidth = width;
14101417
const maxWidth = (rect[2] - rect[0]) / 2;
14111418
const maxHeight = (rect[3] - rect[1]) / 2;
14121419

@@ -4283,6 +4290,10 @@ class InkAnnotation extends MarkupAnnotation {
42834290
const { dict, xref } = params;
42844291
this.data.annotationType = AnnotationType.INK;
42854292
this.data.inkLists = [];
4293+
this.data.isEditable = !this.data.noHTML && this.data.it === "InkHighlight";
4294+
// We want to be able to add mouse listeners to the annotation.
4295+
this.data.noHTML = false;
4296+
this.data.opacity = dict.get("CA") || 1;
42864297

42874298
const rawInkLists = dict.getArray("InkList");
42884299
if (!Array.isArray(rawInkLists)) {
@@ -4534,6 +4545,10 @@ class HighlightAnnotation extends MarkupAnnotation {
45344545

45354546
const { dict, xref } = params;
45364547
this.data.annotationType = AnnotationType.HIGHLIGHT;
4548+
this.data.isEditable = !this.data.noHTML;
4549+
// We want to be able to add mouse listeners to the annotation.
4550+
this.data.noHTML = false;
4551+
this.data.opacity = dict.get("CA") || 1;
45374552

45384553
const quadPoints = (this.data.quadPoints = getQuadPoints(dict, null));
45394554
if (quadPoints) {
@@ -4573,11 +4588,15 @@ class HighlightAnnotation extends MarkupAnnotation {
45734588
}
45744589
}
45754590

4576-
static createNewDict(annotation, xref, { apRef, ap }) {
4591+
static createNewDict(annotation, xref, { apRef, ap, oldAnnotation }) {
45774592
const { color, opacity, rect, rotation, user, quadPoints } = annotation;
4578-
const highlight = new Dict(xref);
4593+
const highlight = oldAnnotation || new Dict(xref);
45794594
highlight.set("Type", Name.get("Annot"));
45804595
highlight.set("Subtype", Name.get("Highlight"));
4596+
highlight.set(
4597+
oldAnnotation ? "M" : "CreationDate",
4598+
`D:${getModificationDate()}`
4599+
);
45814600
highlight.set("CreationDate", `D:${getModificationDate()}`);
45824601
highlight.set("Rect", rect);
45834602
highlight.set("F", 4);

src/display/annotation_layer.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -2807,7 +2807,11 @@ class InkAnnotationElement extends AnnotationElement {
28072807
// Use the polyline SVG element since it allows us to use coordinates
28082808
// directly and to draw both straight lines and curves.
28092809
this.svgElementName = "svg:polyline";
2810-
this.annotationEditorType = AnnotationEditorType.INK;
2810+
2811+
this.annotationEditorType =
2812+
this.data.it === "InkHighlight"
2813+
? AnnotationEditorType.HIGHLIGHT
2814+
: AnnotationEditorType.INK;
28112815
}
28122816

28132817
render() {
@@ -2857,6 +2861,10 @@ class InkAnnotationElement extends AnnotationElement {
28572861
}
28582862

28592863
this.container.append(svg);
2864+
2865+
if (this._isEditable) {
2866+
this._editOnDoubleClick();
2867+
}
28602868
return this.container;
28612869
}
28622870

@@ -2876,6 +2884,7 @@ class HighlightAnnotationElement extends AnnotationElement {
28762884
ignoreBorder: true,
28772885
createQuadrilaterals: true,
28782886
});
2887+
this.annotationEditorType = AnnotationEditorType.HIGHLIGHT;
28792888
}
28802889

28812890
render() {
@@ -2884,6 +2893,8 @@ class HighlightAnnotationElement extends AnnotationElement {
28842893
}
28852894

28862895
this.container.classList.add("highlightAnnotation");
2896+
this._editOnDoubleClick();
2897+
28872898
return this.container;
28882899
}
28892900
}
@@ -3247,6 +3258,7 @@ class AnnotationLayer {
32473258
export {
32483259
AnnotationLayer,
32493260
FreeTextAnnotationElement,
3261+
HighlightAnnotationElement,
32503262
InkAnnotationElement,
32513263
StampAnnotationElement,
32523264
};

src/display/draw_layer.js

+4
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ class DrawLayer {
225225
this.#mapping.get(id).classList.remove(className);
226226
}
227227

228+
getSVGRoot(id) {
229+
return this.#mapping.get(id);
230+
}
231+
228232
remove(id) {
229233
if (this.#parent === null) {
230234
return;

src/display/editor/annotation_editor_layer.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,10 @@ class AnnotationEditorLayer {
323323
editor = changedAnnotations.get(id);
324324
if (editor) {
325325
this.#uiManager.addChangedExistingAnnotation(editor);
326-
editor.renderAnnotationElement(editable);
327-
editor.show(false);
326+
if (editor.renderAnnotationElement(editable)) {
327+
// Content has changed, so we need to hide the editor.
328+
editor.show(false);
329+
}
328330
}
329331
editable.show();
330332
}

src/display/editor/editor.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,7 @@ class AnnotationEditor {
13671367
data.rect,
13681368
pageHeight
13691369
);
1370+
13701371
editor.x = x / pageWidth;
13711372
editor.y = y / pageHeight;
13721373
editor.width = width / pageWidth;
@@ -1765,7 +1766,7 @@ class AnnotationEditor {
17651766
/**
17661767
* Render an annotation in the annotation layer.
17671768
* @param {Object} annotation
1768-
* @returns {HTMLElement}
1769+
* @returns {HTMLElement|null}
17691770
*/
17701771
renderAnnotationElement(annotation) {
17711772
let content = annotation.container.querySelector(".annotationContent");

0 commit comments

Comments
 (0)