From 434d3960045bd150a31f382ee2d0dc6fd3fd8078 Mon Sep 17 00:00:00 2001 From: Ramon Mi Date: Wed, 11 Mar 2026 21:25:44 +0800 Subject: [PATCH 1/2] fix(EPUB/Snapshot): allow Esc key to switch off annotation tool --- src/dom/common/dom-view.tsx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/dom/common/dom-view.tsx b/src/dom/common/dom-view.tsx index 4dd581d83..b1739e457 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,22 @@ abstract class DOMView { ) as HTMLElement | SVGElement | null) ?.focus({ preventScroll: true }); } + handled = true; } else if (f.focusedElement) { f.focusedElement.blur(); + handled = true; + } + if (!this._iframeWindow.getSelection()?.isCollapsed) { + this._iframeWindow.getSelection()?.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) { From 77a2c074f5fe741d85ad66cba54d9a9ca27dff13 Mon Sep 17 00:00:00 2001 From: Ramon Mi Date: Wed, 11 Mar 2026 22:07:26 +0800 Subject: [PATCH 2/2] refine conditional logic --- src/dom/common/dom-view.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/dom/common/dom-view.tsx b/src/dom/common/dom-view.tsx index b1739e457..fac6b56ef 100644 --- a/src/dom/common/dom-view.tsx +++ b/src/dom/common/dom-view.tsx @@ -1018,8 +1018,9 @@ abstract class DOMView { f.focusedElement.blur(); handled = true; } - if (!this._iframeWindow.getSelection()?.isCollapsed) { - this._iframeWindow.getSelection()?.removeAllRanges(); + const selection = this._iframeWindow.getSelection(); + if (selection && !selection.isCollapsed) { + selection.removeAllRanges(); handled = true; }