Skip to content

Commit bc9d34b

Browse files
Merge pull request #20374 from calixteman/bug1995028
[Editor] FreeText annotations aren't supposed to have an attached popup so disable commenting for them (bug 1995028)
2 parents 928a758 + 18a7a82 commit bc9d34b

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

src/display/editor/editor.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class AnnotationEditor {
188188
this.annotationElementId = parameters.annotationElementId || null;
189189
this.creationDate = parameters.creationDate || new Date();
190190
this.modificationDate = parameters.modificationDate || null;
191+
this.canAddComment = true;
191192

192193
const {
193194
rotation,
@@ -1173,7 +1174,7 @@ class AnnotationEditor {
11731174
}
11741175

11751176
addCommentButton() {
1176-
return (this.#comment ||= new Comment(this));
1177+
return this.canAddComment ? (this.#comment ||= new Comment(this)) : null;
11771178
}
11781179

11791180
addStandaloneCommentButton() {
@@ -1991,7 +1992,7 @@ class AnnotationEditor {
19911992
}
19921993

19931994
setCommentButtonStates(options) {
1994-
this.#comment.setCommentButtonStates(options);
1995+
this.#comment?.setCommentButtonStates(options);
19951996
}
19961997

19971998
/**

src/display/editor/freetext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class FreeTextEditor extends AnnotationEditor {
135135
if (!this.annotationElementId) {
136136
this._uiManager.a11yAlert("pdfjs-editor-freetext-added-alert");
137137
}
138+
this.canAddComment = false;
138139
}
139140

140141
/** @inheritdoc */

src/display/editor/toolbar.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,24 +211,35 @@ class EditorToolbar {
211211
async addButton(name, tool) {
212212
switch (name) {
213213
case "colorPicker":
214-
this.addColorPicker(tool);
214+
if (tool) {
215+
this.addColorPicker(tool);
216+
}
215217
break;
216218
case "altText":
217-
await this.addAltText(tool);
219+
if (tool) {
220+
await this.addAltText(tool);
221+
}
218222
break;
219223
case "editSignature":
220-
await this.addEditSignatureButton(tool);
224+
if (tool) {
225+
await this.addEditSignatureButton(tool);
226+
}
221227
break;
222228
case "delete":
223229
this.addDeleteButton();
224230
break;
225231
case "comment":
226-
this.addComment(tool);
232+
if (tool) {
233+
this.addComment(tool);
234+
}
227235
break;
228236
}
229237
}
230238

231239
async addButtonBefore(name, tool, beforeSelector) {
240+
if (!tool && name === "comment") {
241+
return;
242+
}
232243
const beforeElement = this.#buttons.querySelector(beforeSelector);
233244
if (!beforeElement) {
234245
return;

test/integration/comment_spec.mjs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ import {
2727
switchToEditor,
2828
waitAndClick,
2929
waitForSerialized,
30+
waitForTimeout,
3031
} from "./test_utils.mjs";
3132

3233
const switchToHighlight = switchToEditor.bind(null, "Highlight");
3334
const switchToStamp = switchToEditor.bind(null, "Stamp");
3435
const switchToComment = switchToEditor.bind(null, "Comment");
36+
const switchToFreeText = switchToEditor.bind(null, "FreeText");
3537

3638
const highlightSpan = async (page, pageIndex, text) => {
3739
const rect = await getSpanRectFromText(page, pageIndex, text);
@@ -838,4 +840,55 @@ describe("Comment", () => {
838840
);
839841
});
840842
});
843+
844+
describe("FreeText annotation doesn't have a popup (bug 1995028)", () => {
845+
let pages;
846+
847+
beforeEach(async () => {
848+
pages = await loadAndWait(
849+
"empty.pdf",
850+
".annotationEditorLayer",
851+
"page-fit",
852+
null,
853+
{ enableComment: true }
854+
);
855+
});
856+
857+
afterEach(async () => {
858+
await closePages(pages);
859+
});
860+
861+
it("must check that comment button isn't in the annotation toolbar", async () => {
862+
await Promise.all(
863+
pages.map(async ([browserName, page]) => {
864+
await switchToFreeText(page);
865+
866+
const rect = await getRect(page, ".annotationEditorLayer");
867+
const editorSelector = getEditorSelector(0);
868+
const data = "Hello PDF.js World !!";
869+
await page.mouse.click(rect.x + 100, rect.y + 100);
870+
await page.waitForSelector(editorSelector, { visible: true });
871+
await page.type(`${editorSelector} .internal`, data);
872+
await page.keyboard.press("Escape");
873+
874+
await page.waitForSelector(`${editorSelector} .editToolbar`, {
875+
visible: true,
876+
});
877+
878+
// We want to be sure that the comment button isn't rendered.
879+
// eslint-disable-next-line no-restricted-syntax
880+
await waitForTimeout(100);
881+
882+
const hasCommentButton = await page.evaluate(
883+
selector =>
884+
!!document.querySelector(
885+
`${selector} .editToolbar button.comment`
886+
),
887+
editorSelector
888+
);
889+
expect(hasCommentButton).withContext(`In ${browserName}`).toBe(false);
890+
})
891+
);
892+
});
893+
});
841894
});

0 commit comments

Comments
 (0)