Skip to content

Commit

Permalink
Add a setting to change the default directory to save chat files
Browse files Browse the repository at this point in the history
  • Loading branch information
brichet committed Oct 23, 2024
1 parent 2c25b6b commit 63205b5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 10 deletions.
21 changes: 13 additions & 8 deletions packages/jupyterlab-collaborative-chat/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ChatWidget,
IActiveCellManager,
IAutocompletionRegistry,
IConfig,
ISelectionWatcher
} from '@jupyter/chat';
import { IThemeManager } from '@jupyterlab/apputils';
Expand All @@ -20,7 +19,7 @@ import { ISignal, Signal } from '@lumino/signaling';
import { CollaborativeChatModel } from './model';
import { CollaborativeChatPanel } from './widget';
import { YChat } from './ychat';
import { IWidgetConfig } from './token';
import { ICollaborativeChatConfig, IWidgetConfig } from './token';

/**
* The object provided by the chatDocument extension.
Expand All @@ -31,30 +30,36 @@ export class WidgetConfig implements IWidgetConfig {
/**
* The constructor of the WidgetConfig.
*/
constructor(config: Partial<IConfig>) {
constructor(config: Partial<ICollaborativeChatConfig>) {
this._config = config;
}

/**
* Getter and setter for the config.
*/
get config(): Partial<IConfig> {
get config(): Partial<ICollaborativeChatConfig> {
return this._config;
}
set config(value: Partial<IConfig>) {
set config(value: Partial<ICollaborativeChatConfig>) {
this._config = { ...this._config, ...value };
this._configChanged.emit(value);
}

/**
* Getter for the configChanged signal
*/
get configChanged(): ISignal<WidgetConfig, Partial<IConfig>> {
get configChanged(): ISignal<
WidgetConfig,
Partial<ICollaborativeChatConfig>
> {
return this._configChanged;
}

private _config: Partial<IConfig>;
private _configChanged = new Signal<WidgetConfig, Partial<IConfig>>(this);
private _config: Partial<ICollaborativeChatConfig>;
private _configChanged = new Signal<
WidgetConfig,
Partial<ICollaborativeChatConfig>
>(this);
}

/**
Expand Down
12 changes: 11 additions & 1 deletion packages/jupyterlab-collaborative-chat/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export const IChatFactory = new Token<IChatFactory>(
'jupyter-collaborative-chat:IChatFactory'
);

/**
* The collaborative chat configs.
*/
export interface ICollaborativeChatConfig extends IConfig {
/**
* The default directory where to create and look for chat.
*/
defaultDirectory: string;
}

/**
* The interface for the chat factory objects.
*/
Expand All @@ -56,7 +66,7 @@ export interface IWidgetConfig {
/**
* The widget config
*/
config: Partial<IConfig>;
config: Partial<ICollaborativeChatConfig>;

/**
* A signal emitting when the configuration for the chats has changed.
Expand Down
6 changes: 6 additions & 0 deletions python/jupyterlab-collaborative-chat/schema/factory.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
"default": true,
"readOnly": false
},
"defaultDirectory": {
"description": "Default directory where to create and look for chat.",
"type": "string",
"default": ".",
"readOnly": false
},
"toolbar": {
"title": "File browser toolbar items",
"description": "Note: To disable a toolbar item,\ncopy it to User Preferences and add the\n\"disabled\" key. The following example will disable the uploader button:\n{\n \"toolbar\": [\n {\n \"name\": \"uploader\",\n \"disabled\": true\n }\n ]\n}\n\nToolbar description:",
Expand Down
57 changes: 56 additions & 1 deletion python/jupyterlab-collaborative-chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,31 @@ const docFactories: JupyterFrontEndPlugin<IChatFactory> = {
* Load the settings for the chat widgets.
*/
function loadSetting(setting: ISettingRegistry.ISettings): void {
// Remove the previous directory if it is empty and has changed.
const previousDirectory = widgetConfig.config.defaultDirectory;
const currentDirectory = setting.get('defaultDirectory')
.composite as string;

if (
drive &&
previousDirectory &&
previousDirectory !== currentDirectory &&
previousDirectory !== '.'
) {
drive
.get(previousDirectory)
.then(contentModel => {
if (contentModel.content.length === 0) {
drive.delete(previousDirectory).catch(e => {
// no-op, the directory might not be empty
});
}
})
.catch(() => {
// no-op, the directory does not exists.
});
}

// Read the settings and convert to the correct type
widgetConfig.config = {
sendWithShiftEnter: setting.get('sendWithShiftEnter')
Expand All @@ -146,8 +171,33 @@ const docFactories: JupyterFrontEndPlugin<IChatFactory> = {
enableCodeToolbar: setting.get('enableCodeToolbar')
.composite as boolean,
sendTypingNotification: setting.get('sendTypingNotification')
.composite as boolean
.composite as boolean,
defaultDirectory: currentDirectory
};

// Create the new directory if necessary.
if (
drive &&
currentDirectory &&
previousDirectory !== currentDirectory &&
currentDirectory !== '.'
) {
drive.get(currentDirectory, { content: false }).catch(() => {
drive
.newUntitled({
type: 'directory'
})
.then(contentModel => {
drive.rename(contentModel.path, currentDirectory).catch(e => {
drive.delete(contentModel.path);
throw e;
});
})
.catch(e => {
throw e;
});
});
}
}

if (settingRegistry) {
Expand Down Expand Up @@ -326,6 +376,11 @@ const chatCommands: JupyterFrontEndPlugin<void> = {
} else {
filepath = `${name}${chatFileType.extensions[0]}`;
}
// Add the default directory to the path.
filepath = PathExt.join(
widgetConfig.config.defaultDirectory || '.',
filepath
);
}

let fileExist = true;
Expand Down

0 comments on commit 63205b5

Please sign in to comment.