Skip to content

Commit

Permalink
feat: only show toggle when languages.length > 1, close #465
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Nov 7, 2024
1 parent cc4014b commit 5e3e032
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 20 deletions.
3 changes: 3 additions & 0 deletions demo/yun/site.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default defineSiteConfig({
},

lang: 'zh-CN',
// disable show language switch
languages: ['zh-CN'],

title: '自定义博客名称',
timezone: 'Asia/Shanghai',
url: 'https://yun.valaxy.site/',
Expand Down
5 changes: 4 additions & 1 deletion packages/valaxy-theme-yun/components/YunConfig.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<script lang="ts" setup>
import { useAppStore } from 'valaxy'
// sidebar config
const app = useAppStore()
</script>

<template>
<div>
<YunToggleDark />

<YunToggleLocale />
<YunToggleLocale v-if="app.showToggleLocale" />
</div>
</template>
5 changes: 4 additions & 1 deletion packages/valaxy-theme-yun/components/YunFullscreenMenu.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<script setup lang="ts">
import { ref } from 'vue'
import { useAppStore } from 'valaxy'
import { useYunAppStore } from '../stores'
const yunApp = useYunAppStore()
const fullscreenMenuRef = ref<HTMLElement>()
const app = useAppStore()
</script>

<template>
Expand All @@ -16,7 +19,7 @@ const fullscreenMenuRef = ref<HTMLElement>()
>
<div class="flex-center gap-2">
<YunToggleDark transition />
<YunToggleLocale />
<YunToggleLocale v-if="app.showToggleLocale" />
</div>

<YunFullscreenMenuList>
Expand Down
6 changes: 4 additions & 2 deletions packages/valaxy-theme-yun/components/YunNavMenu.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { onMounted, ref, watch } from 'vue'
import { useSiteConfig } from 'valaxy'
import { useAppStore, useSiteConfig } from 'valaxy'
import { useRoute } from 'vue-router'
import { useYunAppStore } from '../stores'
// import { useThemeConfig } from '../composables'
Expand Down Expand Up @@ -30,6 +30,8 @@ watch(() => yunApp.scrollY, () => {
else
playAnimation.value = false
})
const app = useAppStore()
</script>

<template>
Expand Down Expand Up @@ -77,7 +79,7 @@ watch(() => yunApp.scrollY, () => {
</template> -->

<YunToggleLocale
v-if="yunApp.size.isSm"
v-if="yunApp.size.isSm && app.showToggleLocale"
class="rounded-none!"
/>
<YunToggleDark class="rounded-none!" transition />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function toggle(e: MouseEvent) {
<button
class="yun-icon-btn"
:title="themeTitle"
:style="styles" @mousedown.prevent="() => { console.log('yes') }"
:style="styles"
@mousedown.prevent="() => {}"
@click="toggle"
>
<div i="ri-sun-line dark:ri-moon-line" />
Expand Down
13 changes: 11 additions & 2 deletions packages/valaxy/client/stores/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { acceptHMRUpdate, defineStore } from 'pinia'
import { ref } from 'vue'
import { useMobile, useThemeConfig, useValaxyDark } from 'valaxy'
import { computed, ref } from 'vue'
import { useMobile, useSiteConfig, useThemeConfig, useValaxyDark } from 'valaxy'

/**
* Global store for users
Expand All @@ -11,16 +11,25 @@ import { useMobile, useThemeConfig, useValaxyDark } from 'valaxy'
* ```
*/
export const useAppStore = defineStore('app', () => {
const siteConfig = useSiteConfig()

const themeConfig = useThemeConfig()
const { isDark, toggleDark, toggleDarkWithTransition, themeColor } = useValaxyDark(themeConfig.value.valaxyDarkOptions)

const isMobile = useMobile()
const showLoading = ref(true)

const showToggleLocale = computed(() => siteConfig.value.languages.length > 1)

return {
isMobile,
// for dark
isDark,
/**
* show toggle locale btn
* only show toggle when `siteConfig.languages.length > 1`
*/
showToggleLocale,
themeColor,
toggleDark,
toggleDarkWithTransition,
Expand Down
18 changes: 10 additions & 8 deletions packages/valaxy/node/config/merge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import defu, { createDefu } from 'defu'
import { createDefu } from 'defu'

// function replaceByClonedSource(options: any) {
// const clone = options.clone
Expand All @@ -7,10 +7,12 @@
// }
// }

// override array merge
// export const replaceArrMerge = createDefu(({ obj, key, value }) => {
// console.log(value)

// return value
// })
// export const replaceArrMerge = defu
/**
* replace array instead of concat
*/
export const replaceArrMerge = createDefu((obj, key, value) => {
if (key && obj[key] && Array.isArray(obj[key]) && Array.isArray(value)) {
obj[key] = value
return true
}
})
7 changes: 4 additions & 3 deletions packages/valaxy/node/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fs from 'fs-extra'
import _debug from 'debug'
import fg from 'fast-glob'
import { ensureSuffix, uniq } from '@antfu/utils'
import defu from 'defu'
// import defu from 'defu'
import { blue, cyan, magenta, yellow } from 'picocolors'
import consola from 'consola'
import type { DefaultTheme, RedirectItem, RuntimeConfig } from 'valaxy/types'
Expand All @@ -25,6 +25,7 @@ import { getThemeRoot } from './utils/theme'
import { resolveSiteConfig } from './config/site'
import { countPerformanceTime } from './utils/performance'
import { collectRedirects } from './utils/clientRedirects'
import { replaceArrMerge } from './config/merge'

// for cli entry
export interface ValaxyEntryOptions {
Expand Down Expand Up @@ -142,7 +143,7 @@ export async function processValaxyOptions(valaxyOptions: ResolvedValaxyOptions,
const addonsValaxyConfig = await resolveAddonsConfig(addons, valaxyOptions)
valaxyConfig = mergeValaxyConfig(valaxyConfig, addonsValaxyConfig)

const config = defu(valaxyConfig, defaultValaxyConfig)
const config = replaceArrMerge(valaxyConfig, defaultValaxyConfig)
valaxyOptions.config = {
...config,
runtimeConfig: {
Expand Down Expand Up @@ -207,7 +208,7 @@ export async function resolveOptions(
const redirects = collectRedirects(siteConfig.redirects?.rules)

// merge with valaxy
userValaxyConfig = defu<ValaxyNodeConfig, any>({ siteConfig }, { themeConfig }, userValaxyConfig)
userValaxyConfig = replaceArrMerge<ValaxyNodeConfig, any>({ siteConfig } as ValaxyNodeConfig, { themeConfig }, userValaxyConfig)

// pages
// Important: fast-glob doesn't guarantee order of the returned files.
Expand Down
4 changes: 2 additions & 2 deletions packages/valaxy/node/plugins/valaxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { join, relative, resolve } from 'pathe'
import fs from 'fs-extra'

import type { Plugin, ResolvedConfig } from 'vite'
import { defu } from 'defu'
import pascalCase from 'pascalcase'
import type { DefaultTheme, Pkg, SiteConfig } from 'valaxy/types'
import { dim, yellow } from 'picocolors'
Expand All @@ -22,6 +21,7 @@ import { countPerformanceTime } from '../../utils/performance'
import { isProd } from '../../utils/env'
import type { PageDataPayload } from '../../../types'
import { createMarkdownToVueRenderFn } from '../markdown/markdownToVue'
import { replaceArrMerge } from '../../config/merge'

function generateConfig(options: ResolvedValaxyOptions) {
const routes = options.redirects.map<RouteRecordRaw>((redirect) => {
Expand Down Expand Up @@ -279,7 +279,7 @@ export async function createValaxyLoader(options: ResolvedValaxyOptions, serverO
// siteConfig
if (file === options.siteConfigFile) {
const { siteConfig } = await resolveSiteConfig(options.userRoot)
valaxyConfig.siteConfig = defu<SiteConfig, [SiteConfig]>(siteConfig, defaultSiteConfig)
valaxyConfig.siteConfig = replaceArrMerge<SiteConfig, [SiteConfig]>(siteConfig as SiteConfig, defaultSiteConfig)
return reloadConfigAndEntries(valaxyConfig)
}

Expand Down
2 changes: 2 additions & 0 deletions packages/valaxy/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export interface SiteConfig {
/**
* alternative languages
* @description 可选语言
* @en If you want to disable multi-language support for your site, you can set this to only include one language (e.g. `['en']`)
* @zh 如果你想要禁言站点的多语言支持,可以将此项设置为仅包含一个语言 (例如 `['zh-CN']`)
* @default ['en', 'zh-CN']
* @see https://ogp.me/#optional
*/
Expand Down

1 comment on commit 5e3e032

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://yun.valaxy.site as production
🚀 Deployed on https://672cdd29b965b8ae60a8c4db--valaxy.netlify.app

Please sign in to comment.