Skip to content

Commit

Permalink
feat: added valine, twikoo and valine post views stat
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyxguo committed Aug 6, 2023
1 parent 2d15ac6 commit 517e9d0
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/styles/components/article.scss
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@
.post-stats {
@apply flex-row text-sm lg:text-base mr-4 hidden lg:flex;
> span {
@apply text-white stroke-current flex flex-row items-center pr-4 opacity-80;
@apply text-white stroke-current flex flex-row items-center pr-4;
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/utils/comments/twikoo-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,23 @@ export const twikooInit = (configs: TwikooInitConfig) => {
el: '#tcomment', // 容器元素
region: configs.region, // 环境地域,默认为 ap-shanghai,腾讯云环境填 ap-shanghai 或 ap-guangzhou;Vercel 环境不填
path: configs.path, // 用于区分不同文章的自定义 js 路径,如果您的文章路径不是 location.pathname,需传此参数
lang: configs.lang // 用于手动设定评论区语言,支持的语言列表 https://github.com/imaegoo/twikoo/blob/main/src/client/utils/i18n/index.js
lang: configs.lang, // 用于手动设定评论区语言,支持的语言列表 https://github.com/imaegoo/twikoo/blob/main/src/client/utils/i18n/index.js
visitor: true
})
}

export const twikooCommentsCount = async (
envId: string,
path: string
): Promise<number> => {
const commentCounts = await twikoo.getCommentsCount({
envId,
urls: [path],
includeReply: true
})
return commentCounts[0] ? Number(commentCounts[0].count) : 0
}

export class TwikooComments {
configs: TwikooConfig = defaultConfig

Expand Down
17 changes: 17 additions & 0 deletions src/utils/comments/waline-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ declare const Waline: any

import {
init,
pageviewCount,
commentCount,
RecentComments
// @ts-expect-error
} from 'https://unpkg.com/@waline/client@v2/dist/waline.mjs'
Expand Down Expand Up @@ -73,6 +75,21 @@ export const walineInit = ({
return init(options)
}

export const walinePageViewInit = (serverURL: string) => {
console.log(123)
pageviewCount({
serverURL,
path: window.location.pathname
})
}

export const walineCommentViewInit = (serverURL: string, path: string) => {
commentCount({
serverURL,
path
})
}

export class WalineComments {
configs: Partial<WalineConfig> = {
serverURL: '',
Expand Down
191 changes: 172 additions & 19 deletions src/views/Post.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,163 @@
v-if="post.count_time.symbolsTime && post.date"
>
<span>
<SvgIcon icon-class="clock-outline" stroke="var(--text-white)" />
<SvgIcon
icon-class="clock-outline"
fill="none"
stroke="white"
height="1.25em"
width="1.25em"
/>
<span class="pl-2 opacity-70">
{{ post.count_time.symbolsTime }}
</span>
</span>
<span>
<SvgIcon icon-class="text-outline" stroke="var(--text-white)" />
<SvgIcon
icon-class="text-outline"
fill="none"
stroke="white"
height="1.25em"
width="1.25em"
/>
<span class="pl-2 opacity-70">
{{ post.count_time.symbolsCount }}
</span>
</span>

<!-- Waline -->
<span v-if="enabledPlugin === 'waline'">
<SvgIcon
class="h-5 w-5"
icon-class="hot"
fill="none"
stroke="white"
height="1.25em"
width="1.25em"
/>
<span class="pl-2 opacity-70">
<span class="waline-pageview-count" />
</span>
</span>
<span v-if="enabledPlugin === 'waline'">
<SvgIcon
class="h-5 w-5"
icon-class="quote"
fill="none"
stroke="white"
height="1.25em"
width="1.25em"
/>
<span class="pl-2 opacity-70">
<span class="waline-comment-count" :data-path="currentPath" />
</span>
</span>

<!-- Twikoo -->
<span v-if="enabledPlugin === 'twikoo'">
<SvgIcon
class="h-5 w-5"
icon-class="hot"
fill="none"
stroke="white"
height="1.25em"
width="1.25em"
/>
<span class="pl-2 opacity-70" id="twikoo_visitors">
<ob-skeleton width="40px" height="16px" />
</span>
</span>
<span v-if="enabledPlugin === 'twikoo'">
<SvgIcon
class="h-5 w-5"
icon-class="quote"
fill="none"
stroke="white"
height="1.25em"
width="1.25em"
/>
<span class="pl-2 opacity-70">
{{ commentCount }}
</span>
</span>

<!-- Valine -->
<span v-if="enabledPlugin === 'valine'">
<SvgIcon
class="h-5 w-5"
icon-class="hot"
fill="none"
stroke="white"
height="1.25em"
width="1.25em"
/>
<span class="pl-2 opacity-70">
<span
:id="currentPath"
class="leancloud_visitors"
:data-flag-title="post.title"
>
<i class="leancloud-visitors-count">
<ob-skeleton width="40px" height="16px" />
</i>
</span>
</span>
</span>
</div>

<div v-else class="post-stats">
<span>
<SvgIcon icon-class="clock" />
<SvgIcon
icon-class="clock-outline"
fill="none"
stroke="white"
height="1.25em"
width="1.25em"
/>
<span class="pl-2">
<ob-skeleton width="40px" height="16px" />
</span>
</span>
<span>
<SvgIcon icon-class="text" />
<SvgIcon
icon-class="text-outline"
fill="none"
stroke="white"
height="1.25em"
width="1.25em"
/>
<span class="pl-2">
<ob-skeleton width="40px" height="16px" />
</span>
</span>
<span
v-if="
enabledPlugin === 'waline' ||
enabledPlugin === 'twikoo' ||
enabledPlugin === 'valine'
"
>
<SvgIcon
icon-class="hot"
fill="none"
stroke="white"
height="1.25em"
width="1.25em"
/>
<span class="pl-2">
<ob-skeleton width="40px" height="16px" />
</span>
</span>
<span
v-if="enabledPlugin === 'waline' || enabledPlugin === 'twikoo'"
>
<SvgIcon
icon-class="quote"
fill="none"
stroke="white"
height="1.25em"
width="1.25em"
/>
<span class="pl-2">
<ob-skeleton width="40px" height="16px" />
</span>
Expand Down Expand Up @@ -201,15 +336,7 @@
import { Sidebar, Toc, Profile } from '@/components/Sidebar'
import { Post } from '@/models/Post.class'
import { usePostStore } from '@/stores/post'
import {
computed,
defineComponent,
nextTick,
onBeforeUnmount,
onMounted,
ref,
watch
} from 'vue'
import { computed, defineComponent, nextTick, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import Comment from '@/components/Comment.vue'
Expand All @@ -221,7 +348,12 @@ import { useMetaStore } from '@/stores/meta'
import { useAppStore } from '@/stores/app'
import { useCommonStore } from '@/stores/common'
import SvgIcon from '@/components/SvgIcon/index.vue'
import SvgIcon, { SvgTypes } from '@/components/SvgIcon/index.vue'
import {
enabledCommentPlugin,
initCommentPluginCommentCount,
intiCommentPluginPageView
} from '@/utils/comments/helpers'
declare const Prism: any
Expand All @@ -237,6 +369,11 @@ export default defineComponent({
const { t } = useI18n()
const post = ref(new Post())
const loading = ref(true)
const commentCount = ref(0)
const enabledPlugin = computed(
() => enabledCommentPlugin(appStore.themeConfig.plugins).plugin
)
const fetchData = async () => {
loading.value = true
Expand All @@ -263,6 +400,15 @@ export default defineComponent({
)
}
await nextTick()
intiCommentPluginPageView(
enabledPlugin.value,
appStore.themeConfig.plugins
)
commentCount.value = await initCommentPluginCommentCount(
enabledPlugin.value,
route.path,
appStore.themeConfig.plugins
)
Prism.highlightAll()
}
Expand All @@ -278,18 +424,25 @@ export default defineComponent({
window.location.href = link
}
onMounted(fetchData)
onBeforeUnmount(() => {
commonStore.resetHeaderImage()
})
return {
isMobile: computed(() => commonStore.isMobile),
currentPath: computed(() => route.path),
commentCount,
SvgTypes,
enabledPlugin,
commonStore,
fetchData,
handleAuthorClick,
loading,
post,
t
}
},
mounted() {
this.fetchData()
},
beforeUnmount() {
this.commonStore.resetHeaderImage()
}
})
</script>

0 comments on commit 517e9d0

Please sign in to comment.