diff --git a/package.json b/package.json index 9fb4c791..13469b9c 100644 --- a/package.json +++ b/package.json @@ -487,13 +487,6 @@ "editPresentation": "singlelineText", "markdownDescription": "macOS 上 Chromium 可执行文件路径, 用于进行 PDF 导出等操作" }, - "cnblogsClient.createLocalPostFileWithCategory": { - "order": 5, - "default": true, - "scope": "application", - "type": "boolean", - "markdownDescription": "创建本地博文时, 是否根据博文分类保存到对应的文件夹中" - }, "cnblogsClient.pageSize.postList": { "order": 6, "default": 30, diff --git a/src/cmd/post-cat/update-post-cat-treeview.ts b/src/cmd/post-cat/update-post-cat-treeview.ts index f2a78a01..a8bce650 100644 --- a/src/cmd/post-cat/update-post-cat-treeview.ts +++ b/src/cmd/post-cat/update-post-cat-treeview.ts @@ -1,14 +1,11 @@ -import { ProgressLocation, window, Uri, workspace } from 'vscode' +import { ProgressLocation, window } from 'vscode' import { PostCat } from '@/model/post-cat' import { PostCatService } from '@/service/post/post-cat' import { inputPostCat } from './input-post-cat' -import { PostCatCfg } from '@/ctx/cfg/post-cat' -import { WorkspaceCfg } from '@/ctx/cfg/workspace' import { Alert } from '@/infra/alert' import { PostCatTreeItem } from '@/tree-view/model/post-category-tree-item' import { extTreeViews } from '@/tree-view/tree-view-register' import { postCategoryDataProvider } from '@/tree-view/provider/post-category-tree-data-provider' -import { fsUtil } from '@/infra/fs/fsUtil' export async function updatePostCatTreeView(arg?: PostCat | PostCatTreeItem) { let category: PostCat @@ -36,16 +33,6 @@ export async function updatePostCatTreeView(arg?: PostCat | PostCatTreeItem) { try { await PostCatService.update(updateDto) postCategoryDataProvider.refresh() - // 如果选择了createLocalPostFileWithCategory模式且本地有该目录,则重命名该目录 - const workspaceUri = WorkspaceCfg.getWorkspaceUri() - const shouldCreateLocalPostFileWithCategory = PostCatCfg.isCreateLocalPostFileWithCategory() - const path = Uri.joinPath(workspaceUri, category.title).fsPath - const isFileExist = await fsUtil.exists(path) - if (shouldCreateLocalPostFileWithCategory && isFileExist) { - const oldUri = Uri.joinPath(workspaceUri, category.title) - const newUri = Uri.joinPath(workspaceUri, addDto.title) - await workspace.fs.rename(oldUri, newUri) - } p.report({ increment: 100 }) } catch (e) { void Alert.err(`更新博文失败: ${e}`) diff --git a/src/cmd/post-list/open-post-file.ts b/src/cmd/post-list/open-post-file.ts index 559ce5ad..5cc3b972 100644 --- a/src/cmd/post-list/open-post-file.ts +++ b/src/cmd/post-list/open-post-file.ts @@ -6,6 +6,7 @@ import { LocalPost } from '@/service/local-post' import { fsUtil } from '@/infra/fs/fsUtil' import { postPull } from './post-pull' import { Alert } from '@/infra/alert' +import { WorkspaceCfg } from '@/ctx/cfg/workspace' export async function openPostFile( post: LocalPost | Post | string, @@ -17,7 +18,10 @@ export async function openPostFile( 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) + if (autoPull) { + if (!(await fsUtil.exists(filePath)) || filePath.indexOf(WorkspaceCfg.getWorkspaceUri().path) < 0) + await postPull(post, false, true) + } } else { filePath = post } diff --git a/src/cmd/post-list/open-post-in-vscode.ts b/src/cmd/post-list/open-post-in-vscode.ts index d40149f8..8aea7617 100644 --- a/src/cmd/post-list/open-post-in-vscode.ts +++ b/src/cmd/post-list/open-post-in-vscode.ts @@ -5,35 +5,17 @@ import { Alert } from '@/infra/alert' import { PostService } from '@/service/post/post' import { PostFileMapManager } from '@/service/post/post-file-map' import { openPostFile } from './open-post-file' -import { PostCatService } from '@/service/post/post-cat' import sanitizeFileName from 'sanitize-filename' import { WorkspaceCfg } from '@/ctx/cfg/workspace' -import { PostCatCfg } from '@/ctx/cfg/post-cat' import { fsUtil } from '@/infra/fs/fsUtil' -export async function buildLocalPostFileUri(post: Post, includePostId = false): Promise { +export function buildLocalPostFileUri(post: Post, includePostId = false): Uri { const workspaceUri = WorkspaceCfg.getWorkspaceUri() - const shouldCreateLocalPostFileWithCategory = PostCatCfg.isCreateLocalPostFileWithCategory() const ext = `.${post.isMarkdown ? 'md' : 'html'}` const postIdSegment = includePostId ? `.${post.id}` : '' const postTitle = sanitizeFileName(post.title) - if (!shouldCreateLocalPostFileWithCategory) return Uri.joinPath(workspaceUri, `${postTitle}${postIdSegment}${ext}`) - - const firstCategoryId = post.categoryIds?.[0] ?? null - let i = firstCategoryId !== null ? await PostCatService.getOne(firstCategoryId) : null - let categoryTitle = '' - while (i != null) { - categoryTitle = path.join( - sanitizeFileName(i.title, { - replacement: invalidChar => (invalidChar === '/' ? '_' : ''), - }), - categoryTitle - ) - i = i.parent ?? null - } - - return Uri.joinPath(workspaceUri, categoryTitle, `${postTitle}${postIdSegment}${ext}`) + return Uri.joinPath(workspaceUri, `${postTitle}${postIdSegment}${ext}`) } export async function openPostInVscode(postId: number, forceUpdateLocalPostFile = false): Promise { @@ -53,7 +35,7 @@ export async function openPostInVscode(postId: number, forceUpdateLocalPostFile const workspaceUri = WorkspaceCfg.getWorkspaceUri() await mkDirIfNotExist(workspaceUri) - let fileUri = mappedPostFilePath !== undefined ? Uri.file(mappedPostFilePath) : await buildLocalPostFileUri(post) + let fileUri = mappedPostFilePath !== undefined ? Uri.file(mappedPostFilePath) : buildLocalPostFileUri(post) // 博文尚未关联到本地文件的情况 // 本地存在和博文同名的文件, 询问用户是要覆盖还是同时保留两者 @@ -65,7 +47,7 @@ export async function openPostInVscode(postId: number, forceUpdateLocalPostFile ...opt ) - if (selected === opt[0]) fileUri = await buildLocalPostFileUri(post, true) + if (selected === opt[0]) fileUri = buildLocalPostFileUri(post, true) } // 博文内容写入本地文件, 若文件不存在, 会自动创建对应的文件 diff --git a/src/cmd/post-list/post-pull-all.ts b/src/cmd/post-list/post-pull-all.ts index 0b2c04a8..fb33785c 100644 --- a/src/cmd/post-list/post-pull-all.ts +++ b/src/cmd/post-list/post-pull-all.ts @@ -68,7 +68,7 @@ export async function postPullAll() { // 本地没有博文或关联到的文件不存在 if (path === undefined || !(await fsUtil.exists(path))) { - const uri = await buildLocalPostFileUri(post, false) + const uri = buildLocalPostFileUri(post, false) const buf = Buffer.from(post.postBody) await workspace.fs.writeFile(uri, buf) await PostFileMapManager.updateOrCreate(post.id, uri.path) diff --git a/src/cmd/post-list/post-pull.ts b/src/cmd/post-list/post-pull.ts index 79a10fc4..8b1833c4 100644 --- a/src/cmd/post-list/post-pull.ts +++ b/src/cmd/post-list/post-pull.ts @@ -24,7 +24,7 @@ export async function postPull(input: Post | PostTreeItem | Uri | undefined | nu path.indexOf(WorkspaceCfg.getWorkspaceUri().path) < 0 ) { isFreshPull = true - const uri = await buildLocalPostFileUri(post, false) + const uri = buildLocalPostFileUri(post, false) await workspace.fs.writeFile(uri, Buffer.from(post.postBody)) await PostFileMapManager.updateOrCreate(post.id, uri.path) await handlePostInput(input, ctxList, uri.path) diff --git a/src/ctx/cfg/post-cat.ts b/src/ctx/cfg/post-cat.ts deleted file mode 100644 index 2dde22ae..00000000 --- a/src/ctx/cfg/post-cat.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { LocalState } from '@/ctx/local-state' -import { ConfigurationTarget } from 'vscode' - -export namespace PostCatCfg { - export function isCreateLocalPostFileWithCategory(): boolean { - return LocalState.getExtCfg().get('createLocalPostFileWithCategory') ?? false - } - - export function setCreateLocalPostFileWithCategory(val: boolean) { - const cfgTarget = ConfigurationTarget.Global - return LocalState.getExtCfg().update('createLocalPostFileWithCategory', val, cfgTarget) - } -}