Skip to content

Commit 1166845

Browse files
committed
feat: add file
close #102
1 parent 46ce6d2 commit 1166845

File tree

5 files changed

+109
-7
lines changed

5 files changed

+109
-7
lines changed

apps/linebyline/src/components/EditorArea/Editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function Editor(props: EditorProps) {
5656
filePath: file.path,
5757
})
5858
setContent(text as string)
59-
} else if (file.content) {
59+
} else if (file.content !== undefined) {
6060
setContent(file.content)
6161
}
6262
}

apps/linebyline/src/components/Explorer/index.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { useGlobalCacheData, useOpen } from '@/hooks'
1010
import { CacheManager } from '@/helper'
1111
import { Empty, FileTree, List, Popper } from '@/components'
1212
import styled from 'styled-components'
13+
import type { RightNavItem } from '../SideBar/SideBarHeader'
14+
import SideBarHeader from '../SideBar/SideBarHeader'
1315

1416
const RecentListBottom = styled.div`
1517
padding: 8px;
@@ -24,7 +26,7 @@ const RecentListBottom = styled.div`
2426

2527
const Explorer: FC<ExplorerProps> = (props) => {
2628
const { t } = useTranslation()
27-
const { folderData, activeId, addOpenedFile, setActiveId } = useEditorStore()
29+
const { addFile, folderData, activeId, addOpenedFile, setActiveId } = useEditorStore()
2830
const [popperOpen, setPopperOpen] = useState(false)
2931
const [cache] = useGlobalCacheData()
3032
const { openFolderDialog, openFolder } = useOpen()
@@ -49,6 +51,12 @@ const Explorer: FC<ExplorerProps> = (props) => {
4951
[openFolder],
5052
)
5153

54+
const handleRightNavItemClick = useCallback((item: RightNavItem) => {
55+
if (item.key === 'addFile') {
56+
addFile()
57+
}
58+
}, [addFile])
59+
5260
const listData = useMemo(
5361
() =>
5462
cache.openFolderHistory.map((history: { time: string; path: string }) => ({
@@ -63,10 +71,11 @@ const Explorer: FC<ExplorerProps> = (props) => {
6371

6472
return (
6573
<Container className={containerCLs}>
66-
<div className='explorer-header'>
67-
<small>EXPLORER</small>
68-
<div className='flex' />
69-
</div>
74+
<SideBarHeader
75+
name='EXPLORER'
76+
onRightNavItemClick={handleRightNavItemClick}
77+
rightNavItems={[{ iconCls: 'ri-file-add-line', key: 'addFile' }]}
78+
/>
7079
<div className='h-full w-full overflow-auto'>
7180
{folderData && folderData.length > 1 ? (
7281
<FileTree
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { FC } from 'react'
2+
import styled from 'styled-components'
3+
4+
const Container = styled.div`
5+
display: flex;
6+
justify-content: space-between;
7+
align-items: center;
8+
height: 2rem;
9+
padding: 0 8px;
10+
line-height: 2rem;
11+
border-bottom: 1px solid ${(props) => props.theme.borderColor};
12+
`
13+
14+
const SideBarHeader: FC<SideBarHeaderProps> = (props) => {
15+
const handleRightNavItemClick = (item: RightNavItem) => {
16+
if (props.onRightNavItemClick) {
17+
props.onRightNavItemClick(item)
18+
}
19+
}
20+
return (
21+
<Container>
22+
<small>{props.name}</small>
23+
<div className='flex'>
24+
{props.rightNavItems?.map((item) => {
25+
return (
26+
<i
27+
key={item.key}
28+
className={`icon ${item.iconCls}`}
29+
onClick={() => handleRightNavItemClick(item)}
30+
/>
31+
)
32+
})}
33+
</div>
34+
</Container>
35+
)
36+
}
37+
38+
export default SideBarHeader
39+
40+
export interface RightNavItem {
41+
iconCls: string
42+
key: React.Key
43+
[key: string]: any
44+
}
45+
46+
export interface SideBarHeaderProps {
47+
name: string
48+
onRightNavItemClick?: (item: RightNavItem) => void
49+
rightNavItems?: RightNavItem[]
50+
}

apps/linebyline/src/helper/filesys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const createFile = (opt?: Partial<IFile>): IFile => {
3434
name: 'Untitled.md',
3535
kind: 'file',
3636
path: undefined,
37+
content: '',
3738
...opt,
3839
}
3940

apps/linebyline/src/stores/useEditorStore.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { create } from 'zustand'
2-
import type { IFile } from '@/helper/filesys'
2+
import { createFile, type IFile } from '@/helper/filesys'
33
import type { EditorDelegate } from '@linebyline/editor/types'
44

55
const useEditorStore = create<EditorStore>((set, get) => {
@@ -9,6 +9,47 @@ const useEditorStore = create<EditorStore>((set, get) => {
99
folderData: null,
1010
editorCtxMap: new Map(),
1111

12+
addFile: () => {
13+
const untitledFile = createFile()
14+
const { activeId, folderData, setActiveId, addOpenedFile } = get()
15+
if (activeId && folderData ) {
16+
const dfs = (tree: IFile[]) => {
17+
for(let i = 0; i < tree.length; i++) {
18+
if (tree[i].id === activeId) {
19+
tree.splice(i+1, 0, untitledFile)
20+
set(state => {
21+
return {
22+
...state,
23+
folderData: [
24+
...(state.folderData || []),
25+
]
26+
}
27+
})
28+
setActiveId(untitledFile.id)
29+
addOpenedFile(untitledFile.id)
30+
return
31+
} else if (tree[i]?.children) {
32+
dfs(tree[i].children!)
33+
}
34+
}
35+
}
36+
dfs(folderData)
37+
38+
} else {
39+
set(state => {
40+
return {
41+
...state,
42+
folderData: [
43+
...(state.folderData || []),
44+
untitledFile
45+
]
46+
}
47+
})
48+
setActiveId(untitledFile.id)
49+
addOpenedFile(untitledFile.id)
50+
}
51+
},
52+
1253
setActiveId: (id: string) => {
1354
set((state) => ({
1455
...state,
@@ -71,6 +112,7 @@ interface EditorStore {
71112
folderData: null | IFile[]
72113
editorCtxMap: Map<string, EditorDelegate<any>>
73114
setActiveId: (id: string) => void
115+
addFile: () => void
74116
addOpenedFile: (id: string) => void
75117
delOpenedFile: (id: string) => void
76118
setFolderData: (folderData: IFile[]) => void

0 commit comments

Comments
 (0)