mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-01 01:25:38 -05:00
add open book command to open any book from local sys (#8632)
* add open book command to open any book from local sys * no-floating promises * moved all the loc strings to common loc file * typos
This commit is contained in:
@@ -171,6 +171,15 @@
|
||||
"dark": "resources/dark/search_inverse.svg",
|
||||
"light": "resources/light/search.svg"
|
||||
}
|
||||
},
|
||||
{
|
||||
"command": "notebook.command.openBook",
|
||||
"title": "%title.openJupyterBook%",
|
||||
"category": "%books-preview-category%",
|
||||
"icon": {
|
||||
"dark": "resources/dark/open_notebook_inverse.svg",
|
||||
"light": "resources/light/open_notebook.svg"
|
||||
}
|
||||
}
|
||||
],
|
||||
"languages": [
|
||||
@@ -312,6 +321,13 @@
|
||||
"group": "inline"
|
||||
}
|
||||
],
|
||||
"view/title": [
|
||||
{
|
||||
"command": "notebook.command.openBook",
|
||||
"when": "view == bookTreeView",
|
||||
"group": "navigation"
|
||||
}
|
||||
],
|
||||
"notebook/toolbar": [
|
||||
{
|
||||
"command": "jupyter.cmd.managePackages",
|
||||
|
||||
@@ -33,5 +33,6 @@
|
||||
"title.searchJupyterBook": "Search Book",
|
||||
"title.SavedBooks": "Saved Books",
|
||||
"title.UnsavedBooks": "Unsaved Books",
|
||||
"title.PreviewLocalizedBook": "Get localized SQL Server 2019 guide"
|
||||
"title.PreviewLocalizedBook": "Get localized SQL Server 2019 guide",
|
||||
"title.openJupyterBook": "Open Jupyter Book"
|
||||
}
|
||||
|
||||
@@ -12,11 +12,11 @@ import { maxBookSearchDepth, notebookConfigKey } from '../common/constants';
|
||||
import * as path from 'path';
|
||||
import * as fileServices from 'fs';
|
||||
import * as fs from 'fs-extra';
|
||||
import * as nls from 'vscode-nls';
|
||||
import * as loc from '../common/localizedConstants';
|
||||
import { IJupyterBookToc, IJupyterBookSection } from '../contracts/content';
|
||||
import { isNullOrUndefined } from 'util';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
const fsPromises = fileServices.promises;
|
||||
|
||||
export class BookModel implements azdata.nb.NavigationProvider {
|
||||
@@ -54,9 +54,13 @@ export class BookModel implements azdata.nb.NavigationProvider {
|
||||
|
||||
let p = path.join(workspacePath, '**', '_data', 'toc.yml').replace(/\\/g, '/');
|
||||
let tableOfContentPaths = await glob(p, { deep: maxDepth });
|
||||
this._tableOfContentPaths = this._tableOfContentPaths.concat(tableOfContentPaths);
|
||||
let bookOpened: boolean = this._tableOfContentPaths.length > 0;
|
||||
vscode.commands.executeCommand('setContext', 'bookOpened', bookOpened);
|
||||
if (tableOfContentPaths.length > 0) {
|
||||
this._tableOfContentPaths = this._tableOfContentPaths.concat(tableOfContentPaths);
|
||||
vscode.commands.executeCommand('setContext', 'bookOpened', true);
|
||||
} else {
|
||||
vscode.window.showErrorMessage(loc.errBookInitialize);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
@@ -168,7 +172,7 @@ export class BookModel implements azdata.nb.NavigationProvider {
|
||||
);
|
||||
notebooks.push(markdown);
|
||||
} else {
|
||||
let error = localize('missingFileError', "Missing file : {0}", sections[i].title);
|
||||
let error = loc.missingFileError(sections[i].title);
|
||||
vscode.window.showErrorMessage(error);
|
||||
}
|
||||
}
|
||||
@@ -187,9 +191,9 @@ export class BookModel implements azdata.nb.NavigationProvider {
|
||||
try {
|
||||
return section.reduce((acc, val) => Array.isArray(val.sections) ? acc.concat(val).concat(this.parseJupyterSections(val.sections)) : acc.concat(val), []);
|
||||
} catch (error) {
|
||||
let err: string = localize('InvalidError.tocFile', "{0}", error);
|
||||
let err: string = loc.invalidTocFileError(error);
|
||||
if (section.length > 0) {
|
||||
err = localize('Invalid toc.yml', "Error: {0} has an incorrect toc.yml file", section[0].title); //need to find a way to get title.
|
||||
err = loc.invalidTocError(section[0].title);
|
||||
}
|
||||
vscode.window.showErrorMessage(err);
|
||||
throw err;
|
||||
|
||||
@@ -6,9 +6,8 @@
|
||||
import * as vscode from 'vscode';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import * as nls from 'vscode-nls';
|
||||
import { IJupyterBookSection, IJupyterBookToc } from '../contracts/content';
|
||||
const localize = nls.loadMessageBundle();
|
||||
import * as loc from '../common/localizedConstants';
|
||||
|
||||
export enum BookTreeItemType {
|
||||
Book = 'Book',
|
||||
@@ -69,12 +68,12 @@ export class BookTreeItem extends vscode.TreeItem {
|
||||
private setCommand() {
|
||||
if (this.book.type === BookTreeItemType.Notebook) {
|
||||
let pathToNotebook = path.join(this.book.root, 'content', this._uri.concat('.ipynb'));
|
||||
this.command = { command: this.book.isUntitled ? 'bookTreeView.openUntitledNotebook' : 'bookTreeView.openNotebook', title: localize('openNotebookCommand', "Open Notebook"), arguments: [pathToNotebook], };
|
||||
this.command = { command: this.book.isUntitled ? 'bookTreeView.openUntitledNotebook' : 'bookTreeView.openNotebook', title: loc.openNotebookCommand, arguments: [pathToNotebook], };
|
||||
} else if (this.book.type === BookTreeItemType.Markdown) {
|
||||
let pathToMarkdown = path.join(this.book.root, 'content', this._uri.concat('.md'));
|
||||
this.command = { command: 'bookTreeView.openMarkdown', title: localize('openMarkdownCommand', "Open Markdown"), arguments: [pathToMarkdown], };
|
||||
this.command = { command: 'bookTreeView.openMarkdown', title: loc.openMarkdownCommand, arguments: [pathToMarkdown], };
|
||||
} else if (this.book.type === BookTreeItemType.ExternalLink) {
|
||||
this.command = { command: 'bookTreeView.openExternalLink', title: localize('openExternalLinkCommand', "Open External Link"), arguments: [this._uri], };
|
||||
this.command = { command: 'bookTreeView.openExternalLink', title: loc.openExternalLinkCommand, arguments: [this._uri], };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,12 +10,10 @@ import * as fs from 'fs-extra';
|
||||
import { IPrompter, QuestionTypes, IQuestion } from '../prompts/question';
|
||||
import CodeAdapter from '../prompts/adapter';
|
||||
import { BookTreeItem } from './bookTreeItem';
|
||||
import * as nls from 'vscode-nls';
|
||||
import { isEditorTitleFree } from '../common/utils';
|
||||
import { BookModel } from './bookModel';
|
||||
import { Deferred } from '../common/promise';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
import * as loc from '../common/localizedConstants';
|
||||
|
||||
export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeItem> {
|
||||
|
||||
@@ -39,7 +37,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
this._openAsUntitled = openAsUntitled;
|
||||
this._extensionContext = extensionContext;
|
||||
this.books = [];
|
||||
this.initialize(workspaceFolders.map(a => a.uri.fsPath));
|
||||
this.initialize(workspaceFolders.map(a => a.uri.fsPath)).catch(e => console.error(e));
|
||||
this.viewId = view;
|
||||
this.prompter = new CodeAdapter();
|
||||
|
||||
@@ -72,19 +70,17 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
// Check if the book is already open in viewlet.
|
||||
if (books.length > 0 && books[0].bookItems) {
|
||||
this.currentBook = books[0];
|
||||
this.showPreviewFile(urlToOpen);
|
||||
await this.showPreviewFile(urlToOpen);
|
||||
}
|
||||
else {
|
||||
await this.initialize([bookPath]);
|
||||
let bookViewer = vscode.window.createTreeView(this.viewId, { showCollapseAll: true, treeDataProvider: this });
|
||||
this.currentBook = this.books.filter(book => book.bookPath === bookPath)[0];
|
||||
bookViewer.reveal(this.currentBook.bookItems[0], { expand: vscode.TreeItemCollapsibleState.Expanded, focus: true, select: true });
|
||||
this.showPreviewFile(urlToOpen);
|
||||
await this.showPreviewFile(urlToOpen);
|
||||
}
|
||||
} catch (e) {
|
||||
vscode.window.showErrorMessage(localize('openBookError', "Open book {0} failed: {1}",
|
||||
bookPath,
|
||||
e instanceof Error ? e.message : e));
|
||||
vscode.window.showErrorMessage(loc.openFileError(bookPath, e instanceof Error ? e.message : e));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +95,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
this.openMarkdown(sectionToOpenMarkdown);
|
||||
}
|
||||
else if (await fs.pathExists(sectionToOpenNotebook)) {
|
||||
this.openNotebook(sectionToOpenNotebook);
|
||||
await this.openNotebook(sectionToOpenNotebook);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,9 +110,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
vscode.window.showTextDocument(doc);
|
||||
}
|
||||
} catch (e) {
|
||||
vscode.window.showErrorMessage(localize('openNotebookError', "Open notebook {0} failed: {1}",
|
||||
resource,
|
||||
e instanceof Error ? e.message : e));
|
||||
vscode.window.showErrorMessage(loc.openNotebookError(resource, e instanceof Error ? e.message : e));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,9 +119,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
try {
|
||||
vscode.commands.executeCommand('markdown.showPreview', vscode.Uri.file(resource));
|
||||
} catch (e) {
|
||||
vscode.window.showErrorMessage(localize('openMarkdownError', "Open markdown {0} failed: {1}",
|
||||
resource,
|
||||
e instanceof Error ? e.message : e));
|
||||
vscode.window.showErrorMessage(loc.openMarkdownError(resource, e instanceof Error ? e.message : e));
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -144,15 +136,13 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
vscode.window.showErrorMessage(localize('openUntitledNotebookError', "Open untitled notebook {0} as untitled failed: {1}",
|
||||
resource,
|
||||
e instanceof Error ? e.message : e));
|
||||
vscode.window.showErrorMessage(loc.openUntitledNotebookError(resource, e instanceof Error ? e.message : e));
|
||||
}
|
||||
}
|
||||
|
||||
async saveJupyterBooks(): Promise<void> {
|
||||
if (this.currentBook.bookPath) {
|
||||
const allFilesFilter = localize('allFiles', "All Files");
|
||||
const allFilesFilter = loc.allFiles;
|
||||
let filter: any = {};
|
||||
filter[allFilesFilter] = '*';
|
||||
let uris = await vscode.window.showOpenDialog({
|
||||
@@ -160,7 +150,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
canSelectFiles: false,
|
||||
canSelectMany: false,
|
||||
canSelectFolders: true,
|
||||
openLabel: localize('labelPickFolder', "Pick Folder")
|
||||
openLabel: loc.labelPickFolder
|
||||
});
|
||||
if (uris && uris.length > 0) {
|
||||
let pickedFolder = uris[0];
|
||||
@@ -200,6 +190,23 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
}
|
||||
}
|
||||
|
||||
public async openNewBook(): Promise<void> {
|
||||
const allFilesFilter = loc.allFiles;
|
||||
let filter: any = {};
|
||||
filter[allFilesFilter] = '*';
|
||||
let uris = await vscode.window.showOpenDialog({
|
||||
filters: filter,
|
||||
canSelectFiles: false,
|
||||
canSelectMany: false,
|
||||
canSelectFolders: true,
|
||||
openLabel: loc.labelBookFolder
|
||||
});
|
||||
if (uris && uris.length > 0) {
|
||||
let bookPath = uris[0];
|
||||
await this.openBook(bookPath.fsPath);
|
||||
}
|
||||
}
|
||||
|
||||
private runThrottledAction(resource: string, action: () => void) {
|
||||
const isResourceChange = resource !== this._resource;
|
||||
if (isResourceChange) {
|
||||
@@ -230,9 +237,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
try {
|
||||
vscode.env.openExternal(vscode.Uri.parse(resource));
|
||||
} catch (e) {
|
||||
vscode.window.showErrorMessage(localize('openExternalLinkError', "Open link {0} failed: {1}",
|
||||
resource,
|
||||
e instanceof Error ? e.message : e));
|
||||
vscode.window.showErrorMessage(loc.openExternalLinkError(resource, e instanceof Error ? e.message : e));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -316,7 +321,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
private async confirmReplace(): Promise<boolean> {
|
||||
return await this.prompter.promptSingle<boolean>(<IQuestion>{
|
||||
type: QuestionTypes.confirm,
|
||||
message: localize('confirmReplace', "Folder already exists. Are you sure you want to delete and replace this folder?"),
|
||||
message: loc.confirmReplace,
|
||||
default: false
|
||||
});
|
||||
}
|
||||
|
||||
@@ -14,3 +14,26 @@ export const msgNo = localize('msgNo', "No");
|
||||
|
||||
// Jupyter Constants ///////////////////////////////////////////////////////
|
||||
export const msgSampleCodeDataFrame = localize('msgSampleCodeDataFrame', "This sample code loads the file into a data frame and shows the first 10 results.");
|
||||
|
||||
// Book view-let constants
|
||||
export const allFiles = localize('allFiles', "All Files");
|
||||
export const labelPickFolder = localize('labelPickFolder', "Pick Folder");
|
||||
export const labelBookFolder = localize('labelBookFolder', "Select Book");
|
||||
export const confirmReplace = localize('confirmReplace', "Folder already exists. Are you sure you want to delete and replace this folder?");
|
||||
export const openNotebookCommand = localize('openNotebookCommand', "Open Notebook");
|
||||
export const openMarkdownCommand = localize('openMarkdownCommand', "Open Markdown");
|
||||
export const openExternalLinkCommand = localize('openExternalLinkCommand', "Open External Link");
|
||||
|
||||
export const errBookInitialize = localize('bookInitializeFailed', "Book initialize failed: Failed to recognize the structure.");
|
||||
export function missingFileError(title: string): string { return localize('missingFileError', "Missing file : {0}", title); }
|
||||
export function invalidTocFileError(error: string): string { return localize('InvalidError.tocFile', "{0}", error); }
|
||||
export function invalidTocError(title: string): string { return localize('Invalid toc.yml', "Error: {0} has an incorrect toc.yml file", title); }
|
||||
|
||||
export function openFileError(path: string, error: string): string { return localize('openBookError', "Open book {0} failed: {1}", path, error); }
|
||||
export function openNotebookError(resource: string, error: string): string { return localize('openNotebookError', "Open notebook {0} failed: {1}", resource, error); }
|
||||
export function openMarkdownError(resource: string, error: string): string { return localize('openMarkdownError', "Open markdown {0} failed: {1}", resource, error); }
|
||||
export function openUntitledNotebookError(resource: string, error: string): string { return localize('openUntitledNotebookError', "Open untitled notebook {0} as untitled failed: {1}", resource, error); }
|
||||
export function openExternalLinkError(resource: string, error: string): string { return localize('openExternalLinkError', "Open link {0} failed: {1}", resource, error); }
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ export async function activate(extensionContext: vscode.ExtensionContext): Promi
|
||||
extensionContext.subscriptions.push(vscode.commands.registerCommand('notebook.command.saveBook', () => untitledBookTreeViewProvider.saveJupyterBooks()));
|
||||
extensionContext.subscriptions.push(vscode.commands.registerCommand('notebook.command.searchBook', () => bookTreeViewProvider.searchJupyterBooks()));
|
||||
extensionContext.subscriptions.push(vscode.commands.registerCommand('notebook.command.searchUntitledBook', () => untitledBookTreeViewProvider.searchJupyterBooks()));
|
||||
extensionContext.subscriptions.push(vscode.commands.registerCommand('notebook.command.openBook', () => bookTreeViewProvider.openNewBook()));
|
||||
|
||||
extensionContext.subscriptions.push(vscode.commands.registerCommand('_notebook.command.new', (context?: azdata.ConnectedContext) => {
|
||||
let connectionProfile: azdata.IConnectionProfile = undefined;
|
||||
|
||||
Reference in New Issue
Block a user