wujingjing
2024-11-19 a1a6827edea6100849b28d0ef3cdc99c23d8b125
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import {reaction} from 'mobx';
import {applySnapshot, getEnv, getSnapshot, types} from 'mobx-state-tree';
import {getLowCodeJson} from '../api/admin';
import {getPageId} from '../utils/request';
import {PageStore} from './Page';
 
let pagIndex = 1;
export const MainStore = types
  .model('MainStore', {
    pages: types.optional(types.array(PageStore), [
      {
        id: `${pagIndex}`,
        path: 'hello-world',
        label: 'Hello world',
        icon: 'fa fa-file',
        schema: {
          type: 'page',
          title: 'Hello world',
          body: '初始页面'
        }
      }
    ]),
    theme: 'cxd',
    asideFixed: true,
    asideFolded: false,
    offScreen: false,
    addPageIsOpen: false,
    preview: false,
    isMobile: false,
    schema: types.frozen()
  })
  .views(self => ({
    get fetcher() {
      return getEnv(self).fetcher;
    },
    get notify() {
      return getEnv(self).notify;
    },
    get alert() {
      return getEnv(self).alert;
    },
    get copy() {
      return getEnv(self).copy;
    }
  }))
  .actions(self => {
    function toggleAsideFolded() {
      self.asideFolded = !self.asideFolded;
    }
 
    function toggleAsideFixed() {
      self.asideFixed = !self.asideFixed;
    }
 
    function toggleOffScreen() {
      self.offScreen = !self.offScreen;
    }
 
    function setAddPageIsOpen(isOpened: boolean) {
      self.addPageIsOpen = isOpened;
    }
 
    function addPage(data: {
      label: string;
      path: string;
      icon?: string;
      schema?: any;
    }) {
      self.pages.push(
        PageStore.create({
          ...data,
          id: `${++pagIndex}`
        })
      );
    }
 
    function removePageAt(index: number) {
      self.pages.splice(index, 1);
    }
 
    function updatePageSchemaAt(index: number) {
      if (index === -1) return;
      self.pages[index].updateSchema(self.schema);
    }
 
    function updateSchema(value: any) {
      self.schema = value;
    }
 
    function setPreview(value: boolean) {
      self.preview = value;
    }
 
    function setIsMobile(value: boolean) {
      self.isMobile = value;
    }
 
    return {
      toggleAsideFolded,
      toggleAsideFixed,
      toggleOffScreen,
      setAddPageIsOpen,
      addPage,
      removePageAt,
      updatePageSchemaAt,
      updateSchema,
      setPreview,
      setIsMobile,
      async afterCreate() {
        // persist store
        if (typeof window !== 'undefined' && window.localStorage) {
          // const storeData = window.localStorage.getItem('store') as any;
 
          const currentId = getPageId();
          // 先清空,防止接口权限过期,请求到旧数据
          window.localStorage.removeItem('store');
 
          const json = await getLowCodeJson({
            id: currentId
          });
          // const codeJsonList = (await Promise.all(codeJson)).map(item => {
          //   return item?.amis_json;
          // });
          const storeDataObj = {
            pages: [
              {
                id: currentId,
                icon: '',
                label: currentId,
                path: currentId,
                schema: json?.amis_json
              }
            ]
          };
          const storeData = JSON.stringify(storeDataObj);
 
          window.localStorage.setItem('store', storeData);
          if (storeData) applySnapshot(self, storeDataObj);
 
          reaction(
            () => getSnapshot(self),
            json => {
              window.localStorage.setItem('store', JSON.stringify(json));
            }
          );
        }
      }
    };
  });
 
export type IMainStore = typeof MainStore.Type;