From 07a2e9ea3242f367ffcbfbff67af182886d02a88 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Tue, 31 Oct 2023 16:44:46 +0800 Subject: [PATCH] feat(client): add event dispatch --- .../client/src/components/WalineComment.vue | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/packages/client/src/components/WalineComment.vue b/packages/client/src/components/WalineComment.vue index 688267a58f3..ec55bda78d9 100644 --- a/packages/client/src/components/WalineComment.vue +++ b/packages/client/src/components/WalineComment.vue @@ -134,6 +134,16 @@ const props = defineProps([ 'reaction', ]); +const emit = defineEmits<{ + (event: 'update', data: WalineRootComment[]): void; + (event: 'fail', err: Error): void; + (event: 'submit', data: WalineComment): void; + (event: 'like', data: WalineComment, isLike: boolean): void; + (event: 'delete', data: WalineComment): void; + (event: 'status', data: WalineComment): void; + (event: 'sticky', data: WalineComment): void; +}>(); + const sortKeyMap: Record = { latest: 'insertedAt_desc', oldest: 'insertedAt_asc', @@ -167,7 +177,7 @@ useStyleTag(darkmodeStyle, { id: 'waline-darkmode' }); let abort: () => void; -const getCommentData = (pageNumber: number): void => { +const getCommentData = (pageNumber: number): Promise => { const { serverURL, path, pageSize } = config.value; const controller = new AbortController(); @@ -175,7 +185,9 @@ const getCommentData = (pageNumber: number): void => { abort?.(); - getComment({ + abort = controller.abort.bind(controller); + + return getComment({ serverURL, lang: config.value.lang, path, @@ -191,29 +203,31 @@ const getCommentData = (pageNumber: number): void => { data.value.push(...resp.data); page.value = pageNumber; totalPages.value = resp.totalPages; + emit('update', data.value); }) .catch((err: Error) => { if (err.name !== 'AbortError') { console.error(err.message); status.value = 'error'; + emit('fail', err); } }); - - abort = controller.abort.bind(controller); }; -const loadMore = (): void => getCommentData(page.value + 1); +const loadMore = (): Promise => getCommentData(page.value + 1); -const refresh = (): void => { +const refresh = (): Promise => { count.value = 0; data.value = []; - getCommentData(1); + + return getCommentData(1); }; -const onSortByChange = (item: WalineCommentSorting): void => { +const onSortByChange = async (item: WalineCommentSorting): Promise => { if (commentSortingRef.value !== item) { commentSortingRef.value = item; - refresh(); + + return refresh(); } }; @@ -243,6 +257,8 @@ const onSubmit = (comment: WalineComment): void => { data.value.unshift(comment); count.value += 1; } + + emit('submit', comment); }; const onStatusChange = async ({ @@ -265,6 +281,8 @@ const onStatusChange = async ({ }); comment.status = status; + + emit('status', comment); }; const onSticky = async (comment: WalineComment): Promise => { @@ -281,10 +299,13 @@ const onSticky = async (comment: WalineComment): Promise => { }); comment.sticky = !comment.sticky; + + emit('sticky', comment); }; -const onDelete = async ({ objectId }: WalineComment): Promise => { +const onDelete = async (comment: WalineComment): Promise => { if (!confirm('Are you sure you want to delete this comment?')) return; + const { objectId } = comment; const { serverURL, lang } = config.value; @@ -315,6 +336,8 @@ const onDelete = async ({ objectId }: WalineComment): Promise => { return false; }); }); + + emit('delete', comment); }; const onLike = async (comment: WalineComment): Promise => { @@ -340,6 +363,8 @@ const onLike = async (comment: WalineComment): Promise => { } comment.like = (comment.like || 0) + (hasLiked ? -1 : 1); + + emit('like', comment, !hasLiked); }; provide('config', config);