Revert rename if empty

pull/668/head
ioedeveloper 4 years ago
parent a64cc67ad4
commit 8f2ea70e8d
  1. 74
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx
  2. 24
      libs/remix-ui/tree-view/src/lib/tree-view-item/tree-view-item.tsx
  3. 1
      libs/remix-ui/tree-view/src/types/index.ts

@ -51,6 +51,11 @@ export const FileExplorer = (props: FileExplorerProps) => {
handleHide: null handleHide: null
} }
}) })
const editRef = useRef(null)
useEffect(() => {
editRef && editRef.current && editRef.current.focus()
}, [state.focusEdit.element])
useEffect(() => { useEffect(() => {
(async () => { (async () => {
@ -165,15 +170,20 @@ export const FileExplorer = (props: FileExplorerProps) => {
const fileManager = state.fileManager const fileManager = state.fileManager
helper.createNonClashingName(newFilePath, filesProvider, async (error, newName) => { helper.createNonClashingName(newFilePath, filesProvider, async (error, newName) => {
// if (error) return tooltip('Failed to create file ' + newName + ' ' + error) if (error) {
if (error) return return modal('Create File Failed', error, {
const createFile = await fileManager.writeFile(newName, '') label: 'Close',
fn: async () => {}
if (!createFile) { }, null)
// tooltip('Failed to create file ' + newName)
} else { } else {
addFile(parentFolder, newName) const createFile = await fileManager.writeFile(newName, '')
await fileManager.open(newName)
if (!createFile) {
// tooltip('Failed to create file ' + newName)
} else {
addFile(parentFolder, newName)
await fileManager.open(newName)
}
} }
}) })
// }, null, true) // }, null, true)
@ -227,8 +237,8 @@ export const FileExplorer = (props: FileExplorerProps) => {
const exists = fileManager.exists(newPath) const exists = fileManager.exists(newPath)
if (exists) { if (exists) {
return modal('Create File Failed', 'File already exists', { return modal('Rename File Failed', 'File name already exists', {
label: 'Ok', label: 'Close',
fn: async () => {} fn: async () => {}
}, null) }, null)
} else { } else {
@ -240,7 +250,10 @@ export const FileExplorer = (props: FileExplorerProps) => {
}) })
} }
} catch (error) { } catch (error) {
// modalDialogCustom.alert('Unexpected error while renaming: ' + error) modal('Rename File Failed', 'Unexpected error while renaming: ' + error, {
label: 'Close',
fn: async () => {}
}, null)
} }
} }
@ -589,11 +602,17 @@ export const FileExplorer = (props: FileExplorerProps) => {
return { ...prevState, files: updatedFiles, focusEdit: { element: null, isNew: false, type: '', lastEdit: '' } } return { ...prevState, files: updatedFiles, focusEdit: { element: null, isNew: false, type: '', lastEdit: '' } }
}) })
} else { } else {
editRef.current.textContent = state.focusEdit.lastEdit
setState(prevState => { setState(prevState => {
return { ...prevState, focusEdit: { element: null, isNew: false, type: '', lastEdit: '' } } return { ...prevState, focusEdit: { element: null, isNew: false, type: '', lastEdit: '' } }
}) })
} }
} else { } else {
if (state.focusEdit.lastEdit === content) {
return setState(prevState => {
return { ...prevState, focusEdit: { element: null, isNew: false, type: '', lastEdit: '' } }
})
}
if (helper.checkSpecialChars(content)) { if (helper.checkSpecialChars(content)) {
modal('Validation Error', 'Special characters are not allowed', { modal('Validation Error', 'Special characters are not allowed', {
label: 'Ok', label: 'Ok',
@ -647,20 +666,37 @@ export const FileExplorer = (props: FileExplorerProps) => {
editModeOn(parentFolder + '/blank', 'folder', true) editModeOn(parentFolder + '/blank', 'folder', true)
} }
const handleEditInput = (event) => {
if (event.which === 13) {
event.preventDefault()
editModeOff(editRef.current.innerText)
}
}
// warn if file changed outside of Remix // warn if file changed outside of Remix
const remixdDialog = () => { const remixdDialog = () => {
return <div>This file has been changed outside of Remix IDE.</div> return <div>This file has been changed outside of Remix IDE.</div>
} }
const label = (data: File) => { const label = (file: File) => {
return ( return (
<div className='remixui_items'> <div
className='remixui_items d-inline-block w-100'
ref={state.focusEdit.element === file.path ? editRef : null}
suppressContentEditableWarning={true}
contentEditable={state.focusEdit.element === file.path}
onKeyDown={handleEditInput}
onBlur={(e) => {
e.stopPropagation()
editModeOff(editRef.current.innerText)
}}
>
<span <span
title={data.path} title={file.path}
className={'remixui_label ' + (data.isDirectory ? 'folder' : 'remixui_leaf')} className={'remixui_label ' + (file.isDirectory ? 'folder' : 'remixui_leaf')}
data-path={data.path} data-path={file.path}
> >
{ data.name } { file.name }
</span> </span>
</div> </div>
) )
@ -686,8 +722,6 @@ export const FileExplorer = (props: FileExplorerProps) => {
handleContextMenuFolder(e.pageX, e.pageY, file.path, e.target.textContent) 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}
onBlur={(value) => editModeOff(value)}
controlBehaviour={ state.ctrlKey } controlBehaviour={ state.ctrlKey }
expand={state.expandPath.includes(file.path)} expand={state.expandPath.includes(file.path)}
> >
@ -734,8 +768,6 @@ export const FileExplorer = (props: FileExplorerProps) => {
}} }}
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' : '' }
editable={state.focusEdit.element === file.path}
onBlur={(value) => editModeOff(value)}
/> />
{ ((state.focusContext.element === file.path) && (state.focusEdit.element !== file.path)) && { ((state.focusContext.element === file.path) && (state.focusEdit.element !== file.path)) &&
<FileExplorerContextMenu <FileExplorerContextMenu

@ -1,37 +1,21 @@
import React, { useState, useEffect, useRef } from 'react' // eslint-disable-line import React, { useState, useEffect } from 'react' // eslint-disable-line
import { TreeViewItemProps } from '../../types' import { TreeViewItemProps } from '../../types'
import './tree-view-item.css' import './tree-view-item.css'
export const TreeViewItem = (props: TreeViewItemProps) => { export const TreeViewItem = (props: TreeViewItemProps) => {
const { id, children, label, labelClass, expand, iconX = 'fas fa-caret-right', iconY = 'fas fa-caret-down', icon, controlBehaviour = false, innerRef, editable, onBlur, ...otherProps } = props const { id, children, label, labelClass, expand, iconX = 'fas fa-caret-right', iconY = 'fas fa-caret-down', icon, controlBehaviour = false, innerRef, ...otherProps } = props
const [isExpanded, setIsExpanded] = useState(false) const [isExpanded, setIsExpanded] = useState(false)
const contentRef = useRef(null)
useEffect(() => { useEffect(() => {
setIsExpanded(expand) setIsExpanded(expand)
}, [expand]) }, [expand])
useEffect(() => {
if (editable) {
contentRef.current.focus()
}
}, [editable])
const handleInput = (event) => {
if (editable) {
if (event.which === 13) {
event.preventDefault()
onBlur && onBlur(contentRef.current.innerText)
}
}
}
return ( return (
<li ref={innerRef} key={`treeViewLi${id}`} data-id={`treeViewLi${id}`} className='li_tv' onBlur={() => editable && onBlur(contentRef.current.innerText)} {...otherProps}> <li ref={innerRef} key={`treeViewLi${id}`} data-id={`treeViewLi${id}`} className='li_tv' {...otherProps}>
<div key={`treeViewDiv${id}`} data-id={`treeViewDiv${id}`} className={`d-flex flex-row align-items-center ${labelClass}`} onClick={() => !controlBehaviour && setIsExpanded(!isExpanded)}> <div key={`treeViewDiv${id}`} data-id={`treeViewDiv${id}`} className={`d-flex flex-row align-items-center ${labelClass}`} onClick={() => !controlBehaviour && setIsExpanded(!isExpanded)}>
{ children ? <div className={isExpanded ? `px-1 ${iconY} caret caret_tv` : `px-1 ${iconX} caret caret_tv`} style={{ visibility: children ? 'visible' : 'hidden' }}></div> : icon ? <div className={`pr-3 pl-1 ${icon} caret caret_tv`}></div> : null } { children ? <div className={isExpanded ? `px-1 ${iconY} caret caret_tv` : `px-1 ${iconX} caret caret_tv`} style={{ visibility: children ? 'visible' : 'hidden' }}></div> : icon ? <div className={`pr-3 pl-1 ${icon} caret caret_tv`}></div> : null }
<span className='w-100 pl-1' ref={contentRef} suppressContentEditableWarning={true} contentEditable={editable} tabIndex={editable ? -3 : null} onKeyDown={handleInput}> <span className='w-100 pl-1'>
{ label } { label }
</span> </span>
</div> </div>

@ -18,6 +18,5 @@ export interface TreeViewItemProps {
controlBehaviour?: boolean controlBehaviour?: boolean
innerRef?: any, innerRef?: any,
onContextMenu?: (...args: any) => void, onContextMenu?: (...args: any) => void,
editable?: boolean,
onBlur?: (...args: any) => void onBlur?: (...args: any) => void
} }

Loading…
Cancel
Save