Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Editor] Make highlight annotations editable (bug 1883884) #18427

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,11 @@ class Annotation {
this.data.pageIndex = params.pageIndex;
}

const it = dict.get("IT");
if (it instanceof Name) {
this.data.it = it.name;
}

this._isOffscreenCanvasSupported =
params.evaluatorOptions.isOffscreenCanvasSupported;
this._fallbackFontDict = null;
Expand Down Expand Up @@ -1377,6 +1382,7 @@ class Annotation {
class AnnotationBorderStyle {
constructor() {
this.width = 1;
this.rawWidth = 1;
this.style = AnnotationBorderStyleType.SOLID;
this.dashArray = [3];
this.horizontalCornerRadius = 0;
Expand Down Expand Up @@ -1407,6 +1413,7 @@ class AnnotationBorderStyle {
}
if (typeof width === "number") {
if (width > 0) {
this.rawWidth = width;
const maxWidth = (rect[2] - rect[0]) / 2;
const maxHeight = (rect[3] - rect[1]) / 2;

Expand Down Expand Up @@ -4283,6 +4290,10 @@ class InkAnnotation extends MarkupAnnotation {
const { dict, xref } = params;
this.data.annotationType = AnnotationType.INK;
this.data.inkLists = [];
this.data.isEditable = !this.data.noHTML && this.data.it === "InkHighlight";
// We want to be able to add mouse listeners to the annotation.
this.data.noHTML = false;
calixteman marked this conversation as resolved.
Show resolved Hide resolved
this.data.opacity = dict.get("CA") || 1;

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

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

const quadPoints = (this.data.quadPoints = getQuadPoints(dict, null));
if (quadPoints) {
Expand Down Expand Up @@ -4573,11 +4588,15 @@ class HighlightAnnotation extends MarkupAnnotation {
}
}

static createNewDict(annotation, xref, { apRef, ap }) {
static createNewDict(annotation, xref, { apRef, ap, oldAnnotation }) {
const { color, opacity, rect, rotation, user, quadPoints } = annotation;
const highlight = new Dict(xref);
const highlight = oldAnnotation || new Dict(xref);
highlight.set("Type", Name.get("Annot"));
highlight.set("Subtype", Name.get("Highlight"));
highlight.set(
oldAnnotation ? "M" : "CreationDate",
`D:${getModificationDate()}`
);
highlight.set("CreationDate", `D:${getModificationDate()}`);
highlight.set("Rect", rect);
highlight.set("F", 4);
Expand Down
14 changes: 13 additions & 1 deletion src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2807,7 +2807,11 @@ class InkAnnotationElement extends AnnotationElement {
// Use the polyline SVG element since it allows us to use coordinates
// directly and to draw both straight lines and curves.
this.svgElementName = "svg:polyline";
this.annotationEditorType = AnnotationEditorType.INK;

this.annotationEditorType =
this.data.it === "InkHighlight"
? AnnotationEditorType.HIGHLIGHT
: AnnotationEditorType.INK;
}

render() {
Expand Down Expand Up @@ -2857,6 +2861,10 @@ class InkAnnotationElement extends AnnotationElement {
}

this.container.append(svg);

if (this._isEditable) {
this._editOnDoubleClick();
}
return this.container;
}

Expand All @@ -2876,6 +2884,7 @@ class HighlightAnnotationElement extends AnnotationElement {
ignoreBorder: true,
createQuadrilaterals: true,
});
this.annotationEditorType = AnnotationEditorType.HIGHLIGHT;
}

render() {
Expand All @@ -2884,6 +2893,8 @@ class HighlightAnnotationElement extends AnnotationElement {
}

this.container.classList.add("highlightAnnotation");
this._editOnDoubleClick();

return this.container;
}
}
Expand Down Expand Up @@ -3247,6 +3258,7 @@ class AnnotationLayer {
export {
AnnotationLayer,
FreeTextAnnotationElement,
HighlightAnnotationElement,
InkAnnotationElement,
StampAnnotationElement,
};
4 changes: 4 additions & 0 deletions src/display/draw_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ class DrawLayer {
this.#mapping.get(id).classList.remove(className);
}

getSVGRoot(id) {
return this.#mapping.get(id);
}

remove(id) {
if (this.#parent === null) {
return;
Expand Down
6 changes: 4 additions & 2 deletions src/display/editor/annotation_editor_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,10 @@ class AnnotationEditorLayer {
editor = changedAnnotations.get(id);
if (editor) {
this.#uiManager.addChangedExistingAnnotation(editor);
editor.renderAnnotationElement(editable);
editor.show(false);
if (editor.renderAnnotationElement(editable)) {
// Content has changed, so we need to hide the editor.
editor.show(false);
}
}
editable.show();
}
Expand Down
3 changes: 2 additions & 1 deletion src/display/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,7 @@ class AnnotationEditor {
data.rect,
pageHeight
);

editor.x = x / pageWidth;
editor.y = y / pageHeight;
editor.width = width / pageWidth;
Expand Down Expand Up @@ -1765,7 +1766,7 @@ class AnnotationEditor {
/**
* Render an annotation in the annotation layer.
* @param {Object} annotation
* @returns {HTMLElement}
* @returns {HTMLElement|null}
*/
renderAnnotationElement(annotation) {
let content = annotation.container.querySelector(".annotationContent");
Expand Down
Loading