1
1
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'
5
3
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
6
- import { saveLocalPost } from '@/cmd/post-list/upload-post'
7
4
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'
10
9
11
10
export async function createPost ( ) {
12
11
const workspacePath = WorkspaceCfg . getWorkspaceUri ( ) . fsPath
@@ -23,29 +22,18 @@ export async function createPost() {
23
22
24
23
if ( title == null ) return
25
24
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 )
40
38
}
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 ) )
51
39
}
0 commit comments