Skip to content

Commit 8a02f50

Browse files
authored
Merge pull request #205 from cnblogs/pull-post-when-no-local-file
feat: pull post when no local file
2 parents aa6fe8e + 2914f1f commit 8a02f50

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

src/cmd/post-list/open-post-file.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,43 @@ import { execCmd } from '@/infra/cmd'
33
import { Post } from '@/model/post'
44
import { PostFileMapManager } from '@/service/post/post-file-map'
55
import { 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 ?? {})

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { PostTreeItem } from '@/tree-view/model/post-tree-item'
1010
import { MarkdownCfg } from '@/ctx/cfg/markdown'
1111
import { fsUtil } from '@/infra/fs/fsUtil'
1212

13-
export async function postPull(input: Post | PostTreeItem | Uri | undefined | null) {
13+
export async function postPull(input: Post | PostTreeItem | Uri | undefined | null, showConfirm = true, mute = false) {
1414
const ctxList: CmdCtx[] = []
1515
input = input instanceof PostTreeItem ? input.post : input
1616
if (parsePostInput(input) && input.id > 0) {
@@ -22,7 +22,7 @@ export async function postPull(input: Post | PostTreeItem | Uri | undefined | nu
2222

2323
const fileName = resolveFileNames(ctxList)
2424

25-
if (MarkdownCfg.isShowConfirmMsgWhenPullPost()) {
25+
if (showConfirm && MarkdownCfg.isShowConfirmMsgWhenPullPost()) {
2626
const answer = await Alert.warn(
2727
'确认要拉取远程博文吗?',
2828
{
@@ -38,7 +38,7 @@ export async function postPull(input: Post | PostTreeItem | Uri | undefined | nu
3838

3939
await update(ctxList)
4040

41-
void Alert.info(`本地文件 ${resolveFileNames(ctxList)} 已更新`)
41+
if (!mute) void Alert.info(`本地文件 ${resolveFileNames(ctxList)} 已更新`)
4242
}
4343

4444
type InputType = Post | Uri | undefined | null

src/service/post/post-cfg-panel.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ export namespace PostCfgPanel {
4040
export async function open(option: PostCfgPanelOpenOption) {
4141
const { post, breadcrumbs, localFileUri } = option
4242
const panelTitle = option.panelTitle !== undefined ? option.panelTitle : `博文设置 - ${post.title}`
43-
await openPostFile(post, {
44-
viewColumn: vscode.ViewColumn.One,
45-
})
43+
await openPostFile(
44+
post,
45+
{
46+
viewColumn: vscode.ViewColumn.One,
47+
},
48+
true
49+
)
4650

4751
let panel = findPanelById(`${post.id}-${post.title}`)
4852
if (panel !== undefined) {

0 commit comments

Comments
 (0)