Skip to content

Fix checkOutputInputFocus to detect editable elements in shadow DOM and handle tabbing #259592

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,27 @@ async function webviewPreloads(ctx: PreloadContext) {
return;
}
let lastFocusedOutput: { id: string } | undefined = undefined;
let lastInputFocusedOutput: { id: string } | undefined = undefined;
const handleOutputFocusOut = (event: FocusEvent) => {
const outputFocus = event && getOutputContainer(event);
if (!outputFocus) {
return;
}
// Possible we're tabbing through the elements of the same output.
// Lets see if focus is set back to the same output.
const wasInputFocused = lastInputFocusedOutput?.id === outputFocus.id;
lastFocusedOutput = undefined;
lastInputFocusedOutput = undefined;
setTimeout(() => {
if (lastFocusedOutput?.id === outputFocus.id) {
return;
}
postNotebookMessage<webviewMessages.IOutputBlurMessage>('outputBlur', outputFocus);

// If input was focused and we're leaving the output, send input unfocus message
if (wasInputFocused && lastInputFocusedOutput?.id !== outputFocus.id) {
postNotebookMessage<webviewMessages.IOutputInputFocusMessage>('outputInputFocus', { inputFocused: false, id: outputFocus.id });
}
}, 0);
};

Expand All @@ -196,18 +204,25 @@ async function webviewPreloads(ctx: PreloadContext) {
// check if an input element is focused within the output element
const checkOutputInputFocus = (e: FocusEvent) => {
lastFocusedOutput = getOutputContainer(e);
const activeElement = window.document.activeElement;
if (!activeElement) {
return;
}
let activeElement = window.document.activeElement;

const id = lastFocusedOutput?.id;
if (id && (isEditableElement(activeElement) || activeElement.tagName === 'SELECT')) {
postNotebookMessage<webviewMessages.IOutputInputFocusMessage>('outputInputFocus', { inputFocused: true, id });
// Recursively check for editable elements in shadow DOM
while (activeElement) {
if (isEditableElement(activeElement) || activeElement.tagName === 'SELECT') {
const id = lastFocusedOutput?.id;
if (id) {
lastInputFocusedOutput = lastFocusedOutput;
postNotebookMessage<webviewMessages.IOutputInputFocusMessage>('outputInputFocus', { inputFocused: true, id });
}
return;
}

activeElement.addEventListener('blur', () => {
postNotebookMessage<webviewMessages.IOutputInputFocusMessage>('outputInputFocus', { inputFocused: false, id });
}, { once: true });
// If not editable, check shadow root descendants
if (activeElement.shadowRoot) {
activeElement = activeElement.shadowRoot.activeElement;
} else {
break;
}
}
};

Expand Down