Skip to content

Commit 52c98eb

Browse files
authored
Merge pull request #16659 from calixteman/edit_freetexts
[Editor] Edit an existing FreeText annotation in double-clicking on it (bug 1787298)
2 parents c625230 + 5c5f9af commit 52c98eb

File tree

8 files changed

+86
-14
lines changed

8 files changed

+86
-14
lines changed

src/display/annotation_layer.js

+17
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,20 @@ class AnnotationElement {
601601
triggers.classList.add("highlightArea");
602602
}
603603
}
604+
605+
_editOnDoubleClick() {
606+
const {
607+
annotationEditorType: mode,
608+
data: { id: editId },
609+
} = this;
610+
this.container.addEventListener("dblclick", () => {
611+
this.linkService.eventBus?.dispatch("switchannotationeditormode", {
612+
source: this,
613+
mode,
614+
editId,
615+
});
616+
});
617+
}
604618
}
605619

606620
class LinkAnnotationElement extends AnnotationElement {
@@ -2217,6 +2231,9 @@ class FreeTextAnnotationElement extends AnnotationElement {
22172231
if (!this.data.popupRef) {
22182232
this._createPopup();
22192233
}
2234+
2235+
this._editOnDoubleClick();
2236+
22202237
return this.container;
22212238
}
22222239
}

src/display/editor/editor.js

+5
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,11 @@ class AnnotationEditor {
621621
*/
622622
enableEditing() {}
623623

624+
/**
625+
* The editor is about to be edited.
626+
*/
627+
enterInEditMode() {}
628+
624629
/**
625630
* Get the div which really contains the displayed content.
626631
*/

src/display/editor/freetext.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,18 @@ class FreeTextEditor extends AnnotationEditor {
403403
return this.isInEditMode();
404404
}
405405

406+
/** @inheritdoc */
407+
enterInEditMode() {
408+
this.enableEditMode();
409+
this.editorDiv.focus();
410+
}
411+
406412
/**
407413
* ondblclick callback.
408414
* @param {MouseEvent} event
409415
*/
410416
dblclick(event) {
411-
this.enableEditMode();
412-
this.editorDiv.focus();
417+
this.enterInEditMode();
413418
}
414419

415420
/**
@@ -418,8 +423,7 @@ class FreeTextEditor extends AnnotationEditor {
418423
*/
419424
keydown(event) {
420425
if (event.target === this.div && event.key === "Enter") {
421-
this.enableEditMode();
422-
this.editorDiv.focus();
426+
this.enterInEditMode();
423427
}
424428
}
425429

src/display/editor/tools.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -912,17 +912,28 @@ class AnnotationEditorUIManager {
912912
/**
913913
* Change the editor mode (None, FreeText, Ink, ...)
914914
* @param {number} mode
915+
* @param {string|null} editId
915916
*/
916-
updateMode(mode) {
917+
updateMode(mode, editId = null) {
917918
this.#mode = mode;
918919
if (mode === AnnotationEditorType.NONE) {
919920
this.setEditingState(false);
920921
this.#disableAll();
921-
} else {
922-
this.setEditingState(true);
923-
this.#enableAll();
924-
for (const layer of this.#allLayers.values()) {
925-
layer.updateMode(mode);
922+
return;
923+
}
924+
this.setEditingState(true);
925+
this.#enableAll();
926+
for (const layer of this.#allLayers.values()) {
927+
layer.updateMode(mode);
928+
}
929+
if (!editId) {
930+
return;
931+
}
932+
for (const editor of this.#allEditors.values()) {
933+
if (editor.annotationElementId === editId) {
934+
this.setSelected(editor);
935+
editor.enterInEditMode();
936+
break;
926937
}
927938
}
928939
}

test/integration/freetext_editor_spec.js

+33
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,39 @@ describe("FreeText Editor", () => {
11151115
});
11161116
});
11171117

1118+
describe("FreeText (edit existing in double clicking on it)", () => {
1119+
let pages;
1120+
1121+
beforeAll(async () => {
1122+
pages = await loadAndWait("freetexts.pdf", ".annotationEditorLayer");
1123+
});
1124+
1125+
afterAll(async () => {
1126+
await closePages(pages);
1127+
});
1128+
1129+
it("must move an annotation", async () => {
1130+
await Promise.all(
1131+
pages.map(async ([browserName, page]) => {
1132+
await page.click("[data-annotation-id='26R']", { clickCount: 2 });
1133+
await page.waitForTimeout(10);
1134+
1135+
const [focusedId, editable] = await page.evaluate(() => {
1136+
const el = document.activeElement;
1137+
return [el.id, el.contentEditable];
1138+
});
1139+
expect(focusedId)
1140+
.withContext(`In ${browserName}`)
1141+
.toEqual("pdfjs_internal_editor_0-editor");
1142+
expect(editable).withContext(`In ${browserName}`).toEqual("true");
1143+
1144+
const editorIds = await getEditors(page, "freeText");
1145+
expect(editorIds.length).withContext(`In ${browserName}`).toEqual(6);
1146+
})
1147+
);
1148+
});
1149+
});
1150+
11181151
describe("FreeText with popup", () => {
11191152
let pages;
11201153

web/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2518,7 +2518,7 @@ function webViewerPresentationMode() {
25182518
PDFViewerApplication.requestPresentationMode();
25192519
}
25202520
function webViewerSwitchAnnotationEditorMode(evt) {
2521-
PDFViewerApplication.pdfViewer.annotationEditorMode = evt.mode;
2521+
PDFViewerApplication.pdfViewer.annotationEditorMode = evt;
25222522
}
25232523
function webViewerSwitchAnnotationEditorParams(evt) {
25242524
PDFViewerApplication.pdfViewer.annotationEditorParams = evt;

web/pdf_presentation_mode.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ class PDFPresentationMode {
175175
this.pdfViewer.currentScaleValue = "page-fit";
176176

177177
if (this.#args.annotationEditorMode !== null) {
178-
this.pdfViewer.annotationEditorMode = AnnotationEditorType.NONE;
178+
this.pdfViewer.annotationEditorMode = {
179+
mode: AnnotationEditorType.NONE,
180+
};
179181
}
180182
}, 0);
181183

web/pdf_viewer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2203,7 +2203,7 @@ class PDFViewer {
22032203
/**
22042204
* @param {number} mode - AnnotationEditor mode (None, FreeText, Ink, ...)
22052205
*/
2206-
set annotationEditorMode(mode) {
2206+
set annotationEditorMode({ mode, editId = null }) {
22072207
if (!this.#annotationEditorUIManager) {
22082208
throw new Error(`The AnnotationEditor is not enabled.`);
22092209
}
@@ -2222,7 +2222,7 @@ class PDFViewer {
22222222
mode,
22232223
});
22242224

2225-
this.#annotationEditorUIManager.updateMode(mode);
2225+
this.#annotationEditorUIManager.updateMode(mode, editId);
22262226
}
22272227

22282228
// eslint-disable-next-line accessor-pairs

0 commit comments

Comments
 (0)