index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. <script setup lang="ts">
  2. import {
  3. ref,
  4. unref,
  5. watch,
  6. reactive,
  7. computed,
  8. nextTick,
  9. onUnmounted,
  10. onBeforeMount
  11. } from "vue";
  12. import panel from "../panel/index.vue";
  13. import { emitter } from "@/utils/mitt";
  14. import { useNav } from "@/layout/hooks/useNav";
  15. import { useAppStoreHook } from "@/store/modules/app";
  16. import { toggleTheme } from "@pureadmin/theme/dist/browser-utils";
  17. import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
  18. import Segmented, { type OptionsType } from "@/components/ReSegmented";
  19. import { useDataThemeChange } from "@/layout/hooks/useDataThemeChange";
  20. import { useDark, useGlobal, debounce, isNumber } from "@pureadmin/utils";
  21. import Check from "@iconify-icons/ep/check";
  22. import LeftArrow from "@iconify-icons/ri/arrow-left-s-line";
  23. import RightArrow from "@iconify-icons/ri/arrow-right-s-line";
  24. import dayIcon from "@/assets/svg/day.svg?component";
  25. import darkIcon from "@/assets/svg/dark.svg?component";
  26. import systemIcon from "@/assets/svg/system.svg?component";
  27. const { device } = useNav();
  28. const { isDark } = useDark();
  29. const { $storage } = useGlobal<GlobalPropertiesApi>();
  30. const mixRef = ref();
  31. const verticalRef = ref();
  32. const horizontalRef = ref();
  33. const {
  34. dataTheme,
  35. overallStyle,
  36. layoutTheme,
  37. themeColors,
  38. toggleClass,
  39. dataThemeChange,
  40. setLayoutThemeColor
  41. } = useDataThemeChange();
  42. /* body添加layout属性,作用于src/style/sidebar.scss */
  43. if (unref(layoutTheme)) {
  44. const layout = unref(layoutTheme).layout;
  45. const theme = unref(layoutTheme).theme;
  46. toggleTheme({
  47. scopeName: `layout-theme-${theme}`
  48. });
  49. setLayoutModel(layout);
  50. }
  51. /** 默认灵动模式 */
  52. const markValue = ref($storage.configure?.showModel ?? "smart");
  53. const logoVal = ref($storage.configure?.showLogo ?? true);
  54. const settings = reactive({
  55. greyVal: $storage.configure.grey,
  56. weakVal: $storage.configure.weak,
  57. tabsVal: $storage.configure.hideTabs,
  58. showLogo: $storage.configure.showLogo,
  59. showModel: $storage.configure.showModel,
  60. hideFooter: $storage.configure.hideFooter,
  61. multiTagsCache: $storage.configure.multiTagsCache,
  62. stretch: $storage.configure.stretch
  63. });
  64. const getThemeColorStyle = computed(() => {
  65. return color => {
  66. return { background: color };
  67. };
  68. });
  69. /** 当网页整体为暗色风格时不显示亮白色主题配色切换选项 */
  70. const showThemeColors = computed(() => {
  71. return themeColor => {
  72. return themeColor === "light" && isDark.value ? false : true;
  73. };
  74. });
  75. function storageConfigureChange<T>(key: string, val: T): void {
  76. const storageConfigure = $storage.configure;
  77. storageConfigure[key] = val;
  78. $storage.configure = storageConfigure;
  79. }
  80. /** 灰色模式设置 */
  81. const greyChange = (value): void => {
  82. const htmlEl = document.querySelector("html");
  83. toggleClass(settings.greyVal, "html-grey", htmlEl);
  84. storageConfigureChange("grey", value);
  85. };
  86. /** 色弱模式设置 */
  87. const weekChange = (value): void => {
  88. const htmlEl = document.querySelector("html");
  89. toggleClass(settings.weakVal, "html-weakness", htmlEl);
  90. storageConfigureChange("weak", value);
  91. };
  92. /** 隐藏标签页设置 */
  93. const tagsChange = () => {
  94. const showVal = settings.tabsVal;
  95. storageConfigureChange("hideTabs", showVal);
  96. emitter.emit("tagViewsChange", showVal as unknown as string);
  97. };
  98. /** 隐藏页脚设置 */
  99. const hideFooterChange = () => {
  100. const hideFooter = settings.hideFooter;
  101. storageConfigureChange("hideFooter", hideFooter);
  102. };
  103. /** 标签页持久化设置 */
  104. const multiTagsCacheChange = () => {
  105. const multiTagsCache = settings.multiTagsCache;
  106. storageConfigureChange("multiTagsCache", multiTagsCache);
  107. useMultiTagsStoreHook().multiTagsCacheChange(multiTagsCache);
  108. };
  109. function onChange({ option }) {
  110. const { value } = option;
  111. markValue.value = value;
  112. storageConfigureChange("showModel", value);
  113. emitter.emit("tagViewsShowModel", value);
  114. }
  115. /** 侧边栏Logo */
  116. function logoChange() {
  117. unref(logoVal)
  118. ? storageConfigureChange("showLogo", true)
  119. : storageConfigureChange("showLogo", false);
  120. emitter.emit("logoChange", unref(logoVal));
  121. }
  122. function setFalse(Doms): any {
  123. Doms.forEach(v => {
  124. toggleClass(false, "is-select", unref(v));
  125. });
  126. }
  127. /** 页宽 */
  128. const stretchTypeOptions: Array<OptionsType> = [
  129. {
  130. label: "固定",
  131. tip: "紧凑页面,轻松找到所需信息",
  132. value: "fixed"
  133. },
  134. {
  135. label: "自定义",
  136. tip: "最小1280、最大1600",
  137. value: "custom"
  138. }
  139. ];
  140. const setStretch = value => {
  141. settings.stretch = value;
  142. storageConfigureChange("stretch", value);
  143. };
  144. const stretchTypeChange = ({ option }) => {
  145. const { value } = option;
  146. value === "custom" ? setStretch(1440) : setStretch(false);
  147. };
  148. /** 主题色 激活选择项 */
  149. const getThemeColor = computed(() => {
  150. return current => {
  151. if (
  152. current === layoutTheme.value.theme &&
  153. layoutTheme.value.theme !== "light"
  154. ) {
  155. return "#fff";
  156. } else if (
  157. current === layoutTheme.value.theme &&
  158. layoutTheme.value.theme === "light"
  159. ) {
  160. return "#1d2b45";
  161. } else {
  162. return "transparent";
  163. }
  164. };
  165. });
  166. const pClass = computed(() => {
  167. return ["mb-[12px]", "font-medium", "text-sm", "dark:text-white"];
  168. });
  169. const themeOptions = computed<Array<OptionsType>>(() => {
  170. return [
  171. {
  172. label: "浅色",
  173. icon: dayIcon,
  174. theme: "light",
  175. tip: "清新启航,点亮舒适的工作界面",
  176. iconAttrs: { fill: isDark.value ? "#fff" : "#000" }
  177. },
  178. {
  179. label: "深色",
  180. icon: darkIcon,
  181. theme: "dark",
  182. tip: "月光序曲,沉醉于夜的静谧雅致",
  183. iconAttrs: { fill: isDark.value ? "#fff" : "#000" }
  184. },
  185. {
  186. label: "自动",
  187. icon: systemIcon,
  188. theme: "system",
  189. tip: "同步时光,界面随晨昏自然呼应",
  190. iconAttrs: { fill: isDark.value ? "#fff" : "#000" }
  191. }
  192. ];
  193. });
  194. const markOptions: Array<OptionsType> = [
  195. {
  196. label: "灵动",
  197. tip: "灵动标签,添趣生辉",
  198. value: "smart"
  199. },
  200. {
  201. label: "卡片",
  202. tip: "卡片标签,高效浏览",
  203. value: "card"
  204. }
  205. ];
  206. /** 设置导航模式 */
  207. function setLayoutModel(layout: string) {
  208. layoutTheme.value.layout = layout;
  209. window.document.body.setAttribute("layout", layout);
  210. $storage.layout = {
  211. layout,
  212. theme: layoutTheme.value.theme,
  213. darkMode: $storage.layout?.darkMode,
  214. sidebarStatus: $storage.layout?.sidebarStatus,
  215. epThemeColor: $storage.layout?.epThemeColor,
  216. themeColor: $storage.layout?.themeColor,
  217. overallStyle: $storage.layout?.overallStyle
  218. };
  219. useAppStoreHook().setLayout(layout);
  220. }
  221. watch($storage, ({ layout }) => {
  222. switch (layout["layout"]) {
  223. case "vertical":
  224. toggleClass(true, "is-select", unref(verticalRef));
  225. debounce(setFalse([horizontalRef]), 50);
  226. debounce(setFalse([mixRef]), 50);
  227. break;
  228. case "horizontal":
  229. toggleClass(true, "is-select", unref(horizontalRef));
  230. debounce(setFalse([verticalRef]), 50);
  231. debounce(setFalse([mixRef]), 50);
  232. break;
  233. case "mix":
  234. toggleClass(true, "is-select", unref(mixRef));
  235. debounce(setFalse([verticalRef]), 50);
  236. debounce(setFalse([horizontalRef]), 50);
  237. break;
  238. }
  239. });
  240. const mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)");
  241. /** 根据操作系统主题设置平台整体风格 */
  242. function updateTheme() {
  243. if (overallStyle.value !== "system") return;
  244. if (mediaQueryList.matches) {
  245. dataTheme.value = true;
  246. } else {
  247. dataTheme.value = false;
  248. }
  249. dataThemeChange(overallStyle.value);
  250. }
  251. function removeMatchMedia() {
  252. mediaQueryList.removeEventListener("change", updateTheme);
  253. }
  254. /** 监听操作系统主题改变 */
  255. function watchSystemThemeChange() {
  256. updateTheme();
  257. removeMatchMedia();
  258. mediaQueryList.addEventListener("change", updateTheme);
  259. }
  260. onBeforeMount(() => {
  261. /* 初始化项目配置 */
  262. nextTick(() => {
  263. watchSystemThemeChange();
  264. settings.greyVal &&
  265. document.querySelector("html")?.classList.add("html-grey");
  266. settings.weakVal &&
  267. document.querySelector("html")?.classList.add("html-weakness");
  268. settings.tabsVal && tagsChange();
  269. settings.hideFooter && hideFooterChange();
  270. });
  271. });
  272. onUnmounted(() => removeMatchMedia);
  273. </script>
  274. <template>
  275. <panel>
  276. <div class="p-5">
  277. <p :class="pClass">整体风格</p>
  278. <Segmented
  279. class="select-none"
  280. :modelValue="overallStyle === 'system' ? 2 : dataTheme ? 1 : 0"
  281. :options="themeOptions"
  282. @change="
  283. theme => {
  284. theme.index === 1 && theme.index !== 2
  285. ? (dataTheme = true)
  286. : (dataTheme = false);
  287. overallStyle = theme.option.theme;
  288. dataThemeChange(theme.option.theme);
  289. theme.index === 2 && watchSystemThemeChange();
  290. }
  291. "
  292. />
  293. <p :class="['mt-5', pClass]">主题色</p>
  294. <ul class="theme-color">
  295. <li
  296. v-for="(item, index) in themeColors"
  297. v-show="showThemeColors(item.themeColor)"
  298. :key="index"
  299. :style="getThemeColorStyle(item.color)"
  300. @click="setLayoutThemeColor(item.themeColor)"
  301. >
  302. <el-icon
  303. style="margin: 0.1em 0.1em 0 0"
  304. :size="17"
  305. :color="getThemeColor(item.themeColor)"
  306. >
  307. <IconifyIconOffline :icon="Check" />
  308. </el-icon>
  309. </li>
  310. </ul>
  311. <p :class="['mt-5', pClass]">导航模式</p>
  312. <ul class="pure-theme">
  313. <li
  314. ref="verticalRef"
  315. v-tippy="{
  316. content: '左侧菜单,亲切熟悉',
  317. zIndex: 41000
  318. }"
  319. :class="layoutTheme.layout === 'vertical' ? 'is-select' : ''"
  320. @click="setLayoutModel('vertical')"
  321. >
  322. <div />
  323. <div />
  324. </li>
  325. <li
  326. v-if="device !== 'mobile'"
  327. ref="horizontalRef"
  328. v-tippy="{
  329. content: '顶部菜单,简洁概览',
  330. zIndex: 41000
  331. }"
  332. :class="layoutTheme.layout === 'horizontal' ? 'is-select' : ''"
  333. @click="setLayoutModel('horizontal')"
  334. >
  335. <div />
  336. <div />
  337. </li>
  338. <li
  339. v-if="device !== 'mobile'"
  340. ref="mixRef"
  341. v-tippy="{
  342. content: '混合菜单,灵活多变',
  343. zIndex: 41000
  344. }"
  345. :class="layoutTheme.layout === 'mix' ? 'is-select' : ''"
  346. @click="setLayoutModel('mix')"
  347. >
  348. <div />
  349. <div />
  350. </li>
  351. </ul>
  352. <span v-if="device !== 'mobile'">
  353. <p :class="['mt-5', pClass]">页宽</p>
  354. <Segmented
  355. class="mb-2 select-none"
  356. :modelValue="isNumber(settings.stretch) ? 1 : 0"
  357. :options="stretchTypeOptions"
  358. @change="stretchTypeChange"
  359. />
  360. <el-input-number
  361. v-if="isNumber(settings.stretch)"
  362. v-model="settings.stretch as number"
  363. :min="1280"
  364. :max="1600"
  365. controls-position="right"
  366. @change="value => setStretch(value)"
  367. />
  368. <button
  369. v-else
  370. v-ripple="{ class: 'text-gray-300' }"
  371. class="bg-transparent flex-c w-full h-20 rounded-md border border-gray-100"
  372. @click="setStretch(!settings.stretch)"
  373. >
  374. <div
  375. class="flex-bc transition-all duration-300"
  376. :class="[settings.stretch ? 'w-[24%]' : 'w-[50%]']"
  377. style="color: var(--el-color-primary)"
  378. >
  379. <IconifyIconOffline
  380. :icon="settings.stretch ? LeftArrow : RightArrow"
  381. height="20"
  382. />
  383. <div
  384. class="flex-grow border-b border-dashed"
  385. style="border-color: var(--el-color-primary)"
  386. />
  387. <IconifyIconOffline
  388. :icon="settings.stretch ? RightArrow : LeftArrow"
  389. height="20"
  390. />
  391. </div>
  392. </button>
  393. </span>
  394. <p :class="['mt-4', pClass]">页签风格</p>
  395. <Segmented
  396. class="select-none"
  397. :modelValue="markValue === 'smart' ? 0 : 1"
  398. :options="markOptions"
  399. @change="onChange"
  400. />
  401. <p class="mt-5 font-medium text-sm dark:text-white">界面显示</p>
  402. <ul class="setting">
  403. <li>
  404. <span class="dark:text-white">灰色模式</span>
  405. <el-switch
  406. v-model="settings.greyVal"
  407. inline-prompt
  408. active-text="开"
  409. inactive-text="关"
  410. @change="greyChange"
  411. />
  412. </li>
  413. <li>
  414. <span class="dark:text-white">色弱模式</span>
  415. <el-switch
  416. v-model="settings.weakVal"
  417. inline-prompt
  418. active-text="开"
  419. inactive-text="关"
  420. @change="weekChange"
  421. />
  422. </li>
  423. <li>
  424. <span class="dark:text-white">隐藏标签页</span>
  425. <el-switch
  426. v-model="settings.tabsVal"
  427. inline-prompt
  428. active-text="开"
  429. inactive-text="关"
  430. @change="tagsChange"
  431. />
  432. </li>
  433. <li>
  434. <span class="dark:text-white">隐藏页脚</span>
  435. <el-switch
  436. v-model="settings.hideFooter"
  437. inline-prompt
  438. active-text="开"
  439. inactive-text="关"
  440. @change="hideFooterChange"
  441. />
  442. </li>
  443. <li>
  444. <span class="dark:text-white">Logo</span>
  445. <el-switch
  446. v-model="logoVal"
  447. inline-prompt
  448. :active-value="true"
  449. :inactive-value="false"
  450. active-text="开"
  451. inactive-text="关"
  452. @change="logoChange"
  453. />
  454. </li>
  455. <li>
  456. <span class="dark:text-white">页签持久化</span>
  457. <el-switch
  458. v-model="settings.multiTagsCache"
  459. inline-prompt
  460. active-text="开"
  461. inactive-text="关"
  462. @change="multiTagsCacheChange"
  463. />
  464. </li>
  465. </ul>
  466. </div>
  467. </panel>
  468. </template>
  469. <style lang="scss" scoped>
  470. :deep(.el-divider__text) {
  471. font-size: 16px;
  472. font-weight: 700;
  473. }
  474. :deep(.el-switch__core) {
  475. --el-switch-off-color: var(--pure-switch-off-color);
  476. min-width: 36px;
  477. height: 18px;
  478. }
  479. :deep(.el-switch__core .el-switch__action) {
  480. height: 14px;
  481. }
  482. .theme-color {
  483. height: 20px;
  484. li {
  485. float: left;
  486. height: 20px;
  487. margin-right: 8px;
  488. cursor: pointer;
  489. border-radius: 4px;
  490. &:nth-child(1) {
  491. border: 1px solid #ddd;
  492. }
  493. }
  494. }
  495. .pure-theme {
  496. display: flex;
  497. gap: 12px;
  498. li {
  499. position: relative;
  500. width: 46px;
  501. height: 36px;
  502. overflow: hidden;
  503. cursor: pointer;
  504. background: #f0f2f5;
  505. border-radius: 4px;
  506. box-shadow: 0 1px 2.5px 0 rgb(0 0 0 / 18%);
  507. &:nth-child(1) {
  508. div {
  509. &:nth-child(1) {
  510. width: 30%;
  511. height: 100%;
  512. background: #1b2a47;
  513. }
  514. &:nth-child(2) {
  515. position: absolute;
  516. top: 0;
  517. right: 0;
  518. width: 70%;
  519. height: 30%;
  520. background: #fff;
  521. box-shadow: 0 0 1px #888;
  522. }
  523. }
  524. }
  525. &:nth-child(2) {
  526. div {
  527. &:nth-child(1) {
  528. width: 100%;
  529. height: 30%;
  530. background: #1b2a47;
  531. box-shadow: 0 0 1px #888;
  532. }
  533. }
  534. }
  535. &:nth-child(3) {
  536. div {
  537. &:nth-child(1) {
  538. width: 100%;
  539. height: 30%;
  540. background: #1b2a47;
  541. box-shadow: 0 0 1px #888;
  542. }
  543. &:nth-child(2) {
  544. position: absolute;
  545. bottom: 0;
  546. left: 0;
  547. width: 30%;
  548. height: 70%;
  549. background: #fff;
  550. box-shadow: 0 0 1px #888;
  551. }
  552. }
  553. }
  554. }
  555. }
  556. .is-select {
  557. border: 2px solid var(--el-color-primary);
  558. }
  559. .setting {
  560. li {
  561. display: flex;
  562. align-items: center;
  563. justify-content: space-between;
  564. padding: 3px 0;
  565. font-size: 14px;
  566. }
  567. }
  568. </style>