@@ -2,14 +2,17 @@ import React, {useEffect, useState, useRef, SyntheticEvent} from 'react' // esli
22import { useIntl } from 'react-intl'
33import { TreeView } from '@remix-ui/tree-view' // eslint-disable-line
44import { FileExplorerMenu } from './file-explorer-menu' // eslint-disable-line
5- import { FileExplorerProps , WorkSpaceState } from '../types'
5+ import { FileExplorerContextMenu } from './file-explorer-context-menu' // eslint-disable-line
6+ import { FileExplorerProps , FileType , WorkSpaceState } from '../types'
67
78import '../css/file-explorer.css'
89import { checkSpecialChars , extractNameFromKey , extractParentFromKey , joinPath } from '@remix-ui/helper'
910// eslint-disable-next-line @typescript-eslint/no-unused-vars
1011import { FileRender } from './file-render'
11- import { Drag } from '@remix-ui/drag-n-drop'
12+ import { Drag , Draggable } from '@remix-ui/drag-n-drop'
1213import { ROOT_PATH } from '../utils/constants'
14+ import { fileKeySort } from '../utils'
15+ import { moveFileIsAllowed , moveFolderIsAllowed } from '../actions'
1316
1417export const FileExplorer = ( props : FileExplorerProps ) => {
1518 const intl = useIntl ( )
@@ -31,6 +34,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
3134 } = props
3235 const [ state , setState ] = useState < WorkSpaceState > ( workspaceState )
3336 const treeRef = useRef < HTMLDivElement > ( null )
37+ const [ childrenKeys , setChildrenKeys ] = useState < string [ ] > ( [ ] )
3438
3539 useEffect ( ( ) => {
3640 if ( contextMenuItems ) {
@@ -288,32 +292,62 @@ export const FileExplorer = (props: FileExplorerProps) => {
288292 props . dispatchHandleExpandPath ( expandPath )
289293 }
290294
291- const handleFileMove = ( dest : string , src : string ) => {
295+ const handleFileMove = async ( dest : string , src : string ) => {
296+ if ( await moveFileIsAllowed ( src , dest ) === false ) return
292297 try {
293- props . dispatchMoveFile ( src , dest )
298+ props . modal (
299+ intl . formatMessage ( { id : 'filePanel.moveFile' } ) ,
300+ intl . formatMessage ( { id : 'filePanel.moveFileMsg1' } , { src, dest } ) ,
301+ intl . formatMessage ( { id : 'filePanel.yes' } ) ,
302+ ( ) => props . dispatchMoveFile ( src , dest ) ,
303+ intl . formatMessage ( { id : 'filePanel.cancel' } ) ,
304+ ( ) => { }
305+ )
294306 } catch ( error ) {
295307 props . modal (
296- intl . formatMessage ( { id : 'filePanel.movingFileFailed' } ) ,
297- intl . formatMessage ( { id : 'filePanel.movingFileFailedMsg' } , { src} ) ,
298- intl . formatMessage ( { id : 'filePanel.close' } ) ,
308+ intl . formatMessage ( { id : 'filePanel.movingFileFailed' } ) ,
309+ intl . formatMessage ( { id : 'filePanel.movingFileFailedMsg' } , { src } ) ,
310+ intl . formatMessage ( { id : 'filePanel.close' } ) ,
299311 async ( ) => { }
300312 )
301313 }
302314 }
303315
304- const handleFolderMove = ( dest : string , src : string ) => {
316+ const handleFolderMove = async ( dest : string , src : string ) => {
317+ if ( await moveFolderIsAllowed ( src , dest ) === false ) return
305318 try {
306- props . dispatchMoveFolder ( src , dest )
319+ props . modal (
320+ intl . formatMessage ( { id : 'filePanel.moveFile' } ) ,
321+ intl . formatMessage ( { id : 'filePanel.moveFileMsg1' } , { src, dest } ) ,
322+ intl . formatMessage ( { id : 'filePanel.yes' } ) ,
323+ ( ) => props . dispatchMoveFolder ( src , dest ) ,
324+ intl . formatMessage ( { id : 'filePanel.cancel' } ) ,
325+ ( ) => { }
326+ )
307327 } catch ( error ) {
308328 props . modal (
309- intl . formatMessage ( { id : 'filePanel.movingFolderFailed' } ) ,
310- intl . formatMessage ( { id : 'filePanel.movingFolderFailedMsg' } , { src} ) ,
311- intl . formatMessage ( { id : 'filePanel.close' } ) ,
329+ intl . formatMessage ( { id : 'filePanel.movingFolderFailed' } ) ,
330+ intl . formatMessage ( { id : 'filePanel.movingFolderFailedMsg' } , { src } ) ,
331+ intl . formatMessage ( { id : 'filePanel.close' } ) ,
312332 async ( ) => { }
313333 )
314334 }
315335 }
316336
337+ useEffect ( ( ) => {
338+ if ( files [ ROOT_PATH ] ) {
339+ try {
340+ const children : FileType [ ] = files [ ROOT_PATH ] as any
341+ setChildrenKeys ( fileKeySort ( children ) )
342+ } catch ( error ) {
343+ setChildrenKeys ( Object . keys ( files [ ROOT_PATH ] ) )
344+ }
345+ } else {
346+ setChildrenKeys ( [ ] )
347+ }
348+ } , [ props ] )
349+
350+
317351 return (
318352 < Drag onFileMoved = { handleFileMove } onFolderMoved = { handleFolderMove } >
319353 < div ref = { treeRef } tabIndex = { 0 } style = { { outline : 'none' } } >
@@ -339,10 +373,10 @@ export const FileExplorer = (props: FileExplorerProps) => {
339373 </ span >
340374 </ div >
341375 </ li >
342- < div className = "pb-4 mb-4" >
376+ < div >
343377 < TreeView id = "treeViewMenu" >
344378 { files [ ROOT_PATH ] &&
345- Object . keys ( files [ ROOT_PATH ] ) . map ( ( key , index ) => (
379+ childrenKeys . map ( ( key , index ) => (
346380 < FileRender
347381 file = { files [ ROOT_PATH ] [ key ] }
348382 fileDecorations = { fileState }
@@ -364,6 +398,9 @@ export const FileExplorer = (props: FileExplorerProps) => {
364398 }
365399 </ TreeView >
366400 </ div >
401+ < Draggable isDraggable = { false } file = { { name : '/' , path : '/' , type : 'folder' , isDirectory : true } } expandedPath = { props . expandPath } handleClickFolder = { null } >
402+ < div className = 'd-block w-100 pb-4 mb-4' > </ div >
403+ </ Draggable >
367404 </ TreeView >
368405 </ div >
369406 </ Drag >
0 commit comments