+layout.svelte 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <script lang="ts">
  2. import themes from '../data/themes/index.js';
  3. import { switchTheme } from 'stdf/theme';
  4. import Header from '$lib/header/Header.svelte';
  5. import CmdK from '$lib/cmdk/CmdK.svelte';
  6. import Fund from '$lib/fund/Fund.svelte';
  7. import { page } from '$app/stores';
  8. import { isCmdKStore, isShowFundStore } from '../store.js';
  9. import '../app.css';
  10. import { delParamsUrl } from '../utils/index.js';
  11. let { children } = $props();
  12. let showLeftNav = $state(false); //是否显示左侧导航
  13. let isHome = $page.url.pathname === '/' || $page.url.pathname === '';
  14. //路由改变时,更新左侧导航
  15. $effect(() => {
  16. showLeftNav = $page.url.pathname.startsWith('/guide') || $page.url.pathname === '/components';
  17. });
  18. // 点击顶部菜单事件
  19. const headerCmdKFun = () => {
  20. isCmdKStore.set(true);
  21. };
  22. const currentTheme = localStorage.getItem('theme_color') || 'STDF';
  23. // 设置默认主题色
  24. themes.forEach((item) => {
  25. if (item.name === currentTheme) {
  26. const theme = item.theme;
  27. switchTheme(theme);
  28. // 修改 HTML 的 meta name="theme-color" 属性,适配 Safari 的 tab 背景色,需要设置 light 和 dark 两种颜色
  29. const safariLight = theme.color.primaryWhite;
  30. const safariDark = theme.color.darkBlack;
  31. // 查找 meta 标签,name="theme-color" 且 media="(prefers-color-scheme: light)"
  32. const lightMeta = document.querySelector('meta[name="theme-color"][media="(prefers-color-scheme: light)"]');
  33. // 查找 meta 标签,name="theme-color" 且 media="(prefers-color-scheme: dark)"
  34. const darkMeta = document.querySelector('meta[name="theme-color"][media="(prefers-color-scheme: dark)"]');
  35. // 如果找到了,就修改它的 content 属性
  36. if (lightMeta) {
  37. lightMeta.setAttribute('content', safariLight);
  38. }
  39. if (darkMeta) {
  40. darkMeta.setAttribute('content', safariDark);
  41. }
  42. }
  43. });
  44. let urlLang = window.location.href.split('?')[1];
  45. if (!urlLang || (urlLang && !urlLang.includes('lang'))) {
  46. if (!localStorage.getItem('lang')) {
  47. //获取浏览器语言前两位
  48. const lang = navigator.language.substring(0, 2);
  49. if (lang === 'en') {
  50. localStorage.setItem('lang', 'en_US');
  51. } else {
  52. localStorage.setItem('lang', 'zh_CN');
  53. }
  54. }
  55. } else {
  56. let urlParams = new URLSearchParams(urlLang);
  57. localStorage.setItem('lang', urlParams.get('lang') || '');
  58. setTimeout(() => {
  59. // window.location.href = window.location.href.replace(/(\?|#)[^'"]*/, '');//去除参数
  60. const url = new URL(window.location.href);
  61. url.searchParams.delete('lang');
  62. window.history.pushState({}, '', url.toString()); //刷新页面
  63. }, 10);
  64. }
  65. if (localStorage.getItem('theme') === 'dark') {
  66. document.documentElement.classList.add('dark');
  67. } else if (localStorage.getItem('theme') === 'light') {
  68. document.documentElement.classList.remove('dark');
  69. } else {
  70. if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
  71. document.documentElement.classList.add('dark');
  72. } else {
  73. document.documentElement.classList.remove('dark');
  74. }
  75. }
  76. const isZh = localStorage.getItem('lang') === 'zh_CN';
  77. // 根据 params 判断当前 URL 是否含有 fund 参数,如果有则显示赞赏弹窗
  78. const urlFund = window.location.href.split('?')[1];
  79. const urlParams = new URLSearchParams(urlFund);
  80. const isFund = urlParams.has('fund');
  81. if (isFund) {
  82. isShowFundStore.set(true);
  83. // 去除 URL 中的 fund 参数
  84. setTimeout(() => {
  85. window.history.replaceState({}, '', delParamsUrl(window.location.href, 'fund'));
  86. }, 10);
  87. }
  88. </script>
  89. <svelte:head>
  90. <title>STDF - {isZh ? '移动 web 组件库' : 'Mobile web component library'}</title>
  91. </svelte:head>
  92. <main class="bg-primaryWhite dark:bg-darkBlack relative min-h-screen text-justify text-black antialiased dark:text-white">
  93. <Header {showLeftNav} showBottonLine={!isHome} onclickCmdK={headerCmdKFun} />
  94. <!-- <Router {routes} /> -->
  95. <!-- <div class="mx-auto max-w-[1536px]"> -->
  96. {@render children()}
  97. <!-- </div> -->
  98. <CmdK />
  99. <!-- 赞赏 -->
  100. {#if $isShowFundStore}
  101. <Fund />
  102. {/if}
  103. </main>