Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/cmd/post-list/open-post-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`打开本地博文文件失败,重新操作通常可恢复,错误信息: ${<string>e2}`)
}
}
}

function openFile(filePath: string, options?: TextDocumentShowOptions) {
return execCmd(
'vscode.open',
Uri.file(filePath),
Object.assign({ preview: false } as TextDocumentShowOptions, options ?? {})
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/post-list/post-pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(
'确认要拉取远程博文吗?',
{
Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions src/service/post/post-cfg-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down