From 390d22da8f63f338f94646ca0100deb1ff3e84b9 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Tue, 22 Oct 2024 10:52:24 +0800 Subject: [PATCH 1/2] fix: post slug generation not following selected strategy Signed-off-by: Ryan Wang --- ui/console-src/composables/use-slugify.ts | 76 +++++++++++------------ 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/ui/console-src/composables/use-slugify.ts b/ui/console-src/composables/use-slugify.ts index b94c1b4004..6f7eceba2b 100644 --- a/ui/console-src/composables/use-slugify.ts +++ b/ui/console-src/composables/use-slugify.ts @@ -3,28 +3,20 @@ import { FormType } from "@/types/slug"; import { randomUUID } from "@/utils/id"; import ShortUniqueId from "short-unique-id"; import { slugify } from "transliteration"; -import { watch, type Ref } from "vue"; +import { computed, watch, type Ref } from "vue"; + const uid = new ShortUniqueId(); -const Strategy = { - generateByTitle: (value: string) => { - if (!value) return ""; - return slugify(value, { trim: true }); - }, - shortUUID: (value: string) => { - if (!value) return ""; - return uid.randomUUID(8); - }, - UUID: (value: string) => { - if (!value) return ""; - return randomUUID(); - }, - timestamp: (value: string) => { - if (!value) return ""; - return new Date().getTime().toString(); - }, + +type SlugStrategy = (value?: string) => string; + +const strategies: Record = { + generateByTitle: (value?: string) => slugify(value || "", { trim: true }), + shortUUID: () => uid.randomUUID(8), + UUID: () => randomUUID(), + timestamp: () => new Date().getTime().toString(), }; -const onceList = ["shortUUID", "UUID", "timestamp"]; +const onceStrategies = new Set(["shortUUID", "UUID", "timestamp"]); export default function useSlugify( source: Ref, @@ -32,35 +24,43 @@ export default function useSlugify( auto: Ref, formType: FormType ) { - const handleGenerateSlug = (forceUpdate = false, formType: FormType) => { - const globalInfoStore = useGlobalInfoStore(); - const mode = globalInfoStore.globalInfo?.postSlugGenerationStrategy; + const globalInfoStore = useGlobalInfoStore(); - if (!mode) { - return; - } - if (formType != FormType.POST) { - target.value = Strategy["generateByTitle"](source.value); - return; - } - if (forceUpdate) { - target.value = Strategy[mode](source.value); + const currentStrategy = computed( + () => + globalInfoStore.globalInfo?.postSlugGenerationStrategy || + "generateByTitle" + ); + + const generateSlug = (value: string): string => { + const strategy = + formType === FormType.POST + ? strategies[currentStrategy.value] + : strategies.generateByTitle; + + return strategy(value); + }; + + const handleGenerateSlug = (forceUpdate = false) => { + if ( + !forceUpdate && + onceStrategies.has(currentStrategy.value) && + target.value + ) { return; } - if (onceList.includes(mode) && target.value) return; - target.value = Strategy[mode](source.value); + + target.value = generateSlug(source.value); }; watch( - () => source.value, + source, () => { if (auto.value) { - handleGenerateSlug(false, formType); + handleGenerateSlug(false); } }, - { - immediate: true, - } + { immediate: true } ); return { From cf0810821d5622778286c38f5c776b0d075d5aa5 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Tue, 22 Oct 2024 10:59:00 +0800 Subject: [PATCH 2/2] Fix ts error Signed-off-by: Ryan Wang --- ui/console-src/composables/use-slugify.ts | 2 +- .../contents/pages/components/SinglePageSettingModal.vue | 2 +- .../posts/categories/components/CategoryEditingModal.vue | 2 +- .../modules/contents/posts/components/PostSettingModal.vue | 2 +- .../modules/contents/posts/tags/components/TagEditingModal.vue | 2 +- ui/uc-src/modules/contents/posts/components/PostSettingForm.vue | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ui/console-src/composables/use-slugify.ts b/ui/console-src/composables/use-slugify.ts index 6f7eceba2b..4768f153f9 100644 --- a/ui/console-src/composables/use-slugify.ts +++ b/ui/console-src/composables/use-slugify.ts @@ -57,7 +57,7 @@ export default function useSlugify( source, () => { if (auto.value) { - handleGenerateSlug(false); + handleGenerateSlug(true); } }, { immediate: true } diff --git a/ui/console-src/modules/contents/pages/components/SinglePageSettingModal.vue b/ui/console-src/modules/contents/pages/components/SinglePageSettingModal.vue index d47d54036b..f30ff8654a 100644 --- a/ui/console-src/modules/contents/pages/components/SinglePageSettingModal.vue +++ b/ui/console-src/modules/contents/pages/components/SinglePageSettingModal.vue @@ -318,7 +318,7 @@ const { handleGenerateSlug } = useSlugify( $t('core.page.settings.fields.slug.refresh_message') " class="group flex h-full cursor-pointer items-center border-l px-3 transition-all hover:bg-gray-100" - @click="handleGenerateSlug(true, FormType.SINGLE_PAGE)" + @click="handleGenerateSlug(true)" > { $t('core.post.settings.fields.slug.refresh_message') " class="group flex h-full cursor-pointer items-center border-l px-3 transition-all hover:bg-gray-100" - @click="handleGenerateSlug(true, FormType.POST)" + @click="handleGenerateSlug(true)" > {