generated from halo-dev/plugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
196 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { randomUUID } from "@/utils/id"; | ||
import { | ||
consoleApiClient, | ||
type ContentWrapper, | ||
type Post, | ||
type PostRequest, | ||
} from "@halo-dev/api-client"; | ||
import { Toast } from "@halo-dev/components"; | ||
import { cloneDeep, set } from "lodash-es"; | ||
|
||
class PostCloner { | ||
static async clonePost(post: Post): Promise<void> { | ||
try { | ||
const originalContent = await this.fetchPostContent(post.metadata.name); | ||
|
||
const newPostData = this.prepareNewPostData(post, originalContent); | ||
|
||
await consoleApiClient.content.post.draftPost({ | ||
postRequest: newPostData, | ||
}); | ||
|
||
Toast.success("文章克隆成功,如果列表没有刷新,请手动刷新一次"); | ||
} catch (error) { | ||
console.error("Failed to clone post", error); | ||
Toast.error("克隆文章失败"); | ||
} | ||
} | ||
|
||
private static async fetchPostContent( | ||
postName: string, | ||
): Promise<ContentWrapper> { | ||
const { data } = await consoleApiClient.content.post.fetchPostHeadContent({ | ||
name: postName, | ||
}); | ||
|
||
return data; | ||
} | ||
|
||
private static prepareNewPostData( | ||
originalPost: Post, | ||
content: ContentWrapper, | ||
): PostRequest { | ||
const postToCreate = cloneDeep(originalPost); | ||
set(postToCreate, "spec.baseSnapshot", ""); | ||
set(postToCreate, "spec.headSnapshot", ""); | ||
set(postToCreate, "spec.releaseSnapshot", ""); | ||
set( | ||
postToCreate, | ||
"spec.slug", | ||
`${originalPost.spec.slug}-${randomUUID().split("-")[0]}`, | ||
); | ||
set(postToCreate, "spec.title", originalPost.spec.title + "(副本)"); | ||
set(postToCreate, "spec.publish", false); | ||
set(postToCreate, "metadata", { | ||
name: randomUUID(), | ||
}); | ||
|
||
return { | ||
post: postToCreate, | ||
content: content, | ||
}; | ||
} | ||
} | ||
|
||
export default PostCloner; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<script setup lang="ts"> | ||
import PostCloner from "@/class/postCloner"; | ||
import type { ListedPost } from "@halo-dev/api-client"; | ||
import { VDropdownItem } from "@halo-dev/components"; | ||
import { useQueryClient } from "@tanstack/vue-query"; | ||
const queryClient = useQueryClient(); | ||
const { post } = defineProps<{ | ||
post: ListedPost; | ||
}>(); | ||
async function handleClone() { | ||
await PostCloner.clonePost(post.post); | ||
// It may be that the index creation operation cannot obtain the new list in time. | ||
setTimeout(() => { | ||
queryClient.invalidateQueries({ queryKey: ["posts"] }); | ||
}, 1000); | ||
} | ||
</script> | ||
<template> | ||
<VDropdownItem @click="handleClone"> 克隆 </VDropdownItem> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function randomUUID() { | ||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { | ||
const r = (Math.random() * 16) | 0, | ||
v = c === "x" ? r : (r & 0x3) | 0x8; | ||
return v.toString(16); | ||
}); | ||
} |