diff --git a/src/dom/common/dom-view.tsx b/src/dom/common/dom-view.tsx index 4dd581d83..fac6b56ef 100644 --- a/src/dom/common/dom-view.tsx +++ b/src/dom/common/dom-view.tsx @@ -1003,6 +1003,7 @@ abstract class DOMView { let f = this._getFocusState(); if (key === 'Escape' && !this._resizingAnnotationID) { + let handled = false; if (this._selectedAnnotationIDs.length) { this._options.onSelectAnnotations([], event); if (this._lastKeyboardFocusedAnnotationID) { @@ -1011,14 +1012,23 @@ abstract class DOMView { ) as HTMLElement | SVGElement | null) ?.focus({ preventScroll: true }); } + handled = true; } else if (f.focusedElement) { f.focusedElement.blur(); + handled = true; + } + const selection = this._iframeWindow.getSelection(); + if (selection && !selection.isCollapsed) { + selection.removeAllRanges(); + handled = true; + } + + // If Escape was handled locally (annotation selection/focus/text selection), + // don't forward it to common shortcuts. + if (handled) { + return; } - this._iframeWindow.getSelection()?.removeAllRanges(); - // The keyboard shortcut was handled here, therefore no need to - // pass it to this._onKeyDown(event) below - return; } else if (key === 'Shift-Tab') { if (f.focusedElement) {