fix workspace rename

pull/5370/head
yann300 4 years ago
parent 485f3df347
commit f3c43889d4
  1. 10
      apps/remix-ide/src/app/panels/file-panel.js
  2. 18
      libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx

@ -75,6 +75,7 @@ module.exports = class Filepanel extends ViewPlugin {
ReactDOM.render(
<Workspace
createWorkspace={this.createWorkspace.bind(this)}
renameWorkspace={this.renameWorkspace.bind(this)}
setWorkspace={this.setWorkspace.bind(this)}
workspaceRenamed={this.workspaceRenamed.bind(this)}
workspaceDeleted={this.workspaceDeleted.bind(this)}
@ -202,6 +203,15 @@ module.exports = class Filepanel extends ViewPlugin {
}
}
async renameWorkspace (oldName, workspaceName) {
if (!workspaceName) throw new Error('name cannot be empty')
if (checkSpecialChars(workspaceName) || checkSlash(workspaceName)) throw new Error('special characters are not allowed')
if (await this.workspaceExists(workspaceName)) throw new Error('workspace already exists')
const browserProvider = this._deps.fileProviders.browser
const workspacesPath = this._deps.fileProviders.workspace.workspacesPath
browserProvider.rename('browser/' + workspacesPath + '/' + oldName, 'browser/' + workspacesPath + '/' + workspaceName, true)
}
/** these are called by the react component, action is already finished whent it's called */
async setWorkspace (workspace) {
this._deps.fileManager.removeTabsOf(this._deps.fileProviders.workspace)

@ -7,6 +7,7 @@ import { ModalDialog } from '@remix-ui/modal-dialog' // eslint-disable-line
export interface WorkspaceProps {
setWorkspace: ({ name: string, isLocalhost: boolean }) => void,
createWorkspace: (name: string) => void,
renameWorkspace: (oldName: string, newName: string) => void
workspaceRenamed: ({ name: string }) => void,
workspaceCreated: ({ name: string }) => void,
workspaceDeleted: ({ name: string }) => void,
@ -22,10 +23,6 @@ export interface WorkspaceProps {
initialWorkspace: string
}
const hasSpecialChar = (value) => {
return value.match(/[:*?"<>\\'|]/) != null || value.match(/\//) != null
}
var canUpload = window.File || window.FileReader || window.FileList || window.Blob
export const Workspace = (props: WorkspaceProps) => {
const LOCALHOST = ' - connect to localhost - '
@ -171,12 +168,15 @@ export const Workspace = (props: WorkspaceProps) => {
if (workspaceRenameInput.current === undefined) return
// @ts-ignore: Object is possibly 'null'.
const workspaceName = workspaceRenameInput.current.value
if (hasSpecialChar(workspaceName)) return modalMessage('Workspace Rename', 'special characters are not allowed')
const workspacesPath = props.workspace.workspacesPath
await props.fileManager.rename('browser/' + workspacesPath + '/' + state.currentWorkspace, 'browser/' + workspacesPath + '/' + workspaceName)
setWorkspace(workspaceName)
props.workspaceRenamed({ name: state.currentWorkspace })
try {
await props.renameWorkspace(state.currentWorkspace, workspaceName)
setWorkspace(workspaceName)
props.workspaceRenamed({ name: workspaceName })
} catch (e) {
modalMessage('Workspace Rename', e.message)
console.error(e)
}
}
const onFinishCreateWorkspace = async () => {

Loading…
Cancel
Save