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
2 changes: 1 addition & 1 deletion src/cmd/pdf/export-pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/post-list/copy-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/post-list/modify-post-setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/post-list/open-post-in-vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/post-list/post-pull-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/post-list/post-pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/post-list/rename-post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function renameLinkedFile(post: Post): Promise<void> {
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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/post-list/upload-post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/show-local-file-to-post-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function showLocalFileToPostInfo(input: Uri | number): Promise<void
let filePath: string | undefined
let postId: number | undefined
if (input instanceof Uri && input.scheme === 'file') {
postId = PostFileMapManager.getPostId(input.fsPath)
postId = PostFileMapManager.getPostId(input.path)
filePath = input.fsPath
if (postId === undefined) {
const options = ['现在去关联']
Expand All @@ -36,7 +36,7 @@ export async function showLocalFileToPostInfo(input: Uri | number): Promise<void
'搜索要关联的博文'
)
if (selectedPost !== undefined) {
await PostFileMapManager.updateOrCreate(selectedPost.id, filePath)
await PostFileMapManager.updateOrCreate(selectedPost.id, input.path)
void Alert.info(`本地文件已与博文(${selectedPost.title}, Id: ${selectedPost.id})建立关联`)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/view-post-online.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function viewPostOnline(input?: Post | PostTreeItem | Uri) {
if (input === undefined) input = window.activeTextEditor?.document.uri

if (input instanceof Uri) {
const postId = PostFileMapManager.getPostId(input.fsPath)
const postId = PostFileMapManager.getPostId(input.path)
if (postId !== undefined) post = (await PostService.getPostEditDto(postId))?.post
}

Expand Down
5 changes: 4 additions & 1 deletion src/service/post/post-file-map.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { postCategoryDataProvider } from '@/tree-view/provider/post-category-tree-data-provider'
import { postDataProvider } from '@/tree-view/provider/post-data-provider'
import { LocalState } from '@/ctx/local-state'
import { Uri } from 'vscode'

const validatePostFileMap = (map: PostFileMap) => map[0] >= 0 && map[1] !== ''

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/setup/setup-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})