Skip to content

Commit f5b5b68

Browse files
authored
Merge pull request #20295 from calixteman/comment_update_position
[Editor] Update the color and the position of the comment button in reading mode they've been modified
2 parents ae3c23e + 0b40bf1 commit f5b5b68

10 files changed

Lines changed: 209 additions & 68 deletions

File tree

src/display/annotation_layer.js

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,31 @@ class AnnotationElement {
202202
}
203203

204204
get hasPopupData() {
205-
return AnnotationElement._hasPopupData(this.data);
205+
return (
206+
AnnotationElement._hasPopupData(this.data) ||
207+
(this.enableComment && !!this.commentText)
208+
);
209+
}
210+
211+
get commentData() {
212+
const { data } = this;
213+
const editor = this.annotationStorage?.getEditor(data.id);
214+
if (editor) {
215+
return editor.getData();
216+
}
217+
return data;
206218
}
207219

208220
get hasCommentButton() {
209221
return this.enableComment && this.hasPopupElement;
210222
}
211223

212224
get commentButtonPosition() {
213-
const { quadPoints, rect } = this.data;
225+
const editor = this.annotationStorage?.getEditor(this.data.id);
226+
if (editor) {
227+
return editor.commentButtonPositionInPage;
228+
}
229+
const { quadPoints, inkLists, rect } = this.data;
214230
let maxX = -Infinity;
215231
let maxY = -Infinity;
216232
if (quadPoints?.length >= 8) {
@@ -224,6 +240,21 @@ class AnnotationElement {
224240
}
225241
return [maxX, maxY];
226242
}
243+
if (inkLists?.length >= 1) {
244+
for (const inkList of inkLists) {
245+
for (let i = 0, ii = inkList.length; i < ii; i += 2) {
246+
if (inkList[i + 1] > maxY) {
247+
maxY = inkList[i + 1];
248+
maxX = inkList[i];
249+
} else if (inkList[i + 1] === maxY) {
250+
maxX = Math.max(maxX, inkList[i]);
251+
}
252+
}
253+
}
254+
if (maxX !== Infinity) {
255+
return [maxX, maxY];
256+
}
257+
}
227258
if (rect) {
228259
return [rect[2], rect[3]];
229260
}
@@ -2380,7 +2411,6 @@ class PopupElement {
23802411
this.#dateObj = PDFDateString.toDateObject(modificationDate);
23812412

23822413
if (commentManager) {
2383-
this.#popupAbortController = new AbortController();
23842414
this.#renderCommentButton();
23852415
} else {
23862416
this.trigger = elements.flatMap(e => e.getElementsToTriggerPopup());
@@ -2457,7 +2487,7 @@ class PopupElement {
24572487
button.ariaHasPopup = "dialog";
24582488
button.ariaControls = "commentPopup";
24592489

2460-
const { signal } = this.#popupAbortController;
2490+
const { signal } = (this.#popupAbortController = new AbortController());
24612491
button.addEventListener("keydown", this.#boundKeyDown, { signal });
24622492
button.addEventListener(
24632493
"click",
@@ -2488,19 +2518,26 @@ class PopupElement {
24882518
},
24892519
{ signal }
24902520
);
2491-
const { style } = button;
2492-
style.left = `calc(${this.#commentButtonPosition[0]}%)`;
2493-
style.top = `calc(${this.#commentButtonPosition[1]}% - var(--comment-button-dim))`;
2494-
if (this.commentButtonColor) {
2495-
style.backgroundColor = this.commentButtonColor;
2496-
}
2521+
this.#updateColor();
2522+
this.#updateCommentButtonPosition();
24972523
parentContainer.after(button);
24982524
}
24992525

2526+
#updateCommentButtonPosition() {
2527+
this.#renderCommentButton();
2528+
const [x, y] = this.#commentButtonPosition;
2529+
const { style } = this.#commentButton;
2530+
style.left = `calc(${x}%)`;
2531+
style.top = `calc(${y}% - var(--comment-button-dim))`;
2532+
}
2533+
2534+
#updateColor() {
2535+
this.#renderCommentButton();
2536+
this.#commentButton.style.backgroundColor = this.commentButtonColor || "";
2537+
}
2538+
25002539
get commentButtonColor() {
2501-
const {
2502-
data: { color, opacity },
2503-
} = this.#firstElement;
2540+
const { color, opacity } = this.#firstElement.commentData;
25042541
if (!color) {
25052542
return null;
25062543
}
@@ -2509,7 +2546,7 @@ class PopupElement {
25092546

25102547
getData() {
25112548
const { richText, color, opacity, creationDate, modificationDate } =
2512-
this.#firstElement.data;
2549+
this.#firstElement.commentData;
25132550
return {
25142551
contentsObj: { str: this.comment },
25152552
richText,
@@ -2743,7 +2780,22 @@ class PopupElement {
27432780

27442781
updateEdited({ rect, popup, deleted }) {
27452782
if (this.#commentManager) {
2746-
this.#commentText = deleted ? null : popup.text;
2783+
if (deleted) {
2784+
this.remove();
2785+
this.#commentText = null;
2786+
} else if (popup) {
2787+
if (popup.deleted) {
2788+
this.remove();
2789+
} else {
2790+
this.#updateColor();
2791+
this.#commentText = popup.text;
2792+
}
2793+
}
2794+
if (rect) {
2795+
this.#commentButtonPosition = null;
2796+
this.#setCommentButtonPosition();
2797+
this.#updateCommentButtonPosition();
2798+
}
27472799
return;
27482800
}
27492801
if (deleted || popup?.deleted) {
@@ -2758,7 +2810,7 @@ class PopupElement {
27582810
if (rect) {
27592811
this.#position = null;
27602812
}
2761-
if (popup) {
2813+
if (popup && popup.text) {
27622814
this.#richText = this.#makePopupContent(popup.text);
27632815
this.#dateObj = PDFDateString.toDateObject(popup.date);
27642816
this.#contentsObj = null;
@@ -3346,31 +3398,6 @@ class InkAnnotationElement extends AnnotationElement {
33463398
addHighlightArea() {
33473399
this.container.classList.add("highlightArea");
33483400
}
3349-
3350-
get commentButtonPosition() {
3351-
const { inkLists, rect } = this.data;
3352-
if (inkLists?.length >= 1) {
3353-
let maxX = -Infinity;
3354-
let maxY = -Infinity;
3355-
for (const inkList of inkLists) {
3356-
for (let i = 0, ii = inkList.length; i < ii; i += 2) {
3357-
if (inkList[i + 1] > maxY) {
3358-
maxY = inkList[i + 1];
3359-
maxX = inkList[i];
3360-
} else if (inkList[i + 1] === maxY) {
3361-
maxX = Math.max(maxX, inkList[i]);
3362-
}
3363-
}
3364-
}
3365-
if (maxX !== Infinity) {
3366-
return [maxX, maxY];
3367-
}
3368-
}
3369-
if (rect) {
3370-
return [rect[2], rect[3]];
3371-
}
3372-
return null;
3373-
}
33743401
}
33753402

33763403
class HighlightAnnotationElement extends AnnotationElement {

src/display/annotation_storage.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ class AnnotationStorage {
268268
return false;
269269
}
270270

271+
getEditor(annotationId) {
272+
return this.#editorsMap?.get(annotationId) || null;
273+
}
274+
271275
/**
272276
* @returns {{ids: Set<string>, hash: string}}
273277
*/

src/display/editor/editor.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,17 @@ class AnnotationEditor {
19301930
return this._uiManager.direction === "ltr" ? [1, 0] : [0, 0];
19311931
}
19321932

1933+
get commentButtonPositionInPage() {
1934+
const {
1935+
commentButtonPosition: [posX, posY],
1936+
} = this;
1937+
const [blX, blY, trX, trY] = this.getPDFRect();
1938+
return [
1939+
AnnotationEditor._round(blX + (trX - blX) * posX),
1940+
AnnotationEditor._round(blY + (trY - blY) * (1 - posY)),
1941+
];
1942+
}
1943+
19331944
get commentButtonColor() {
19341945
return this._uiManager.makeCommentColor(
19351946
this.getNonHCMColor(),

src/display/editor/freetext.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -904,13 +904,13 @@ class FreeTextEditor extends AnnotationEditor {
904904
content.append(div);
905905
}
906906

907-
const params = {
907+
annotation.updateEdited({
908908
rect: this.getPDFRect(),
909-
};
910-
params.popup = this.hasEditedComment
911-
? this.comment
912-
: { text: this.#content };
913-
annotation.updateEdited(params);
909+
popup:
910+
this._uiManager.hasCommentManager() || this.hasEditedComment
911+
? this.comment
912+
: { text: this.#content },
913+
});
914914

915915
return content;
916916
}

src/display/editor/highlight.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,13 @@ class HighlightEditor extends AnnotationEditor {
172172
);
173173
this.#focusOutlines = outlinerForOutline.getOutlines();
174174

175-
// The last point is in the pages coordinate system.
176-
const { firstPoint, lastPoint } = this.#focusOutlines;
175+
const { firstPoint } = this.#highlightOutlines;
177176
this.#firstPoint = [
178177
(firstPoint[0] - this.x) / this.width,
179178
(firstPoint[1] - this.y) / this.height,
180179
];
180+
// The last point is in the pages coordinate system.
181+
const { lastPoint } = this.#focusOutlines;
181182
this.#lastPoint = [
182183
(lastPoint[0] - this.x) / this.width,
183184
(lastPoint[1] - this.y) / this.height,
@@ -268,11 +269,12 @@ class HighlightEditor extends AnnotationEditor {
268269
}
269270
}
270271

271-
const { firstPoint, lastPoint } = this.#focusOutlines;
272+
const { firstPoint } = highlightOutlines;
272273
this.#firstPoint = [
273274
(firstPoint[0] - x) / width,
274275
(firstPoint[1] - y) / height,
275276
];
277+
const { lastPoint } = this.#focusOutlines;
276278
this.#lastPoint = [(lastPoint[0] - x) / width, (lastPoint[1] - y) / height];
277279
}
278280

@@ -1082,13 +1084,10 @@ class HighlightEditor extends AnnotationEditor {
10821084
annotation.hide();
10831085
return null;
10841086
}
1085-
const params = {
1087+
annotation.updateEdited({
10861088
rect: this.getPDFRect(),
1087-
};
1088-
if (this.hasEditedComment) {
1089-
params.popup = this.comment;
1090-
}
1091-
annotation.updateEdited(params);
1089+
popup: this.comment,
1090+
});
10921091

10931092
return null;
10941093
}

src/display/editor/ink.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,12 @@ class InkEditor extends DrawingEditor {
305305
return null;
306306
}
307307
const { points, rect } = this.serializeDraw(/* isForCopying = */ false);
308-
const params = {
308+
annotation.updateEdited({
309309
rect,
310310
thickness: this._drawingOptions["stroke-width"],
311311
points,
312-
};
313-
if (this.hasEditedComment) {
314-
params.popup = this.comment;
315-
}
316-
annotation.updateEdited(params);
312+
popup: this.comment,
313+
});
317314

318315
return null;
319316
}

src/display/editor/stamp.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -942,13 +942,10 @@ class StampEditor extends AnnotationEditor {
942942
annotation.hide();
943943
return null;
944944
}
945-
const params = {
945+
annotation.updateEdited({
946946
rect: this.getPDFRect(),
947-
};
948-
if (this.hasEditedComment) {
949-
params.popup = this.comment;
950-
}
951-
annotation.updateEdited(params);
947+
popup: this.comment,
948+
});
952949

953950
return null;
954951
}

0 commit comments

Comments
 (0)