Skip to content

Commit 870ca27

Browse files
authored
Merge pull request #283 from cnblogs/refactor-creating-post
refactor: creating post
2 parents 57a64b6 + d876cb6 commit 870ca27

File tree

3 files changed

+21
-33
lines changed

3 files changed

+21
-33
lines changed

src/cmd/post-list/modify-post-setting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function modifyPostSetting(input: Post | PostTreeItem | Uri) {
3838
post: postEditDto,
3939
localFileUri: localFilePath !== undefined ? Uri.file(localFilePath) : undefined,
4040
afterSuccess: ({ id }) => {
41-
void Alert.info('博文已更新')
41+
void Alert.info('博文设置已更新')
4242
postDataProvider.fireTreeDataChangedEvent(id)
4343
postCategoryDataProvider.onPostUpdated({ refreshPost: false, postIds: [id] })
4444
},

src/cmd/post-list/upload-post.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ export async function saveLocalPost(localPost: LocalPost) {
5353
afterSuccess: async savedPost => {
5454
await PostListView.refresh()
5555
await openPostFile(localPost)
56-
57-
await PostFileMapManager.updateOrCreate(savedPost.id, localPost.filePath)
56+
const uriPath = Uri.file(localPost.filePath).path
57+
await PostFileMapManager.updateOrCreate(savedPost.id, uriPath)
5858
await openPostFile(localPost)
5959
postDataProvider.fireTreeDataChangedEvent()
6060
void Alert.info('博文已创建')

src/service/post/create.ts

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { homedir } from 'os'
2-
import path from 'path'
3-
import { Uri, window, workspace } from 'vscode'
4-
import { osOpenActiveFile } from '@/cmd/open/os-open-active-file'
2+
import { window } from 'vscode'
53
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
6-
import { saveLocalPost } from '@/cmd/post-list/upload-post'
74
import { openPostFile } from '@/cmd/post-list/open-post-file'
8-
import { LocalPost } from '@/service/local-post'
9-
import { fsUtil } from '@/infra/fs/fsUtil'
5+
import { Post, PostType } from '@/model/post'
6+
import { PostService } from './post'
7+
import { Alert } from '@/infra/alert'
8+
import { PostListView } from '@/cmd/post-list/post-list-view'
109

1110
export async function createPost() {
1211
const workspacePath = WorkspaceCfg.getWorkspaceUri().fsPath
@@ -23,29 +22,18 @@ export async function createPost() {
2322

2423
if (title == null) return
2524

26-
const filePath = path.join(workspacePath, `${title}.md`)
27-
28-
if (!(await fsUtil.exists(filePath)))
29-
await workspace.fs.writeFile(Uri.file(filePath), Buffer.from('# Hello World\n'))
30-
31-
await openPostFile(filePath)
32-
await osOpenActiveFile()
33-
// 设置中关闭了 `autoReveal` 的情况下, 需要两次调用 `workbench.files.action.showActiveFileInExplorer` 命令, 才能正确 `reveal`
34-
if (workspace.getConfiguration('explorer').get<boolean>('autoReveal') === false) await osOpenActiveFile()
35-
36-
const focusEditor = async () => {
37-
const editor = window.activeTextEditor
38-
if (editor !== undefined)
39-
await window.showTextDocument(editor.document, { preview: false, preserveFocus: false })
25+
const post = new Post()
26+
post.title = title
27+
post.postBody = '# Hello World\n'
28+
post.isMarkdown = true
29+
post.isDraft = true
30+
post.postType = PostType.blogPost
31+
const postId = (await PostService.update(post)).id
32+
if (postId > 0) {
33+
post.id = postId
34+
await PostListView.refresh()
35+
await openPostFile(post, undefined, true)
36+
} else {
37+
void Alert.err('创建博文失败,postId: ' + postId)
4038
}
41-
await focusEditor()
42-
// 确保能 focus 到编辑器(不这么做, 有时会聚焦到 explorer 处)
43-
await new Promise<void>(resolve => {
44-
const innerTimeout = setTimeout(() => {
45-
clearTimeout(innerTimeout)
46-
void focusEditor().finally(() => resolve())
47-
}, 50)
48-
})
49-
50-
await saveLocalPost(new LocalPost(filePath))
5139
}

0 commit comments

Comments
 (0)