Skip to content

Commit 2e211b9

Browse files
committed
feat(plugin-git): refactor and add composables
1 parent f11f467 commit 2e211b9

17 files changed

+227
-175
lines changed

plugins/development/plugin-git/src/client/components/Contributors.ts

-74
This file was deleted.

plugins/development/plugin-git/src/client/components/Changelog.ts plugins/development/plugin-git/src/client/components/GitChangelog.ts

+18-51
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import { useToggle } from '@vueuse/core'
22
import type { FunctionalComponent } from 'vue'
3-
import { computed, defineComponent, h } from 'vue'
4-
import { usePageData, usePageFrontmatter, usePageLang } from 'vuepress/client'
5-
import type {
6-
GitChangelog,
7-
GitPluginFrontmatter,
8-
GitPluginPageData,
9-
} from '../../shared/index.js'
10-
import { useGitLocaleConfig } from '../composables/index.js'
3+
import { defineComponent, h } from 'vue'
4+
import type { GitChangelogItem } from '../composables/index.js'
5+
import {
6+
useChangelog,
7+
useGitLocaleConfig,
8+
useLastUpdated,
9+
} from '../composables/index.js'
1110
import { VPHeader } from './VPHeader.js'
1211

1312
import '../styles/vars.css'
1413
import '../styles/changelog.css'
1514

16-
type ResolvedChangelog = Omit<GitChangelog, 'date'> & { datetime: string }
17-
18-
export const Changelog = defineComponent({
19-
name: 'Changelog',
15+
export const GitChangelog = defineComponent({
16+
name: 'GitChangelog',
2017

2118
props: {
2219
/** Title of changelog */
@@ -30,55 +27,25 @@ export const Changelog = defineComponent({
3027
},
3128

3229
setup(props) {
30+
const changelog = useChangelog()
3331
const locale = useGitLocaleConfig()
34-
const frontmatter = usePageFrontmatter<GitPluginFrontmatter>()
35-
const page = usePageData<GitPluginPageData>()
36-
const lang = usePageLang()
37-
38-
const list = computed<ResolvedChangelog[]>(() => {
39-
if (frontmatter.value.changelog === false) return []
40-
41-
const formatter = new Intl.DateTimeFormat(lang.value, {
42-
dateStyle: 'short',
43-
})
44-
45-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
46-
return (page.value.git?.changelog ?? []).map(({ date, ...item }) => {
47-
const datetime = formatter.format(date)
48-
return { datetime, ...item }
49-
})
50-
})
51-
52-
const latestUpdated = computed(() => {
53-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
54-
const latest = (page.value.git?.changelog ?? [])[0]
55-
56-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
57-
if (!latest) return ''
58-
59-
const formatter = new Intl.DateTimeFormat(lang.value, {
60-
dateStyle: 'short',
61-
timeStyle: 'short',
62-
})
63-
return formatter.format(latest.date)
64-
})
32+
const latestUpdated = useLastUpdated()
6533

6634
const [active, toggleActive] = useToggle()
6735

6836
const ChangelogHeader: FunctionalComponent = () =>
6937
h('div', { class: 'changelog-header', onClick: () => toggleActive() }, [
7038
h('div', { class: 'latest-updated' }, [
7139
h('span', { class: 'vpi-changelog' }),
72-
h('span', [locale.value.latestUpdateAt, ' ']),
73-
h('span', { 'data-allow-mismatch': '' }, latestUpdated.value),
40+
h('span', { 'data-allow-mismatch': '' }, latestUpdated.value!.text),
7441
]),
7542
h('div', [
7643
h('span', { class: 'vpi-changelog-menu' }),
7744
h('span', locale.value.viewChangelog),
7845
]),
7946
])
8047

81-
const ReleaseTag: FunctionalComponent<{ item: ResolvedChangelog }> = ({
48+
const ReleaseTag: FunctionalComponent<{ item: GitChangelogItem }> = ({
8249
item,
8350
}) =>
8451
h(
@@ -89,12 +56,12 @@ export const Changelog = defineComponent({
8956
h('span', { 'class': 'datetime', 'data-allow-mismatch': '' }, [
9057
locale.value.timeOn,
9158
' ',
92-
item.datetime,
59+
item.date,
9360
]),
9461
]),
9562
)
9663

97-
const Commit: FunctionalComponent<{ item: ResolvedChangelog }> = ({
64+
const Commit: FunctionalComponent<{ item: GitChangelogItem }> = ({
9865
item,
9966
}) =>
10067
h('li', { class: 'changelog commit' }, [
@@ -113,12 +80,12 @@ export const Changelog = defineComponent({
11380
h('span', { 'class': 'datetime', 'data-allow-mismatch': '' }, [
11481
locale.value.timeOn || 'on',
11582
' ',
116-
item.datetime,
83+
item.date,
11784
]),
11885
])
11986

12087
return () =>
121-
list.value.length
88+
changelog.value.length
12289
? [
12390
h(VPHeader, {
12491
level: props.headerLevel,
@@ -130,7 +97,7 @@ export const Changelog = defineComponent({
13097
h(ChangelogHeader),
13198

13299
h('ul', { class: 'changelog-list' }, [
133-
list.value.map((item) =>
100+
changelog.value.map((item) =>
134101
item.tag
135102
? h(ReleaseTag, { item, key: item.tag })
136103
: h(Commit, { item, key: item.hash }),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { FunctionalComponent } from 'vue'
2+
import { defineComponent, h } from 'vue'
3+
import type { GitContributorInfo } from '../../shared/index.js'
4+
import { useContributors, useGitLocaleConfig } from '../composables/index.js'
5+
import { VPHeader } from './VPHeader.js'
6+
7+
import '../styles/contributors.css'
8+
9+
export const GitContributor: FunctionalComponent<GitContributorInfo> = ({
10+
name,
11+
url,
12+
avatar,
13+
}) =>
14+
h(
15+
url ? 'a' : 'span',
16+
{
17+
href: url,
18+
target: '_blank',
19+
rel: 'noreferrer',
20+
class: 'vp-contributor',
21+
},
22+
[
23+
avatar
24+
? h('img', { src: avatar, alt: name, class: 'vp-contributor-avatar' })
25+
: null,
26+
h('span', { class: 'vp-contributor-name' }, name),
27+
],
28+
)
29+
30+
export const GitContributors = defineComponent({
31+
name: 'GitContributors',
32+
33+
props: {
34+
/** Contributor title */
35+
title: String,
36+
37+
/** header level of contributor title */
38+
headerLevel: {
39+
type: Number,
40+
default: 2,
41+
},
42+
},
43+
44+
setup(props) {
45+
const contributors = useContributors()
46+
const locale = useGitLocaleConfig()
47+
48+
return () =>
49+
contributors.value.length
50+
? [
51+
h(VPHeader, {
52+
level: props.headerLevel,
53+
anchor: 'doc-contributors',
54+
text: props.title || locale.value.contributors,
55+
}),
56+
h(
57+
'div',
58+
{ class: 'vp-contributors' },
59+
contributors.value.map((item) => h(GitContributor, item)),
60+
),
61+
]
62+
: null
63+
},
64+
})
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './Contributors.js'
2-
export * from './Changelog.js'
1+
export * from './GitContributors.js'
2+
export * from './GitChangelog.js'
33
export * from './VPHeader.js'
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
export * from './useChangelog.js'
2+
export * from './useContributors.js'
13
export * from './useGitLocales.js'
4+
export * from './useLastUpdated.js'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { ComputedRef } from 'vue'
2+
import { computed } from 'vue'
3+
import { usePageData, usePageFrontmatter, usePageLang } from 'vuepress/client'
4+
import type {
5+
GitChangelogInfo,
6+
GitPluginFrontmatter,
7+
GitPluginPageData,
8+
} from '../../shared/index.js'
9+
10+
export interface GitChangelogItem extends GitChangelogInfo {
11+
date: string
12+
}
13+
14+
export const useChangelog = (): ComputedRef<GitChangelogItem[]> => {
15+
const frontmatter = usePageFrontmatter<GitPluginFrontmatter>()
16+
const lang = usePageLang()
17+
const page = usePageData<GitPluginPageData>()
18+
19+
return computed(() => {
20+
if (frontmatter.value.changelog === false) return []
21+
22+
const formatter = new Intl.DateTimeFormat(lang.value, {
23+
dateStyle: 'short',
24+
})
25+
26+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
27+
return (page.value.git?.changelog ?? []).map((item) => ({
28+
date: formatter.format(item.time),
29+
...item,
30+
}))
31+
})
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { ComputedRef } from 'vue'
2+
import { computed } from 'vue'
3+
import { usePageData, usePageFrontmatter } from 'vuepress/client'
4+
import type {
5+
GitContributorInfo,
6+
GitPluginFrontmatter,
7+
GitPluginPageData,
8+
} from '../../shared/index.js'
9+
10+
export const useContributors = (): ComputedRef<GitContributorInfo[]> => {
11+
const frontmatter = usePageFrontmatter<GitPluginFrontmatter>()
12+
const page = usePageData<GitPluginPageData>()
13+
14+
return computed(() => {
15+
if (frontmatter.value.contributors === false) return []
16+
17+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
18+
return page.value.git?.contributors ?? []
19+
})
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { ComputedRef } from 'vue'
2+
import { computed } from 'vue'
3+
import { usePageData, usePageLang } from 'vuepress/client'
4+
import type { GitPluginPageData } from '../../shared/index.js'
5+
import { useGitLocaleConfig } from './useGitLocales.js'
6+
7+
export interface LastUpdated {
8+
date: Date
9+
iso: string
10+
text: string
11+
}
12+
13+
export const useLastUpdated = (): ComputedRef<LastUpdated | null> => {
14+
const lang = usePageLang()
15+
const locale = useGitLocaleConfig()
16+
const page = usePageData<GitPluginPageData>()
17+
18+
return computed(() => {
19+
const timeStamp =
20+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
21+
page.value.git?.updatedTime ?? page.value.git?.changelog?.[0].time
22+
23+
if (!timeStamp) return null
24+
25+
const date = new Date(timeStamp)
26+
27+
const formatted = new Intl.DateTimeFormat(lang.value, {
28+
dateStyle: 'short',
29+
timeStyle: 'short',
30+
}).format(timeStamp)
31+
32+
return {
33+
date,
34+
iso: date.toISOString(),
35+
text: `${locale.value.latestUpdateAt} ${formatted}`,
36+
}
37+
})
38+
}

0 commit comments

Comments
 (0)