Skip to content

Commit bc86068

Browse files
committed
feat: Localize commands, extract notebook creation logic to reuse in methods
Signed-off-by: Tomas Kislan <[email protected]>
1 parent 79a8470 commit bc86068

File tree

3 files changed

+61
-58
lines changed

3 files changed

+61
-58
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,37 +183,37 @@
183183
},
184184
{
185185
"command": "deepnote.renameProject",
186-
"title": "Rename Project",
186+
"title": "%deepnote.commands.renameProject.title%",
187187
"category": "Deepnote",
188188
"icon": "$(edit)"
189189
},
190190
{
191191
"command": "deepnote.deleteProject",
192-
"title": "Delete Project",
192+
"title": "%deepnote.commands.deleteProject.title%",
193193
"category": "Deepnote",
194194
"icon": "$(trash)"
195195
},
196196
{
197197
"command": "deepnote.renameNotebook",
198-
"title": "Rename Notebook",
198+
"title": "%deepnote.commands.renameNotebook.title%",
199199
"category": "Deepnote",
200200
"icon": "$(edit)"
201201
},
202202
{
203203
"command": "deepnote.deleteNotebook",
204-
"title": "Delete Notebook",
204+
"title": "%deepnote.commands.deleteNotebook.title%",
205205
"category": "Deepnote",
206206
"icon": "$(trash)"
207207
},
208208
{
209209
"command": "deepnote.duplicateNotebook",
210-
"title": "Duplicate Notebook",
210+
"title": "%deepnote.commands.duplicateNotebook.title%",
211211
"category": "Deepnote",
212212
"icon": "$(copy)"
213213
},
214214
{
215215
"command": "deepnote.addNotebookToProject",
216-
"title": "Add Notebook",
216+
"title": "%deepnote.commands.addNotebookToProject.title%",
217217
"category": "Deepnote",
218218
"icon": "$(add)"
219219
},

package.nls.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@
265265
"deepnote.commands.addInputFileBlock.title": "Add Input File Block",
266266
"deepnote.commands.addButtonBlock.title": "Add Button Block",
267267
"deepnote.commands.newNotebook.title": "New Notebook",
268+
"deepnote.commands.renameProject.title": "Rename Project",
269+
"deepnote.commands.deleteProject.title": "Delete Project",
270+
"deepnote.commands.renameNotebook.title": "Rename Notebook",
271+
"deepnote.commands.deleteNotebook.title": "Delete Notebook",
272+
"deepnote.commands.duplicateNotebook.title": "Duplicate Notebook",
273+
"deepnote.commands.addNotebookToProject.title": "Add Notebook",
268274
"deepnote.views.explorer.name": "Explorer",
269275
"deepnote.views.explorer.welcome": "No Deepnote notebooks found in this workspace.",
270276
"deepnote.command.selectNotebook.title": "Select Notebook"

src/notebooks/deepnote/deepnoteExplorerView.ts

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,47 @@ export class DeepnoteExplorerView {
227227
});
228228
}
229229

230+
/**
231+
* Shared helper that creates and adds a new notebook to a project
232+
* @param fileUri The URI of the project file
233+
* @param projectId The project ID
234+
* @returns Object with notebook ID and name if successful, or null if aborted/failed
235+
*/
236+
private async createAndAddNotebookToProject(
237+
fileUri: Uri,
238+
projectId: string
239+
): Promise<{ id: string; name: string } | null> {
240+
// Read the Deepnote project file
241+
const projectData = await this.readDeepnoteProjectFile(fileUri);
242+
243+
if (!projectData?.project) {
244+
await window.showErrorMessage(l10n.t('Invalid Deepnote file format'));
245+
return null;
246+
}
247+
248+
// Generate suggested name and prompt user
249+
const suggestedName = this.generateSuggestedNotebookName(projectData);
250+
const notebookName = await this.promptForNotebookName(suggestedName);
251+
252+
if (!notebookName) {
253+
return null;
254+
}
255+
256+
// Create new notebook with initial block
257+
const newNotebook = this.createNotebookWithFirstBlock(notebookName);
258+
259+
// Add new notebook to the project (initialize array if needed)
260+
if (!projectData.project.notebooks) {
261+
projectData.project.notebooks = [];
262+
}
263+
projectData.project.notebooks.push(newNotebook);
264+
265+
// Save and open the new notebook
266+
await this.saveProjectAndOpenNotebook(fileUri, projectData, projectId, newNotebook.id);
267+
268+
return { id: newNotebook.id, name: notebookName };
269+
}
270+
230271
private refreshExplorer(): void {
231272
this.treeDataProvider.refresh();
232273
}
@@ -450,35 +491,12 @@ export class DeepnoteExplorerView {
450491
}
451492

452493
try {
453-
// Read the current file
454-
const projectData = await this.readDeepnoteProjectFile(fileUri);
455-
456-
if (!projectData?.project) {
457-
await window.showErrorMessage(l10n.t('Invalid Deepnote file format'));
458-
return;
459-
}
460-
461-
// Generate suggested name and prompt user
462-
const suggestedName = this.generateSuggestedNotebookName(projectData);
463-
const notebookName = await this.promptForNotebookName(suggestedName);
464-
465-
if (!notebookName) {
466-
return;
467-
}
468-
469-
// Create new notebook with initial block
470-
const newNotebook = this.createNotebookWithFirstBlock(notebookName);
494+
// Use shared helper to create and add notebook
495+
const result = await this.createAndAddNotebookToProject(fileUri, projectId);
471496

472-
// Add new notebook to the project
473-
if (!projectData.project.notebooks) {
474-
projectData.project.notebooks = [];
497+
if (result) {
498+
await window.showInformationMessage(l10n.t('Created new notebook: {0}', result.name));
475499
}
476-
projectData.project.notebooks.push(newNotebook);
477-
478-
// Save and open the new notebook
479-
await this.saveProjectAndOpenNotebook(fileUri, projectData, projectId, newNotebook.id);
480-
481-
await window.showInformationMessage(l10n.t('Created new notebook: {0}', notebookName));
482500
} catch (error) {
483501
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
484502
await window.showErrorMessage(l10n.t('Failed to add notebook: {0}', errorMessage));
@@ -946,34 +964,13 @@ export class DeepnoteExplorerView {
946964

947965
try {
948966
const fileUri = Uri.file(treeItem.context.filePath);
949-
const projectData = await this.readDeepnoteProjectFile(fileUri);
950-
951-
if (!projectData?.project) {
952-
await window.showErrorMessage(l10n.t('Invalid Deepnote file format'));
953-
return;
954-
}
955967

956-
// Generate suggested name and prompt user
957-
const suggestedName = this.generateSuggestedNotebookName(projectData);
958-
const notebookName = await this.promptForNotebookName(suggestedName);
968+
// Use shared helper to create and add notebook
969+
const result = await this.createAndAddNotebookToProject(fileUri, projectId);
959970

960-
if (!notebookName) {
961-
return;
971+
if (result) {
972+
await window.showInformationMessage(l10n.t('Created new notebook: {0}', result.name));
962973
}
963-
964-
// Create new notebook with initial block
965-
const newNotebook = this.createNotebookWithFirstBlock(notebookName);
966-
967-
// Add new notebook to the project
968-
if (!projectData.project.notebooks) {
969-
projectData.project.notebooks = [];
970-
}
971-
projectData.project.notebooks.push(newNotebook);
972-
973-
// Save and open the new notebook
974-
await this.saveProjectAndOpenNotebook(fileUri, projectData, projectId, newNotebook.id);
975-
976-
await window.showInformationMessage(l10n.t('Created new notebook: {0}', notebookName));
977974
} catch (error) {
978975
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
979976
await window.showErrorMessage(l10n.t('Failed to add notebook: {0}', errorMessage));

0 commit comments

Comments
 (0)