UNPKG

@plasosdk/client-sdk

Version:

Plaso教育平台的客户端SDK

337 lines (329 loc) 15.3 kB
/****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; // 创建一个变量来存储ffmpeg模块 var ffmpegModuleInstance = null; var init = function (remoteMain, ffmpegModule, ffmpegPath) { //@ts-ignore var _a = require("electron"), app = _a.app, BrowserWindow = _a.BrowserWindow, ipcMain = _a.ipcMain, webContents = _a.webContents; var Store = require("electron-store"); var path = require("path"); var fs = require("fs"); var EventEmitter = require("events").EventEmitter; var emitter = new EventEmitter(); // 添加缺失的变量声明 var args = process.argv; var manifest = require("../package.json"); var exec = require("child_process").exec; var store = new Store(); // 初始化 FFmpeg 模块 var initFFmpeg = function () { // 如果外部传入了模块,直接使用 if (ffmpegModule && ffmpegPath) { try { ffmpegModule.setFfmpegPath(ffmpegPath); ffmpegModuleInstance = ffmpegModule; console.log("FFmpeg loaded successfully from external modules, path:", ffmpegPath); return true; } catch (error) { console.error("Error setting FFmpeg path with external modules:", error); return false; } } // 否则尝试从 require 加载 try { var ffmpegTemp = require("fluent-ffmpeg"); var ffmpegStaticPath = require("ffmpeg-static"); ffmpegTemp.setFfmpegPath(ffmpegStaticPath); ffmpegModuleInstance = ffmpegTemp; console.log("FFmpeg loaded successfully via require, path:", ffmpegStaticPath); return true; } catch (error) { console.error("FFmpeg module loading error:", error); console.error("Please ensure fluent-ffmpeg and ffmpeg-static are installed in your project, or pass them as parameters to init()"); return false; } }; // 初始化 FFmpeg initFFmpeg(); ipcMain.on("enable-remote", function (event, webContentsId) { var target = webContents.fromId(webContentsId); remoteMain.enable(target); // 主进程操作‌:ml-citation{ref="4" data="citationList"} }); // 你包内部收到 relaunch 命令时 function handleRelaunch(args) { // ...你自己的逻辑... // 通知外部 emitter.emit("relaunch", args); } // 目前使用了第三个 --- relaunch ipcMain.on("synchronous-message", function (event, arg) { var cmd = arg, params = []; if (typeof arg == "object" && !Array.isArray(arg)) { cmd = arg.cmd; params = arg.params; } switch (cmd) { case "argv": event.returnValue = args; break; case "manifest": event.returnValue = manifest; break; case "relaunch": handleRelaunch(args); break; case "quit": app.exit(); break; case "shareToQQ": { var shareWin = new BrowserWindow(); try { require("@electron/remote/main").enable(shareWin.webContents); } catch (error) { console.error("Module not found", "Please run `npm install @electron/remote` to enable remote module and update electron version to 22.0.0 or higher"); } shareWin.loadURL(params); } break; default: event.returnValue = "no such command!!"; break; } }); ipcMain.handle("getStoreValue", function (event, key) { return store.get(key); }); ipcMain.handle("setStoreValue", function (event, key, value) { store.set(key, value); return true; }); // 转换wav to mp3 ipcMain.handle("convert-audio", function (event, _a) { var inputPath = _a.inputPath, outputPath = _a.outputPath; console.log("Convert-audio called with:", { inputPath: inputPath, outputPath: outputPath }); // 检查参数 if (!inputPath || !outputPath) { console.error("Missing required parameters"); return Promise.reject(new Error("Missing required parameters: inputPath or outputPath")); } // 检查ffmpeg是否正确加载 if (!ffmpegModuleInstance) { console.error("FFmpeg module not loaded"); return Promise.reject(new Error("FFmpeg module not loaded. Please install fluent-ffmpeg and ffmpeg-static packages.")); } // 检查输入文件是否存在 try { if (!fs.existsSync(inputPath)) { console.error("Input file does not exist:", inputPath); return Promise.reject(new Error("Input file does not exist: ".concat(inputPath))); } } catch (error) { console.error("Error checking input file:", error); return Promise.reject(error); } // 确保输出目录存在 try { var outputDir = path.dirname(outputPath); if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }); console.log("Created output directory:", outputDir); } } catch (error) { console.error("Error creating output directory:", error); return Promise.reject(error); } console.log("Starting audio conversion..."); return new Promise(function (resolve, reject) { ffmpegModuleInstance() .input(inputPath) .audioCodec("libmp3lame") .audioBitrate(192) .toFormat("mp3") .on("start", function (commandLine) { console.log("FFmpeg process started:", commandLine); }) .on("progress", function (progress) { console.log("Processing: ".concat(Math.floor(progress.percent), "% done")); }) .on("end", function () { console.log("Conversion completed successfully"); resolve(outputPath); }) .on("error", function (err) { console.error("Conversion error:", err); reject(err); }) .save(outputPath); }); }); // 获取进程 var getData = function (type) { var cmd = type === "mac" ? "ps -Aco command" : "tasklist"; return new Promise(function (resolve, reject) { exec(cmd, function (err, stdout) { if (err) { return console.error(err); } if (stdout) { resolve(stdout); } }); }); }; ipcMain.handle("getSystemProcess", function (event, type) { return __awaiter(void 0, void 0, void 0, function () { var data; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, getData(type)]; case 1: data = _a.sent(); return [2 /*return*/, data]; } }); }); }); ipcMain.handle("open-new-window", function (event, params) { var _a; var parentWindow = null; if (params === null || params === void 0 ? void 0 : params.parentWindowId) { parentWindow = BrowserWindow.fromId(params.parentWindowId); } // 默认尺寸 var width = 800; var height = 600; // 如果需要和父窗口一样大 if ((params === null || params === void 0 ? void 0 : params.matchParentSize) && parentWindow) { _a = parentWindow.getSize(), width = _a[0], height = _a[1]; } var win = new BrowserWindow({ width: width, height: height, frame: (params === null || params === void 0 ? void 0 : params.frameless) ? false : true, titleBarStyle: (params === null || params === void 0 ? void 0 : params.frameless) ? "hidden" : "default", alwaysOnTop: (params === null || params === void 0 ? void 0 : params.alwaysOnTop) ? true : false, parent: parentWindow, modal: (params === null || params === void 0 ? void 0 : params.modal) ? true : false, transparent: (params === null || params === void 0 ? void 0 : params.transparent) ? true : false, backgroundColor: (params === null || params === void 0 ? void 0 : params.transparent) ? "#00ffffff" : "#ffffff", webPreferences: { nodeIntegration: true, contextIsolation: false, webviewTag: true, webSecurity: false, devTools: true, }, }); win.setMenu(null); remoteMain.enable(win.webContents); // 居中窗口 win.center(); if (params) { win.loadURL(params.url); } else { win.loadURL("about:blank"); } if (params.debug) { win.webContents.openDevTools(); } // 关键:监听父窗口移动,同步子窗口位置和尺寸 if (parentWindow && !(params === null || params === void 0 ? void 0 : params.modal)) { // 记录初始尺寸 var _b = parentWindow.getSize(), parentInitWidth = _b[0], parentInitHeight = _b[1]; var _c = win.getSize(), childInitWidth = _c[0], childInitHeight = _c[1]; var widthRatio_1 = childInitWidth / parentInitWidth; var heightRatio_1 = childInitHeight / parentInitHeight; var syncPositionAndSize = function () { var _a = parentWindow.getPosition(), parentX = _a[0], parentY = _a[1]; var _b = parentWindow.getSize(), parentWidth = _b[0], parentHeight = _b[1]; var childWidth, childHeight; if (params === null || params === void 0 ? void 0 : params.matchParentSize) { childWidth = parentWidth; childHeight = parentHeight; } else { // 按比例缩放子窗口 childWidth = Math.round(parentWidth * widthRatio_1); childHeight = Math.round(parentHeight * heightRatio_1); } win.setSize(childWidth, childHeight); // 居中于父窗口 var x = parentX + Math.round((parentWidth - childWidth) / 2); var y = parentY + Math.round((parentHeight - childHeight) / 2); win.setPosition(x, y); }; parentWindow.on("move", syncPositionAndSize); parentWindow.on("resize", syncPositionAndSize); // 初始同步一次 syncPositionAndSize(); // 父窗口关闭时,子窗口也关闭 parentWindow.on("closed", function () { if (!win.isDestroyed()) win.close(); }); } // 页面加载完成后再显示窗口 win.webContents.once("did-finish-load", function () { win.show(); }); return { windowId: win.id, webContentsId: win.webContents.id }; }); return emitter; }; export { init }; //# sourceMappingURL=electronCommon.esm.js.map