Skip to content

Commit 172a7f6

Browse files
Backport PR #19149 on branch 4.6.x (Fix notebook initialization stealing focus) (#19161)
Backport PR #19149: Fix notebook initialization stealing focus Co-authored-by: ANUJ KUMAR SINGH <[email protected]>
1 parent 34e1782 commit 172a7f6

5 files changed

Lines changed: 27 additions & 11 deletions

File tree

‎galata/src/helpers/notebook.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,6 @@ export class NotebookHelper {
475475
.locator('[data-icon="ui-components:not-trusted"]')
476476
.count()) === 1
477477
) {
478-
// Workaround for https://github.com/jupyterlab/jupyterlab/issues/18457
479-
await this.page
480-
.locator('.jp-Notebook-ExecutionIndicator[data-status="idle"]')
481-
.waitFor();
482-
483478
await this.page.keyboard.press('Control+Shift+C');
484479
await this.page.getByPlaceholder('SEARCH', { exact: true }).fill('trust');
485480
await this.page.getByText('Trust Notebook').click();

‎galata/test/jupyterlab/help.test.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ test('Switch back and forth to an iframe', async ({ page }) => {
3030

3131
await page.notebook.setCell(0, 'markdown', cellContent);
3232

33-
// Workaround for https://github.com/jupyterlab/jupyterlab/issues/18457
34-
await page.getByText('Python 3 (ipykernel) | Idle').waitFor();
35-
3633
// Open a local page (the unauthenticated Jupyter Server `/api` version
3734
// endpoint) in an in-app iframe tab, avoiding a dependency on an external
3835
// website which would make this test flaky.

‎galata/test/jupyterlab/metadataform.test.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,6 @@ test.describe('Notebook level and cell type metadata', () => {
511511
let nbMetadata = await getNotebookMetadata(page);
512512
expect(nbMetadata['nb-nested']).toBeUndefined();
513513

514-
// Workaround for https://github.com/jupyterlab/jupyterlab/issues/18457
515-
await page.getByText('Python 3 (ipykernel) | Idle').waitFor();
516-
517514
// Fill the first level nested metadata.
518515
await formGroup.locator('input').first().fill('Cell input');
519516
await formGroup.locator('input').last().fill('Notebook input');

‎packages/notebook/src/widget.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,6 +2834,10 @@ export class Notebook extends StaticNotebook {
28342834
* Ensure that the notebook has proper focus.
28352835
*/
28362836
private _ensureFocus(force = false): void {
2837+
const activeElement = document.activeElement;
2838+
if (!force && activeElement && !this.node.contains(activeElement)) {
2839+
return;
2840+
}
28372841
// No-op is the footer has the focus.
28382842
const footer = (this.layout as NotebookWindowedLayout).footer;
28392843
if (footer && document.activeElement === footer.node) {

‎packages/notebook/test/widget.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,29 @@ describe('@jupyter/notebook', () => {
10251025
expect(widget.mode).toBe('edit');
10261026
});
10271027

1028+
it('should not steal focus from outside the notebook when setting mode', async () => {
1029+
const widget = createActiveWidget();
1030+
const input = document.createElement('input');
1031+
document.body.appendChild(input);
1032+
Widget.attach(widget, document.body);
1033+
try {
1034+
await framePromise();
1035+
1036+
input.focus();
1037+
expect(document.activeElement).toBe(input);
1038+
expect(widget.node.contains(input)).toBe(false);
1039+
widget.mode = 'edit';
1040+
expect(document.activeElement).toBe(input);
1041+
await framePromise();
1042+
1043+
expect(widget.mode).toBe('edit');
1044+
expect(document.activeElement).toBe(input);
1045+
} finally {
1046+
widget.dispose();
1047+
input.remove();
1048+
}
1049+
});
1050+
10281051
it('should emit the `stateChanged` signal', () => {
10291052
const widget = createActiveWidget();
10301053
let called = false;

0 commit comments

Comments
 (0)