Fix search for pinned notebooks (#12719)

* fix search for pinned notebooks

* fix filtering when verifying that a search folder is not a subdirectory from the current folder queries path

* Show book node on pinned notebooks search results

* fix parent node on pinned notebooks search results

* fix search for pinned notebook and modify how pinned notebooks are stored in workspace

* update format of pinned notebooks for users that used the september release version

* removed unused functions

* Address PR comments

* fix parent node for legacy version of jupyter books

* remove cast from book path
This commit is contained in:
Barbara Valdez
2020-10-06 11:54:42 -07:00
committed by GitHub
parent 288288df03
commit 825663fd77
10 changed files with 89 additions and 47 deletions

View File

@@ -35,7 +35,8 @@ export class BookModel {
public readonly bookPath: string,
public readonly openAsUntitled: boolean,
public readonly isNotebook: boolean,
private _extensionContext: vscode.ExtensionContext) {
private _extensionContext: vscode.ExtensionContext,
public readonly notebookRootPath?: string) {
this._bookItems = [];
}
@@ -107,7 +108,7 @@ export class BookModel {
let notebookItem = new BookTreeItem({
title: pathDetails.name,
contentPath: this.bookPath,
root: pathDetails.dir,
root: this.notebookRootPath ? this.notebookRootPath : pathDetails.dir,
tableOfContents: { sections: undefined },
page: { sections: undefined },
type: BookTreeItemType.Notebook,

View File

@@ -6,7 +6,7 @@ import * as path from 'path';
import * as vscode from 'vscode';
import * as constants from './../common/constants';
import { BookTreeItem } from './bookTreeItem';
import { getPinnedNotebooks } from '../common/utils';
import { getPinnedNotebooks, setPinnedBookPathsInConfig, IBookNotebook } from '../common/utils';
export interface IBookPinManager {
pinNotebook(notebook: BookTreeItem): boolean;
@@ -33,7 +33,7 @@ export class BookPinManager implements IBookPinManager {
}
isNotebookPinned(notebookPath: string): boolean {
if (getPinnedNotebooks().findIndex(x => x === notebookPath) > -1) {
if (getPinnedNotebooks().findIndex(x => x.notebookPath === notebookPath) > -1) {
return true;
}
return false;
@@ -47,41 +47,23 @@ export class BookPinManager implements IBookPinManager {
return this.updatePinnedBooks(notebook, PinBookOperation.Unpin);
}
getPinnedBookPathsInConfig(): string[] {
let config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration(constants.notebookConfigKey);
let pinnedBookDirectories: string[] = config.get(constants.pinnedBooksConfigKey);
return pinnedBookDirectories;
}
setPinnedBookPathsInConfig(bookPaths: string[]) {
let config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration(constants.notebookConfigKey);
let storeInWorspace: boolean = this.hasWorkspaceFolders();
config.update(constants.pinnedBooksConfigKey, bookPaths, storeInWorspace ? false : vscode.ConfigurationTarget.Global);
}
hasWorkspaceFolders(): boolean {
let workspaceFolders = vscode.workspace.workspaceFolders;
return workspaceFolders && workspaceFolders.length > 0;
}
updatePinnedBooks(notebook: BookTreeItem, operation: PinBookOperation) {
let modifiedPinnedBooks = false;
let bookPathToChange: string = notebook.book.contentPath;
let pinnedBooks: string[] = this.getPinnedBookPathsInConfig();
let existingBookIndex = pinnedBooks.map(pinnedBookPath => path.normalize(pinnedBookPath)).indexOf(bookPathToChange);
let pinnedBooks: IBookNotebook[] = getPinnedNotebooks();
let existingBookIndex = pinnedBooks.map(pinnedBookPath => path.normalize(pinnedBookPath?.notebookPath)).indexOf(bookPathToChange);
if (existingBookIndex !== -1 && operation === PinBookOperation.Unpin) {
pinnedBooks.splice(existingBookIndex, 1);
modifiedPinnedBooks = true;
} else if (existingBookIndex === -1 && operation === PinBookOperation.Pin) {
pinnedBooks.push(bookPathToChange);
let addNotebook: IBookNotebook = { notebookPath: bookPathToChange, bookPath: notebook.book.root };
pinnedBooks.push(addNotebook);
modifiedPinnedBooks = true;
}
this.setPinnedBookPathsInConfig(pinnedBooks);
setPinnedBookPathsInConfig(pinnedBooks);
this.setPinnedSectionContext();
return modifiedPinnedBooks;

View File

@@ -39,6 +39,7 @@ export class BookTreeItem extends vscode.TreeItem {
private _nextUri: string;
public readonly version: string;
public command: vscode.Command;
public resourceUri: vscode.Uri;
constructor(public book: BookTreeItemFormat, icons: any) {
super(book.title, book.treeItemCollapsibleState);
@@ -74,6 +75,7 @@ export class BookTreeItem extends vscode.TreeItem {
}
else {
this.tooltip = this.book.type === BookTreeItemType.Book ? (this.book.version === BookVersion.v1 ? path.join(this.book.root, content) : this.book.root) : this.book.contentPath;
this.resourceUri = vscode.Uri.file(this.book.root);
}
}

View File

@@ -57,9 +57,9 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
private async initialize(workspaceFolders: vscode.WorkspaceFolder[]): Promise<void> {
if (this.viewId === constants.PINNED_BOOKS_VIEWID) {
await Promise.all(getPinnedNotebooks().map(async (notebookPath) => {
await Promise.all(getPinnedNotebooks().map(async (notebook) => {
try {
await this.createAndAddBookModel(notebookPath, true);
await this.createAndAddBookModel(notebook.notebookPath, true, notebook.bookPath);
} catch {
// no-op, not all workspace folders are going to be valid books
}
@@ -90,7 +90,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
}
trustBook(bookTreeItem?: BookTreeItem): void {
let bookPathToTrust = bookTreeItem ? bookTreeItem.root : this.currentBook?.bookPath;
let bookPathToTrust: string = bookTreeItem ? bookTreeItem.root : this.currentBook?.bookPath;
if (bookPathToTrust) {
let trustChanged = this._bookTrustManager.setBookAsTrusted(bookPathToTrust);
if (trustChanged) {
@@ -169,7 +169,8 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
async addNotebookToPinnedView(bookItem: BookTreeItem): Promise<void> {
let notebookPath: string = bookItem.book.contentPath;
if (notebookPath) {
await this.createAndAddBookModel(notebookPath, true);
let rootPath: string = bookItem.book.root ? bookItem.book.root : '';
await this.createAndAddBookModel(notebookPath, true, rootPath);
}
}
@@ -215,10 +216,12 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
* 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.
* @param bookPath The path to the book folder to create the model for
* @param isNotebook A boolean value to know we are creating a model for a notebook or a book
* @param notebookBookRoot For pinned notebooks we need to know if the notebook is part of a book or it's a standalone notebook
*/
private async createAndAddBookModel(bookPath: string, isNotebook: boolean): Promise<void> {
private async createAndAddBookModel(bookPath: string, isNotebook: boolean, notebookBookRoot?: string): Promise<void> {
if (!this.books.find(x => x.bookPath === bookPath)) {
const book: BookModel = new BookModel(bookPath, this._openAsUntitled, isNotebook, this._extensionContext);
const book: BookModel = new BookModel(bookPath, this._openAsUntitled, isNotebook, this._extensionContext, notebookBookRoot);
await book.initializeContents();
this.books.push(book);
if (!this.currentBook) {