Publish folder to gist

pull/1185/head
ioedeveloper 4 years ago
parent 0553a65ade
commit a50628aaac
  1. 2
      apps/remix-ide/src/lib/gist-handler.js
  2. 14
      libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts
  3. 2
      libs/remix-ui/file-explorer/src/lib/file-explorer-context-menu.tsx
  4. 66
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx
  5. 9
      libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts
  6. 5
      libs/remix-ui/file-explorer/src/lib/types/index.ts

@ -54,7 +54,7 @@ function GistHandler (_window) {
}
const obj = {}
Object.keys(data.files).forEach((element) => {
obj['/' + gistId + '/' + element] = data.files[element]
obj['/' + 'gist-' + gistId + '/' + element] = data.files[element]
})
fileManager.setBatchFiles(obj, 'workspace', true, (errorLoadingFile) => {
if (!errorLoadingFile) {

@ -41,14 +41,16 @@ const normalize = (parent, filesList, newInputType?: string): any => {
if (filesList[key].isDirectory) {
folders[extractNameFromKey(key)] = {
path,
name: extractNameFromKey(path),
isDirectory: filesList[key].isDirectory
name: extractNameFromKey(path).indexOf('gist-') === 0 ? extractNameFromKey(path).split('-')[1] : extractNameFromKey(path),
isDirectory: filesList[key].isDirectory,
type: extractNameFromKey(path).indexOf('gist-') === 0 ? 'gist' : 'folder'
}
} else {
files[extractNameFromKey(key)] = {
path,
name: extractNameFromKey(path),
isDirectory: filesList[key].isDirectory
isDirectory: filesList[key].isDirectory,
type: 'file'
}
}
})
@ -59,7 +61,8 @@ const normalize = (parent, filesList, newInputType?: string): any => {
folders[path] = {
path: path,
name: '',
isDirectory: true
isDirectory: true,
type: 'folder'
}
} else if (newInputType === 'file') {
const path = parent + '/blank'
@ -67,7 +70,8 @@ const normalize = (parent, filesList, newInputType?: string): any => {
files[path] = {
path: path,
name: '',
isDirectory: false
isDirectory: false,
type: 'file'
}
}

@ -50,7 +50,7 @@ export const FileExplorerContextMenu = (props: FileExplorerContextMenuProps) =>
deletePath(path)
break
case 'Push changes to gist':
publishToGist()
publishToGist(path, type)
break
case 'Run':
runScript(path)

@ -31,14 +31,14 @@ export const FileExplorer = (props: FileExplorerProps) => {
actions: [{
id: 'newFile',
name: 'New File',
type: ['folder'],
type: ['folder', 'gist'],
path: [],
extension: [],
pattern: []
}, {
id: 'newFolder',
name: 'New Folder',
type: ['folder'],
type: ['folder', 'gist'],
path: [],
extension: [],
pattern: []
@ -52,17 +52,24 @@ export const FileExplorer = (props: FileExplorerProps) => {
}, {
id: 'delete',
name: 'Delete',
type: ['file', 'folder'],
type: ['file', 'folder', 'gist'],
path: [],
extension: [],
pattern: []
}, {
id: 'pushChangesToGist',
name: 'Push changes to gist',
type: [],
name: 'Push back changes to gist',
type: ['gist'],
path: [],
extension: [],
pattern: ['^browser/gists/([0-9]|[a-z])*$']
pattern: []
}, {
id: 'publishFolderToGist',
name: 'Publish folder to gist',
type: ['folder'],
path: [],
extension: [],
pattern: []
}, {
id: 'run',
name: 'Run',
@ -228,6 +235,15 @@ export const FileExplorer = (props: FileExplorerProps) => {
else return false
}
const getFocusedFolder = () => {
if (state.focusElement[0]) {
if (state.focusElement[0].type === 'folder' && state.focusElement[0].key) return state.focusElement[0].key
else if (state.focusElement[0].type === 'gist' && state.focusElement[0].key) return state.focusElement[0].key
else if (state.focusElement[0].type === 'file' && state.focusElement[0].key) return extractParentFromKey(state.focusElement[0].key)
else return name
}
}
const createNewFile = async (newFilePath: string) => {
const fileManager = state.fileManager
@ -309,7 +325,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
// 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
// pick that up via the 'fileAdded' event from the files module.
const parentFolder = state.focusElement[0] ? state.focusElement[0].type === 'folder' ? state.focusElement[0].key : extractParentFromKey(state.focusElement[0].key) : name
const parentFolder = getFocusedFolder()
const expandPath = [...new Set([...state.expandPath, parentFolder])]
setState(prevState => {
@ -359,7 +375,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
modal('Create a public gist', `Are you sure you want to anonymously publish all your files in the ${name} workspace as a public gist on github.com?`, 'OK', toGist, 'Cancel', () => {})
}
const toGist = (id?: string) => {
const toGist = (path?: string, type?: string) => {
const filesProvider = fileSystem.provider.provider
const proccedResult = function (error, data) {
if (error) {
@ -393,7 +409,8 @@ export const FileExplorer = (props: FileExplorerProps) => {
}
// If 'id' is not defined, it is not a gist update but a creation so we have to take the files from the browser explorer.
const folder = id ? '/gists/' + id : '/'
const folder = path || '/'
const id = type === 'gist' ? extractNameFromKey(path).split('-')[1] : null
packageFiles(filesProvider, folder, async (error, packaged) => {
if (error) {
@ -501,15 +518,15 @@ export const FileExplorer = (props: FileExplorerProps) => {
})
}
const handleClickFile = (path: string) => {
const handleClickFile = (path: string, type: string) => {
path = path.indexOf(props.name + '/') === 0 ? path.replace(props.name + '/', '') : path
state.fileManager.open(path)
setState(prevState => {
return { ...prevState, focusElement: [{ key: path, type: 'file' }] }
return { ...prevState, focusElement: [{ key: path, type }] }
})
}
const handleClickFolder = async (path: string) => {
const handleClickFolder = async (path: string, type: string) => {
if (state.ctrlKey) {
if (state.focusElement.findIndex(item => item.key === path) !== -1) {
setState(prevState => {
@ -517,7 +534,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
})
} else {
setState(prevState => {
return { ...prevState, focusElement: [...prevState.focusElement, { key: path, type: 'folder' }] }
return { ...prevState, focusElement: [...prevState.focusElement, { key: path, type }] }
})
}
} else {
@ -531,22 +548,22 @@ export const FileExplorer = (props: FileExplorerProps) => {
}
setState(prevState => {
return { ...prevState, focusElement: [{ key: path, type: 'folder' }], expandPath }
return { ...prevState, focusElement: [{ key: path, type }], expandPath }
})
}
}
const handleContextMenuFile = (pageX: number, pageY: number, path: string, content: string) => {
const handleContextMenuFile = (pageX: number, pageY: number, path: string, content: string, type: string) => {
if (!content) return
setState(prevState => {
return { ...prevState, focusContext: { element: path, x: pageX, y: pageY, type: 'file' }, focusEdit: { ...prevState.focusEdit, lastEdit: content }, showContextMenu: prevState.focusEdit.element !== path }
return { ...prevState, focusContext: { element: path, x: pageX, y: pageY, type }, focusEdit: { ...prevState.focusEdit, lastEdit: content }, showContextMenu: prevState.focusEdit.element !== path }
})
}
const handleContextMenuFolder = (pageX: number, pageY: number, path: string, content: string) => {
const handleContextMenuFolder = (pageX: number, pageY: number, path: string, content: string, type: string) => {
if (!content) return
setState(prevState => {
return { ...prevState, focusContext: { element: path, x: pageX, y: pageY, type: 'folder' }, focusEdit: { ...prevState.focusEdit, lastEdit: content }, showContextMenu: prevState.focusEdit.element !== path }
return { ...prevState, focusContext: { element: path, x: pageX, y: pageY, type }, focusEdit: { ...prevState.focusEdit, lastEdit: content }, showContextMenu: prevState.focusEdit.element !== path }
})
}
@ -618,7 +635,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
}
const handleNewFileInput = async (parentFolder?: string) => {
if (!parentFolder) parentFolder = state.focusElement[0] ? state.focusElement[0].type === 'folder' ? state.focusElement[0].key ? state.focusElement[0].key : name : extractParentFromKey(state.focusElement[0].key) ? extractParentFromKey(state.focusElement[0].key) : name : name
if (!parentFolder) parentFolder = getFocusedFolder()
const expandPath = [...new Set([...state.expandPath, parentFolder])]
await addInputField(fileSystem.provider.provider, 'file', parentFolder)(dispatch)
@ -629,7 +646,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
}
const handleNewFolderInput = async (parentFolder?: string) => {
if (!parentFolder) parentFolder = state.focusElement[0] ? state.focusElement[0].type === 'folder' ? state.focusElement[0].key ? state.focusElement[0].key : name : extractParentFromKey(state.focusElement[0].key) ? extractParentFromKey(state.focusElement[0].key) : name : name
if (!parentFolder) parentFolder = getFocusedFolder()
else if ((parentFolder.indexOf('.sol') !== -1) || (parentFolder.indexOf('.js') !== -1)) parentFolder = extractParentFromKey(parentFolder)
const expandPath = [...new Set([...state.expandPath, parentFolder])]
@ -705,12 +722,12 @@ export const FileExplorer = (props: FileExplorerProps) => {
label={label(file)}
onClick={(e) => {
e.stopPropagation()
if (state.focusEdit.element !== file.path) handleClickFolder(file.path)
if (state.focusEdit.element !== file.path) handleClickFolder(file.path, file.type)
}}
onContextMenu={(e) => {
e.preventDefault()
e.stopPropagation()
handleContextMenuFolder(e.pageX, e.pageY, file.path, e.target.textContent)
handleContextMenuFolder(e.pageX, e.pageY, file.path, e.target.textContent, file.type)
}}
labelClass={labelClass}
controlBehaviour={ state.ctrlKey }
@ -742,12 +759,12 @@ export const FileExplorer = (props: FileExplorerProps) => {
label={label(file)}
onClick={(e) => {
e.stopPropagation()
if (state.focusEdit.element !== file.path) handleClickFile(file.path)
if (state.focusEdit.element !== file.path) handleClickFile(file.path, file.type)
}}
onContextMenu={(e) => {
e.preventDefault()
e.stopPropagation()
handleContextMenuFile(e.pageX, e.pageY, file.path, e.target.textContent)
handleContextMenuFile(e.pageX, e.pageY, file.path, e.target.textContent, file.type)
}}
icon={icon}
labelClass={labelClass}
@ -841,6 +858,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
e.stopPropagation()
handleMouseOver(state.focusContext.element)
}}
publishToGist={publishToGist}
/>
}
</div>

@ -258,7 +258,8 @@ const resolveDirectory = (root, path: string, files, content) => {
files = _.set(files, _path, {
isDirectory: true,
path,
name: extractNameFromKey(path),
name: extractNameFromKey(path).indexOf('gist-') === 0 ? extractNameFromKey(path).split('-')[1] : extractNameFromKey(path),
type: extractNameFromKey(path).indexOf('gist-') === 0 ? 'gist' : 'folder',
child: { ...content[pathArr[pathArr.length - 1]], ...(prevFiles ? prevFiles.child : {}) }
})
@ -278,7 +279,8 @@ const removePath = (root, path: string, pathName, files) => {
files = _.set(files, _path, {
isDirectory: true,
path,
name: extractNameFromKey(path),
name: extractNameFromKey(path).indexOf('gist-') === 0 ? extractNameFromKey(path).split('-')[1] : extractNameFromKey(path),
type: extractNameFromKey(path).indexOf('gist-') === 0 ? 'gist' : 'folder',
child: prevFiles ? prevFiles.child : {}
})
@ -336,7 +338,8 @@ const fileRenamed = (root, path: string, removePath: string, files, content) =>
files = _.set(files, _path, {
isDirectory: true,
path,
name: extractNameFromKey(path),
name: extractNameFromKey(path).indexOf('gist-') === 0 ? extractNameFromKey(path).split('-')[1] : extractNameFromKey(path),
type: extractNameFromKey(path).indexOf('gist-') === 0 ? 'gist' : 'folder',
child: { ...content[pathArr[pathArr.length - 1]], ...prevFiles.child }
})

@ -15,6 +15,7 @@ export interface File {
path: string,
name: string,
isDirectory: boolean,
type: string,
child?: File[]
}
@ -24,7 +25,7 @@ export interface FileExplorerMenuProps {
fileManager: any,
createNewFile: (folder?: string) => void,
createNewFolder: (parentFolder?: string) => void,
publishToGist: () => void,
publishToGist: (path?: string) => void,
uploadFile: (target: EventTarget & HTMLInputElement) => void
}
@ -35,7 +36,7 @@ export interface FileExplorerContextMenuProps {
deletePath: (path: string) => void,
renamePath: (path: string, type: string) => void,
hideContextMenu: () => void,
publishToGist?: () => void,
publishToGist?: (path?: string, type?: string) => void,
runScript?: (path: string) => void,
emit?: (id: string, path: string) => void,
pageX: number,

Loading…
Cancel
Save