Debub editable content

pull/5370/head
ioedeveloper 4 years ago
parent ae560ac72e
commit fb8aadf418
  1. 89
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx

@ -11,7 +11,6 @@ import './css/file-explorer.css'
export const FileExplorer = (props: FileExplorerProps) => { export const FileExplorer = (props: FileExplorerProps) => {
const { filesProvider, name, registry, plugin } = props const { filesProvider, name, registry, plugin } = props
const containerRef = useRef(null)
const [state, setState] = useState({ const [state, setState] = useState({
focusElement: [{ focusElement: [{
key: name, key: name,
@ -32,7 +31,8 @@ export const FileExplorer = (props: FileExplorerProps) => {
focusEdit: { focusEdit: {
element: null, element: null,
type: '', type: '',
isNew: false isNew: false,
lastEdit: ''
}, },
fileExternallyChanged: false, fileExternallyChanged: false,
expandPath: [], expandPath: [],
@ -180,12 +180,6 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
const createNewFolder = async (parentFolder: string, newFolderPath: string) => { const createNewFolder = async (parentFolder: string, newFolderPath: string) => {
// const self = this
// modalDialogCustom.prompt('Create new folder', '', 'New folder', (input) => {
// if (!input) {
// return tooltip('Failed to create folder. The name can not be empty')
// }
const fileManager = state.fileManager const fileManager = state.fileManager
const dirName = newFolderPath + '/' const dirName = newFolderPath + '/'
// if (error) return tooltip('Unexpected error while creating folder: ' + error) // if (error) return tooltip('Unexpected error while creating folder: ' + error)
@ -198,18 +192,14 @@ export const FileExplorer = (props: FileExplorerProps) => {
} catch (e) { } catch (e) {
// tooltip('Failed to create file ' + newName) // tooltip('Failed to create file ' + newName)
} }
// }, null, true)
} }
const deletePath = async (path: string) => { const deletePath = async (path: string) => {
// if (self.files.isReadOnly(key)) { return tooltip('cannot delete file. ' + self.files.type + ' is a read only explorer') } // if (self.files.isReadOnly(key)) { return tooltip('cannot delete file. ' + self.files.type + ' is a read only explorer') }
if (filesProvider.isReadOnly(path)) return if (filesProvider.isReadOnly(path)) return
// modalDialogCustom.confirm(
// 'Delete file', `Are you sure you want to delete ${currentFilename} file?`,
// async () => {
const isDir = state.fileManager.isDirectory(path) const isDir = state.fileManager.isDirectory(path)
showModal('Delete file', `Are you sure you want to delete ${path} ${isDir ? 'folder' : 'file'}?`, { modal('Delete file', `Are you sure you want to delete ${path} ${isDir ? 'folder' : 'file'}?`, {
label: 'Ok', label: 'Ok',
fn: async () => { fn: async () => {
try { try {
@ -236,14 +226,19 @@ export const FileExplorer = (props: FileExplorerProps) => {
const fileManager = state.fileManager const fileManager = state.fileManager
const exists = fileManager.exists(newPath) const exists = fileManager.exists(newPath)
if (exists) return if (exists) {
// modalDialogCustom.alert(File already exists.) return modal('Create File Failed', 'File already exists', {
await fileManager.rename(oldPath, newPath) label: 'Ok',
const files = replacePath(oldPath, newPath, state.files) fn: async () => {}
}, null)
} else {
await fileManager.rename(oldPath, newPath)
const files = replacePath(oldPath, newPath, state.files)
setState(prevState => { setState(prevState => {
return { ...prevState, files } return { ...prevState, files }
}) })
}
} catch (error) { } catch (error) {
// modalDialogCustom.alert('Unexpected error while renaming: ' + error) // modalDialogCustom.alert('Unexpected error while renaming: ' + error)
} }
@ -504,7 +499,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
}) })
} }
const showModal = (title: string, message: string, ok: { label: string, fn: () => void }, cancel: { label: string, fn: () => void }) => { const modal = (title: string, message: string, ok: { label: string, fn: () => void }, cancel: { label: string, fn: () => void }) => {
setState(prevState => { setState(prevState => {
return { return {
...prevState, ...prevState,
@ -555,15 +550,17 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
} }
const handleContextMenuFile = (pageX: number, pageY: number, path: string) => { const handleContextMenuFile = (pageX: number, pageY: number, path: string, content: string) => {
if (!content) return
setState(prevState => { setState(prevState => {
return { ...prevState, focusContext: { element: path, x: pageX, y: pageY } } return { ...prevState, focusContext: { element: path, x: pageX, y: pageY }, focusEdit: { ...prevState.focusEdit, lastEdit: content } }
}) })
} }
const handleContextMenuFolder = (pageX: number, pageY: number, path: string) => { const handleContextMenuFolder = (pageX: number, pageY: number, path: string, content: string) => {
if (!content) return
setState(prevState => { setState(prevState => {
return { ...prevState, focusContext: { element: path, x: pageX, y: pageY } } return { ...prevState, focusContext: { element: path, x: pageX, y: pageY }, focusEdit: { ...prevState.focusEdit, lastEdit: content } }
}) })
} }
@ -576,7 +573,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
const editModeOn = (path: string, type: string, isNew: boolean = false) => { const editModeOn = (path: string, type: string, isNew: boolean = false) => {
if (filesProvider.isReadOnly(path)) return if (filesProvider.isReadOnly(path)) return
setState(prevState => { setState(prevState => {
return { ...prevState, focusEdit: { element: path, isNew, type } } return { ...prevState, focusEdit: { ...prevState.focusEdit, element: path, isNew, type } }
}) })
} }
@ -589,23 +586,16 @@ export const FileExplorer = (props: FileExplorerProps) => {
const updatedFiles = files.filter(file => file) const updatedFiles = files.filter(file => file)
setState(prevState => { setState(prevState => {
return { ...prevState, files: updatedFiles, focusEdit: { element: null, isNew: false, type: '' } } return { ...prevState, files: updatedFiles, focusEdit: { element: null, isNew: false, type: '', lastEdit: '' } }
}) })
} else { } else {
// modalDialogCustom.alert('Are you sure you want to delete this file?') setState(prevState => {
if (true) { // eslint-disable-line return { ...prevState, focusEdit: { element: null, isNew: false, type: '', lastEdit: '' } }
await deletePath(state.focusEdit.element) })
setState(prevState => {
return { ...prevState, focusEdit: { element: null, isNew: false, type: '' } }
})
} else {
}
} }
} else { } else {
if (helper.checkSpecialChars(content)) { if (helper.checkSpecialChars(content)) {
showModal('Validation Error', 'Special characters are not allowed', { modal('Validation Error', 'Special characters are not allowed', {
label: 'Ok', label: 'Ok',
fn: () => {} fn: () => {}
}, null) }, null)
@ -626,7 +616,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
renamePath(oldPath, newPath) renamePath(oldPath, newPath)
} }
setState(prevState => { setState(prevState => {
return { ...prevState, focusEdit: { element: null, isNew: false, type: '' } } return { ...prevState, focusEdit: { element: null, isNew: false, type: '', lastEdit: '' } }
}) })
} }
} }
@ -693,7 +683,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
onContextMenu={(e) => { onContextMenu={(e) => {
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
handleContextMenuFolder(e.pageX, e.pageY, file.path) handleContextMenuFolder(e.pageX, e.pageY, file.path, e.target.textContent)
}} }}
labelClass={ state.focusEdit.element === file.path ? 'bg-light' : state.focusElement.findIndex(item => item.key === file.path) !== -1 ? 'bg-secondary' : '' } labelClass={ state.focusEdit.element === file.path ? 'bg-light' : state.focusElement.findIndex(item => item.key === file.path) !== -1 ? 'bg-secondary' : '' }
editable={state.focusEdit.element === file.path} editable={state.focusEdit.element === file.path}
@ -740,7 +730,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
onContextMenu={(e) => { onContextMenu={(e) => {
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
handleContextMenuFile(e.pageX, e.pageY, file.path) handleContextMenuFile(e.pageX, e.pageY, file.path, e.target.textContent)
}} }}
icon='fa fa-file' icon='fa fa-file'
labelClass={ state.focusEdit.element === file.path ? 'bg-light' : state.focusElement.findIndex(item => item.key === file.path) !== -1 ? 'bg-secondary' : '' } labelClass={ state.focusEdit.element === file.path ? 'bg-light' : state.focusElement.findIndex(item => item.key === file.path) !== -1 ? 'bg-secondary' : '' }
@ -767,22 +757,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
return ( return (
<div <div>
ref={containerRef}
tabIndex={-1}
onKeyDown={(e) => {
if (e.ctrlKey) {
setState(prevState => {
return { ...prevState, ctrlKey: true }
})
}
}}
onKeyUp={() => {
setState(prevState => {
return { ...prevState, ctrlKey: false }
})
}}
>
<TreeView id='treeView'> <TreeView id='treeView'>
<TreeViewItem id="treeViewItem" <TreeViewItem id="treeViewItem"
label={ label={

Loading…
Cancel
Save