Commit 6ee39a5c authored by tom's avatar tom

select file in the tree

parent ecb17934
...@@ -5,7 +5,7 @@ import React from 'react'; ...@@ -5,7 +5,7 @@ import React from 'react';
import type { File } from './types'; import type { File } from './types';
import CodeEditorFileExplorer from './CodeEditorFileExplorer'; import CodeEditorSideBar from './CodeEditorSideBar';
import * as themes from './utils/themes'; import * as themes from './utils/themes';
export type Monaco = typeof monaco; export type Monaco = typeof monaco;
...@@ -16,7 +16,7 @@ interface Props { ...@@ -16,7 +16,7 @@ interface Props {
const CodeEditor = ({ data }: Props) => { const CodeEditor = ({ data }: Props) => {
const instance = React.useRef<Monaco>(); const instance = React.useRef<Monaco>();
const [ index ] = React.useState(0); const [ index, setIndex ] = React.useState(0);
const { colorMode } = useColorMode(); const { colorMode } = useColorMode();
...@@ -35,10 +35,9 @@ const CodeEditor = ({ data }: Props) => { ...@@ -35,10 +35,9 @@ const CodeEditor = ({ data }: Props) => {
}, [ ]); }, [ ]);
return ( return (
<Flex overflow="hidden" borderRadius="md"> <Flex overflow="hidden" borderRadius="md" height="500px">
<Box flexGrow={ 1 }> <Box flexGrow={ 1 }>
<MonacoEditor <MonacoEditor
height="500px"
language="sol" language="sol"
path={ data[index].file_path } path={ data[index].file_path }
defaultValue={ data[index].source_code } defaultValue={ data[index].source_code }
...@@ -46,9 +45,7 @@ const CodeEditor = ({ data }: Props) => { ...@@ -46,9 +45,7 @@ const CodeEditor = ({ data }: Props) => {
onMount={ handleEditorDidMount } onMount={ handleEditorDidMount }
/> />
</Box> </Box>
<Box w="250px" flexShrink={ 0 } bgColor="lightpink" fontSize="sm"> <CodeEditorSideBar data={ data } onFileSelect={ setIndex }/>
<CodeEditorFileExplorer data={ data }/>
</Box>
</Flex> </Flex>
); );
}; };
......
...@@ -8,16 +8,26 @@ import composeFileTree from './utils/composeFileTree'; ...@@ -8,16 +8,26 @@ import composeFileTree from './utils/composeFileTree';
interface Props { interface Props {
data: Array<File>; data: Array<File>;
onFileSelect: (index: number) => void;
} }
const CodeEditorFileExplorer = ({ data }: Props) => { const CodeEditorFileExplorer = ({ data, onFileSelect }: Props) => {
const tree = React.useMemo(() => { const tree = React.useMemo(() => {
return composeFileTree(data); return composeFileTree(data);
}, [ data ]); }, [ data ]);
const handleFileClick = React.useCallback((event: React.MouseEvent) => {
const filePath = (event.currentTarget as HTMLDivElement).getAttribute('data-file-path');
const fileIndex = data.findIndex((item) => item.file_path === filePath);
if (fileIndex > -1) {
onFileSelect(fileIndex);
}
}, [ data, onFileSelect ]);
return ( return (
<Box> <Box>
<CodeEditorFileTree tree={ tree }/> <CodeEditorFileTree tree={ tree } onItemClick={ handleFileClick }/>
</Box> </Box>
); );
}; };
......
...@@ -7,9 +7,10 @@ import type { FileTree } from './types'; ...@@ -7,9 +7,10 @@ import type { FileTree } from './types';
interface Props { interface Props {
tree: FileTree; tree: FileTree;
level?: number; level?: number;
onItemClick: (event: React.MouseEvent) => void;
} }
const CodeEditorFileTree = ({ tree, level = 0 }: Props) => { const CodeEditorFileTree = ({ tree, level = 0, onItemClick }: Props) => {
const itemProps: ChakraProps = { const itemProps: ChakraProps = {
ml: level ? 4 : 0, ml: level ? 4 : 0,
borderWidth: '0px', borderWidth: '0px',
...@@ -18,8 +19,9 @@ const CodeEditorFileTree = ({ tree, level = 0 }: Props) => { ...@@ -18,8 +19,9 @@ const CodeEditorFileTree = ({ tree, level = 0 }: Props) => {
}, },
cursor: 'pointer', cursor: 'pointer',
}; };
return ( return (
<Accordion allowMultiple> <Accordion allowMultiple defaultIndex={ tree.map((item, index) => index) } reduceMotion>
{ {
tree.map((leaf, index) => { tree.map((leaf, index) => {
if ('children' in leaf) { if ('children' in leaf) {
...@@ -30,14 +32,14 @@ const CodeEditorFileTree = ({ tree, level = 0 }: Props) => { ...@@ -30,14 +32,14 @@ const CodeEditorFileTree = ({ tree, level = 0 }: Props) => {
<span>{ leaf.name }</span> <span>{ leaf.name }</span>
</AccordionButton> </AccordionButton>
<AccordionPanel p={ 0 }> <AccordionPanel p={ 0 }>
<CodeEditorFileTree tree={ leaf.children } level={ level + 1 }/> <CodeEditorFileTree tree={ leaf.children } level={ level + 1 } onItemClick={ onItemClick }/>
</AccordionPanel> </AccordionPanel>
</AccordionItem> </AccordionItem>
); );
} }
return ( return (
<AccordionItem key={ index } { ...itemProps }> <AccordionItem key={ index } { ...itemProps } onClick={ onItemClick } data-file-path={ leaf.file_path }>
{ leaf.name } { leaf.name }
</AccordionItem> </AccordionItem>
); );
......
import { Box } from '@chakra-ui/react';
import React from 'react';
import type { File } from './types';
import CodeEditorFileExplorer from './CodeEditorFileExplorer';
interface Props {
data: Array<File>;
onFileSelect: (index: number) => void;
}
const CodeEditorSideBar = ({ onFileSelect, data }: Props) => {
return (
<Box w="250px" flexShrink={ 0 } bgColor="lightpink" fontSize="sm" overflowY="scroll">
<Box fontFamily="heading" pl={ 3 } letterSpacing={ 0.5 } fontWeight={ 600 }>EXPLORER</Box>
<CodeEditorFileExplorer data={ data } onFileSelect={ onFileSelect }/>
</Box>
);
};
export default React.memo(CodeEditorSideBar);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment