Skip to content

Commit

Permalink
更新记录
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyihl committed Sep 6, 2024
1 parent e23321a commit c26502e
Show file tree
Hide file tree
Showing 10 changed files with 619 additions and 25 deletions.
22 changes: 11 additions & 11 deletions dist/main.js

Large diffs are not rendered by default.

43 changes: 32 additions & 11 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { ipcMain, app, shell, BrowserWindow } = require("electron");
const { fetchOptions } = require("./main_models/options.js");
const { UorI } = require("./main_models/pluginApi.js");
const { fetchOptions } = require("./main_modules/options.js");
const { UorI } = require("./main_modules/pluginApi.js");
const fetch = require("node-fetch");
const path = require("path");

Expand All @@ -10,14 +10,18 @@ let apiLimit = false; // github API调用是否被限制

app.whenReady().then(() => {
if(!LiteLoader.plugins["protocio"]) return;
LiteLoader.api.registerUrlHandler("plugininstaller", (rest, all) => { openPluginInfoWindow(`https://raw.githubusercontent.com/` + rest.join("/"))});
LiteLoader.api.registerUrlHandler("plugininstaller", async (rest, all) => {
const url = `https://raw.githubusercontent.com/` + rest.join("/");
await initPluginData(url);
openPluginInfoWindow();
});
});

ipcMain.on("LiteLoader.plugininstaller.restart", (event) => { app.relaunch(); app.exit() });
ipcMain.on("LiteLoader.plugininstaller.restart", (event) => { app.relaunch(); app.exit(); });

ipcMain.handle("LiteLoader.plugininstaller.chackPluginUpdate", async (event, slug) => await chackPluginUpdate(slug));
ipcMain.handle("LiteLoader.plugininstaller.chackPluginUpdate", (event, slug) => chackPluginUpdate(slug));

ipcMain.on("LiteLoader.plugininstaller.installByUrl", (event, url) => openPluginInfoWindow(url));
ipcMain.on("LiteLoader.plugininstaller.installByUrl", async (event, url) => { await initPluginData(url); openPluginInfoWindow();});

ipcMain.on("LiteLoader.plugininstaller.updateBySlug", (event, slug) => openPluginInfoWindowBySlug(slug));

Expand All @@ -33,17 +37,34 @@ ipcMain.on("LiteLoader.plugininstaller.log", (event, level, ...args) => console[

ipcMain.on("LiteLoader.plugininstaller.WindowShow", (event) => BrowserWindow.fromWebContents(event.sender).show());

ipcMain.handle("LiteLoader.plugininstaller.initUpdatePluginData", (event, slug) => initUpdatePluginData(slug));

ipcMain.on("LiteLoader.plugininstaller.openPluginInfoWindow", (event) => openPluginInfoWindow());

async function initUpdatePluginData(slug) {
const isInstall = LiteLoader.plugins[slug] ? true : false;
if(!isInstall) {
console.error("[Plugininstaller openPluginInfoWindow]", slug, "Plugin not found");
return;
}
const plugin = LiteLoader.plugins[slug].manifest;
const url = `https://raw.githubusercontent.com/${plugin.repository.repo}/${plugin.repository.branch}/manifest.json`;
await initPluginData(url);
return plugin_data;
}

async function initPluginData(url) {
if (plugin_data && plugin_data.PIInfoUrl == url) return;
try {
plugin_data = await (await fetch(url, await fetchOptions())).json();
plugin_data.PIurl = plugin_data.repository?.release?.file ? `https://github.com/${plugin_data.repository.repo}/releases/latest/download/${plugin_data.repository?.release?.file}` : `https://github.com/${plugin_data.repository.repo}/archive/${plugin_data.repository.branch}.zip`;

plugin_data.PIUpdateBody = "# github API 超限无法获取更新日志";
// 当 github API 未被限制时使用 github API 获取下载地址
if(!apiLimit){
const downloadtemp = await (await fetch(`https://api.github.com/repos/${plugin_data.repository.repo}/releases/latest`, await fetchOptions())).json();
if(downloadtemp.message?.includes("API rate limit")){ apiLimit = true; }
if(downloadtemp.assets){ plugin_data.PIurl = downloadtemp.assets[0] ? downloadtemp.assets[0].browser_download_url : downloadtemp.zipball_url; }
if(downloadtemp.body){plugin_data.PIUpdateBody = downloadtemp.body;}
}

const isInstall = LiteLoader.plugins[plugin_data.slug] ? true : false;
Expand All @@ -58,8 +79,7 @@ async function initPluginData(url) {
}
}

async function openPluginInfoWindow(url) {
await initPluginData(url);
async function openPluginInfoWindow() {
const installWindow = new BrowserWindow({
frame: false,
resizable: false,
Expand All @@ -73,15 +93,16 @@ async function openPluginInfoWindow(url) {
installWindow.loadFile(path.join(__dirname, "../src/window/install.html"));
}

function openPluginInfoWindowBySlug(slug) {
async function openPluginInfoWindowBySlug(slug) {
const isInstall = LiteLoader.plugins[slug] ? true : false;
if(!isInstall) {
console.error("[Plugininstaller openPluginInfoWindow]", slug, "Plugin not found");
return;
}
const plugin = LiteLoader.plugins[slug].manifest;
const url = `https://raw.githubusercontent.com/${plugin.repository.repo}/${plugin.repository.branch}/manifest.json`;
openPluginInfoWindow(url);
await initPluginData(url);
openPluginInfoWindow();
}

async function chackPluginUpdate(slug) {
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion src/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ contextBridge.exposeInMainWorld("plugininstaller", {
openWeb: (url) => ipcRenderer.send("LiteLoader.plugininstaller.openWeb", url),
installByUrl: (url) => ipcRenderer.send("LiteLoader.plugininstaller.installByUrl", url),
updateBySlug: (slug) => ipcRenderer.send("LiteLoader.plugininstaller.updateBySlug", slug),
chackPluginUpdate: (slug) => ipcRenderer.invoke("LiteLoader.plugininstaller.chackPluginUpdate", slug)
chackPluginUpdate: (slug) => ipcRenderer.invoke("LiteLoader.plugininstaller.chackPluginUpdate", slug),
initUpdatePluginData: (slug) => ipcRenderer.invoke("LiteLoader.plugininstaller.initUpdatePluginData", slug),
openPluginInfoWindow: () => ipcRenderer.send("LiteLoader.plugininstaller.openPluginInfoWindow")
});
53 changes: 53 additions & 0 deletions src/render_modules/openChangeLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 以下内容来自 https://github.com/xiyuesaves/LiteLoaderQQNT-lite_tools/blob/v4/src/render_modules/openChangeLog.js
const updateMsgBox = `
<div class="lite-tools-mask">
<div class="lite-tools-update-msg-box">
<div class="title" title="{{title}}">{{title}}</div>
<div class="context">{{context}}</div>
<div class="bottom-solt">
<button class="q-button q-button--small q-button--primary update-btn">更新</button>
<!-- <button class="q-button q-button--small q-button--secondary detail-btn">详情</button> -->
<button class="q-button q-button--small q-button--secondary quite-btn">返回</button>
</div>
</div>
</div>`;

/**
* 显示更新日志,该模块只能在设置页面使用
* @param {String} html html字符串
* @param {Boolean} showDownloadBtn 是否显示下载按钮
*/
function openChangeLog(html) {
const newMsgBox = updateMsgBox.replace(/\{\{([^}]+)\}\}/g, (match, name) => {
switch (name) {
case "title":
return "更新日志";
case "context":
return html;
default:
return name;
}
});
document.querySelector(".update-view.plugininstaller").insertAdjacentHTML("beforeend", newMsgBox);
const showMsgBox = document.querySelector(".lite-tools-mask");
showMsgBox.offsetHeight;
showMsgBox.classList.add("show");
showMsgBox.querySelector(".quite-btn").addEventListener("click", () => {
showMsgBox.addEventListener("transitionend", () => {
showMsgBox.remove();
});
showMsgBox.classList.remove("show");
});
showMsgBox.querySelector(".update-btn").addEventListener("click", () => {
showMsgBox.addEventListener("transitionend", () => {
showMsgBox.remove();
});
showMsgBox.classList.remove("show");
plugininstaller.openPluginInfoWindow();
//lite_tools.updatePlugins(updateUrl);
});
//showMsgBox.querySelector(".detail-btn").addEventListener("click", () => {
// lite_tools.openWeb(detailUrl);
//});
}
export { openChangeLog };
103 changes: 103 additions & 0 deletions src/render_modules/simpleMarkdownToHTML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 以下内容来自 https://github.com/xiyuesaves/LiteLoaderQQNT-lite_tools/blob/v4/src/render_modules/simpleMarkdownToHTML.js
/**
* markdown转换为html
* @param {String} markdown markdown字符串
* @returns
*/
function simpleMarkdownToHTML(markdown, repository) {
const strs = markdown.split("\n");
let ulStack = 0;
let inBlockquote = 0;

return strs
.map((md) => {
// Headers
md = md.replace(/^# (.*$)/gim, "<h1>$1</h1>");
md = md.replace(/^## (.*$)/gim, "<h2>$1</h2>");
md = md.replace(/^### (.*$)/gim, "<h3>$1</h3>");
md = md.replace(/^#### (.*$)/gim, "<h4>$1</h4>");

// Inline code
md = md.replace(/`(.*?)`/g, "<code>$1</code>");

// Bold and Italic
md = md.replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>");
md = md.replace(/\*(.*?)\*/g, "<em>$1</em>");

// Strikethrough
md = md.replace(/~~(.*?)~~/g, "<del>$1</del>");

// Images
md = md.replace(/!\[(.*?)\]\((.*?)\)/g, '<img src="$2" alt="$1">');

// Links
md = md.replace(/\[(.*?)\]\((.*?)\)/g, '<a data-href="$2">$1</a>');

// GitHub Issue Links
md = md.replace(/#(\d+)/g, `<a data-href="https://github.com/${repository}/issues/$1">#$1</a>`);

md = md.replace(/@(\S+)/g, '<a data-href="https://github.com/$1">@$1</a>');

md = md.replace(/^\s*-\s(.*)$/gim, "<li>$1</li>");

if (md.includes("<li>") && ulStack) {
md = `<ul>\n${md}`;
ulStack++;
}

if (!md.includes("</li>") && !ulStack) {
md = `</ul>\n${md}`;
ulStack--;
}

// 引用
if (/^\s*>/.test(md)) {
const str = md.replace(/^\s*> ?/, "").replace(/\\/, "");
if (!inBlockquote) {
md = `<div class="markdown-alert ${getAlertType(str)}">\n${isTitle(str)}`;
inBlockquote++;
} else {
md = "<br>" + str;
}
} else if (inBlockquote) {
md = `</div>\n${md}`;
inBlockquote--;
}

return md;
})
.join("");
}

function getAlertType(str) {
const alertTypeMap = {
"[!NOTE]": "markdown-alert-note",
"[!IMPORTANT]": "markdown-alert-important",
"[!WARNING]": "markdown-alert-warning",
"[!TIP]": "markdown-alert-tip",
"[!CAUTION]": "markdown-alert-caution",
};

return alertTypeMap[str.trim()] || "";
}

function isTitle(str) {
const titleMap = {
"[!NOTE]": "Note",
"[!IMPORTANT]": "Important",
"[!WARNING]": "Warning",
"[!TIP]": "Tip",
"[!CAUTION]": "Caution",
};

const trimmedStr = str.trim();
const title = titleMap[trimmedStr];

if (title) {
return `<p class="markdown-alert-title">${title}</p>`;
}

return str;
}

export { simpleMarkdownToHTML };
18 changes: 16 additions & 2 deletions src/renderer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {openChangeLog} from "./render_modules/openChangeLog.js";
import {simpleMarkdownToHTML} from "./render_modules/simpleMarkdownToHTML.js";
const plugin_path = LiteLoader.plugins["plugininstaller"].path.plugin;

export const onSettingWindowCreated = async view => {
Expand Down Expand Up @@ -44,14 +46,26 @@ async function pluginupdate(view, slug) {
<span class="secondary-text">${plugin.PIoldversion} -> ${plugin.version}</span>
</div>
<div>
<button class="q-button q-button--small q-button--secondary update">更新</button>
<button class="q-button q-button--small q-button--secondary update">详情</button>
</div>
</setting-item>
`;
const dom = new DOMParser().parseFromString(temp, "text/html");
dom.querySelector(".update").addEventListener("click", debounce(() => plugininstaller.updateBySlug(plugin.slug), 500));
dom.querySelector(".update").addEventListener("click", debounce(async () => {
const d = await plugininstaller.initUpdatePluginData(plugin.slug);
openChangeLog(simpleMarkdownToHTML(d.PIUpdateBody, plugin.repository?.repo))
//plugininstaller.updateBySlug(plugin.slug)
}, 500));
update_list.appendChild(dom.querySelector("setting-item"));
})
view.addEventListener("click", (e) => {
if (e.target.tagName === "A") {
const href = e.target.getAttribute("data-href");
if (href) {
plugininstaller.openWeb(href);
}
}
});
}

function handleURL(url) {
Expand Down
Loading

0 comments on commit c26502e

Please sign in to comment.