From cc3b3323c74dc3a162f9e5183607086431a3d0ab Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Sun, 27 Jul 2025 13:17:16 +0800 Subject: [PATCH] feat: add slug existence check when creating categories and tags (#7616) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /area ui /kind improvement /milestone 2.21.x #### What this PR does / why we need it: This PR adds frontend support for checking if an slug already exists when creating post categories and tags. image #### Which issue(s) this PR fixes: Fixes https://github.com/halo-dev/halo/issues/3172 #### Special notes for your reviewer: #### Does this PR introduce a user-facing change? ```release-note 创建文章分类和标签时支持检查别名是否已存在 ``` --- .../components/CategoryEditingModal.vue | 32 ++++++++++- .../posts/tags/components/TagEditingModal.vue | 53 +++++++++++++------ .../inputs/category-select/CategorySelect.vue | 17 +++++- .../formkit/inputs/tag-select/TagSelect.vue | 16 +++++- ui/src/locales/_missing_translations_es.yaml | 3 ++ ui/src/locales/en.yaml | 3 ++ ui/src/locales/zh-CN.yaml | 3 ++ ui/src/locales/zh-TW.yaml | 3 ++ 8 files changed, 109 insertions(+), 21 deletions(-) diff --git a/ui/console-src/modules/contents/posts/categories/components/CategoryEditingModal.vue b/ui/console-src/modules/contents/posts/categories/components/CategoryEditingModal.vue index 9144eda85..5c440b161 100644 --- a/ui/console-src/modules/contents/posts/categories/components/CategoryEditingModal.vue +++ b/ui/console-src/modules/contents/posts/categories/components/CategoryEditingModal.vue @@ -5,7 +5,7 @@ import { setFocus } from "@/formkit/utils/focus"; import { FormType } from "@/types/slug"; import useSlugify from "@console/composables/use-slugify"; import { useThemeCustomTemplates } from "@console/modules/interface/themes/composables/use-theme"; -import { reset, submitForm } from "@formkit/core"; +import { reset, submitForm, type FormKitNode } from "@formkit/core"; import type { Category } from "@halo-dev/api-client"; import { coreApiClient } from "@halo-dev/api-client"; import { @@ -181,6 +181,28 @@ const { handleGenerateSlug } = useSlugify( computed(() => !isUpdateMode), FormType.CATEGORY ); + +// fixme: check if slug is unique +// Finally, we need to check if the slug is unique in the database +async function slugUniqueValidation(node: FormKitNode) { + const value = node.value; + if (!value) { + return true; + } + + const fieldSelector = [`spec.slug=${value}`]; + + if (props.category) { + fieldSelector.push(`metadata.name!=${props.category.metadata.name}`); + } + + const { data: categoriesWithSameSlug } = + await coreApiClient.content.category.listCategory({ + fieldSelector, + }); + + return !categoriesWithSameSlug.total; +}