mirror of https://github.com/halo-dev/halo
feat: add slug existence check when creating categories and tags (#7616)
#### 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. <img width="701" alt="image" src="https://github.com/user-attachments/assets/050c2fc3-b82c-42f1-b58e-cf12c6852959" /> #### 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 创建文章分类和标签时支持检查别名是否已存在 ```pull/7642/head
parent
395399f078
commit
cc3b3323c7
|
@ -5,7 +5,7 @@ import { setFocus } from "@/formkit/utils/focus";
|
||||||
import { FormType } from "@/types/slug";
|
import { FormType } from "@/types/slug";
|
||||||
import useSlugify from "@console/composables/use-slugify";
|
import useSlugify from "@console/composables/use-slugify";
|
||||||
import { useThemeCustomTemplates } from "@console/modules/interface/themes/composables/use-theme";
|
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 type { Category } from "@halo-dev/api-client";
|
||||||
import { coreApiClient } from "@halo-dev/api-client";
|
import { coreApiClient } from "@halo-dev/api-client";
|
||||||
import {
|
import {
|
||||||
|
@ -181,6 +181,28 @@ const { handleGenerateSlug } = useSlugify(
|
||||||
computed(() => !isUpdateMode),
|
computed(() => !isUpdateMode),
|
||||||
FormType.CATEGORY
|
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;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<VModal
|
<VModal
|
||||||
|
@ -231,7 +253,13 @@ const { handleGenerateSlug } = useSlugify(
|
||||||
name="slug"
|
name="slug"
|
||||||
:label="$t('core.post_category.editing_modal.fields.slug.label')"
|
:label="$t('core.post_category.editing_modal.fields.slug.label')"
|
||||||
type="text"
|
type="text"
|
||||||
validation="required|length:0,50"
|
validation="required|length:0,50|slugUniqueValidation"
|
||||||
|
:validation-rules="{ slugUniqueValidation }"
|
||||||
|
:validation-messages="{
|
||||||
|
slugUniqueValidation: $t(
|
||||||
|
'core.common.form.validation.slug_unique'
|
||||||
|
),
|
||||||
|
}"
|
||||||
>
|
>
|
||||||
<template #suffix>
|
<template #suffix>
|
||||||
<div
|
<div
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
// core libs
|
|
||||||
import { coreApiClient } from "@halo-dev/api-client";
|
|
||||||
import { computed, nextTick, ref, watch } from "vue";
|
|
||||||
|
|
||||||
// components
|
|
||||||
import SubmitButton from "@/components/button/SubmitButton.vue";
|
import SubmitButton from "@/components/button/SubmitButton.vue";
|
||||||
|
import AnnotationsForm from "@/components/form/AnnotationsForm.vue";
|
||||||
|
import { setFocus } from "@/formkit/utils/focus";
|
||||||
|
import { FormType } from "@/types/slug";
|
||||||
|
import useSlugify from "@console/composables/use-slugify";
|
||||||
|
import { reset, submitForm, type FormKitNode } from "@formkit/core";
|
||||||
|
import type { Tag } from "@halo-dev/api-client";
|
||||||
|
import { coreApiClient } from "@halo-dev/api-client";
|
||||||
import {
|
import {
|
||||||
IconArrowLeft,
|
IconArrowLeft,
|
||||||
IconArrowRight,
|
IconArrowRight,
|
||||||
|
@ -14,18 +16,8 @@ import {
|
||||||
VModal,
|
VModal,
|
||||||
VSpace,
|
VSpace,
|
||||||
} from "@halo-dev/components";
|
} from "@halo-dev/components";
|
||||||
|
|
||||||
// types
|
|
||||||
import type { Tag } from "@halo-dev/api-client";
|
|
||||||
|
|
||||||
// libs
|
|
||||||
import AnnotationsForm from "@/components/form/AnnotationsForm.vue";
|
|
||||||
import { setFocus } from "@/formkit/utils/focus";
|
|
||||||
import { FormType } from "@/types/slug";
|
|
||||||
import useSlugify from "@console/composables/use-slugify";
|
|
||||||
import { reset, submitForm } from "@formkit/core";
|
|
||||||
import { cloneDeep } from "lodash-es";
|
import { cloneDeep } from "lodash-es";
|
||||||
import { onMounted } from "vue";
|
import { computed, nextTick, onMounted, ref, watch } from "vue";
|
||||||
import { useI18n } from "vue-i18n";
|
import { useI18n } from "vue-i18n";
|
||||||
|
|
||||||
const props = withDefaults(
|
const props = withDefaults(
|
||||||
|
@ -153,6 +145,27 @@ const { handleGenerateSlug } = useSlugify(
|
||||||
computed(() => !isUpdateMode.value),
|
computed(() => !isUpdateMode.value),
|
||||||
FormType.TAG
|
FormType.TAG
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 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.tag) {
|
||||||
|
fieldSelector.push(`metadata.name!=${props.tag.metadata.name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data: tagsWithSameSlug } = await coreApiClient.content.tag.listTag({
|
||||||
|
fieldSelector,
|
||||||
|
});
|
||||||
|
|
||||||
|
return !tagsWithSameSlug.total;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<VModal ref="modal" :title="modalTitle" :width="700" @close="emit('close')">
|
<VModal ref="modal" :title="modalTitle" :width="700" @close="emit('close')">
|
||||||
|
@ -198,7 +211,13 @@ const { handleGenerateSlug } = useSlugify(
|
||||||
:label="$t('core.post_tag.editing_modal.fields.slug.label')"
|
:label="$t('core.post_tag.editing_modal.fields.slug.label')"
|
||||||
name="slug"
|
name="slug"
|
||||||
type="text"
|
type="text"
|
||||||
validation="required|length:0,50"
|
validation="required|length:0,50|slugUniqueValidation"
|
||||||
|
:validation-rules="{ slugUniqueValidation }"
|
||||||
|
:validation-messages="{
|
||||||
|
slugUniqueValidation: $t(
|
||||||
|
'core.common.form.validation.slug_unique'
|
||||||
|
),
|
||||||
|
}"
|
||||||
>
|
>
|
||||||
<template #suffix>
|
<template #suffix>
|
||||||
<div
|
<div
|
||||||
|
|
|
@ -12,6 +12,7 @@ import { coreApiClient } from "@halo-dev/api-client";
|
||||||
import { IconArrowRight } from "@halo-dev/components";
|
import { IconArrowRight } from "@halo-dev/components";
|
||||||
import { onClickOutside } from "@vueuse/core";
|
import { onClickOutside } from "@vueuse/core";
|
||||||
import Fuse from "fuse.js";
|
import Fuse from "fuse.js";
|
||||||
|
import ShortUniqueId from "short-unique-id";
|
||||||
import { slugify } from "transliteration";
|
import { slugify } from "transliteration";
|
||||||
import { computed, provide, ref, watch, type PropType, type Ref } from "vue";
|
import { computed, provide, ref, watch, type PropType, type Ref } from "vue";
|
||||||
import CategoryListItem from "./components/CategoryListItem.vue";
|
import CategoryListItem from "./components/CategoryListItem.vue";
|
||||||
|
@ -212,16 +213,30 @@ const scrollToSelected = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const uid = new ShortUniqueId();
|
||||||
|
|
||||||
const handleCreateCategory = async () => {
|
const handleCreateCategory = async () => {
|
||||||
if (!currentUserHasPermission(["system:posts:manage"])) {
|
if (!currentUserHasPermission(["system:posts:manage"])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let slug = slugify(text.value, { trim: true });
|
||||||
|
|
||||||
|
// Check if slug is unique, if not, add -1 to the slug
|
||||||
|
const { data: categoriesWithSameSlug } =
|
||||||
|
await coreApiClient.content.category.listCategory({
|
||||||
|
fieldSelector: [`spec.slug=${slug}`],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (categoriesWithSameSlug.total) {
|
||||||
|
slug = `${slug}-${uid.randomUUID(8)}`;
|
||||||
|
}
|
||||||
|
|
||||||
const { data } = await coreApiClient.content.category.createCategory({
|
const { data } = await coreApiClient.content.category.createCategory({
|
||||||
category: {
|
category: {
|
||||||
spec: {
|
spec: {
|
||||||
displayName: text.value,
|
displayName: text.value,
|
||||||
slug: slugify(text.value, { trim: true }),
|
slug,
|
||||||
description: "",
|
description: "",
|
||||||
cover: "",
|
cover: "",
|
||||||
template: "",
|
template: "",
|
||||||
|
|
|
@ -13,6 +13,7 @@ import {
|
||||||
} from "@halo-dev/components";
|
} from "@halo-dev/components";
|
||||||
import { onClickOutside } from "@vueuse/core";
|
import { onClickOutside } from "@vueuse/core";
|
||||||
import Fuse from "fuse.js";
|
import Fuse from "fuse.js";
|
||||||
|
import ShortUniqueId from "short-unique-id";
|
||||||
import { slugify } from "transliteration";
|
import { slugify } from "transliteration";
|
||||||
import { computed, ref, watch, type PropType } from "vue";
|
import { computed, ref, watch, type PropType } from "vue";
|
||||||
|
|
||||||
|
@ -191,16 +192,29 @@ const scrollToSelected = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const uid = new ShortUniqueId();
|
||||||
|
|
||||||
const handleCreateTag = async () => {
|
const handleCreateTag = async () => {
|
||||||
if (!currentUserHasPermission(["system:posts:manage"])) {
|
if (!currentUserHasPermission(["system:posts:manage"])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let slug = slugify(text.value, { trim: true });
|
||||||
|
|
||||||
|
// Check if slug is unique, if not, add -1 to the slug
|
||||||
|
const { data: tagsWithSameSlug } = await coreApiClient.content.tag.listTag({
|
||||||
|
fieldSelector: [`spec.slug=${slug}`],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (tagsWithSameSlug.total) {
|
||||||
|
slug = `${slug}-${uid.randomUUID(8)}`;
|
||||||
|
}
|
||||||
|
|
||||||
const { data } = await coreApiClient.content.tag.createTag({
|
const { data } = await coreApiClient.content.tag.createTag({
|
||||||
tag: {
|
tag: {
|
||||||
spec: {
|
spec: {
|
||||||
displayName: text.value,
|
displayName: text.value,
|
||||||
slug: slugify(text.value, { trim: true }),
|
slug,
|
||||||
color: "#ffffff",
|
color: "#ffffff",
|
||||||
cover: "",
|
cover: "",
|
||||||
},
|
},
|
||||||
|
|
|
@ -792,6 +792,9 @@ core:
|
||||||
system_protection: System protection
|
system_protection: System protection
|
||||||
all: All
|
all: All
|
||||||
detail: Detail
|
detail: Detail
|
||||||
|
form:
|
||||||
|
validation:
|
||||||
|
slug_unique: The current slug already exists
|
||||||
uc_post:
|
uc_post:
|
||||||
creation_modal:
|
creation_modal:
|
||||||
title: Create post
|
title: Create post
|
||||||
|
|
|
@ -2030,6 +2030,9 @@ core:
|
||||||
recovering: Recovering
|
recovering: Recovering
|
||||||
fields:
|
fields:
|
||||||
post_count: "{count} Posts"
|
post_count: "{count} Posts"
|
||||||
|
form:
|
||||||
|
validation:
|
||||||
|
slug_unique: The current slug already exists
|
||||||
uc_post:
|
uc_post:
|
||||||
creation_modal:
|
creation_modal:
|
||||||
title: Create post
|
title: Create post
|
||||||
|
|
|
@ -1884,6 +1884,9 @@ core:
|
||||||
recovering: 恢复中
|
recovering: 恢复中
|
||||||
fields:
|
fields:
|
||||||
post_count: "{count} 篇文章"
|
post_count: "{count} 篇文章"
|
||||||
|
form:
|
||||||
|
validation:
|
||||||
|
slug_unique: 当前别名已存在
|
||||||
tool:
|
tool:
|
||||||
title: 工具
|
title: 工具
|
||||||
empty:
|
empty:
|
||||||
|
|
|
@ -1869,6 +1869,9 @@ core:
|
||||||
recovering: 還原中
|
recovering: 還原中
|
||||||
fields:
|
fields:
|
||||||
post_count: "{count} 篇文章"
|
post_count: "{count} 篇文章"
|
||||||
|
form:
|
||||||
|
validation:
|
||||||
|
slug_unique: 當前別名已存在
|
||||||
uc_post:
|
uc_post:
|
||||||
creation_modal:
|
creation_modal:
|
||||||
title: 創建文章
|
title: 創建文章
|
||||||
|
|
Loading…
Reference in New Issue