diff --git a/src/cmd/pdf/export-pdf.ts b/src/cmd/pdf/export-pdf.ts index 7131ba17..b3be9a44 100644 --- a/src/cmd/pdf/export-pdf.ts +++ b/src/cmd/pdf/export-pdf.ts @@ -144,7 +144,7 @@ function handlePostInput(post: Post | PostTreeItem) { async function handleUriInput(uri: Uri) { const { fsPath } = uri - const postId = PostFileMapManager.getPostId(fsPath) + const postId = PostFileMapManager.getPostId(uri.path) if (postId === undefined) return [] const { post: inputPost } = await PostService.getPostEditDto(postId) diff --git a/src/cmd/post-list/copy-link.ts b/src/cmd/post-list/copy-link.ts index da631498..3845db45 100644 --- a/src/cmd/post-list/copy-link.ts +++ b/src/cmd/post-list/copy-link.ts @@ -35,7 +35,7 @@ export async function copyPostLink(input: Post | PostTreeItem | Uri) { post = input.post } else { // input instanceof Uri - const postId = PostFileMapManager.findByFilePath(input.fsPath)?.[0] + const postId = PostFileMapManager.findByFilePath(input.path)?.[0] if (postId === undefined || postId <= 0) { void Alert.fileNotLinkedToPost(input) return diff --git a/src/cmd/post-list/modify-post-setting.ts b/src/cmd/post-list/modify-post-setting.ts index d5f72b77..51a994b8 100644 --- a/src/cmd/post-list/modify-post-setting.ts +++ b/src/cmd/post-list/modify-post-setting.ts @@ -22,7 +22,7 @@ export async function modifyPostSetting(input: Post | PostTreeItem | Uri) { postId = input.id } else { //type of input is Uri - postId = PostFileMapManager.getPostId(input.fsPath) ?? -1 + postId = PostFileMapManager.getPostId(input.path) ?? -1 if (postId < 0) return Alert.fileNotLinkedToPost(input) } diff --git a/src/cmd/post-list/open-post-in-vscode.ts b/src/cmd/post-list/open-post-in-vscode.ts index 8aea7617..cc4b9ac7 100644 --- a/src/cmd/post-list/open-post-in-vscode.ts +++ b/src/cmd/post-list/open-post-in-vscode.ts @@ -52,7 +52,7 @@ export async function openPostInVscode(postId: number, forceUpdateLocalPostFile // 博文内容写入本地文件, 若文件不存在, 会自动创建对应的文件 await workspace.fs.writeFile(fileUri, Buffer.from(post.postBody)) - await PostFileMapManager.updateOrCreate(postId, fileUri.fsPath) + await PostFileMapManager.updateOrCreate(postId, fileUri.path) await openPostFile(post) return fileUri } diff --git a/src/cmd/post-list/post-pull-all.ts b/src/cmd/post-list/post-pull-all.ts index fb33785c..f88963ca 100644 --- a/src/cmd/post-list/post-pull-all.ts +++ b/src/cmd/post-list/post-pull-all.ts @@ -6,6 +6,7 @@ import { ProgressLocation, Uri, window, workspace } from 'vscode' import { buildLocalPostFileUri } from '@/cmd/post-list/open-post-in-vscode' import { UserService } from '@/service/user.service' import { fsUtil } from '@/infra/fs/fsUtil' +import { WorkspaceCfg } from '@/ctx/cfg/workspace' enum ConflictStrategy { ask, @@ -67,7 +68,11 @@ export async function postPullAll() { const path = PostFileMapManager.getFilePath(post.id) // 本地没有博文或关联到的文件不存在 - if (path === undefined || !(await fsUtil.exists(path))) { + if ( + path === undefined || + !(await fsUtil.exists(path)) || + path.indexOf(WorkspaceCfg.getWorkspaceUri().path) < 0 + ) { const uri = buildLocalPostFileUri(post, false) const buf = Buffer.from(post.postBody) await workspace.fs.writeFile(uri, buf) diff --git a/src/cmd/post-list/post-pull.ts b/src/cmd/post-list/post-pull.ts index 8b1833c4..b1d7e78c 100644 --- a/src/cmd/post-list/post-pull.ts +++ b/src/cmd/post-list/post-pull.ts @@ -30,6 +30,7 @@ export async function postPull(input: Post | PostTreeItem | Uri | undefined | nu await handlePostInput(input, ctxList, uri.path) } else { isFreshPull = !(await fsUtil.exists(path)) + if (!path.startsWith('/')) await PostFileMapManager.updateOrCreate(post.id, Uri.file(path).path) await handlePostInput(input, ctxList, path) } } else { @@ -82,7 +83,7 @@ function parseUriInput(input: InputType): Uri | undefined { } function handleUriInput(fileUri: Uri, contexts: CmdCtx[]) { - const postId = PostFileMapManager.getPostId(fileUri.fsPath) + const postId = PostFileMapManager.getPostId(fileUri.path) if (postId === undefined) return Alert.fileNotLinkedToPost(fileUri) contexts.push({ postId, fileUri }) diff --git a/src/cmd/post-list/rename-post.ts b/src/cmd/post-list/rename-post.ts index aaee51a3..e56f184d 100644 --- a/src/cmd/post-list/rename-post.ts +++ b/src/cmd/post-list/rename-post.ts @@ -29,7 +29,7 @@ async function renameLinkedFile(post: Post): Promise { const ext = path.extname(fileName) const newFilePath = filePath.replace(new RegExp(`${escapeRegExp(fileName)}$`), `${post.title}${ext}`) await workspace.fs.rename(fileUri, Uri.file(newFilePath)) - await PostFileMapManager.updateOrCreate(post.id, newFilePath) + await PostFileMapManager.updateOrCreate(post.id, fileUri.path) postDataProvider.fireTreeDataChangedEvent(post) } } diff --git a/src/cmd/post-list/upload-post.ts b/src/cmd/post-list/upload-post.ts index 8aef68e2..c3064ef6 100644 --- a/src/cmd/post-list/upload-post.ts +++ b/src/cmd/post-list/upload-post.ts @@ -205,7 +205,7 @@ export async function uploadPostFile(fileUri?: Uri, confirm = true) { if (parsedFileUri === undefined) return const { fsPath: filePath } = parsedFileUri - const postId = PostFileMapManager.getPostId(filePath) + const postId = PostFileMapManager.getPostId(parsedFileUri.path) if (postId !== undefined && postId >= 0) { const dto = await PostService.getPostEditDto(postId) @@ -232,7 +232,7 @@ export async function uploadPostFile(fileUri?: Uri, confirm = true) { ) if (selectedPost === undefined) return - await PostFileMapManager.updateOrCreate(selectedPost.id, filePath) + await PostFileMapManager.updateOrCreate(selectedPost.id, parsedFileUri.path) const postEditDto = await PostService.getPostEditDto(selectedPost.id) if (postEditDto === undefined) return if (fileContent === '') await workspace.fs.writeFile(parsedFileUri, Buffer.from(postEditDto.post.postBody)) diff --git a/src/cmd/show-local-file-to-post-info.ts b/src/cmd/show-local-file-to-post-info.ts index 3d25d6bd..7a5d19a1 100644 --- a/src/cmd/show-local-file-to-post-info.ts +++ b/src/cmd/show-local-file-to-post-info.ts @@ -18,7 +18,7 @@ export async function showLocalFileToPostInfo(input: Uri | number): Promise map[0] >= 0 && map[1] !== '' @@ -58,7 +59,9 @@ export namespace PostFileMapManager { export function findByFilePath(path: string) { const maps = getMaps().filter(validatePostFileMap) - return maps.find(x => x[0] !== 0 && x[1] === path) + let map = maps.find(x => x[0] !== 0 && x[1] === path) + if (map === undefined) map = maps.find(x => x[0] !== 0 && x[1] === Uri.parse(path).fsPath) + return map } export function getFilePath(postId: number) { diff --git a/src/setup/setup-watch.ts b/src/setup/setup-watch.ts index a4bdcee4..a2886b5b 100644 --- a/src/setup/setup-watch.ts +++ b/src/setup/setup-watch.ts @@ -29,7 +29,7 @@ export const setupWorkspaceFileWatch = () => workspace.onDidRenameFiles(e => { for (const item of e.files) { const { oldUri, newUri } = item - const postId = PostFileMapManager.getPostId(oldUri.fsPath) - if (postId !== undefined) void PostFileMapManager.updateOrCreate(postId, newUri.fsPath) + const postId = PostFileMapManager.getPostId(oldUri.path) + if (postId !== undefined) void PostFileMapManager.updateOrCreate(postId, newUri.path) } })