Add new file UI (#14773)

Adds a notebook or markdown file to a book's top level or a section by right click on Book Tree View
This commit is contained in:
Barbara Valdez
2021-03-30 18:44:52 -07:00
committed by GitHub
parent b774f09b6c
commit 8aa222d1c8
13 changed files with 428 additions and 247 deletions

View File

@@ -16,10 +16,11 @@ import { Deferred } from '../common/promise';
import { IBookTrustManager, BookTrustManager } from './bookTrustManager';
import * as loc from '../common/localizedConstants';
import * as glob from 'fast-glob';
import { getPinnedNotebooks, confirmReplace, getNotebookType } from '../common/utils';
import { getPinnedNotebooks, confirmMessageDialog, getNotebookType, FileExtension } from '../common/utils';
import { IBookPinManager, BookPinManager } from './bookPinManager';
import { BookTocManager, IBookTocManager, quickPickResults } from './bookTocManager';
import { CreateBookDialog } from '../dialog/createBookDialog';
import { AddFileDialog } from '../dialog/addFileDialog';
import { getContentPath } from './bookVersionHandler';
import { TelemetryReporter, BookTelemetryView, NbTelemetryActions } from '../telemetry';
@@ -197,37 +198,10 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
const pickedSection = selectionResults.quickPickSection;
const updateBook = selectionResults.book;
const targetSection = pickedSection.detail !== undefined ? updateBook.findChildSection(pickedSection.detail) : undefined;
if (movingElement.tableOfContents.sections) {
if (movingElement.contextValue === 'savedNotebook' || movingElement.contextValue === 'savedBookNotebook') {
let sourceBook = this.books.find(book => book.getNotebook(path.normalize(movingElement.book.contentPath)));
movingElement.tableOfContents.sections = sourceBook?.bookItems[0].sections;
}
}
const sourceBook = this.books.find(book => book.bookPath === movingElement.book.root);
const targetBook = this.books.find(book => book.bookPath === updateBook.book.root);
this.bookTocManager = new BookTocManager(targetBook, sourceBook);
// remove watch on toc file from source book.
if (sourceBook) {
sourceBook.unwatchTOC();
}
try {
await this.bookTocManager.updateBook(movingElement, updateBook, targetSection);
} catch (e) {
await this.bookTocManager.recovery();
vscode.window.showErrorMessage(loc.editBookError(updateBook.book.contentPath, e instanceof Error ? e.message : e));
} finally {
try {
await targetBook.reinitializeContents();
} finally {
if (sourceBook && sourceBook.bookPath !== targetBook.bookPath) {
// refresh source book model to pick up latest changes
await sourceBook.reinitializeContents();
}
if (sourceBook) {
sourceBook.watchTOC();
}
}
}
this.bookTocManager = new BookTocManager(sourceBook, targetBook);
await this.bookTocManager.updateBook(movingElement, updateBook, targetSection);
}
}
@@ -276,7 +250,23 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
}
}
async createMarkdownFile(bookItem: BookTreeItem): Promise<void> {
const book = this.books.find(b => b.bookPath === bookItem.root);
this.bookTocManager = new BookTocManager(book);
const dialog = new AddFileDialog(this.bookTocManager, bookItem, FileExtension.Markdown);
await dialog.createDialog();
}
async createNotebook(bookItem: BookTreeItem): Promise<void> {
const book = this.books.find(b => b.bookPath === bookItem.root);
this.bookTocManager = new BookTocManager(book);
const dialog = new AddFileDialog(this.bookTocManager, bookItem, FileExtension.Notebook);
await dialog.createDialog();
}
async removeNotebook(bookItem: BookTreeItem): Promise<void> {
const book = this.books.find(b => b.bookPath === bookItem.root);
this.bookTocManager = new BookTocManager(book);
return this.bookTocManager.removeNotebook(bookItem);
}
@@ -479,7 +469,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
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 confirmReplace(this.prompter);
let doReplace = await confirmMessageDialog(this.prompter, loc.confirmReplace);
if (!doReplace) {
return undefined;
}