From dea284635cba52764407681f2e78b2b56354d00f Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Wed, 19 May 2021 17:18:14 +0100 Subject: [PATCH] Implement copyFile --- apps/remix-ide/src/app/files/fileManager.js | 10 ++++---- .../src/lib/file-explorer-context-menu.tsx | 2 +- .../file-explorer/src/lib/file-explorer.tsx | 24 +++++++++++++++---- .../file-explorer/src/lib/types/index.ts | 2 +- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/apps/remix-ide/src/app/files/fileManager.js b/apps/remix-ide/src/app/files/fileManager.js index 44e33ff0d2..65b34c9b80 100644 --- a/apps/remix-ide/src/app/files/fileManager.js +++ b/apps/remix-ide/src/app/files/fileManager.js @@ -217,12 +217,14 @@ class FileManager extends Plugin { try { src = this.limitPluginScope(src) dest = this.limitPluginScope(dest) - await this._handleExists(src, `Cannot copy from ${src}`) - await this._handleIsFile(src, `Cannot copy from ${src}`) - await this._handleIsFile(dest, `Cannot paste content into ${dest}`) + await this._handleExists(src, `Cannot copy from ${src}. Path does not exist.`) + await this._handleIsFile(src, `Cannot copy from ${src}. Path is not a file.`) + 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 = `/Copy_${src.split('/')[src.split('/').length - 1]}` - await this.writeFile(dest, content) + await this.writeFile(dest + copiedFileName, content) } catch (e) { throw new Error(e) } diff --git a/libs/remix-ui/file-explorer/src/lib/file-explorer-context-menu.tsx b/libs/remix-ui/file-explorer/src/lib/file-explorer-context-menu.tsx index a63633e3b2..f7c5302b06 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer-context-menu.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer-context-menu.tsx @@ -65,7 +65,7 @@ export const FileExplorerContextMenu = (props: FileExplorerContextMenuProps) => copy(path, type) break case 'Paste': - paste(path) + paste(path, type) break default: emit && emit(item.id, path) 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 6135c6ecc6..53a752e45d 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx @@ -121,7 +121,7 @@ export const FileExplorer = (props: FileExplorerProps) => { reservedKeywords: [name, 'gist-'], copyElement: [] }) - const [canPaste, setcanPaste] = useState(false) + const [canPaste, setCanPaste] = useState(false) const [fileSystem, dispatch] = useReducer(fileSystemReducer, fileSystemInitialState) const editRef = useRef(null) @@ -413,6 +413,16 @@ export const FileExplorer = (props: FileExplorerProps) => { }) } + const copyFile = (src: string, dest: string) => { + const fileManager = state.fileManager + + try { + fileManager.copyFile(src, dest) + } catch (error) { + console.log('Oops! An error ocurred while performing copyFile operation.' + error) + } + } + const publishToGist = (path?: string, type?: string) => { modal('Create a public gist', `Are you sure you want to anonymously publish all your files in the ${name} workspace as a public gist on github.com?`, 'OK', () => toGist(path, type), 'Cancel', () => {}) } @@ -734,15 +744,19 @@ export const FileExplorer = (props: FileExplorerProps) => { setState(prevState => { return { ...prevState, copyElement: [...prevState.copyElement, { key: path, type }] } }) - setcanPaste(true) + setCanPaste(true) + toast('Copied to clipboard') } - const handlePasteClick = (dest: string) => { - console.log('destination: ', dest) + const handlePasteClick = (dest: string, destType: string) => { + dest = destType === 'file' ? extractParentFromKey(dest) || props.name : dest + state.copyElement.map(({ key, type }) => { + type === 'file' ? copyFile(key, dest) : copyFile(key, dest) + }) setState(prevState => { return { ...prevState, copyElement: [] } }) - setcanPaste(false) + setCanPaste(false) } const label = (file: File) => { diff --git a/libs/remix-ui/file-explorer/src/lib/types/index.ts b/libs/remix-ui/file-explorer/src/lib/types/index.ts index 5c29ebac8e..e89361378c 100644 --- a/libs/remix-ui/file-explorer/src/lib/types/index.ts +++ b/libs/remix-ui/file-explorer/src/lib/types/index.ts @@ -48,5 +48,5 @@ export interface FileExplorerContextMenuProps { type: string, onMouseOver?: (...args) => void, copy?: (path: string, type: string) => void - paste?: (destination: string) => void + paste?: (destination: string, type: string) => void }