From 3ef80e0e3edcc37dfd03f9ecdedb0afcf3beba0e Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Wed, 26 May 2021 03:29:27 +0100 Subject: [PATCH 1/2] Allow multiple pasting of files and folders with non-clashing names. --- apps/remix-ide/src/app/files/fileManager.js | 8 +++++--- apps/remix-ide/src/lib/helper.js | 14 ++++++++++++++ .../file-explorer/src/lib/file-explorer.tsx | 4 ---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/apps/remix-ide/src/app/files/fileManager.js b/apps/remix-ide/src/app/files/fileManager.js index 79664f5799..7bd7b04066 100644 --- a/apps/remix-ide/src/app/files/fileManager.js +++ b/apps/remix-ide/src/app/files/fileManager.js @@ -222,9 +222,10 @@ class FileManager extends Plugin { await this._handleExists(dest, `Cannot paste content into ${dest}. Path does not exist.`) await this._handleIsDir(dest, `Cannot paste content into ${dest}. Path is not directory.`) const content = await this.readFile(src) - const copiedFileName = customName ? '/' + customName : '/' + `Copy_${helper.extractNameFromKey(src)}` + let copiedFilePath = dest + (customName ? '/' + customName : '/' + `Copy_${helper.extractNameFromKey(src)}`) + copiedFilePath = await helper.createNonClashingNameAsync(copiedFilePath, this) - await this.writeFile(dest + copiedFileName, content) + await this.writeFile(copiedFilePath, content) } catch (e) { throw new Error(e) } @@ -252,7 +253,8 @@ class FileManager extends Plugin { async inDepthCopy (src, dest, count = 0) { const content = await this.readdir(src) - const copiedFolderPath = count === 0 ? dest + '/' + `Copy_${helper.extractNameFromKey(src)}` : dest + '/' + helper.extractNameFromKey(src) + let copiedFolderPath = count === 0 ? dest + '/' + `Copy_${helper.extractNameFromKey(src)}` : dest + '/' + helper.extractNameFromKey(src) + copiedFolderPath = await helper.createNonClashingDirNameAsync(copiedFolderPath, this) await this.mkdir(copiedFolderPath) diff --git a/apps/remix-ide/src/lib/helper.js b/apps/remix-ide/src/lib/helper.js index b910dccfb3..0486c557d0 100644 --- a/apps/remix-ide/src/lib/helper.js +++ b/apps/remix-ide/src/lib/helper.js @@ -71,6 +71,20 @@ module.exports = { return name + counter + prefix + '.' + ext }, + async createNonClashingDirNameAsync (name, fileManager) { + if (!name) name = 'Undefined' + let counter = '' + let exist = true + + do { + const isDuplicate = await fileManager.exists(name + counter) + + if (isDuplicate) counter = (counter | 0) + 1 + else exist = false + } while (exist) + + return name + counter + }, checkSpecialChars (name) { return name.match(/[:*?"<>\\'|]/) != null }, diff --git a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx index a176909b94..a17f2c3faa 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx @@ -763,10 +763,6 @@ export const FileExplorer = (props: FileExplorerProps) => { state.copyElement.map(({ key, type }) => { type === 'file' ? copyFile(key, dest) : copyFolder(key, dest) }) - setState(prevState => { - return { ...prevState, copyElement: [] } - }) - setCanPaste(false) } const label = (file: File) => { From 0e7949c5a71eca02015c86651e0e6d1135082099 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Wed, 26 May 2021 10:29:54 +0100 Subject: [PATCH 2/2] Fixed failing test --- apps/remix-ide-e2e/src/tests/fileManager_api.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/remix-ide-e2e/src/tests/fileManager_api.spec.ts b/apps/remix-ide-e2e/src/tests/fileManager_api.spec.ts index ec4af936d0..26a49d9beb 100644 --- a/apps/remix-ide-e2e/src/tests/fileManager_api.spec.ts +++ b/apps/remix-ide-e2e/src/tests/fileManager_api.spec.ts @@ -147,8 +147,8 @@ const executeReadFile = ` const executeCopyFile = ` const run = async () => { - await remix.call('fileManager', 'copyFile', 'contracts/3_Ballot.sol', '/', 'new_contract.sol') - const result = await remix.call('fileManager', 'readFile', 'new_contract.sol') + await remix.call('fileManager', 'copyFile', 'contracts/3_Ballot.sol', '/', 'copy_contract.sol') + const result = await remix.call('fileManager', 'readFile', 'copy_contract.sol') console.log(result) }