Fix/open book error (#11379)

* add isNotebook param and showPreview option

* showPreview changes

* update OpenNotebookFolder to open a specific path

* added test for showPreviewFile

* test name typo

* remove isNotebook from openBook
This commit is contained in:
Maddy
2020-07-22 22:23:37 -07:00
committed by GitHub
parent a889d017f5
commit 21cf89fb7e
3 changed files with 74 additions and 28 deletions

View File

@@ -190,17 +190,37 @@ 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'));
// The Notebook editor expects a posix path for the resource (it will still resolve to the correct fsPath based on OS)
const sectionToOpenNotebook: string = path.posix.join(this.currentBook.bookPath, Content, urlPath.concat('.ipynb'));
if (await fs.pathExists(sectionToOpenMarkdown)) {
this.openMarkdown(sectionToOpenMarkdown);
let urlPath: string;
if (this.currentBook.isNotebook) {
urlPath = urlToOpen && this.currentBook.bookPath === urlToOpen ? this.currentBook.bookPath : undefined;
} else {
if (urlToOpen) {
const bookRoot = this.currentBook.bookItems[0];
const sectionToOpen = bookRoot.findChildSection(urlToOpen);
urlPath = sectionToOpen?.url;
} else {
urlPath = this.currentBook.bookItems[0].tableOfContents.sections[0].url;
}
}
else if (await fs.pathExists(sectionToOpenNotebook)) {
await this.openNotebook(sectionToOpenNotebook);
if (urlPath) {
if (this.currentBook.isNotebook) {
if (urlPath.endsWith('.md')) {
this.openMarkdown(urlPath);
}
else if (urlPath.endsWith('.ipynb')) {
await this.openNotebook(urlPath);
}
} else {
// The Notebook editor expects a posix path for the resource (it will still resolve to the correct fsPath based on OS)
const sectionToOpenMarkdown: string = path.posix.join(this.currentBook.bookPath, Content, urlPath.concat('.md'));
const sectionToOpenNotebook: string = path.posix.join(this.currentBook.bookPath, Content, urlPath.concat('.ipynb'));
if (await fs.pathExists(sectionToOpenMarkdown)) {
this.openMarkdown(sectionToOpenMarkdown);
}
else if (await fs.pathExists(sectionToOpenNotebook)) {
await this.openNotebook(sectionToOpenNotebook);
}
}
}
}
}
@@ -383,29 +403,33 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
}
}
public async openNotebookFolder(): 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.labelSelectFolder
});
if (uris && uris.length > 0) {
await this.loadNotebooksInFolder(uris[0]?.fsPath);
public async openNotebookFolder(folderPath?: string, urlToOpen?: string, showPreview?: boolean): Promise<void> {
if (!folderPath) {
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.labelSelectFolder
});
folderPath = uris && uris.length > 0 ? uris[0].fsPath : undefined;
}
if (folderPath) {
await this.loadNotebooksInFolder(folderPath, urlToOpen, showPreview);
}
}
public async loadNotebooksInFolder(folderPath: string) {
public async loadNotebooksInFolder(folderPath: string, urlToOpen?: string, showPreview?: boolean) {
let bookCollection = await this.getNotebooksInTree(folderPath);
for (let i = 0; i < bookCollection.bookPaths.length; i++) {
await this.openBook(bookCollection.bookPaths[i], undefined, false);
await this.openBook(bookCollection.bookPaths[i], urlToOpen, showPreview);
}
for (let i = 0; i < bookCollection.notebookPaths.length; i++) {
await this.openBook(bookCollection.notebookPaths[i], undefined, false, true);
await this.openBook(bookCollection.notebookPaths[i], urlToOpen, showPreview, true);
}
}