Skip to content

Commit

Permalink
refactor: comment plugins utils
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyxguo committed Aug 6, 2023
1 parent ac95755 commit b5cfe0c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 36 deletions.
40 changes: 8 additions & 32 deletions src/components/Sidebar/src/RecentComment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import { useI18n } from 'vue-i18n'
import { TwikooComments } from '@/utils/comments/twikoo-api'
import { WalineComments } from '@/utils/comments/waline-api'
import { SvgTypes } from '@/components/SvgIcon/index.vue'
import { enabledCommentPlugin } from '@/utils/comments/helpers'
export default defineComponent({
name: 'ObRecentComment',
Expand All @@ -116,45 +117,20 @@ export default defineComponent({
let recentComments = ref<RecentComment[]>([])
let loading = ref<boolean>(true)
const enabledCommentPlugin = computed<string | undefined>(() => {
if (
!!appStore.themeConfig.plugins.gitalk.enable &&
!!appStore.themeConfig.plugins.gitalk.recentComment
) {
return 'gitalk'
}
if (
!!appStore.themeConfig.plugins.valine.enable &&
!!appStore.themeConfig.plugins.valine.recentComment
) {
return 'valine'
}
if (
!!appStore.themeConfig.plugins.twikoo.enable &&
!!appStore.themeConfig.plugins.twikoo.recentComment
) {
return 'twikoo'
}
if (
!!appStore.themeConfig.plugins.waline.enable &&
!!appStore.themeConfig.plugins.waline.recentComment
) {
return 'waline'
}
return undefined
const enabledPlugin = computed<string | undefined>(() => {
const result = enabledCommentPlugin(appStore.themeConfig.plugins)
return result.plugin !== '' && !!result.recentComment
? result.plugin
: undefined
})
const initRecentComment = () => {
if (!appStore.configReady || enabledCommentPlugin.value === undefined) {
if (!appStore.configReady || enabledPlugin.value === undefined) {
loading.value = false
return
}
switch (enabledCommentPlugin.value) {
switch (enabledPlugin.value) {
case 'gitalk':
const githubComments = new GithubComments({
repo: appStore.themeConfig.plugins.gitalk.repo,
Expand Down
11 changes: 7 additions & 4 deletions src/locales/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { createI18n } from 'vue-i18n'

interface MessageType {
[key: string]: { [key: string]: string}
[key: string]: { [key: string]: string }
}
interface MessageTranslation {
[key: string]: MessageType
}

function loadLocaleMessages() {
const locales: Record<string, MessageType> = import.meta.glob('./languages/*.json', {eager: true})
const locales: Record<string, MessageType> = import.meta.glob(
'./languages/*.json',
{ eager: true }
)

const messages: MessageTranslation = {}
Object.keys(locales).forEach((key) => {
Object.keys(locales).forEach(key => {
const matched = key.match(/([A-Za-z0-9-_]+)\./i)
if (matched && matched.length > 1) {
const locale = matched[1]
Expand All @@ -26,7 +29,7 @@ const i18n = createI18n({
locale: import.meta.env.VITE_APP_I18N_LOCALE || 'en',
fallbackLocale: import.meta.env.VITE_APP_I18N_FALLBACK_LOCALE || 'en',
messages: loadLocaleMessages(),
globalInjection: true,
globalInjection: true
})

export default i18n
63 changes: 63 additions & 0 deletions src/utils/comments/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { PluginsData } from '@/models/ThemeConfig.class'
import { walineCommentViewInit, walinePageViewInit } from './waline-api'
import { twikooCommentsCount } from './twikoo-api'

export const enabledCommentPlugin = (plugins: PluginsData) => {
const res = {
plugin: '',
recentComment: false
}

if (!!plugins.gitalk.enable && !!plugins.gitalk.recentComment) {
res.plugin = 'gitalk'
res.recentComment = !!plugins.gitalk.recentComment
return res
}

if (!!plugins.valine.enable && !!plugins.valine.recentComment) {
res.plugin = 'valine'
res.recentComment = !!plugins.valine.recentComment
return res
}

if (!!plugins.twikoo.enable && !!plugins.twikoo.recentComment) {
res.plugin = 'twikoo'
res.recentComment = !!plugins.twikoo.recentComment
return res
}

if (!!plugins.waline.enable && !!plugins.waline.recentComment) {
res.plugin = 'waline'
res.recentComment = !!plugins.waline.recentComment
return res
}

return res
}

export const intiCommentPluginPageView = (
plugin: string,
plugins: PluginsData
) => {
switch (plugin) {
case 'waline':
walinePageViewInit(plugins.waline.serverURL)
break
}
}

export const initCommentPluginCommentCount = async (
plugin: string,
path: string,
plugins: PluginsData
) => {
switch (plugin) {
case 'waline':
walineCommentViewInit(plugins.waline.serverURL, path)
return 0
case 'twikoo':
return await twikooCommentsCount(plugins.twikoo.envId, path)
}

return 0
}
12 changes: 12 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,15 @@ export function filterHTMLContent(content: string, length?: number): string {

return content
}

export function getDaysTillNow(from: string) {
const today = new Date()
const fromDate = new Date(from)
console.log(fromDate)

// To calculate the time difference of two dates
const timeDiff = today.getTime() - fromDate.getTime()

// To calculate the no. of days between two dates
return Math.floor(timeDiff / (1000 * 3600 * 24))
}

0 comments on commit b5cfe0c

Please sign in to comment.