From c5c31bb9f5dfb10d66f6b6478395e2068d593949 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 4 May 2026 11:48:41 -0700 Subject: [PATCH] Fix global keypress handler with the ACE editor This fixes an issue where pressing `?` inside the ACE editor was opening the help popup. The solution here is to ensure that the global keypress handler works the same as the one used in the search handler. Fortunately other keypresses like left and right were working OK because ACE was doing something like stopPropagation (I'm not sure exactly). Fixes https://github.com/rust-lang/mdBook/issues/3064 --- crates/mdbook-html/front-end/js/book.js | 19 +++++++++++++++---- .../front-end/searcher/searcher.js | 2 +- tests/gui/editor-keypress.goml | 3 ++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/mdbook-html/front-end/js/book.js b/crates/mdbook-html/front-end/js/book.js index 62d7c4cc..ed1dfca4 100644 --- a/crates/mdbook-html/front-end/js/book.js +++ b/crates/mdbook-html/front-end/js/book.js @@ -19,6 +19,14 @@ function playground_text(playground, hidden = true) { } } +/** + * Helper for global keypress handlers so they don't trigger when certain elements are active. + * @returns {boolean} True if the keypress handler should be skipped. + */ +function mdbook_something_else_has_focus(e) { + return /^(?:input|select|textarea)$/i.test(e.target.nodeName); +} + (function codeSnippets() { function fetch_with_timeout(url, options, timeout = 6000) { return Promise.race([ @@ -648,12 +656,15 @@ aria-label="Show hidden lines">'; (function chapterNavigation() { document.addEventListener('keydown', function(e) { - if (e.altKey || e.ctrlKey || e.metaKey) { - return; - } - if (window.search && window.search.hasFocus()) { + if (e.altKey || + e.ctrlKey || + e.metaKey || + window.search && window.search.hasFocus() || + mdbook_something_else_has_focus(e) + ) { return; } + const html = document.querySelector('html'); function next() { diff --git a/crates/mdbook-html/front-end/searcher/searcher.js b/crates/mdbook-html/front-end/searcher/searcher.js index 5c4bab65..ea17ae38 100644 --- a/crates/mdbook-html/front-end/searcher/searcher.js +++ b/crates/mdbook-html/front-end/searcher/searcher.js @@ -357,7 +357,7 @@ window.search = window.search || {}; e.shiftKey || e.target.type === 'textarea' || e.target.type === 'text' || - !hasFocus() && /^(?:input|select|textarea)$/i.test(e.target.nodeName) + !hasFocus() && mdbook_something_else_has_focus(e) ) { return; } diff --git a/tests/gui/editor-keypress.goml b/tests/gui/editor-keypress.goml index 6b1a96fc..f2cfe6f2 100644 --- a/tests/gui/editor-keypress.goml +++ b/tests/gui/editor-keypress.goml @@ -10,9 +10,10 @@ press-key: "s" wait-for: 200 wait-for-css: ("#mdbook-search-wrapper", {"display": "none"}) +// ? inside ACE editor shouldn't show global help popup. press-key: "?" wait-for: 200 -wait-for-css: ("#mdbook-help-container", {"display": "flex"}) +wait-for-css: ("#mdbook-help-container", {"display": "none"}) // Make sure arrow keys don"t navigate. press-key: "ArrowRight"