diff --git a/src/display/editor/tools.js b/src/display/editor/tools.js index 01cf7a26a2728..d802b4248e077 100644 --- a/src/display/editor/tools.js +++ b/src/display/editor/tools.js @@ -1810,6 +1810,15 @@ class AnnotationEditorUIManager { this.#mode !== AnnotationEditorType.NONE && !this.isEditorHandlingKeyboard ) { + if ( + event.key === "Escape" && + !this.hasSomethingToControl() && + !this.#currentDrawingSession + ) { + this.updateToolbar({ mode: AnnotationEditorType.NONE }); + stopEvent(event); + return; + } AnnotationEditorUIManager._keyboardManager.exec(this, event); } } diff --git a/test/integration/freetext_editor_spec.mjs b/test/integration/freetext_editor_spec.mjs index 79e3fccfe6bf4..458bfdc68ed33 100644 --- a/test/integration/freetext_editor_spec.mjs +++ b/test/integration/freetext_editor_spec.mjs @@ -128,6 +128,28 @@ describe("FreeText Editor", () => { await closePages(pages); }); + it("must turn off the active editor tool with Escape", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await switchToFreeText(page); + + const modeChangedHandle = await waitForAnnotationModeChanged(page); + await page.keyboard.press("Escape"); + await awaitPromise(modeChangedHandle); + + await page.waitForSelector("#editorFreeTextButton:not(.toggled)"); + await page.waitForSelector( + ".annotationEditorLayer:not(.freetextEditing)" + ); + + const mode = await page.evaluate( + () => window.PDFViewerApplication.pdfViewer.annotationEditorMode + ); + expect(mode).withContext(`In ${browserName}`).toEqual(0); + }) + ); + }); + it("must write a string in a FreeText editor", async () => { await Promise.all( pages.map(async ([browserName, page]) => {