mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 18:46:40 -05:00
Books/viewlet refactor (#7208)
* add saved/untitled views to the books viewlet and provide save option to untitled * addressed comments * fixes after merge * await on async methods * reverted back * await on promise * added localize for books view names * initial commit * missed a file change * changes to make the merges work part1 * fixes after merges 2 * getChildren to get all books * chnages to address comments * fsPromises instead of fs.readSync * merged master * replaced deprecated fs.exists async call with pathExists * renamed method
This commit is contained in:
231
extensions/notebook/src/book/bookModel.ts
Normal file
231
extensions/notebook/src/book/bookModel.ts
Normal file
@@ -0,0 +1,231 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as azdata from 'azdata';
|
||||
import * as vscode from 'vscode';
|
||||
import * as yaml from 'js-yaml';
|
||||
import * as glob from 'fast-glob';
|
||||
import { BookTreeItem, BookTreeItemType } from './bookTreeItem';
|
||||
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 { IJupyterBookToc, IJupyterBookSection } from '../contracts/content';
|
||||
import { isNullOrUndefined } from 'util';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
const fsPromises = fileServices.promises;
|
||||
|
||||
export class BookModel implements azdata.nb.NavigationProvider {
|
||||
private _bookItems: BookTreeItem[];
|
||||
private _allNotebooks = new Map<string, BookTreeItem>();
|
||||
private _tableOfContentPaths: string[] = [];
|
||||
readonly providerId: string = 'BookNavigator';
|
||||
|
||||
constructor(public bookPath: string, public openAsUntitled: boolean, private _extensionContext: vscode.ExtensionContext) {
|
||||
this.bookPath = bookPath;
|
||||
this.openAsUntitled = openAsUntitled;
|
||||
this._bookItems = [];
|
||||
this._extensionContext.subscriptions.push(azdata.nb.registerNavigationProvider(this));
|
||||
}
|
||||
|
||||
public async initializeContents(): Promise<void> {
|
||||
await this.getTableOfContentFiles(this.bookPath);
|
||||
await this.readBooks();
|
||||
}
|
||||
|
||||
public getAllBooks(): Map<string, BookTreeItem> {
|
||||
return this._allNotebooks;
|
||||
}
|
||||
|
||||
public async getTableOfContentFiles(workspacePath: string): Promise<void> {
|
||||
try {
|
||||
let notebookConfig = vscode.workspace.getConfiguration(notebookConfigKey);
|
||||
let maxDepth = notebookConfig[maxBookSearchDepth];
|
||||
// Use default value if user enters an invalid value
|
||||
if (isNullOrUndefined(maxDepth) || maxDepth < 0) {
|
||||
maxDepth = 5;
|
||||
} else if (maxDepth === 0) { // No limit of search depth if user enters 0
|
||||
maxDepth = undefined;
|
||||
}
|
||||
|
||||
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);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async readBooks(): Promise<BookTreeItem[]> {
|
||||
for (const contentPath of this._tableOfContentPaths) {
|
||||
let root = path.dirname(path.dirname(contentPath));
|
||||
try {
|
||||
let fileContents = await fsPromises.readFile(path.join(root, '_config.yml'), 'utf-8');
|
||||
const config = yaml.safeLoad(fileContents.toString());
|
||||
fileContents = await fsPromises.readFile(contentPath, 'utf-8');
|
||||
const tableOfContents = yaml.safeLoad(fileContents.toString());
|
||||
let book = new BookTreeItem({
|
||||
title: config.title,
|
||||
root: root,
|
||||
tableOfContents: { sections: this.parseJupyterSections(tableOfContents) },
|
||||
page: tableOfContents,
|
||||
type: BookTreeItemType.Book,
|
||||
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Expanded,
|
||||
isUntitled: this.openAsUntitled,
|
||||
},
|
||||
{
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/book.svg'),
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/book_inverse.svg')
|
||||
}
|
||||
);
|
||||
this._bookItems.push(book);
|
||||
} catch (e) {
|
||||
let error = e instanceof Error ? e.message : e;
|
||||
vscode.window.showErrorMessage(error);
|
||||
}
|
||||
}
|
||||
return this._bookItems;
|
||||
}
|
||||
|
||||
public get bookItems(): BookTreeItem[] {
|
||||
return this._bookItems;
|
||||
}
|
||||
|
||||
public async getSections(tableOfContents: IJupyterBookToc, sections: IJupyterBookSection[], root: string): Promise<BookTreeItem[]> {
|
||||
let notebooks: BookTreeItem[] = [];
|
||||
for (let i = 0; i < sections.length; i++) {
|
||||
if (sections[i].url) {
|
||||
if (sections[i].external) {
|
||||
let externalLink = new BookTreeItem({
|
||||
title: sections[i].title,
|
||||
root: root,
|
||||
tableOfContents: tableOfContents,
|
||||
page: sections[i],
|
||||
type: BookTreeItemType.ExternalLink,
|
||||
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
|
||||
isUntitled: this.openAsUntitled
|
||||
},
|
||||
{
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/link.svg'),
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/link_inverse.svg')
|
||||
}
|
||||
);
|
||||
|
||||
notebooks.push(externalLink);
|
||||
} else {
|
||||
let pathToNotebook = path.join(root, 'content', sections[i].url.concat('.ipynb'));
|
||||
let pathToMarkdown = path.join(root, 'content', sections[i].url.concat('.md'));
|
||||
// Note: Currently, if there is an ipynb and a md file with the same name, Jupyter Books only shows the notebook.
|
||||
// Following Jupyter Books behavior for now
|
||||
if (await fs.pathExists(pathToNotebook)) {
|
||||
let notebook = new BookTreeItem({
|
||||
title: sections[i].title,
|
||||
root: root,
|
||||
tableOfContents: tableOfContents,
|
||||
page: sections[i],
|
||||
type: BookTreeItemType.Notebook,
|
||||
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
|
||||
isUntitled: this.openAsUntitled
|
||||
},
|
||||
{
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/notebook.svg'),
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/notebook_inverse.svg')
|
||||
}
|
||||
);
|
||||
|
||||
if (this.openAsUntitled) {
|
||||
if (!this._allNotebooks.get(path.basename(pathToNotebook))) {
|
||||
this._allNotebooks.set(path.basename(pathToNotebook), notebook);
|
||||
notebooks.push(notebook);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!this._allNotebooks.get(pathToNotebook)) {
|
||||
this._allNotebooks.set(pathToNotebook, notebook);
|
||||
notebooks.push(notebook);
|
||||
}
|
||||
}
|
||||
} else if (await fs.pathExists(pathToMarkdown)) {
|
||||
let markdown = new BookTreeItem({
|
||||
title: sections[i].title,
|
||||
root: root,
|
||||
tableOfContents: tableOfContents,
|
||||
page: sections[i],
|
||||
type: BookTreeItemType.Markdown,
|
||||
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
|
||||
isUntitled: this.openAsUntitled
|
||||
},
|
||||
{
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/markdown.svg'),
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/markdown_inverse.svg')
|
||||
}
|
||||
);
|
||||
notebooks.push(markdown);
|
||||
} else {
|
||||
let error = localize('missingFileError', "Missing file : {0}", sections[i].title);
|
||||
vscode.window.showErrorMessage(error);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// TODO: search functionality (#6160)
|
||||
}
|
||||
}
|
||||
return notebooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively parses out a section of a Jupyter Book.
|
||||
* @param section The input data to parse
|
||||
*/
|
||||
private parseJupyterSections(section: any[]): IJupyterBookSection[] {
|
||||
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);
|
||||
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.
|
||||
}
|
||||
vscode.window.showErrorMessage(err);
|
||||
throw err;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public get tableOfContentPaths() {
|
||||
return this._tableOfContentPaths;
|
||||
}
|
||||
|
||||
getNavigation(uri: vscode.Uri): Thenable<azdata.nb.NavigationResult> {
|
||||
let notebook = !this.openAsUntitled ? this._allNotebooks.get(uri.fsPath) : this._allNotebooks.get(path.basename(uri.fsPath));
|
||||
let result: azdata.nb.NavigationResult;
|
||||
if (notebook) {
|
||||
result = {
|
||||
hasNavigation: true,
|
||||
previous: notebook.previousUri ? this.openAsUntitled ? this.getPlatformSpecificUri(notebook.previousUri) : vscode.Uri.file(notebook.previousUri) : undefined,
|
||||
next: notebook.nextUri ? this.openAsUntitled ? this.getPlatformSpecificUri(notebook.nextUri) : vscode.Uri.file(notebook.nextUri) : undefined
|
||||
};
|
||||
} else {
|
||||
result = {
|
||||
hasNavigation: false,
|
||||
previous: undefined,
|
||||
next: undefined
|
||||
};
|
||||
}
|
||||
return Promise.resolve(result);
|
||||
}
|
||||
|
||||
getPlatformSpecificUri(resource: string): vscode.Uri {
|
||||
if (process.platform === 'win32') {
|
||||
return vscode.Uri.parse(`untitled:${resource}`);
|
||||
}
|
||||
else {
|
||||
return vscode.Uri.parse(resource).with({ scheme: 'untitled' });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ export interface BookTreeItemFormat {
|
||||
page: any;
|
||||
type: BookTreeItemType;
|
||||
treeItemCollapsibleState: number;
|
||||
isUntitled: boolean;
|
||||
}
|
||||
|
||||
export class BookTreeItem extends vscode.TreeItem {
|
||||
@@ -39,6 +40,9 @@ export class BookTreeItem extends vscode.TreeItem {
|
||||
if (book.type === BookTreeItemType.Book) {
|
||||
this.collapsibleState = book.treeItemCollapsibleState;
|
||||
this._sections = book.page;
|
||||
if (book.isUntitled) {
|
||||
this.contextValue = 'untitledBook';
|
||||
}
|
||||
} else {
|
||||
this.setPageVariables();
|
||||
this.setCommand();
|
||||
@@ -63,12 +67,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: 'bookTreeView.openNotebook', title: localize('openNotebookCommand', 'Open Notebook'), arguments: [pathToNotebook], };
|
||||
this.command = { command: this.book.isUntitled ? 'bookTreeView.openUntitledNotebook' : 'bookTreeView.openNotebook', title: localize('openNotebookCommand', "Open Notebook"), 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: localize('openMarkdownCommand', "Open Markdown"), 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: localize('openExternalLinkCommand', "Open External Link"), arguments: [this._uri], };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,99 +6,75 @@
|
||||
import * as azdata from 'azdata';
|
||||
import * as vscode from 'vscode';
|
||||
import * as path from 'path';
|
||||
import { promises as fs } from 'fs';
|
||||
import * as yaml from 'js-yaml';
|
||||
import * as glob from 'fast-glob';
|
||||
import { BookTreeItem, BookTreeItemType } from './bookTreeItem';
|
||||
import { maxBookSearchDepth, notebookConfigKey } from '../common/constants';
|
||||
import { isEditorTitleFree, exists } from '../common/utils';
|
||||
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 { IJupyterBookToc, IJupyterBookSection } from '../contracts/content';
|
||||
import { isEditorTitleFree } from '../common/utils';
|
||||
import { BookModel } from './bookModel';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeItem>, azdata.nb.NavigationProvider {
|
||||
readonly providerId: string = 'BookNavigator';
|
||||
export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeItem> {
|
||||
|
||||
private _onDidChangeTreeData: vscode.EventEmitter<BookTreeItem | undefined> = new vscode.EventEmitter<BookTreeItem | undefined>();
|
||||
readonly onDidChangeTreeData: vscode.Event<BookTreeItem | undefined> = this._onDidChangeTreeData.event;
|
||||
private _tableOfContentPaths: string[] = [];
|
||||
private _allNotebooks = new Map<string, BookTreeItem>();
|
||||
private _extensionContext: vscode.ExtensionContext;
|
||||
private _throttleTimer: any;
|
||||
private _resource: string;
|
||||
private _extensionContext: vscode.ExtensionContext;
|
||||
private prompter: IPrompter;
|
||||
|
||||
// For testing
|
||||
private _errorMessage: string;
|
||||
private _onReadAllTOCFiles: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
|
||||
private _openAsUntitled: boolean;
|
||||
public viewId: string;
|
||||
public books: BookModel[];
|
||||
public currentBook: BookModel;
|
||||
|
||||
constructor(workspaceFolders: vscode.WorkspaceFolder[], extensionContext: vscode.ExtensionContext, openAsUntitled: boolean, view: string) {
|
||||
this._openAsUntitled = openAsUntitled;
|
||||
this._extensionContext = extensionContext;
|
||||
this.books = [];
|
||||
this.initialize(workspaceFolders.map(a => a.uri.fsPath));
|
||||
vscode.commands.executeCommand('setContext', 'untitledBooks', openAsUntitled);
|
||||
this.viewId = view;
|
||||
this.prompter = new CodeAdapter();
|
||||
|
||||
constructor(workspaceFolders: vscode.WorkspaceFolder[], extensionContext: vscode.ExtensionContext) {
|
||||
this.initialize(workspaceFolders, null, extensionContext);
|
||||
}
|
||||
|
||||
private initialize(workspaceFolders: vscode.WorkspaceFolder[], bookPath: string, context: vscode.ExtensionContext): void {
|
||||
let workspacePaths: string[] = [];
|
||||
if (bookPath) {
|
||||
workspacePaths.push(bookPath);
|
||||
}
|
||||
else if (workspaceFolders) {
|
||||
workspacePaths = workspaceFolders.map(a => a.uri.fsPath);
|
||||
}
|
||||
this.getTableOfContentFiles(workspacePaths).then(() => undefined, (err) => { console.log(err); });
|
||||
this._extensionContext = context;
|
||||
private async initialize(bookPaths: string[]): Promise<any> {
|
||||
return Promise.all(bookPaths.map(async (bookPath) => {
|
||||
let book: BookModel = new BookModel(bookPath, this._openAsUntitled, this._extensionContext);
|
||||
await book.initializeContents();
|
||||
this.books.push(book);
|
||||
if (!this.currentBook) {
|
||||
this.currentBook = book;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
public get onReadAllTOCFiles(): vscode.Event<void> {
|
||||
return this._onReadAllTOCFiles.event;
|
||||
}
|
||||
|
||||
async getTableOfContentFiles(workspacePaths: string[]): Promise<void> {
|
||||
let notebookConfig = vscode.workspace.getConfiguration(notebookConfigKey);
|
||||
let maxDepth = notebookConfig[maxBookSearchDepth];
|
||||
// Use default value if user enters an invalid value
|
||||
if (maxDepth === undefined || maxDepth < 0) {
|
||||
maxDepth = 5;
|
||||
} else if (maxDepth === 0) { // No limit of search depth if user enters 0
|
||||
maxDepth = undefined;
|
||||
}
|
||||
for (let workspacePath of workspacePaths) {
|
||||
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);
|
||||
this._onReadAllTOCFiles.fire();
|
||||
}
|
||||
|
||||
async openBook(bookPath: string, openAsUntitled: boolean, urlToOpen?: string): Promise<void> {
|
||||
|
||||
async openBook(bookPath: string, urlToOpen?: string): Promise<void> {
|
||||
try {
|
||||
let books: BookModel[] = this.books.filter(book => book.bookPath === bookPath) || [];
|
||||
// Check if the book is already open in viewlet.
|
||||
if (this._tableOfContentPaths.indexOf(path.join(bookPath, '_data', 'toc.yml').replace(/\\/g, '/')) > -1 && this._allNotebooks.size > 0) {
|
||||
vscode.commands.executeCommand('workbench.books.action.focusBooksExplorer');
|
||||
if (books.length > 0 && books[0].bookItems) {
|
||||
this.currentBook = books[0];
|
||||
this.showPreviewFile(urlToOpen);
|
||||
}
|
||||
else {
|
||||
await this.getTableOfContentFiles([bookPath]);
|
||||
const bookViewer = vscode.window.createTreeView('bookTreeView', { showCollapseAll: true, treeDataProvider: this });
|
||||
await vscode.commands.executeCommand('workbench.books.action.focusBooksExplorer');
|
||||
this._openAsUntitled = openAsUntitled;
|
||||
let books = await this.getBooks();
|
||||
if (books && books.length > 0) {
|
||||
const rootTreeItem = books[0];
|
||||
const sectionToOpen = rootTreeItem.findChildSection(urlToOpen);
|
||||
bookViewer.reveal(rootTreeItem, { expand: vscode.TreeItemCollapsibleState.Expanded, focus: true, select: true });
|
||||
const urlPath = sectionToOpen ? sectionToOpen.url : rootTreeItem.tableOfContents.sections[0].url;
|
||||
const sectionToOpenMarkdown: string = path.join(bookPath, 'content', urlPath.concat('.md'));
|
||||
const sectionToOpenNotebook: string = path.join(bookPath, 'content', urlPath.concat('.ipynb'));
|
||||
const markdownExists = await exists(sectionToOpenMarkdown);
|
||||
const notebookExists = await exists(sectionToOpenNotebook);
|
||||
if (markdownExists) {
|
||||
vscode.commands.executeCommand('markdown.showPreview', vscode.Uri.file(sectionToOpenMarkdown));
|
||||
}
|
||||
else if (notebookExists) {
|
||||
this.openNotebook(sectionToOpenNotebook);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
} catch (e) {
|
||||
vscode.window.showErrorMessage(localize('openBookError', "Open book {0} failed: {1}",
|
||||
@@ -107,6 +83,22 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
}
|
||||
}
|
||||
|
||||
async showPreviewFile(urlToOpen?: string): Promise<void> {
|
||||
if (this.currentBook) {
|
||||
const bookRoot = this.currentBook.bookItems[0];
|
||||
const sectionToOpen = bookRoot.findChildSection(urlToOpen);
|
||||
const urlPath = sectionToOpen ? sectionToOpen.url : bookRoot.tableOfContents.sections[0].url;
|
||||
const sectionToOpenMarkdown: string = path.join(this.currentBook.bookPath, 'content', urlPath.concat('.md'));
|
||||
const sectionToOpenNotebook: string = path.join(this.currentBook.bookPath, 'content', urlPath.concat('.ipynb'));
|
||||
if (await fs.pathExists(sectionToOpenMarkdown)) {
|
||||
this.openMarkdown(sectionToOpenMarkdown);
|
||||
}
|
||||
else if (await fs.pathExists(sectionToOpenNotebook)) {
|
||||
this.openNotebook(sectionToOpenNotebook);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async openNotebook(resource: string): Promise<void> {
|
||||
try {
|
||||
if (this._openAsUntitled) {
|
||||
@@ -117,7 +109,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
vscode.window.showTextDocument(doc);
|
||||
}
|
||||
} catch (e) {
|
||||
vscode.window.showErrorMessage(localize('openNotebookError', "Open file {0} failed: {1}",
|
||||
vscode.window.showErrorMessage(localize('openNotebookError', "Open notebook {0} failed: {1}",
|
||||
resource,
|
||||
e instanceof Error ? e.message : e));
|
||||
}
|
||||
@@ -128,7 +120,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 file {0} failed: {1}",
|
||||
vscode.window.showErrorMessage(localize('openMarkdownError', "Open markdown {0} failed: {1}",
|
||||
resource,
|
||||
e instanceof Error ? e.message : e));
|
||||
}
|
||||
@@ -147,12 +139,56 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
vscode.window.showErrorMessage(localize('openUntitledNotebookError', "Open file {0} as untitled failed: {1}",
|
||||
vscode.window.showErrorMessage(localize('openUntitledNotebookError', "Open untitled notebook {0} as untitled failed: {1}",
|
||||
resource,
|
||||
e instanceof Error ? e.message : e));
|
||||
}
|
||||
}
|
||||
|
||||
async saveJupyterBooks(): Promise<void> {
|
||||
if (this.currentBook.bookPath) {
|
||||
const allFilesFilter = localize('allFiles', "All Files");
|
||||
let filter: any = {};
|
||||
filter[allFilesFilter] = '*';
|
||||
let uris = await vscode.window.showOpenDialog({
|
||||
filters: filter,
|
||||
canSelectFiles: false,
|
||||
canSelectMany: false,
|
||||
canSelectFolders: true,
|
||||
openLabel: localize('labelPickFolder', "Pick Folder")
|
||||
});
|
||||
if (uris && uris.length > 0) {
|
||||
let pickedFolder = uris[0];
|
||||
let destinationUri: vscode.Uri = vscode.Uri.file(path.join(pickedFolder.fsPath, path.basename(this.currentBook.bookPath)));
|
||||
if (destinationUri) {
|
||||
if (await fs.pathExists(destinationUri.fsPath)) {
|
||||
let doReplace = await this.confirmReplace();
|
||||
if (!doReplace) {
|
||||
return undefined;
|
||||
}
|
||||
else {
|
||||
//remove folder if exists
|
||||
await fs.remove(destinationUri.fsPath);
|
||||
}
|
||||
}
|
||||
//make directory for each contribution book.
|
||||
await fs.mkdir(destinationUri.fsPath);
|
||||
await fs.copy(this.currentBook.bookPath, destinationUri.fsPath);
|
||||
|
||||
//remove book from the untitled books and open it from Saved books
|
||||
let untitledBookIndex: number = this.books.indexOf(this.currentBook);
|
||||
if (untitledBookIndex > -1) {
|
||||
this.books.splice(untitledBookIndex, 1);
|
||||
this.currentBook = undefined;
|
||||
this._onDidChangeTreeData.fire();
|
||||
vscode.commands.executeCommand('bookTreeView.openBook', destinationUri.fsPath, false, undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private runThrottledAction(resource: string, action: () => void) {
|
||||
const isResourceChange = resource !== this._resource;
|
||||
if (isResourceChange) {
|
||||
@@ -189,168 +225,58 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
getChildren(element?: BookTreeItem): Thenable<BookTreeItem[]> {
|
||||
if (element) {
|
||||
if (element.sections) {
|
||||
return Promise.resolve(this.getSections(element.tableOfContents, element.sections, element.root));
|
||||
return Promise.resolve(this.currentBook.getSections(element.tableOfContents, element.sections, element.root).then(sections => { return sections; }));
|
||||
} else {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
} else {
|
||||
return Promise.resolve(this.getBooks());
|
||||
let booksitems: BookTreeItem[] = [];
|
||||
this.books.map(book => {
|
||||
booksitems = booksitems.concat(book.bookItems);
|
||||
});
|
||||
return Promise.resolve(booksitems);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively parses out a section of a Jupyter Book.
|
||||
* @param array The input data to parse
|
||||
*/
|
||||
private parseJupyterSection(array: any[]): IJupyterBookSection[] {
|
||||
return array.reduce((acc, val) => Array.isArray(val.sections) ? acc.concat(val).concat(this.parseJupyterSection(val.sections)) : acc.concat(val), []);
|
||||
}
|
||||
|
||||
public async getBooks(): Promise<BookTreeItem[]> {
|
||||
let books: BookTreeItem[] = [];
|
||||
for (const contentPath of this._tableOfContentPaths) {
|
||||
let root = path.dirname(path.dirname(contentPath));
|
||||
try {
|
||||
const config = yaml.safeLoad((await fs.readFile(path.join(root, '_config.yml'), 'utf-8')).toString());
|
||||
const tableOfContents = yaml.safeLoad((await fs.readFile(contentPath, 'utf-8')).toString());
|
||||
try {
|
||||
let book = new BookTreeItem({
|
||||
title: config.title,
|
||||
root: root,
|
||||
tableOfContents: { sections: this.parseJupyterSection(tableOfContents) },
|
||||
page: tableOfContents,
|
||||
type: BookTreeItemType.Book,
|
||||
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Expanded,
|
||||
},
|
||||
{
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/book.svg'),
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/book_inverse.svg')
|
||||
}
|
||||
);
|
||||
books.push(book);
|
||||
} catch (e) {
|
||||
throw Error(localize('invalidTocError', "Error: {0} has an incorrect toc.yml file. {1}", config.title, e instanceof Error ? e.message : e));
|
||||
getParent(element?: BookTreeItem): vscode.ProviderResult<BookTreeItem> {
|
||||
if (element) {
|
||||
let parentPath;
|
||||
if (element.root.endsWith('.md')) {
|
||||
parentPath = path.join(this.currentBook.bookPath, 'content', 'readme.md');
|
||||
if (parentPath === element.root) {
|
||||
return undefined;
|
||||
}
|
||||
} catch (e) {
|
||||
let error = e instanceof Error ? e.message : e;
|
||||
this._errorMessage = error;
|
||||
vscode.window.showErrorMessage(error);
|
||||
}
|
||||
}
|
||||
return books;
|
||||
}
|
||||
|
||||
public async getSections(tableOfContents: IJupyterBookToc, sections: IJupyterBookSection[], root: string): Promise<BookTreeItem[]> {
|
||||
let notebooks: BookTreeItem[] = [];
|
||||
for (let i = 0; i < sections.length; i++) {
|
||||
if (sections[i].url) {
|
||||
if (sections[i].external) {
|
||||
let externalLink = new BookTreeItem({
|
||||
title: sections[i].title,
|
||||
root: root,
|
||||
tableOfContents: tableOfContents,
|
||||
page: sections[i],
|
||||
type: BookTreeItemType.ExternalLink,
|
||||
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed
|
||||
},
|
||||
{
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/link.svg'),
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/link_inverse.svg')
|
||||
}
|
||||
);
|
||||
|
||||
notebooks.push(externalLink);
|
||||
} else {
|
||||
let pathToNotebook = path.join(root, 'content', sections[i].url.concat('.ipynb'));
|
||||
let pathToMarkdown = path.join(root, 'content', sections[i].url.concat('.md'));
|
||||
// Note: Currently, if there is an ipynb and a md file with the same name, Jupyter Books only shows the notebook.
|
||||
// Following Jupyter Books behavior for now
|
||||
if (await exists(pathToNotebook)) {
|
||||
let notebook = new BookTreeItem({
|
||||
title: sections[i].title,
|
||||
root: root,
|
||||
tableOfContents: tableOfContents,
|
||||
page: sections[i],
|
||||
type: BookTreeItemType.Notebook,
|
||||
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed
|
||||
},
|
||||
{
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/notebook.svg'),
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/notebook_inverse.svg')
|
||||
}
|
||||
);
|
||||
notebooks.push(notebook);
|
||||
this._allNotebooks.set(pathToNotebook, notebook);
|
||||
if (this._openAsUntitled) {
|
||||
this._allNotebooks.set(path.basename(pathToNotebook), notebook);
|
||||
}
|
||||
} else if (await exists(pathToMarkdown)) {
|
||||
let markdown = new BookTreeItem({
|
||||
title: sections[i].title,
|
||||
root: root,
|
||||
tableOfContents: tableOfContents,
|
||||
page: sections[i],
|
||||
type: BookTreeItemType.Markdown,
|
||||
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed
|
||||
},
|
||||
{
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/markdown.svg'),
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/markdown_inverse.svg')
|
||||
}
|
||||
);
|
||||
notebooks.push(markdown);
|
||||
} else {
|
||||
let error = localize('missingFileError', 'Missing file : {0}', sections[i].title);
|
||||
this._errorMessage = error;
|
||||
vscode.window.showErrorMessage(error);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// TODO: search functionality (#6160)
|
||||
else if (element.root.endsWith('.ipynb')) {
|
||||
let baseName: string = path.basename(element.root);
|
||||
parentPath = element.root.replace(baseName, 'readme.md');
|
||||
}
|
||||
}
|
||||
return notebooks;
|
||||
}
|
||||
|
||||
getNavigation(uri: vscode.Uri): Thenable<azdata.nb.NavigationResult> {
|
||||
let notebook = uri.scheme !== 'untitled' ? this._allNotebooks.get(uri.fsPath) : this._allNotebooks.get(path.basename(uri.fsPath));
|
||||
let result: azdata.nb.NavigationResult;
|
||||
if (notebook) {
|
||||
result = {
|
||||
hasNavigation: true,
|
||||
previous: notebook.previousUri ? this._openAsUntitled ? vscode.Uri.parse(notebook.previousUri).with({ scheme: 'untitled' }) : vscode.Uri.file(notebook.previousUri) : undefined,
|
||||
next: notebook.nextUri ? this._openAsUntitled ? vscode.Uri.parse(notebook.nextUri).with({ scheme: 'untitled' }) : vscode.Uri.file(notebook.nextUri) : undefined
|
||||
};
|
||||
else {
|
||||
return undefined;
|
||||
}
|
||||
return this.currentBook.getAllBooks().get(parentPath);
|
||||
} else {
|
||||
result = {
|
||||
hasNavigation: false,
|
||||
previous: undefined,
|
||||
next: undefined
|
||||
};
|
||||
return undefined;
|
||||
}
|
||||
return Promise.resolve(result);
|
||||
}
|
||||
|
||||
public get errorMessage() {
|
||||
return this._errorMessage;
|
||||
}
|
||||
|
||||
public get tableOfContentPaths() {
|
||||
return this._tableOfContentPaths;
|
||||
}
|
||||
|
||||
getUntitledNotebookUri(resource: string): vscode.Uri {
|
||||
let untitledFileName: vscode.Uri;
|
||||
if (process.platform.indexOf('win') > -1) {
|
||||
if (process.platform === 'win32') {
|
||||
let title = path.join(path.dirname(resource), this.findNextUntitledFileName(resource));
|
||||
untitledFileName = vscode.Uri.parse(`untitled:${title}`);
|
||||
}
|
||||
else {
|
||||
untitledFileName = vscode.Uri.parse(resource).with({ scheme: 'untitled' });
|
||||
}
|
||||
if (!this._allNotebooks.get(untitledFileName.fsPath) && !this._allNotebooks.get(path.basename(untitledFileName.fsPath))) {
|
||||
let notebook = this._allNotebooks.get(resource);
|
||||
this._allNotebooks.set(path.basename(untitledFileName.fsPath), notebook);
|
||||
if (!this.currentBook.getAllBooks().get(untitledFileName.fsPath) && !this.currentBook.getAllBooks().get(path.basename(untitledFileName.fsPath))) {
|
||||
let notebook = this.currentBook.getAllBooks().get(resource);
|
||||
this.currentBook.getAllBooks().set(path.basename(untitledFileName.fsPath), notebook);
|
||||
}
|
||||
return untitledFileName;
|
||||
}
|
||||
@@ -368,4 +294,14 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
||||
return title;
|
||||
}
|
||||
|
||||
//Confirmation message dialog
|
||||
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?"),
|
||||
default: false
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user