Fix book editing issues (#14177)

* Fix issue moving a duplicated section to another book
This commit is contained in:
Barbara Valdez
2021-02-05 18:50:29 -08:00
committed by GitHub
parent 67829af0c5
commit 467a34e878
2 changed files with 28 additions and 21 deletions

View File

@@ -33,9 +33,9 @@ export function hasSections(node: JupyterBookSection): boolean {
export class BookTocManager implements IBookTocManager { export class BookTocManager implements IBookTocManager {
public tableofContents: JupyterBookSection[]; public tableofContents: JupyterBookSection[];
public newSection: JupyterBookSection; public newSection: JupyterBookSection;
private _movedFiles = new Map<string, string>(); private _movedFiles: Map<string, string>;
private _modifiedDirectory = new Set<string>(); private _modifiedDirectory: Set<string>;
private _tocFiles = new Map<string, JupyterBookSection[]>(); private _tocFiles: Map<string, JupyterBookSection[]>;
private sourceBookContentPath: string; private sourceBookContentPath: string;
private targetBookContentPath: string; private targetBookContentPath: string;
private _sourceBook: BookModel; private _sourceBook: BookModel;
@@ -44,6 +44,9 @@ export class BookTocManager implements IBookTocManager {
this._sourceBook = sourceBook; this._sourceBook = sourceBook;
this.newSection = {}; this.newSection = {};
this.tableofContents = []; this.tableofContents = [];
this._movedFiles = new Map<string, string>();
this._modifiedDirectory = new Set<string>();
this._tocFiles = new Map<string, JupyterBookSection[]>();
this.sourceBookContentPath = sourceBook?.bookItems[0].rootContentPath; this.sourceBookContentPath = sourceBook?.bookItems[0].rootContentPath;
this.targetBookContentPath = targetBook?.bookItems[0].rootContentPath; this.targetBookContentPath = targetBook?.bookItems[0].rootContentPath;
} }
@@ -136,16 +139,22 @@ export class BookTocManager implements IBookTocManager {
await fs.rmdir(directory); await fs.rmdir(directory);
} else { } else {
contents.forEach(async (content) => { contents.forEach(async (content) => {
if ((await fs.stat(path.join(directory, content))).isFile) { let filePath = path.join(directory, content);
let fileStat = await fs.stat(filePath);
if (fileStat.isFile) {
//check if the file is in the moved files //check if the file is in the moved files
let isCopy = this._movedFiles.get(path.join(directory, content)); let newPath = this._movedFiles.get(filePath);
if (isCopy && this._movedFiles.get(path.join(directory, content)) !== path.join(directory, content)) { if (newPath) {
// the file could not be renamed, so a copy was created. let exists = await fs.pathExists(newPath);
// remove file only if the new path and old path are different // if the file exists in the new path and if the the new path and old path are different
await fs.unlink(path.join(directory, content)); // then we can remove it
if (exists && newPath !== filePath) {
// the file could not be renamed, so a copy was created.
await fs.unlink(filePath);
}
} }
} else if ((await fs.stat(path.join(directory, content))).isDirectory) { } else if (fileStat.isDirectory) {
await this.cleanUp(path.join(directory, content)); await this.cleanUp(filePath);
} }
}); });
} }
@@ -352,12 +361,12 @@ export class BookTocManager implements IBookTocManager {
* @param targetSection Book section that'll be modified. * @param targetSection Book section that'll be modified.
*/ */
public async updateBook(element: BookTreeItem, targetBook: BookTreeItem, targetSection?: JupyterBookSection): Promise<void> { public async updateBook(element: BookTreeItem, targetBook: BookTreeItem, targetSection?: JupyterBookSection): Promise<void> {
const targetBookVersion = targetBook.book.version === BookVersion.v1 ? BookVersion.v1 : BookVersion.v2; const targetBookVersion = targetBook.book.version as BookVersion;
if (element.contextValue === 'section') { if (element.contextValue === 'section') {
await this.addSection(element, targetBook);
const elementVersion = element.book.version === BookVersion.v1 ? BookVersion.v1 : BookVersion.v2;
// modify the sourceBook toc and remove the section // modify the sourceBook toc and remove the section
const findSection: JupyterBookSection = { file: element.book.page.file?.replace(/\\/g, '/'), title: element.book.page.title }; const findSection: JupyterBookSection = { file: element.book.page.file?.replace(/\\/g, '/'), title: element.book.page.title };
await this.addSection(element, targetBook);
const elementVersion = element.book.version as BookVersion;
await this.updateTOC(elementVersion, element.tableOfContentsPath, findSection, undefined); await this.updateTOC(elementVersion, element.tableOfContentsPath, findSection, undefined);
if (targetSection) { if (targetSection) {
// adding new section to the target book toc file // adding new section to the target book toc file
@@ -373,11 +382,11 @@ export class BookTocManager implements IBookTocManager {
} }
} }
else if (element.contextValue === 'savedNotebook') { else if (element.contextValue === 'savedNotebook') {
// the notebook is part of a book so we need to modify its toc as well
const findSection = { file: element.book.page?.file?.replace(/\\/g, '/'), title: element.book.page?.title };
await this.addNotebook(element, targetBook); await this.addNotebook(element, targetBook);
if (element.tableOfContentsPath) { if (element.tableOfContentsPath) {
const elementVersion = element.book.version === BookVersion.v1 ? BookVersion.v1 : BookVersion.v2; const elementVersion = element.book.version as BookVersion;
// the notebook is part of a book so we need to modify its toc as well
const findSection = { file: element.book.page.file?.replace(/\\/g, '/'), title: element.book.page.title };
await this.updateTOC(elementVersion, element.tableOfContentsPath, findSection, undefined); await this.updateTOC(elementVersion, element.tableOfContentsPath, findSection, undefined);
} else { } else {
// close the standalone notebook, so it doesn't throw an error when we move the notebook to new location. // close the standalone notebook, so it doesn't throw an error when we move the notebook to new location.
@@ -418,5 +427,4 @@ export class BookTocManager implements IBookTocManager {
public set modifiedDir(files: Set<string>) { public set modifiedDir(files: Set<string>) {
this._modifiedDirectory = files; this._modifiedDirectory = files;
} }
} }

View File

@@ -213,9 +213,9 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
const sourceBook = this.books.find(book => book.bookPath === movingElement.book.root); const sourceBook = this.books.find(book => book.bookPath === movingElement.book.root);
const targetBook = this.books.find(book => book.bookPath === updateBook.book.root); const targetBook = this.books.find(book => book.bookPath === updateBook.book.root);
this.bookTocManager = new BookTocManager(targetBook, sourceBook); this.bookTocManager = new BookTocManager(targetBook, sourceBook);
// remove watch on toc file from both books. // remove watch on toc file from source book.
if (sourceBook) { if (sourceBook) {
fs.unwatchFile(movingElement.tableOfContentsPath); fs.unwatchFile(sourceBook.tableOfContentsPath);
} }
try { try {
await this.bookTocManager.updateBook(movingElement, updateBook, targetSection); await this.bookTocManager.updateBook(movingElement, updateBook, targetSection);
@@ -232,7 +232,6 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
if (sourceBook) { if (sourceBook) {
this.setFileWatcher(sourceBook); this.setFileWatcher(sourceBook);
} }
this.setFileWatcher(targetBook);
} }
} }
} }