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
This commit is contained in:
Eric Huss 2026-05-04 11:48:41 -07:00
parent 4adc4b08ba
commit c5c31bb9f5
3 changed files with 18 additions and 6 deletions

View file

@ -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 codeSnippets() {
function fetch_with_timeout(url, options, timeout = 6000) { function fetch_with_timeout(url, options, timeout = 6000) {
return Promise.race([ return Promise.race([
@ -648,12 +656,15 @@ aria-label="Show hidden lines"></button>';
(function chapterNavigation() { (function chapterNavigation() {
document.addEventListener('keydown', function(e) { document.addEventListener('keydown', function(e) {
if (e.altKey || e.ctrlKey || e.metaKey) { if (e.altKey ||
return; e.ctrlKey ||
} e.metaKey ||
if (window.search && window.search.hasFocus()) { window.search && window.search.hasFocus() ||
mdbook_something_else_has_focus(e)
) {
return; return;
} }
const html = document.querySelector('html'); const html = document.querySelector('html');
function next() { function next() {

View file

@ -357,7 +357,7 @@ window.search = window.search || {};
e.shiftKey || e.shiftKey ||
e.target.type === 'textarea' || e.target.type === 'textarea' ||
e.target.type === 'text' || e.target.type === 'text' ||
!hasFocus() && /^(?:input|select|textarea)$/i.test(e.target.nodeName) !hasFocus() && mdbook_something_else_has_focus(e)
) { ) {
return; return;
} }

View file

@ -10,9 +10,10 @@ press-key: "s"
wait-for: 200 wait-for: 200
wait-for-css: ("#mdbook-search-wrapper", {"display": "none"}) wait-for-css: ("#mdbook-search-wrapper", {"display": "none"})
// ? inside ACE editor shouldn't show global help popup.
press-key: "?" press-key: "?"
wait-for: 200 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. // Make sure arrow keys don"t navigate.
press-key: "ArrowRight" press-key: "ArrowRight"