@@ -3,16 +3,43 @@ import { execCmd } from '@/infra/cmd'
33import { Post } from '@/model/post'
44import { PostFileMapManager } from '@/service/post/post-file-map'
55import { LocalPost } from '@/service/local-post'
6+ import { fsUtil } from '@/infra/fs/fsUtil'
7+ import { postPull } from './post-pull'
8+ import { Alert } from '@/infra/alert'
69
7- export async function openPostFile ( post : LocalPost | Post | string , options ?: TextDocumentShowOptions ) {
10+ export async function openPostFile (
11+ post : LocalPost | Post | string ,
12+ options ?: TextDocumentShowOptions ,
13+ autoPull = false
14+ ) {
815 let filePath = ''
9- if ( post instanceof LocalPost ) filePath = post . filePath
10- else if ( post instanceof Post ) filePath = PostFileMapManager . getFilePath ( post . id ) ?? ''
11- else filePath = post
16+ if ( post instanceof LocalPost ) {
17+ filePath = post . filePath
18+ } else if ( post instanceof Post ) {
19+ filePath = PostFileMapManager . getFilePath ( post . id ) ?? ''
20+ if ( autoPull ) if ( ! ( await fsUtil . exists ( filePath ) ) ) await postPull ( post , false , true )
21+ } else {
22+ filePath = post
23+ }
1224
1325 if ( filePath === '' ) return
1426
15- await execCmd (
27+ if ( ! ( await fsUtil . exists ( filePath ) ) ) await new Promise ( f => setTimeout ( f , 200 ) )
28+
29+ try {
30+ await openFile ( filePath , options )
31+ } catch ( e ) {
32+ await new Promise ( f => setTimeout ( f , 500 ) )
33+ try {
34+ await openFile ( filePath , options )
35+ } catch ( e2 ) {
36+ await Alert . err ( `打开本地博文文件失败,重新操作通常可恢复,错误信息: ${ < string > e2 } ` )
37+ }
38+ }
39+ }
40+
41+ function openFile ( filePath : string , options ?: TextDocumentShowOptions ) {
42+ return execCmd (
1643 'vscode.open' ,
1744 Uri . file ( filePath ) ,
1845 Object . assign ( { preview : false } as TextDocumentShowOptions , options ?? { } )
0 commit comments