Commit 9084e342 authored by ioedeveloper's avatar ioedeveloper

Display workspace files

parent c0a6a539
// var modalDialogCustom = require('../app/ui/modal-dialog-custom') // var modalDialogCustom = require('../app/ui/modal-dialog-custom')
var request = require('request') import * as request from 'request'
// Allowing window to be overriden for testing export class GistHandler {
function GistHandler (_window) { handleLoad (params) {
if (_window !== undefined) { let loadingFromGist = false
// modalDialogCustom = _window let gistId
}
this.handleLoad = function (params, cb) { if (params.gist) {
if (!cb) cb = () => {}
var loadingFromGist = false
var gistId
if (params.gist === '') {
loadingFromGist = true loadingFromGist = true
// modalDialogCustom.prompt('Load a Gist', 'Enter the ID of the Gist or URL you would like to load.', null, (target) => {
if (target !== '') {
gistId = getGistId(target)
if (gistId) {
cb(gistId)
} else {
// modalDialogCustom.alert('Gist load error', 'Error while loading gist. Please provide a valid Gist ID or URL.')
}
}
})
return loadingFromGist
} else { } else {
gistId = params.gist gistId = params.gist
loadingFromGist = !!gistId loadingFromGist = !!gistId
} }
if (loadingFromGist) {
cb(gistId) return gistId
}
return loadingFromGist
} }
function getGistId (str) { getGistId (str: string) {
var idr = /[0-9A-Fa-f]{8,}/ const idr = /[0-9A-Fa-f]{8,}/
var match = idr.exec(str) const match = idr.exec(str)
return match ? match[0] : null return match ? match[0] : null
} }
this.loadFromGist = (params, fileManager) => { loadFromGist (params, fileManager) {
const self = this const gistId = this.handleLoad(params)
return self.handleLoad(params, function (gistId) {
request.get({ request.get({
url: `https://api.github.com/gists/${gistId}`, url: `https://api.github.com/gists/${gistId}`,
json: true json: true
...@@ -52,6 +35,7 @@ function GistHandler (_window) { ...@@ -52,6 +35,7 @@ function GistHandler (_window) {
return return
} }
const obj = {} const obj = {}
Object.keys(data.files).forEach((element) => { Object.keys(data.files).forEach((element) => {
const path = element.replace(/\.\.\./g, '/') const path = element.replace(/\.\.\./g, '/')
...@@ -60,14 +44,12 @@ function GistHandler (_window) { ...@@ -60,14 +44,12 @@ function GistHandler (_window) {
fileManager.setBatchFiles(obj, 'workspace', true, (errorLoadingFile) => { fileManager.setBatchFiles(obj, 'workspace', true, (errorLoadingFile) => {
if (!errorLoadingFile) { if (!errorLoadingFile) {
const provider = fileManager.getProvider('workspace') const provider = fileManager.getProvider('workspace')
provider.lastLoadedGistId = gistId provider.lastLoadedGistId = gistId
} else { } else {
// modalDialogCustom.alert('Gist load error', errorLoadingFile.message || errorLoadingFile) // modalDialogCustom.alert('Gist load error', errorLoadingFile.message || errorLoadingFile)
} }
}) })
}) })
})
} }
} }
module.exports = GistHandler
...@@ -35,7 +35,6 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -35,7 +35,6 @@ export const Workspace = (props: WorkspaceProps) => {
const [state, setState] = useState({ const [state, setState] = useState({
workspaces: [], workspaces: [],
reset: false, reset: false,
currentWorkspace: NO_WORKSPACE,
hideRemixdExplorer: true, hideRemixdExplorer: true,
displayNewFile: false, displayNewFile: false,
externalUploads: null, externalUploads: null,
...@@ -53,7 +52,7 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -53,7 +52,7 @@ export const Workspace = (props: WorkspaceProps) => {
loadingLocalhost: false, loadingLocalhost: false,
toasterMsg: '' toasterMsg: ''
}) })
const [currentWorkspace, setCurrentWorkspace] = useState<string>('') const [currentWorkspace, setCurrentWorkspace] = useState<string>(NO_WORKSPACE)
const [fs, dispatch] = useReducer(browserReducer, browserInitialState) const [fs, dispatch] = useReducer(browserReducer, browserInitialState)
useEffect(() => { useEffect(() => {
...@@ -102,14 +101,14 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -102,14 +101,14 @@ export const Workspace = (props: WorkspaceProps) => {
} }
props.request.getCurrentWorkspace = () => { props.request.getCurrentWorkspace = () => {
return { name: state.currentWorkspace, isLocalhost: state.currentWorkspace === LOCALHOST, absolutePath: `${props.workspace.workspacesPath}/${state.currentWorkspace}` } return { name: currentWorkspace, isLocalhost: currentWorkspace === LOCALHOST, absolutePath: `${props.workspace.workspacesPath}/${currentWorkspace}` }
} }
const localhostDisconnect = () => { const localhostDisconnect = () => {
if (state.currentWorkspace === LOCALHOST) setWorkspace(props.workspaces.length > 0 ? props.workspaces[0] : NO_WORKSPACE) if (currentWorkspace === LOCALHOST) setWorkspace(props.workspaces.length > 0 ? props.workspaces[0] : NO_WORKSPACE)
// This should be removed some time after refactoring: https://github.com/ethereum/remix-project/issues/1197 // This should be removed some time after refactoring: https://github.com/ethereum/remix-project/issues/1197
else { else {
setWorkspace(state.currentWorkspace) // Useful to switch to last selcted workspace when remixd is disconnected setWorkspace(currentWorkspace) // Useful to switch to last selcted workspace when remixd is disconnected
props.fileManager.setMode('browser') props.fileManager.setMode('browser')
} }
} }
...@@ -161,7 +160,7 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -161,7 +160,7 @@ export const Workspace = (props: WorkspaceProps) => {
const workspaceName = workspaceRenameInput.current.value const workspaceName = workspaceRenameInput.current.value
try { try {
await props.renameWorkspace(state.currentWorkspace, workspaceName) await props.renameWorkspace(currentWorkspace, workspaceName)
setWorkspace(workspaceName) setWorkspace(workspaceName)
props.workspaceRenamed({ name: workspaceName }) props.workspaceRenamed({ name: workspaceName })
} catch (e) { } catch (e) {
...@@ -188,8 +187,8 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -188,8 +187,8 @@ export const Workspace = (props: WorkspaceProps) => {
const onFinishDeleteWorkspace = async () => { const onFinishDeleteWorkspace = async () => {
await props.fileManager.closeAllFiles() await props.fileManager.closeAllFiles()
const workspacesPath = props.workspace.workspacesPath const workspacesPath = props.workspace.workspacesPath
props.browser.remove(workspacesPath + '/' + state.currentWorkspace) props.browser.remove(workspacesPath + '/' + currentWorkspace)
const name = state.currentWorkspace const name = currentWorkspace
setWorkspace(NO_WORKSPACE) setWorkspace(NO_WORKSPACE)
props.workspaceDeleted({ name }) props.workspaceDeleted({ name })
} }
...@@ -284,7 +283,7 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -284,7 +283,7 @@ export const Workspace = (props: WorkspaceProps) => {
return ( return (
<> <>
<span>{ state.modal.message }</span> <span>{ state.modal.message }</span>
<input type="text" data-id="modalDialogCustomPromptTextRename" defaultValue={ state.currentWorkspace } ref={workspaceRenameInput} className="form-control" /> <input type="text" data-id="modalDialogCustomPromptTextRename" defaultValue={ currentWorkspace } ref={workspaceRenameInput} className="form-control" />
</> </>
) )
} }
...@@ -324,7 +323,7 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -324,7 +323,7 @@ export const Workspace = (props: WorkspaceProps) => {
title='Create'> title='Create'>
</span> </span>
<span <span
hidden={state.currentWorkspace === LOCALHOST || state.currentWorkspace === NO_WORKSPACE} hidden={currentWorkspace === LOCALHOST || currentWorkspace === NO_WORKSPACE}
id='workspaceRename' id='workspaceRename'
data-id='workspaceRename' data-id='workspaceRename'
onClick={(e) => { onClick={(e) => {
...@@ -335,7 +334,7 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -335,7 +334,7 @@ export const Workspace = (props: WorkspaceProps) => {
title='Rename'> title='Rename'>
</span> </span>
<span <span
hidden={state.currentWorkspace === LOCALHOST || state.currentWorkspace === NO_WORKSPACE} hidden={currentWorkspace === LOCALHOST || currentWorkspace === NO_WORKSPACE}
id='workspaceDelete' id='workspaceDelete'
data-id='workspaceDelete' data-id='workspaceDelete'
onClick={(e) => { onClick={(e) => {
...@@ -353,8 +352,8 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -353,8 +352,8 @@ export const Workspace = (props: WorkspaceProps) => {
return <option key={index} value={folder}>{folder}</option> return <option key={index} value={folder}>{folder}</option>
}) })
} }
<option value={LOCALHOST}>{state.currentWorkspace === LOCALHOST ? 'localhost' : LOCALHOST}</option> <option value={LOCALHOST}>{currentWorkspace === LOCALHOST ? 'localhost' : LOCALHOST}</option>
{ state.workspaces.length <= 0 && <option value={NO_WORKSPACE}>{NO_WORKSPACE}</option> } { fs.browser.workspaces.length <= 0 && <option value={NO_WORKSPACE}>{NO_WORKSPACE}</option> }
</select> </select>
</div> </div>
</header> </header>
...@@ -362,9 +361,9 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -362,9 +361,9 @@ export const Workspace = (props: WorkspaceProps) => {
<div className='remixui_fileExplorerTree'> <div className='remixui_fileExplorerTree'>
<div> <div>
<div className='pl-2 remixui_treeview' data-id='filePanelFileExplorerTree'> <div className='pl-2 remixui_treeview' data-id='filePanelFileExplorerTree'>
{ state.hideRemixdExplorer && state.currentWorkspace && state.currentWorkspace !== NO_WORKSPACE && state.currentWorkspace !== LOCALHOST && { state.hideRemixdExplorer && currentWorkspace && currentWorkspace !== NO_WORKSPACE && currentWorkspace !== LOCALHOST &&
<FileExplorer <FileExplorer
name={state.currentWorkspace} name={currentWorkspace}
registry={props.registry} registry={props.registry}
filesProvider={props.workspace} filesProvider={props.workspace}
menuItems={['createNewFile', 'createNewFolder', 'publishToGist', canUpload ? 'uploadFile' : '']} menuItems={['createNewFile', 'createNewFolder', 'publishToGist', canUpload ? 'uploadFile' : '']}
......
...@@ -224,6 +224,7 @@ ...@@ -224,6 +224,7 @@
"@types/react-beautiful-dnd": "^13.1.2", "@types/react-beautiful-dnd": "^13.1.2",
"@types/react-dom": "^17.0.9", "@types/react-dom": "^17.0.9",
"@types/react-router-dom": "^5.3.0", "@types/react-router-dom": "^5.3.0",
"@types/request": "^2.48.7",
"@types/tape": "^4.13.0", "@types/tape": "^4.13.0",
"@types/ws": "^7.2.4", "@types/ws": "^7.2.4",
"@typescript-eslint/eslint-plugin": "^4.32.0", "@typescript-eslint/eslint-plugin": "^4.32.0",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment