main.js 9.9 KB
Newer Older
1 2
import { clipboard, ipcRenderer, remote } from "electron";
import { v4 as uuidv4 } from "uuid";
M
muwoo 已提交
3 4 5 6 7
import {
  getWindowHeight,
  searchKeyValues,
  sysFile,
  mergePlugins,
M
muwoo 已提交
8
  find,
M
muwoo 已提交
9
  downloadZip,
郭维嘉 已提交
10
  fileLists
11 12
} from "../../assets/common/utils";
import systemMethod from "../../assets/common/system";
郭维嘉 已提交
13
import fs, { stat } from "fs";
14 15
import path from "path";
import { execSync } from "child_process";
M
init  
muwoo 已提交
16

郭维嘉 已提交
17 18
const opConfig = remote.getGlobal("opConfig");

M
init  
muwoo 已提交
19 20 21 22
const state = {
  selected: null,
  options: [],
  showMain: false,
23 24
  current: ["market"],
  searchValue: "",
M
muwoo 已提交
25
  devPlugins: mergePlugins(sysFile.getUserPlugins() || []),
26
  subPlaceHolder: "",
郭维嘉 已提交
27
  userInfo: { ...opConfig.get() }.userInfo,
郭维嘉 已提交
28
  webviewPath: "https://so.csdn.net/so/search?q=csdn",
M
muwoo 已提交
29
  pluginLoading: true,
M
muwoo 已提交
30 31
  pluginInfo: (() => {
    try {
32
      return window.pluginInfo || {};
M
muwoo 已提交
33
    } catch (e) {}
郭维嘉 已提交
34
  })()
35
};
M
init  
muwoo 已提交
36 37

const mutations = {
郭维嘉 已提交
38 39 40 41 42 43
  setUserInfo(state, info) {
    state.userInfo = info;
  },
  changeWebviewPath(state, path) {
    state.webviewPath = path;
  },
44
  commonUpdate(state, payload) {
郭维嘉 已提交
45
    Object.keys(payload).forEach(key => {
M
init  
muwoo 已提交
46
      state[key] = payload[key];
47
      if (key === "devPlugins") {
48
        sysFile.savePlugins(payload[key]);
M
init  
muwoo 已提交
49 50 51
      }
    });
  },
M
muwoo 已提交
52 53 54
  setSubPlaceHolder(state, payload) {
    state.subPlaceHolder = payload;
  },
M
muwoo 已提交
55
  deleteDevPlugin(state, payload) {
56
    state.devPlugins = state.devPlugins.filter(
郭维嘉 已提交
57
      plugin => plugin.name !== payload.name
58
    );
M
muwoo 已提交
59 60 61
    sysFile.savePlugins(state.devPlugins);
  },
  deleteProdPlugin(state, payload) {
62
    state.devPlugins = state.devPlugins.filter(
郭维嘉 已提交
63
      plugin => plugin.id !== payload.id
64
    );
M
muwoo 已提交
65 66 67 68
    sysFile.savePlugins(state.devPlugins);
    // todo 删除 static 目录下的对应插件
  },
  devPluginStatusChange(state, payload) {
郭维嘉 已提交
69
    state.devPlugins.forEach(plugin => {
M
muwoo 已提交
70 71 72 73 74 75
      if (plugin.name === payload.name) {
        plugin.status = !plugin.status;
      }
    });
    state.devPlugins = [...state.devPlugins];
    sysFile.savePlugins(state.devPlugins);
郭维嘉 已提交
76
  }
77
};
M
init  
muwoo 已提交
78 79

const actions = {
T
tcsnzh 已提交
80
  showMainUI({ commit, state }, payload) {
81
    ipcRenderer.send("changeWindowSize-rubick", {
郭维嘉 已提交
82
      height: getWindowHeight()
M
init  
muwoo 已提交
83
    });
M
muwoo 已提交
84
    setTimeout(() => {
85
      commit("commonUpdate", {
M
muwoo 已提交
86 87
        showMain: true,
        selected: {
88
          key: "market",
郭维嘉 已提交
89 90
          name: "插件中心"
        }
M
muwoo 已提交
91 92
      });
    }, 50);
M
init  
muwoo 已提交
93
  },
M
muwoo 已提交
94
  reloadDevPlugin({ commit }, payload) {
95 96 97
    const config = JSON.parse(
      fs.readFileSync(path.join(payload.sourceFile, "../plugin.json"), "utf-8")
    );
M
muwoo 已提交
98 99
    const pluginConfig = {
      ...config,
郭维嘉 已提交
100
      sourceFile: path.join(payload.sourceFile, `../${config.main}`)
M
muwoo 已提交
101 102
    };
    const devPlugins = [...state.devPlugins];
103
    commit("commonUpdate", {
郭维嘉 已提交
104
      devPlugins: devPlugins.map(plugin => {
M
muwoo 已提交
105 106 107
        if (plugin.name === payload.name) {
          return {
            ...plugin,
郭维嘉 已提交
108
            ...pluginConfig
109
          };
M
muwoo 已提交
110 111
        }
        return plugin;
郭维嘉 已提交
112
      })
113
    });
M
muwoo 已提交
114
  },
115 116 117 118 119 120
  /**
   * @param {Object} payload payload.filePath为配置文件的绝对路径。payload.value为搜索栏文字值。
   */
  async onSearch({ commit }, payload) {
    if (state.selected && state.selected.key !== "plugin-container") {
      commit("commonUpdate", { searchValue: "" });
M
muwoo 已提交
121 122
      return;
    }
123
    const value = payload.value;
124
    // 在插件界面不触发其他功能
125 126 127 128 129
    if (
      (state.selected && state.selected.key === "plugin-container") ||
      payload.searchType === "subWindow"
    ) {
      commit("commonUpdate", { searchValue: value });
M
muwoo 已提交
130 131
      return;
    }
132 133 134 135
    const fileUrl =
      payload.filePath ||
      clipboard.read("public.file-url").replace("file://", "");
    commit("commonUpdate", { searchValue: value });
M
init  
muwoo 已提交
136
    // 复制文件
137 138
    if (payload.filePath || (fileUrl && value === "plugin.json")) {
      const config = JSON.parse(fs.readFileSync(fileUrl, "utf-8"));
M
init  
muwoo 已提交
139 140

      const pluginConfig = {
M
muwoo 已提交
141
        ...config,
142
        sourceFile: path.join(fileUrl, `../${config.main || "index.html"}`),
M
muwoo 已提交
143
        id: uuidv4(),
144 145
        type: "dev",
        icon: "image://" + path.join(fileUrl, `../${config.logo}`),
M
muwoo 已提交
146 147
        subType: (() => {
          if (config.main) {
148
            return "";
M
muwoo 已提交
149
          }
150
          return "template";
郭维嘉 已提交
151
        })()
M
init  
muwoo 已提交
152
      };
153
      commit("commonUpdate", {
M
init  
muwoo 已提交
154
        selected: {
155
          key: "plugin",
郭维嘉 已提交
156
          name: "plugin.json"
M
init  
muwoo 已提交
157
        },
158
        searchValue: "",
M
init  
muwoo 已提交
159 160
        options: [
          {
161 162
            name: "新建rubick开发插件",
            value: "new-plugin",
郭维嘉 已提交
163
            icon: "https://static.91jkys.com/activity/img/b37ff555c748489f88f3adac15b76f18.png",
164
            desc: "新建rubick开发插件",
郭维嘉 已提交
165
            click: router => {
166
              commit("commonUpdate", {
M
init  
muwoo 已提交
167
                showMain: true,
M
muwoo 已提交
168
                devPlugins: [pluginConfig, ...state.devPlugins],
M
init  
muwoo 已提交
169
                selected: {
170
                  key: "plugin",
郭维嘉 已提交
171
                  name: "新建rubick开发插件"
M
init  
muwoo 已提交
172
                },
郭维嘉 已提交
173
                current: ["dev"]
M
init  
muwoo 已提交
174
              });
175
              ipcRenderer.send("changeWindowSize-rubick", {
郭维嘉 已提交
176
                height: getWindowHeight()
M
init  
muwoo 已提交
177
              });
178
              router.push("/home/dev");
郭维嘉 已提交
179
            }
M
init  
muwoo 已提交
180 181
          },
          {
182 183 184
            name: "复制路径",
            desc: "复制路径",
            value: "copy-path",
郭维嘉 已提交
185
            icon: "https://static.91jkys.com/activity/img/ac0d4df0247345b9a84c8cd7ea3dd696.png",
M
init  
muwoo 已提交
186
            click: () => {
M
muwoo 已提交
187
              clipboard.writeText(fileUrl);
188
              commit("commonUpdate", {
M
muwoo 已提交
189 190
                showMain: false,
                selected: null,
郭维嘉 已提交
191
                options: []
M
muwoo 已提交
192
              });
193
              ipcRenderer.send("changeWindowSize-rubick", {
郭维嘉 已提交
194
                height: getWindowHeight([])
M
muwoo 已提交
195
              });
196
              remote.Notification("Rubick 通知", { body: "复制成功" });
郭维嘉 已提交
197 198 199
            }
          }
        ]
M
init  
muwoo 已提交
200 201
      });
      // 调整窗口大小
202
      ipcRenderer.send("changeWindowSize-rubick", {
郭维嘉 已提交
203
        height: getWindowHeight(state.options)
M
init  
muwoo 已提交
204
      });
205
      return;
M
init  
muwoo 已提交
206 207 208 209 210
    }

    let options = [];

    // check 是否是插件
M
muwoo 已提交
211
    if (value) {
郭维嘉 已提交
212
      state.devPlugins.forEach(plugin => {
M
muwoo 已提交
213
        // dev 插件未开启
214
        if (plugin.type === "dev" && !plugin.status) return;
M
muwoo 已提交
215
        const feature = plugin.features;
郭维嘉 已提交
216
        feature.forEach(fe => {
M
muwoo 已提交
217 218 219
          const cmds = searchKeyValues(fe.cmds, value);
          options = [
            ...options,
郭维嘉 已提交
220
            ...cmds.map(cmd => ({
M
muwoo 已提交
221
              name: cmd,
222 223 224 225
              value: "plugin",
              icon: plugin.sourceFile
                ? "image://" + path.join(plugin.sourceFile, `../${plugin.logo}`)
                : plugin.logo,
M
muwoo 已提交
226
              desc: fe.explain,
M
muwoo 已提交
227
              type: plugin.type,
郭维嘉 已提交
228
              click: router => {
229 230 231 232
                actions.openPlugin(
                  { commit },
                  { cmd, plugin, feature: fe, router }
                );
郭维嘉 已提交
233 234
              }
            }))
235 236
          ];
        });
M
muwoo 已提交
237
      });
238 239

      let descMap = new Map();
M
muwoo 已提交
240 241
      options = [
        ...options,
242
        ...fileLists
郭维嘉 已提交
243
          .filter(plugin => {
244 245
            if (!descMap.get(plugin)) {
              descMap.set(plugin, true);
M
muwoo 已提交
246
              let has = false;
郭维嘉 已提交
247
              plugin.keyWords.some(keyWord => {
248 249 250 251 252
                if (
                  keyWord
                    .toLocaleUpperCase()
                    .indexOf(value.toLocaleUpperCase()) >= 0
                ) {
M
muwoo 已提交
253 254 255 256 257 258 259
                  has = keyWord;
                  plugin.name = keyWord;
                  return true;
                }
                return false;
              });
              return has;
260 261 262 263
            } else {
              return false;
            }
          })
郭维嘉 已提交
264
          .map(plugin => {
265 266 267 268
            plugin.click = () => {
              actions.openPlugin({ commit }, { plugin });
            };
            return plugin;
郭维嘉 已提交
269
          })
270 271 272
      ];

      descMap = null;
M
muwoo 已提交
273
    }
M
init  
muwoo 已提交
274

275
    commit("commonUpdate", {
郭维嘉 已提交
276
      options
M
init  
muwoo 已提交
277
    });
278
    ipcRenderer.send("changeWindowSize-rubick", {
郭维嘉 已提交
279
      height: getWindowHeight(state.options)
M
init  
muwoo 已提交
280 281
    });
  },
282
  async downloadPlugin({ commit }, payload) {
M
muwoo 已提交
283 284 285
    const distUrl = await downloadZip(payload.downloadUrl, payload.name);
    const fileUrl = find(distUrl);

M
init  
muwoo 已提交
286
    // 复制文件
287 288 289
    const config = JSON.parse(
      fs.readFileSync(`${fileUrl}/plugin.json`, "utf-8")
    );
M
init  
muwoo 已提交
290 291
    const pluginConfig = {
      ...config,
M
muwoo 已提交
292
      id: uuidv4(),
M
muwoo 已提交
293
      sourceFile: `${fileUrl}/${config.main}`,
294
      type: "prod",
M
muwoo 已提交
295 296 297
      icon: payload.logo,
      subType: (() => {
        if (config.main) {
298
          return "";
M
muwoo 已提交
299
        }
300
        return "template";
郭维嘉 已提交
301
      })()
M
init  
muwoo 已提交
302
    };
303
    commit("commonUpdate", {
郭维嘉 已提交
304
      devPlugins: [pluginConfig, ...state.devPlugins]
M
init  
muwoo 已提交
305
    });
M
muwoo 已提交
306
  },
307
  openPlugin({ commit }, { cmd, plugin, feature, router, payload }) {
308
    if (plugin.type === "app") {
M
muwoo 已提交
309
      execSync(plugin.action);
310
      commit("commonUpdate", {
M
muwoo 已提交
311 312 313
        selected: null,
        showMain: false,
        options: [],
郭维嘉 已提交
314
        searchValue: ""
M
muwoo 已提交
315
      });
316
      ipcRenderer.send("changeWindowSize-rubick", {
郭维嘉 已提交
317
        height: getWindowHeight([])
M
muwoo 已提交
318 319 320
      });
      return;
    }
321
    commit("commonUpdate", {
M
muwoo 已提交
322
      selected: {
323
        key: "plugin-container",
324
        name: cmd.label ? cmd.label : cmd,
郭维嘉 已提交
325
        icon: "image://" + path.join(plugin.sourceFile, `../${plugin.logo}`)
M
muwoo 已提交
326
      },
327
      searchValue: "",
郭维嘉 已提交
328
      showMain: true
M
muwoo 已提交
329
    });
330
    ipcRenderer.send("changeWindowSize-rubick", {
郭维嘉 已提交
331
      height: getWindowHeight()
M
muwoo 已提交
332
    });
333
    if (plugin.type === "system") {
334
      systemMethod[plugin.tag][feature.code]();
335
      commit("commonUpdate", {
M
muwoo 已提交
336 337
        selected: null,
        showMain: false,
郭维嘉 已提交
338
        options: []
M
muwoo 已提交
339
      });
340
      ipcRenderer.send("changeWindowSize-rubick", {
郭维嘉 已提交
341
        height: getWindowHeight([])
M
muwoo 已提交
342 343
      });
      router.push({
郭维嘉 已提交
344
        path: "/home"
M
muwoo 已提交
345 346 347
      });
      return;
    }
348
    commit("commonUpdate", {
349 350 351 352
      pluginInfo: {
        cmd,
        ...plugin,
        detail: feature,
郭维嘉 已提交
353 354
        payload
      }
355 356
    });

M
muwoo 已提交
357
    router.push({
358
      path: "/plugin",
M
muwoo 已提交
359 360
      query: {
        ...plugin,
361
        _modify: Date.now(),
郭维嘉 已提交
362 363
        detail: JSON.stringify(feature)
      }
364
    });
郭维嘉 已提交
365
  }
366
};
M
init  
muwoo 已提交
367 368 369 370 371

export default {
  namespaced: true,
  state,
  mutations,
郭维嘉 已提交
372
  actions
373
};