Use event emitter for ciontext menu action

pull/752/head
ioedeveloper 4 years ago
parent 9f4599eb7c
commit 2cb01ff79f
  1. 8
      apps/remix-ide/src/app/panels/file-panel.js
  2. 4
      libs/remix-ui/file-explorer/src/lib/file-explorer-context-menu.tsx
  3. 12
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx
  4. 3
      libs/remix-ui/file-explorer/src/lib/types/index.ts

@ -108,11 +108,11 @@ module.exports = class Filepanel extends ViewPlugin {
* @param item { name: string, type?: string[], path?: string[], extension?: string[], pattern?: string[] }
* @param callback (...args) => void
*/
registerContextMenuItem (item, callback) {
if (!item.name || !callback) return console.error('menu name and callback is mandatory')
if (!item.type && !item.path && !item.extension && !item.pattern) return console.error('invalid file matching criteria provided')
registerContextMenuItem (item) {
if (!item) throw new Error('Invalid register context menu argument')
if (!item.name || !item.id) throw new Error('Item name and id is mandatory')
if (!item.type && !item.path && !item.extension && !item.pattern) throw new Error('Invalid file matching criteria provided')
item.action = callback
this.registeredMenuItems = [...this.registeredMenuItems, item]
this.renderComponent()
}

@ -4,7 +4,7 @@ import { FileExplorerContextMenuProps } from './types'
import './css/file-explorer-context-menu.css'
export const FileExplorerContextMenu = (props: FileExplorerContextMenuProps) => {
const { actions, createNewFile, createNewFolder, deletePath, renamePath, hideContextMenu, publishToGist, runScript, pageX, pageY, path, type, ...otherProps } = props
const { actions, createNewFile, createNewFolder, deletePath, renamePath, hideContextMenu, publishToGist, runScript, emit, pageX, pageY, path, type, ...otherProps } = props
const contextMenuRef = useRef(null)
useEffect(() => {
@ -56,7 +56,7 @@ export const FileExplorerContextMenu = (props: FileExplorerContextMenuProps) =>
runScript(path)
break
default:
item.action && item.action(path)
emit && emit(item.id, path)
break
}
hideContextMenu()

@ -76,36 +76,42 @@ export const FileExplorer = (props: FileExplorerProps) => {
const accessToken = config.get('settings/gist-access-token')
const files = await fetchDirectoryContent(name)
const actions = [{
id: 'newFile',
name: 'New File',
type: ['folder'],
path: [],
extension: [],
pattern: []
}, {
id: 'newFolder',
name: 'New Folder',
type: ['folder'],
path: [],
extension: [],
pattern: []
}, {
id: 'rename',
name: 'Rename',
type: ['file', 'folder'],
path: [],
extension: [],
pattern: []
}, {
id: 'delete',
name: 'Delete',
type: ['file', 'folder'],
path: [],
extension: [],
pattern: []
}, {
id: 'pushChangesToGist',
name: 'Push changes to gist',
type: [],
path: [],
extension: [],
pattern: ['^browser/gists/([0-9]|[a-z])*$']
}, {
id: 'run',
name: 'Run',
type: [],
path: [],
@ -614,6 +620,10 @@ export const FileExplorer = (props: FileExplorerProps) => {
})
}
const emitContextMenuEvent = (id: string, path: string) => {
plugin.emit(id, path)
}
const handleHideModal = () => {
setState(prevState => {
return { ...prevState, modalOptions: { ...state.modalOptions, hide: true } }
@ -850,6 +860,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
deletePath={deletePath}
renamePath={editModeOn}
publishToGist={publishToGist}
emit={emitContextMenuEvent}
pageX={state.focusContext.x}
pageY={state.focusContext.y}
path={file.path}
@ -886,6 +897,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
deletePath={deletePath}
renamePath={editModeOn}
runScript={runScript}
emit={emitContextMenuEvent}
pageX={state.focusContext.x}
pageY={state.focusContext.y}
path={file.path}

@ -27,7 +27,7 @@ export interface FileExplorerMenuProps {
}
export interface FileExplorerContextMenuProps {
actions: { name: string, type: string[], path: string[], extension: string[], pattern: string[], action?: (...args) => void }[],
actions: { name: string, type: string[], path: string[], extension: string[], pattern: string[], id: string }[],
createNewFile: (folder?: string) => void,
createNewFolder: (parentFolder?: string) => void,
deletePath: (path: string) => void,
@ -35,6 +35,7 @@ export interface FileExplorerContextMenuProps {
hideContextMenu: () => void,
publishToGist?: () => void,
runScript?: (path: string) => void,
emit?: (id: string, path: string) => void,
pageX: number,
pageY: number,
path: string,

Loading…
Cancel
Save