app.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { defineStore } from "pinia";
  2. import {
  3. type appType,
  4. store,
  5. getConfig,
  6. storageLocal,
  7. deviceDetection,
  8. responsiveStorageNameSpace
  9. } from "../utils";
  10. export const useAppStore = defineStore("pure-app", {
  11. state: (): appType => ({
  12. sidebar: {
  13. opened: true,
  14. withoutAnimation: false,
  15. isClickCollapse: false
  16. },
  17. // 这里的layout用于监听容器拖拉后恢复对应的菜单布局
  18. layout:
  19. storageLocal().getItem<StorageConfigs>(
  20. `${responsiveStorageNameSpace()}layout`
  21. )?.layout ?? getConfig().Layout,
  22. device: deviceDetection() ? "mobile" : "desktop",
  23. // 浏览器窗口的可视区域大小
  24. viewportSize: {
  25. width: document.documentElement.clientWidth,
  26. height: document.documentElement.clientHeight
  27. },
  28. // 作用于 src/views/components/draggable/index.vue 页面,当离开页面并不会销毁 new Swap(),sortablejs 官网也没有提供任何销毁的 api
  29. sortSwap: false
  30. }),
  31. getters: {
  32. getSidebarStatus(state) {
  33. return state.sidebar.opened;
  34. },
  35. getDevice(state) {
  36. return state.device;
  37. },
  38. getViewportWidth(state) {
  39. return state.viewportSize.width;
  40. },
  41. getViewportHeight(state) {
  42. return state.viewportSize.height;
  43. }
  44. },
  45. actions: {
  46. TOGGLE_SIDEBAR(_opened?: boolean, _resize?: string) {
  47. this.sidebar.opened = true;
  48. },
  49. async toggleSideBar(opened?: boolean, resize?: string) {
  50. await this.TOGGLE_SIDEBAR(opened, resize);
  51. },
  52. toggleDevice(device: string) {
  53. this.device = device;
  54. },
  55. setLayout(layout) {
  56. this.layout = layout;
  57. },
  58. setViewportSize(size) {
  59. this.viewportSize = size;
  60. },
  61. setSortSwap(val) {
  62. this.sortSwap = val;
  63. }
  64. }
  65. });
  66. export function useAppStoreHook() {
  67. return useAppStore(store);
  68. }