Make exposed plugin methods return promises.

Set flag for creating empty workspaces and added workspace initialization event.
pull/5370/head
ioedeveloper 3 years ago
parent 4b3dec02a9
commit b50050fe04
  1. 51
      apps/remix-ide/src/app/panels/file-panel.js
  2. 41
      libs/remix-ui/workspace/src/lib/actions/events.ts
  3. 1
      libs/remix-ui/workspace/src/lib/actions/index.ts
  4. 18
      libs/remix-ui/workspace/src/lib/actions/workspace.ts

@ -76,11 +76,21 @@ module.exports = class Filepanel extends ViewPlugin {
* @param callback (...args) => void * @param callback (...args) => void
*/ */
registerContextMenuItem (item) { registerContextMenuItem (item) {
this.emit('registerContextMenuItem', item) return new Promise((resolve, reject) => {
this.emit('registerContextMenuItemReducerEvent', item, (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
} }
removePluginActions (plugin) { removePluginActions (plugin) {
this.emit('removePluginActions', plugin) return new Promise((resolve, reject) => {
this.emit('removePluginActionsReducerEvent', plugin, (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
} }
getCurrentWorkspace () { getCurrentWorkspace () {
@ -95,25 +105,40 @@ module.exports = class Filepanel extends ViewPlugin {
this.worspaces = workspaces this.worspaces = workspaces
} }
async createNewFile () { createNewFile () {
const provider = this.fileManager.currentFileProvider() return new Promise((resolve, reject) => {
const dir = provider.workspace || '/' const provider = this.fileManager.currentFileProvider()
const dir = provider.workspace || '/'
this.emit('displayNewFileInput', dir) this.emit('createNewFileInputReducerEvent', dir, (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
} }
async uploadFile (target) { uploadFile (target) {
const provider = this.fileManager.currentFileProvider() return new Promise((resolve, reject) => {
const dir = provider.workspace || '/' const provider = this.fileManager.currentFileProvider()
const dir = provider.workspace || '/'
return this.emit('uploadFileEvent', dir, target) return this.emit('uploadFileReducerEvent', dir, target, (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
} }
async createWorkspace (workspaceName) { createWorkspace (workspaceName, isEmpty) {
this.emit('createWorkspace', workspaceName) return new Promise((resolve, reject) => {
this.emit('createWorkspaceReducerEvent', workspaceName, isEmpty, (err, data) => {
if (err) reject(err)
else resolve(data || true)
})
})
} }
async renameWorkspace (oldName, workspaceName) { renameWorkspace (oldName, workspaceName) {
this.emit('renameWorkspace', oldName, workspaceName) this.emit('renameWorkspace', oldName, workspaceName)
} }

@ -10,28 +10,28 @@ let plugin, dispatch: React.Dispatch<any>
export const listenOnPluginEvents = (filePanelPlugin) => { export const listenOnPluginEvents = (filePanelPlugin) => {
plugin = filePanelPlugin plugin = filePanelPlugin
plugin.on('filePanel', 'createWorkspace', (name: string) => { plugin.on('filePanel', 'createWorkspaceReducerEvent', (name: string, isEmpty = false, cb: (err: Error, result?: string | number | boolean | Record<string, any>) => void) => {
createWorkspace(name) createWorkspace(name, isEmpty, cb)
}) })
plugin.on('filePanel', 'renameWorkspace', (oldName: string, workspaceName: string) => { plugin.on('filePanel', 'renameWorkspace', (oldName: string, workspaceName: string) => {
renameWorkspace(oldName, workspaceName) renameWorkspace(oldName, workspaceName)
}) })
plugin.on('filePanel', 'registerContextMenuItem', (item: action) => { plugin.on('filePanel', 'registerContextMenuItemReducerEvent', (item: action, cb: (err: Error, result?: string | number | boolean | Record<string, any>) => void) => {
registerContextMenuItem(item) registerContextMenuItem(item, cb)
}) })
plugin.on('filePanel', 'removePluginActions', (plugin) => { plugin.on('filePanel', 'removePluginActionsReducerEvent', (plugin, cb: (err: Error, result?: string | number | boolean | Record<string, any>) => void) => {
removePluginActions(plugin) removePluginActions(plugin, cb)
}) })
plugin.on('filePanel', 'displayNewFileInput', (path) => { plugin.on('filePanel', 'createNewFileInputReducerEvent', (path, cb: (err: Error, result?: string | number | boolean | Record<string, any>) => void) => {
addInputField('file', path) addInputField('file', path, cb)
}) })
plugin.on('filePanel', 'uploadFileEvent', (dir: string, target) => { plugin.on('filePanel', 'uploadFileReducerEvent', (dir: string, target, cb: (err: Error, result?: string | number | boolean | Record<string, any>) => void) => {
uploadFile(target, dir) uploadFile(target, dir, cb)
}) })
plugin.on('remixd', 'rootFolderChanged', async (path: string) => { plugin.on('remixd', 'rootFolderChanged', async (path: string) => {
@ -106,15 +106,26 @@ export const listenOnProviderEvents = (provider) => (reducerDispatch: React.Disp
}) })
} }
const registerContextMenuItem = (item: action) => { const registerContextMenuItem = (item: action, cb?: (err: Error, result?: string | number | boolean | Record<string, any>) => void) => {
if (!item) return dispatch(displayPopUp('Invalid register context menu argument')) if (!item) {
if (!item.name || !item.id) return dispatch(displayPopUp('Item name and id is mandatory')) cb && cb(new Error('Invalid register context menu argument'))
if (!item.type && !item.path && !item.extension && !item.pattern) return dispatch(displayPopUp('Invalid file matching criteria provided')) return dispatch(displayPopUp('Invalid register context menu argument'))
}
if (!item.name || !item.id) {
cb && cb(new Error('Item name and id is mandatory'))
return dispatch(displayPopUp('Item name and id is mandatory'))
}
if (!item.type && !item.path && !item.extension && !item.pattern) {
cb && cb(new Error('Invalid file matching criteria provided'))
return dispatch(displayPopUp('Invalid file matching criteria provided'))
}
dispatch(setContextMenuItem(item)) dispatch(setContextMenuItem(item))
cb && cb(null, item)
} }
const removePluginActions = (plugin) => { const removePluginActions = (plugin, cb: (err: Error, result?: string | number | boolean | Record<string, any>) => void) => {
dispatch(removeContextMenuItem(plugin)) dispatch(removeContextMenuItem(plugin))
cb && cb(null, true)
} }
const fileAdded = async (filePath: string) => { const fileAdded = async (filePath: string) => {

@ -50,6 +50,7 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.
listenOnProviderEvents(workspaceProvider)(dispatch) listenOnProviderEvents(workspaceProvider)(dispatch)
listenOnProviderEvents(localhostProvider)(dispatch) listenOnProviderEvents(localhostProvider)(dispatch)
dispatch(setMode('browser')) dispatch(setMode('browser'))
plugin.emit('workspaceInitializationCompleted')
} }
} }

@ -17,12 +17,16 @@ export const setPlugin = (filePanelPlugin, reducerDispatch) => {
dispatch = reducerDispatch dispatch = reducerDispatch
} }
export const addInputField = async (type: 'file' | 'folder', path: string) => { export const addInputField = async (type: 'file' | 'folder', path: string, cb?: (err: Error, result?: string | number | boolean | Record<string, any>) => void) => {
const provider = plugin.fileManager.currentFileProvider() const provider = plugin.fileManager.currentFileProvider()
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {
provider.resolveDirectory(path, (error, fileTree) => { provider.resolveDirectory(path, (error, fileTree) => {
if (error) reject(error) if (error) {
cb && cb(error)
return reject(error)
}
cb(null, true)
resolve(fileTree) resolve(fileTree)
}) })
}) })
@ -35,17 +39,19 @@ export const addInputField = async (type: 'file' | 'folder', path: string) => {
return promise return promise
} }
export const createWorkspace = async (workspaceName: string) => { export const createWorkspace = async (workspaceName: string, isEmpty = false, cb?: (err: Error, result?: string | number | boolean | Record<string, any>) => void) => {
await plugin.fileManager.closeAllFiles() await plugin.fileManager.closeAllFiles()
const promise = createWorkspaceTemplate(workspaceName, 'default-template') const promise = createWorkspaceTemplate(workspaceName, 'default-template')
dispatch(createWorkspaceRequest(promise)) dispatch(createWorkspaceRequest(promise))
promise.then(async () => { promise.then(async () => {
dispatch(createWorkspaceSuccess(workspaceName)) dispatch(createWorkspaceSuccess(workspaceName))
await loadWorkspacePreset('default-template') if (!isEmpty) await loadWorkspacePreset('default-template')
plugin.emit('setWorkspace', { name: workspaceName, isLocalhost: false }) plugin.emit('setWorkspace', { name: workspaceName, isLocalhost: false })
cb && cb(null, workspaceName)
}).catch((error) => { }).catch((error) => {
dispatch(createWorkspaceError({ error })) dispatch(createWorkspaceError({ error }))
cb && cb(error)
}) })
return promise return promise
} }
@ -204,7 +210,7 @@ export const switchToWorkspace = async (name: string) => {
} }
} }
export const uploadFile = async (target, targetFolder: string) => { export const uploadFile = async (target, targetFolder: string, cb?: (err: Error, result?: string | number | boolean | Record<string, any>) => void) => {
// TODO The file explorer is merely a view on the current state of // TODO The file explorer is merely a view on the current state of
// the files module. Please ask the user here if they want to overwrite // the files module. Please ask the user here if they want to overwrite
// a file and then just use `files.add`. The file explorer will // a file and then just use `files.add`. The file explorer will
@ -231,6 +237,7 @@ export const uploadFile = async (target, targetFolder: string) => {
} }
} }
fileReader.readAsText(file) fileReader.readAsText(file)
cb(null, true)
} }
const name = `${targetFolder}/${file.name}` const name = `${targetFolder}/${file.name}`
@ -243,6 +250,7 @@ export const uploadFile = async (target, targetFolder: string) => {
}, () => {})) }, () => {}))
} }
}).catch(error => { }).catch(error => {
cb(error)
if (error) console.log(error) if (error) console.log(error)
}) })
}) })

Loading…
Cancel
Save