Register context menu items

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

@ -66,6 +66,7 @@ module.exports = class Filepanel extends ViewPlugin {
} }
} }
this.reset = false this.reset = false
this.registeredMenuItems = []
this.el = yo` this.el = yo`
<div id="fileExplorerView"> <div id="fileExplorerView">
</div> </div>
@ -106,9 +107,13 @@ module.exports = class Filepanel extends ViewPlugin {
* *
* @param { name: string, type?: string[], path?: string[], extension?: string[], pattern?: string[] } * @param { name: string, type?: string[], path?: string[], extension?: string[], pattern?: string[] }
*/ */
registerContextMenuItem (action, callback) { registerContextMenuItem (item, callback) {
if (!action.name || !callback) return if (!item.name || !callback) return console.error('menu name and item is mandatory')
if (!item.type && !item.path && !item.extension && !item.pattern) return console.error('invalid menu match criteria provided')
item.action = callback
this.registeredMenuItems = [...this.registeredMenuItems, item]
this.renderComponent()
} }
renderComponent () { renderComponent () {
@ -125,6 +130,7 @@ module.exports = class Filepanel extends ViewPlugin {
menuItems={['createNewFile', 'createNewFolder', 'publishToGist', canUpload ? 'uploadFile' : '']} menuItems={['createNewFile', 'createNewFolder', 'publishToGist', canUpload ? 'uploadFile' : '']}
plugin={this} plugin={this}
focusRoot={this.reset} focusRoot={this.reset}
contextMenuItems={this.registeredMenuItems}
/> />
</div> </div>
<div className='pl-2 filesystemexplorer remixui_treeview'> <div className='pl-2 filesystemexplorer remixui_treeview'>
@ -136,6 +142,7 @@ module.exports = class Filepanel extends ViewPlugin {
menuItems={['createNewFile', 'createNewFolder']} menuItems={['createNewFile', 'createNewFolder']}
plugin={this} plugin={this}
focusRoot={this.reset} focusRoot={this.reset}
contextMenuItems={this.registeredMenuItems}
/> />
} }
</div> </div>

@ -24,10 +24,10 @@ export const FileExplorerContextMenu = (props: FileExplorerContextMenuProps) =>
const menu = () => { const menu = () => {
return actions.filter(item => { return actions.filter(item => {
if (item.type.findIndex(name => name === type) !== -1) return true if (item.type && Array.isArray(item.type) && (item.type.findIndex(name => name === type) !== -1)) return true
else if (item.path.findIndex(key => key === path) !== -1) return true else if (item.path && Array.isArray(item.path) && (item.path.findIndex(key => key === path) !== -1)) return true
else if (item.extension.findIndex(ext => path.endsWith(ext)) !== -1) return true else if (item.extension && Array.isArray(item.extension) && (item.extension.findIndex(ext => path.endsWith(ext)) !== -1)) return true
else if (item.pattern.filter(value => path.match(new RegExp(value))).length > 0) return true else if (item.pattern && Array.isArray(item.pattern) && (item.pattern.filter(value => path.match(new RegExp(value))).length > 0)) return true
else return false else return false
}).map((item, index) => { }).map((item, index) => {
return <li return <li
@ -56,6 +56,7 @@ export const FileExplorerContextMenu = (props: FileExplorerContextMenuProps) =>
runScript(path) runScript(path)
break break
default: default:
item.action && item.action()
break break
} }
hideContextMenu() hideContextMenu()

@ -16,7 +16,7 @@ import './css/file-explorer.css'
const queryParams = new QueryParams() const queryParams = new QueryParams()
export const FileExplorer = (props: FileExplorerProps) => { export const FileExplorer = (props: FileExplorerProps) => {
const { filesProvider, name, registry, plugin, focusRoot } = props const { filesProvider, name, registry, plugin, focusRoot, contextMenuItems } = props
const [state, setState] = useState({ const [state, setState] = useState({
focusElement: [{ focusElement: [{
key: name, key: name,
@ -165,6 +165,17 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
}, [focusRoot]) }, [focusRoot])
useEffect(() => {
if (contextMenuItems) {
setState(prevState => {
// filter duplicate items
const items = contextMenuItems.filter(({ name }) => prevState.actions.findIndex(action => action.name === name) === -1)
return { ...prevState, actions: [...prevState.actions, ...items] }
})
}
}, [contextMenuItems])
const resolveDirectory = async (folderPath, dir: File[], isChild = false): Promise<File[]> => { const resolveDirectory = async (folderPath, dir: File[], isChild = false): Promise<File[]> => {
if (!isChild && (state.focusEdit.element === 'browser/blank') && state.focusEdit.isNew && (dir.findIndex(({ path }) => path === 'browser/blank') === -1)) { if (!isChild && (state.focusEdit.element === 'browser/blank') && state.focusEdit.isNew && (dir.findIndex(({ path }) => path === 'browser/blank') === -1)) {
dir = state.focusEdit.type === 'file' ? [...dir, { dir = state.focusEdit.type === 'file' ? [...dir, {

@ -5,7 +5,8 @@ export interface FileExplorerProps {
filesProvider: any, filesProvider: any,
menuItems?: string[], menuItems?: string[],
plugin: any, plugin: any,
focusRoot: boolean focusRoot: boolean,
contextMenuItems: { name: string, type: string[], path: string[], extension: string[], pattern: string[] }[]
} }
export interface File { export interface File {
@ -26,7 +27,7 @@ export interface FileExplorerMenuProps {
} }
export interface FileExplorerContextMenuProps { export interface FileExplorerContextMenuProps {
actions: { name: string, type: string[], path: string[], extension: string[], pattern: string[] }[], actions: { name: string, type: string[], path: string[], extension: string[], pattern: string[], action?: (...args) => void }[],
createNewFile: (folder?: string) => void, createNewFile: (folder?: string) => void,
createNewFolder: (parentFolder?: string) => void, createNewFolder: (parentFolder?: string) => void,
deletePath: (path: string) => void, deletePath: (path: string) => void,

Loading…
Cancel
Save