Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore default file browser manually. #7695

Merged
merged 2 commits into from Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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