Skip to content

Commit

Permalink
feat(FontSwitcher): ✨ enhance font switching functionality with dynam…
Browse files Browse the repository at this point in the history
…ic font loading
  • Loading branch information
BeiyanYunyi committed Dec 28, 2024
1 parent 2b1f7dd commit 5deeb41
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 47 deletions.
1 change: 1 addition & 0 deletions example/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const themeConfig: ThemeContext = {
// HideReadingTime: true, /* 隐藏字数和预计阅读时间 */
// HideLastUpdated: true, /* 隐藏最后更新时间 */
// HideAuthors: true, /* 隐藏作者信息 */
// fontsBaseUrl: 'http://localhost:8788', // For local development with wrangler pages dev
}

// https://vitepress.dev/reference/site-config
Expand Down
77 changes: 34 additions & 43 deletions src/components/FontSwitcher/FontSwitcher.vue
Original file line number Diff line number Diff line change
@@ -1,62 +1,53 @@
<script setup lang="tsx">
import { useStorage } from '@vueuse/core'
import type { PjtsThemeConfig } from '../../config'
import { useFetch, useStorage } from '@vueuse/core'
import { useData } from 'vitepress'
import VPFlyout from 'vitepress/dist/client/theme-default/components/VPFlyout.vue'
import { watchEffect } from 'vue'
import { computed, watchEffect } from 'vue'
import FontSwitcherItem from './FontSwitcherItem.vue'
const { theme } = useData<PjtsThemeConfig>()
const fontsBaseUrl = computed(() => theme.value.fontsBaseUrl)
const { data: fonts } = useFetch<Record<string, { paths: string[], fontFamily: string }>>(
`${fontsBaseUrl.value}/path_map.json`,
).json()
const activeFont = useStorage('activeFont', '')
watchEffect(() => {
if (!fonts.value)
return
if (!fonts.value[activeFont.value]) {
activeFont.value = '默认字体'
}
document.documentElement.style.setProperty(
'--main-font',
activeFont.value,
fonts.value[activeFont.value].fontFamily,
)
})
const items = [
{
name: '黑体',
value: 'sans',
},
{
name: '宋体',
value: 'serif',
},
{
name: '更纱黑体',
value: 'Sarasa UI SC',
},
{
name: '思源宋体',
value: 'Source Han Serif CN',
},
{
name: '霞鹜文楷',
value: 'LXGW WenKai',
},
{
name: '霞鹜文楷 Mono',
value: 'LXGW WenKai Mono',
},
{
name: '霞鹜新晰黑',
value: 'LXGW Neo XiHei',
},
{
name: '新晰黑 Code',
value: 'NeoXiHei Code',
},
{
name: '默认字体',
value: '',
},
].map(item => ({
component: <FontSwitcherItem fontName={item.name} value={item.value} />,
}))
const items = computed(() => {
if (!fonts.value)
return []
return Object.entries(fonts.value).map(([displayName]) => ({
component: <FontSwitcherItem fontName={displayName} />,
}))
})
</script>

<template>
<VPFlyout icon="i-octicon:typography-16" :items="items" />
<template v-if="fonts && fonts[activeFont]">
<link
v-for="css in fonts[activeFont].paths"
:key="css"
rel="stylesheet"
as="style"
:href="`${fontsBaseUrl}/${css}/result.css`"
>
</template>
</template>

<style lang="css" scoped>
Expand Down
6 changes: 3 additions & 3 deletions src/components/FontSwitcher/FontSwitcherItem.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script setup lang="ts">
import { useStorage } from '@vueuse/core'
defineProps<{ fontName: string, value: string }>()
defineProps<{ fontName: string }>()
const activeFont = useStorage('activeFont', '')
</script>

<template>
<div class="VPMenuLink" @click="activeFont = value">
<span class="link" :class="[activeFont === value && 'active']">{{ fontName }}</span>
<div class="VPMenuLink" @click="activeFont = fontName">
<span class="link" :class="[activeFont === fontName && 'active']">{{ fontName }}</span>
</div>
</template>

Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface PjtsThemeConfig extends DefaultTheme.Config {
sitemap?: {
hostname: string
}
fontsBaseUrl: string
}

// 从文件系统读取 Markdown 文件内容
Expand Down Expand Up @@ -74,6 +75,7 @@ function genConfig() {
HideAuthors,
rootDir,
hostName,
fontsBaseUrl = 'https://fonts.project-trans.org',
} = themeConfig

return defineConfigWithTheme<PjtsThemeConfig>({
Expand Down Expand Up @@ -193,6 +195,7 @@ function genConfig() {
},
},
},
fontsBaseUrl,
},
transformHead: async (context) => {
let head = [...context.head]
Expand Down
1 change: 0 additions & 1 deletion src/style.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@import 'https://fonts.project-trans.org/fonts.css';
:root {
--vp-font-family-base: var(--main-font) !important; /* 文本字体 */
--vp-font-family-mono: var(--main-font) !important; /* 代码字体 */
Expand Down
1 change: 1 addition & 0 deletions src/utils/themeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ThemeContext {
HideLastUpdated?: boolean
HideAuthors?: boolean
hostName: string
fontsBaseUrl?: string
}

const themeContext = new AsyncLocalStorage<ThemeContext>()
Expand Down

0 comments on commit 5deeb41

Please sign in to comment.