From fd56f24b1fe0cc71185df393ef50fa61941f1442 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Tue, 23 Aug 2022 17:10:00 +0800 Subject: [PATCH] refactor: post editing Signed-off-by: Ryan Wang --- src/modules/contents/posts/PostEditor.vue | 45 +++++++----- src/modules/contents/posts/PostList.vue | 22 ++++-- .../components/CategoryEditingModal.vue | 25 +++++-- .../components/CategoryListItem.vue | 2 + .../posts/components/PostSettingModal.vue | 73 +++++++------------ src/modules/contents/posts/tags/TagList.vue | 2 + 6 files changed, 93 insertions(+), 76 deletions(-) diff --git a/src/modules/contents/posts/PostEditor.vue b/src/modules/contents/posts/PostEditor.vue index 245cb58b..86346b75 100644 --- a/src/modules/contents/posts/PostEditor.vue +++ b/src/modules/contents/posts/PostEditor.vue @@ -14,8 +14,6 @@ import { apiClient } from "@halo-dev/admin-shared"; import { useRouteQuery } from "@vueuse/router"; import { v4 as uuid } from "uuid"; -const name = useRouteQuery("name"); - const initialFormState: PostRequest = { post: { spec: { @@ -60,10 +58,13 @@ const isUpdateMode = computed(() => { return !!formState.value.post.metadata.creationTimestamp; }); -const handleSavePost = async () => { +const handleSave = async () => { try { saving.value = true; + + // Set rendered content formState.value.content.content = formState.value.content.raw; + if (isUpdateMode.value) { const { data } = await apiClient.post.updateDraftPost( formState.value.post.metadata.name, @@ -75,20 +76,38 @@ const handleSavePost = async () => { formState.value.post = data; name.value = data.metadata.name; } + + await handleFetchContent(); } catch (e) { - alert(`保存异常: ${e}`); console.error("Failed to save post", e); } finally { saving.value = false; } }; +const handleFetchContent = async () => { + if (!formState.value.post.spec.headSnapshot) { + return; + } + const { data } = await apiClient.content.obtainSnapshotContent( + formState.value.post.spec.headSnapshot + ); + + formState.value.content = data; +}; + const onSettingSaved = (post: PostRequest) => { + // Set route query parameter + if (!isUpdateMode.value) { + name.value = post.post.metadata.name; + } + formState.value = post; settingModal.value = false; - handleSavePost(); }; +// Get post data when the route contains the name parameter +const name = useRouteQuery("name"); onMounted(async () => { if (name.value) { // fetch post @@ -98,12 +117,8 @@ onMounted(async () => { ); formState.value.post = post; - if (formState.value.post.spec.headSnapshot) { - const { data: content } = await apiClient.content.obtainSnapshotContent( - formState.value.post.spec.headSnapshot - ); - formState.value.content = content; - } + // fetch post content + await handleFetchContent(); } }); @@ -111,7 +126,6 @@ onMounted(async () => {