Feat/close book (#9453)

* initial commit

* Add Reveal in Books editor tab context option

* Select item in books viewlet automatically

* changes

* easier than i thought it'd be

* added file watcher on toc file v1

* Merge from Feat/create book

* Undo Merge from Feat/create book

* Use fsPath instead of path

* repen book on toc update

* update book in-place

* fix close book

* error handling for closeBook

* PR comments

* addressed comments

* moved the watch block to try ,watch vs watchFile

Co-authored-by: chlafreniere <hichise@gmail.com>
Co-authored-by: Chris LaFreniere <40371649+chlafreniere@users.noreply.github.com>
This commit is contained in:
Maddy
2020-03-06 14:55:22 -08:00
committed by GitHub
parent 6f17f0de58
commit 9cc31bee62
7 changed files with 55 additions and 1 deletions

View File

@@ -186,6 +186,10 @@
"light": "resources/light/open_notebook.svg"
}
},
{
"command": "notebook.command.closeBook",
"title": "%title.closeJupyterBook%"
},
{
"command": "notebook.command.createBook",
"title": "%title.createJupyterBook%",
@@ -288,6 +292,10 @@
"command": "notebook.command.searchUntitledBook",
"when": "false"
},
{
"command": "notebook.command.closeBook",
"when": "false"
},
{
"command": "notebook.command.revealInBooksViewlet",
"when": "false"
@@ -332,6 +340,10 @@
"command": "notebook.command.saveBook",
"when": "view == unsavedBookTreeView && viewItem == unsavedBook && unsavedBooks",
"group": "inline"
},
{
"command": "notebook.command.closeBook",
"when": "view == bookTreeView && viewItem == savedBook"
}
],
"view/title": [

View File

@@ -35,6 +35,7 @@
"title.UnsavedBooks": "Unsaved Books",
"title.PreviewLocalizedBook": "Get localized SQL Server 2019 guide",
"title.openJupyterBook": "Open Jupyter Book",
"title.closeJupyterBook": "Close Jupyter Book",
"title.revealInBooksViewlet": "Reveal in Books",
"title.createJupyterBook": "Create Book"
}

View File

@@ -37,6 +37,8 @@ export class BookModel implements azdata.nb.NavigationProvider {
}
public async initializeContents(): Promise<void> {
this._tableOfContentPaths = [];
this._bookItems = [];
await this.getTableOfContentFiles(this.bookPath);
await this.readBooks();
}
@@ -157,8 +159,8 @@ export class BookModel implements azdata.nb.NavigationProvider {
let uriToNotebook: vscode.Uri = vscode.Uri.file(pathToNotebook);
if (!this._allNotebooks.get(uriToNotebook.fsPath)) {
this._allNotebooks.set(uriToNotebook.fsPath, notebook);
notebooks.push(notebook);
}
notebooks.push(notebook);
}
} else if (await fs.pathExists(pathToMarkdown)) {
let markdown: BookTreeItem = new BookTreeItem({

View File

@@ -7,6 +7,7 @@ import * as azdata from 'azdata';
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs-extra';
import * as fsw from 'fs';
import { IPrompter, QuestionTypes, IQuestion } from '../prompts/question';
import CodeAdapter from '../prompts/adapter';
import { BookTreeItem } from './bookTreeItem';
@@ -74,11 +75,43 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
bookViewer.reveal(this.currentBook.bookItems[0], { expand: vscode.TreeItemCollapsibleState.Expanded, focus: true, select: true });
await this.showPreviewFile(urlToOpen);
}
// add file watcher on toc file.
fsw.watch(path.join(bookPath, '_data', 'toc.yml'), async (event, filename) => {
if (event === 'change') {
let index = this.books.findIndex(book => book.bookPath === bookPath);
await this.books[index].initializeContents().then(() => {
this._onDidChangeTreeData.fire(this.books[index].bookItems[0]);
});
this._onDidChangeTreeData.fire();
}
});
} catch (e) {
vscode.window.showErrorMessage(loc.openFileError(bookPath, e instanceof Error ? e.message : e));
}
}
async closeBook(book: BookTreeItem): Promise<void> {
// remove book from the saved books
let deletedBook: BookModel;
try {
let index: number = this.books.indexOf(this.books.find(b => b.bookPath.replace(/\\/g, '/') === book.root));
if (index > -1) {
deletedBook = this.books.splice(index, 1)[0];
if (this.currentBook === deletedBook) {
this.currentBook = this.books.length > 0 ? this.books[this.books.length - 1] : undefined;
}
this._onDidChangeTreeData.fire();
}
} catch (e) {
vscode.window.showErrorMessage(loc.closeBookError(book.root, e instanceof Error ? e.message : e));
} finally {
// remove watch on toc file.
if (deletedBook) {
fsw.unwatchFile(path.join(deletedBook.bookPath, '_data', 'toc.yml'));
}
}
}
/**
* Creates a model for the specified folder path and adds it to the known list of books if we
* were able to successfully parse it.

View File

@@ -33,3 +33,4 @@ export function openNotebookError(resource: string, error: string): string { ret
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); }
export function closeBookError(resource: string, error: string): string { return localize('closeBookError', "Close book {0} failed: {1}", resource, error); }

View File

@@ -39,6 +39,7 @@ export async function activate(extensionContext: vscode.ExtensionContext): Promi
extensionContext.subscriptions.push(vscode.commands.registerCommand('notebook.command.searchBook', (item) => bookTreeViewProvider.searchJupyterBooks(item)));
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.closeBook', (book: any) => bookTreeViewProvider.closeBook(book)));
extensionContext.subscriptions.push(vscode.commands.registerCommand('notebook.command.createBook', async () => {
let untitledFileName: vscode.Uri = vscode.Uri.parse(`untitled:${createBookPath}`);

View File

@@ -114,6 +114,7 @@ describe('BookTreeViewProviderTests', function () {
await fs.writeFile(notebook2File, '');
await fs.writeFile(notebook3File, '');
await fs.writeFile(markdownFile, '');
appContext = new AppContext(undefined, new ApiWrapper());
});
it('should initialize correctly with empty workspace array', async () => {
@@ -230,6 +231,7 @@ describe('BookTreeViewProviderTests', function () {
bookTreeViewProvider = new BookTreeViewProvider(appContext.apiWrapper, [folder], mockExtensionContext, false, 'bookTreeView');
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
appContext = new AppContext(undefined, new ApiWrapper());
});
it('should ignore toc.yml files not in _data folder', async () => {
@@ -273,6 +275,7 @@ describe('BookTreeViewProviderTests', function () {
bookTreeViewProvider = new BookTreeViewProvider(appContext.apiWrapper, [folder], mockExtensionContext, false, 'bookTreeView');
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
appContext = new AppContext(undefined, new ApiWrapper());
});
it('should show error message if config.yml file not found', async () => {
@@ -332,6 +335,7 @@ describe('BookTreeViewProviderTests', function () {
bookTreeViewProvider = new BookTreeViewProvider(appContext.apiWrapper, [folder], mockExtensionContext, false, 'bookTreeView');
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
appContext = new AppContext(undefined, new ApiWrapper());
});
it('should show error if notebook or markdown file is missing', async function (): Promise<void> {