From 2914f1f8843b9eebd287169bba838a6c771ef627 Mon Sep 17 00:00:00 2001 From: dudu Date: Sat, 16 Sep 2023 14:27:05 +0800 Subject: [PATCH] feat: pull post when no local file --- src/cmd/post-list/open-post-file.ts | 37 +++++++++++++++++++++++++---- src/cmd/post-list/post-pull.ts | 6 ++--- src/service/post/post-cfg-panel.ts | 10 +++++--- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/cmd/post-list/open-post-file.ts b/src/cmd/post-list/open-post-file.ts index d9c76699..559ce5ad 100644 --- a/src/cmd/post-list/open-post-file.ts +++ b/src/cmd/post-list/open-post-file.ts @@ -3,16 +3,43 @@ import { execCmd } from '@/infra/cmd' import { Post } from '@/model/post' import { PostFileMapManager } from '@/service/post/post-file-map' import { LocalPost } from '@/service/local-post' +import { fsUtil } from '@/infra/fs/fsUtil' +import { postPull } from './post-pull' +import { Alert } from '@/infra/alert' -export async function openPostFile(post: LocalPost | Post | string, options?: TextDocumentShowOptions) { +export async function openPostFile( + post: LocalPost | Post | string, + options?: TextDocumentShowOptions, + autoPull = false +) { let filePath = '' - if (post instanceof LocalPost) filePath = post.filePath - else if (post instanceof Post) filePath = PostFileMapManager.getFilePath(post.id) ?? '' - else filePath = post + if (post instanceof LocalPost) { + filePath = post.filePath + } else if (post instanceof Post) { + filePath = PostFileMapManager.getFilePath(post.id) ?? '' + if (autoPull) if (!(await fsUtil.exists(filePath))) await postPull(post, false, true) + } else { + filePath = post + } if (filePath === '') return - await execCmd( + if (!(await fsUtil.exists(filePath))) await new Promise(f => setTimeout(f, 200)) + + try { + await openFile(filePath, options) + } catch (e) { + await new Promise(f => setTimeout(f, 500)) + try { + await openFile(filePath, options) + } catch (e2) { + await Alert.err(`打开本地博文文件失败,重新操作通常可恢复,错误信息: ${e2}`) + } + } +} + +function openFile(filePath: string, options?: TextDocumentShowOptions) { + return execCmd( 'vscode.open', Uri.file(filePath), Object.assign({ preview: false } as TextDocumentShowOptions, options ?? {}) diff --git a/src/cmd/post-list/post-pull.ts b/src/cmd/post-list/post-pull.ts index afeb9e4d..2efc3fd4 100644 --- a/src/cmd/post-list/post-pull.ts +++ b/src/cmd/post-list/post-pull.ts @@ -10,7 +10,7 @@ import { PostTreeItem } from '@/tree-view/model/post-tree-item' import { MarkdownCfg } from '@/ctx/cfg/markdown' import { fsUtil } from '@/infra/fs/fsUtil' -export async function postPull(input: Post | PostTreeItem | Uri | undefined | null) { +export async function postPull(input: Post | PostTreeItem | Uri | undefined | null, showConfirm = true, mute = false) { const ctxList: CmdCtx[] = [] input = input instanceof PostTreeItem ? input.post : input if (parsePostInput(input) && input.id > 0) { @@ -22,7 +22,7 @@ export async function postPull(input: Post | PostTreeItem | Uri | undefined | nu const fileName = resolveFileNames(ctxList) - if (MarkdownCfg.isShowConfirmMsgWhenPullPost()) { + if (showConfirm && MarkdownCfg.isShowConfirmMsgWhenPullPost()) { const answer = await Alert.warn( '确认要拉取远程博文吗?', { @@ -38,7 +38,7 @@ export async function postPull(input: Post | PostTreeItem | Uri | undefined | nu await update(ctxList) - void Alert.info(`本地文件 ${resolveFileNames(ctxList)} 已更新`) + if (!mute) void Alert.info(`本地文件 ${resolveFileNames(ctxList)} 已更新`) } type InputType = Post | Uri | undefined | null diff --git a/src/service/post/post-cfg-panel.ts b/src/service/post/post-cfg-panel.ts index 9907e4a7..344bee6e 100644 --- a/src/service/post/post-cfg-panel.ts +++ b/src/service/post/post-cfg-panel.ts @@ -40,9 +40,13 @@ export namespace PostCfgPanel { export async function open(option: PostCfgPanelOpenOption) { const { post, breadcrumbs, localFileUri } = option const panelTitle = option.panelTitle !== undefined ? option.panelTitle : `博文设置 - ${post.title}` - await openPostFile(post, { - viewColumn: vscode.ViewColumn.One, - }) + await openPostFile( + post, + { + viewColumn: vscode.ViewColumn.One, + }, + true + ) let panel = findPanelById(`${post.id}-${post.title}`) if (panel !== undefined) {