Make exposed plugin methods return promises.

Set flag for creating empty workspaces and added workspace initialization event.
pull/1575/head
ioedeveloper 3 years ago
parent c04cedd890
commit b45fbdff4d
  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
*/
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) {
this.emit('removePluginActions', plugin)
return new Promise((resolve, reject) => {
this.emit('removePluginActionsReducerEvent', plugin, (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
}
getCurrentWorkspace () {
@ -95,25 +105,40 @@ module.exports = class Filepanel extends ViewPlugin {
this.worspaces = workspaces
}
async createNewFile () {
const provider = this.fileManager.currentFileProvider()
const dir = provider.workspace || '/'
createNewFile () {
return new Promise((resolve, reject) => {
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) {
const provider = this.fileManager.currentFileProvider()
const dir = provider.workspace || '/'
uploadFile (target) {
return new Promise((resolve, reject) => {
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) {
this.emit('createWorkspace', workspaceName)
createWorkspace (workspaceName, isEmpty) {
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)
}

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

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

@ -17,12 +17,16 @@ export const setPlugin = (filePanelPlugin, 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 promise = new Promise((resolve, reject) => {
provider.resolveDirectory(path, (error, fileTree) => {
if (error) reject(error)
if (error) {
cb && cb(error)
return reject(error)
}
cb(null, true)
resolve(fileTree)
})
})
@ -35,17 +39,19 @@ export const addInputField = async (type: 'file' | 'folder', path: string) => {
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()
const promise = createWorkspaceTemplate(workspaceName, 'default-template')
dispatch(createWorkspaceRequest(promise))
promise.then(async () => {
dispatch(createWorkspaceSuccess(workspaceName))
await loadWorkspacePreset('default-template')
if (!isEmpty) await loadWorkspacePreset('default-template')
plugin.emit('setWorkspace', { name: workspaceName, isLocalhost: false })
cb && cb(null, workspaceName)
}).catch((error) => {
dispatch(createWorkspaceError({ error }))
cb && cb(error)
})
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
// 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
@ -231,6 +237,7 @@ export const uploadFile = async (target, targetFolder: string) => {
}
}
fileReader.readAsText(file)
cb(null, true)
}
const name = `${targetFolder}/${file.name}`
@ -243,6 +250,7 @@ export const uploadFile = async (target, targetFolder: string) => {
}, () => {}))
}
}).catch(error => {
cb(error)
if (error) console.log(error)
})
})

Loading…
Cancel
Save