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

@@ -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.