App.svelte 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <script>
  2. import { setContext, onMount } from 'svelte';
  3. import Router, { querystring, location, push } from 'svelte-spa-router';
  4. import { routes, routes_en } from './route';
  5. import { NavBar, Icon } from '../../packages/stdf';
  6. import zh_CN from '../../packages/stdf/lang/zh_CN';
  7. import en_US from '../../packages/stdf/lang/en_US';
  8. import menuList from './data/menuList';
  9. // 循环 menuList,将所有元素的 childs 组成一个数组
  10. // Cycle menuList, and combine the childs of all elements into an array
  11. const menuListArr = menuList.reduce((acc, cur) => {
  12. if (cur.childs) {
  13. acc.push(...cur.childs);
  14. }
  15. return acc;
  16. }, []);
  17. const params = new URLSearchParams('?' + $querystring); //获取参数 get parameters
  18. const isIframe = params.get('channel') && params.get('channel') === 'iframe' ? '1' : '0'; //判断是否是iframe judge whether it is iframe
  19. setContext('iframe', isIframe); //设置iframe setting iframe
  20. let theme = localStorage.getItem('theme') === 'dark' ? 'dark' : 'light';
  21. //截取字符?后面的所有字符
  22. // intercept all characters after the character?
  23. let urlLang = window.location.href.split('?')[1];
  24. let urlParams = new URLSearchParams(urlLang);
  25. // 设置语言
  26. // setting language
  27. const isZh = sessionStorage.getItem('lang') === 'zh_CN';
  28. setContext('STDF_lang', isZh ? zh_CN : en_US);
  29. $: showLeft = isIframe === '1' ? false : $location !== '/';
  30. //手动切换主题
  31. // manually switch theme
  32. const toggleFun = () => {
  33. if (theme === 'dark') {
  34. // 切换到light
  35. // switch to light
  36. theme = 'light';
  37. localStorage.setItem('theme', 'light');
  38. document.documentElement.classList.remove('dark');
  39. } else {
  40. // 切换到dark
  41. // switch to dark
  42. theme = 'dark';
  43. localStorage.setItem('theme', 'dark');
  44. document.documentElement.classList.add('dark');
  45. }
  46. };
  47. const toHomeFun = () => {
  48. push(`/`);
  49. };
  50. const mode = import.meta.env.MODE;
  51. // 判断 mode 后三个字符是否是 _en
  52. // Determine whether the last three characters of mode are _en
  53. const englishMode = mode.slice(-3) === '_en';
  54. onMount(() => {
  55. if (mode != 'production' && mode != 'development' && mode != 'english') {
  56. // 英文模式去掉 mode 后面的 _en 作为 nav
  57. // English mode removes _en after mode as nav
  58. const nav = englishMode ? mode.slice(0, -3) : mode;
  59. push(`/${nav}`);
  60. }
  61. });
  62. </script>
  63. <main class="bg-gray9 dark:bg-gray3 text-black dark:text-white/90 relative min-h-screen text-justify w-screen antialiased">
  64. <div class="sticky z-10 top-0">
  65. <NavBar
  66. left={showLeft ? 'back' : ''}
  67. title={$location === '/'
  68. ? isZh
  69. ? 'STDF 示例'
  70. : 'STDF Demo'
  71. : menuListArr.filter(item => item.nav === $location.substring(1))[0][isZh ? 'title_zh' : 'title_en'] +
  72. (isZh ? '示例' : ' Demo')}
  73. rightSlot
  74. on:clickleft={toHomeFun}
  75. injClass="bg-white/60 dark:bg-gray1/60 backdrop-blur"
  76. >
  77. <div slot="right" class="flex text-center">
  78. {#if isIframe === '0'}
  79. <div class="h-12 w-10 leading-10">
  80. <a href="https://github.com/dufu1991/stdf" target="_blank" rel="noreferrer">
  81. <Icon name="ri-github-fill" />
  82. </a>
  83. </div>
  84. <div class="h-12 w-10 leading-10">
  85. <a
  86. href={`https://stdf.design${$location === '/' ? '' : `/#/components?nav=${$location.substring(1)}&tab=0`}`}
  87. target="_blank"
  88. rel="noreferrer"
  89. >
  90. <Icon name="ri-compass-line" />
  91. </a>
  92. </div>
  93. {/if}
  94. <!-- svelte-ignore a11y-click-events-have-key-events -->
  95. <div class="h-12 w-10 leading-10" on:click={toggleFun}>
  96. <Icon name={theme === 'dark' ? 'ri-moon-fill' : 'ri-sun-line'} theme={true} />
  97. </div>
  98. </div>
  99. </NavBar>
  100. </div>
  101. <Router routes={isZh ? routes : routes_en} />
  102. </main>