Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reader context menu keyboard navigation #139

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/common/components/context-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ function ContextMenu({ params, onClose }) {
const [position, setPosition] = useState({ style: {} });
const [update, setUpdate] = useState();
const containerRef = useRef();
const searchStringRef = useRef('');
const searchTimeoutRef = useRef(null);

useEffect(() => {
setUpdate({});
Expand Down Expand Up @@ -134,6 +136,33 @@ function ContextMenu({ params, onClose }) {
}
}

// Select a menuitem from typing, similar to native context menus
function handleKeyDown(event) {
let { key } = event;
// Ignore non-characters
if (key.length !== 1 || !key.match(/\S/)) return;

// Clear search string after 3 seconds of no typing
if (searchTimeoutRef.current) {
clearTimeout(searchTimeoutRef.current);
}
searchTimeoutRef.current = setTimeout(() => {
searchStringRef.current = '';
}, 2000);

// Keep track of what has been typed so far
searchStringRef.current += key.toLowerCase();

// Find all buttons with text that start with what has been typed
let menuOptions = [...document.querySelectorAll(".context-menu button:not([disabled])")];
let candidates = menuOptions.filter(option => option.textContent.toLowerCase().startsWith(searchStringRef.current));

// Focus the first match
if (candidates.length) {
candidates[0].focus();
}
}

function handleClick(event, item) {
onClose();
event.preventDefault();
Expand All @@ -143,7 +172,7 @@ function ContextMenu({ params, onClose }) {

return (
<div className="context-menu-overlay" onPointerDown={handlePointerDown}>
<div ref={containerRef} className="context-menu" style={position.style}>
<div ref={containerRef} className="context-menu" style={position.style} data-tabstop={1} onKeyDown={handleKeyDown}>
{params.itemGroups.map((items, i) => (
<div key={i} className="group">
{items.map((item, i) => {
Expand Down
6 changes: 5 additions & 1 deletion src/common/components/sidebar/annotations-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ const AnnotationsView = memo(React.forwardRef((props, ref) => {
props.onUpdateAnnotations([annotation]);
}, []);

function handlePointerDown() {
function handlePointerDown(event) {
// Clicking on the rendered content when a contextmenu is open will
// lead to pointerup event not firing and the annotation becoming not-selectable
// via keyboard until it is clicked.
if (event.target.classList.contains("context-menu-overlay")) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to reproduce the mentioned issue. Could you help me with the steps to reproduce it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it actually is a bit harder to reproduce now than before. The recent fix to context menu focus regression must have helped.

I did get to reproduce it with an unloaded reader tab. After it loads, click on the annotation ... to open the context menu, then click on the rendered PDF content, and try to navigate to the sidebar with tab. It gets skipped for me. Clicking around resolves the issue, and on subsequent attempts, it doesn't seem to happen again.

Screen.Recording.2024-10-10.at.12.27.36.PM.mov

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be fixed in 97dd5ab. Thanks for helping to reproduce it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's fixed now. That was definitely a less-roundabout way to do it!

I removed this changed from here.

pointerDownRef.current = true;
}

Expand Down
12 changes: 12 additions & 0 deletions src/common/focus-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ export class FocusManager {
e.preventDefault();
this.tabToItem(true);
}
// If context menu is opened and a character is typed, forward the event to context menu
// so it can select a menuitem, similar to how native menus do it.
let contextMenu = document.querySelector('.context-menu');
if (contextMenu && e.key.length == 1 && !e.forwardedToContextMenu && !contextMenu.contains(e.target)) {
let eventCopy = new KeyboardEvent('keydown', {
key: e.key,
bubbles: true
});
// Mark the event to skip it when it gets captured here to avoid infinite loop
eventCopy.forwardedToContextMenu = true;
contextMenu.dispatchEvent(eventCopy);
}
}

tabToGroup(reverse) {
Expand Down
Loading