updateItemKeyValue(index, key.value, val)
"
@@ -155,6 +158,7 @@ export default defineComponent({
return {
value: key.value || key,
label: key.label || key,
+ width: key.width,
};
}) || []
);
diff --git a/src/components/composer/common/VariableInput.vue b/src/components/composer/common/VariableInput.vue
index e81a173..17d8b8d 100644
--- a/src/components/composer/common/VariableInput.vue
+++ b/src/components/composer/common/VariableInput.vue
@@ -131,7 +131,7 @@
-
+
@@ -187,6 +187,7 @@ export default defineComponent({
},
label: String,
icon: String,
+ noIcon: Boolean,
options: {
type: Object,
default: () => ({}),
diff --git a/src/components/composer/ui/SelectListEditor.vue b/src/components/composer/ui/SelectListEditor.vue
index 2d4f0eb..15a2ab5 100644
--- a/src/components/composer/ui/SelectListEditor.vue
+++ b/src/components/composer/ui/SelectListEditor.vue
@@ -6,7 +6,7 @@
@@ -22,7 +22,7 @@
{
+ if (typeof val !== "string") val = JSON.stringify(val);
+ return {
+ value: val,
+ isString: type === "str",
+ __varInputVal__: true,
+ };
};
-const textDefaultSelects = {
- selects: new Array(3).fill({
- value: "",
- isString: true,
- __varInputVal__: true,
- }),
-};
+const jsonDefaultSelects = new Array(3).fill().map((_, index) => ({
+ id: newVarInputVal("var", index),
+ title: newVarInputVal("str"),
+ description: newVarInputVal("str"),
+}));
+
+const textDefaultSelects = new Array(3).fill(newVarInputVal("str"));
const defaultSelects = {
json: jsonDefaultSelects,
@@ -248,12 +247,25 @@ export default defineComponent({
},
},
methods: {
- updateArgvs(key, value) {
+ /**
+ * 更新参数
+ * @param {string|string[]} keys - 键名
+ * @param {string|string[]} values - 值
+ */
+ updateArgvs(keys, values) {
+ if (typeof keys === "string") {
+ keys = [keys];
+ values = [values];
+ }
const argvs = { ...this.argvs };
- const keys = key.split(".");
- const lastKey = keys.pop();
- const target = keys.reduce((obj, key) => obj[key], argvs);
- target[lastKey] = value;
+ // 更新每一个键
+ keys.forEach((key, index) => {
+ // 支持嵌套对象的键值
+ const subKeys = key.split(".");
+ const lastKey = subKeys.pop();
+ const target = subKeys.reduce((obj, key) => obj[key], argvs);
+ target[lastKey] = values[index];
+ });
this.updateModelValue(argvs);
},
generateCode(argvs = this.argvs) {
@@ -285,13 +297,16 @@ export default defineComponent({
try {
const result = parseFunction(code, {
- variableFormatPaths: ["arg0", "arg1.placeholder"],
+ variableFormatPaths: ["arg0", "arg0[*]", "arg1.placeholder"],
});
+
if (!result) return this.defaultArgvs;
const [selects, options = {}] = result.argvs;
+ const inputMode = selects.__varInputVal__ ? "variable" : "manual";
return {
...this.defaultArgvs,
+ inputMode,
selects,
...options,
};
@@ -318,20 +333,46 @@ export default defineComponent({
argvs,
});
},
+ handleOptionTypeChange(newOptionType) {
+ const oldOptionType = this.argvs.optionType;
+ // 原地点击不处理
+ if (oldOptionType === newOptionType) return;
+ // 变量输入模式不需要处理 selects
+ if (this.argvs.inputMode === "variable") {
+ this.updateArgvs("optionType", newOptionType);
+ return;
+ }
+ const oldSelects = this.argvs.selects;
+ let newSelects = oldSelects;
+ // 从JSON转换为非JSON时,取title或description
+ if (oldOptionType === "json") {
+ newSelects = oldSelects.map(
+ (item) => item.title || item.description || newVarInputVal("str")
+ );
+ } else if (newOptionType === "json") {
+ // 从非JSON转换为JSON时,添加title和description
+ newSelects = oldSelects.map((item) => ({
+ title: item,
+ description: item,
+ }));
+ }
+ this.updateArgvs(["optionType", "selects"], [newOptionType, newSelects]);
+ },
+ handleInputModeChange(newInputMode) {
+ let newSelects = this.argvs.selects;
+ if (newInputMode === "variable") {
+ newSelects = newVarInputVal("var");
+ } else {
+ newSelects = defaultSelects[this.argvs.optionType];
+ }
+ this.updateArgvs(["inputMode", "selects"], [newInputMode, newSelects]);
+ },
},
mounted() {
if (!this.modelValue.argvs && !this.modelValue.code) {
this.updateModelValue(this.defaultArgvs);
}
},
- watch: {
- "argvs.optionType": {
- immediate: true,
- handler(newVal) {
- this.argvs.selects = defaultSelects[newVal];
- },
- },
- },
});
diff --git a/src/js/composer/formatString.js b/src/js/composer/formatString.js
index 6360239..1f531cd 100644
--- a/src/js/composer/formatString.js
+++ b/src/js/composer/formatString.js
@@ -306,9 +306,37 @@ export const parseFunction = (functionStr, options = {}) => {
return obj;
}, {});
case "ArrayExpression":
- return node.elements.map((element, index) =>
- processNode(element, `${currentPath}[${index}]`)
- );
+ return node.elements.map((element, index) => {
+ const elementPath = `${currentPath}[${index}]`;
+ const processedElement = processNode(element, elementPath);
+
+ if (
+ shouldUseVariableFormat &&
+ typeof processedElement === "object"
+ ) {
+ return Object.entries(processedElement).reduce(
+ (acc, [key, value]) => {
+ // 如果值已经是 varInputVal 格式,直接使用
+ if (value?.__varInputVal__) {
+ acc[key] = value;
+ } else {
+ // 否则转换为 varInputVal 格式
+ acc[key] = {
+ value:
+ typeof value === "string"
+ ? value
+ : JSON.stringify(value),
+ isString: typeof value === "string",
+ __varInputVal__: true,
+ };
+ }
+ return acc;
+ },
+ {}
+ );
+ }
+ return processedElement;
+ });
case "ObjectProperty":
return processNode(node.value, currentPath);
case "MemberExpression":