添加OptionEditor组件,优化参数传递方式

This commit is contained in:
fofolee
2025-01-10 12:15:04 +08:00
parent ada0d2b968
commit 41b3501945
26 changed files with 764 additions and 478 deletions

View File

@@ -7,27 +7,27 @@ let currentAudio = null;
// 系统音效映射
const SYSTEM_SOUNDS = {
beep: {
win: "Beep",
win: "Windows Ding",
mac: "Ping.aiff",
},
error: {
win: "Asterisk",
win: "Windows Critical Stop",
mac: "Basso.aiff",
},
warning: {
win: "Exclamation",
win: "Windows Exclamation",
mac: "Sosumi.aiff",
},
notification: {
win: "Notification",
win: "Windows Notify",
mac: "Glass.aiff",
},
complete: {
win: "SystemAsterisk",
win: "Windows Print complete",
mac: "Hero.aiff",
},
click: {
win: "MenuCommand",
win: "Windows Navigation Start",
mac: "Tink.aiff",
},
};
@@ -40,7 +40,7 @@ const SYSTEM_SOUNDS = {
* @param {boolean} autoplay 是否自动播放
*/
async function play(file, volume = 1, loop = false, autoplay = true) {
// 停止当前音频
// 停止当前音频
stop();
// 检查文件是否存在
@@ -50,6 +50,8 @@ async function play(file, volume = 1, loop = false, autoplay = true) {
// 创建新的音频实例
const audio = new Audio();
// 设置音频属性
audio.src = `file://${file}`;
audio.volume = parseFloat(volume) || 1;
audio.loop = !!loop;
@@ -59,22 +61,33 @@ async function play(file, volume = 1, loop = false, autoplay = true) {
// 如果设置了自动播放
if (autoplay !== false) {
audio.play();
try {
await audio.play();
} catch (error) {
console.warn("播放失败:", error);
currentAudio = null;
throw error;
}
}
// 返回 Promise在播放结束时 resolve
return new Promise((resolve, reject) => {
audio.onended = () => {
if (!audio.loop) {
currentAudio = null;
resolve();
}
};
audio.onerror = (error) => {
// 立即返回,不等待播放完成
return Promise.resolve();
}
/**
* 停止音频播放
*/
function stop() {
if (currentAudio) {
try {
currentAudio.pause();
currentAudio.currentTime = 0;
} catch (error) {
console.warn("停止播放时发生错误:", error);
} finally {
currentAudio = null;
reject(error);
};
});
}
}
}
/**
@@ -86,8 +99,8 @@ async function beep(type = "beep", volume = 1) {
// 在 Windows 上使用 PowerShell 播放系统音效
if (process.platform === "win32") {
const soundName = SYSTEM_SOUNDS[type]?.win || SYSTEM_SOUNDS.beep.win;
// 使用系统音效
const script = `[System.Media.SystemSounds]::${soundName}.Play()`;
// 使用 PowerShell 播放 Windows 系统音效
const script = `(New-Object System.Media.SoundPlayer "C:\\Windows\\Media\\${soundName}.wav").PlaySync()`;
return new Promise((resolve, reject) => {
const ps = spawn("powershell", ["-Command", script]);
@@ -120,17 +133,6 @@ async function beep(type = "beep", volume = 1) {
}
}
/**
* 停止音频播放
*/
function stop() {
if (currentAudio) {
currentAudio.pause();
currentAudio.currentTime = 0;
currentAudio = null;
}
}
/**
* 分析音频文件
* @param {string} file 音频文件路径

View File

@@ -5,7 +5,7 @@
*/
function evaluate(expression) {
// 移除所有空白字符
expression = expression.replace(/\s+/g, "");
expression = expression.toString().replace(/\s+/g, "");
// 检查表达式是否为空
if (!expression) {
@@ -31,20 +31,87 @@ function evaluate(expression) {
}
try {
// 将表达式中的数字转换为整数计算,使用固定精度
const scale = Math.pow(10, 15); // 使用固定的15位精度
const processedExpression = expression.replace(/\d*\.?\d+/g, (match) => {
const num = parseFloat(match);
return Math.round(num * scale);
});
// 使用 Function 构造函数创建一个安全的计算环境
const result = new Function(`return ${expression}`)();
const result = new Function(`return ${processedExpression}`)();
// 检查结果是否为有限数
if (!Number.isFinite(result)) {
throw new Error("计算结果无效");
}
return result;
// 还原小数位数
return result / scale;
} catch (error) {
throw new Error("表达式计算错误: " + error.message);
}
}
/**
* 处理两个数的加法,避免浮点数精度问题
* @param {number} a 第一个数
* @param {number} b 第二个数
* @returns {number} 计算结果
*/
function add(a, b) {
const precision = Math.max(
(a.toString().split(".")[1] || "").length,
(b.toString().split(".")[1] || "").length
);
const scale = Math.pow(10, precision);
return (Math.round(a * scale) + Math.round(b * scale)) / scale;
}
/**
* 处理两个数的减法,避免浮点数精度问题
* @param {number} a 第一个数
* @param {number} b 第二个数
* @returns {number} 计算结果
*/
function subtract(a, b) {
const precision = Math.max(
(a.toString().split(".")[1] || "").length,
(b.toString().split(".")[1] || "").length
);
const scale = Math.pow(10, precision);
return (Math.round(a * scale) - Math.round(b * scale)) / scale;
}
/**
* 处理两个数的乘法,避免浮点数精度问题
* @param {number} a 第一个数
* @param {number} b 第二个数
* @returns {number} 计算结果
*/
function multiply(a, b) {
const precision =
(a.toString().split(".")[1] || "").length +
(b.toString().split(".")[1] || "").length;
const scale = Math.pow(10, precision);
return (Math.round(a * scale) * Math.round(b * scale)) / (scale * scale);
}
/**
* 处理两个数的除法,避免浮点数精度问题
* @param {number} a 第一个数
* @param {number} b 第二个数
* @param {number} [precision=10] 精度
* @returns {number} 计算结果
*/
function divide(a, b, precision = 10) {
if (b === 0) {
throw new Error("除数不能为零");
}
const scale = Math.pow(10, precision);
return Math.round(a * scale) / Math.round(b * scale);
}
/**
* 计算阶乘
* @param {number} n 非负整数
@@ -74,7 +141,11 @@ function factorial(n) {
* @returns {number} 绝对值
*/
function abs(x) {
return Math.abs(x);
// 将数字转换为整数计算,使用固定精度
const scale = Math.pow(10, 15); // 使用固定的15位精度
const num = parseFloat(x.toString());
const result = Math.abs(Math.round(num * scale));
return result / scale;
}
/**