123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- const fs = require("fire-fs");
- const { shell, ipcRenderer } = require("electron");
- const http = Editor.require("packages://im-plugin/core/http.js");
- const { tr } = Editor.require('packages://im-plugin/core/translate.js');
- const { readJson } = Editor.require('packages://im-plugin/core/utils');
- const pluginVersion = readJson(Editor.url("packages://im-plugin/package.json")).version;
- let forceClose = false;
- Editor.Panel.extend({
- listeners: {},
- template: fs.readFileSync(
- Editor.url("packages://im-plugin/panel/index.html"),
- "utf-8"
- ),
- messages: {
- "im-plugin:force-close"(event) {
- forceClose = true;
- Editor.Ipc.sendToMain("im-plugin:close");
- },
- },
- $: {
- im_webview: "#im-webview",
- loading: "#imPlugin-loading",
- },
- // 阻止关闭
- // 2.4.0增加canClose(),旧版本只能用close(),故这里代码写了两遍
- close(event) {
- if (forceClose) {
- return true;
- }
- Editor.Ipc.sendToMain("im-plugin:hide");
- return false;
- },
- canClose() {
- if (forceClose) {
- return true;
- }
- Editor.Ipc.sendToMain("im-plugin:hide");
- return false;
- },
- async ready() {
- forceClose = false;
- const isLogin = await Editor.User.isLoggedIn();
- if (!isLogin) {
- this.showInfoAndForceClose("info-non-logged-in");
- return;
- }
- !window._Scene &&
- (document.title = Editor.T("im-plugin.title") + ` v${pluginVersion}`);
- // 注入 ipcRenderer 的 sendToHost 接口,提供给 webview 内嵌页面使用
- this.$im_webview.setAttribute(
- "preload",
- `file://${Editor.url("packages://im-plugin/panel/preload.js")}`
- );
- // 打开 webview 内嵌页面的开发者工具
- // this.$im_webview.addEventListener("dom-ready", () =>
- // this.$im_webview.openDevTools()
- // );
- // 清空webview缓存
- const webContents = this.$im_webview.getWebContents();
- typeof webContents !== "undefined" &&
- webContents.session.clearStorageData([
- "appcache",
- "cookies",
- "filesystem",
- "indexdb",
- "localstorage",
- "shadercache",
- "websql",
- "serviceworkers",
- "cachestorage",
- ]);
- // 注册 ipc 消息
- this.$im_webview.addEventListener("ipc-message", (event) => {
- switch (event.channel) {
- case "new-msg":
- Editor.Ipc.sendToMain(
- "im-plugin:update-icon",
- event.args[0].newMsgCount
- );
- break;
- // 加载完毕,隐藏loading状态
- case "is-ready":
- this.$loading.style.display = "none";
- break;
- case "download":
- http.download(event.args[0].url, event.args[0].path, () => {});
- break;
- default:
- break;
- }
- });
- this.$im_webview.addEventListener("new-window", (event) => {
- shell.openExternal(
- `https://creator-api.cocos.com/api/account/client_signin?session_id=${
- http.userInfo.session_id
- }&redirect_url=${encodeURIComponent(event.url)}`
- );
- });
- // 暂时每次都reload,避免session的改变,URL只会在面板第一次打开时获取,
- // 之后面板会一直保持显示或者隐藏直到用户退出或者关闭Creator,
- // 故init的调用是不频繁的。
- await http.init(true);
- const entryUrl = ipcRenderer.sendSync(
- "im-plugin:get-entry-url"
- );
- if (!entryUrl) {
- this.showInfoAndForceClose("get-entry-url-failed");
- return;
- }
- this.openUrl = `https://creator-api.cocos.com/api/account/client_signin?session_id=${
- http.userInfo.session_id
- }&redirect_url=${encodeURIComponent(entryUrl)}%3flang%3d${Editor.lang}`;
- this.$im_webview.src = this.openUrl;
- // 将窗口状态IPC消息转发到 webview 中
- ipcRenderer.on("im-plugin:panel-win-state-changed", (event, eventName) => {
- // 当用户登出,面板被强制关闭的时候,webview的DOM会被销毁,此时发消息会产生异常,这里捕获并忽略该异常
- try {
- this.$im_webview.send("im-plugin:panel-win-state-changed", eventName);
- } catch (_) {}
- });
- Editor.Ipc.sendToMain("im-plugin:panel-ready");
- },
- showInfoAndForceClose(trMsg) {
- Editor.Dialog.messageBox({
- title: tr("info-title"),
- message: tr(trMsg),
- buttons: [tr("ok")],
- defaultId: 0,
- noLink: true,
- });
- forceClose = true;
- Editor.Ipc.sendToMain("im-plugin:close");
- },
- });
|