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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move handling of the file browser settings to a separate plugin #7481

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 54 additions & 19 deletions packages/tree-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,59 @@ const fileActions: JupyterFrontEndPlugin<void> = {
},
};

/**
* A plugin to set the default file browser settings.
*/
const fileBrowserSettings: JupyterFrontEndPlugin<void> = {
id: '@jupyter-notebook/tree-extension:settings',
description: 'Set up the default file browser settings',
requires: [IDefaultFileBrowser],
optional: [ISettingRegistry],
autoStart: true,
activate: (
app: JupyterFrontEnd,
browser: IDefaultFileBrowser,
settingRegistry: ISettingRegistry | null
) => {
/**
* File browser default configuration.
*/
const defaultFileBrowserConfig = {
navigateToCurrentDirectory: false,
singleClickNavigation: true,
showLastModifiedColumn: true,
showFileSizeColumn: true,
showHiddenFiles: false,
showFileCheckboxes: true,
sortNotebooksFirst: true,
showFullPath: false,
};

// apply defaults
let key: keyof typeof defaultFileBrowserConfig;
for (key in defaultFileBrowserConfig) {
browser[key] = defaultFileBrowserConfig[key];
}

if (settingRegistry) {
void settingRegistry.load(FILE_BROWSER_PLUGIN_ID).then((settings) => {
function onSettingsChanged(settings: ISettingRegistry.ISettings): void {
let key: keyof typeof defaultFileBrowserConfig;
for (key in defaultFileBrowserConfig) {
const value = settings.get(key).user as boolean;
// only set the setting if it is defined by the user
if (value !== undefined) {
browser[key] = value;
}
}
}
settings.changed.connect(onSettingsChanged);
onSettingsChanged(settings);
});
}
},
};

/**
* A plugin to add the file filter toggle command to the palette
*/
Expand Down Expand Up @@ -360,25 +413,6 @@ const notebookTreeWidget: JupyterFrontEndPlugin<INotebookTree> = {
nbTreeWidget.tabBar.addTab(running.title);
}

const settings = settingRegistry.load(FILE_BROWSER_PLUGIN_ID);
Promise.all([settings, app.restored])
.then(([settings]) => {
// Set Notebook 7 defaults if there is no user setting override
[
'showFileCheckboxes',
'showFileSizeColumn',
'sortNotebooksFirst',
'showFullPath',
].forEach((setting) => {
if (settings.user[setting] === undefined) {
void settings.set(setting, true);
}
});
})
.catch((reason: Error) => {
console.error(reason.message);
});

app.shell.add(nbTreeWidget, 'main', { rank: 100 });

// add a separate tab for each setting editor
Expand Down Expand Up @@ -419,6 +453,7 @@ const notebookTreeWidget: JupyterFrontEndPlugin<INotebookTree> = {
const plugins: JupyterFrontEndPlugin<any>[] = [
createNew,
fileActions,
fileBrowserSettings,
fileFilterCommand,
loadPlugins,
openFileBrowser,
Expand Down
Loading