统一VarInput变量的管理

This commit is contained in:
fofolee
2025-01-08 14:39:08 +08:00
parent b528cfa97d
commit 7a43695e2d
24 changed files with 190 additions and 516 deletions

View File

@@ -1,3 +1,5 @@
import { newVarInputVal } from "js/composer/varInputValManager";
export const dataCommands = {
label: "数据处理",
icon: "format_color_text",
@@ -552,13 +554,7 @@ export const dataCommands = {
type: "arrayEditor",
icon: "memory",
width: 12,
defaultValue: [
{
value: "",
isString: false,
__varInputVal__: true,
},
],
defaultValue: [newVarInputVal("var")],
},
{
label: "总长度(可选)",

View File

@@ -1,3 +1,5 @@
import { newVarInputVal } from "js/composer/varInputValManager";
export const uiCommands = {
label: "UI操作",
icon: "web",
@@ -14,16 +16,8 @@ export const uiCommands = {
label: "按钮组",
type: "arrayEditor",
defaultValue: [
{
value: "是",
isString: true,
__varInputVal__: true,
},
{
value: "否",
isString: true,
__varInputVal__: true,
},
newVarInputVal("str", "是"),
newVarInputVal("str", "否"),
],
},
],
@@ -53,16 +47,8 @@ export const uiCommands = {
},
defaultValue: [
{
label: {
value: "请输入",
isString: true,
__varInputVal__: true,
},
value: {
value: "",
isString: true,
__varInputVal__: true,
},
label: newVarInputVal("str", "请输入"),
value: newVarInputVal("str"),
},
],
},

View File

@@ -249,8 +249,7 @@ const customComponentGuide = {
parseToHasType: {
description: "将字符串解析为带类型的值",
usage: "用于解析 VariableInput 类型的值",
example:
"将 '\"text\"' 解析为 {value: 'text', isString: true, __varInputVal__: true,}",
example: "将 '\"text\"' 解析为 newVarInputVal('str', 'text')",
},
},
},
@@ -292,7 +291,7 @@ const customComponentGuide = {
description: "变量输入组件",
usage: "用于输入可能包含变量的字符串",
props: [
"model-value - 输入值,需要包含 value、isString、__varInputVal__ 属性",
"model-value - 输入值,需要包含 value、isString、__varInputVal__ 属性, 通过 varInputValManager 的 newVarInputVal 创建",
"label - 输入框标签",
"icon - 输入框图标",
],

View File

@@ -1,13 +1,9 @@
import { parse } from "@babel/parser";
/**
* 处理带有 __varInputVal__ 属性的对象
* @param {Object} argv 要处理的对象
* @returns {string} 处理后的字符串
*/
const stringifyVarInputVal = (argv) => {
return argv.isString ? `"${argv.value}"` : argv.value;
};
import {
stringifyVarInputVal,
isVarInputVal,
newVarInputVal,
} from "./varInputValManager";
/**
* 递归移除对象中的空值
@@ -17,12 +13,10 @@ const stringifyVarInputVal = (argv) => {
const removeEmptyValues = (obj) => {
return window.lodashM.omitBy(obj, (value) => {
// 如果value是VariableInput的输出则取其value值
const realValue = value?.hasOwnProperty("__varInputVal__")
? value.value
: value;
const realValue = isVarInputVal(value) ? value.value : value;
if (window.lodashM.isNil(realValue) || realValue === "") return true;
// 如果value是对象并且不是VariableInput的输出则递归移除空值
if (typeof value === "object" && !value.hasOwnProperty("__varInputVal__"))
if (typeof value === "object" && !isVarInputVal(value))
return window.lodashM.isEmpty(removeEmptyValues(value));
return false;
});
@@ -38,7 +32,7 @@ const processObject = (obj, parentPath = "") => {
// 移除空值
obj = removeEmptyValues(obj);
// 如果是 VariableInput 的输出,直接用 stringifyVarInputVal 处理
if (obj?.hasOwnProperty("__varInputVal__")) {
if (isVarInputVal(obj)) {
return stringifyVarInputVal(obj);
}
@@ -51,7 +45,7 @@ const processObject = (obj, parentPath = "") => {
// 处理对象类型
if (value && typeof value === "object") {
// 如果是 VariableInput 的输出,直接用 stringifyVarInputVal 处理
if (value.hasOwnProperty("__varInputVal__")) {
if (isVarInputVal(value)) {
valueStr = stringifyVarInputVal(value);
} else {
valueStr = processObject(value, parentPath + " ");
@@ -83,7 +77,7 @@ const processObject = (obj, parentPath = "") => {
* @returns {string} 格式化后的JSON字符串
*/
const stringifyObject = (jsonObj) => {
if (jsonObj?.hasOwnProperty("__varInputVal__")) {
if (isVarInputVal(jsonObj)) {
return stringifyVarInputVal(jsonObj);
}
if (jsonObj instanceof Array) {
@@ -126,23 +120,11 @@ export const stringifyArgv = (argv) => {
*/
export const parseToHasType = (str) => {
if (!str) {
return {
value: "",
isString: true,
__varInputVal__: true,
};
return newVarInputVal("str", "");
}
return str.startsWith('"') && str.endsWith('"')
? {
value: str.slice(1, -1),
isString: true,
__varInputVal__: true,
}
: {
value: str,
isString: false,
__varInputVal__: true,
};
? newVarInputVal("str", str.slice(1, -1))
: newVarInputVal("var", str);
};
/**
@@ -272,31 +254,23 @@ export const parseFunction = (functionStr, options = {}) => {
case "StringLiteral":
// 字符串字面量总是带引号的
return shouldUseVariableFormat
? { value: node.value, isString: true, __varInputVal__: true }
? newVarInputVal("str", node.value)
: node.value;
// 数字、布尔
case "NumericLiteral":
case "BooleanLiteral":
return shouldUseVariableFormat
? {
value: JSON.stringify(node.value),
isString: false,
__varInputVal__: true,
}
? newVarInputVal("var", JSON.stringify(node.value))
: node.value;
// null
case "NullLiteral":
return shouldUseVariableFormat
? {
value: "null",
isString: false,
__varInputVal__: true,
}
? newVarInputVal("var", "null")
: null;
case "Identifier":
// 标识符(变量)总是不带引号的
return shouldUseVariableFormat
? { value: node.name, isString: false, __varInputVal__: true }
? newVarInputVal("var", node.name)
: node.name;
case "ObjectExpression":
return node.properties.reduce((obj, prop) => {
@@ -317,18 +291,14 @@ export const parseFunction = (functionStr, options = {}) => {
return Object.entries(processedElement).reduce(
(acc, [key, value]) => {
// 如果值已经是 varInputVal 格式,直接使用
if (value?.__varInputVal__) {
if (isVarInputVal(value)) {
acc[key] = value;
} else {
// 否则转换为 varInputVal 格式
acc[key] = {
value:
typeof value === "string"
? value
: JSON.stringify(value),
isString: typeof value === "string",
__varInputVal__: true,
};
acc[key] = newVarInputVal(
typeof value === "string" ? "str" : "var",
typeof value === "string" ? value : JSON.stringify(value)
);
}
return acc;
},

View File

@@ -1,3 +1,5 @@
import { newVarInputVal } from "js/composer/varInputValManager";
// ubrowser 浏览器操作配置
export const ubrowserOperationConfigs = [
{
@@ -565,22 +567,10 @@ const defaultUBrowserRunConfigs = {
export const defaultUBrowserConfigs = {
// 基础参数
goto: {
url: {
value: "",
isString: true,
__varInputVal__: true,
},
url: newVarInputVal("str"),
headers: {
Referer: {
value: "",
isString: true,
__varInputVal__: true,
},
userAgent: {
value: "",
isString: true,
__varInputVal__: true,
},
Referer: newVarInputVal("str"),
userAgent: newVarInputVal("str"),
},
timeout: 60000,
},
@@ -590,183 +580,87 @@ export const defaultUBrowserConfigs = {
timeout: 60000,
},
click: {
selector: {
value: "",
isString: true,
__varInputVal__: true,
},
selector: newVarInputVal("str"),
},
css: {
value: {
value: "",
isString: true,
__varInputVal__: true,
},
value: newVarInputVal("str"),
},
press: {
key: {
value: "",
isString: true,
__varInputVal__: true,
},
key: newVarInputVal("str"),
modifiers: [],
},
paste: {
text: {
value: "",
isString: true,
__varInputVal__: true,
},
text: newVarInputVal("str"),
},
screenshot: {
selector: {
value: "",
isString: true,
__varInputVal__: true,
},
selector: newVarInputVal("str"),
rect: { x: 0, y: 0, width: 0, height: 0 },
savePath: {
value: "",
isString: true,
__varInputVal__: true,
},
savePath: newVarInputVal("str"),
},
pdf: {
options: {
marginsType: 0,
pageSize: "A4",
},
savePath: {
value: "",
isString: true,
__varInputVal__: true,
},
savePath: newVarInputVal("str"),
},
device: {
size: { width: 1280, height: 800 },
useragent: {
value: "",
isString: true,
__varInputVal__: true,
},
useragent: newVarInputVal("str"),
},
cookies: {
name: {
value: "",
isString: true,
__varInputVal__: true,
},
name: newVarInputVal("str"),
},
setCookies: {
items: [
{
name: {
value: "",
isString: true,
__varInputVal__: true,
},
value: {
value: "",
isString: true,
__varInputVal__: true,
},
name: newVarInputVal("str"),
value: newVarInputVal("str"),
},
],
},
removeCookies: {
name: {
value: "",
isString: true,
__varInputVal__: true,
},
name: newVarInputVal("str"),
},
clearCookies: {
url: {
value: "",
isString: true,
__varInputVal__: true,
},
url: newVarInputVal("str"),
},
evaluate: {
function: "",
params: [],
},
when: {
condition: {
value: "",
isString: false,
__varInputVal__: true,
},
condition: newVarInputVal("var"),
},
mousedown: {
selector: {
value: "",
isString: true,
__varInputVal__: true,
},
selector: newVarInputVal("str"),
},
mouseup: {
selector: {
value: "",
isString: true,
__varInputVal__: true,
},
selector: newVarInputVal("str"),
},
file: {
selector: {
value: "",
isString: true,
__varInputVal__: true,
},
selector: newVarInputVal("str"),
files: [],
},
value: {
selector: {
value: "",
isString: true,
__varInputVal__: true,
},
value: {
value: "",
isString: true,
__varInputVal__: true,
},
selector: newVarInputVal("str"),
value: newVarInputVal("str"),
},
check: {
selector: {
value: "",
isString: true,
__varInputVal__: true,
},
selector: newVarInputVal("str"),
checked: false,
},
focus: {
selector: {
value: "",
isString: true,
__varInputVal__: true,
},
selector: newVarInputVal("str"),
},
scroll: {
target: {
value: "",
isString: true,
__varInputVal__: true,
},
target: newVarInputVal("str"),
x: 0,
y: 0,
},
download: {
url: {
value: "",
isString: true,
__varInputVal__: true,
},
savePath: {
value: "",
isString: true,
__varInputVal__: true,
},
url: newVarInputVal("str"),
savePath: newVarInputVal("str"),
},
// 运行参数
run: defaultUBrowserRunConfigs,

View File

@@ -0,0 +1,41 @@
/**
* 创建一个 VariableInput 对象
* @param {string} type - 变量类型,默认为 "str"
* @param {string} val - 变量值
* @returns {Object} VariableInput 对象
*/
export const newVarInputVal = (type = "str", val = "") => {
if (typeof val !== "string") val = JSON.stringify(val);
return {
value: val,
isString: type === "str",
__varInputVal__: true,
};
};
/**
* 检查一个对象是否是 VariableInput 对象
* @param {Object} val - 要检查的对象
* @returns {boolean} 是否是 VariableInput 对象
*/
export const isVarInputVal = (val) => {
return val && val.__varInputVal__;
};
/**
* 创建一个空的 VariableInput 对象
* @param {string} type - 变量类型,默认为 "str"
* @returns {Object} 空的 VariableInput 对象
*/
export const newEmptyVarInputVal = (type = "str") => {
return newVarInputVal(type, "");
};
/**
* 处理带有 __varInputVal__ 属性的对象
* @param {Object} argv 要处理的对象
* @returns {string} 处理后的字符串
*/
export const stringifyVarInputVal = (argv) => {
return argv.isString ? `"${argv.value}"` : argv.value;
};