cleanup fix undefined workspaces. Creating default Workspace

pull/5370/head
lianahus 2 years ago
parent 22a5800e0d
commit 357aba8e0c
  1. 40
      apps/remix-ide/src/app/files/fileManager.ts
  2. 48
      libs/remix-ui/home-tab/src/lib/components/homeTabFile.tsx

@ -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

@ -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 && <span className="text-nowrap align-self-center mr-2">ipfs://</span>}
<input
ref={inputValue}
<<<<<<< HEAD
type="text"
name="prompt_text"
id="inputPrompt_text"
=======
type='text'
name='prompt_text'
id='inputPrompt_text'
>>>>>>> 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) {
</button>
</CustomTooltip>
</div>
{ !(state.recentWorkspaces.first == '' &&
state.recentWorkspaces.second == '' &&
state.recentWorkspaces.third == '') &&
<div className='d-flex flex-column'>
<label style={{fontSize: "0.8rem"}} className='mt-3'>Recent workspaces</label>
<a
{(state.recentWorkspaces.first !== 'undefined' && state.recentWorkspaces.first !== '') && <a
className='cursor-pointer mb-1 ml-2'
href="#"
onClick={(e) => handleSwichToRecentWorkspace(e, state.recentWorkspaces.first)}>{state.recentWorkspaces.first}
</a>
<a
</a> }
{(state.recentWorkspaces.second !== 'undefined' && state.recentWorkspaces.second !== '') && <a
className='cursor-pointer mb-1 ml-2'
href="#"
onClick={(e) => handleSwichToRecentWorkspace(e, state.recentWorkspaces.second)}>{state.recentWorkspaces.second}
</a>
<a
</a> }
{(state.recentWorkspaces.third !== 'undefined' && state.recentWorkspaces.third !== '') && <a
className='cursor-pointer ml-2'
href="#"
onClick={(e) => handleSwichToRecentWorkspace(e, state.recentWorkspaces.third)}>{state.recentWorkspaces.third}
</a>
</div>
</a> }
</div> }
</div>
<label style={{fontSize: "0.8rem"}} className="pt-3"><FormattedMessage id='home.loadFrom' /></label>
<div className="d-flex">

Loading…
Cancel
Save