Skip to content

Commit

Permalink
Short-circuit selectItemByName() if already selected (#15970)
Browse files Browse the repository at this point in the history
* Short-circuit `selectItemByName()` if already selected

* Allow forcing the side-effects for internal logic ensuring focus after rename
  • Loading branch information
krassowski committed Mar 13, 2024
1 parent 2e2d64e commit 80dd3d6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
25 changes: 23 additions & 2 deletions packages/filebrowser/src/listing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,28 @@ export class DirListing extends Widget {
* @returns A promise that resolves when the name is selected.
*/
async selectItemByName(name: string, focus: boolean = false): Promise<void> {
return this._selectItemByName(name, focus);
}

/**
* Select an item by name.
*
* @param name - The name of the item to select.
* @param focus - Whether to move focus to the selected item.
* @param force - Whether to proceed with selection even if the file was already selected.
*
* @returns A promise that resolves when the name is selected.
*/
private async _selectItemByName(
name: string,
focus: boolean = false,
force: boolean = false
): Promise<void> {
if (!force && this.isSelected(name)) {
// Avoid API polling and DOM updates if already selected
return;
}

// Make sure the file is available.
await this.model.refresh();

Expand All @@ -662,7 +684,6 @@ export class DirListing extends Widget {
MessageLoop.sendMessage(this, Widget.Msg.UpdateRequest);
ElementExt.scrollIntoViewIfNeeded(this.contentNode, this._items[index]);
}

/**
* Handle the DOM events for the directory listing.
*
Expand Down Expand Up @@ -1935,7 +1956,7 @@ export class DirListing extends Widget {
this.selection[item.path]
) {
try {
await this.selectItemByName(finalFilename, true);
await this._selectItemByName(finalFilename, true, true);
} catch {
// do nothing
console.warn('After rename, failed to select file', finalFilename);
Expand Down
38 changes: 37 additions & 1 deletion packages/filebrowser/test/listing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DocumentManager } from '@jupyterlab/docmanager';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { DocumentWidgetOpenerMock } from '@jupyterlab/docregistry/lib/testutils';
import { ServiceManagerMock } from '@jupyterlab/services/lib/testutils';
import { signalToPromise } from '@jupyterlab/testing';
import { framePromise, signalToPromise } from '@jupyterlab/testing';
import { Signal } from '@lumino/signaling';
import { Widget } from '@lumino/widgets';
import expect from 'expect';
Expand Down Expand Up @@ -101,6 +101,42 @@ describe('filebrowser/listing', () => {
});
});

describe('#selectItemByName()', () => {
it('should select item in the current directory by name', async () => {
const name = [...dirListing.sortedItems()][2].name;
expect(dirListing.isSelected(name)).toBe(false);
await dirListing.selectItemByName(name);
expect(dirListing.isSelected(name)).toBe(true);
});

it('should trigger update when selecting an item', async () => {
const name = [...dirListing.sortedItems()][2].name;
let updateEmitted = false;
const listener = () => {
updateEmitted = true;
};
dirListing.updated.connect(listener);
await dirListing.selectItemByName(name);
await framePromise();
dirListing.updated.disconnect(listener);
expect(updateEmitted).toBe(true);
});

it('should be a no-op if the item is already selected', async () => {
const name = [...dirListing.sortedItems()][2].name;
await dirListing.selectItemByName(name);
let updateEmitted = false;
const listener = () => {
updateEmitted = true;
};
dirListing.updated.connect(listener);
await dirListing.selectItemByName(name);
await framePromise();
dirListing.updated.disconnect(listener);
expect(updateEmitted).toBe(false);
});
});

describe('#rename', () => {
it('backspace during rename does not trigger goUp method', async () => {
dirListing.selectNext();
Expand Down

0 comments on commit 80dd3d6

Please sign in to comment.