+layout.svelte 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <script>
  2. import { setContext, onMount } from 'svelte';
  3. // @ts-ignore
  4. import { page } from '$app/stores';
  5. // @ts-ignore
  6. import { goto } from '$app/navigation';
  7. import { NavBar, Icon } from '../../../packages/stdf/components';
  8. import zh_CN from '../../../packages/stdf/lang/zh_CN';
  9. import en_US from '../../../packages/stdf/lang/en_US';
  10. import '../app.css';
  11. import menuList from '../data/menuList';
  12. import ThemeSwitch from './components/ThemeSwitch.svelte';
  13. // 循环 menuList,将所有元素的 childs 组成一个数组
  14. // Cycle menuList, and combine the childs of all elements into an array
  15. const menuListArr = menuList.reduce((acc, cur) => {
  16. if (cur.childs) {
  17. acc.push(...cur.childs);
  18. }
  19. return acc;
  20. }, []);
  21. // 使用 `URLSearchParams` 对象来获取 URL 查询参数
  22. // Use the `URLSearchParams` object to get the URL query parameters
  23. const urlParams = new URLSearchParams($page.url.search);
  24. const channel = urlParams.get('channel');
  25. // 判断是否是 iframe
  26. // judge whether it is iframe
  27. const isIframe = channel && channel === 'iframe' ? '1' : '0';
  28. // 设置iframe
  29. // setting iframe
  30. setContext('iframe', isIframe);
  31. // 环境变量
  32. // environment variables
  33. // @ts-ignore
  34. const mode = import.meta.env.MODE;
  35. // mode 是否是指定组件模式
  36. // whether mode is specified component mode
  37. const isComponentMode = mode != 'production' && mode != 'development' && mode != 'english' ? true : false;
  38. $: showLeft = isIframe === '1' || $page.url.pathname === '/' || isComponentMode ? false : true;
  39. let theme = localStorage.getItem('theme') === 'dark' ? 'dark' : 'light';
  40. // 设置主题
  41. // Set theme
  42. if (localStorage.getItem('theme') === 'dark') {
  43. document.documentElement.classList.add('dark');
  44. } else if (localStorage.getItem('theme') === 'light') {
  45. document.documentElement.classList.remove('dark');
  46. } else {
  47. document.documentElement.classList.remove('dark');
  48. }
  49. //手动切换亮暗模式
  50. // manually switch light and dark mode
  51. const toggleFun = () => {
  52. if (theme === 'dark') {
  53. // 切换到light
  54. // switch to light
  55. theme = 'light';
  56. localStorage.setItem('theme', 'light');
  57. document.documentElement.classList.remove('dark');
  58. } else {
  59. // 切换到dark
  60. // switch to dark
  61. theme = 'dark';
  62. localStorage.setItem('theme', 'dark');
  63. document.documentElement.classList.add('dark');
  64. }
  65. };
  66. // 判断 mode 是否是 English 模式
  67. // Determine whether mode is English mode
  68. const englishMode = mode.slice(-3) === '_en' || mode === 'english';
  69. let lang = 'zh_CN';
  70. if (englishMode) {
  71. // 固定为英文
  72. // Fixed to English
  73. lang = 'en_US';
  74. } else {
  75. // 根据 url 或 urlParams 或 sessionStorage 获取语言,优先级为 urlParams > url > sessionStorage
  76. // Get the language according to the url or urlParams or sessionStorage, the priority is urlParams > url > sessionStorage
  77. // 如果 urlParams 中有 ?lang=en_US,则设置为英文,如果有 ?lang=zh_CN,则设置为中文
  78. // If there is ?lang=en_US in urlParams, set it to English, if there is ?lang=zh_CN, set it to Chinese
  79. const urlParamsLang = urlParams.get('lang');
  80. if (urlParamsLang) {
  81. lang = urlParamsLang;
  82. // 将 url 的 ?lang=en_US 或 ?lang=zh_CN 去掉
  83. // Remove ?lang=en_US or ?lang=zh_CN from url
  84. setTimeout(() => {
  85. history.replaceState(null, '', location.href.replace(`?lang=${urlParamsLang}`, ''));
  86. }, 100);
  87. } else {
  88. // 如果 URL 中包含 /en_US/ 或 /zh_CN/,则设置为英文或中文
  89. // If the URL contains /en_US/ or /zh_CN/, set it to English or Chinese
  90. const urlLang = $page.url.pathname.split('/')[1];
  91. if (urlLang === 'en_US' || urlLang === 'zh_CN') {
  92. lang = urlLang;
  93. } else {
  94. // 如果 sessionStorage 中有 lang,则设置为 sessionStorage 中的 lang
  95. // If there is lang in sessionStorage, set it to lang in sessionStorage
  96. const sessionStorageLang = sessionStorage.getItem('lang');
  97. if (sessionStorageLang) {
  98. lang = sessionStorageLang;
  99. } else {
  100. // 如果 sessionStorage 中没有 lang,则设置为中文
  101. // If there is no lang in sessionStorage, set it to Chinese
  102. lang = 'zh_CN';
  103. }
  104. }
  105. }
  106. }
  107. sessionStorage.setItem('lang', lang);
  108. const isZh = lang === 'zh_CN' ? true : false;
  109. setContext('STDF_lang', isZh ? zh_CN : en_US);
  110. onMount(() => {
  111. if (isComponentMode) {
  112. // 英文模式去掉 mode 后面的 _en 作为 nav
  113. // English mode removes _en after mode as nav
  114. const nav = englishMode ? mode.slice(0, -3) : mode;
  115. goto(`/${englishMode ? 'en_US' : 'zh_CN'}/${nav}`);
  116. }
  117. });
  118. let showTheme = false;
  119. // 切换主题
  120. // switch theme
  121. const switchThemeFunc = () => {
  122. showTheme = !showTheme;
  123. };
  124. </script>
  125. <div class="sticky z-[100] top-0">
  126. <NavBar
  127. title={$page.url.pathname === '/'
  128. ? isZh
  129. ? 'STDF 示例'
  130. : 'STDF Demo'
  131. : menuListArr.filter(item => item.nav === $page.url.pathname.substring(7))[0][isZh ? 'title_zh' : 'title_en'] +
  132. (isZh ? '示例' : ' Demo')}
  133. left={showLeft ? 'back' : 'none'}
  134. rightSlot
  135. injClass="bg-white/60 dark:bg-black/60 backdrop-blur"
  136. on:clickleft={() => window.history.back()}
  137. >
  138. <div slot="right" class="flex text-center">
  139. {#if isIframe === '0'}
  140. <div class="h-12 w-10 leading-10">
  141. <a href="https://github.com/any-tdf/stdf" target="_blank" rel="noreferrer">
  142. <Icon name="ri-github-fill" />
  143. </a>
  144. </div>
  145. <div class="h-12 w-10 leading-10">
  146. <a
  147. href={`https://stdf.design${$page.url.pathname === '/' ? '' : `/#/components?nav=${$page.url.pathname.substring(7)}&tab=0`}`}
  148. target="_blank"
  149. rel="noreferrer"
  150. >
  151. <Icon name="ri-compass-line" />
  152. </a>
  153. </div>
  154. {/if}
  155. <!-- svelte-ignore a11y-click-events-have-key-events -->
  156. <!-- svelte-ignore a11y-no-static-element-interactions -->
  157. <div class="h-12 w-10 leading-10" on:click={toggleFun}>
  158. <Icon name={theme === 'dark' ? 'ri-moon-fill' : 'ri-sun-line'} theme={true} />
  159. </div>
  160. <!-- svelte-ignore a11y-click-events-have-key-events -->
  161. <!-- svelte-ignore a11y-no-static-element-interactions -->
  162. <div class="h-12 w-10 leading-10" on:click={switchThemeFunc}>
  163. <Icon name="ri-palette-line" theme={true} />
  164. </div>
  165. </div>
  166. </NavBar>
  167. </div>
  168. <slot />
  169. <div
  170. class="fixed z-[1000] {showTheme
  171. ? 'right-0'
  172. : '-right-80'} transition-all duration-500 top-12 bg-white dark:bg-black px-4 shadow rounded-xl"
  173. >
  174. <ThemeSwitch />
  175. </div>