Skip to content

Commit

Permalink
Merge pull request #7695 from afshin/issue-4009
Browse files Browse the repository at this point in the history
Restore default file browser manually.
  • Loading branch information
Steven Silvester committed Jan 2, 2020
2 parents 065be47 + 435c61e commit 35cdd43
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 45 deletions.
85 changes: 50 additions & 35 deletions packages/filebrowser-extension/src/index.ts
Expand Up @@ -224,6 +224,7 @@ async function activateFactory(
options: IFileBrowserFactory.IOptions = {}
) => {
const model = new FileBrowserModel({
auto: options.auto ?? true,
iconRegistry: icoReg,
manager: docManager,
driveName: options.driveName || '',
Expand All @@ -250,42 +251,14 @@ async function activateFactory(
return widget;
};

// Manually restore the default file browser.
const id = 'filebrowser';
const defaultBrowser = createFileBrowser(id, { restore: false });
const plugin = { createFileBrowser, defaultBrowser, tracker };
const restoring = 'jp-mod-restoring';

defaultBrowser.addClass(restoring);

if (!router) {
void defaultBrowser.model.restore(id).then(() => {
defaultBrowser.removeClass(restoring);
});
return plugin;
}

const listener = async () => {
router.routed.disconnect(listener);

const paths = await tree?.paths;
if (paths) {
// Restore the model without populating it.
await defaultBrowser.model.restore(id, false);
if (paths.file) {
await commands.execute(CommandIDs.openPath, { path: paths.file });
}
if (paths.browser) {
await commands.execute(CommandIDs.openPath, { path: paths.browser });
}
} else {
await defaultBrowser.model.restore(id);
}
defaultBrowser.removeClass(restoring);
};
router.routed.connect(listener);
// Manually restore and load the default file browser.
const defaultBrowser = createFileBrowser('filebrowser', {
auto: false,
restore: false
});
void Private.restoreBrowser(defaultBrowser, commands, router, tree);

return plugin;
return { createFileBrowser, defaultBrowser, tracker };
}

/**
Expand Down Expand Up @@ -1089,4 +1062,46 @@ namespace Private {
}
return item;
}

/**
* Restores file browser state and overrides state if tree resolver resolves.
*/
export async function restoreBrowser(
browser: FileBrowser,
commands: CommandRegistry,
router: IRouter | null,
tree: JupyterFrontEnd.ITreeResolver | null
): Promise<void> {
const restoring = 'jp-mod-restoring';

browser.addClass(restoring);

if (!router) {
await browser.model.restore(browser.id);
await browser.model.refresh();
browser.removeClass(restoring);
return;
}

const listener = async () => {
router.routed.disconnect(listener);

const paths = await tree?.paths;
if (paths?.file || paths?.browser) {
// Restore the model without populating it.
await browser.model.restore(browser.id, false);
if (paths.file) {
await commands.execute(CommandIDs.openPath, { path: paths.file });
}
if (paths.browser) {
await commands.execute(CommandIDs.openPath, { path: paths.browser });
}
} else {
await browser.model.restore(browser.id);
await browser.model.refresh();
}
browser.removeClass(restoring);
};
router.routed.connect(listener);
}
}
5 changes: 4 additions & 1 deletion packages/filebrowser/src/browser.ts
Expand Up @@ -313,8 +313,11 @@ export namespace FileBrowser {

/**
* Whether a file browser automatically restores state when instantiated.
*
* The default is `true`.
*
* #### Notes
* The file browser model will need to be restored before for the file
* browser to start saving its state.
*/
restore?: boolean;
}
Expand Down
22 changes: 15 additions & 7 deletions packages/filebrowser/src/model.ts
Expand Up @@ -101,6 +101,8 @@ export class FileBrowserModel implements IDisposable {
};
window.addEventListener('beforeunload', this._unloadEventListener);
this._poll = new Poll({
auto: options.auto ?? true,
name: '@jupyterlab/filebrowser:Model',
factory: () => this.cd('.'),
frequency: {
interval: refreshInterval,
Expand Down Expand Up @@ -656,14 +658,10 @@ export namespace FileBrowserModel {
*/
export interface IOptions {
/**
* An icon registry instance.
* Whether a file browser automatically loads its initial path.
* The default is `true`.
*/
iconRegistry: IIconRegistry;

/**
* A document manager instance.
*/
manager: IDocumentManager;
auto?: boolean;

/**
* An optional `Contents.IDrive` name for the model.
Expand All @@ -672,6 +670,16 @@ export namespace FileBrowserModel {
*/
driveName?: string;

/**
* An icon registry instance.
*/
iconRegistry: IIconRegistry;

/**
* A document manager instance.
*/
manager: IDocumentManager;

/**
* The time interval for browser refreshing, in ms.
*/
Expand Down
14 changes: 12 additions & 2 deletions packages/filebrowser/src/tokens.ts
Expand Up @@ -71,6 +71,14 @@ export namespace IFileBrowserFactory {
* state database.
*/
export interface IOptions {
/**
* Whether a file browser automatically loads its initial path.
*
* #### Notes
* The default is `true`.
*/
auto?: boolean;

/**
* An optional `Contents.IDrive` name for the model.
* If given, the model will prepend `driveName:` to
Expand All @@ -84,10 +92,12 @@ export namespace IFileBrowserFactory {
refreshInterval?: number;

/**
* Whether to restore the file browser state after instantiation.
* Whether a file browser automatically restores state when instantiated.
* The default is `true`.
*
* #### Notes
* The default value is `true`.
* The file browser model will need to be restored before for the file
* browser to start saving its state.
*/
restore?: boolean;

Expand Down

0 comments on commit 35cdd43

Please sign in to comment.