From bed66d6187d5e2d76c21909b7b8713486e4a97af Mon Sep 17 00:00:00 2001 From: dudu Date: Wed, 23 Jul 2025 18:39:04 +0800 Subject: [PATCH] fix: flatten tree categories --- src/model/post-cat.ts | 1 + src/service/post/post-cat.ts | 15 +++++++++++++++ src/service/post/post-cfg-panel.ts | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/model/post-cat.ts b/src/model/post-cat.ts index 853667a6..dd4ae1b4 100644 --- a/src/model/post-cat.ts +++ b/src/model/post-cat.ts @@ -10,6 +10,7 @@ export class PostCat { childCount = 0 visibleChildCount = 0 parent?: PostCat | null + children?: PostCat | null flattenParents(includeSelf: boolean): PostCat[] { // eslint-disable-next-line @typescript-eslint/no-this-alias diff --git a/src/service/post/post-cat.ts b/src/service/post/post-cat.ts index 6c1b1918..0fb32245 100644 --- a/src/service/post/post-cat.ts +++ b/src/service/post/post-cat.ts @@ -28,6 +28,21 @@ export namespace PostCatService { } } + export async function getFlatAll() { + const categories = await getAll() + + const flat = [] + const queue = categories + while (queue.length > 0) { + const current = queue.pop() + flat.push(current) + + if (current?.children != null) for (const child of current.children) queue.unshift(child) + } + + return flat + } + export async function getOne(categoryId: number) { const req = await getAuthedPostCatReq() try { diff --git a/src/service/post/post-cfg-panel.ts b/src/service/post/post-cfg-panel.ts index a2b0f38e..20329463 100644 --- a/src/service/post/post-cfg-panel.ts +++ b/src/service/post/post-cfg-panel.ts @@ -76,7 +76,7 @@ export namespace PostCfgPanel { command: Webview.Cmd.Ui.editPostCfg, post: cloneDeep(post), activeTheme: vscode.window.activeColorTheme.kind, - userCats: cloneDeep(await PostCatService.getAll()), + userCats: cloneDeep(await PostCatService.getFlatAll()), siteCats: cloneDeep(await PostCatService.getSitePresetList()), tags, breadcrumbs,