diff --git a/src/app/files/file-explorer.js b/src/app/files/file-explorer.js index 4c7f0cb332..e2c5f83503 100644 --- a/src/app/files/file-explorer.js +++ b/src/app/files/file-explorer.js @@ -213,7 +213,7 @@ function fileExplorer (localRegistry, files, menuItems) { modalDialogCustom.confirm( 'Discard changes', 'Are you sure you want to discard all your changes?', - () => { files.discardChanges(key) }, + () => { self.files.discardChanges(key) }, () => {} ) } @@ -244,6 +244,9 @@ function fileExplorer (localRegistry, files, menuItems) { modalDialogCustom.confirm('Confirm to delete a folder', 'Are you sure you want to delete this folder?', () => { if (!files.remove(key)) tooltip(`failed to remove ${key}. Make sure the directory is empty before removing it.`) + else { + self.updatePath('browser') + } }, () => {}) } MENU_HANDLE = contextMenu(event, actions) @@ -265,7 +268,10 @@ function fileExplorer (localRegistry, files, menuItems) { if (self.files.isReadOnly(key)) { return tooltip('cannot delete file. ' + self.files.type + ' is a read only explorer') } modalDialogCustom.confirm( 'Delete a file', 'Are you sure you want to delete this file?', - () => { files.remove(key) }, + () => { + files.remove(key) + self.updatePath('browser') + }, () => {} ) } @@ -599,14 +605,24 @@ fileExplorer.prototype.createNewFile = function (parentFolder = 'browser') { fileExplorer.prototype.createNewFolder = function (parentFolder) { let self = this - modalDialogCustom.prompt('Create new folder', '', '', (input) => { + modalDialogCustom.prompt('Create new folder', '', 'New folder', (input) => { + if (input === '') { + modalDialogCustom.alert('Failed to create folder. The name can not be empty') + return false + } + const currentPath = !parentFolder ? self._deps.fileManager.currentPath() : parentFolder let newName = currentPath ? currentPath + '/' + input : self.files.type + '/' + input newName = newName + '/' - if (!self.files.set(newName, '')) { - modalDialogCustom.alert('Failed to create folder ' + newName) - } + self.files.exists(newName, (error, exist) => { + if (error) return modalDialogCustom.alert('Unexpected error while creating folder: ' + error) + if (!exist) { + self.files.set(newName, '') + } else { + modalDialogCustom.alert('Folder already exists.', () => {}) + } + }) }, null, true) } diff --git a/src/app/files/fileProvider.js b/src/app/files/fileProvider.js index b7cfb2822e..8aaf2d238f 100644 --- a/src/app/files/fileProvider.js +++ b/src/app/files/fileProvider.js @@ -64,6 +64,8 @@ class FileProvider { } exists (path, cb) { + // todo check the type (directory/file) as well #2386 + // currently it is not possible to have a file and folder with same path cb(null, this._exists(path)) } @@ -138,22 +140,36 @@ class FileProvider { remove (path) { path = this.removePrefix(path) if (window.remixFileSystem.existsSync(path)) { - window.remixFileSystem.readdirSync(path).forEach((file, index) => { - let curPath = path + '/' + file - let stat = window.remixFileSystem.statSync(curPath) - try { - if (stat.isDirectory()) { - this.remove(curPath) - window.remixFileSystem.rmdirSync(curPath, console.log) - } else { // delete file - window.remixFileSystem.unlinkSync(curPath, console.log) + const stat = window.remixFileSystem.statSync(path) + try { + if (!stat.isDirectory()) { + window.remixFileSystem.unlinkSync(path, console.log) + this.event.trigger('fileRemoved', [this._normalizePath(path)]) + return true + } else { + let items = window.remixFileSystem.readdirSync(path) + if (items.length !== 0) { + items.forEach((item, index) => { + let curPath = path + '/' + item + if (window.remixFileSystem.statSync(curPath).isDirectory()) { // delete folder + this.remove(curPath) + } else { // delete file + window.remixFileSystem.unlinkSync(curPath, console.log) + this.event.trigger('fileRemoved', [this._normalizePath(path)]) + } + }) + if (window.remixFileSystem.readdirSync(path).length === 0) window.remixFileSystem.rmdirSync(path, console.log) + } else { + // folder is empty + window.remixFileSystem.rmdirSync(path, console.log) } - } catch (e) { - console.log(e) - return false } - }) + } catch (e) { + console.log(e) + return false + } } + return true } rename (oldPath, newPath, isFolder) {