From 4ae5add4b94c45f811d0cba4d5c85e5a0ee49eeb Mon Sep 17 00:00:00 2001 From: dudu Date: Wed, 6 Dec 2023 13:58:37 +0800 Subject: [PATCH] feat: update post-file map for changed workspace --- src/ctx/cfg/workspace.ts | 3 +++ src/service/post/post-file-map.ts | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/ctx/cfg/workspace.ts b/src/ctx/cfg/workspace.ts index a2c8a8c2..73f0d088 100644 --- a/src/ctx/cfg/workspace.ts +++ b/src/ctx/cfg/workspace.ts @@ -3,6 +3,7 @@ import getPlatformCfg = PlatformCfg.getPlatformCfg import os from 'os' import { ConfigurationTarget, Uri, workspace } from 'vscode' import { Alert } from '@/infra/alert' +import { PostFileMapManager } from '@/service/post/post-file-map' export namespace WorkspaceCfg { export function getWorkspaceUri() { @@ -23,7 +24,9 @@ export namespace WorkspaceCfg { throw e } + const oldWorkspaceUri = WorkspaceCfg.getWorkspaceUri() const cfgTarget = ConfigurationTarget.Global await getPlatformCfg()?.update('workspace', fsPath, cfgTarget) + PostFileMapManager.updateWithWorkspace(oldWorkspaceUri) } } diff --git a/src/service/post/post-file-map.ts b/src/service/post/post-file-map.ts index 23dc22b0..b80f2964 100644 --- a/src/service/post/post-file-map.ts +++ b/src/service/post/post-file-map.ts @@ -2,6 +2,7 @@ import { postCategoryDataProvider } from '@/tree-view/provider/post-category-tre import { postDataProvider } from '@/tree-view/provider/post-data-provider' import { LocalState } from '@/ctx/local-state' import { Uri } from 'vscode' +import { WorkspaceCfg } from '@/ctx/cfg/workspace' const validatePostFileMap = (map: PostFileMap) => map[0] >= 0 && map[1] !== '' export type PostFileMap = [postId: number, filePath: string] @@ -11,6 +12,10 @@ function getMaps(): PostFileMap[] { return LocalState.getState(storageKey) ?? [] } +function isUriPath(path: string) { + return path.startsWith('/') +} + export namespace PostFileMapManager { export async function updateOrCreateMany( arg: @@ -67,7 +72,7 @@ export namespace PostFileMapManager { if (map === undefined) return const path = map[1] if (path === '') return - return path.startsWith('/') ? Uri.parse(path).fsPath : path + return isUriPath(path) ? Uri.parse(path).fsPath : path } export function getPostId(filePath: string): number | undefined { @@ -81,4 +86,16 @@ export namespace PostFileMapManager { if (match == null) return return Number(match[1]) } + + export function updateWithWorkspace(oldWorkspaceUri: Uri) { + const newWorkspaceUri = WorkspaceCfg.getWorkspaceUri() + if (newWorkspaceUri.path === oldWorkspaceUri.path) return + getMaps().forEach(x => { + const filePath = x[1] + if (isUriPath(filePath) && filePath.indexOf(oldWorkspaceUri.path) >= 0) + x[1] = filePath.replace(oldWorkspaceUri.path, newWorkspaceUri.path) + else if (!isUriPath(filePath) && filePath.indexOf(oldWorkspaceUri.fsPath) >= 0) + x[1] = filePath.replace(oldWorkspaceUri.fsPath, newWorkspaceUri.fsPath) + }) + } }