Skip to content

Commit 7c191df

Browse files
committed
feat: support open a file (#810)
1 parent 5080aed commit 7c191df

File tree

7 files changed

+105
-15
lines changed

7 files changed

+105
-15
lines changed

apps/desktop/src/components/EditorArea/EmptyState.tsx

+32-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ import styled from 'styled-components'
66

77
export const EmptyState = memo(() => {
88
const { t } = useTranslation()
9-
const { openFolderDialog } = useOpen()
9+
const { openFolderDialog, openFile } = useOpen()
1010

1111
const startNavItems = [
12+
{
13+
name: t('file.openDir'),
14+
action: openFolderDialog,
15+
},
16+
{
17+
name: t('file.openFile'),
18+
action: openFile,
19+
},
1220
{
1321
name: t('action.create_file'),
1422
action: () =>
@@ -17,21 +25,16 @@ export const EmptyState = memo(() => {
1725
content: '',
1826
}),
1927
},
20-
{
21-
name: t('file.openDir'),
22-
action: openFolderDialog,
23-
},
2428
]
2529

2630
return (
2731
<Container className='w-full h-full'>
2832
<div>
29-
<p className='nav-title'>{t('file.emptyOpened')}</p>
3033
<div className='nav-list'>
3134
{startNavItems.map((item) => (
32-
<div className='nav-list__item' key={item.name} onClick={item.action}>
35+
<ActionButton key={item.name} onClick={item.action}>
3336
{item.name}
34-
</div>
37+
</ActionButton>
3538
))}
3639
</div>
3740
</div>
@@ -57,7 +60,7 @@ const Container = styled.div`
5760
align-items: center;
5861
justify-content: center;
5962
gap: 8px;
60-
min-width: 160px;
63+
min-width: 180px;
6164
6265
&__item {
6366
width: 100%;
@@ -66,3 +69,23 @@ const Container = styled.div`
6669
}
6770
}
6871
`
72+
73+
const ActionButton = styled.button`
74+
width: 100%;
75+
display: flex;
76+
align-items: center;
77+
justify-content: center;
78+
padding: 8px 16px;
79+
border: none;
80+
border-radius: 4px;
81+
font-size: 14px;
82+
font-weight: 500;
83+
cursor: pointer;
84+
transition: all 0.3s ease;
85+
background-color: ${(props) => props.theme.bgColorSecondary};
86+
color: ${(props) => props.theme.primaryFontColor};
87+
88+
&:hover {
89+
background-color: ${(props) => props.theme.hoverColor};
90+
}
91+
`

apps/desktop/src/hooks/useOpen.ts

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { open } from '@tauri-apps/plugin-dialog'
33
import { readDirectory } from '@/helper/filesys'
44
import { useEditorStore } from '@/stores'
55
import useOpenedCacheStore from '@/stores/useOpenedCacheStore'
6+
import { addExistingMarkdownFileEdit } from '@/services/editor-file'
7+
import { getFileContent } from '@/services/file-info'
68

79
const useOpen = () => {
810
const { setFolderData } = useEditorStore()
@@ -28,9 +30,24 @@ const useOpen = () => {
2830
openFolder(dir)
2931
}, [openFolder])
3032

33+
const openFile = useCallback(async () => {
34+
const file = await open({ multiple: false, filters: [{ name: 'Markdown', extensions: ['md'] }] })
35+
if (typeof file !== 'string') return
36+
const fileContent = await getFileContent({ filePath: file })
37+
if (!fileContent) return
38+
const fileName = file.split('/').pop() || 'new-file.md'
39+
await addExistingMarkdownFileEdit({
40+
fileName,
41+
content: fileContent,
42+
path: file,
43+
})
44+
}
45+
, [addRecentWorkspaces, setFolderData])
46+
3147
return {
3248
openFolderDialog,
3349
openFolder,
50+
openFile,
3451
}
3552
}
3653

apps/desktop/src/services/editor-file.ts

+35-5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,47 @@ interface AddNewMarkdownFileEditParams {
77
}
88

99
export const addNewMarkdownFileEdit = async (params: AddNewMarkdownFileEditParams) => {
10-
const { fileName, content } = params
10+
await addMarkdownFileEdit(params)
11+
}
12+
13+
interface AddExistingMarkdownFileEditParams {
14+
fileName: string
15+
content: string
16+
path: string
17+
}
18+
19+
export const addExistingMarkdownFileEdit = async (params: AddExistingMarkdownFileEditParams) => {
20+
await addNewMarkdownFileEdit(params)
21+
}
22+
23+
interface AddMarkdownFileEditParams {
24+
fileName: string
25+
content: string
26+
path?: string
27+
}
28+
29+
export const addMarkdownFileEdit = async (params: AddMarkdownFileEditParams) => {
30+
const { fileName } = params
1131
const newFile = createFile({
1232
name: fileName,
13-
content: content,
33+
...params,
1434
})
1535
const { addOpenedFile, setActiveId } = useEditorStore.getState()
1636
const { setIdStateMap } = useEditorStateStore.getState()
1737

1838
addOpenedFile(newFile.id)
1939
setActiveId(newFile.id)
20-
setIdStateMap(newFile.id, {
21-
hasUnsavedChanges: true,
22-
})
40+
if (params.path) {
41+
const state = {
42+
hasUnsavedChanges: false,
43+
undoDepth: 0,
44+
}
45+
const { setIdStateMap } = useEditorStateStore.getState()
46+
47+
setIdStateMap(newFile.id, state)
48+
} else {
49+
setIdStateMap(newFile.id, {
50+
hasUnsavedChanges: true,
51+
})
52+
}
2353
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { invoke } from '@tauri-apps/api/core'
2+
3+
export const getFileContent = async (params: { filePath?: string }) => {
4+
const { filePath } = params
5+
if (!filePath) {
6+
return null
7+
}
8+
const isExists = await invoke('file_exists', { filePath })
9+
if (isExists) {
10+
const text = await invoke<string>('get_file_content', {
11+
filePath,
12+
})
13+
return text
14+
} else {
15+
return null
16+
}
17+
}

locales/en.json

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"file": {
6464
"untitled": "untitled",
6565
"openDir": "Open Folder",
66+
"openFile": "Open a File",
6667
"recentDir": "Recently opened folders",
6768
"emptyOpened": "No files opened"
6869
},

packages/theme/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@markflowy/theme",
3-
"version": "0.0.16",
3+
"version": "0.0.17",
44
"type": "module",
55
"license": "MIT",
66
"homepage": "",

packages/theme/src/theme/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const styledLightTheme = {
7676
accentColor: '#0369a1',
7777
borderColor: '#d7d7dc',
7878
bgColor: '#f7f7f7',
79+
bgColorSecondary: "#e7e7e7",
7980
hoverColor: '#d7d7dc',
8081
warnColor: '#e2b340',
8182
dangerColor: '#dc2626',
@@ -114,6 +115,7 @@ const styledDarkTheme = {
114115
accentColor: '#1c78aa',
115116
borderColor: '#363b41',
116117
bgColor: '#151515',
118+
bgColorSecondary: "#252525",
117119
hoverColor: '#2b2f33',
118120
warnColor: '#e2b340',
119121
dangerColor: '#dc2626',

0 commit comments

Comments
 (0)