Skip to content

Commit

Permalink
Fix control + k keyboard shortcut (#1571)
Browse files Browse the repository at this point in the history
* Fix control + k keyboard shortcut

* Update src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js
  • Loading branch information
gabalafou authored Nov 23, 2023
1 parent 3381187 commit 194f6a0
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ var addEventListenerForSearchKeyboard = () => {
!event.shiftKey &&
!event.altKey &&
// On Mac use ⌘, all other OS use Ctrl
(isMac
(useCommandKey
? event.metaKey && !event.ctrlKey
: !event.metaKey && event.ctrlKey) &&
// Case-insensitive so the shortcut still works with caps lock
Expand All @@ -260,28 +260,23 @@ var addEventListenerForSearchKeyboard = () => {
};

/**
* Find out if we're on a Mac
* If the user is on a Mac, use command (⌘) instead of control (ctrl) key
*
* Note: `navigator.platform` is deprecated; however MDN still recommends using
* it for the one specific use case of detecting whether a keyboard shortcut
* should use control or command:
* https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform#examples
*/
var isMac = (navigator) => {
var platform = "";
if (
typeof navigator.userAgentData !== "undefined" &&
navigator.userAgentData != null
) {
platform = navigator.userAgentData.platform;
} else if (typeof navigator.platform !== "undefined") {
platform = navigator.platform;
}
return /mac.?os/.test(platform.toLowerCase());
};
var useCommandKey =
navigator.platform.indexOf("Mac") === 0 || navigator.platform === "iPhone";

/**
* Change the search hint to `meta key` if we are a Mac
*/

var changeSearchShortcutKey = () => {
let shortcuts = document.querySelectorAll(".search-button__kbd-shortcut");
if (isMac(window.navigator)) {
if (useCommandKey) {
shortcuts.forEach(
(f) => (f.querySelector("kbd.kbd-shortcut__modifier").innerText = "⌘")
);
Expand Down

0 comments on commit 194f6a0

Please sign in to comment.