From 357aba8e0c7a60ab58760cffeba7a03a62b90dfe Mon Sep 17 00:00:00 2001 From: lianahus Date: Thu, 25 May 2023 13:36:41 +0200 Subject: [PATCH] cleanup fix undefined workspaces. Creating default Workspace --- apps/remix-ide/src/app/files/fileManager.ts | 40 +++++++++++++--- .../src/lib/components/homeTabFile.tsx | 48 ++++++++++++++----- 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/apps/remix-ide/src/app/files/fileManager.ts b/apps/remix-ide/src/app/files/fileManager.ts index a145a462d8..7022e34bc2 100644 --- a/apps/remix-ide/src/app/files/fileManager.ts +++ b/apps/remix-ide/src/app/files/fileManager.ts @@ -22,9 +22,11 @@ const profile = { icon: 'assets/img/fileManager.webp', permission: true, version: packageJson.version, - methods: ['closeAllFiles', 'closeFile', 'file', 'exists', 'open', 'writeFile', 'writeMultipleFiles', 'readFile', 'copyFile', 'copyDir', 'rename', 'mkdir', - 'readdir', 'dirList', 'fileList', 'remove', 'getCurrentFile', 'getFile', 'getFolder', 'setFile', 'switchFile', 'refresh', - 'getProviderOf', 'getProviderByName', 'getPathFromUrl', 'getUrlFromPath', 'saveCurrentFile', 'setBatchFiles', 'isGitRepo'], + methods: ['closeAllFiles', 'closeFile', 'file', 'exists', 'open', 'writeFile', 'writeMultipleFiles', 'writeFileNoRewrite', + 'readFile', 'copyFile', 'copyDir', 'rename', 'mkdir', 'readdir', 'dirList', 'fileList', 'remove', 'getCurrentFile', 'getFile', + 'getFolder', 'setFile', 'switchFile', 'refresh', 'getProviderOf', 'getProviderByName', 'getPathFromUrl', 'getUrlFromPath', + 'saveCurrentFile', 'setBatchFiles', 'isGitRepo' + ], kind: 'file-system' } const errorMsg = { @@ -37,7 +39,6 @@ const errorMsg = { const createError = (err) => { return new Error(`${errorMsg[err.code]} ${err.message || ''}`) } - class FileManager extends Plugin { mode: string openedFiles: any @@ -206,11 +207,11 @@ class FileManager extends Plugin { path = this.normalize(path) path = this.limitPluginScope(path) if (await this.exists(path)) { - const newPath = await helper.createNonClashingNameAsync(path, this) - //await this._handleIsFile(path, `Cannot write file ${path}`) - return await this.setFileContent(newPath, data) + await this._handleIsFile(path, `Cannot write file ${path}`) + return await this.setFileContent(path, data) } else { const ret = await this.setFileContent(path, data) + this.emit('fileAdded', path) return ret } } catch (e) { @@ -249,6 +250,31 @@ class FileManager extends Plugin { throw new Error(e) } } + + /** + * Set the content of a specific file, does nnot rewrite file if it exists but creates a new unique name + * @param {string} path path of the file + * @param {string} data content to write on the file + * @returns {void} + */ + async writeFileNoRewrite(path, data) { + try { + path = this.normalize(path) + path = this.limitPluginScope(path) + if (await this.exists(path)) { + const newPath = await helper.createNonClashingNameAsync(path, this) + const content = await this.setFileContent(newPath, data) + console.log("newp in write ", newPath) + return {newContent: content, newPath} + } else { + const ret = await this.setFileContent(path, data) + this.emit('fileAdded', path) + return {newContent: ret, newpath: path} + } + } catch (e) { + throw new Error(e) + } + } /** * Return the content of a specific file diff --git a/libs/remix-ui/home-tab/src/lib/components/homeTabFile.tsx b/libs/remix-ui/home-tab/src/lib/components/homeTabFile.tsx index b9046cf574..59f5165257 100644 --- a/libs/remix-ui/home-tab/src/lib/components/homeTabFile.tsx +++ b/libs/remix-ui/home-tab/src/lib/components/homeTabFile.tsx @@ -5,6 +5,7 @@ import {ModalDialog} from '@remix-ui/modal-dialog' // eslint-disable-line import {Toaster} from '@remix-ui/toaster' // eslint-disable-line const _paq = window._paq = window._paq || [] // eslint-disable-line import {CustomTooltip} from '@remix-ui/helper'; +import {TEMPLATE_NAMES} from '@remix-ui/workspace' interface HomeTabFileProps { plugin: any @@ -56,10 +57,8 @@ function HomeTabFile({plugin}: HomeTabFileProps) { const inputValue = useRef(null) useEffect(() => { - console.log("useEffHomeFile") plugin.on('filePanel', 'setWorkspace', async () => { let recents = JSON.parse(localStorage.getItem('recentWorkspaces')) - console.log("useEffHomeFilerecents ", recents) if (!recents) recents = {first:'', second: '', third: ''} setState(prevState => { @@ -115,6 +114,15 @@ function HomeTabFile({plugin}: HomeTabFileProps) { const startCoding = async () => { _paq.push(['trackEvent', 'hometab', 'filesSection', 'startCoding']) plugin.verticalIcons.select('filePanel') + + const wName = 'Coding playground' + const workspaces = await plugin.call('filePanel', 'getWorkspaces') + if (!workspaces.find(workspace => workspace.name === wName)) { + await plugin.call('filePanel', 'createWorkspace', wName, 'remixDefault') + } + await plugin.call('filePanel', 'switchToWorkspace', { name: wName, isLocalHost: false }) + await plugin.call('filePanel', 'switchToWorkspace', { name: wName, isLocalHost: false }) // don't ask why + const content = ` // SPDX-License-Identifier: MIT pragma solidity >=0.7.0 <0.9.0; @@ -122,8 +130,8 @@ function HomeTabFile({plugin}: HomeTabFileProps) { contract helloWorld { } ` - await plugin.call('fileManager', 'writeFile', '/helloWorld.sol', content) - await plugin.call('fileManager', 'open', '/helloWorld.sol') + const {newContent, newPath} = await plugin.call('fileManager', 'writeFileNoRewrite', '/contracts/helloWorld.sol', content) + await plugin.call('fileManager', 'open', newPath) } const uploadFile = async (target) => { @@ -165,8 +173,12 @@ function HomeTabFile({plugin}: HomeTabFileProps) { const handleSwichToRecentWorkspace = async (e, workspaceName) => { e.preventDefault(); +<<<<<<< HEAD await plugin.call('filePanel', 'switchToWorkspace', { name: workspaceName, isLocalhost: false }) console.log('The link was clicked.'); +======= + await plugin.call('filePanel', 'switchToWorkspace', { name: workspaceName, isLocalhost: false }) +>>>>>>> e9b2848c5... cleanup fix undefined workspaces. Creating default Workspace } const examples = state.modalInfo.examples.map((urlEl, key) => ( @@ -196,15 +208,26 @@ function HomeTabFile({plugin}: HomeTabFileProps) { {state.modalInfo.prefix && ipfs://} >>>>>> e9b2848c5... cleanup fix undefined workspaces. Creating default Workspace className="w-100 mt-1 form-control" data-id="homeTabModalDialogCustomPromptText" value={state.importSource} onInput={(e) => { +<<<<<<< HEAD setState((prevState) => { return {...prevState, importSource: inputValue.current.value} +======= + setState(prevState => { + return { ...prevState, importSource: inputValue.current.value } +>>>>>>> e9b2848c5... cleanup fix undefined workspaces. Creating default Workspace }) }} /> @@ -255,24 +278,27 @@ function HomeTabFile({plugin}: HomeTabFileProps) { + { !(state.recentWorkspaces.first == '' && + state.recentWorkspaces.second == '' && + state.recentWorkspaces.third == '') &&
- handleSwichToRecentWorkspace(e, state.recentWorkspaces.first)}>{state.recentWorkspaces.first} - - } + {(state.recentWorkspaces.second !== 'undefined' && state.recentWorkspaces.second !== '') && handleSwichToRecentWorkspace(e, state.recentWorkspaces.second)}>{state.recentWorkspaces.second} - - } + {(state.recentWorkspaces.third !== 'undefined' && state.recentWorkspaces.third !== '') && handleSwichToRecentWorkspace(e, state.recentWorkspaces.third)}>{state.recentWorkspaces.third} - -
+ } + }