App.svelte 4.3 KB

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