mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-12-20 19:20:37 +08:00
调整子流程标签栏样式,支持重命名
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
export function generateCode(commandFlow, functionName = null) {
|
||||
export function generateCode(flow) {
|
||||
const { commands, name, label } = flow;
|
||||
// 检查是否包含异步函数
|
||||
const hasAsyncFunction = commandFlow.some((cmd) => cmd.isAsync);
|
||||
const hasAsyncFunction = commands.some((cmd) => cmd.isAsync);
|
||||
|
||||
let code = [];
|
||||
const funcName = functionName || "run";
|
||||
const funcName = name || "func_" + new Date().getTime();
|
||||
|
||||
code.push(`// ${label}`);
|
||||
// 生成函数声明
|
||||
code.push(`${hasAsyncFunction ? "async " : ""}function ${funcName}() {`);
|
||||
|
||||
const indent = " ";
|
||||
|
||||
commandFlow.forEach((cmd) => {
|
||||
commands.forEach((cmd) => {
|
||||
// 跳过禁用的命令
|
||||
if (cmd.disabled) return;
|
||||
if (!cmd.code) return;
|
||||
@@ -28,7 +30,7 @@ export function generateCode(commandFlow, functionName = null) {
|
||||
code.push("}"); // Close the function
|
||||
|
||||
// 如果是主函数,则自动执行
|
||||
if (functionName === "main") {
|
||||
if (funcName === "main") {
|
||||
code.push("\nmain();"); // Call the main function
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import { validateVariableName } from "js/common/variableValidator";
|
||||
* @param {number} varCount - 当前变量数量
|
||||
* @returns {string} 随机后缀
|
||||
*/
|
||||
function generateRandomSuffix(varCount) {
|
||||
function generateRandomSuffix(varCount, withPrefix = true) {
|
||||
// 根据变量数量决定后缀长度
|
||||
let length = 1; // 默认1位
|
||||
if (varCount >= 100) {
|
||||
@@ -15,7 +15,7 @@ function generateRandomSuffix(varCount) {
|
||||
}
|
||||
|
||||
return (
|
||||
"_" +
|
||||
(withPrefix ? "_" : "") +
|
||||
Math.random()
|
||||
.toString(36)
|
||||
.substring(2, 2 + length)
|
||||
@@ -66,18 +66,19 @@ function generateValidVarName(baseName, existingVars, currentName = null) {
|
||||
}
|
||||
|
||||
// 如果变量名重复,添加随机后缀直到不重复
|
||||
let finalName = baseName;
|
||||
while (existingVars.includes(finalName)) {
|
||||
let suffix;
|
||||
do {
|
||||
suffix = generateRandomSuffix(existingVars.length);
|
||||
} while (existingVars.includes(baseName + suffix));
|
||||
finalName = baseName + suffix;
|
||||
}
|
||||
let finalName = baseName + generateUniqSuffix(baseName, existingVars);
|
||||
|
||||
return finalName;
|
||||
}
|
||||
|
||||
export function generateUniqSuffix(baseName, existingVars, withPrefix = true) {
|
||||
let suffix;
|
||||
do {
|
||||
suffix = generateRandomSuffix(existingVars.length, withPrefix);
|
||||
} while (existingVars.includes(baseName + suffix));
|
||||
return suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理变量更新
|
||||
* @param {Object} params - 参数对象
|
||||
|
||||
Reference in New Issue
Block a user