Displaying cmd+k/ctrl+k global search keyboard shortcut suggestion.

This commit is contained in:
Roland 2026-02-23 15:59:40 -07:00
parent ca3589b555
commit 21c96d9501
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
4 changed files with 142 additions and 5 deletions

View file

@ -3,6 +3,7 @@ import {Table, TableBody, TableCell, TableHeader, TableHeaderCell, TableRow} fro
import {Button, Header, Modal} from "./Theme";
import {ThemeContext} from "../contexts/contexts";
import {SHORTCUTS} from "./KeyboardShortcutsProvider";
import {usePlatformModifier} from "../hooks/customHooks";
// Format keyboard keys for display
function KeyboardKey({children}) {
@ -27,9 +28,27 @@ function KeyboardKey({children}) {
}
// Parse shortcut keys string and render as keyboard keys
function ShortcutKeys({keys}) {
function ShortcutKeys({keys, isMac}) {
// Handle multiple key combinations (e.g., "meta+k, ctrl+k")
const combinations = keys.split(', ');
let combinations = keys.split(', ');
// Filter to show only platform-appropriate shortcuts when there are multiple combinations
if (combinations.length > 1) {
const hasMeta = combinations.some(c => c.toLowerCase().includes('meta'));
const hasCtrl = combinations.some(c => c.toLowerCase().includes('ctrl'));
// If we have both meta and ctrl variants, show only the platform-appropriate one
if (hasMeta && hasCtrl) {
combinations = combinations.filter(c => {
const lowerCombo = c.toLowerCase();
if (isMac) {
return lowerCombo.includes('meta');
} else {
return lowerCombo.includes('ctrl');
}
});
}
}
return (
<span>
@ -98,6 +117,7 @@ function groupShortcutsByCategory(shortcuts) {
export default function HelpModal({open, onClose}) {
const {i, t} = useContext(ThemeContext);
const {isMac} = usePlatformModifier();
const groupedShortcuts = groupShortcutsByCategory(SHORTCUTS);
// Order categories
@ -116,7 +136,7 @@ export default function HelpModal({open, onClose}) {
{groupedShortcuts[category].map((shortcut, idx) => (
<TableRow key={idx}>
<TableCell width={6}>
<ShortcutKeys keys={shortcut.keys}/>
<ShortcutKeys keys={shortcut.keys} isMac={isMac}/>
</TableCell>
<TableCell {...t}>{shortcut.description}</TableCell>
</TableRow>

View file

@ -2,6 +2,7 @@ import React, {useState} from "react";
import {Link, Route, Routes, useNavigate} from "react-router";
import {FilesSearchView} from "./Files";
import {useLatestRequest, usePages, useSearchChannels, useSearchDate, useSearchFilter} from "../hooks/customHooks";
import {ShortcutHint} from "./ShortcutHint";
import {ZimSearchView} from "./Zim";
import {searchEstimateFiles, searchEstimateOthers, searchEstimateZims, searchSuggestions} from "../api";
import {filterToMimetypes, fuzzyMatch, normalizeEstimate, SearchResultsInput, TabLinks} from "./Common";
@ -408,6 +409,7 @@ export function SearchIconButton() {
return (
<a className='item' style={{paddingRight: '0.7em'}} onClick={openSearchModal}>
<Icon name='search'/>
<ShortcutHint shortcutKey="K"/>
</a>
);
}

View file

@ -0,0 +1,58 @@
import React from "react";
import {useKeyboardDetected, usePlatformModifier} from "../hooks/customHooks";
/**
* Displays a keyboard shortcut hint that adapts to the user's platform.
* Shows the correct modifier key (Cmd on macOS, Ctrl on Windows/Linux).
* Hidden on touch devices until a hardware keyboard is detected via key press.
*
* @param {string} shortcutKey - The key part of the shortcut (e.g., "K", "P", "/")
* @param {object} style - Optional additional styles to apply
*/
export function ShortcutHint({shortcutKey, style = {}}) {
const {modifierKey} = usePlatformModifier();
const keyboardDetected = useKeyboardDetected();
if (!keyboardDetected) {
return null;
}
const defaultStyle = {
fontSize: '0.75em',
opacity: 0.7,
marginLeft: '0.3em',
};
return (
<span style={{...defaultStyle, ...style}}>
{modifierKey}{shortcutKey}
</span>
);
}
/**
* Displays a simple key hint without a modifier (e.g., for "/" or "?" shortcuts).
* Hidden on touch devices until a hardware keyboard is detected via key press.
*
* @param {string} keyLabel - The key to display (e.g., "?", "/")
* @param {object} style - Optional additional styles to apply
*/
export function KeyHint({keyLabel, style = {}}) {
const keyboardDetected = useKeyboardDetected();
if (!keyboardDetected) {
return null;
}
const defaultStyle = {
fontSize: '0.75em',
opacity: 0.7,
marginLeft: '0.3em',
};
return (
<span style={{...defaultStyle, ...style}}>
{keyLabel}
</span>
);
}

View file

@ -52,11 +52,16 @@ import {useForm} from "./useForm";
export const useIsTouchDevice = () => {
// Detects if the device has a coarse pointer (touch screen).
// Used to disable drag selection on mobile devices.
const [isTouchDevice, setIsTouchDevice] = useState(false);
// Initialize synchronously to avoid race conditions with dependent hooks.
const [isTouchDevice, setIsTouchDevice] = useState(() => {
if (typeof window !== 'undefined') {
return window.matchMedia('(pointer: coarse)').matches;
}
return false;
});
useEffect(() => {
const touchQuery = window.matchMedia('(pointer: coarse)');
setIsTouchDevice(touchQuery.matches);
const handleChange = (e) => setIsTouchDevice(e.matches);
touchQuery.addEventListener('change', handleChange);
@ -67,6 +72,58 @@ export const useIsTouchDevice = () => {
return isTouchDevice;
};
export const usePlatformModifier = () => {
// Detects if the user is on macOS to display the correct keyboard shortcut modifier.
const [isMac, setIsMac] = useState(false);
useEffect(() => {
// Modern API (Chrome 90+)
if (navigator.userAgentData?.platform) {
setIsMac(navigator.userAgentData.platform === 'macOS');
} else {
// Fallback for older browsers
setIsMac(/Mac|iPhone|iPad|iPod/.test(navigator.platform));
}
}, []);
return {
isMac,
modifierKey: isMac ? '\u2318' : 'Ctrl',
modifierKeyLong: isMac ? 'Cmd' : 'Ctrl',
};
};
export const useKeyboardDetected = () => {
// Detects if a hardware keyboard is present by listening for key presses.
// On desktop devices, returns true immediately.
// On touch devices (iPad with Bluetooth keyboard), returns true after the first key press
// outside of input fields. This allows showing keyboard shortcuts to iPad users with
// external keyboards while hiding them for pure touch users.
const [keyboardDetected, setKeyboardDetected] = useState(false);
const isTouchDevice = useIsTouchDevice();
useEffect(() => {
// Desktop devices always have a keyboard.
if (!isTouchDevice) {
setKeyboardDetected(true);
return;
}
const handleKeyDown = (e) => {
// A key press outside of input fields indicates a hardware keyboard is connected.
if (!e.target.matches('input, textarea, [contenteditable]')) {
setKeyboardDetected(true);
document.removeEventListener('keydown', handleKeyDown);
}
};
document.addEventListener('keydown', handleKeyDown);
return () => document.removeEventListener('keydown', handleKeyDown);
}, [isTouchDevice]);
return keyboardDetected;
};
const calculatePage = (offset, limit) => {
return offset && limit ? Math.round((offset / limit) + 1) : 1;
}