From b45fbdff4d48ce3a3bf8690dbf2d6cd48fe22d5c Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Thu, 14 Oct 2021 14:43:50 +0100 Subject: [PATCH] Make exposed plugin methods return promises. Set flag for creating empty workspaces and added workspace initialization event. --- apps/remix-ide/src/app/panels/file-panel.js | 51 ++++++++++++++----- .../workspace/src/lib/actions/events.ts | 41 +++++++++------ .../workspace/src/lib/actions/index.ts | 1 + .../workspace/src/lib/actions/workspace.ts | 18 +++++-- 4 files changed, 78 insertions(+), 33 deletions(-) diff --git a/apps/remix-ide/src/app/panels/file-panel.js b/apps/remix-ide/src/app/panels/file-panel.js index 6cc57923dc..6e88ef9ebe 100644 --- a/apps/remix-ide/src/app/panels/file-panel.js +++ b/apps/remix-ide/src/app/panels/file-panel.js @@ -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) } diff --git a/libs/remix-ui/workspace/src/lib/actions/events.ts b/libs/remix-ui/workspace/src/lib/actions/events.ts index 5c0ab82cf4..83874f8236 100644 --- a/libs/remix-ui/workspace/src/lib/actions/events.ts +++ b/libs/remix-ui/workspace/src/lib/actions/events.ts @@ -10,28 +10,28 @@ let plugin, dispatch: React.Dispatch 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) => 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) => void) => { + registerContextMenuItem(item, cb) }) - plugin.on('filePanel', 'removePluginActions', (plugin) => { - removePluginActions(plugin) + plugin.on('filePanel', 'removePluginActionsReducerEvent', (plugin, cb: (err: Error, result?: string | number | boolean | Record) => 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) => 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) => 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) => 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) => void) => { dispatch(removeContextMenuItem(plugin)) + cb && cb(null, true) } const fileAdded = async (filePath: string) => { diff --git a/libs/remix-ui/workspace/src/lib/actions/index.ts b/libs/remix-ui/workspace/src/lib/actions/index.ts index 5b5e363da7..f59a6c1e0c 100644 --- a/libs/remix-ui/workspace/src/lib/actions/index.ts +++ b/libs/remix-ui/workspace/src/lib/actions/index.ts @@ -50,6 +50,7 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React. listenOnProviderEvents(workspaceProvider)(dispatch) listenOnProviderEvents(localhostProvider)(dispatch) dispatch(setMode('browser')) + plugin.emit('workspaceInitializationCompleted') } } diff --git a/libs/remix-ui/workspace/src/lib/actions/workspace.ts b/libs/remix-ui/workspace/src/lib/actions/workspace.ts index dca9c66c48..e97da3ce3a 100644 --- a/libs/remix-ui/workspace/src/lib/actions/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/actions/workspace.ts @@ -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) => 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) => 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) => 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) }) })