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', icon: 'assets/img/fileManager.webp',
permission: true, permission: true,
version: packageJson.version, version: packageJson.version,
methods: ['closeAllFiles', 'closeFile', 'file', 'exists', 'open', 'writeFile', 'writeMultipleFiles', 'readFile', 'copyFile', 'copyDir', 'rename', 'mkdir', methods: ['closeAllFiles', 'closeFile', 'file', 'exists', 'open', 'writeFile', 'writeMultipleFiles', 'writeFileNoRewrite',
'readdir', 'dirList', 'fileList', 'remove', 'getCurrentFile', 'getFile', 'getFolder', 'setFile', 'switchFile', 'refresh', 'readFile', 'copyFile', 'copyDir', 'rename', 'mkdir', 'readdir', 'dirList', 'fileList', 'remove', 'getCurrentFile', 'getFile',
'getProviderOf', 'getProviderByName', 'getPathFromUrl', 'getUrlFromPath', 'saveCurrentFile', 'setBatchFiles', 'isGitRepo'], 'getFolder', 'setFile', 'switchFile', 'refresh', 'getProviderOf', 'getProviderByName', 'getPathFromUrl', 'getUrlFromPath',
'saveCurrentFile', 'setBatchFiles', 'isGitRepo'
],
kind: 'file-system' kind: 'file-system'
} }
const errorMsg = { const errorMsg = {
@ -37,7 +39,6 @@ const errorMsg = {
const createError = (err) => { const createError = (err) => {
return new Error(`${errorMsg[err.code]} ${err.message || ''}`) return new Error(`${errorMsg[err.code]} ${err.message || ''}`)
} }
class FileManager extends Plugin { class FileManager extends Plugin {
mode: string mode: string
openedFiles: any openedFiles: any
@ -206,11 +207,11 @@ class FileManager extends Plugin {
path = this.normalize(path) path = this.normalize(path)
path = this.limitPluginScope(path) path = this.limitPluginScope(path)
if (await this.exists(path)) { if (await this.exists(path)) {
const newPath = await helper.createNonClashingNameAsync(path, this) await this._handleIsFile(path, `Cannot write file ${path}`)
//await this._handleIsFile(path, `Cannot write file ${path}`) return await this.setFileContent(path, data)
return await this.setFileContent(newPath, data)
} else { } else {
const ret = await this.setFileContent(path, data) const ret = await this.setFileContent(path, data)
this.emit('fileAdded', path)
return ret return ret
} }
} catch (e) { } catch (e) {
@ -249,6 +250,31 @@ class FileManager extends Plugin {
throw new Error(e) 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 * 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 import {Toaster} from '@remix-ui/toaster' // eslint-disable-line
const _paq = window._paq = window._paq || [] // eslint-disable-line const _paq = window._paq = window._paq || [] // eslint-disable-line
import {CustomTooltip} from '@remix-ui/helper'; import {CustomTooltip} from '@remix-ui/helper';
import {TEMPLATE_NAMES} from '@remix-ui/workspace'
interface HomeTabFileProps { interface HomeTabFileProps {
plugin: any plugin: any
@ -56,10 +57,8 @@ function HomeTabFile({plugin}: HomeTabFileProps) {
const inputValue = useRef(null) const inputValue = useRef(null)
useEffect(() => { useEffect(() => {
console.log("useEffHomeFile")
plugin.on('filePanel', 'setWorkspace', async () => { plugin.on('filePanel', 'setWorkspace', async () => {
let recents = JSON.parse(localStorage.getItem('recentWorkspaces')) let recents = JSON.parse(localStorage.getItem('recentWorkspaces'))
console.log("useEffHomeFilerecents ", recents)
if (!recents) recents = {first:'', second: '', third: ''} if (!recents) recents = {first:'', second: '', third: ''}
setState(prevState => { setState(prevState => {
@ -115,6 +114,15 @@ function HomeTabFile({plugin}: HomeTabFileProps) {
const startCoding = async () => { const startCoding = async () => {
_paq.push(['trackEvent', 'hometab', 'filesSection', 'startCoding']) _paq.push(['trackEvent', 'hometab', 'filesSection', 'startCoding'])
plugin.verticalIcons.select('filePanel') 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 = ` const content = `
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0; pragma solidity >=0.7.0 <0.9.0;
@ -122,8 +130,8 @@ function HomeTabFile({plugin}: HomeTabFileProps) {
contract helloWorld { contract helloWorld {
} }
` `
await plugin.call('fileManager', 'writeFile', '/helloWorld.sol', content) const {newContent, newPath} = await plugin.call('fileManager', 'writeFileNoRewrite', '/contracts/helloWorld.sol', content)
await plugin.call('fileManager', 'open', '/helloWorld.sol') await plugin.call('fileManager', 'open', newPath)
} }
const uploadFile = async (target) => { const uploadFile = async (target) => {
@ -165,8 +173,12 @@ function HomeTabFile({plugin}: HomeTabFileProps) {
const handleSwichToRecentWorkspace = async (e, workspaceName) => { const handleSwichToRecentWorkspace = async (e, workspaceName) => {
e.preventDefault(); e.preventDefault();
<<<<<<< HEAD
await plugin.call('filePanel', 'switchToWorkspace', { name: workspaceName, isLocalhost: false }) await plugin.call('filePanel', 'switchToWorkspace', { name: workspaceName, isLocalhost: false })
console.log('The link was clicked.'); 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) => ( 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>} {state.modalInfo.prefix && <span className="text-nowrap align-self-center mr-2">ipfs://</span>}
<input <input
ref={inputValue} ref={inputValue}
<<<<<<< HEAD
type="text" type="text"
name="prompt_text" name="prompt_text"
id="inputPrompt_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" className="w-100 mt-1 form-control"
data-id="homeTabModalDialogCustomPromptText" data-id="homeTabModalDialogCustomPromptText"
value={state.importSource} value={state.importSource}
onInput={(e) => { onInput={(e) => {
<<<<<<< HEAD
setState((prevState) => { setState((prevState) => {
return {...prevState, importSource: inputValue.current.value} 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> </button>
</CustomTooltip> </CustomTooltip>
</div> </div>
{ !(state.recentWorkspaces.first == '' &&
state.recentWorkspaces.second == '' &&
state.recentWorkspaces.third == '') &&
<div className='d-flex flex-column'> <div className='d-flex flex-column'>
<label style={{fontSize: "0.8rem"}} className='mt-3'>Recent workspaces</label> <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' className='cursor-pointer mb-1 ml-2'
href="#" href="#"
onClick={(e) => handleSwichToRecentWorkspace(e, state.recentWorkspaces.first)}>{state.recentWorkspaces.first} 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' className='cursor-pointer mb-1 ml-2'
href="#" href="#"
onClick={(e) => handleSwichToRecentWorkspace(e, state.recentWorkspaces.second)}>{state.recentWorkspaces.second} 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' className='cursor-pointer ml-2'
href="#" href="#"
onClick={(e) => handleSwichToRecentWorkspace(e, state.recentWorkspaces.third)}>{state.recentWorkspaces.third} onClick={(e) => handleSwichToRecentWorkspace(e, state.recentWorkspaces.third)}>{state.recentWorkspaces.third}
</a> </a> }
</div> </div> }
</div> </div>
<label style={{fontSize: "0.8rem"}} className="pt-3"><FormattedMessage id='home.loadFrom' /></label> <label style={{fontSize: "0.8rem"}} className="pt-3"><FormattedMessage id='home.loadFrom' /></label>
<div className="d-flex"> <div className="d-flex">

Loading…
Cancel
Save