@@ -11,74 +11,86 @@ import { MarkdownCfg } from '@/ctx/cfg/markdown'
1111import { fsUtil } from '@/infra/fs/fsUtil'
1212import { searchPostByTitle } from '@/service/post/search-post-by-title'
1313
14- export async function postPull ( input : Post | PostTreeItem | Uri | undefined | null , showConfirm = true , mute = false ) {
15- const ctxList : CmdCtx [ ] = [ ]
16- let isFreshPull = false
17- input = input instanceof PostTreeItem ? input . post : input
18- if ( parsePostInput ( input ) && input . id > 0 ) {
19- const post = input
20- const path = PostFileMapManager . getFilePath ( post . id )
21- if ( path === undefined || ! ( await fsUtil . exists ( path ) ) ) {
22- isFreshPull = true
23- const uri = buildLocalPostFileUri ( post )
24- await workspace . fs . writeFile ( uri , Buffer . from ( post . postBody ) )
25- await PostFileMapManager . updateOrCreate ( post . id , uri . path )
26- await handlePostInput ( input , ctxList , uri . path )
27- } else {
28- isFreshPull = ! ( await fsUtil . exists ( path ) )
29- if ( ! path . startsWith ( '/' ) ) await PostFileMapManager . updateOrCreate ( post . id , Uri . file ( path ) . path )
30- await handlePostInput ( input , ctxList , path )
31- }
32- } else {
33- const uri = parseUriInput ( input )
34- if ( uri != null ) await handleUriInput ( uri , ctxList )
14+ type InputType = Post | PostTreeItem | Uri | number | undefined | null
15+
16+ async function getPostId ( input : InputType ) : Promise < number | undefined | null > {
17+ if ( typeof input === 'number' ) return input
18+ if ( input instanceof Post && input . id > 0 ) return input . id
19+ if ( input instanceof PostTreeItem && input . post . id > 0 ) return input . post . id
20+ if ( input instanceof Uri ) {
21+ const postId = await getPostIdFromUri ( input )
22+ return postId
3523 }
3624
37- const fileName = resolveFileNames ( ctxList )
38-
39- if ( showConfirm && ! isFreshPull && MarkdownCfg . isShowConfirmMsgWhenPullPost ( ) ) {
40- const answer = await Alert . warn (
41- '确认要拉取远程博文吗?' ,
42- {
43- modal : true ,
44- detail : `本地文件「${ fileName } 」将被覆盖(可通过设置关闭对话框)` ,
45- } ,
46- '确认'
47- )
48- if ( answer !== '确认' ) return
25+ const doc = window . activeTextEditor ?. document
26+ if ( doc != null && ! doc . isUntitled ) {
27+ const postId = await getPostIdFromUri ( doc . uri )
28+ return postId
4929 }
30+ }
5031
51- if ( ctxList . length <= 0 ) return
32+ export async function postPull ( input : InputType , showConfirm = true , mute = false ) : Promise < boolean > {
33+ let isFreshPull = false
34+ let post : Post | null = null
5235
53- await update ( ctxList )
36+ const postId = await getPostId ( input )
37+ if ( postId == null ) {
38+ void Alert . err ( `无效的额 postId,值为 ${ postId } ` )
39+ return false
40+ }
5441
55- if ( ! mute ) {
56- if ( isFreshPull ) await Alert . info ( `博文已下载至本地:${ resolveFileNames ( ctxList ) } ` )
57- else await Alert . info ( `本地文件已更新: ${ resolveFileNames ( ctxList ) } ` )
42+ post = ( await PostService . getPostEditDto ( postId ) ) ?. post
43+ if ( post == null ) {
44+ void Alert . err ( `对应的博文不存在,postId: ${ postId } ` )
45+ return false
5846 }
59- }
6047
61- type InputType = Post | Uri | undefined | null
62- type CmdCtx = {
63- postId : number
64- fileUri : Uri
65- }
48+ let uriPath = PostFileMapManager . getFilePath ( post . id )
49+ let fileUri : Uri
50+ if ( uriPath == null ) {
51+ fileUri = buildLocalPostFileUri ( post )
52+ } else {
53+ // replace fsPath with uriPath
54+ if ( ! uriPath . startsWith ( '/' ) ) uriPath = Uri . file ( uriPath ) . path
55+ if ( ! PostFileMapManager . isInWorkspace ( uriPath ) ) fileUri = buildLocalPostFileUri ( post )
56+ else fileUri = Uri . parse ( uriPath )
57+ }
6658
67- const parsePostInput = ( input : InputType ) : input is Post => input instanceof Post
59+ uriPath = fileUri . path
60+ const fsPath = fileUri . fsPath
61+ const fileName = path . basename ( fsPath )
62+ // eslint-disable-next-line @typescript-eslint/naming-convention
63+ const fileExists = await fsUtil . exists ( fsPath )
64+
65+ if ( fileExists ) {
66+ if ( showConfirm && ! isFreshPull && MarkdownCfg . isShowConfirmMsgWhenPullPost ( ) ) {
67+ const answer = await Alert . warn (
68+ '确认要拉取远程博文吗?' ,
69+ {
70+ modal : true ,
71+ detail : `本地文件「${ fileName } 」将被覆盖(可通过设置关闭对话框)` ,
72+ } ,
73+ '确认'
74+ )
75+ if ( answer !== '确认' ) return false
76+ }
77+ } else {
78+ isFreshPull = true
79+ }
6880
69- async function handlePostInput ( post : Post , contexts : CmdCtx [ ] , path : string ) {
81+ await workspace . fs . writeFile ( fileUri , Buffer . from ( post . postBody ) )
82+ await PostFileMapManager . updateOrCreate ( post . id , uriPath )
7083 await revealPostListItem ( post )
71- contexts . push ( { postId : post . id , fileUri : Uri . file ( path ) } )
72- }
7384
74- function parseUriInput ( input : InputType ) : Uri | undefined {
75- if ( input instanceof Uri ) return input
85+ if ( ! mute ) {
86+ if ( isFreshPull ) await Alert . info ( `博文已下载至本地:${ fileName } ` )
87+ else await Alert . info ( `本地文件已更新: ${ fileName } ` )
88+ }
7689
77- const doc = window . activeTextEditor ?. document
78- if ( doc !== undefined && ! doc . isUntitled ) return doc . uri
90+ return true
7991}
8092
81- async function handleUriInput ( fileUri : Uri , contexts : CmdCtx [ ] ) {
93+ async function getPostIdFromUri ( fileUri : Uri ) : Promise < number | undefined > {
8294 let postId = PostFileMapManager . getPostId ( fileUri . path )
8395 if ( postId == null ) {
8496 const mapPost = '关联已有博文并拉取'
@@ -97,32 +109,18 @@ async function handleUriInput(fileUri: Uri, contexts: CmdCtx[]) {
97109 postId = PostFileMapManager . extractPostId ( filenName )
98110 if ( postId == null ) {
99111 const selectedPost = await searchPostByTitle ( filenName , '搜索要关联的博文' )
100- if ( selectedPost == null ) return Alert . info ( '未选择要关联的博文' )
112+ if ( selectedPost == null ) {
113+ void Alert . info ( '未选择要关联的博文' )
114+ return
115+ }
101116 postId = selectedPost . id
102117 }
103118 }
104119
105120 if ( postId != null ) await PostFileMapManager . updateOrCreate ( postId , fileUri . path )
106121 }
107122
108- if ( postId == null ) return Alert . fileNotLinkedToPost ( fileUri )
109-
110- contexts . push ( { postId, fileUri } )
111- }
112-
113- async function update ( contexts : CmdCtx [ ] ) {
114- for ( const ctx of contexts ) {
115- const { fileUri, postId } = ctx
116-
117- const { post } = await PostService . getPostEditDto ( postId )
118-
119- const textEditors = window . visibleTextEditors . filter ( x => x . document . uri . fsPath === fileUri . fsPath )
120- await Promise . all ( textEditors . map ( editor => editor . document . save ( ) ) )
121- await workspace . fs . writeFile ( fileUri , Buffer . from ( post . postBody ) )
122- }
123- }
123+ if ( postId == null ) Alert . fileNotLinkedToPost ( fileUri )
124124
125- function resolveFileNames ( ctxList : CmdCtx [ ] ) {
126- const arr = ctxList . map ( x => path . basename ( x . fileUri . fsPath ) )
127- return `${ arr . join ( ', ' ) } `
125+ return postId
128126}
0 commit comments