Skip to content

Commit 50221d1

Browse files
committed
use focusin event
1 parent 55799e3 commit 50221d1

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import type { Event } from 'vs/base/common/event';
77
import type { IDisposable } from 'vs/base/common/lifecycle';
8-
import * as webviewMessages from 'vs/workbench/contrib/notebook/browser/view/renderers/webviewMessages';
8+
import type * as webviewMessages from 'vs/workbench/contrib/notebook/browser/view/renderers/webviewMessages';
99
import type { NotebookCellMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1010
import type * as rendererApi from 'vscode-notebook-renderer';
1111

@@ -94,6 +94,8 @@ async function webviewPreloads(ctx: PreloadContext) {
9494
let currentRenderOptions = ctx.renderOptions;
9595
const settingChange: EmitterLike<RenderOptions> = createEmitter<RenderOptions>();
9696

97+
let isInputElementFocused: boolean | undefined = undefined;
98+
9799
const acquireVsCodeApi = globalThis.acquireVsCodeApi;
98100
const vscode = acquireVsCodeApi();
99101
delete (globalThis as any).acquireVsCodeApi;
@@ -138,16 +140,19 @@ async function webviewPreloads(ctx: PreloadContext) {
138140
};
139141

140142
// check if an input element is focused within the output element
141-
const handleOutputFocus = () => {
143+
const checkOutputInputFocus = () => {
144+
142145
const activeElement = document.activeElement;
143146
if (!activeElement) {
144147
return;
145148
}
146149

147-
if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') {
150+
isInputElementFocused = !!activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA');
151+
152+
if (isInputElementFocused) {
148153
postNotebookMessage<webviewMessages.IOutputInputFocusMessage>('outputInputFocus', { hasFocus: true });
149154

150-
activeElement.addEventListener('blur', () => {
155+
activeElement.addEventListener('onblur', () => {
151156
postNotebookMessage<webviewMessages.IOutputInputFocusMessage>('outputInputFocus', { hasFocus: false });
152157
}, { once: true });
153158
}
@@ -158,7 +163,7 @@ async function webviewPreloads(ctx: PreloadContext) {
158163
return;
159164
}
160165

161-
handleOutputFocus();
166+
checkOutputInputFocus();
162167

163168
for (const node of event.composedPath()) {
164169
if (node instanceof HTMLElement && node.classList.contains('output')) {
@@ -240,7 +245,6 @@ async function webviewPreloads(ctx: PreloadContext) {
240245
};
241246

242247
document.body.addEventListener('click', handleInnerClick);
243-
document.body.addEventListener('focusin', handleOutputFocus);
244248

245249
interface RendererContext extends rendererApi.RendererContext<unknown> {
246250
readonly onDidChangeSettings: Event<RenderOptions>;

0 commit comments

Comments
 (0)