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 fetch_with_timeout(url, options, timeout = 6000) {
return Promise.race([
@ -648,12 +656,15 @@ aria-label="Show hidden lines"></button>';
(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() {

View file

@ -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;
}

View file

@ -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"